Beispiel #1
0
        /// <summary>
        /// Calculates Metrics based on ChromPeakIqTarget
        /// NET Error, Mass Error, Isotopic Fit, & Isotope Correlation
        /// </summary>
        protected override void ExecuteWorkflow(IqResult result)
        {
            result.IsExported = false;

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

            var target = result.Target as ChromPeakIqTarget;

            if (target == null)
            {
                throw new NullReferenceException("The ChromPeakAnalyzerIqWorkflow only works with the ChromPeakIqTarget.");
            }

            MSGenerator.MinMZ = target.MZTheor - 2;
            MSGenerator.MaxMZ = target.MZTheor + 5;

            //Sums Scan

            var lcscanset = _chromPeakUtilities.GetLCScanSetForChromPeak(target.ChromPeak, Run, WorkflowParameters.NumMSScansToSum);

            //Generate a mass spectrum
            var massSpectrumXYData = MSGenerator.GenerateMS(Run, lcscanset);

            //massSpectrumXYData = massSpectrumXYData.TrimData(result.Target.MZTheor - 5, result.Target.MZTheor + 15);

            //Find isotopic profile
            List <Peak> mspeakList;

            result.ObservedIsotopicProfile = TargetedMSFeatureFinder.IterativelyFindMSFeature(massSpectrumXYData, target.TheorIsotopicProfile, out mspeakList);


            //Get NET Error
            var netError = target.ChromPeak.NETValue - target.ElutionTimeTheor;


            var leftOfMonoPeakLooker = new LeftOfMonoPeakLooker();
            var peakToTheLeft        = leftOfMonoPeakLooker.LookforPeakToTheLeftOfMonoPeak(target.TheorIsotopicProfile.getMonoPeak(), target.ChargeState, mspeakList);

            var hasPeakTotheLeft = peakToTheLeft != null;

            if (result.ObservedIsotopicProfile == null)
            {
                result.IsotopicProfileFound = false;
                result.FitScore             = 1;
            }
            else
            {
                //Get fit score
                var observedIsoList      = result.ObservedIsotopicProfile.Peaklist.Cast <Peak>().ToList();
                var minIntensityForScore = 0.05;
                var fitScore             = PeakFitter.GetFit(target.TheorIsotopicProfile.Peaklist.Select(p => (Peak)p).ToList(), observedIsoList, minIntensityForScore, WorkflowParameters.MSToleranceInPPM);

                //get i_score
                var iscore = InterferenceScorer.GetInterferenceScore(result.ObservedIsotopicProfile, mspeakList);

                //get ppm error
                var massErrorInDaltons = TheorMostIntensePeakMassError(target.TheorIsotopicProfile, result.ObservedIsotopicProfile, target.ChargeState);
                var ppmError           = (massErrorInDaltons / target.MonoMassTheor) * 1e6;

                //Get Isotope Correlation
                var    scan = lcscanset.PrimaryScanNumber;
                double chromScanWindowWidth = target.ChromPeak.Width * 2;

                //Determines where to start and stop chromatogram correlation
                var startScan = scan - (int)Math.Round(chromScanWindowWidth / 2, 0);
                var stopScan  = scan + (int)Math.Round(chromScanWindowWidth / 2, 0);

                result.CorrelationData             = ChromatogramCorrelator.CorrelateData(Run, result, startScan, stopScan);
                result.LcScanObs                   = lcscanset.PrimaryScanNumber;
                result.ChromPeakSelected           = target.ChromPeak;
                result.LCScanSetSelected           = new ScanSet(lcscanset.PrimaryScanNumber);
                result.IsotopicProfileFound        = true;
                result.FitScore                    = fitScore;
                result.InterferenceScore           = iscore;
                result.IsIsotopicProfileFlagged    = hasPeakTotheLeft;
                result.NETError                    = netError;
                result.MassErrorBefore             = ppmError;
                result.IqResultDetail.MassSpectrum = massSpectrumXYData;
                result.Abundance                   = GetAbundance(result);
            }

            Display(result);
        }
        public List <ChromPeakQualityData> GetChromPeakQualityData(Run run, IqTarget target, List <Peak> chromPeakList)
        {
            var peakQualityList = new List <ChromPeakQualityData>();


            if (MSGenerator == null)
            {
                MSGenerator = MSGeneratorFactory.CreateMSGenerator(run.MSFileType);
                MSGenerator.IsTICRequested = false;
            }

            //iterate over peaks within tolerance and score each peak according to MSFeature quality
#if DEBUG
            var tempMinScanWithinTol = (int)run.NetAlignmentInfo.GetScanForNet(target.ElutionTimeTheor - Parameters.ChromNETTolerance);
            var tempMaxScanWithinTol = (int)run.NetAlignmentInfo.GetScanForNet(target.ElutionTimeTheor + Parameters.ChromNETTolerance);
            var tempCenterTol        = (int)run.NetAlignmentInfo.GetScanForNet(target.ElutionTimeTheor);


            Console.WriteLine("SmartPeakSelector --> NETTolerance= " + Parameters.ChromNETTolerance + ";  chromMinCenterMax= " +
                              tempMinScanWithinTol + "\t" + tempCenterTol + "" +
                              "\t" + tempMaxScanWithinTol);
            Console.WriteLine("MT= " + target.ID + ";z= " + target.ChargeState + "; mz= " + target.MZTheor.ToString("0.000") +
                              ";  ------------------------- PeaksWithinTol = " + chromPeakList.Count);
#endif



            //target.NumChromPeaksWithinTolerance = peaksWithinTol.Count;


            foreach (ChromPeak chromPeak in chromPeakList)
            {
                // TODO: Currently hard-coded to sum only 1 scan
                var lcscanset = _chromPeakUtilities.GetLCScanSetForChromPeak(chromPeak, run, 1);

                //generate a mass spectrum
                var massSpectrumXYData = MSGenerator.GenerateMS(run, lcscanset);

                //find isotopic profile
                var mspeakList  = new List <Peak>();
                var observedIso = TargetedMSFeatureFinder.IterativelyFindMSFeature(massSpectrumXYData, target.TheorIsotopicProfile, out mspeakList);

                double fitScore = 1;

                double iscore = 1;

                //get fit score
                fitScore = FitScoreCalc.CalculateFitScore(target.TheorIsotopicProfile, observedIso, massSpectrumXYData);

                //get i_score
                iscore = InterferenceScorer.GetInterferenceScore(target.TheorIsotopicProfile, mspeakList);

                var leftOfMonoPeakLooker = new LeftOfMonoPeakLooker();
                var peakToTheLeft        = leftOfMonoPeakLooker.LookforPeakToTheLeftOfMonoPeak(target.TheorIsotopicProfile.getMonoPeak(), target.ChargeState,
                                                                                               mspeakList);


                var hasPeakTotheLeft = peakToTheLeft != null;

                //collect the results together


                var pq = new ChromPeakQualityData(chromPeak);

                if (observedIso == null)
                {
                    pq.IsotopicProfileFound = false;
                }
                else
                {
                    pq.IsotopicProfileFound     = true;
                    pq.Abundance                = observedIso.IntensityMostAbundant;
                    pq.FitScore                 = fitScore;
                    pq.InterferenceScore        = iscore;
                    pq.IsotopicProfile          = observedIso;
                    pq.IsIsotopicProfileFlagged = hasPeakTotheLeft;
                    pq.ScanLc = lcscanset.PrimaryScanNumber;
                }

                peakQualityList.Add(pq);
#if DEBUG
                pq.Display();
#endif
            }

            return(peakQualityList);
        }
Beispiel #3
0
        /// <summary>
        /// Calculates Metrics based on ChromPeakIqTarget
        /// NET Error, Mass Error, Isotopic Fit, & Isotope Correlation
        /// </summary>
        protected override void ExecuteWorkflow(IqResult result)
        {
            result.IsExported = false;

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

            var target = result.Target as ChromPeakIqTarget;

            if (target == null)
            {
                throw new NullReferenceException("The ChromPeakAnalyzerIqWorkflow only works with the ChromPeakIqTarget.");
            }


            var lcscanset = _chromPeakUtilities.GetLCScanSetForChromPeak(target.ChromPeak, Run, WorkflowParameters.SmartChromPeakSelectorNumMSSummed);

            //Generate a mass spectrum
            var massSpectrumXYData = MSGenerator.GenerateMS(Run, lcscanset);

            massSpectrumXYData = massSpectrumXYData.TrimData(result.Target.MZTheor - 5, result.Target.MZTheor + 15);

            //Find isotopic profile
            List <Peak> mspeakList;

            result.ObservedIsotopicProfile = TargetedMSFeatureFinder.IterativelyFindMSFeature(massSpectrumXYData, target.TheorIsotopicProfile, out mspeakList);

            //Default Worst Scores
            double iscore = 1;

            //Get NET Error
            var netError = target.ChromPeak.NETValue - target.ElutionTimeTheor;


            var leftOfMonoPeakLooker = new LeftOfMonoPeakLooker();
            var peakToTheLeft        = leftOfMonoPeakLooker.LookforPeakToTheLeftOfMonoPeak(target.TheorIsotopicProfile.getMonoPeak(), target.ChargeState, mspeakList);

            var hasPeakTotheLeft = peakToTheLeft != null;

            if (result.ObservedIsotopicProfile == null)
            {
                result.IsotopicProfileFound = false;
                result.FitScore             = 1;
            }
            else
            {
                //Get fit score O16 profile
                var observedIsoList = result.ObservedIsotopicProfile.Peaklist.Cast <Peak>().Take(4).ToList();    //first 4 peaks excludes the O18 double label peak (fifth peak)
                var theorPeakList   = target.TheorIsotopicProfile.Peaklist.Select(p => (Peak)p).Take(4).ToList();
                result.FitScore = PeakFitter.GetFit(theorPeakList, observedIsoList, 0.05, WorkflowParameters.MSToleranceInPPM);

                // fit score O18 profile
                var o18Iso = ((O16O18IqResult)result).ConvertO16ProfileToO18(target.TheorIsotopicProfile, 4);
                theorPeakList   = o18Iso.Peaklist.Select(p => (Peak)p).ToList();
                observedIsoList = result.ObservedIsotopicProfile.Peaklist.Cast <Peak>().Skip(4).ToList();    //skips the first 4 peaks and thus includes the O18 double label isotopic profile
                ((O16O18IqResult)result).FitScoreO18Profile = PeakFitter.GetFit(theorPeakList, observedIsoList, 0.05, WorkflowParameters.MSToleranceInPPM);

                //get i_score
                iscore = InterferenceScorer.GetInterferenceScore(result.ObservedIsotopicProfile, mspeakList);

                //get ppm error
                var massErrorInDaltons = TheorMostIntensePeakMassError(target.TheorIsotopicProfile, result.ObservedIsotopicProfile, target.ChargeState);
                var ppmError           = (massErrorInDaltons / target.MonoMassTheor) * 1e6;

                //Get Isotope Correlation
                var scan = lcscanset.PrimaryScanNumber;

                var sigma = target.ChromPeak.Width / 2.35;
                var chromScanWindowWidth = 4 * sigma;

                //Determines where to start and stop chromatogram correlation
                var startScan = scan - (int)Math.Round(chromScanWindowWidth / 2, 0);
                var stopScan  = scan + (int)Math.Round(chromScanWindowWidth / 2, 0);

                result.CorrelationData          = ChromatogramCorrelator.CorrelateData(Run, result, startScan, stopScan);
                result.LcScanObs                = lcscanset.PrimaryScanNumber;
                result.ChromPeakSelected        = target.ChromPeak;
                result.LCScanSetSelected        = new ScanSet(lcscanset.PrimaryScanNumber);
                result.IsotopicProfileFound     = true;
                result.InterferenceScore        = iscore;
                result.IsIsotopicProfileFlagged = hasPeakTotheLeft;
                result.NETError                    = netError;
                result.MassErrorBefore             = ppmError;
                result.IqResultDetail.MassSpectrum = massSpectrumXYData;
                result.Abundance                   = GetAbundance(result);
            }
        }
        protected override void ExecuteWorkflow(IqResult result)
        {
            var children = result.Target.ChildTargets().ToList();

            foreach (var child in children)
            {
                child.DoWorkflow();
                var childResult = child.GetResult();

                var chromPeakLevelResults = childResult.ChildResults();

                var filteredChromPeakResults = chromPeakLevelResults.Where(r => r.IsotopicProfileFound).ToList();

                childResult.FavoriteChild = SelectBestChromPeakIqResult(childResult, filteredChromPeakResults);

                GetDataFromFavoriteChild(childResult);
            }

            result.FavoriteChild = SelectBestChargeStateChildResult(result);
            GetDataFromFavoriteChild(result);

            var favResult = result.FavoriteChild;

            double?rsquaredVal, slope;

            getRsquaredVal(result, out rsquaredVal, out slope);
            var favChargeState = result.FavoriteChild == null ? 0 : result.FavoriteChild.Target.ChargeState;
            var favMz          = result.FavoriteChild == null ? 0 : result.FavoriteChild.Target.MZTheor;

            if (!_headerLogged)
            {
                _headerLogged = true;
                IqLogger.Log.Info("\t" + "TargetID" + "\t\t\t" + "M/Z" + "\t" + "Charge" + "\t" + "LCScan" + "\t" + "RSquared" + "\t" + "Slope");
            }

            IqLogger.Log.Info("\t" + result.Target.ID + "\t\t\t" + favMz.ToString("0.000") + "\t" + favChargeState + "\t" + result.LcScanObs + "\t" + rsquaredVal + "\t" + slope);

            //now get the mass spectrum given the info from the favorite child charge state result

            if (favResult != null)
            {
                var scanset = new ScanSetFactory().CreateScanSet(Run, favResult.LCScanSetSelected.PrimaryScanNumber,
                                                                 WorkflowParameters.NumMSScansToSum);

                var selectedChromPeak    = favResult.ChromPeakSelected;
                var sigma                = selectedChromPeak.Width / 2.35;
                var chromScanWindowWidth = 4 * sigma;

                //Determines where to start and stop chromatogram correlation
                var startScan = scanset.PrimaryScanNumber - (int)Math.Round(chromScanWindowWidth / 2, 0);
                var stopScan  = scanset.PrimaryScanNumber + (int)Math.Round(chromScanWindowWidth / 2, 0);

                var massSpectrum = MSGenerator.GenerateMS(Run, scanset);

                foreach (var iqTarget in children)
                {
                    var childStateIqResult = (O16O18IqResult)iqTarget.GetResult();

                    childStateIqResult.IqResultDetail.MassSpectrum = massSpectrum.TrimData(iqTarget.MZTheor - 3, iqTarget.MZTheor + 8);

                    var mspeakList = _mspeakDetector.FindPeaks(childStateIqResult.IqResultDetail.MassSpectrum.Xvalues,
                                                               childStateIqResult.IqResultDetail.MassSpectrum.Yvalues);


                    childStateIqResult.CorrelationData = ChromatogramCorrelator.CorrelateData(Run, childStateIqResult, startScan, stopScan);


                    childStateIqResult.CorrelationO16O18SingleLabel           = childStateIqResult.GetCorrelationO16O18SingleLabel();
                    childStateIqResult.CorrelationO16O18DoubleLabel           = childStateIqResult.GetCorrelationO16O18DoubleLabel();
                    childStateIqResult.CorrelationBetweenSingleAndDoubleLabel = childStateIqResult.GetCorrelationBetweenSingleAndDoubleLabel();

                    childStateIqResult.RatioO16O18DoubleLabel   = childStateIqResult.GetRatioO16O18DoubleLabel();
                    childStateIqResult.RatioO16O18SingleLabel   = childStateIqResult.GetRatioO16O18SingleLabel();
                    childStateIqResult.RatioSingleToDoubleLabel = childStateIqResult.GetRatioSingleToDoubleLabel();

                    childStateIqResult.ObservedIsotopicProfile = MsfeatureFinder.IterativelyFindMSFeature(childStateIqResult.IqResultDetail.MassSpectrum,
                                                                                                          iqTarget.TheorIsotopicProfile);


                    if (childStateIqResult.ObservedIsotopicProfile != null)
                    {
                        var observedIsoList = childStateIqResult.ObservedIsotopicProfile.Peaklist.Cast <Peak>().Take(4).ToList();    //first 4 peaks excludes the O18 double label peak (fifth peak)
                        var theorPeakList   = iqTarget.TheorIsotopicProfile.Peaklist.Select(p => (Peak)p).Take(4).ToList();
                        childStateIqResult.FitScore = PeakFitter.GetFit(theorPeakList, observedIsoList, 0.05, WorkflowParameters.MSToleranceInPPM);

                        var o18Iso = childStateIqResult.ConvertO16ProfileToO18(iqTarget.TheorIsotopicProfile, 4);
                        theorPeakList   = o18Iso.Peaklist.Select(p => (Peak)p).ToList();
                        observedIsoList = childStateIqResult.ObservedIsotopicProfile.Peaklist.Cast <Peak>().Skip(4).ToList();    //skips the first 4 peaks and thus includes the O18 double label isotopic profile
                        childStateIqResult.FitScoreO18Profile = PeakFitter.GetFit(theorPeakList, observedIsoList, 0.05, WorkflowParameters.MSToleranceInPPM);



                        childStateIqResult.InterferenceScore     = InterferenceScorer.GetInterferenceScore(childStateIqResult.ObservedIsotopicProfile, mspeakList);
                        childStateIqResult.MZObs                 = childStateIqResult.ObservedIsotopicProfile.MonoPeakMZ;
                        childStateIqResult.MonoMassObs           = childStateIqResult.ObservedIsotopicProfile.MonoIsotopicMass;
                        childStateIqResult.MZObsCalibrated       = Run.GetAlignedMZ(childStateIqResult.MZObs);
                        childStateIqResult.MonoMassObsCalibrated = (childStateIqResult.MZObsCalibrated - DeconTools.Backend.Globals.PROTON_MASS) * childStateIqResult.Target.ChargeState;
                        childStateIqResult.ElutionTimeObs        = ((ChromPeak)favResult.ChromPeakSelected).NETValue;
                    }
                    else
                    {
                        childStateIqResult.FitScore          = -1;
                        childStateIqResult.InterferenceScore = -1;
                    }


                    getRsquaredVal(childStateIqResult, out rsquaredVal, out slope);
                    IqLogger.Log.Info("\t\t\t" + childStateIqResult.Target.ID + "\t" + childStateIqResult.Target.MZTheor.ToString("0.000") + "\t" + childStateIqResult.Target.ChargeState
                                      + "\t" + childStateIqResult.LcScanObs + "\t" + childStateIqResult.FitScore.ToString("0.000") + "\t" + rsquaredVal + "\t" + slope);


                    childStateIqResult.LCScanSetSelected = favResult.LCScanSetSelected;
                    childStateIqResult.LcScanObs         = favResult.LcScanObs;

                    if (GraphsAreOutputted)
                    {
                        if (_graphGenerator == null)
                        {
                            _graphGenerator = new BasicGraphControl();
                        }

                        ExportGraphs(childStateIqResult);
                    }
                }
            }
        }
Beispiel #5
0
        protected virtual void ExecuteWorkflow(IqResult result)
        {
            result.Target.TheorIsotopicProfile = TheorFeatureGen.GenerateTheorProfile(result.Target.EmpiricalFormula, result.Target.ChargeState);

            result.IqResultDetail.Chromatogram = ChromGen.GenerateChromatogram(Run, result.Target.TheorIsotopicProfile, result.Target.ElutionTimeTheor);

            result.IqResultDetail.Chromatogram = ChromSmoother.Smooth(result.IqResultDetail.Chromatogram);

            result.ChromPeakList = ChromPeakDetector.FindPeaks(result.IqResultDetail.Chromatogram);

            ChromPeakDetector.CalculateElutionTimes(Run, result.ChromPeakList);

            ChromPeakDetector.FilterPeaksOnNET(WorkflowParameters.ChromNETTolerance, result.Target.ElutionTimeTheor, result.ChromPeakList);

            result.IqResultDetail.ChromPeakQualityData = ChromPeakAnalyzer.GetChromPeakQualityData(Run, result.Target, result.ChromPeakList);

            var filterOutFlagged = result.Target.TheorIsotopicProfile.GetIndexOfMostIntensePeak() == 0;

            result.ChromPeakSelected = ChromPeakSelector.SelectBestPeak(result.IqResultDetail.ChromPeakQualityData, filterOutFlagged);


            result.LCScanSetSelected = ChromPeakUtilities.GetLCScanSetForChromPeak(result.ChromPeakSelected, Run,
                                                                                   WorkflowParameters.NumMSScansToSum);

            result.LcScanObs = result.LCScanSetSelected == null ? -1 : result.LCScanSetSelected.PrimaryScanNumber;

            result.IqResultDetail.MassSpectrum = MSGenerator.GenerateMS(Run, result.LCScanSetSelected);

            TrimData(result.IqResultDetail.MassSpectrum, result.Target.MZTheor, MsLeftTrimAmount, MsRightTrimAmount);

            List <Peak> mspeakList;

            result.ObservedIsotopicProfile = MsfeatureFinder.IterativelyFindMSFeature(result.IqResultDetail.MassSpectrum, result.Target.TheorIsotopicProfile, out mspeakList);


            result.FitScore = FitScoreCalc.CalculateFitScore(result.Target.TheorIsotopicProfile, result.ObservedIsotopicProfile,
                                                             result.IqResultDetail.MassSpectrum);

            result.InterferenceScore = InterferenceScorer.GetInterferenceScore(result.ObservedIsotopicProfile, mspeakList);

            //if (_workflowParameters.ChromatogramCorrelationIsPerformed)
            //{
            //    ExecuteTask(_chromatogramCorrelator);
            //}

            result.MonoMassObs = result.ObservedIsotopicProfile == null ? 0 : result.ObservedIsotopicProfile.MonoIsotopicMass;

            result.MZObs = result.ObservedIsotopicProfile == null ? 0 : result.ObservedIsotopicProfile.MonoPeakMZ;

            result.MZObsCalibrated       = result.ObservedIsotopicProfile == null ? 0 : Run.GetAlignedMZ(result.ObservedIsotopicProfile.MonoPeakMZ, result.LcScanObs);
            result.MonoMassObsCalibrated = result.ObservedIsotopicProfile == null
                                               ? 0
                                               : (result.MZObsCalibrated - DeconTools.Backend.Globals.PROTON_MASS) * result.Target.ChargeState;


            var elutionTime = result.ChromPeakSelected == null ? 0d : ((ChromPeak)result.ChromPeakSelected).NETValue;

            result.ElutionTimeObs = elutionTime;

            result.Abundance = GetAbundance(result);
        }