Пример #1
0
        public void ExecuteWorkflow2(DeconTools.Backend.Core.Run run)
        {
            double scanTolerance = 100;


            Check.Require(run != null, String.Format("{0} failed. Run not defined.", this.Name));
            Check.Require(run.ResultCollection != null && run.ResultCollection.MSPeakResultList != null && run.ResultCollection.MSPeakResultList.Count > 0,
                          String.Format("{0} failed. Workflow requires MSPeakResults, but these were not defined.", this.Name));

            var sortedMSPeakResultList = run.ResultCollection.MSPeakResultList.OrderByDescending(p => p.MSPeak.Height).ToList();

            var msGeneratorNeedsInitializing = (this.MSgen == null);

            if (msGeneratorNeedsInitializing)
            {
                this.MSgen = MSGeneratorFactory.CreateMSGenerator(run.MSFileType);
            }



            var sw         = new System.Diagnostics.Stopwatch();
            var deconTimes = new List <long>();

            var sb = new StringBuilder();

            var totalPeaks = sortedMSPeakResultList.Count;

            var counter = -1;



            var whatPeakWentWhere = new Dictionary <int, string>();

            var lastPeakResult = sortedMSPeakResultList.Last();

            var chromPeaksCounter = 0;


            foreach (var peakResult in sortedMSPeakResultList)
            {
                counter++;

                //if (counter > 10000)
                //{
                //    break;
                //}


                if (counter % 1000 == 0)
                {
                    var logEntry = DateTime.Now + "\tWorking on peak " + counter + " of " + totalPeaks + "\tMSFeaturesCount =\t" + m_msFeatureCounter + "\tChomPeaks =\t" + chromPeaksCounter;
                    Logger.Instance.AddEntry(logEntry, m_logFileName);
                    Console.WriteLine(logEntry);
                    chromPeaksCounter = 0;
                }

                //if (peakResult.PeakID == 396293)
                //{
                //    Console.WriteLine(DateTime.Now + "\tWorking on peak " + peakResult.PeakID);
                //}


                var peakFate = "Undefined";

                var peakResultAlreadyIncludedInChromatogram = (peakResult.ChromID != -1);
                if (peakResultAlreadyIncludedInChromatogram)
                {
                    peakFate = "Chrom_Already";
                }
                else
                {
                    var peakResultAlreadyFoundInAnMSFeature = findPeakWithinMSFeatureResults(run.ResultCollection.ResultList, peakResult, scanTolerance);
                    if (peakResultAlreadyFoundInAnMSFeature)
                    {
                        peakFate = "MSFeature_Already";
                    }
                    else
                    {
                        peakFate = "CHROM";
                    }
                }

                whatPeakWentWhere.Add(peakResult.PeakID, peakFate);

                try
                {
                    if (peakFate == "CHROM")
                    {
                        //generate chromatogram & tag MSPeakResults

                        var minScanForChrom = peakResult.Scan_num - (int)scanTolerance;
                        if (minScanForChrom < run.MinLCScan)
                        {
                            minScanForChrom = run.MinLCScan;
                        }

                        var maxScanForChrom = peakResult.Scan_num + (int)scanTolerance;
                        if (maxScanForChrom > run.MaxLCScan)
                        {
                            maxScanForChrom = run.MaxLCScan;
                        }

                        PeakChrom chrom = new BasicPeakChrom();
                        chrom.ChromSourceData = this.ChromGenerator.GeneratePeakChromatogram(run.ResultCollection.MSPeakResultList, minScanForChrom, maxScanForChrom,
                                                                                             peakResult.MSPeak.XValue, this.ChromGenToleranceInPPM);

                        if (chrom.IsNullOrEmpty)
                        {
                            continue;
                        }

                        chrom.XYData = chrom.GetXYDataFromChromPeakData(minScanForChrom, maxScanForChrom);



                        //remove points from chromatogram due to MS/MS level data
                        if (run.ContainsMSMSData)
                        {
                            chrom.XYData = filterOutMSMSRelatedPoints(run, chrom.XYData);
                        }

                        //smooth the chromatogram
                        chrom.XYData = this.ChromSmoother.Smooth(chrom.XYData);

                        //detect peaks in chromatogram
                        chrom.PeakList = this.ChromPeakDetector.FindPeaks(chrom.XYData, 0, 0);

                        //Console.WriteLine("source peak -> scan= " + peakResult.Scan_num + "; m/z= " + peakResult.MSPeak.XValue);
                        //chrom.XYData.Display();



                        if (!chrom.PeakDataIsNullOrEmpty)
                        {
                            var chromPeak = chrom.GetChromPeakForGivenSource(peakResult);
                            if (chromPeak == null)
                            {
                                continue;
                            }

                            var peakWidthSigma = chromPeak.Width / 2.35;      //   width@half-height =  2.35σ   (Gaussian peak theory)

                            //now mark all peakResults that are members of this chromPeak
                            chrom.GetMSPeakMembersForGivenChromPeakAndAssignChromID(chromPeak, peakWidthSigma * 4, peakResult.PeakID);

                            run.CurrentScanSet = createScanSetFromChromatogramPeak(run, chromPeak);

                            var tempChromPeakMSFeatures = new List <IsosResult>();

                            MSgen.Execute(run.ResultCollection);

                            //trim the XYData to help the peak detector and Deconvolutor work faster

                            if (run.XYData == null)
                            {
                                continue;
                            }

                            run.XYData = run.XYData.TrimData(peakResult.MSPeak.XValue - 2, peakResult.MSPeak.XValue + 2);

                            if (run.XYData == null || run.XYData.Xvalues == null || run.XYData.Xvalues.Length == 0)
                            {
                                continue;
                            }

                            this.MSPeakDetector.Execute(run.ResultCollection);

                            //HACK:  calling 'deconvolute' will write results to 'isosResultBin' but not to 'ResultList';  I will manually add what I want to the official 'ResultList'
                            run.ResultCollection.IsosResultBin.Clear();
                            this.Deconvolutor.Deconvolute(run.ResultCollection);

                            this.Validator.Execute(run.ResultCollection);

                            //now, look in the isosResultBin and see what IsosResult (if any) the source peak is a member of
                            var msfeature = getMSFeatureForCurrentSourcePeak(peakResult, run);

                            //Console.WriteLine("source peak -> peakID= " + peakResult.PeakID + ";scan= " + peakResult.Scan_num + "; m/z= " + peakResult.MSPeak.XValue);

                            if (msfeature == null)  // didn't find a feature.  Source peak might be a 'lone wolf'
                            {
                                //Console.WriteLine("No MSFeature found!");
                            }
                            else
                            {
                                //Console.WriteLine("!!!!!!! MSFeature found!");

                                double toleranceInMZ = peakResult.MSPeak.Width / 2;

                                var msFeatureAlreadyExists = checkIfMSFeatureAlreadyExists(msfeature, run.ResultCollection.ResultList, toleranceInMZ, peakWidthSigma);

                                if (msFeatureAlreadyExists)
                                {
                                    //Console.WriteLine("---- but MSFeature was already present");
                                }
                                else
                                {
                                    //generate chromatograms and tag other peak members of the isotopic profile...
                                    foreach (var isoPeak in msfeature.IsotopicProfile.Peaklist)
                                    {
                                        PeakChrom isoPeakChrom = new BasicPeakChrom();

                                        isoPeakChrom.ChromSourceData = this.ChromGenerator.GeneratePeakChromatogram(run.ResultCollection.MSPeakResultList, minScanForChrom, maxScanForChrom,
                                                                                                                    isoPeak.XValue, this.ChromGenToleranceInPPM);
                                        if (!isoPeakChrom.IsNullOrEmpty)
                                        {
                                            isoPeakChrom.GetMSPeakMembersForGivenChromPeakAndAssignChromID(chromPeak, peakWidthSigma * 4, peakResult.PeakID);
                                        }
                                    }

                                    run.ResultCollection.ResultList.Add(msfeature);
                                    m_msFeatureCounter++;
                                }
                            }

                            //Console.WriteLine();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.Instance.AddEntry("ERROR:  peakID = " + peakResult.PeakID + "\t" + ex.Message + ";\t" + ex.StackTrace, m_logFileName);
                }



                var triggerToExport = 10;
                if (run.ResultCollection.ResultList.Count > triggerToExport)
                {
                    isosExporter.ExportResults(run.ResultCollection.ResultList);
                    run.ResultCollection.ResultList.Clear();

                    exportPeakData(run, m_peakOutputFileName, whatPeakWentWhere);
                    whatPeakWentWhere.Clear();
                }
            }


            //needs clean up....   sometimes there might be a case where the above loop is broken and we need the last few results written out.
            isosExporter.ExportResults(run.ResultCollection.ResultList);
            run.ResultCollection.ResultList.Clear();

            exportPeakData(run, m_peakOutputFileName, whatPeakWentWhere);
            whatPeakWentWhere.Clear();



            //foreach (var item in deconTimes)
            //{
            //    Console.WriteLine(item);
            //}

            //Console.WriteLine("Average = " + deconTimes.Average());
            //Console.WriteLine("Top 50 = " + deconTimes.Take(50).Average());
            //Console.WriteLine("Next 50 = " + deconTimes.Skip(50).Take(50).Average());



            //Console.WriteLine(sb.ToString());
        }
Пример #2
0
        public override void Execute()
        {
            const double scanTolerance = 100;


            Check.Require(this.Run != null, String.Format("{0} failed. Run not defined.", this.Name));
            Check.Require(this.Run.ResultCollection != null && this.Run.ResultCollection.MSPeakResultList != null && this.Run.ResultCollection.MSPeakResultList.Count > 0,
                          String.Format("{0} failed. Workflow requires MSPeakResults, but these were not defined.", this.Name));

            var sortedMSPeakResultList = this.Run.ResultCollection.MSPeakResultList.OrderByDescending(p => p.MSPeak.Height).ToList();

            var msGeneratorNeedsInitializing = (this.MSgen == null);

            if (msGeneratorNeedsInitializing)
            {
                this.MSgen = MSGeneratorFactory.CreateMSGenerator(this.Run.MSFileType);
            }



            var sw         = new System.Diagnostics.Stopwatch();
            var deconTimes = new List <long>();

            var sb = new StringBuilder();

            var totalPeaks = sortedMSPeakResultList.Count;

            var counter = -1;



            var whatPeakWentWhere = new Dictionary <int, string>();

            var lastPeakResult = sortedMSPeakResultList.Last();

            var chromPeaksCounter = 0;

            foreach (var peakResult in sortedMSPeakResultList)
            {
                counter++;

                //if (counter > 10000)
                //{
                //    break;
                //}


                if (counter % 1000 == 0)
                {
                    var logEntry = DateTime.Now + "\tWorking on peak " + counter + " of " + totalPeaks + "\tMSFeaturesCount =\t" + m_msFeatureCounter + "\tChomPeaks =\t" + chromPeaksCounter;
                    Logger.Instance.AddEntry(logEntry, m_logFileName);
                    Console.WriteLine(logEntry);
                    chromPeaksCounter = 0;
                }

                //if (peakResult.PeakID == 396293)
                //{
                //    Console.WriteLine(DateTime.Now + "\tWorking on peak " + peakResult.PeakID);
                //}


                var peakFate = "Undefined";

                var peakResultAlreadyIncludedInChromatogram = (peakResult.ChromID != -1);
                if (peakResultAlreadyIncludedInChromatogram)
                {
                    peakFate = "Chrom_Already";
                }

                if (!peakResultAlreadyIncludedInChromatogram)
                {
                    var peakResultAlreadyFoundInAnMSFeature = findPeakWithinMSFeatureResults(this.Run.ResultCollection.ResultList, peakResult, scanTolerance);
                    if (peakResultAlreadyFoundInAnMSFeature)
                    {
                        peakFate = "MSFeature_Already";
                    }
                    else
                    {
                        peakFate = "CHROM";
                    }
                }

                whatPeakWentWhere.Add(peakResult.PeakID, peakFate);

                try
                {
                    if (peakFate == "CHROM")
                    {
                        //generate chromatogram & tag MSPeakResults
                        var chromatogram = this.ChromGenerator.GenerateChromatogram(this.Run.ResultCollection.MSPeakResultList, this.Run.MinLCScan, this.Run.MaxLCScan, peakResult.MSPeak.XValue, this.ChromGenToleranceInPPM, peakResult.PeakID);

                        if (chromatogram == null)
                        {
                            continue;
                        }


                        //remove points from chromatogram due to MS/MS level data
                        if (this.Run.ContainsMSMSData)
                        {
                            chromatogram = filterOutMSMSRelatedPoints(this.Run, chromatogram);
                        }

                        //smooth the chromatogram
                        chromatogram = this.ChromSmoother.Smooth(chromatogram);

                        //detect peaks in chromatogram
                        var chromPeakList = this.ChromPeakDetector.FindPeaks(chromatogram, 0, 0);

                        //sort chrompeak list so it is decending.
                        chromPeakList = chromPeakList.OrderByDescending(p => p.Height).ToList();

                        //this is the temporary results that are collected for MSFeatures found over each chromPeak of the ChromPeakList
                        var tempChromPeakMSFeatures = new List <IsosResult>();

                        //store each chrom peak (which, here, is an ElutingPeak)

                        foreach (var chromPeak in chromPeakList)
                        {
                            var diffBetweenSourcePeakAndChromPeak = Math.Abs(peakResult.Scan_num - (int)chromPeak.XValue);

                            //TODO: examine whether or not we should iterate over the chromPeakList or not....
                            chromPeaksCounter++;
                            //Console.WriteLine("source peak = " + peakResult.PeakID + "; scan = " + peakResult.Scan_num + "; diff = " + diffBetweenSourcePeakAndChromPeak);

                            try
                            {
                                this.Run.CurrentScanSet = createScanSetFromChromatogramPeak(this.Run, chromPeak);

                                MSgen.Execute(this.Run.ResultCollection);

                                //trim the XYData to help the peak detector and Deconvolutor work faster
                                this.Run.XYData = this.Run.XYData.TrimData(peakResult.MSPeak.XValue - 2, peakResult.MSPeak.XValue + 2);

                                this.MSPeakDetector.Execute(this.Run.ResultCollection);

                                //HACK:  calling 'deconvolute' will write results to 'isosResultBin' but not to 'ResultList';  I will manually add what I want to the official 'ResultList'
                                this.Run.ResultCollection.IsosResultBin.Clear();
                                this.Deconvolutor.Deconvolute(this.Run.ResultCollection);

                                this.Validator.Execute(this.Run.ResultCollection);

                                //Need to find the target peak within the MSFeature.  Then mark other peaks of the MSFeature as being found, so that we don't bother generating a MS and deisotoping
                                findTargetPeakAddResultsToCollectionAndMarkAssociatedPeaks(tempChromPeakMSFeatures, peakResult, this.Run, scanTolerance);
                            }
                            catch (Exception ex)
                            {
                                Logger.Instance.AddEntry("ERROR:  peakID = " + peakResult.PeakID + "\t" + ex.Message + ";\t" + ex.StackTrace, m_logFileName);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.Instance.AddEntry("ERROR:  peakID = " + peakResult.PeakID + "\t" + ex.Message + ";\t" + ex.StackTrace, m_logFileName);
                }



                var triggerToExport = 10;
                if (this.Run.ResultCollection.ResultList.Count > triggerToExport || peakResult == lastPeakResult)
                {
                    isosExporter.ExportResults(this.Run.ResultCollection.ResultList);
                    this.Run.ResultCollection.ResultList.Clear();



                    exportPeakData(this.Run, m_peakOutputFileName, whatPeakWentWhere);
                    whatPeakWentWhere.Clear();
                }
            }

            //foreach (var item in deconTimes)
            //{
            //    Console.WriteLine(item);
            //}

            //Console.WriteLine("Average = " + deconTimes.Average());
            //Console.WriteLine("Top 50 = " + deconTimes.Take(50).Average());
            //Console.WriteLine("Next 50 = " + deconTimes.Skip(50).Take(50).Average());



            //Console.WriteLine(sb.ToString());
        }