public override XYData GenerateMS(DeconTools.Backend.Core.Run run, ScanSet lcScanSet, ScanSet imsScanSet = null)
        {
            Check.Require(run != null, string.Format("{0} failed. Run has not been defined.", this.Name));
            if (run == null)
            {
                return(new XYData());
            }

            Check.Require(run.PeakList != null && run.PeakList.Count > 0, string.Format("{0} failed. Run has not been defined.", this.Name));
            if (run.PeakList == null)
            {
                return(new XYData());
            }

            var syntheticMSData = new XYData();
            var xVals           = new List <double>();
            var yVals           = new List <double>();

            foreach (var peak in run.PeakList)
            {
                var msPeak = (MSPeak)peak;

                XYData generatedXYData;

                switch (ModeOfPeakWidthCalculation)
                {
                case SyntheticMSGeneratorFromPeakDataMode.WidthsCalculatedFromSingleValue:
                    generatedXYData = msPeak.GetTheorPeakData(this.PeakWidthForAllPeaks, 11);
                    break;

                case SyntheticMSGeneratorFromPeakDataMode.WidthsCalculatedOnAPerPeakBasis:
                    generatedXYData = msPeak.GetTheorPeakData(msPeak.Width, 11);
                    break;

                default:
                    generatedXYData = msPeak.GetTheorPeakData(this.PeakWidthForAllPeaks, 11);
                    break;
                }

                xVals.AddRange(generatedXYData.Xvalues);
                yVals.AddRange(generatedXYData.Yvalues);
            }

            var xyData = new XYData
            {
                Xvalues = xVals.ToArray(),
                Yvalues = yVals.ToArray()
            };

            return(xyData);
        }
Esempio n. 2
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());
        }