Ejemplo n.º 1
0
        public void getPeakChromAndStoreInPeakChromObject_Test1()
        {
            double chromToleranceInPPM = 20;
            var    targetMZ            = 831.48;


            Run run = new XCaliburRun2(FileRefs.RawDataMSFiles.OrbitrapStdFile1);

            var peakImporter = new DeconTools.Backend.Data.PeakImporterFromText(FileRefs.PeakDataFiles.OrbitrapPeakFile_scans5500_6500);

            peakImporter.ImportPeaks(run.ResultCollection.MSPeakResultList);

            var chromGen = new ChromatogramGenerator();

            PeakChrom chrom = new BasicPeakChrom();

            chrom.ChromSourceData = chromGen.GeneratePeakChromatogram(run.ResultCollection.MSPeakResultList, run.MinLCScan, run.MaxLCScan, targetMZ, chromToleranceInPPM);

            Assert.AreEqual(59, chrom.ChromSourceData.Count);
        }
Ejemplo n.º 2
0
        public override void Execute()
        {
            var uimfRun = (UIMFRun)this.Run;

            //for each frame

            foreach (var frame in uimfRun.ScanSetCollection.ScanSetList)
            {
                uimfRun.CurrentScanSet = frame;


                // detect all peaks in frame
                var masterPeakList = getAllPeaksInFrame(uimfRun, NumMSScansToSumWhenBuildingMasterPeakList);

                // sort peaks
                masterPeakList.Sort(delegate(MSPeakResult peak1, MSPeakResult peak2)
                {
                    return(peak2.MSPeak.Height.CompareTo(peak1.MSPeak.Height));
                });


                // for each peak
                var peakCounter = 0;
                var peaksThatGenerateAChromatogram = 0;
                foreach (var peak in masterPeakList)
                {
                    peakCounter++;

                    //if (peakCounter > 500) break;
                    if (peak.MSPeak.Height < 1000)
                    {
                        break;
                    }


                    var peakFate = "Undefined";

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

                        displayPeakInfoAndFate(peak, peakFate);


                        continue;
                    }
                    else
                    {
                        peakFate = "CHROM";

                        //bool peakResultAlreadyFoundInAnMSFeature = findPeakWithinMSFeatureResults(run.ResultCollection.ResultList, peakResult, scanTolerance);
                        //if (peakResultAlreadyFoundInAnMSFeature)
                        //{
                        //    peakFate = "MSFeature_Already";
                        //}
                        //else
                        //{
                        //    peakFate = "CHROM";
                        //}
                    }

                    peaksThatGenerateAChromatogram++;
                    PeakChrom chrom = new BasicPeakChrom();

                    // create drift profile from raw data
                    var driftTimeProfileMZTolerance = this.DriftTimeProfileExtractionPPMTolerance * peak.MSPeak.XValue / 1e6;

                    //TODO: Fix this: update to use UIMF library and not DeconTools
                    //uimfRun.GetDriftTimeProfile  (frame.PrimaryFrame, this.Run.MinScan, this.Run.MaxScan, peak.MSPeak.XValue, driftTimeProfileMZTolerance);

                    var driftTimeProfileIsEmpty = (uimfRun.XYData.Xvalues == null);
                    if (driftTimeProfileIsEmpty)
                    {
                        addPeakToProcessedPeakList(peak);
                        peakFate = peakFate + " DriftProfileEmpty";
                        displayPeakInfoAndFate(peak, peakFate);

                        continue;
                    }

                    chrom.XYData = uimfRun.XYData;


                    // smooth drift profile
                    chrom.XYData = ChromSmoother.Smooth(uimfRun.XYData);

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

                    if (chrom.PeakDataIsNullOrEmpty)
                    {
                        addPeakToProcessedPeakList(peak);
                        peakFate = peakFate + " NoChromPeaksDetected";
                        displayPeakInfoAndFate(peak, peakFate);


                        continue;
                    }

                    // find which drift profile peak,  if any, the source peak is a member of
                    var chromPeak = chrom.GetChromPeakForGivenSource(peak);
                    if (chromPeak == null)
                    {
                        addPeakToProcessedPeakList(peak);
                        peakFate = peakFate + " TargetChromPeakNotFound";
                        displayPeakInfoAndFate(peak, peakFate);


                        continue;
                    }


                    // find other peaks in the master peaklist that are members of the found drift profile peak
                    // tag these peaks with the source peak's ID
                    var peakWidthSigma = chromPeak.Width / 2.35;      //   width@half-height =  2.35σ   (Gaussian peak theory)

                    var minScanForChrom = (int)Math.Floor(chromPeak.XValue - peakWidthSigma * 4);
                    var maxScanForChrom = (int)Math.Floor(chromPeak.XValue + peakWidthSigma * 4);

                    var peakToleranceInMZ   = driftTimeProfileMZTolerance;
                    var minMZForChromFilter = peak.MSPeak.XValue - peakToleranceInMZ;
                    var maxMZForChromFilter = peak.MSPeak.XValue + peakToleranceInMZ;


                    chrom.ChromSourceData = (from n in masterPeakList
                                             where n.Scan_num >= minScanForChrom && n.Scan_num <= maxScanForChrom &&
                                             n.MSPeak.XValue >= minMZForChromFilter && n.MSPeak.XValue < maxMZForChromFilter
                                             select n).ToList();

                    foreach (var item in chrom.ChromSourceData)
                    {
                        item.ChromID = peak.PeakID;
                    }

                    displayPeakInfoAndFate(peak, peakFate);
                }

                Console.WriteLine("peaksProcessed = " + peakCounter);
                Console.WriteLine("peaks generating a chrom = " + peaksThatGenerateAChromatogram);
            }



            // generate MS by integrating over drift profile peak

            // find MS peaks within range

            // find MS Features.

            // find MS Feature for which the source peak is a member of.


            // if found, add it.
            // And, for each MS peaks of the found MS Feature,  mark all peaks of the masterpeak list that correspond to the found drift time peak and m/z
        }
Ejemplo n.º 3
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());
        }