Exemplo n.º 1
0
        public static int[] Get_propertyIndexes <T>(PropertyInfo[] propInfo, string[] key_propertyNames)
        {
            int key_length = key_propertyNames.Length;

            int[]    propertyIndexes = new int[key_length];
            string[] propInfo_names  = new string[propInfo.Length];

            for (int i = 0; i < propInfo.Length; i++)
            {
                propInfo_names[i] = propInfo[i].Name;
            }

            for (int i = 0; i < key_length; i++)
            {
                int index = Array.IndexOf(propInfo_names, key_propertyNames[i]);
                if (index >= 0)
                {
                    propertyIndexes[i] = index;
                }
                if (index < 0)
                {
                    Report_class.Write_error_line("{0}: propertyName \"{1}\" does not exist", typeof(T).Name, key_propertyNames[i]);
                    throw new Exception();
                }
            }
            return(propertyIndexes);
        }
Exemplo n.º 2
0
        public static int[] Get_propertyIndexes_of_corresponding_given_columnNames <T>(PropertyInfo[] propInfo, string[] propertyNames, string[] given_columnNames, string[] search_given_columnNames)
        {
            int search_length = search_given_columnNames.Length;

            int[] columnNames_indexes = new int[search_length];
            if (search_length == 0)
            {
                Report_class.Write_error_line("{0}: no search columnNames to search for", typeof(T).Name);
                throw new Exception();
            }
            for (int i = 0; i < search_length; i++)
            {
                int index = Array.IndexOf(given_columnNames, search_given_columnNames[i]);
                if (index >= 0)
                {
                    columnNames_indexes[i] = index;
                }
                else
                {
                    Report_class.Write_error_line("{0}: given_columnName \"{1}\" does not exist", typeof(T).Name, given_columnNames[i]);
                    throw new Exception();
                }
            }
            string[] corresponding_propertyNames = new string[search_length];
            for (int indexS = 0; indexS < search_length; indexS++)
            {
                corresponding_propertyNames[indexS] = propertyNames[columnNames_indexes[indexS]];
            }
            int[] propertyIndexes = Get_propertyIndexes <T>(propInfo, corresponding_propertyNames);
            return(propertyIndexes);
        }
Exemplo n.º 3
0
        private bool Check_if_n_not_larger_than_max_size(int a, int b, int c, int d)
        {
            bool smaller = true;
            int  n       = a + b + c + d;

            if (n > max_size + 1)
            {
                Report_class.Write_error_line("{0}: n ({1}) is larger than max_size ({2}): initialize new fisher exact test instance", typeof(Fisher_exact_test_class).Name, n, max_size);
                smaller = false;
            }
            return(smaller);
        }
        public NetworkNode_line_class Get_indexed_node_line_if_index_is_correct(int indexNode)
        {
            NetworkNode_line_class node_line = Nodes[indexNode];

            if (node_line.NW_index != indexNode)
            {
                node_line = new NetworkNode_line_class();
                Report_class.Write_error_line("{0}: Get indexed node line, Indexes do not match ({1} <-> {2})", typeof(NetworkNode_line_class).Name, indexNode, node_line.NW_index);
                throw new Exception();
            }
            return(node_line);
        }
Exemplo n.º 5
0
 public static T[] Deep_copy_array <T>(T[] array)
 {
     T[] copy = new T[0];
     if (typeof(T) == typeof(string))
     {
         Report_class.Write_error_line("{0}: Deep copy array, typeof(T)=={1} is not allowed", typeof(Array_class).Name, typeof(T));
     }
     else if (array != null)
     {
         int array_length = array.Length;
         copy = new T[array_length];
         for (int indexA = 0; indexA < array_length; indexA++)
         {
             copy[indexA] = (T)array[indexA];
         }
     }
     else
     {
         copy = array;
     }
     return(copy);
 }
Exemplo n.º 6
0
        public static int[] Get_columnIndexes_of_given_columnNames <T>(string[] columnNames, params string[] given_columnNames)
        {
            int given_length = given_columnNames.Length;

            if (given_length == 0)
            {
                Report_class.Write_error_line("{0}: no columnNames to search for", typeof(T).Name);
            }
            int[] columnIndexes = new int[given_length];
            for (int i = 0; i < given_length; i++)
            {
                int index = Array.IndexOf(columnNames, given_columnNames[i]);
                if (index >= 0)
                {
                    columnIndexes[i] = index;
                }
                else
                {
                    Report_class.Write_error_line("{0}: columnName \"{1}\" does not exist", typeof(T).Name, given_columnNames[i]);
                    throw new Exception();
                }
            }
            return(columnIndexes);
        }
Exemplo n.º 7
0
        public static Dictionary <TD, T_line> ReadRawData_and_fillDictionary <TD, T_line>(StreamReader stream, DictionaryReadWriteOptions_base options, string file_name) where T_line : class
        {
            if ((options.Report == ReadWrite_report_enum.Report_main) || (options.Report == ReadWrite_report_enum.Report_everything))
            {
                Report_class.WriteLine("{0}:\nRead file for dictionary: {1}", typeof(T_line).Name, file_name);
            }
            Stopwatch timer = new Stopwatch();

            timer.Start();
            //Read headline, if it exists, determine indexes of columns to be safed in list
            //Begin
            FileInfo file = new FileInfo(options.File);

            PropertyInfo[] propInfo          = typeof(T_line).GetProperties();
            PropertyInfo   dict_key_propInfo = typeof(TD).GetProperties()[0];

            string[] columnNames = { Global_class.Empty_entry };
            int      dictionaryKey_index;

            int[] columnIndexes;
            int[] invalidLine_defining_columnIndexes  = new int[0];
            int[] invalidLine_defining_popertyIndexes = new int[0];
            int[] propertyIndexes;

            if (options.File_has_headline)
            {
                string headline = stream.ReadLine();
                columnNames         = Get_and_modify_columnNames(headline, options);
                columnIndexes       = Get_columnIndexes_of_given_columnNames <T_line>(columnNames, options.Key_columnNames);
                dictionaryKey_index = Get_columnIndexes_of_given_columnNames <T_line>(columnNames, options.DictionaryKey_columnName)[0];
                if (options.Invalid_line_defining_columnNames.Length > 0)
                {
                    invalidLine_defining_columnIndexes  = Get_columnIndexes_of_given_columnNames <T_line>(columnNames, options.Invalid_line_defining_columnNames);
                    invalidLine_defining_popertyIndexes = Get_propertyIndexes_of_corresponding_given_columnNames <T_line>(propInfo, options.Key_propertyNames, options.Key_columnNames, options.Invalid_line_defining_columnNames);
                }
            }
            else
            {
                columnIndexes       = options.Key_columnIndexes;
                dictionaryKey_index = options.DictionaryKey_columnIndex;
            }
            propertyIndexes = Get_propertyIndexes <T_line>(propInfo, options.Key_propertyNames);
            if (columnIndexes.Length != propertyIndexes.Length)
            {
                Report_class.Write_error_line("{0}: Length columnIndexes (Key_columnNames/columnIndexes) != propertyIndexes (Key_propertyNames)", typeof(T_line).Name);
            }
            //End

            //Skip lines
            for (int indexSkip = 0; indexSkip < options.Skip_lines; indexSkip++)
            {
                stream.ReadLine();
            }

            //Determine indexes of columns which contain a safecondition, if safeconditions exist
            //Begin
            bool safeConditions_exist = options.SafeCondition_entries != null;

            int[]    safeConditions_columnIndexes = new int[0];
            string[] safeConditions_entries       = options.SafeCondition_entries;
            int      safeConditions_length        = -1;

            if (safeConditions_exist == true)
            {
                safeConditions_length = options.SafeCondition_entries.Length;
                if (options.File_has_headline)
                {
                    safeConditions_columnIndexes = Get_columnIndexes_of_given_columnNames <T_line>(columnNames, options.SafeCondition_columnNames);
                }
                else
                {
                    safeConditions_columnIndexes = options.SafeCondition_columnIndexes;
                }
                if (safeConditions_columnIndexes.Length != safeConditions_entries.Length)
                {
                    Report_class.WriteLine("{0}: length safeConditions_columnIndexes (_columnNames/columnIndexes) != length safeConditions_columnEntries", typeof(T_line).Name);
                }
            }
            //End

            //Generate and fill list
            //Begin
            Dictionary <TD, T_line> dictionary = new Dictionary <TD, T_line>();

            var TD_Type     = typeof(TD);
            var T_line_Type = typeof(T_line);

            int    invalidLine_defining_columnIndexes_length = invalidLine_defining_columnIndexes.Length;
            string inputLine;
            int    readLines  = 0;
            int    safedLines = 0;
            int    colIndex;
            int    propIndex;
            bool   safeLine;
            bool   report_check_lineDelimiters = false;
            bool   valid;
            string invalidLineDefiningColumnEntry;
            int    line_count = 0;

            while ((inputLine = stream.ReadLine()) != null)
            {
                if ((inputLine.Length > 0) && (!inputLine.Substring(0, 5).Equals("-----")))
                {
                    line_count++;
                    string[] columnEntries = inputLine.Split(options.LineDelimiters);
                    if (columnEntries.Length == 1)
                    {
                        report_check_lineDelimiters = true;
                    }
                    safeLine = true;
                    if (safeConditions_exist)
                    {
                        for (int indexSC = 0; indexSC < safeConditions_length; indexSC++)
                        {
                            columnEntries[safeConditions_columnIndexes[indexSC]] = char.ToUpper(columnEntries[safeConditions_columnIndexes[indexSC]][0]) + columnEntries[safeConditions_columnIndexes[indexSC]].ToLower().Substring(1);  ///transient solution!!!!!!
                            if (safeConditions_entries[indexSC] != columnEntries[safeConditions_columnIndexes[indexSC]])
                            {
                                safeLine = false;
                            }
                        }
                    }
                    valid = true;
                    for (int indexIndex = 0; indexIndex < invalidLine_defining_columnIndexes_length; indexIndex++)
                    {
                        invalidLineDefiningColumnEntry = columnEntries[invalidLine_defining_columnIndexes[indexIndex]];
                        try
                        {
                            var obj = Convert.ChangeType(invalidLineDefiningColumnEntry, propInfo[invalidLine_defining_popertyIndexes[indexIndex]].PropertyType);
                            valid = true;
                        }
                        catch (InvalidCastException)
                        {
                            valid = false;
                        }
                        catch (FormatException)
                        {
                            valid = false;
                        }
                        catch (OverflowException)
                        {
                            valid = false;
                        }
                        catch (ArgumentNullException)
                        {
                            valid = false;
                        }
                    }
                    if ((safeLine) && (valid))
                    {
                        //TD new_dictionary_key = new TD();//)Activator.CreateInstance(TD_Type);
                        TD new_dictionary_key = (TD)Convert.ChangeType(columnEntries[dictionaryKey_index], TD_Type);

                        T_line newLine = (T_line)Activator.CreateInstance(T_line_Type);
                        for (int i = 0; i < columnIndexes.Length; i++)
                        {
                            colIndex  = columnIndexes[i];
                            propIndex = propertyIndexes[i];
                            if (columnEntries[colIndex] == "#DIV/0!")
                            {
                                columnEntries[colIndex] = "NaN";
                            }
                            if (propInfo[propIndex].PropertyType.IsEnum)
                            {
                                columnEntries[colIndex] = char.ToUpper(columnEntries[colIndex][0]) + columnEntries[colIndex].ToLower().Substring(1);
                                propInfo[propIndex].SetValue(newLine, Enum.Parse(propInfo[propIndex].PropertyType, columnEntries[colIndex]), null);
                            }
                            else if (string.IsNullOrEmpty(columnEntries[colIndex]))
                            {
                                if (propInfo[propIndex].PropertyType == typeof(int))
                                {
                                    propInfo[propIndex].SetValue(newLine, options.Empty_integer_value, null);
                                }
                                else if (propInfo[propIndex].PropertyType == typeof(string))
                                {
                                    propInfo[propIndex].SetValue(newLine, options.Empty_string_value, null);
                                }
                                else
                                {
                                    Report_class.Write_error_line("{0}: ReadRawData_and_FillList: {1} unhandled null entry", typeof(ReadWriteClass).Name, options.File);
                                }
                            }
                            else
                            {
                                if ((columnEntries[colIndex] != "") && ((columnEntries[colIndex] != "NA") || (propInfo[propIndex].PropertyType == typeof(string))))
                                {
                                    propInfo[propIndex].SetValue(newLine, Convert.ChangeType(columnEntries[colIndex], propInfo[propIndex].PropertyType), null);
                                }
                            }
                        }
                        dictionary.Add(new_dictionary_key, newLine);
                        safedLines = safedLines + 1;
                    }
                    readLines = readLines + 1;
                    if ((options.Report == ReadWrite_report_enum.Report_everything) && (readLines % 2000000 == 0))
                    {
                        Report_class.WriteLine("{0}: Read lines: {1} Mio, \tSafed lines: {2} Mio", typeof(T_line).Name, (double)readLines / 1000000, (double)safedLines / 1000000);
                    }
                }
            }
            if (report_check_lineDelimiters)
            {
                Report_class.Write_error_line("{0}: only one column entry: Check lineDelimiters", typeof(ReadWriteClass).Name);
            }
            timer.Stop();
            if (options.Report == ReadWrite_report_enum.Report_everything)
            {
                Report_class.WriteLine("{0}: Read lines: {1} Mio, Safed lines: {2} Mio", typeof(T_line).Name, (double)readLines / 1000000, (double)safedLines / 1000000);
                Report_class.WriteLine("{0}: Time: {1}", typeof(T_line).Name, timer.Elapsed);
            }
            if (options.Report >= ReadWrite_report_enum.Report_main)
            {
                Report_class.WriteLine();
            }
            stream.Close();
            if (dictionary.Count == 0)
            {
                Report_class.Write_error_line("{0}: ReadFile {1} data.count == 0, no lines filled!", typeof(ReadWriteClass).Name, options.File);
            }
            return(dictionary);
        }