public void peak_interference_UIMF_expectInterference_test1()
        {
            var uimfFrame1200_142 = FileRefs.RawDataBasePath + @"\UIMF\Sarc_MS_90_21Aug10_Cheetah_10-08-02_0000_frame1200_scan142.txt";

            Run run = new DeconTools.Backend.Runs.MSScanFromTextFileRun(uimfFrame1200_142);

            var scanSet = new ScanSet(0);

            run.CurrentScanSet = scanSet;

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

            msgen.MinMZ = 200;
            msgen.MaxMZ = 2000;

            var peakDetector = new DeconToolsPeakDetectorV2(4, 3, DeconTools.Backend.Globals.PeakFitType.QUADRATIC, true);

            var decon = new HornDeconvolutor();

            decon.MinIntensityForScore     = 10;
            decon.DeleteIntensityThreshold = 10;
            decon.MaxFitAllowed            = 0.4;
            decon.MinMZ         = 200;
            decon.MaxMZ         = 2000;
            decon.IsMZRangeUsed = false;


            var zeroFiller = new DeconToolsZeroFiller();

            msgen.Execute(run.ResultCollection);
            zeroFiller.Execute(run.ResultCollection);

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

            //Assert.AreEqual(93, run.ResultCollection.ResultList.Count);

            var testResult = run.ResultCollection.ResultList.Where(p => p.IsotopicProfile.MonoPeakMZ > 428 && p.IsotopicProfile.MonoPeakMZ < 430).First();

            var monoPeak = testResult.IsotopicProfile.getMonoPeak();
            var lastPeak = testResult.IsotopicProfile.Peaklist[testResult.IsotopicProfile.Peaklist.Count - 1];

            var startIndexOfXYData = MathUtils.BinarySearchWithTolerance(run.XYData.Xvalues, monoPeak.XValue - 3, 0, (run.XYData.Xvalues.Length - 1), 2);

            //interference scorer

            var interferenceScorer = new InterferenceScorer();

            var stopwatch = new Stopwatch();

            stopwatch.Start();
            var scanPeaks         = run.PeakList.Select <Peak, MSPeak>(i => (MSPeak)i).ToList();
            var interferenceScore = interferenceScorer.GetInterferenceScore(scanPeaks, testResult.IsotopicProfile.Peaklist, monoPeak.XValue - 1.1,
                                                                            lastPeak.XValue + lastPeak.Width);

            stopwatch.Stop();

            Console.WriteLine("interference= " + interferenceScore);
            Console.WriteLine("Time taken = " + stopwatch.ElapsedMilliseconds);
        }
Exemple #2
0
        public void Test1()
        {
            var saturationDetector = new SaturationDetector();

            var testFile = FileRefs.RawDataMSFiles.UIMFStdFile3;


            var run = (UIMFRun) new RunFactory().CreateRun(testFile);


            var msgen      = MSGeneratorFactory.CreateMSGenerator(run.MSFileType);
            var zeroFiller = new DeconToolsZeroFiller();


            var peakDetector = new DeconToolsPeakDetectorV2(4, 3, Globals.PeakFitType.QUADRATIC, false);

            var decon = new HornDeconvolutor();

            decon.LeftFitStringencyFactor  = 2.5;
            decon.RightFitStringencyFactor = 0.5;


            var startScan = 100;
            var stopScan  = 200;


            for (var i = startScan; i < stopScan; i++)
            {
                var frame = new ScanSet(500);

                var primaryScan = i;

                var scan = new ScanSet(primaryScan, primaryScan - 3, primaryScan + 3);


                run.CurrentScanSet = frame;
                run.CurrentScanSet = scan;


                msgen.Execute(run.ResultCollection);

                zeroFiller.Execute(run.ResultCollection);

                peakDetector.Execute(run.ResultCollection);

                decon.Execute(run.ResultCollection);

                saturationDetector.GetUnsummedIntensitiesAndDetectSaturation(run, run.ResultCollection.IsosResultBin);
            }


            var msfeatureElution = (from n in run.ResultCollection.ResultList
                                    where
                                    n.IsotopicProfile.MonoIsotopicMass > 1253.71 &&
                                    n.IsotopicProfile.MonoIsotopicMass < 1253.73
                                    select n);


            foreach (var result in msfeatureElution)
            {
                if (result.IsotopicProfile.IsSaturated || true)
                {
                    Console.WriteLine(result.ScanSet.PrimaryScanNumber + "\t" +
                                      result.IsotopicProfile.MonoPeakMZ.ToString("0.0000") + "\t" +
                                      result.IntensityAggregate + "\t" +
                                      result.IsotopicProfile.IntensityAggregateAdjusted);
                }
            }



            //TestUtilities.DisplayIsotopicProfileData(testIso1);
            //TestUtilities.DisplayPeaks(run.PeakList);
        }
        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;
                }
            }
        }
Exemple #4
0
        public void BPISaturationCorrectedTest1()
        {
            var filename = @"D:\Data\UIMF\Sarc_P13_C10_1186_23Sep11_Cheetah_11-09-06.uimf";
            var run      = new RunFactory().CreateRun(filename);

            run.ScanSetCollection = new ScanSetCollection();
            //scanSetCollection.Create(run, 500, 550,1,1);

            var startFrame = 477;
            var stopFrame  = 477;

            //startFrame = run.MinLCScan;
            //stopFrame = run.MaxLCScan;


            run.ScanSetCollection.Create(run, startFrame, stopFrame, 1, 1);



            ((UIMFRun)run).IMSScanSetCollection = new IMSScanSetCollection();
            ((UIMFRun)run).IMSScanSetCollection.Create(run, 122, 122, 1, 1);

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

            var peakDet = new DeconToolsPeakDetectorV2();

            peakDet.PeakToBackgroundRatio  = 2;
            peakDet.SignalToNoiseThreshold = 2;
            peakDet.IsDataThresholded      = true;


            var _zeroFiller = new DeconToolsZeroFiller();

            var _deconvolutor = new ThrashDeconvolutorV2();

            _deconvolutor.UseAutocorrelationChargeDetermination = true;
            _deconvolutor.Parameters.MaxFit = 0.8;


            var saturationWorkflow = new SaturationIMSScanBasedWorkflow(new DeconToolsParameters(), run);



            var outputFile = @"C:\Users\d3x720\Documents\PNNL\My_DataAnalysis\2013\Saturation_Correction\chromOuputSatCorrected.txt";

            using (var writer = new StreamWriter(outputFile))
            {
                var uimfRun = (UIMFRun)run;


                var _unsummedMSFeatures = new List <IsosResult>();

                foreach (var lcScanSet in run.ScanSetCollection.ScanSetList)
                {
                    Console.WriteLine("Scanset= " + lcScanSet);


                    uimfRun.ResultCollection.MSPeakResultList.Clear();
                    _unsummedMSFeatures.Clear();


                    var unsummedFrameSet = new ScanSet(lcScanSet.PrimaryScanNumber);
                    //get saturated MSFeatures for unsummed data
                    uimfRun.CurrentScanSet = unsummedFrameSet;


                    var sb = new StringBuilder();

                    var errorInFrame = false;
                    var numIMSScans  = uimfRun.IMSScanSetCollection.ScanSetList.Count;
                    for (var imsScanNum = 0; imsScanNum < numIMSScans; imsScanNum++)
                    {
                        var imsScanSet = uimfRun.IMSScanSetCollection.ScanSetList[imsScanNum];
                        uimfRun.ResultCollection.IsosResultBin.Clear(); //clear any previous MSFeatures

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

                        try
                        {
                            msgen.Execute(run.ResultCollection);

                            _zeroFiller.Execute(run.ResultCollection);

                            peakDet.Execute(run.ResultCollection);

                            _deconvolutor.Deconvolute(uimfRun.ResultCollection); //adds to IsosResultBin
                        }
                        catch (Exception ex)
                        {
                            errorInFrame = true;
                        }

                        _unsummedMSFeatures.AddRange(run.ResultCollection.IsosResultBin);


                        var basePeakIntensity = 0;
                        //iterate over unsummed MSFeatures and check for saturation
                        foreach (var isosResult in uimfRun.ResultCollection.IsosResultBin)
                        {
                            var isPossiblySaturated = isosResult.IntensityAggregate > 1e7;

                            if (isPossiblySaturated)
                            {
                                var theorIso = new IsotopicProfile();

                                saturationWorkflow.RebuildSaturatedIsotopicProfile(run.XYData, isosResult, uimfRun.PeakList, out theorIso);
                                saturationWorkflow.AdjustSaturatedIsotopicProfile(isosResult.IsotopicProfile, theorIso, true, true);
                            }


                            TestUtilities.DisplayIsotopicProfileData(isosResult.IsotopicProfile);


                            var height = (int)isosResult.IsotopicProfile.getMostIntensePeak().Height;
                            if (height > basePeakIntensity)
                            {
                                basePeakIntensity = height;
                            }
                        }


                        sb.Append(basePeakIntensity);

                        var isNotLastIMSScan = imsScanNum != numIMSScans - 1;
                        if (isNotLastIMSScan)
                        {
                            sb.Append("\t");
                        }
                    }

                    if (errorInFrame)
                    {
                    }
                    else
                    {
                        writer.WriteLine(sb.ToString());
                    }
                }
            }
        }