Exemplo n.º 1
0
        /// <summary>
        /// Run the QC application module
        /// </summary>
        /// <param name="file"></param>
        private void Run(CommandLineOptions myArgs)
        {
            #region Determine parser
            // Determine parser type
            InputSubmission input = new InputSubmission(myArgs.InputFile);
            input.DetermineParserUtil();

            if (input.Parser is Bio.IO.FastQ.FastQParser &&
                myArgs.FastqFormat == null)
            {
                myArgs.ErrorWriteLine("For FASTQ input, please provide a valid FASTQ format: [Sanger, Solexa, Illumina]");
            }
            #endregion

            myArgs.WriteLine("Processing the file " + myArgs.InputFile +
                             "...this may take a while depending on the input size. Please be patient!");

            Stopwatch sw = new Stopwatch();
            sw.Start();

            #region Run QC analysis
            // Run QC analysis
            try
            {
                qcm = new Seqcos(input.Parser, myArgs.InputFile, myArgs.ExecuteSequenceQc, myArgs.ExecuteQualityScoreQc, myArgs.ExecuteBlast, myArgs.FastqFormat, dir: myArgs.OutputDirectory);
            }
            catch (ArgumentNullException e)
            {
                myArgs.ErrorWriteLine(e.Message);
            }

            myArgs.WriteLine("Performing sequence-level QC...");
            qcm.SequenceQc.Process();
            var time = ElapsedSeconds(sw);
            Console.WriteLine(time.ToString() + " s");

            if (!(input.Parser is FastAParser))
            {
                myArgs.WriteLine("Performing quality score-level QC...");
                qcm.QualityScoreQc.Process();
                time = ElapsedSeconds(sw, (long)time);
                Console.WriteLine(time.ToString() + " s");
            }

            #endregion

            #region Display statistics to console
            if (!myArgs.silent)
            {
                qcm.WriteInputStatistics(myArgs.UseExcelHyperlinks);
                DisplayInputStatistics();
            }
            #endregion

            #region Generate plots
            myArgs.WriteLine("Generating plots and saving them to: " + qcm.OutputDirectory);

            // Do these last, so that after plotting each section you can't free up memory
            qcm.PlotSequenceLevelStats();
            qcm.FinishSequenceQc();      // free up some memory, since we won't be using this anymore

            if (!(input.Parser is FastAParser))
            {
                qcm.PlotQualityScoreLevelStats();
                qcm.FinishQualityScoreQC();
            }

            #endregion

            #region Carry out BLAST analysis

            if (myArgs.ExecuteBlast)
            {
                myArgs.WriteLine("Searching for contaminants...");
                // Convert FASTQ to FASTA
                string targetFasta = qcm.OutputDirectory + "/" + qcm.GetPrefix() + ".fa";
                BioHelper.ConvertToFASTA(qcm.ContaminationFinder.TargetSequences, targetFasta, myArgs.BlastSize, overwrite: true);

                // Run local NCBI BLAST
                qcm.ContaminationFinder.RunLocalBlast(myArgs.BlastDbPrefix, targetFasta);
                time = ElapsedSeconds(sw, (long)time);
                Console.WriteLine(time.ToString() + " s");
                File.Delete(targetFasta);

                if (!myArgs.silent)
                {
                    HighlightText("NCBI BLAST results from searching against " + myArgs.BlastDbPrefix + " database:\n");
                    DisplayBlastResults();
                }
            }

            #endregion


            input.Parser.Close();

            sw.Stop();
            myArgs.WriteLine("\nTotal time: " + ToSeconds(sw.ElapsedMilliseconds) + " s");
        }
Exemplo n.º 2
0
        /// <summary>
        /// This event is fired by analysisThread when the thread is invoked.
        /// This event calls the main Qc application framework and executes the analysis.
        /// </summary>
        /// <param name="sender">BackgroundWorker instance</param>
        /// <param name="e">Event parameters</param>
        private void DoQcAnalysis(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            if (worker != null)
            {
                try
                {
                    OpenFileArgs args = e.Argument as OpenFileArgs;
                    System.Windows.Threading.Dispatcher dispatcher = run.Dispatcher;
                    UpdateProgressDelegate update = new UpdateProgressDelegate(UpdateProgressText);

                    if (args != null)
                    {
                        dispatcher.BeginInvoke(update, 0, "Starting analysis...please be patient!");
                        application = new Seqcos(args.InputInfo.Parser, args.InputInfo.Filename, args.CanRunSequenceQc, args.CanRunQualityScoreQc, args.CanRunBlast, args.FastqFormat);

                        #region Sequence-level QC

                        /// Run sequence level QC
                        if (args.CanRunSequenceQc)
                        {
                            // Content by position
                            dispatcher.BeginInvoke(update, 25, "Performing sequence-level QC...analyzing base positions");
                            application.SequenceQc.ContentByPosition();
                            dispatcher.BeginInvoke(update, 45, "Performing sequence-level QC...analyzing base positions");
                            application.SequenceQc.GCContentByPosition();
                            System.Threading.Thread.Sleep(100);
                            if (worker.CancellationPending)
                            {
                                e.Cancel = true;
                                return;
                            }

                            // Content by sequence
                            dispatcher.BeginInvoke(update, 65, "Performing sequence-level QC...analyzing sequences");
                            application.SequenceQc.ContentBySequence();
                            dispatcher.BeginInvoke(update, 85, "Performing sequence-level QC...analyzing sequences");
                            System.Threading.Thread.Sleep(100);
                            if (worker.CancellationPending)
                            {
                                e.Cancel = true;
                                return;
                            }

                            // Plot results
                            dispatcher.BeginInvoke(update, 80, "Generating plots...");
                            application.PlotSequenceLevelStats();
                            System.Threading.Thread.Sleep(100);
                            if (worker.CancellationPending)
                            {
                                e.Cancel = true;
                                return;
                            }

                            dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Send, update, 100, "Done sequence-level QC!");
                        }
                        #endregion

                        #region Quality score-level QC

                        /// Run quality score level QC
                        if (args.CanRunQualityScoreQc)
                        {
                            dispatcher.BeginInvoke(update, 5, "Performing quality score-level QC...analyzing base positions");
                            application.QualityScoreQc.ContentByPosition();
                            System.Threading.Thread.Sleep(100);
                            if (worker.CancellationPending)
                            {
                                e.Cancel = true;
                                return;
                            }

                            dispatcher.BeginInvoke(update, 25, "Performing quality score-level QC...analyzing sequences");
                            application.QualityScoreQc.ContentBySequence();
                            System.Threading.Thread.Sleep(100);
                            if (worker.CancellationPending)
                            {
                                e.Cancel = true;
                                return;
                            }

                            dispatcher.BeginInvoke(update, 55, "Generating plots (the boxplot will take a while...please be patient!)");
                            application.PlotQualityScoreLevelStats();
                            System.Threading.Thread.Sleep(100);
                            if (worker.CancellationPending)
                            {
                                e.Cancel = true;
                                return;
                            }

                            dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Send, update, 100, "Done quality score-level QC!");
                        }

                        application.WriteInputStatistics(excelFormat: false);
                        application.FinishQualityScoreQC();

                        #endregion

                        #region BLAST

                        /// Run BLAST
                        if (args.CanRunBlast)
                        {
                            dispatcher.BeginInvoke(update, 10, "Generating temporary FASTA file for BLAST...");

                            // execute BLAST here..
                            string targetFasta = application.OutputDirectory + "/" + application.GetPrefix() + ".fa";
                            BioHelper.ConvertToFASTA(application.ContaminationFinder.TargetSequences, targetFasta, args.BlastArgs.NumInputSequences, false);

                            dispatcher.BeginInvoke(update, 70, "Running BLAST...");
                            application.ContaminationFinder.RunLocalBlast(args.BlastArgs.Database, targetFasta);

                            dispatcher.BeginInvoke(update, 100, "Finished! Deleting FASTA file...");
                            File.Delete(targetFasta);
                        }

                        #endregion
                    }
                }
                catch (ArgumentNullException ex)
                {
                    e.Cancel = true;
                    if (worker.CancellationPending)
                    {
                        return;
                    }

                    MessageBox.Show(ex.TargetSite + ": " + ex.Message);
                }
                catch (Exception ex)
                {
                    e.Cancel = true;
                    if (worker.CancellationPending)
                    {
                        return;
                    }

                    MessageBox.Show(ex.TargetSite + ": " + ex.Message);
                }
            }
        }