コード例 #1
0
ファイル: Program.cs プロジェクト: nandobsilva/Portfolio
        //###################################  MAIN PROGRAM   #########################################
        static void Main(string[] args)
        {
            Console.Clear();
            string[] input = args;
            bool     check;

            //Check if all input variables are valid. If valid check = true.
            check = Validation.ValidationInput(input);

            if (check)
            {
                // Variables to hold the arguments passed by the user
                string searchType       = input[0];
                string fileName         = input[1];
                string fullpath         = fileName;
                string searchParameter1 = input[2];
                string searchParameter2;
                if (input.Length <= 3)
                {
                    searchParameter2 = "";
                }
                else
                {
                    searchParameter2 = input[3];
                }
                // Variables used to print and save sequence in the search type: level2 and level3
                bool          printControl = false;
                int           numLines     = 0;
                bool          searchFound  = false;                          //Used to print message for sequences not found
                List <string> query        = new List <string>();
                if (searchType == "-level3")
                {
                    numLines = Function.CounLine(searchParameter1);           // Count the number of line in a file
                    query    = Function.CreateListFromFile(searchParameter1); // Save sequences found in a list to be saved in a file (-level3)
                }
                int[]    queryFound     = new int[numLines];                  //Used to print message for sequences not found (-level3)
                string[] sequencesFound = new string[numLines * 2];           //Hold the sequences found in a list (-level3)

                // Open file (input[1]) and execute the search program.
                FileStream   newFile     = new FileStream(fullpath, FileMode.Open, FileAccess.Read);
                StreamReader readFile    = new StreamReader(newFile);
                string       recordIn    = readFile.ReadLine();
                double       countDna    = 0;
                int          indexNumber = 0;

                // Search 4 using direct access
                if (searchType == "-level4")
                {
                    Search.SearchLevel_4(fileName, searchParameter1, searchParameter2, input[4]);
                }
                else
                {
                    while (recordIn != null)
                    {
                        string line = recordIn;
                        countDna = countDna + 1;

                        if (searchType == "-level1")
                        {
                            Search.SearchLevel_1(line, countDna, searchParameter1, searchParameter2);
                        }
                        else if (searchType == "-level2")
                        {
                            if (printControl)
                            {
                                Console.WriteLine(line);
                                printControl = false;
                            }
                            printControl = Search.SearchLevel_2(line, countDna, searchParameter1);
                            if (printControl)
                            {
                                searchFound = true;
                            }
                        }
                        else if (searchType == "-level3")
                        {
                            // Lines which will be included in the file
                            if (printControl)
                            {
                                sequencesFound[indexNumber] = line;
                                indexNumber++;
                                printControl = false;
                            }
                            bool controlLine = false; // Controle the line that will be save in the array.
                            for (int i = 0; i < query.Count; i++)
                            {
                                string lineFound = Search.SearchLevel_3(line, countDna, query[i]);
                                // Lines which will be included in the file
                                if (lineFound != null && controlLine == false)
                                {
                                    sequencesFound[indexNumber] = line;
                                    indexNumber++;
                                    printControl = true;
                                    controlLine  = true;
                                }
                                // Control the sequences that were not found to return an alert to the user
                                if (lineFound != null)
                                {
                                    queryFound[i] = 1;
                                }
                            }
                        }
                        else if (searchType == "-level5")
                        {
                            int count         = 0;
                            int sequenceFound = Search.SearchLevel_5(fileName, searchParameter1, countDna, line);
                            count = count + sequenceFound;
                            if (count > 0)
                            {
                                searchFound = true;
                            }
                        }
                        else if (searchType == "-level6")
                        {
                            int count         = 0;
                            int sequenceFound = Search.SearchLevel_6(searchParameter1, countDna, line);
                            count = count + sequenceFound;
                            if (count > 0)
                            {
                                searchFound = true;
                            }
                        }
                        else if (searchType == "-level7")
                        {
                            string pattern = searchParameter1.Replace("*", "[A-Z]*");  // Create a regex pattern
                            pattern = "^" + pattern + "$";                             // Create a regex pattern
                            Match patterFound = Regex.Match(line, pattern);            // Compare the pattern to each line of the file
                            if (patterFound.Success)
                            {
                                Search.SearchLevel_7(fileName, countDna);
                                searchFound = true;
                            }
                        }
                        recordIn = readFile.ReadLine();
                    }
                    readFile.Close();
                    newFile.Close();
                }

                //  Print error messages for sequences lines that exceed the number of the lines in the file
                if (searchType == "-level1")
                {
                    Validation.ValidationOutput(fileName, searchParameter1, searchParameter2, countDna);
                }
                // Write to file seqeunces found and print message for sequences not found
                if (searchType == "-level3")
                {
                    Validation.ValidationOutput(searchParameter2, query, queryFound, sequencesFound);
                }
                //  Print message for sequences not found in search -level2, 5, 6 and 7.
                if (
                    (searchType == "-level2" && searchFound != true) ||
                    (searchType == "-level5" && searchFound != true) ||
                    (searchType == "-level6" && searchFound != true) ||
                    (searchType == "-level7" && searchFound != true)
                    )
                {
                    Alerts.Alert_7(searchParameter1);
                }
            }
        }