Exemple #1
0
        /// <summary>
        /// Get the scorer by spectrum.
        /// This creates the scorer directly for that spectrum. It bypasses the scorer cache.
        /// </summary>
        /// <param name="spectrum">The spectrum number to get the scorer for.</param>
        /// <param name="precursorMass">The precursor mass to get the scorer for.</param>
        /// <param name="precursorCharge">The precursor charge to get the scorer for.</param>
        /// <returns>The scorer selected based on the arguments.</returns>
        public IScorer GetScorer(ProductSpectrum spectrum, double precursorMass, int precursorCharge, ActivationMethod activationMethod = ActivationMethod.Unknown)
        {
            var parameters = this.scoringParameterSet.GetScoringParameters(spectrum.ActivationMethod, precursorMass);
            var targetedDeconvolutedSpectrum = new TargetedDeconvolutedSpectrum(spectrum, 1, precursorCharge - 1);

            return(new FlipScorer <TargetedDeconvolutedSpectrum>(parameters, targetedDeconvolutedSpectrum));
        }
Exemple #2
0
            internal DeconvScorer(ProductSpectrum deconvolutedSpectrum, Tolerance productTolerance)
            {
                if (deconvolutedSpectrum.ActivationMethod != ActivationMethod.ETD)
                {
                    _prefixOffsetMass = BaseIonType.B.OffsetComposition.Mass;
                    _suffixOffsetMass = BaseIonType.Y.OffsetComposition.Mass;
                }
                else
                {
                    _prefixOffsetMass = BaseIonType.C.OffsetComposition.Mass;
                    _suffixOffsetMass = BaseIonType.Z.OffsetComposition.Mass;
                }

                _ionMassBins = new HashSet <int>();
                foreach (var p in deconvolutedSpectrum.Peaks)
                {
                    var mass      = p.Mz;
                    var deltaMass = productTolerance.GetToleranceAsDa(mass, 1);
                    var minMass   = mass - deltaMass;
                    var maxMass   = mass + deltaMass;

                    var minBinNum = GetBinNumber(minMass);
                    var maxBinNum = GetBinNumber(maxMass);
                    for (var binNum = minBinNum; binNum <= maxBinNum; binNum++)
                    {
                        _ionMassBins.Add(binNum);
                    }
                }
            }
 public FitBasedLogLikelihoodRatioScorer(ProductSpectrum ms2Spec, Tolerance tolerance, int minCharge, int maxCharge)
 {
     _ms2Spec      = ms2Spec;
     _tolerance    = tolerance;
     _minCharge    = minCharge;
     _maxCharge    = maxCharge;
     _baseIonTypes = ms2Spec.ActivationMethod != ActivationMethod.ETD ? BaseIonTypesCID : BaseIonTypesETD;
 }
Exemple #4
0
        /// <summary>
        /// Get the correct scorer based on the parameters supplied to this factory.
        /// </summary>
        /// <param name="productSpectrum">The spectrum that the scorer should calculate scores from.</param>
        /// <returns>A scorer.</returns>
        public IScorer GetScorer(ProductSpectrum productSpectrum)
        {
            if (this.model == null)
            {
                return(new CorrMatchedPeakCounter(productSpectrum, this.tolerance, this.minCharge, this.maxCharge, this.corrScoreThreshold));
            }

            return(new LikelihoodScorer(this.model, productSpectrum, this.tolerance, this.minCharge, this.maxCharge));
        }
Exemple #5
0
        public IcScores GetScores(ProductSpectrum spec, string seqStr, Composition composition, int charge, int ms2ScanNum)
        {
            if (spec == null)
            {
                return(null);
            }
            var scorer   = new CompositeScorer(spec, Tolerance, MinProductCharge, Math.Min(MaxProductCharge, charge), activationMethod: ActivationMethod);
            var seqGraph = SequenceGraph.CreateGraph(AminoAcidSet, AminoAcid.ProteinNTerm, seqStr, AminoAcid.ProteinCTerm);

            if (seqGraph == null)
            {
                return(null);
            }

            var bestScore = double.NegativeInfinity;
            Tuple <double, string> bestScoreAndModifications = null;
            var protCompositions = seqGraph.GetSequenceCompositions();

            for (var modIndex = 0; modIndex < protCompositions.Length; modIndex++)
            {
                seqGraph.SetSink(modIndex);
                var protCompositionWithH2O = seqGraph.GetSinkSequenceCompositionWithH2O();

                if (!protCompositionWithH2O.Equals(composition))
                {
                    continue;
                }

                var curScoreAndModifications = seqGraph.GetFragmentScoreAndModifications(scorer);
                var curScore = curScoreAndModifications.Item1;

                if (!(curScore > bestScore))
                {
                    continue;
                }

                bestScoreAndModifications = curScoreAndModifications;
                bestScore = curScore;
            }

            if (bestScoreAndModifications == null)
            {
                return(null);
            }

            var modifications = bestScoreAndModifications.Item2;
            var seqObj        = Sequence.CreateSequence(seqStr, modifications, AminoAcidSet);

            double score;
            int    nMatchedFragments;

            GetCompositeScores(seqObj, charge, ms2ScanNum, out score, out nMatchedFragments);
            return(new IcScores(nMatchedFragments, score, modifications));
        }
Exemple #6
0
        public LikelihoodScorer(LikelihoodScoringModel model, ProductSpectrum ms2Spec, Tolerance tolerance, int minCharge, int maxCharge, bool massErrorScore = true)
            : base(ms2Spec, tolerance, minCharge, maxCharge, 0.1)
        {
            //_model = model;

            //var refIntensity = ms2Spec.Peaks.Max(p => p.Intensity) * 0.1;
            //var medIntensity = ms2Spec.Peaks.Select(p => p.Intensity).Median();
            //_refIntensity = Math.Min(medIntensity, refIntensity);

            _refIntensity = GetRefIntensity(ms2Spec.Peaks);

            _includeMassErrorScore = massErrorScore;
        }
        public void OutputStatistics(ProductSpectrum spectrum, Sequence sequence)
        {
            var baseIonTypes = spectrum.ActivationMethod != ActivationMethod.ETD ? BaseIonTypesCid : BaseIonTypesEtd;
            var cleavages    = sequence.GetInternalCleavages().ToArray();
            var tolerance    = new Tolerance(10);

            var maxIntensity = spectrum.Peaks.Max(p => p.Intensity);

            foreach (var c in cleavages)
            {
                foreach (var baseIonType in baseIonTypes)
                {
                    var fragmentComposition = baseIonType.IsPrefix
                        ? c.PrefixComposition + baseIonType.OffsetComposition
                        : c.SuffixComposition + baseIonType.OffsetComposition;

                    for (int charge = MinCharge; charge <= MaxCharge; charge++)
                    {
                        var ion           = new Ion(fragmentComposition, charge);
                        var observedPeaks = spectrum.GetAllIsotopePeaks(ion, tolerance, RelativeIsotopeIntensityThreshold);

                        if (observedPeaks == null)
                        {
                            continue;
                        }

                        var mostAbundantIsotopeIndex = ion.Composition.GetMostAbundantIsotopeZeroBasedIndex();


                        // representative peak intensity
                        var ionPeakIntensity = observedPeaks[mostAbundantIsotopeIndex].Intensity;

                        // calc. correlation
                        var isotopomerEnvelope  = ion.Composition.GetIsotopomerEnvelopeRelativeIntensities();
                        var observedIntensities = new double[observedPeaks.Length];
                        for (var i = 0; i < observedPeaks.Length; i++)
                        {
                            var observedPeak = observedPeaks[i];
                            observedIntensities[i] = observedPeak != null ? (float)observedPeak.Intensity : 0.0;
                        }
                        var corrCoeff = FitScoreCalculator.GetPearsonCorrelation(isotopomerEnvelope, observedIntensities);

                        // mz error
                        var mostAbundantIsotopeMz = ion.GetIsotopeMz(mostAbundantIsotopeIndex);
                        var errorPpm = ((observedPeaks[mostAbundantIsotopeIndex].Mz - mostAbundantIsotopeMz) /
                                        mostAbundantIsotopeMz) * 1e6;
                    }
                }
            }
        }
Exemple #8
0
        public SequenceTagFinder(ProductSpectrum spec, Tolerance tolerance, int minTagLength = 5, int maxTagLength = 8, AminoAcid[] aminoAcidsArray = null)
            : base(maxTagLength)
        {
            var baseIonTypes   = spec.ActivationMethod != ActivationMethod.ETD ? BaseIonTypesCID : BaseIonTypesETD;
            var ionTypeFactory = new IonTypeFactory(baseIonTypes, new List <NeutralLoss> {
                NeutralLoss.NoLoss
            }, MaxCharge);

            // ReSharper disable once UnusedVariable
            // This call is used to validate the ion types returned by ionTypeFactory
            var _ionTypes = ionTypeFactory.GetAllKnownIonTypes().ToArray();

            _aminoAcidsArray = aminoAcidsArray ?? AminoAcid.StandardAminoAcidArr;
            _tolerance       = tolerance;

            if (_aminoAcidsArray.Length - 1 > Byte.MaxValue)
            {
                throw new Exception("Too many amino acid types");
            }

            _maxAminoAcidMass = 0d;
            _minAminoAcidMass = 10E4;
            foreach (var aa in _aminoAcidsArray)
            {
                if (aa.Composition.Mass > _maxAminoAcidMass)
                {
                    _maxAminoAcidMass = aa.Composition.Mass;
                }

                if (aa.Composition.Mass < _minAminoAcidMass)
                {
                    _minAminoAcidMass = aa.Composition.Mass;
                }
            }
            _minTagLength = minTagLength;

            _spectrum          = spec;
            _deconvolutedPeaks = Deconvoluter.GetDeconvolutedPeaks(_spectrum.ScanNum, _spectrum.Peaks, MinCharge, MaxCharge, IsotopeOffsetTolerance, 1.1, _tolerance, 0.7);

            SetNodeCount(_deconvolutedPeaks.Count);
            CollectSequenceTagGraphEdges();

            _seqTagSet                = new HashSet <SequenceTag>();
            NumberOfProcessedPaths    = 0;
            MaxNumberOfProcessedPaths = 1024;
        }
Exemple #9
0
 public TagMatchFinder(
     ProductSpectrum spec,
     IScorer ms2Scorer,
     LcMsPeakMatrix featureFinder,
     string proteinSequence,
     Tolerance tolerance,
     AminoAcidSet aaSet,
     double maxSequenceMass)
 {
     _spec            = spec;
     _ms2Scorer       = ms2Scorer;
     _featureFinder   = featureFinder;
     _proteinSequence = proteinSequence;
     _tolerance       = tolerance;
     _aaSet           = aaSet;
     _maxSequenceMass = maxSequenceMass;
 }
Exemple #10
0
        public double ComputeScore(ProductSpectrum ms2Spec, Sequence sequence)
        {
            _baseIonTypes = ms2Spec.ActivationMethod != ActivationMethod.ETD ? BaseIonTypesCID : BaseIonTypesETD;
            _sequence     = sequence;
            _ms2Spec      = ms2Spec;

            if (!_rankingInfo.TryGetValue(ms2Spec.ScanNum, out _peakRanking))
            {
                _peakRanking = ArrayUtil.GetRankings(ms2Spec.Peaks.Select(peak => peak.Intensity));
                _rankingInfo.Add(ms2Spec.ScanNum, _peakRanking);
            }

            FindMatchedPeaks();

            var score = GetRankSumScore();

            return(score);
        }
Exemple #11
0
        /// <summary>
        /// Reads the mass spectrum with the specified scanNum from the raw file
        /// </summary>
        /// <param name="scanNum">scan number</param>
        /// <param name="includePeaks">whether to include peak data</param>
        /// <returns>mass spectrum</returns>
        public Spectrum ReadMassSpectrum(int scanNum, bool includePeaks = true)
        {
            var scanInfo = GetScanInfo(scanNum);

            // default empty arrays, if peak data not requested.
            double[] mzArr        = new double[] {};
            double[] intensityArr = new double[] {};

            if (includePeaks)
            {
                _msfileReader.GetScanData(scanNum, out mzArr, out intensityArr, 0, true);
            }

            var elutionTime = RtFromScanNum(scanNum);
            var nativeId    = "controllerType=0 controllerNumber=1 scan=" + scanNum;

            // Call scanInfo.MSLevel in order to update dictionary _msLevel
            var msLevel = ReadMsLevel(scanNum);

            if (msLevel == 1)
            {
                return new Spectrum(mzArr, intensityArr, scanNum)
                       {
                           ElutionTime     = elutionTime,
                           TotalIonCurrent = scanInfo.TotalIonCurrent,
                           NativeId        = nativeId,
                       }
            }
            ;

            var isolationWindow = ReadPrecursorInfo(scanNum);

            var productSpec = new ProductSpectrum(mzArr, intensityArr, scanNum)
            {
                MsLevel          = scanInfo.MSLevel,
                ElutionTime      = elutionTime,
                TotalIonCurrent  = scanInfo.TotalIonCurrent,
                NativeId         = nativeId,
                ActivationMethod = GetActivationMethod(scanNum),
                IsolationWindow  = isolationWindow
            };

            return(productSpec);
        }
        /// <summary>
        /// Calculate the sequence coverage for the given sequence and spectrum.
        /// </summary>
        /// <param name="spectrum">The spectrum to calculate sequence coverage within.</param>
        /// <param name="sequence">The sequence to calculate coverage for.</param>
        /// <param name="charge">The parent charge state of this spectrum.</param>
        /// <returns>The sequence coverage.</returns>
        private double CalculateSequenceCoverage(ProductSpectrum spectrum, Sequence sequence, int charge)
        {
            var ionTypes = this.ionTypeFactory.GetAllKnownIonTypes().ToList();

            int found = 0;

            for (int clv = 1; clv < sequence.Count; clv++)
            {
                bool haveFoundClv = false;
                var  nTermSeq     = sequence.GetRange(0, clv);
                var  nTermComp    = nTermSeq.Aggregate(Composition.Zero, (l, r) => l + r.Composition);
                var  cTermSeq     = sequence.GetRange(clv, sequence.Count - clv);
                var  cTermComp    = cTermSeq.Aggregate(Composition.Zero, (l, r) => l + r.Composition);

                foreach (var ionType in ionTypes)
                {
                    if (haveFoundClv)
                    {
                        break;
                    }

                    if (ionType.Charge >= charge)
                    {
                        continue;
                    }

                    var comp = ionType.IsPrefixIon ? nTermComp : cTermComp;
                    var aa   = ionType.IsPrefixIon ? nTermSeq[nTermSeq.Count - 1] : cTermSeq[0];
                    var ions = ionType.GetPossibleIons(comp, aa);
                    foreach (var ion in ions)
                    {
                        if (spectrum.GetCorrScore(ion, this.tolerance) > 0.7)
                        {
                            found++;
                            haveFoundClv = true;
                            break;
                        }
                    }
                }
            }

            return((100.0 * found) / (sequence.Count - 1));
        }
Exemple #13
0
 public SpectrumSearchResult(ProductSpectrum hcdSpectrum, ProductSpectrum cidSpectrum, List <MsMsSearchResult> hcdSearchResultList, List <MsMsSearchResult> cidSearchResultList, LcMsRun lcMsRun, ScoreModel scoreModel = null, LipidTarget lipidTarget = null)
 {
     HcdSpectrum         = hcdSpectrum;
     CidSpectrum         = cidSpectrum;
     PrecursorSpectrum   = null;
     HcdSearchResultList = hcdSearchResultList;
     CidSearchResultList = cidSearchResultList;
     Xic           = null;
     LcMsRun       = lcMsRun;
     PeakArea      = null;
     RunLength     = lcMsRun.GetElutionTime(lcMsRun.MaxLcScan);
     ApexScanNum   = 0;
     ApexIntensity = 0;
     RetentionTime = LcMsRun.GetElutionTime(hcdSpectrum?.ScanNum ?? cidSpectrum.ScanNum);
     if (scoreModel != null && lipidTarget != null)
     {
         ModelScore = scoreModel.ScoreLipid(lipidTarget, this);
     }
 }
Exemple #14
0
 public SpectrumSearchResult(ProductSpectrum hcdSpectrum, ProductSpectrum cidSpectrum, Spectrum precursorSpectrum, List <MsMsSearchResult> hcdSearchResultList, List <MsMsSearchResult> cidSearchResultList, Xic xic, LcMsRun lcMsRun, ScoreModel scoreModel = null, LipidTarget lipidTarget = null)
 {
     HcdSpectrum         = hcdSpectrum;
     CidSpectrum         = cidSpectrum;
     PrecursorSpectrum   = precursorSpectrum;
     HcdSearchResultList = hcdSearchResultList;
     CidSearchResultList = cidSearchResultList;
     Xic           = xic;
     LcMsRun       = lcMsRun;
     PeakArea      = null;
     RunLength     = lcMsRun.GetElutionTime(lcMsRun.MaxLcScan);
     ApexScanNum   = Xic.GetNearestApexScanNum(PrecursorSpectrum.ScanNum, performSmoothing: true);
     ApexIntensity = Xic.Where(x => x.ScanNum == ApexScanNum).Sum(x => x.Intensity);
     RetentionTime = LcMsRun.GetElutionTime(ApexScanNum);
     if (scoreModel != null && lipidTarget != null)
     {
         ModelScore = scoreModel.ScoreLipid(lipidTarget, this);
     }
 }
Exemple #15
0
        public IScorer GetScorer(ProductSpectrum spectrum, double precursorMass, int precursorCharge, ActivationMethod activationMethod = ActivationMethod.Unknown)
        {
            IScorer scorer = null;

            if (spectrum is DeconvolutedSpectrum)
            {
                var deconSpec = spectrum as DeconvolutedSpectrum;
                scorer = new CompositeScorerBasedOnDeconvolutedSpectrum(deconSpec, spectrum, _productTolerance, _comparer, spectrum.ActivationMethod);
            }
            else
            {
                scorer = new CompositeScorer(
                    spectrum,
                    this._productTolerance,
                    activationMethod: activationMethod,
                    minCharge: _minProductCharge,
                    maxCharge: _maxProductCharge);
            }
            return(scorer);
        }
Exemple #16
0
        public double GetScoreTest(Sequence sequence, ProductSpectrum spectrum)
        {
            var score         = 0d;
            var tol           = new Tolerance(10);
            var matchCounter  = new CorrMatchedPeakCounter(spectrum, tol, 1, 20);
            var prefixCompArr = sequence.GetPrefixCompositions().ToArray();

            foreach (var c in prefixCompArr)
            {
                if (c.Equals(Composition.Zero))
                {
                    Console.WriteLine("found zero");
                }
            }
            var suffixCompArr = sequence.GetSuffixCompositions().ToArray();

            for (int i = 0; i < prefixCompArr.Length; i++)
            {
                score += matchCounter.GetFragmentScore(prefixCompArr[i], suffixCompArr[i]);
            }
            return(score);
        }
Exemple #17
0
        private PlotModel CreateMsMsPlot(List <MsMsSearchResult> searchResultList, ProductSpectrum productSpectrum)
        {
            var spectrumSearchResult = CurrentSpectrumSearchResult;
            var lipidTarget          = CurrentLipidTarget;
            var commonName           = lipidTarget.StrippedDisplay;
            var parentScan           = spectrumSearchResult.PrecursorSpectrum?.ScanNum ?? 0;
            var peakList             = productSpectrum.Peaks;
            var fragmentationType    = productSpectrum.ActivationMethod == ActivationMethod.CID ? FragmentationType.CID : FragmentationType.HCD;

            if (!peakList.Any())
            {
                return(new PlotModel());
            }

            var plotTitle = commonName + "\nMS/MS Spectrum - " + productSpectrum.ActivationMethod + " - " + productSpectrum.ScanNum + " // Precursor Scan - " + parentScan + " (" + productSpectrum.IsolationWindow.IsolationWindowTargetMz.ToString("0.0000") + " m/z)";

            var plotModel = new PlotModel
            {
                Title         = plotTitle,
                TitleFontSize = 14,
                Padding       = new OxyThickness(0),
                PlotMargins   = new OxyThickness(0)
            };
            var mzPeakSeries = new StemSeries
            {
                Color           = OxyColors.Black,
                StrokeThickness = 0.5,
                Title           = "Peaks"
            };
            var annotatedPeakSeries = new StemSeries
            {
                Color           = OxyColors.Green,
                StrokeThickness = 2,
                Title           = "Matched Ions"
            };
            var diagnosticPeakSeries = new StemSeries
            {
                Color           = OxyColors.Red,
                StrokeThickness = 2,
                Title           = "Diagnostic Ion"
            };

            plotModel.IsLegendVisible = true;
            plotModel.LegendPosition  = LegendPosition.TopRight;
            plotModel.LegendPlacement = LegendPlacement.Inside;
            plotModel.LegendMargin    = 0;
            plotModel.LegendFontSize  = 10;

            var minMz              = double.MaxValue;
            var maxMz              = double.MinValue;
            var maxIntensity       = double.MinValue;
            var secondMaxIntensity = double.MinValue;

            foreach (var msPeak in peakList)
            {
                var mz        = msPeak.Mz;
                var intensity = msPeak.Intensity;

                if (mz < minMz)
                {
                    minMz = mz;
                }
                if (mz > maxMz)
                {
                    maxMz = mz;
                }
                if (intensity > maxIntensity)
                {
                    secondMaxIntensity = maxIntensity;
                    maxIntensity       = intensity;
                }
                else if (intensity > secondMaxIntensity)
                {
                    secondMaxIntensity = intensity;
                }

                var dataPoint = new DataPoint(mz, intensity);

                var isDiagnostic = false;

                var matchedPeaks = searchResultList.Where(x => x.ObservedPeak.Equals(msPeak)).ToList();
                foreach (var matchedSearchResult in matchedPeaks)
                {
                    var annotation = new MsMsAnnotation(fragmentationType)
                    {
                        Text                    = matchedSearchResult.TheoreticalPeak.DescriptionForUi,
                        TextColor               = OxyColors.Blue,
                        TextPosition            = dataPoint,
                        TextVerticalAlignment   = VerticalAlignment.Middle,
                        TextHorizontalAlignment = HorizontalAlignment.Left,
                        TextRotation            = -90,
                        StrokeThickness         = 0,
                        Offset                  = new ScreenVector(0, -5),
                        Selectable              = true
                    };
                    plotModel.Annotations.Add(annotation);
                    MsMsAnnotationList.Add(annotation);

                    if (!isDiagnostic)
                    {
                        isDiagnostic = matchedSearchResult.TheoreticalPeak.IsDiagnostic;
                    }
                }

                if (isDiagnostic)
                {
                    diagnosticPeakSeries.Points.Add(dataPoint);
                }
                else if (matchedPeaks.Any())
                {
                    annotatedPeakSeries.Points.Add(dataPoint);
                }
                else
                {
                    mzPeakSeries.Points.Add(dataPoint);
                }
            }

            plotModel.Series.Add(mzPeakSeries);
            plotModel.Series.Add(annotatedPeakSeries);
            plotModel.Series.Add(diagnosticPeakSeries);

            var yAxis = new LinearAxis
            {
                Position        = AxisPosition.Left,
                Title           = "Intensity",
                Minimum         = 0,
                AbsoluteMinimum = 0
            };

            //yAxis.Maximum = maxIntensity + (maxIntensity * .05);
            //yAxis.AbsoluteMaximum = maxIntensity + (maxIntensity * .05);
            if (secondMaxIntensity > 0)
            {
                yAxis.Maximum         = secondMaxIntensity + secondMaxIntensity * .25;
                yAxis.AbsoluteMaximum = maxIntensity + maxIntensity * .25;
                yAxis.MinorTickSize   = MINOR_TICK_SIZE;
                yAxis.MajorStep       = (secondMaxIntensity + secondMaxIntensity * .25) / 5.0;
                yAxis.StringFormat    = "0.0E00";
            }
            else if (maxIntensity > 0)
            {
                yAxis.Maximum         = maxIntensity + maxIntensity * .25;
                yAxis.AbsoluteMaximum = maxIntensity + maxIntensity * .25;
                yAxis.MinorTickSize   = MINOR_TICK_SIZE;
                yAxis.MajorStep       = (maxIntensity + maxIntensity * .25) / 5.0;
                yAxis.StringFormat    = "0.0E00";
            }
            else
            {
                yAxis.Maximum         = 1;
                yAxis.AbsoluteMaximum = 1;
                yAxis.MinorTickSize   = 0;
                yAxis.MajorStep       = 1;
                yAxis.StringFormat    = "0.0";
            }

            yAxis.AxisChanged += OnYAxisChange;

            var xAxis = new LinearAxis
            {
                Position        = AxisPosition.Bottom,
                Title           = "m/z",
                Minimum         = minMz - 20,
                AbsoluteMinimum = minMz - 20,
                Maximum         = maxMz + 20,
                AbsoluteMaximum = maxMz + 20
            };

            plotModel.Axes.Add(yAxis);
            plotModel.Axes.Add(xAxis);

            return(plotModel);
        }
Exemple #18
0
        private FlankingMassMatch GetBestMatchInTheGraph(ShiftedSequenceGraph seqGraph, ProductSpectrum spec, double?featureMass)
        {
            FlankingMassMatch match = null;
            var bestScore           = double.NegativeInfinity;
            var protCompositions    = seqGraph.GetSequenceCompositions();

            for (var modIndex = 0; modIndex < protCompositions.Length; modIndex++)
            {
                seqGraph.SetSink(modIndex);
                var protCompositionWithH2O = seqGraph.GetSinkSequenceCompositionWithH2O();
                var sequenceMass           = protCompositionWithH2O.Mass;
                if (featureMass != null && !_tolerance.IsWithin(sequenceMass, (double)featureMass))
                {
                    continue;
                }

                var charge =
                    (int)
                    Math.Round(sequenceMass /
                               (spec.IsolationWindow.IsolationWindowTargetMz - Constants.Proton));

                var mostAbundantIsotopeMz = Ion.GetIsotopeMz(sequenceMass, charge,
                                                             Averagine.GetIsotopomerEnvelope(sequenceMass).MostAbundantIsotopeIndex);

                if (!spec.IsolationWindow.Contains(mostAbundantIsotopeMz))
                {
                    continue;
                }

                //var feature = new TargetFeature(sequenceMass, charge, spec.ScanNum);

                if (_featureFinder != null)
                {
                    var ms1Corr = _featureFinder.GetMs1EvidenceScore(spec.ScanNum, sequenceMass, charge);
                    if (ms1Corr < Ms1CorrThreshold)
                    {
                        continue;
                    }
                }

                var curScoreAndModifications = seqGraph.GetScoreAndModifications(_ms2Scorer);
                var curScore = curScoreAndModifications.Item1;
//                var curScore = seqGraph.GetFragmentScore(_ms2Scorer);
                if (curScore > bestScore)
                {
                    match = new FlankingMassMatch(curScore,
                                                  sequenceMass - Composition.H2O.Mass - seqGraph.ShiftMass, charge, curScoreAndModifications.Item2);
                    //match = new FlankingMassMatch(curScore,
                    //    sequenceMass - Composition.H2O.Mass - seqGraph.ShiftMass, charge, new ModificationInstance[0]);
                    bestScore = curScore;
                }
            }

            return(match);
        }
        public CompositeScorerBasedOnDeconvolutedSpectrum(DeconvolutedSpectrum deconvolutedSpectrum, ProductSpectrum spec, Tolerance productTolerance, IMassBinning comparer)
            : base(deconvolutedSpectrum, productTolerance)
        {
            ReferencePeakIntensity = GetRefIntensity(spec.Peaks);
            _comparer         = comparer;
            _massBinToPeakMap = new Dictionary <int, DeconvolutedPeak>();

            foreach (var p in deconvolutedSpectrum.Peaks)
            {
                var mass      = p.Mz;
                var deltaMass = productTolerance.GetToleranceAsDa(mass, 1);
                var minMass   = mass - deltaMass;
                var maxMass   = mass + deltaMass;

                var binNum = comparer.GetBinNumber(mass);

                if (binNum < 0)
                {
                    binNum = comparer.GetBinNumber(minMass);
                    if (binNum < 0)
                    {
                        binNum = comparer.GetBinNumber(maxMass);
                    }
                }

                // filter out
                if (binNum < 0)
                {
                    continue;
                }

                UpdateDeconvPeak(binNum, p as DeconvolutedPeak);
                // going up
                for (var nextBinNum = binNum + 1; nextBinNum < comparer.NumberOfBins; nextBinNum++)
                {
                    var nextBinMass = comparer.GetMassStart(nextBinNum);
                    if (minMass < nextBinMass && nextBinMass < maxMass)
                    {
                        UpdateDeconvPeak(nextBinNum, p as DeconvolutedPeak);                                                 //_ionMassChkBins[nextBinNum] = true;
                    }
                    else
                    {
                        break;
                    }
                }

                // going down
                for (var prevBinNum = binNum - 1; prevBinNum < comparer.NumberOfBins; prevBinNum--)
                {
                    var prevBinMass = comparer.GetMassEnd(prevBinNum);
                    if (minMass < prevBinMass && prevBinMass < maxMass)
                    {
                        UpdateDeconvPeak(prevBinNum, p as DeconvolutedPeak);                                                 //_ionMassChkBins[prevBinNum] = true;
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }
 public PeptideSpectrumMatch(Sequence.Sequence peptide, ProductSpectrum spectrum)
 {
     Peptide = peptide;
     Spectrum = spectrum;
 }
Exemple #21
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="peptide"></param>
 /// <param name="spectrum"></param>
 public PeptideSpectrumMatch(Sequence.Sequence peptide, ProductSpectrum spectrum)
 {
     Peptide  = peptide;
     Spectrum = spectrum;
 }
Exemple #22
0
 public CorrMatchedPeakCounter(ProductSpectrum ms2Spec, Tolerance tolerance, int minCharge, int maxCharge, double corrScoreThreshold = 0.7) :
     base(ms2Spec, tolerance, minCharge, maxCharge, 0.1)
 {
     _corrScoreThreshold = corrScoreThreshold;
 }
Exemple #23
0
        private void GetNodeStatistics(bool isDecoy, ProductSpectrum ms2Spec, Sequence sequence, StreamWriter writer) //, StreamWriter mzErrorWriter)
        {
            if (ms2Spec == null)
            {
                return;
            }
            if (sequence == null)
            {
                return;
            }
            //var refIntensity = ms2Spec.Peaks.Max(p => p.Intensity) * 0.01;
            //refIntensity = Math.Min(ms2Spec.Peaks.Select(p => p.Intensity).Median(), refIntensity);

            var BaseIonTypesCID = new[] { BaseIonType.B, BaseIonType.Y };
            var BaseIonTypesETD = new[] { BaseIonType.C, BaseIonType.Z };
            var tolerance       = new Tolerance(15);
            var minCharge       = 1;
            var maxCharge       = 20;
            var baseIonTypes    = ms2Spec.ActivationMethod != ActivationMethod.ETD ? BaseIonTypesCID : BaseIonTypesETD;

            var refIntensity = ms2Spec.Peaks.Max(p => p.Intensity);

            var activationMethodFlag = ms2Spec.ActivationMethod == ActivationMethod.ETD ? 1 : 2;
            var cleavages            = sequence.GetInternalCleavages();

            var prevPrefixFragMass        = 0d;
            var prevPrefixObsIonMass      = 0d;
            var prevPrefixObsIonCharge    = 0;
            var prevPrefixObsIonIntensity = 0d;

            var prevSuffixFragMass        = 0d;
            var prevSuffixObsIonMass      = 0d;
            var prevSuffixObsIonCharge    = 0;
            var prevSuffixObsIonIntensity = 0d;

            var nComplementaryFrags = 0;

            var cleavageIndex = 0;

            foreach (var c in cleavages)
            {
                var bothObs = true;
                foreach (var baseIonType in baseIonTypes)
                {
                    var peakType            = baseIonType.IsPrefix ? 1 : 2; // unexplained
                    var fragmentComposition = baseIonType.IsPrefix
                                  ? c.PrefixComposition + baseIonType.OffsetComposition
                                  : c.SuffixComposition + baseIonType.OffsetComposition;

                    var curFragMass        = fragmentComposition.Mass;
                    var curObsIonMass      = 0d;
                    var curObsIonCharge    = 0;
                    var curObsIonDist      = 1.0d;
                    var curObsIonCorr      = 0d;
                    var curObsIonIntensity = 0d;

                    var ionMatch = false;
                    for (var charge = minCharge; charge <= maxCharge; charge++)
                    {
                        var ion = new Ion(fragmentComposition, charge);

                        var isotopePeaks = ms2Spec.GetAllIsotopePeaks(ion, tolerance, 0.1);

                        if (isotopePeaks == null)
                        {
                            continue;
                        }

                        var distCorr = AbstractFragmentScorer.GetDistCorr(ion, isotopePeaks);

                        if (distCorr.Item2 < 0.7 && distCorr.Item1 > 0.07)
                        {
                            continue;
                        }
                        var mostAbundantIsotopeIndex = ion.Composition.GetMostAbundantIsotopeZeroBasedIndex();
                        var mostAbuPeak = isotopePeaks[mostAbundantIsotopeIndex];

                        var summedIntensity = isotopePeaks.Where(p => p != null).Sum(p => p.Intensity);
                        var intScore        = summedIntensity / refIntensity;
                        //var intScore = mostAbuPeak.Intensity / medIntensity;
                        //var intScore = summedIntensity / refIntensity;

                        if (ionMatch == false || curObsIonIntensity < intScore)
                        {
                            curObsIonMass      = Ion.GetMonoIsotopicMass(mostAbuPeak.Mz, charge, mostAbundantIsotopeIndex);
                            curObsIonCharge    = charge;
                            curObsIonCorr      = distCorr.Item2;
                            curObsIonDist      = distCorr.Item1;
                            curObsIonIntensity = intScore;
                        }
                        ionMatch = true;
                    }

                    if (!ionMatch)
                    {
                        bothObs = false;
                        continue;
                    }

                    writer.Write(activationMethodFlag);
                    writer.Write("\t");

                    writer.Write(peakType);
                    writer.Write("\t");

                    writer.Write("{0:0.000}", curFragMass);
                    writer.Write("\t");

                    writer.Write("{0}", curObsIonCharge);
                    writer.Write("\t");

                    writer.Write("{0:0.000}", curObsIonDist);
                    writer.Write("\t");

                    writer.Write("{0:0.000}", curObsIonCorr);
                    writer.Write("\t");

                    writer.Write("{0:0.000}", curObsIonIntensity);
                    writer.Write("\t");

                    writer.Write("{0:0.000}", (Math.Abs(curFragMass - curObsIonMass) / curFragMass) * 1e6);
                    writer.Write("\n");

                    // mz error output

                    /*
                     * if (baseIonType.IsPrefix && prevPrefixFragMass > 0 & prevPrefixObsIonMass > 0)
                     * {
                     *  var aaMass = Math.Abs(prevPrefixFragMass - curFragMass);
                     *  var massError = Math.Abs(Math.Abs(prevPrefixObsIonMass - curObsIonMass) - aaMass);
                     *  var massErrorPpm = (massError / curObsIonMass) * 1e6;
                     *   mzErrorWriter.WriteLine("{0}\t{1:0.000}\t{2}", activationMethodFlag, massErrorPpm, Math.Abs(prevPrefixObsIonCharge - curObsIonCharge));
                     * }
                     * else if (prevSuffixFragMass > 0 & prevSuffixObsIonMass > 0)
                     * {
                     *  var aaMass = Math.Abs(prevSuffixFragMass - curFragMass);
                     *  var massError = Math.Abs(Math.Abs(prevSuffixObsIonMass - curObsIonMass) - aaMass);
                     *  var massErrorPpm = (massError / curObsIonMass) * 1e6;
                     *  mzErrorWriter.WriteLine("{0}\t{1:0.000}\t{2}", activationMethodFlag, massErrorPpm, Math.Abs(prevSuffixObsIonCharge - curObsIonCharge));
                     * }
                     */
                    if (baseIonType.IsPrefix)
                    {
                        prevPrefixFragMass        = curFragMass;
                        prevPrefixObsIonMass      = curObsIonMass;
                        prevPrefixObsIonCharge    = curObsIonCharge;
                        prevPrefixObsIonIntensity = curObsIonIntensity;
                        //Array.Copy(curObsIonMass, prevPrefixObsIonMass, curObsIonMass.Length);
                    }
                    else
                    {
                        prevSuffixFragMass        = curFragMass;
                        prevSuffixObsIonMass      = curObsIonMass;
                        prevSuffixObsIonCharge    = curObsIonCharge;
                        prevSuffixObsIonIntensity = curObsIonIntensity;
                        //Array.Copy(curObsIonMass, prevSuffixObsIonMass, curObsIonMass.Length);
                    }
                }

                if (bothObs)
                {
                    //pairWriter.Write("{0}\t{1}\t", prevPrefixObsIonIntensity, prevSuffixObsIonIntensity);
                    nComplementaryFrags++;
                }
                cleavageIndex++;
            }

            Console.WriteLine("{0}\t{1}", nComplementaryFrags, sequence.Count);

            //if (!isDecoy) Console.WriteLine("{0}", totalExplainedAbundanceRatio);
        }
Exemple #24
0
        public Tuple <int, int, int, int, string, string> GetLongestSequence(ProductSpectrum spectrum, Sequence sequence)
        {
            _spectrum     = spectrum;
            _sequence     = sequence;
            _baseIonTypes = _spectrum.ActivationMethod != ActivationMethod.ETD ? BaseIonTypesCid : BaseIonTypesEtd;

            var cleavages      = _sequence.GetInternalCleavages().ToArray();
            var prefixValueArr = new int[cleavages.Length];
            var suffixValueArr = new int[cleavages.Length];
            var prefixPeakArr  = new Peak[cleavages.Length];
            var suffixPeakArr  = new Peak[cleavages.Length];
            //var peakList = new double[_spectrum.Peaks.Length];

            int cleavageIndex = 0;

            /*
             * for (int i = 0; i < peakList.Length; i++)
             * {
             *  peakList[i] = _spectrum.Peaks[i].Intensity;
             * }*/

            //var rankings = ArrayUtil.GetRankings(peakList);

            foreach (var c in cleavages)
            {
                foreach (var baseIonType in _baseIonTypes)
                {
                    var fragmentComposition = baseIonType.IsPrefix
                                 ? c.PrefixComposition + baseIonType.OffsetComposition
                                 : c.SuffixComposition + baseIonType.OffsetComposition;
                    for (var charge = _minCharge; charge <= _maxCharge; charge++)
                    {
                        var ion = new Ion(fragmentComposition, charge);
                        if (_spectrum.GetCorrScore(ion, _tolerance, RelativeIsotopeIntensityThreshold) < .7)
                        {
                            continue;
                        }
                        if (baseIonType.IsPrefix)
                        {
                            prefixValueArr[cleavageIndex] = 1;
                        }
                        else
                        {
                            suffixValueArr[cleavageIndex] = 1;
                        }
                    }
                }
                cleavageIndex++;
            }

            var prefixSequenceArr = new int[_sequence.Count];
            var suffixSequenceArr = new int[_sequence.Count];

            prefixSequenceArr[0] = prefixValueArr[0];
            suffixSequenceArr[suffixSequenceArr.Length - 1] = suffixValueArr[suffixValueArr.Length - 1];

            for (int i = 1; i < prefixValueArr.Length; i++)
            {
                if (prefixValueArr[i] == 1 && prefixValueArr[i - 1] == 1)
                {
                    if (_sequence[i] is ModifiedAminoAcid)
                    {
                        continue;
                    }
                    prefixSequenceArr[i] = 1;
                }
            }

            for (int i = suffixValueArr.Length - 2; i >= 0; i--)
            {
                if (suffixValueArr[i] == 1 && suffixValueArr[i + 1] == 1)
                {
                    if (_sequence[i + 1] is ModifiedAminoAcid)
                    {
                        continue;
                    }
                    suffixSequenceArr[i + 1] = 1;
                }
            }

            var prefixSubString  = FindLongestSubstring(prefixSequenceArr);
            var prefixStartIndex = -1;
            var prefixEndIndex   = -1;
            //var prefixSequencePeaks = new List<Peak>();
            //var prefixPval = -1.0;
            var prefixSequence = "";

            if (prefixSubString != "")
            {
                var prefixIndex = string.Concat(prefixSequenceArr);
                prefixStartIndex = prefixIndex.IndexOf(prefixSubString) + 1;
                prefixEndIndex   = (prefixStartIndex == 1) ? 1 : prefixStartIndex + prefixSubString.Length - 1;
                //prefixSequencePeaks = GetPrefixSequencePeaks(prefixPeakArr, prefixStartIndex, prefixEndIndex);
                //var prefixRankSum = GetSequenceRankSum(prefixSequencePeaks, rankings, peakList);
                //prefixPval = FitScoreCalculator.GetRankSumPvalue(peakList.Length, prefixSequencePeaks.Count, prefixRankSum);
                prefixSequence = GetStringSubSequence(_sequence, prefixStartIndex, prefixEndIndex);
            }

            var suffixSubString  = FindLongestSubstring(suffixSequenceArr);
            var suffixStartIndex = -1;
            var suffixEndIndex   = -1;
            //var suffixSequencePeaks = new List<Peak>();
            //var suffixPval = -1.0;
            var suffixSequence = "";

            if (suffixSubString != "")
            {
                var suffixIndex = string.Concat(suffixSequenceArr);
                suffixStartIndex = suffixIndex.IndexOf(suffixSubString) + 1;
                suffixEndIndex   = (suffixStartIndex == 1) ? 1 : suffixStartIndex + suffixSubString.Length - 1;
                //suffixSequencePeaks = GetSuffixSequencePeaks(suffixPeakArr, suffixStartIndex, suffixEndIndex);
                //var suffixRankSum = GetSequenceRankSum(suffixSequencePeaks, rankings, peakList);
                //suffixPval = FitScoreCalculator.GetRankSumPvalue(peakList.Length, suffixSequencePeaks.Count, suffixRankSum);
                suffixSequence = GetStringSubSequence(_sequence, suffixStartIndex, suffixEndIndex);
            }

            return(new Tuple <int, int, int, int, string, string>(prefixStartIndex, prefixEndIndex, suffixStartIndex, suffixEndIndex, prefixSequence, suffixSequence));
        }
Exemple #25
0
        private static bool IsGoodTarget(ProductSpectrum ms2Spec, Sequence sequence)
        {
            var          BaseIonTypesCID = new[] { BaseIonType.B, BaseIonType.Y };
            var          BaseIonTypesETD = new[] { BaseIonType.C, BaseIonType.Z };
            var          tolerance       = new Tolerance(10);
            var          minCharge       = 1;
            var          maxCharge       = 20;
            var          baseIonTypes    = ms2Spec.ActivationMethod != ActivationMethod.ETD ? BaseIonTypesCID : BaseIonTypesETD;
            var          cleavages       = sequence.GetInternalCleavages();
            const double RelativeIsotopeIntensityThreshold = 0.7d;

            var nTheoreticalIonPeaks    = 0;
            var nObservedIonPeaks       = 0;
            var nObservedPrefixIonPeaks = 0;
            var nObservedsuffixIonPeaks = 0;

            foreach (var c in cleavages)
            {
                foreach (var baseIonType in baseIonTypes)
                {
                    var fragmentComposition = baseIonType.IsPrefix
                                  ? c.PrefixComposition + baseIonType.OffsetComposition
                                  : c.SuffixComposition + baseIonType.OffsetComposition;

                    for (var charge = minCharge; charge <= maxCharge; charge++)
                    {
                        var ion = new Ion(fragmentComposition, charge);
                        if (ms2Spec.ContainsIon(ion, tolerance, RelativeIsotopeIntensityThreshold))
                        {
                            if (baseIonType.IsPrefix)
                            {
                                nObservedPrefixIonPeaks++;
                            }
                            else
                            {
                                nObservedsuffixIonPeaks++;
                            }
                            nObservedIonPeaks++;
                        }
                        nTheoreticalIonPeaks++;
                    }
                }
            }

            if (sequence.Composition.Mass > 3000)
            {
                if ((double)nObservedPrefixIonPeaks / nObservedIonPeaks > 0.85 ||
                    (double)nObservedsuffixIonPeaks / nObservedIonPeaks > 0.85)
                {
                    return(false);
                }

                if (nObservedPrefixIonPeaks < 3 || nObservedsuffixIonPeaks < 3)
                {
                    return(false);
                }
            }
            else
            {
                if (nObservedPrefixIonPeaks < 1 || nObservedsuffixIonPeaks < 1)
                {
                    return(false);
                }
            }

            return(true);
        }
Exemple #26
0
 public MatchedPeakCounter(ProductSpectrum ms2Spec, Tolerance tolerance, int minCharge, int maxCharge)
     : base(ms2Spec, tolerance, minCharge, maxCharge)
 {
     RelativeIsotopeIntensityThreshold = 0.7;
 }
Exemple #27
0
        public static List <SpectrumSearchResult> RunFragmentWorkflow(ICollection <MsMsSearchUnit> fragments, LcMsRun lcmsRun, double hcdMassError, double cidMassError, int minMatches, IProgress <int> progress = null)
        {
            var PISearchUnits = fragments.Where(x => x.Description.Equals("Product Ion")).ToList();
            var hcdTolerance  = new Tolerance(hcdMassError, ToleranceUnit.Ppm);
            var cidTolerance  = new Tolerance(cidMassError, ToleranceUnit.Ppm);
            var scanTracker   = new List <int>(); //track what scans have been included in spectrumSearchResultsList so we don't make duplicate entries for matched CID and HCD

            // Find all MS/MS scans
            var msmsScanNumers           = lcmsRun.GetScanNumbers(2);
            var spectrumSearchResultList = new List <SpectrumSearchResult>();
            var maxScans = msmsScanNumers.Count;

            foreach (var scan in msmsScanNumers)
            {
                // Lookup the MS/MS Spectrum
                var MsMsSpectrum = lcmsRun.GetSpectrum(scan) as ProductSpectrum;
                if (MsMsSpectrum == null)
                {
                    continue;
                }

                ProductSpectrum MatchedSpectrum = null;
                var             spectrum1       = lcmsRun.GetSpectrum(scan + 1);
                var             spectrum2       = lcmsRun.GetSpectrum(scan - 1);
                if (spectrum1 != null && spectrum1.MsLevel == 2)
                {
                    var productSpectrum = spectrum1 as ProductSpectrum;
                    if (productSpectrum != null)
                    {
                        var deltaMz = productSpectrum.IsolationWindow.IsolationWindowTargetMz - MsMsSpectrum.IsolationWindow.IsolationWindowTargetMz;
                        if (Math.Abs(deltaMz) < float.Epsilon)
                        {
                            MatchedSpectrum = productSpectrum;
                        }
                    }
                }
                if (spectrum2 != null && spectrum2.MsLevel == 2)
                {
                    var productSpectrum = spectrum2 as ProductSpectrum;
                    if (productSpectrum != null)
                    {
                        var deltaMz = productSpectrum.IsolationWindow.IsolationWindowTargetMz - MsMsSpectrum.IsolationWindow.IsolationWindowTargetMz;
                        if (Math.Abs(deltaMz) < float.Epsilon)
                        {
                            MatchedSpectrum = productSpectrum;
                        }
                    }
                }

                if (scanTracker.Contains(MsMsSpectrum.ScanNum))
                {
                    continue;
                }

                var msmsPrecursorMz = MsMsSpectrum.IsolationWindow.IsolationWindowTargetMz;

                var xic = lcmsRun.GetFullPrecursorIonExtractedIonChromatogram(msmsPrecursorMz, hcdTolerance);

                // Bogus data
                //if (xic.GetApexScanNum() < 0) continue;
                var msmsPrecursorScan = 0;
                if (lcmsRun.MinMsLevel == 1) //Make sure there are precursor scans in file
                {
                    msmsPrecursorScan = lcmsRun.GetPrecursorScanNum(scan);
                }
                var precursorSpectrum = lcmsRun.GetSpectrum(msmsPrecursorScan);

                // Get all matching peaks

                //IEnumerable<MsMsSearchUnit> NLSearchUnits = fragments.Where(x=> x.Description.Equals("Neutral Loss")).Select(x => {x.Mz = (msmsPrecursorMz - x.Mz); return x;});
                var NLSearchUnits   = fragments.Where(x => x.Description.Equals("Neutral Loss")).Select(y => new MsMsSearchUnit(msmsPrecursorMz - y.Mz, "Neutral Loss"));
                var MsMsSearchUnits = PISearchUnits.Concat(NLSearchUnits).ToList();
                SpectrumSearchResult spectrumSearchResult;


                var hcdSpectrum = MsMsSpectrum.ActivationMethod == ActivationMethod.HCD ? MsMsSpectrum : MatchedSpectrum;
                var cidSpectrum = MsMsSpectrum.ActivationMethod == ActivationMethod.CID ? MsMsSpectrum : MatchedSpectrum;


                var HcdSearchResultList = hcdSpectrum != null? (from msMsSearchUnit in MsMsSearchUnits
                                                                let peak = hcdSpectrum.FindPeak(msMsSearchUnit.Mz, hcdTolerance)
                                                                           select new MsMsSearchResult(msMsSearchUnit, peak)).ToList() : new List <MsMsSearchResult>();
                var CidSearchResultList = cidSpectrum != null? (from msMsSearchUnit in MsMsSearchUnits
                                                                let peak = cidSpectrum.FindPeak(msMsSearchUnit.Mz, cidTolerance)
                                                                           select new MsMsSearchResult(msMsSearchUnit, peak)).ToList() : new List <MsMsSearchResult>();
                var SearchResultList = HcdSearchResultList.Concat(CidSearchResultList).ToList();
                if (precursorSpectrum != null)
                {
                    spectrumSearchResult = new SpectrumSearchResult(hcdSpectrum, cidSpectrum, precursorSpectrum,
                                                                    HcdSearchResultList, CidSearchResultList, xic, lcmsRun)
                    {
                        PrecursorTolerance = new Tolerance(hcdMassError, ToleranceUnit.Ppm)
                    };
                }
                else
                {
                    spectrumSearchResult = new SpectrumSearchResult(hcdSpectrum, cidSpectrum, HcdSearchResultList, CidSearchResultList, lcmsRun)
                    {
                        PrecursorTolerance = new Tolerance(hcdMassError, ToleranceUnit.Ppm)
                    };
                }

                if (hcdSpectrum != null)
                {
                    scanTracker.Add(hcdSpectrum.ScanNum);
                }
                if (cidSpectrum != null)
                {
                    scanTracker.Add(cidSpectrum.ScanNum);
                }

                if (SearchResultList.Count(x => x.ObservedPeak != null) < minMatches)
                {
                    continue;
                }
                spectrumSearchResultList.Add(spectrumSearchResult);

                // Report progress
                if (progress != null)
                {
                    var currentProgress = (int)((double)scan / maxScans * 100);
                    progress.Report(currentProgress);
                }
            }

            return(spectrumSearchResultList);
        }
Exemple #28
0
        private IEnumerable <TagSequenceMatch> GetMatches(IEnumerable <SequenceTag.SequenceTag> tags, ProductSpectrum spec, IScorer scorer)
        {
            // Match tags against the database
            var proteinsToTags = GetProteinToMatchedTagsMap(tags, _searchableDb, _aaSet, _tolerance, _tolerance);

            //var tagSequenceMatchList = new List<TagSequenceMatch>();

            // Extend matches
            foreach (var entry in proteinsToTags)
            {
                var proteinName     = entry.Key;
                var matchedTagSet   = entry.Value;
                var proteinSequence = matchedTagSet.Sequence;

                var tagFinder = new TagMatchFinder(spec, scorer, _featureFinder, proteinSequence, _tolerance, _aaSet, _maxSequenceMass);

                foreach (var matchedTag in matchedTagSet.Tags)
                {
                    if (matchedTag.Length < _minMatchedTagLength)
                    {
                        continue;
                    }
                    if (matchedTag.NTermFlankingMass == null && matchedTag.CTermFlankingMass == null)
                    {
                        continue;
                    }

                    var matches = tagFinder.FindMatches(matchedTag).ToArray();
                    //var prevScore = double.NegativeInfinity;
                    //foreach (var match in matches.OrderByDescending(m => m.Score))
                    foreach (var match in matches)
                    {
                        var sequence = proteinSequence.Substring(match.StartIndex, match.EndIndex - match.StartIndex);
                        //re-scoring
                        var sequenceObj = Sequence.CreateSequence(sequence, match.ModificationText, _aaSet);
                        match.Score = sequenceObj.GetInternalCleavages().Sum(c => scorer.GetFragmentScore(c.PrefixComposition, c.SuffixComposition));

                        //var numMatches = matchedTag.Length * 2 + match.NTermScore + match.CTermScore;
                        //var score = match.NTermScore + match.CTermScore;
                        //score += (matchedTag.NumReliableNTermFlankingMasses > 0)
                        //  ? matchedTag.Length*CompositeScorer.ScoreParam.Prefix.ConsecutiveMatch
                        //: matchedTag.Length*CompositeScorer.ScoreParam.Suffix.ConsecutiveMatch;

                        // Poisson p-value score
                        //var n = (match.EndIndex - match.StartIndex - 1)*2;
                        //var lambda = numMatches / n;
                        //var pValue = 1 - Poisson.CDF(lambda, numMatches);
                        //var pScore = (pValue > 0) ? - Math.Log(pValue, 2) : 50.0;
                        //if (numMatches < 5) break;
                        //if (prevScore - numMatches > 2) break;
                        //prevScore = numMatches;

                        var pre  = match.StartIndex == 0 ? '-' : proteinSequence[match.StartIndex - 1];              // startIndex is inclusive
                        var post = match.EndIndex >= proteinSequence.Length ? '-' : proteinSequence[match.EndIndex]; // endIndex is Exclusive

                        yield return(new TagSequenceMatch(sequence, proteinName, match, pre, post));

                        //tagSequenceMatchList.Add(new TagSequenceMatch(sequence, proteinName, match, pre, post));
                    }
                }
            }
            //return tagSequenceMatchList;
        }
Exemple #29
0
        public static List <LipidGroupSearchResult> RunGlobalWorkflow(IEnumerable <Lipid> lipidList, LcMsRun lcmsRun, double hcdMassError, double cidMassError, ScoreModel scoreModel, IProgress <int> progress = null)
        {
            //TextWriter textWriter = new StreamWriter("outputNeg.tsv");
            var lipidGroupSearchResultList = new List <LipidGroupSearchResult>();

            var hcdTolerance = new Tolerance(hcdMassError, ToleranceUnit.Ppm);
            var cidTolerance = new Tolerance(cidMassError, ToleranceUnit.Ppm);

            //var lipidsGroupedByTarget = lipidList.OrderBy(x => x.LipidTarget.Composition.Mass).GroupBy(x => x.LipidTarget).ToList(); //order by mz
            var lipidsGroupedByTarget = lipidList.OrderBy(x => x.LipidTarget.MzRounded).GroupBy(x => x.LipidTarget).ToList();

            var    minLcScan = lcmsRun.MinLcScan;
            double maxLcScan = lcmsRun.MaxLcScan;



            var activationMethodCombination = FigureOutActivationMethodCombination(lcmsRun);

            if (activationMethodCombination == ActivationMethodCombination.Unsupported)
            {
                throw new SystemException("Unsupported activation method.");
            }
            var useTwoScans = activationMethodCombination == ActivationMethodCombination.CidThenHcd || activationMethodCombination == ActivationMethodCombination.HcdThenCid;

            for (var i = minLcScan; i <= maxLcScan; i++)
            {
                // Lookup the MS/MS Spectrum
                var firstMsMsSpectrum = lcmsRun.GetSpectrum(i) as ProductSpectrum;
                if (firstMsMsSpectrum == null)
                {
                    continue;
                }

                // Lookup the MS/MS Spectrum
                ProductSpectrum secondMsMsSpectrum = null;
                if (useTwoScans)
                {
                    secondMsMsSpectrum = lcmsRun.GetSpectrum(i + 1) as ProductSpectrum;
                    if (secondMsMsSpectrum == null)
                    {
                        continue;
                    }

                    // If m/z values of the MS/MS spectrums do not match, just move on
                    var deltaMz = firstMsMsSpectrum.IsolationWindow.IsolationWindowTargetMz -
                                  secondMsMsSpectrum.IsolationWindow.IsolationWindowTargetMz;
                    if (Math.Abs(deltaMz) > 0.01)
                    {
                        continue;
                    }
                }

                //textWriter.WriteLine(i);
                //Console.WriteLine(DateTime.Now + "\tProcessing Scan" + i);

                // Grab Precursor Spectrum

                var precursorScanNumber = 0;
                if (lcmsRun.MinMsLevel == 1) //Make sure there are precursor scans in file
                {
                    precursorScanNumber = lcmsRun.GetPrecursorScanNum(i);
                }

                var precursorSpectrum = lcmsRun.GetSpectrum(precursorScanNumber);

                // Assign each MS/MS spectrum to HCD or CID
                ProductSpectrum hcdSpectrum;
                ProductSpectrum cidSpectrum;
                if (firstMsMsSpectrum.ActivationMethod == ActivationMethod.HCD)
                {
                    hcdSpectrum = firstMsMsSpectrum;
                    cidSpectrum = secondMsMsSpectrum;
                }
                else
                {
                    hcdSpectrum = secondMsMsSpectrum;
                    cidSpectrum = firstMsMsSpectrum;
                }

                var msMsPrecursorMz     = firstMsMsSpectrum.IsolationWindow.IsolationWindowTargetMz;
                var mzToSearchTolerance = hcdMassError * msMsPrecursorMz / 1000000;
                var lowMz  = msMsPrecursorMz - mzToSearchTolerance;
                var highMz = msMsPrecursorMz + mzToSearchTolerance;

                foreach (var grouping in lipidsGroupedByTarget)
                {
                    var lipidTarget = grouping.Key;
                    var lipidMz     = lipidTarget.MzRounded;

                    // If we reached the point where the m/z is too high, we can exit
                    if (lipidMz > highMz)
                    {
                        break;
                    }

                    if (lipidMz > lowMz)
                    {
                        // Find the MS1 data
                        //Xic xic = lcmsRun.GetPrecursorExtractedIonChromatogram(lipidMz, hcdTolerance, i);
                        var xic = lcmsRun.GetFullPrecursorIonExtractedIonChromatogram(lipidMz, hcdTolerance);

                        // Bogus data
                        if (precursorSpectrum != null && (xic.GetApexScanNum() < 0 || xic.GetSumIntensities() <= 0))
                        {
                            continue;
                        }

                        // Grab the MS/MS peak to search for
                        IEnumerable <MsMsSearchUnit> msMsSearchUnits = lipidTarget.GetMsMsSearchUnits();

                        // Get all matching peaks
                        var hcdSearchResultList = hcdSpectrum != null ? (from msMsSearchUnit in msMsSearchUnits let peak = hcdSpectrum.FindPeak(msMsSearchUnit.Mz, hcdTolerance) select new MsMsSearchResult(msMsSearchUnit, peak)).ToList() : new List <MsMsSearchResult>();
                        var cidSearchResultList = cidSpectrum != null ? (from msMsSearchUnit in msMsSearchUnits let peak = cidSpectrum.FindPeak(msMsSearchUnit.Mz, cidTolerance) select new MsMsSearchResult(msMsSearchUnit, peak)).ToList() : new List <MsMsSearchResult>();

                        // Create spectrum search results
                        SpectrumSearchResult   spectrumSearchResult;
                        LipidGroupSearchResult lipidGroupSearchResult;

                        if (precursorSpectrum != null)
                        {
                            spectrumSearchResult = new SpectrumSearchResult(hcdSpectrum, cidSpectrum, precursorSpectrum, hcdSearchResultList, cidSearchResultList, xic, lcmsRun)
                            {
                                PrecursorTolerance = new Tolerance(hcdMassError, ToleranceUnit.Ppm)
                            };
                            lipidGroupSearchResult = new LipidGroupSearchResult(lipidTarget, grouping.ToList(), spectrumSearchResult, scoreModel);
                        }
                        else //If there are no precursor scans in this file
                        {
                            spectrumSearchResult = new SpectrumSearchResult(hcdSpectrum, cidSpectrum, hcdSearchResultList, cidSearchResultList, lcmsRun)
                            {
                                PrecursorTolerance = new Tolerance(hcdMassError, ToleranceUnit.Ppm)
                            };
                            lipidGroupSearchResult = new LipidGroupSearchResult(lipidTarget, grouping.ToList(), spectrumSearchResult, scoreModel);
                        }


                        lipidGroupSearchResultList.Add(lipidGroupSearchResult);
                    }
                }

                // Skip an extra scan if we look at 2 at a time
                if (useTwoScans)
                {
                    i++;
                }

                // Report progress
                if (progress != null)
                {
                    var currentProgress = (int)(i / maxLcScan * 100);
                    progress.Report(currentProgress);
                }
            }

            //textWriter.Close();
            return(lipidGroupSearchResultList);
        }
Exemple #30
0
        private void GetMatchStatistics(ProductSpectrum ms2Spec, Sequence sequence, int parentIonCharge, StreamWriter writer)
        {
            if (ms2Spec == null)
            {
                return;
            }
            if (sequence == null)
            {
                return;
            }

            var BaseIonTypesCID  = new[] { BaseIonType.B, BaseIonType.Y };
            var BaseIonTypesETD  = new[] { BaseIonType.C, BaseIonType.Z };
            var tolerance        = new Tolerance(12);
            var MinProductCharge = 1;
            var MaxProductCharge = Math.Min(parentIonCharge + 2, 20);

            var baseIonTypes = ms2Spec.ActivationMethod != ActivationMethod.ETD ? BaseIonTypesCID : BaseIonTypesETD;
            var refIntensity = CompositeScorer.GetRefIntensity(ms2Spec.Peaks);

            var activationMethodFlag = ms2Spec.ActivationMethod == ActivationMethod.ETD ? 2 : 1;
            var cleavages            = sequence.GetInternalCleavages();
            var nComplementaryFrags  = 0;

            var prefixStat = new FragmentStat();
            var suffixStat = new FragmentStat();

            var minMz = ms2Spec.Peaks.First().Mz;
            var maxMz = ms2Spec.Peaks.Last().Mz;

            var cleavageIndex = 0;

            var preFixIonCheck = new bool[sequence.Count + 1];
            var sufFixIonCheck = new bool[sequence.Count + 1];

            foreach (var c in cleavages)
            {
                var prefixHit = false;
                var suffixHit = false;

                foreach (var baseIonType in baseIonTypes)
                {
                    var stat = baseIonType.IsPrefix ? prefixStat : suffixStat;

                    var fragmentComposition = baseIonType.IsPrefix
                                  ? c.PrefixComposition + baseIonType.OffsetComposition
                                  : c.SuffixComposition + baseIonType.OffsetComposition;

                    if (fragmentComposition.Mass < ms2Spec.Peaks[0].Mz)
                    {
                        continue;
                    }

                    var curFragMass = fragmentComposition.Mass;

                    /*var curObsIonCharge = 0;
                     * var curObsIonDist = 1.0d;
                     * var curObsIonCorr = 0d;
                     * var curObsIonIntensity = 0d;
                     * var curObsIonMassError = 0d;*/

                    var mostAbundantIsotopeIndex = fragmentComposition.GetMostAbundantIsotopeZeroBasedIndex();
                    var fragmentIonMostAbuMass   = fragmentComposition.Mass + Constants.C13MinusC12 * mostAbundantIsotopeIndex;

                    var maxCharge = (int)Math.Floor(fragmentIonMostAbuMass / (minMz - Constants.Proton));
                    var minCharge = (int)Math.Ceiling(fragmentIonMostAbuMass / (maxMz - Constants.Proton));
                    if (maxCharge < 1 || maxCharge > MaxProductCharge)
                    {
                        maxCharge = MaxProductCharge;
                    }
                    if (minCharge < 1 || minCharge < MinProductCharge)
                    {
                        minCharge = MinProductCharge;
                    }

                    //var ionMatch = false;
                    for (var charge = minCharge; charge <= maxCharge; charge++)
                    {
                        var ion = new Ion(fragmentComposition, charge);

                        var isotopePeaks = ms2Spec.GetAllIsotopePeaks(ion, tolerance, 0.1);

                        if (isotopePeaks == null)
                        {
                            continue;
                        }

                        var distCorr = CompositeScorer.GetDistCorr(ion, isotopePeaks);
                        if (distCorr.Item2 < 0.7 && distCorr.Item1 > 0.03)
                        {
                            continue;
                        }
                        var mostAbuPeak = isotopePeaks[mostAbundantIsotopeIndex];
                        var intScore    = mostAbuPeak.Intensity / refIntensity;

                        /*
                         * if (ionMatch == false || curObsIonIntensity < intScore)
                         * {
                         *  curObsIonCharge = charge;
                         *  curObsIonCorr = distCorr.Item2;
                         *  curObsIonDist = distCorr.Item1;
                         *  curObsIonIntensity = intScore;
                         *
                         *  var mostAbuPeakMz = Ion.GetIsotopeMz(curFragMass, charge, mostAbundantIsotopeIndex);
                         *  curObsIonMassError = (Math.Abs(mostAbuPeak.Mz - mostAbuPeakMz) / mostAbuPeakMz) * 1e6;
                         *
                         *  //var curObsIonMass = Ion.GetMonoIsotopicMass(mostAbuPeak.Mz, charge, mostAbundantIsotopeIndex);
                         *  //curObsIonMassError = (Math.Abs(curFragMass - curObsIonMass) / curFragMass) * 1e6;
                         * }
                         * ionMatch = true;
                         */
                        var mostAbuPeakMz      = Ion.GetIsotopeMz(curFragMass, charge, mostAbundantIsotopeIndex);
                        var curObsIonMassError = (Math.Abs(mostAbuPeak.Mz - mostAbuPeakMz) / mostAbuPeakMz) * 1e6;

                        stat.Count++;
                        stat.Intensity += Math.Min(intScore, 1.0);
                        stat.Corr      += distCorr.Item2;
                        stat.Dist      += distCorr.Item1;
                        stat.MassError += curObsIonMassError;

                        if (baseIonType.IsPrefix)
                        {
                            prefixHit = true;
                        }
                        else
                        {
                            suffixHit = true;
                        }
                    }

                    //if (!ionMatch) continue;
                }

                if (prefixHit)
                {
                    preFixIonCheck[cleavageIndex] = true;
                }
                if (suffixHit)
                {
                    sufFixIonCheck[cleavageIndex] = true;
                }

                if (prefixHit && suffixHit)
                {
                    nComplementaryFrags++;
                }
                cleavageIndex++;
            }

            var preContCount = 0;
            var sufContCount = 0;

            for (var i = 0; i < preFixIonCheck.Length - 1; i++)
            {
                if (preFixIonCheck[i] && preFixIonCheck[i + 1])
                {
                    preContCount++;
                }
                if (sufFixIonCheck[i] && sufFixIonCheck[i + 1])
                {
                    sufContCount++;
                }
            }

            writer.Write(activationMethodFlag);
            writer.Write("\t");
            writer.Write(sequence.Composition.Mass);
            writer.Write("\t");
            writer.Write(sequence.Count);
            writer.Write("\t");
            writer.Write(nComplementaryFrags);
            writer.Write("\t");

            writer.Write("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", prefixStat.Count, preContCount, prefixStat.Intensity, prefixStat.Corr, prefixStat.Dist, prefixStat.MassError);
            writer.Write("\t");
            writer.Write("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", suffixStat.Count, sufContCount, suffixStat.Intensity, suffixStat.Corr, suffixStat.Dist, suffixStat.MassError);
            writer.Write("\n");
        }
Exemple #31
0
        public static List <SpectrumSearchResult> RunInformedWorkflow(
            LipidTarget target,
            LcMsRun lcmsRun,
            double hcdMassError,
            double cidMassError,
            ScoreModel scoreModel = null)
        {
            IEnumerable <MsMsSearchUnit> msMsSearchUnits = target.GetMsMsSearchUnits();

            // I have to subtract an H for the target Ion since InformedProteomics will assume protenated
            var targetIon    = new Ion(target.Composition - Composition.Hydrogen, 1);
            var targetMz     = target.MzRounded;
            var hcdTolerance = new Tolerance(hcdMassError, ToleranceUnit.Ppm);
            var cidTolerance = new Tolerance(cidMassError, ToleranceUnit.Ppm);
            var activationMethodCombination = GlobalWorkflow.FigureOutActivationMethodCombination(lcmsRun);

            // Find out which MS/MS scans have a precursor m/z that matches the target
            //List<int> matchingMsMsScanNumbers = lcmsRun.GetFragmentationSpectraScanNums(targetIon).ToList();
            var matchingMsMsScanNumbers = lcmsRun.GetFragmentationSpectraScanNums(targetMz).ToList();

            var spectrumSearchResultList = new List <SpectrumSearchResult>();

            for (var i = 0; i + 1 < matchingMsMsScanNumbers.Count; i += 2)
            {
                var firstScanNumber  = matchingMsMsScanNumbers[i];
                var secondScanNumber = matchingMsMsScanNumbers[i + 1];

                // Scan numbers should be consecutive.

                /*
                 * if (secondScanNumber - firstScanNumber != 1)
                 * {
                 *  i--;
                 *  continue;
                 * }
                 * */
                ProductSpectrum firstMsMsSpectrum  = null;
                ProductSpectrum secondMsMsSpectrum = null;

                if (activationMethodCombination == ActivationMethodCombination.CidThenHcd ||
                    activationMethodCombination == ActivationMethodCombination.HcdThenCid)
                {
                    // Lookup the MS/MS Spectrum
                    firstMsMsSpectrum = lcmsRun.GetSpectrum(firstScanNumber) as ProductSpectrum;
                    if (firstMsMsSpectrum == null)
                    {
                        continue;
                    }

                    // Lookup the MS/MS Spectrum
                    secondMsMsSpectrum = lcmsRun.GetSpectrum(secondScanNumber) as ProductSpectrum;
                    if (secondMsMsSpectrum == null)
                    {
                        continue;
                    }
                }
                else if (activationMethodCombination == ActivationMethodCombination.CidOnly ||
                         activationMethodCombination == ActivationMethodCombination.HcdOnly)
                {
                    firstMsMsSpectrum = lcmsRun.GetSpectrum(firstScanNumber) as ProductSpectrum;
                }

                if (firstMsMsSpectrum == null)
                {
                    continue;
                }

                // Filter MS/MS Spectrum based on mass error
                var msMsPrecursorMz = firstMsMsSpectrum.IsolationWindow.IsolationWindowTargetMz;
                //if (Math.Abs(msMsPrecursorMz - targetMz) > 0.4) continue;
                var ppmError = LipidUtil.PpmError(targetMz, msMsPrecursorMz);
                if (Math.Abs(ppmError) > hcdMassError)
                {
                    continue;
                }


                // Assign each MS/MS spectrum to HCD or CID
                ProductSpectrum hcdSpectrum;
                ProductSpectrum cidSpectrum;
                if (firstMsMsSpectrum.ActivationMethod == ActivationMethod.HCD)
                {
                    hcdSpectrum = firstMsMsSpectrum;
                    cidSpectrum = secondMsMsSpectrum;
                }
                else
                {
                    hcdSpectrum = secondMsMsSpectrum;
                    cidSpectrum = firstMsMsSpectrum;
                }

                // Get all matching peaks
                var hcdSearchResultList = hcdSpectrum != null ? (from msMsSearchUnit in msMsSearchUnits let peak = hcdSpectrum.FindPeak(msMsSearchUnit.Mz, hcdTolerance) select new MsMsSearchResult(msMsSearchUnit, peak)).ToList() : new List <MsMsSearchResult>();
                var cidSearchResultList = cidSpectrum != null ? (from msMsSearchUnit in msMsSearchUnits let peak = cidSpectrum.FindPeak(msMsSearchUnit.Mz, cidTolerance) select new MsMsSearchResult(msMsSearchUnit, peak)).ToList() : new List <MsMsSearchResult>();

                // Find the MS1 data
                //Xic xic = lcmsRun.GetPrecursorExtractedIonChromatogram(targetMz, hcdTolerance, firstScanNumber);
                var precursorScanNumber = 0;
                if (lcmsRun.MinMsLevel == 1) //Make sure there are precursor scans in file
                {
                    precursorScanNumber = lcmsRun.GetPrecursorScanNum(matchingMsMsScanNumbers[i]);
                }
                var precursorSpectrum = lcmsRun.GetSpectrum(precursorScanNumber);
                var xic = lcmsRun.GetFullPrecursorIonExtractedIonChromatogram(targetMz, hcdTolerance);


                // Bogus data
                if (precursorSpectrum != null && (xic.GetApexScanNum() < 0 || xic.GetSumIntensities() <= 0))
                {
                    continue;
                }

                SpectrumSearchResult spectrumSearchResult;
                if (precursorSpectrum != null)
                {
                    spectrumSearchResult = new SpectrumSearchResult(hcdSpectrum, cidSpectrum, precursorSpectrum, hcdSearchResultList, cidSearchResultList, xic, lcmsRun, scoreModel, target)
                    {
                        PrecursorTolerance = new Tolerance(hcdMassError, ToleranceUnit.Ppm)
                    };
                }
                else //If there are no precursor scans in this file
                {
                    spectrumSearchResult = new SpectrumSearchResult(hcdSpectrum, cidSpectrum, hcdSearchResultList, cidSearchResultList, lcmsRun, scoreModel, target)
                    {
                        PrecursorTolerance = new Tolerance(hcdMassError, ToleranceUnit.Ppm),
                    };
                }
                spectrumSearchResultList.Add(spectrumSearchResult);
            }

            return(spectrumSearchResultList);
        }