public void deconWithRAPID_Then_ValidateTest1()
        {
            Run     run     = new XCaliburRun2(FileRefs.RawDataMSFiles.OrbitrapStdFile1);
            ScanSet scanSet = new ScanSet(6005);

            run.CurrentScanSet = scanSet;


            MSGenerator msgen = MSGeneratorFactory.CreateMSGenerator(run.MSFileType);

            DeconToolsPeakDetectorV2 peakDetector = new DeconToolsPeakDetectorV2(1.3, 2, DeconTools.Backend.Globals.PeakFitType.QUADRATIC, true);
            RapidDeconvolutor        decon        = new RapidDeconvolutor();
            ResultValidatorTask      validator    = new ResultValidatorTask();

            msgen.Execute(run.ResultCollection);
            peakDetector.Execute(run.ResultCollection);
            decon.Execute(run.ResultCollection);
            validator.Execute(run.ResultCollection);

            Assert.AreEqual(190, run.ResultCollection.ResultList.Count);
            IsosResult testResult = run.ResultCollection.ResultList[0];

            Assert.AreEqual(0.0213668719893332m, (decimal)testResult.InterferenceScore);

            //TestUtilities.DisplayMSFeatures(run.ResultCollection.ResultList);
        }
        public void findMSFeaturesInOrbitrapData_Test1()
        {
            Run run = new XCaliburRun2(FileRefs.RawDataMSFiles.OrbitrapStdFile1);

            ScanSet scanSet = new ScanSet(6005);

            run.CurrentScanSet = scanSet;


            MSGenerator msgen = MSGeneratorFactory.CreateMSGenerator(run.MSFileType);

            DeconToolsPeakDetectorV2 peakDetector = new DeconToolsPeakDetectorV2(1.3, 2, DeconTools.Backend.Globals.PeakFitType.QUADRATIC, true);

            peakDetector.IsDataThresholded = true;

            RapidDeconvolutor decon = new RapidDeconvolutor();

            decon.IsNewFitCalculationPerformed = true;


            msgen.Execute(run.ResultCollection);
            peakDetector.Execute(run.ResultCollection);
            decon.Execute(run.ResultCollection);

            Assert.AreEqual(192, run.ResultCollection.ResultList.Count);

            //order and get the most intense msfeature
            run.ResultCollection.ResultList = run.ResultCollection.ResultList.OrderByDescending(p => p.IntensityAggregate).ToList();
            IsosResult testIso = run.ResultCollection.ResultList[0];

            Assert.AreEqual(20986588.375, testIso.IntensityAggregate);
            Assert.AreEqual(2, testIso.IsotopicProfile.ChargeState);
            Assert.AreEqual(0.00548633562231413m, (decimal)testIso.IsotopicProfile.Score);
            Assert.AreEqual(3, testIso.IsotopicProfile.Peaklist.Count);
            Assert.AreEqual(481.274105402604m, (decimal)testIso.IsotopicProfile.Peaklist[0].XValue);
            Assert.AreEqual(481.775412188198m, (decimal)testIso.IsotopicProfile.Peaklist[1].XValue);
            Assert.AreEqual(482.276820274024m, (decimal)testIso.IsotopicProfile.Peaklist[2].XValue);

            TestUtilities.DisplayMSFeatures(run.ResultCollection.ResultList);
        }
예제 #3
0
        public void Execute()
        {
            InitializeWorkflow();

            if (Run.MSFileType == Globals.MSFileType.PNNL_UIMF)
            {
                IMSScanSetCollection = CreateIMSScanSetCollection();
            }

            LcScanSetCollection = CreateLcScanSetCollection();

            PrepareOutputDirectory(WorkflowParameters.OutputDirectory);

            var outputPeaksFilePath = getOutputPeaksFilename();

            peakExporter = new PeakListTextExporter(Run.MSFileType, outputPeaksFilePath);

            var numTotalScans = LcScanSetCollection.ScanSetList.Count;
            var lastProgress  = DateTime.UtcNow;

            using (var sw = new StreamWriter(new FileStream(outputPeaksFilePath, FileMode.Append, FileAccess.Write, FileShare.Read)))
            {
                if (Run.MSFileType == Globals.MSFileType.PNNL_UIMF && Run is UIMFRun uimfRun)
                {
                    var numTotalFrames = LcScanSetCollection.ScanSetList.Count;
                    var frameCounter   = 0;

                    foreach (var frameSet in LcScanSetCollection.ScanSetList)
                    {
                        frameCounter++;
                        uimfRun.CurrentScanSet = frameSet;
                        uimfRun.ResultCollection.MSPeakResultList.Clear();

                        foreach (var scanSet in IMSScanSetCollection.ScanSetList)
                        {
                            uimfRun.CurrentIMSScanSet = (IMSScanSet)scanSet;
                            MSGenerator.Execute(uimfRun.ResultCollection);
                            _ms1PeakDetector.Execute(uimfRun.ResultCollection);
                        }
                        peakExporter.WriteOutPeaks(sw, uimfRun.ResultCollection.MSPeakResultList);

                        if (DateTime.UtcNow.Subtract(lastProgress).TotalSeconds >= 1 || frameCounter == numTotalFrames)
                        {
                            lastProgress = DateTime.UtcNow;
                            var percentProgress = frameCounter * 100 / (double)numTotalFrames;
                            reportProgress(percentProgress);
                        }
                    }
                }
                else
                {
                    var scanCounter = 0;
                    foreach (var scan in LcScanSetCollection.ScanSetList)
                    {
                        scanCounter++;

                        Run.CurrentScanSet = scan;

                        Run.ResultCollection.MSPeakResultList.Clear();

                        MSGenerator.Execute(Run.ResultCollection);
                        if (Run.GetMSLevel(scan.PrimaryScanNumber) == 1)
                        {
                            _ms1PeakDetector.Execute(Run.ResultCollection);
                        }
                        else
                        {
                            var dataIsCentroided = Run.IsDataCentroided(scan.PrimaryScanNumber);
                            if (dataIsCentroided)
                            {
                                _ms2PeakDetectorForCentroidedData.Execute(Run.ResultCollection);
                            }
                            else
                            {
                                _ms2PeakDetectorForProfileData.Execute(Run.ResultCollection);
                            }
                        }

                        peakExporter.WriteOutPeaks(sw, Run.ResultCollection.MSPeakResultList);

                        if (DateTime.UtcNow.Subtract(lastProgress).TotalSeconds >= 1 || scanCounter == numTotalScans)
                        {
                            lastProgress = DateTime.UtcNow;
                            var percentProgress = scanCounter * 100 / (double)numTotalScans;
                            reportProgress(percentProgress);
                        }
                    }
                }
            }

            Run.ResultCollection.MSPeakResultList.Clear();
        }
        public override void Execute()
        {
            InitializeWorkflow();

            PrepareOutputFolder(_workflowParameters.OutputFolder);

            string outputPeaksFileName = getOutputPeaksFilename();

            peakExporter = new PeakListTextExporter(Run.MSFileType, outputPeaksFileName);

            int numTotalScans = Run.ScanSetCollection.ScanSetList.Count;
            int scanCounter   = 0;

            if (Run.MSFileType == DeconTools.Backend.Globals.MSFileType.PNNL_UIMF)
            {
                var uimfrun = Run as UIMFRun;

                int numTotalFrames = uimfrun.ScanSetCollection.ScanSetList.Count;
                int frameCounter   = 0;

                foreach (var frameSet in uimfrun.ScanSetCollection.ScanSetList)
                {
                    frameCounter++;
                    uimfrun.CurrentScanSet = frameSet;
                    uimfrun.ResultCollection.MSPeakResultList.Clear();

                    foreach (var scanSet in uimfrun.IMSScanSetCollection.ScanSetList)
                    {
                        uimfrun.CurrentIMSScanSet = (IMSScanSet)scanSet;
                        MSGenerator.Execute(uimfrun.ResultCollection);
                        this._ms1PeakDetector.Execute(uimfrun.ResultCollection);
                    }
                    peakExporter.WriteOutPeaks(uimfrun.ResultCollection.MSPeakResultList);

                    if (frameCounter % 5 == 0 || scanCounter == numTotalFrames)
                    {
                        double percentProgress = frameCounter * 100 / numTotalFrames;
                        reportProgress(percentProgress);
                    }
                }
            }
            else
            {
                foreach (var scan in Run.ScanSetCollection.ScanSetList)
                {
                    scanCounter++;

                    Run.CurrentScanSet = scan;

                    Run.ResultCollection.MSPeakResultList.Clear();

                    MSGenerator.Execute(Run.ResultCollection);
                    if (Run.GetMSLevel(scan.PrimaryScanNumber) == 1)
                    {
                        this._ms1PeakDetector.Execute(Run.ResultCollection);
                    }
                    else
                    {
                        var dataIsCentroided = Run.IsDataCentroided(scan.PrimaryScanNumber);
                        if (dataIsCentroided)
                        {
                            _ms2PeakDetectorForCentroidedData.Execute(Run.ResultCollection);
                        }
                        else
                        {
                            _ms2PeakDetectorForProfileData.Execute(Run.ResultCollection);
                        }
                    }

                    peakExporter.WriteOutPeaks(Run.ResultCollection.MSPeakResultList);

                    if (scanCounter % 50 == 0 || scanCounter == numTotalScans)
                    {
                        double percentProgress = scanCounter * 100 / numTotalScans;
                        reportProgress(percentProgress);
                    }
                }
            }



            Run.ResultCollection.MSPeakResultList.Clear();
        }
예제 #5
0
        protected override void IterateOverScans()
        {
            DeconMSnResults.Clear();
            _scanCounter = 0;

            var resultCounter = 0;

            foreach (var scanSet in Run.ScanSetCollection.ScanSetList)
            {
                _scanCounter++;

                if (BackgroundWorker != null)
                {
                    if (BackgroundWorker.CancellationPending)
                    {
                        return;
                    }
                }

                //check ms level
                var currentMSLevel = Run.GetMSLevel(scanSet.PrimaryScanNumber);

                if (currentMSLevel == 1)
                {
                    Run.ResultCollection.IsosResultBin.Clear();

                    Run.CurrentScanSet = scanSet;

                    MSGenerator.Execute(Run.ResultCollection);

                    _currentMS1XYValues = new XYData
                    {
                        Xvalues = Run.XYData.Xvalues,
                        Yvalues = Run.XYData.Yvalues
                    };

                    _currentMS1TICIntensity = Run.GetTICFromInstrumentInfo(scanSet.PrimaryScanNumber);

                    PeakDetector.Execute(Run.ResultCollection);
                    _currentMS1Peaks = new List <Peak>(Run.PeakList);
                }
                else if (currentMSLevel == 2)
                {
                    if (_currentMS1Peaks == null || _currentMS1Peaks.Count == 0)
                    {
                        continue;
                    }

                    var precursorInfo = Run.GetPrecursorInfo(scanSet.PrimaryScanNumber);
                    Run.CurrentScanSet = scanSet;
                    var inaccurateParentMZ = precursorInfo.PrecursorMZ;

                    resultCounter++;
                    var deconMSnResult = new DeconMSnResult {
                        ParentScan = Run.GetParentScan(scanSet.PrimaryScanNumber)
                    };
                    deconMSnResult.IonInjectionTime       = Run.GetIonInjectionTimeInMilliseconds(deconMSnResult.ParentScan);
                    deconMSnResult.ScanNum                = scanSet.PrimaryScanNumber;
                    deconMSnResult.OriginalMZTarget       = inaccurateParentMZ;
                    deconMSnResult.ParentScanTICIntensity = _currentMS1TICIntensity;

                    MSGenerator.Execute(Run.ResultCollection);

                    var lowerMZ = inaccurateParentMZ - 1.1;
                    var upperMZ = inaccurateParentMZ + 1.1;

                    var dataIsCentroided = Run.IsDataCentroided(scanSet.PrimaryScanNumber);
                    if (dataIsCentroided)
                    {
                        _ms2PeakDetectorForCentroidData.Execute(Run.ResultCollection);
                    }
                    else
                    {
                        _ms2PeakDetectorForProfileData.Execute(Run.ResultCollection);
                    }
                    var ms2Peaks = new List <Peak>(Run.PeakList);

                    var filteredMS1Peaks = _currentMS1Peaks.Where(p => p.XValue > lowerMZ && p.XValue < upperMZ).ToList();

                    IsotopicProfile selectedMS1Feature = null;
                    for (var attemptNum = 0; attemptNum < NumMaxAttemptsAtLowIntensitySpecies; attemptNum++)
                    {
                        var candidateMS1Features = GetCandidateMS1Features(inaccurateParentMZ, filteredMS1Peaks);

                        //if none were found, will regenerate MS1 spectrum and find peaks again
                        if (candidateMS1Features.Count == 0)
                        {
                            var numSummed = attemptNum * 2 + 3;
                            var ms1Scan   = precursorInfo.PrecursorScan;

                            var ms1ScanSet = new ScanSetFactory().CreateScanSet(Run, ms1Scan, numSummed);

                            //get MS1 mass spectrum again. This time sum spectra
                            Run.CurrentScanSet = ms1ScanSet;
                            MSGenerator.Execute(Run.ResultCollection);

                            //run MS1 peak detector, with greater sensitivity
                            var isLastAttempt = attemptNum >= NumMaxAttemptsAtLowIntensitySpecies - 2;    //need to do -2 because of the way the loop advances the counter.

                            if (isLastAttempt)
                            {
                                _superSensitiveMS1PeakDetector.MinX = lowerMZ;
                                _superSensitiveMS1PeakDetector.MaxX = upperMZ;
                                _superSensitiveMS1PeakDetector.Execute(Run.ResultCollection);
                                filteredMS1Peaks = new List <Peak>(Run.PeakList);
                            }
                            else
                            {
                                _moreSensitiveMS1PeakDetector.Execute(Run.ResultCollection);
                                var moreSensitiveMS1Peaks = new List <Peak>(Run.PeakList);
                                filteredMS1Peaks = moreSensitiveMS1Peaks.Where(p => p.XValue > lowerMZ && p.XValue < upperMZ).ToList();
                            }
                        }
                        else if (candidateMS1Features.Count == 1)
                        {
                            selectedMS1Feature = candidateMS1Features.First();
                            break;   //we found something, so no need for any further attempts
                        }
                        else
                        {
                            var highQualityCandidates = candidateMS1Features.Where(p => p.Score < 0.15).ToList();
                            if (highQualityCandidates.Count == 0)
                            {
                                selectedMS1Feature = candidateMS1Features.OrderByDescending(p => p.IntensityMostAbundantTheor).First();
                            }
                            else if (highQualityCandidates.Count == 1)
                            {
                                selectedMS1Feature = highQualityCandidates.First();
                            }
                            else
                            {
                                selectedMS1Feature = highQualityCandidates.OrderByDescending(p => p.IntensityMostAbundantTheor).First();
                            }

                            deconMSnResult.ExtraInfo = "Warning - multiple MSFeatures found for target parent MZ";
                            break;   //we found something, so no need for any further attempts
                        }
                    }

                    if (selectedMS1Feature != null)
                    {
                        deconMSnResult.ParentMZ           = selectedMS1Feature.MonoPeakMZ;
                        deconMSnResult.ParentChargeState  = selectedMS1Feature.ChargeState;
                        deconMSnResult.ParentIntensity    = selectedMS1Feature.IntensityMostAbundantTheor;
                        deconMSnResult.IntensityAggregate = selectedMS1Feature.IntensityMostAbundantTheor;
                        deconMSnResult.IsotopicProfile    = selectedMS1Feature;
                    }
                    else
                    {
                        var candidatePeaks = new List <Peak>();

                        foreach (var peak in filteredMS1Peaks)
                        {
                            var currentDiff = Math.Abs(peak.XValue - inaccurateParentMZ);

                            var toleranceInMZ = ToleranceInPPM * peak.XValue / 1e6;

                            if (currentDiff < toleranceInMZ)
                            {
                                candidatePeaks.Add(peak);
                            }
                        }

                        Peak selectedPeak = null;

                        if (candidatePeaks.Count == 0)
                        {
                            //cannot even find a suitable MS1 peak. So can't do anything
                        }
                        else if (candidatePeaks.Count == 1)
                        {
                            selectedPeak = candidatePeaks.First();
                        }
                        else
                        {
                            selectedPeak = candidatePeaks.OrderByDescending(p => p.Height).First();
                        }

                        if (selectedPeak != null)
                        {
                            deconMSnResult.ParentMZ          = selectedPeak.XValue;
                            deconMSnResult.ParentChargeState = 1;   //not sure what charge I should assign... Ask SangTae
                            deconMSnResult.ParentIntensity   = selectedPeak.Height;
                            deconMSnResult.IsotopicProfile   = null;
                            deconMSnResult.ExtraInfo         = "Failure code 1: No MSFeature, but peak found";
                        }
                        else
                        {
                            deconMSnResult.ParentMZ          = precursorInfo.PrecursorMZ; //could not find the accurate parentMZ, so just report what the instrument found.
                            deconMSnResult.ParentChargeState = 1;                         //not sure what charge I should assign... Ask SangTae
                            deconMSnResult.ParentIntensity   = 0;
                            deconMSnResult.IsotopicProfile   = null;
                            deconMSnResult.ExtraInfo         = "Failure code 2: No MSFeature or peak found";
                        }
                    }

                    //Build export data.
                    var outputString = GetMGFOutputString(Run, scanSet.PrimaryScanNumber, deconMSnResult, ms2Peaks);

                    var includeHeader = resultCounter == 1;
                    var summaryString = GetSummaryString(deconMSnResult, includeHeader);

                    var parentProfileString = GetParentProfileString(deconMSnResult, includeHeader);

                    if (ExportData)
                    {
                        WriteOutMainData(outputString);

                        WriteOutDeconMSnSummary(summaryString);

                        if (IsParentProfileDataExported)
                        {
                            WriteOutParentProfileInfoString(parentProfileString);
                        }
                    }

                    if (deconMSnResult.ParentIntensity > 0)
                    {
                        DeconMSnResults.Add(deconMSnResult);
                    }
                }
                else
                {
                    throw new ApplicationException(
                              "DeconMSn only works on MS1 and MS2 data; You are attempting MS3");
                }

                ReportProgress();
            }
        }
        protected override void IterateOverScans()
        {
            const int CONSOLE_INTERVAL_SECONDS = 15;

            const bool SKIP_DECONVOLUTION = false;

            var uimfRun = (UIMFRun)Run;

            var startTime      = DateTime.UtcNow;
            var dtLastProgress = DateTime.UtcNow.Subtract(new TimeSpan(1, 0, 0));

            var frameCountProcessed = 0;
            var maxRuntimeHours     = NewDeconToolsParameters.MiscMSProcessingParameters.MaxHoursPerDataset;

            // uimfRun.IMSScanSetCollection.ScanSetList =uimfRun.IMSScanSetCollection.ScanSetList.Where(p => p.PrimaryScanNumber == 153).ToList();

            // Iterate over unsummed data and fix saturated isotopic profiles. Unsummed will be used during a second iteration (over summed data)
            foreach (var lcScanSet in uimfRun.ScanSetCollection.ScanSetList)
            {
                uimfRun.ResultCollection.MSPeakResultList.Clear();
                _unsummedMSFeatures.Clear();

                var unsummedFrameSet = new ScanSet(lcScanSet.PrimaryScanNumber);

                // Get saturated MSFeatures for unsummed data
                uimfRun.CurrentScanSet = unsummedFrameSet;

                var forceProgressMessage  = true;
                var imsScanCountProcessed = 0;

                var maxMinutesPerFrame = NewDeconToolsParameters.MiscMSProcessingParameters.MaxMinutesPerFrame;
                if (uimfRun.GetNumFrames() <= 10)
                {
                    // Disable the per-frame timeout if we have 10 or fewer frames
                    maxMinutesPerFrame = int.MaxValue;
                }
                var frameStartTime = DateTime.UtcNow;
                var timeoutReached = false;

                foreach (var scanset in uimfRun.IMSScanSetCollection.ScanSetList)
                {
                    if (DateTime.UtcNow.Subtract(dtLastProgress).TotalSeconds >= CONSOLE_INTERVAL_SECONDS || forceProgressMessage)
                    {
                        dtLastProgress       = DateTime.UtcNow;
                        forceProgressMessage = false;
                        Console.WriteLine("Processing frame " + lcScanSet.PrimaryScanNumber + ", scan " + scanset.PrimaryScanNumber + " (Unsummed)");
                    }

                    if (DateTime.UtcNow.Subtract(frameStartTime).TotalMinutes >= maxMinutesPerFrame)
                    {
                        Console.WriteLine(
                            "Aborted processing of frame {0} because {1} minutes have elapsed (processing unsummed features); IMSScanCount processed = {2}",
                            lcScanSet.PrimaryScanNumber,
                            (int)DateTime.UtcNow.Subtract(frameStartTime).TotalMinutes,
                            imsScanCountProcessed);

                        timeoutReached = true;

                        break;
                    }

                    uimfRun.ResultCollection.IsosResultBin.Clear();  //clear any previous MSFeatures

                    var unsummedIMSScanset = new IMSScanSet(scanset.PrimaryScanNumber);
                    uimfRun.CurrentIMSScanSet = unsummedIMSScanset;

                    _msGenerator.Execute(Run.ResultCollection);

                    _zeroFiller.Execute(Run.ResultCollection);

                    // For debugging....
                    // if (scanset.PrimaryScanNumber == 123)
                    // {
                    //    Console.WriteLine(scanset + "\t being processed!");
                    // }

                    _peakDetector.Execute(Run.ResultCollection);

                    imsScanCountProcessed++;

                    if (SKIP_DECONVOLUTION)
                    {
                        continue;
                    }

                    // Deconvolute Unsummed MSFeatures
                    // This is a preliminary step for Saturation Detection

//#if Disable_DeconToolsV2
//                        throw new NotImplementedException("Cannot use class SaturationIMSScanBasedWorkflow since support for C++ based DeconToolsV2 is disabled");
//#else
                    _deconvolutor.Deconvolute(uimfRun.ResultCollection); //adds to IsosResultBin
//#endif

                    // Note: the deconvolutor automatically increases the MSFeatureCounter.
                    // Here, we don't want this, since this data is used only for saturation correction,
                    // not for generating the official MSFeatures list. So we need to
                    // correct the MSFeatureCounter value.
                    Run.ResultCollection.MSFeatureCounter = Run.ResultCollection.MSFeatureCounter -
                                                            Run.ResultCollection.IsosResultBin.Count;

                    _unsummedMSFeatures.AddRange(Run.ResultCollection.IsosResultBin);

                    // Iterate over unsummed MSFeatures and check for saturation
                    foreach (var isosResult in uimfRun.ResultCollection.IsosResultBin)
                    {
                        var msFeatureXYData = Run.XYData.TrimData(isosResult.IsotopicProfile.MonoPeakMZ - 10,
                                                                  isosResult.IsotopicProfile.MonoPeakMZ + 10);

                        var tempMZ      = isosResult.IsotopicProfile.MonoPeakMZ;
                        var currentScan = scanset.PrimaryScanNumber;

                        var isPossiblySaturated = isosResult.IntensityAggregate >
                                                  NewDeconToolsParameters.MiscMSProcessingParameters.SaturationThreshold;


                        // For debugging...
                        // UIMFIsosResult tempIsosResult = (UIMFIsosResult) isosResult;

                        // if (tempIsosResult.IMSScanSet.PrimaryScanNumber == 123)
                        // {
                        //    Console.WriteLine(tempIsosResult + "\t being processed!");
                        // }

                        if (isPossiblySaturated)
                        {
                            RebuildSaturatedIsotopicProfile(msFeatureXYData, isosResult, uimfRun.PeakList, out var theorIso);
                            AdjustSaturatedIsotopicProfile(isosResult.IsotopicProfile, theorIso, AdjustMonoIsotopicMasses, true);
                        }
                    }
                }


                // DisplayMSFeatures(_unsummedMSFeatures);

                // Now sum across IMSScans, deconvolute and adjust using saturation detection
                forceProgressMessage  = true;
                imsScanCountProcessed = 0;

                // Compute a buffer using .MaxMinutesPerFrame times 20%
                double maxFrameMinutesAddon = 0;
                var    bufferMinutes        = maxMinutesPerFrame * 0.2;
                if (bufferMinutes < 1)
                {
                    bufferMinutes = 1;
                }

                if (timeoutReached ||
                    DateTime.UtcNow.Subtract(frameStartTime).TotalMinutes + bufferMinutes >= maxMinutesPerFrame)
                {
                    // Maximum time per frame has been reached (or has almost been reached)
                    // Allow the next step to run for an additional bufferMinutes
                    maxFrameMinutesAddon = bufferMinutes;
                }

                foreach (var scanSet in uimfRun.IMSScanSetCollection.ScanSetList)
                {
                    var scanset = (IMSScanSet)scanSet;
                    if (DateTime.UtcNow.Subtract(dtLastProgress).TotalSeconds >= CONSOLE_INTERVAL_SECONDS || forceProgressMessage)
                    {
                        dtLastProgress       = DateTime.UtcNow;
                        forceProgressMessage = false;
                        Console.WriteLine("Processing frame " + lcScanSet.PrimaryScanNumber + ", scan " + scanset.PrimaryScanNumber + " (Summed)");
                    }

                    if (DateTime.UtcNow.Subtract(frameStartTime).TotalMinutes >= maxMinutesPerFrame + maxFrameMinutesAddon)
                    {
                        Console.WriteLine(
                            "Aborted processing of frame {0} because {1} minutes have elapsed (processing summed features); IMSScanCount processed = {2}",
                            lcScanSet.PrimaryScanNumber,
                            (int)DateTime.UtcNow.Subtract(frameStartTime).TotalMinutes,
                            imsScanCountProcessed);

                        break;
                    }

                    uimfRun.ResultCollection.IsosResultBin.Clear();  //clear any previous MSFeatures

                    // Get the summed isotopic profile
                    uimfRun.CurrentScanSet    = lcScanSet;
                    uimfRun.CurrentIMSScanSet = scanset;

                    ExecuteTask(MSGenerator);

                    if (NewDeconToolsParameters.MiscMSProcessingParameters.UseZeroFilling)
                    {
                        ExecuteTask(ZeroFiller);
                    }

                    if (NewDeconToolsParameters.MiscMSProcessingParameters.UseSmoothing)
                    {
                        ExecuteTask(Smoother);
                    }

                    ExecuteTask(PeakDetector);

                    imsScanCountProcessed++;

                    if (!SKIP_DECONVOLUTION)
                    {
                        ExecuteTask(Deconvolutor);


                        foreach (var isosResult in Run.ResultCollection.IsosResultBin)
                        {
                            var isPossiblySaturated = isosResult.IntensityAggregate >
                                                      NewDeconToolsParameters.MiscMSProcessingParameters.SaturationThreshold;



                            if (isPossiblySaturated)
                            {
                                var msFeatureXYData = Run.XYData.TrimData(isosResult.IsotopicProfile.MonoPeakMZ - 10,
                                                                          isosResult.IsotopicProfile.MonoPeakMZ + 10);


                                RebuildSaturatedIsotopicProfile(msFeatureXYData, isosResult, Run.PeakList, out var theorIso);
                                AdjustSaturatedIsotopicProfile(isosResult.IsotopicProfile, theorIso, AdjustMonoIsotopicMasses, false);

                                var currentScan = scanset.PrimaryScanNumber;
                                var currentMZ   = isosResult.IsotopicProfile.MonoPeakMZ;

                                UpdateReportedSummedPeakIntensities(isosResult, lcScanSet, scanset);
                            }
                            else
                            {
                            }

                            if (isosResult.IsotopicProfile.IsSaturated)
                            {
                                GetRebuiltFitScore(isosResult);
                            }
                        }
                    }


                    // Need to remove any duplicate MSFeatures (this occurs when incorrectly deisotoped profiles are built).
                    // Will do this by making the MSFeatureID the same. Then the Exporter will ensure that only one MSFeature per MSFeatureID
                    // is exported. This isn't ideal. Better to remove the features but this proves to be quite hard to do without large performance hits.
                    foreach (var isosResult in Run.ResultCollection.IsosResultBin)
                    {
                        double ppmToleranceForDuplicate = 20;
                        var    massTolForDuplicate      = ppmToleranceForDuplicate *
                                                          isosResult.IsotopicProfile.MonoIsotopicMass / 1e6;


                        var isosResultLocal      = isosResult;
                        var duplicateIsosResults = (from n in Run.ResultCollection.IsosResultBin
                                                    where
                                                    Math.Abs(n.IsotopicProfile.MonoIsotopicMass -
                                                             isosResultLocal.IsotopicProfile.MonoIsotopicMass) <
                                                    massTolForDuplicate && n.IsotopicProfile.ChargeState == isosResultLocal.IsotopicProfile.ChargeState
                                                    select n);

                        var minMSFeatureID = int.MaxValue;
                        foreach (var dup in duplicateIsosResults)
                        {
                            if (dup.MSFeatureID < minMSFeatureID)
                            {
                                minMSFeatureID = dup.MSFeatureID;
                            }
                            else
                            {
                                //here we have found a duplicate
                                dup.MSFeatureID = minMSFeatureID;

                                //because there are duplicates, we need to maintain the MSFeatureCounter so it doesn't skip values, as will
                                //happen when there are duplicates
                                //Run.ResultCollection.MSFeatureCounter--;
                            }
                        }
                    }

                    ExecuteTask(ResultValidator);

                    ExecuteTask(ScanResultUpdater);

                    if (NewDeconToolsParameters.ScanBasedWorkflowParameters.IsRefittingPerformed)
                    {
                        ExecuteTask(FitScoreCalculator);
                    }

                    // Allows derived classes to execute additional tasks
                    ExecuteOtherTasksHook();

                    if (ExportData)
                    {
                        // The following exporting tasks should be last
                        if (NewDeconToolsParameters.ScanBasedWorkflowParameters.ExportPeakData)
                        {
                            ExecuteTask(PeakToMSFeatureAssociator);
                            ExecuteTask(PeakListExporter);
                        }

                        ExecuteTask(IsosResultExporter);

                        ExecuteTask(ScanResultExporter);
                    }

                    ReportProgress();
                }

                frameCountProcessed++;

                if (DateTime.UtcNow.Subtract(startTime).TotalHours >= maxRuntimeHours)
                {
                    Console.WriteLine(
                        "Aborted processing because {0} hours have elapsed; Frames processed = {1}",
                        (int)DateTime.UtcNow.Subtract(startTime).TotalHours,
                        frameCountProcessed);

                    break;
                }
            }
        }
예제 #7
0
        public void GetUnsummedIntensitiesAndDetectSaturation(Run run, IEnumerable <IsosResult> resultList)
        {
            Check.Require(run != null, "SaturationDetector failed. Run is null");
            if (run == null)
            {
                return;
            }

            if (_msGenerator == null)
            {
                _msGenerator = MSGeneratorFactory.CreateMSGenerator(run.MSFileType);
            }

            if (run is UIMFRun uimfRun)
            {
                if (uimfRun.CurrentScanSet == null)
                {
                    throw new NullReferenceException("CurrentScanSet is null. You need to set it.");
                }
                if (uimfRun.CurrentIMSScanSet == null)
                {
                    throw new NullReferenceException("CurrentIMSScanSet is null. You need to set it.");
                }

                //this creates a FrameSet containing only the primary frame.  Therefore no summing will occur
                var lcScanSet = new ScanSet(uimfRun.CurrentScanSet.PrimaryScanNumber);

                //this creates a ScanSet containing only the primary scan.  Therefore no summing will occur
                var imsScanSet = new IMSScanSet(uimfRun.CurrentIMSScanSet.PrimaryScanNumber);

                //get the mass spectrum +/- 5 da from the range of the isotopicProfile

                uimfRun.CurrentScanSet    = lcScanSet;
                uimfRun.CurrentIMSScanSet = imsScanSet;
                _msGenerator.Execute(run.ResultCollection);
            }
            else
            {
                if (run.CurrentScanSet == null)
                {
                    throw new NullReferenceException("CurrentScanSet is null. You need to set it.");
                }

                //this creates a ScanSet containing only the primary scan.  Therefore no summing will occur
                var scanSet = new ScanSet(run.CurrentScanSet.PrimaryScanNumber);

                run.CurrentScanSet = scanSet;

                _msGenerator.Execute(run.ResultCollection);
            }

            foreach (var result in resultList)
            {
                var indexOfObsMostAbundant = result.IsotopicProfile.GetIndexOfMostIntensePeak();

                var mzOfMostAbundant = result.IsotopicProfile.Peaklist[indexOfObsMostAbundant].XValue;

                var indexOfUnsummedMostAbundantMZ = run.XYData.GetClosestXVal(mzOfMostAbundant);
                if (indexOfUnsummedMostAbundantMZ >= 0)
                {
                    result.IsotopicProfile.OriginalIntensity = run.XYData.Yvalues[indexOfUnsummedMostAbundantMZ];
                    result.IsotopicProfile.IsSaturated       = (result.IsotopicProfile.OriginalIntensity >=
                                                                SaturationThreshold);

                    if (result.IsotopicProfile.IsSaturated)
                    {
                        //problem is that with these saturated profiles, they are often truncated because another
                        //isotopic profile was falsely assigned to the back end of it. So we need to find more peaks that should
                        //belong to the saturated profile.
                        var theorTarget = new PeptideTarget
                        {
                            ChargeState      = (short)result.IsotopicProfile.ChargeState,
                            MonoIsotopicMass = result.IsotopicProfile.MonoIsotopicMass
                        };

                        theorTarget.MZ = (theorTarget.MonoIsotopicMass / theorTarget.ChargeState) + Globals.PROTON_MASS;

                        var averagineFormula = _tomIsotopicPatternGenerator.GetClosestAvnFormula(result.IsotopicProfile.MonoIsotopicMass, false);
                        theorTarget.IsotopicProfile  = _tomIsotopicPatternGenerator.GetIsotopePattern(averagineFormula, _tomIsotopicPatternGenerator.aafIsos);
                        theorTarget.EmpiricalFormula = averagineFormula;
                        theorTarget.CalculateMassesForIsotopicProfile(result.IsotopicProfile.ChargeState);

                        AssignMissingPeaksToSaturatedProfile(run.PeakList, result.IsotopicProfile, theorTarget.IsotopicProfile);

                        //goal is to find the index of the isotopic profile peaks of a peak that is not saturated

                        var indexOfGoodUnsaturatedPeak = -1;

                        for (var i = indexOfObsMostAbundant + 1; i < result.IsotopicProfile.Peaklist.Count; i++)
                        {
                            var indexUnsummedData = run.XYData.GetClosestXVal(result.IsotopicProfile.Peaklist[i].XValue);

                            var unsummedIntensity = run.XYData.Yvalues[indexUnsummedData];

                            if (unsummedIntensity < _minRelIntTheorProfile * SaturationThreshold)
                            {
                                indexOfGoodUnsaturatedPeak = i;
                                break;
                            }
                        }

                        AdjustSaturatedIsotopicProfile(result.IsotopicProfile, theorTarget.IsotopicProfile,
                                                       indexOfGoodUnsaturatedPeak);
                    }
                }

                //double summedIntensity = 0;
                //foreach (MSPeak peak in result.IsotopicProfile.Peaklist)
                //{
                //    int indexOfMZ = result.Run.XYData.GetClosestXVal(peak.XValue);
                //    if (indexOfMZ >= 0)
                //    {
                //        summedIntensity += result.Run.XYData.Yvalues[indexOfMZ];
                //    }
                //}

                //result.IsotopicProfile.OriginalTotalIsotopicAbundance = summedIntensity;
            }
        }
        public override void Execute(ResultCollection resultList)
        {
            Check.Require(resultList.Run.CurrentMassTag != null, this.Name + " failed. MassTag was not defined.");

            var currentResult = resultList.GetTargetedResult(resultList.Run.CurrentMassTag);

            if (msgen == null)
            {
                msgen = MSGeneratorFactory.CreateMSGenerator(resultList.Run.MSFileType);
                msgen.IsTICRequested = false;
            }

            var mt = resultList.Run.CurrentMassTag;

            float normalizedElutionTime;

            if (currentResult.Run.CurrentMassTag.ElutionTimeUnit == DeconTools.Backend.Globals.ElutionTimeUnit.ScanNum)
            {
                normalizedElutionTime = resultList.Run.CurrentMassTag.ScanLCTarget / (float)currentResult.Run.GetNumMSScans();
            }
            else
            {
                normalizedElutionTime = resultList.Run.CurrentMassTag.NormalizedElutionTime;
            }

            //collect Chrom peaks that fall within the NET tolerance
            var peaksWithinTol = new List <ChromPeak>(); //

            foreach (ChromPeak peak in resultList.Run.PeakList)
            {
                if (Math.Abs(peak.NETValue - normalizedElutionTime) <= Parameters.NETTolerance)     //peak.NETValue was determined by the ChromPeakDetector or a future ChromAligner Task
                {
                    peaksWithinTol.Add(peak);
                }
            }

            var peakQualityList = new List <ChromPeakQualityData>();

            //iterate over peaks within tolerance and score each peak according to MSFeature quality

            var tempMinScanWithinTol = (int)resultList.Run.NetAlignmentInfo.GetScanForNet(normalizedElutionTime - Parameters.NETTolerance);
            var tempMaxScanWithinTol = (int)resultList.Run.NetAlignmentInfo.GetScanForNet(normalizedElutionTime + Parameters.NETTolerance);
            var tempCenterTol        = (int)resultList.Run.NetAlignmentInfo.GetScanForNet(normalizedElutionTime);

            IqLogger.Log.Debug("SmartPeakSelector --> NETTolerance= " + Parameters.NETTolerance + ";  chromMinCenterMax= " + tempMinScanWithinTol + "\t" + tempCenterTol + "" +
                               "\t" + tempMaxScanWithinTol);
            IqLogger.Log.Debug("MT= " + currentResult.Target.ID + ";z= " + currentResult.Target.ChargeState + "; mz= " + currentResult.Target.MZ.ToString("0.000") + ";  ------------------------- PeaksWithinTol = " + peaksWithinTol.Count);

            currentResult.NumChromPeaksWithinTolerance = peaksWithinTol.Count;
            currentResult.NumQualityChromPeaks         = -1;

            ChromPeak bestChromPeak;

            if (currentResult.NumChromPeaksWithinTolerance > _parameters.NumChromPeaksAllowed)
            {
                bestChromPeak = null;
            }
            else
            {
                foreach (var chromPeak in peaksWithinTol)
                {
                    var pq = new ChromPeakQualityData(chromPeak);
                    peakQualityList.Add(pq);

                    var lcscanSet = ChromPeakUtilities.GetLCScanSetForChromPeak(chromPeak, resultList.Run, _parameters.NumMSSummedInSmartSelector);
                    resultList.Run.CurrentScanSet = lcscanSet;

                    //This resets the flags and the scores on a given result
                    currentResult.ResetResult();

                    //generate a mass spectrum
                    msgen.Execute(resultList);

                    //find isotopic profile
                    TargetedMSFeatureFinder.Execute(resultList);

                    try
                    {
                        //get fit score
                        fitScoreCalc.Execute(resultList);

                        //get i_score
                        resultValidator.Execute(resultList);
                    }
                    catch (Exception)
                    {
                        currentResult.FailedResult = true;
                    }

                    //collect the results together



                    AddScoresToPeakQualityData(pq, currentResult);
#if DEBUG
                    IqLogger.Log.Debug(pq.Display() + Environment.NewLine);
#endif
                }

                //run a algorithm that decides, based on fit score mostly.
                bestChromPeak = determineBestChromPeak(peakQualityList, currentResult);
            }

            currentResult.ChromPeakQualityList = peakQualityList;

            if (Parameters.SummingMode == SummingModeEnum.SUMMINGMODE_STATIC)
            {
                resultList.Run.CurrentScanSet = ChromPeakUtilities.GetLCScanSetForChromPeak(bestChromPeak, resultList.Run, Parameters.NumScansToSum);
            }
            else
            {
                resultList.Run.CurrentScanSet = ChromPeakUtilities.GetLCScanSetForChromPeakBasedOnPeakWidth(bestChromPeak, resultList.Run,
                                                                                                            Parameters.AreaOfPeakToSumInDynamicSumming, Parameters.MaxScansSummedInDynamicSumming);
            }

            UpdateResultWithChromPeakAndLCScanInfo(currentResult, bestChromPeak);
        }