private LibraryRankedSpectrumInfo(SpectrumPeaksInfo info, IsotopeLabelType labelType,
                                          TransitionGroup group, SrmSettings settings,
                                          string lookupSequence, ExplicitMods lookupMods,
                                          IEnumerable <int> charges, IEnumerable <IonType> types,
                                          IEnumerable <int> rankCharges, IEnumerable <IonType> rankTypes,
                                          bool useFilter, bool matchAll, int minPeaks)
        {
            LabelType = labelType;

            // Avoid ReSharper multiple enumeration warning
            var rankChargesArray = rankCharges.ToArray();
            var rankTypesArray   = rankTypes.ToArray();

            if (!useFilter)
            {
                if (charges == null)
                {
                    charges = GetRanked(rankChargesArray, Transition.ALL_CHARGES);
                }
                if (types == null)
                {
                    types = GetRanked(rankTypesArray, Transition.ALL_TYPES);
                }
                matchAll = true;
            }

            RankParams rp = new RankParams
            {
                sequence        = lookupSequence,
                precursorCharge = group.PrecursorCharge,
                charges         = charges ?? rankCharges,
                types           = types ?? rankTypes,
                matchAll        = matchAll,
                rankCharges     = rankChargesArray,
                rankTypes       = rankTypesArray,
                // Precursor isotopes will not be included in MS/MS, if they will be filtered
                // from MS1
                excludePrecursorIsotopes = settings.TransitionSettings.FullScan.IsEnabledMs,
                tranSettings             = settings.TransitionSettings
            };

            // Get necessary mass calculators and masses
            var calcMatchPre = settings.GetPrecursorCalc(labelType, lookupMods);
            var calcMatch    = settings.GetFragmentCalc(labelType, lookupMods);
            var calcPredict  = settings.GetFragmentCalc(group.LabelType, lookupMods);

            if (!string.IsNullOrEmpty(rp.sequence))
            {
                rp.precursorMz  = SequenceMassCalc.GetMZ(calcMatchPre.GetPrecursorMass(rp.sequence), rp.precursorCharge);
                rp.massPreMatch = calcMatch.GetPrecursorFragmentMass(rp.sequence);
                rp.massesMatch  = calcMatch.GetFragmentIonMasses(rp.sequence);
            }
            else
            {
                rp.precursorMz  = 0.0;
                rp.massPreMatch = 0.0;
                rp.massesMatch  = new double[0, 0];
            }
            rp.massPrePredict = rp.massPreMatch;
            rp.massesPredict  = rp.massesMatch;
            if (!ReferenceEquals(calcPredict, calcMatch) && !string.IsNullOrEmpty(rp.sequence))
            {
                rp.massPrePredict = calcPredict.GetPrecursorFragmentMass(rp.sequence);
                rp.massesPredict  = calcPredict.GetFragmentIonMasses(rp.sequence);
            }

            // Get values of interest from the settings.
            var tranSettings = settings.TransitionSettings;
            var predict      = tranSettings.Prediction;
            var filter       = tranSettings.Filter;
            var libraries    = tranSettings.Libraries;
            var instrument   = tranSettings.Instrument;

            // Get potential losses to all fragments in this peptide
            rp.massType        = predict.FragmentMassType;
            rp.potentialLosses = TransitionGroup.CalcPotentialLosses(rp.sequence,
                                                                     settings.PeptideSettings.Modifications, lookupMods,
                                                                     rp.massType);

            // Create arrays because ReadOnlyCollection enumerators are too slow
            // In some cases these collections must be enumerated for every ion
            // allowed in the library specturm.
            rp.startFinder = filter.FragmentRangeFirst;
            rp.endFinder   = filter.FragmentRangeLast;

            // Get library settings
            Tolerance    = libraries.IonMatchTolerance;
            rp.tolerance = Tolerance;
            rp.pick      = tranSettings.Libraries.Pick;
            int ionMatchCount = libraries.IonCount;

            // If no library filtering will happen, return all rankings for view in the UI
            if (!useFilter || rp.pick == TransitionLibraryPick.none)
            {
                if (rp.pick == TransitionLibraryPick.none)
                {
                    rp.pick = TransitionLibraryPick.all;
                }
                ionMatchCount = -1;
            }

            // Get instrument settings
            rp.minMz = instrument.MinMz;
            rp.maxMz = instrument.MaxMz;

            // Get the library spectrum mass-intensity pairs
            IList <SpectrumPeaksInfo.MI> listMI = info.Peaks;

            // Because sorting and matching observed ions with predicted
            // ions appear as bottlenecks in a profiler, a minimum number
            // of peaks may be supplied to allow the use of a 2-phase linear
            // filter that can significantly reduce the number of peaks
            // needing the O(n*log(n)) sorting and the O(n*m) matching.

            int   len             = listMI.Count;
            float intensityCutoff = 0;

            if (minPeaks != -1)
            {
                // Start searching for good cut-off at mean intensity.
                double totalIntensity = info.Intensities.Sum();

                FindIntensityCutoff(listMI, 0, (float)(totalIntensity / len) * 2, minPeaks, 1, ref intensityCutoff, ref len);
            }

            // Create filtered peak array storing original index for m/z ordering
            // to avoid needing to sort to return to this order.
            RankedMI[] arrayRMI = new RankedMI[len];
            // Detect when m/z values are out of order, and use the expensive sort
            // by m/z to correct this.
            double lastMz = double.MinValue;
            bool   sortMz = false;

            for (int i = 0, j = 0, lenOrig = listMI.Count; i < lenOrig; i++)
            {
                SpectrumPeaksInfo.MI mi = listMI[i];
                if (mi.Intensity >= intensityCutoff || intensityCutoff == 0)
                {
                    arrayRMI[j] = new RankedMI(mi, j);
                    j++;
                }
                if (ionMatchCount == -1)
                {
                    if (mi.Mz < lastMz)
                    {
                        sortMz = true;
                    }
                    lastMz = mi.Mz;
                }
            }

            // The one expensive sort is used to determine rank order
            // by intensity.
            Array.Sort(arrayRMI, OrderIntensityDesc);

            RankedMI[] arrayResult = new RankedMI[ionMatchCount != -1 ? ionMatchCount : arrayRMI.Length];

            foreach (RankedMI rmi in arrayRMI)
            {
                rmi.CalculateRank(rp);

                // If not filtering for only the highest ionMatchCount ranks
                if (ionMatchCount == -1)
                {
                    // Put the ranked record back where it started in the
                    // m/z ordering to avoid a second sort.
                    arrayResult[rmi.IndexMz] = rmi;
                }
                // Otherwise, if this ion was ranked, add it to the result array
                else if (rmi.Rank > 0)
                {
                    int countRanks = rmi.Rank;
                    arrayResult[countRanks - 1] = rmi;
                    // And stop when the array is full
                    if (countRanks == ionMatchCount)
                    {
                        break;
                    }
                }
            }

            // If not enough ranked ions were found, fill the rest of the results array
            if (ionMatchCount != -1)
            {
                for (int i = rp.Ranked; i < ionMatchCount; i++)
                {
                    arrayResult[i] = RankedMI.EMPTY;
                }
            }
            // If all ions are to be included, and some were found out of order, then
            // the expensive full sort by m/z is necesary.
            else if (sortMz)
            {
                Array.Sort(arrayResult, OrderMz);
            }

            _spectrum = MakeReadOnly(arrayResult);
        }
예제 #2
0
        private LibraryRankedSpectrumInfo(SpectrumPeaksInfo info, IsotopeLabelType labelType,
                                          TransitionGroupDocNode groupDocNode, SrmSettings settings,
                                          Target lookupSequence, ExplicitMods lookupMods,
                                          IEnumerable <Adduct> charges, IEnumerable <IonType> types,
                                          IEnumerable <Adduct> rankCharges, IEnumerable <IonType> rankTypes,
                                          double?score, bool useFilter, bool matchAll, int minPeaks)
        {
            LabelType = labelType;

            // Avoid ReSharper multiple enumeration warning
            var rankChargesArray = rankCharges.ToArray();
            var rankTypesArray   = rankTypes.ToArray();

            TransitionGroup group       = groupDocNode.TransitionGroup;
            bool            isProteomic = group.IsProteomic;

            if (score == null && groupDocNode.HasLibInfo && groupDocNode.LibInfo is BiblioSpecSpectrumHeaderInfo libInfo)
            {
                Score = libInfo.Score;
            }
            else
            {
                Score = score;
            }

            if (!useFilter)
            {
                if (charges == null)
                {
                    charges = GetRanked(rankChargesArray, isProteomic ? Transition.DEFAULT_PEPTIDE_CHARGES : Transition.DEFAULT_MOLECULE_CHARGES);
                }
                if (types == null)
                {
                    types = GetRanked(rankTypesArray, isProteomic ? Transition.PEPTIDE_ION_TYPES : Transition.MOLECULE_ION_TYPES);
                }
                matchAll = true;
            }

            bool limitRanks =
                groupDocNode.IsCustomIon && // For small molecules, cap the number of ranked ions displayed if we don't have any peak metadata
                groupDocNode.Transitions.Any(t => string.IsNullOrEmpty(t.FragmentIonName));

            RankParams rp = new RankParams
            {
                sequence        = lookupSequence,
                precursorAdduct = group.PrecursorAdduct,
                adducts         = charges ?? rankCharges,
                types           = types ?? rankTypes,
                matchAll        = matchAll,
                rankCharges     = rankChargesArray.Select(a => Math.Abs(a.AdductCharge)).ToArray(),
                rankTypes       = rankTypesArray,
                // Precursor isotopes will not be included in MS/MS, if they will be filtered
                // from MS1
                excludePrecursorIsotopes = settings.TransitionSettings.FullScan.IsEnabledMs,
                tranSettings             = settings.TransitionSettings,
                rankLimit = limitRanks ? settings.TransitionSettings.Libraries.IonCount : (int?)null
            };

            // Get necessary mass calculators and masses
            var calcMatchPre = settings.GetPrecursorCalc(labelType, lookupMods);
            var calcMatch    = isProteomic ? settings.GetFragmentCalc(labelType, lookupMods) : settings.GetDefaultFragmentCalc();
            var calcPredict  = isProteomic ? settings.GetFragmentCalc(group.LabelType, lookupMods) : calcMatch;

            if (isProteomic && rp.sequence.IsProteomic)
            {
                rp.precursorMz    = SequenceMassCalc.GetMZ(calcMatchPre.GetPrecursorMass(rp.sequence), rp.precursorAdduct);
                rp.massPreMatch   = calcMatch.GetPrecursorFragmentMass(rp.sequence);
                rp.massesMatch    = calcMatch.GetFragmentIonMasses(rp.sequence);
                rp.knownFragments = null;
            }
            else if (!isProteomic && !rp.sequence.IsProteomic)
            {
                string isotopicForumla;
                rp.precursorMz  = SequenceMassCalc.GetMZ(calcMatchPre.GetPrecursorMass(rp.sequence.Molecule, null, rp.precursorAdduct, out isotopicForumla), rp.precursorAdduct);
                rp.massPreMatch = calcMatch.GetPrecursorFragmentMass(rp.sequence);
                // rp.massesMatch = calcMatch.GetFragmentIonMasses(rp.molecule); CONSIDER, for some molecule types someday?
                // For small molecules we can't predict fragmentation, so just use those we have
                // Older Resharper code inspection implementations insist on warning here
                // Resharper disable PossibleMultipleEnumeration
                var existing = groupDocNode.Transitions.Where(tran => tran.Transition.IsNonPrecursorNonReporterCustomIon()).Select(t => t.Transition.CustomIon.GetMass(MassType.Monoisotopic)).ToArray();
                rp.massesMatch = new IonTable <TypedMass>(IonType.custom, existing.Length);
                for (var i = 0; i < existing.Length; i++)
                {
                    rp.massesMatch[IonType.custom, i] = existing[i];
                }
                // Resharper restore PossibleMultipleEnumeration
                rp.knownFragments = groupDocNode.Transitions.Where(tran => tran.Transition.IsNonPrecursorNonReporterCustomIon()).Select(t =>
                                                                                                                                        new KnownFragment
                {
                    Adduct = t.Transition.Adduct,
                    Name   = t.GetFragmentIonName(CultureInfo.CurrentCulture, settings.TransitionSettings.Libraries.IonMatchTolerance),
                    Mz     = t.Mz
                }).ToList();
            }
            else
            {
                rp.precursorMz    = 0.0;
                rp.massPreMatch   = TypedMass.ZERO_MONO_MASSH;
                rp.massesMatch    = IonTable <TypedMass> .EMPTY;
                rp.knownFragments = null;
            }
            rp.massPrePredict = rp.massPreMatch;
            rp.massesPredict  = rp.massesMatch;
            if (!ReferenceEquals(calcPredict, calcMatch))
            {
                rp.massPrePredict = calcPredict.GetPrecursorFragmentMass(rp.sequence);
                if (rp.sequence.IsProteomic) // CONSIDER - eventually we may be able to predict fragments for small molecules?
                {
                    rp.massesPredict = calcPredict.GetFragmentIonMasses(rp.sequence);
                }
            }

            // Get values of interest from the settings.
            var tranSettings = settings.TransitionSettings;
            var predict      = tranSettings.Prediction;
            var filter       = tranSettings.Filter;
            var libraries    = tranSettings.Libraries;
            var instrument   = tranSettings.Instrument;

            // Get potential losses to all fragments in this peptide
            rp.massType        = predict.FragmentMassType;
            rp.potentialLosses = TransitionGroup.CalcPotentialLosses(rp.sequence,
                                                                     settings.PeptideSettings.Modifications, lookupMods,
                                                                     rp.massType);

            // Create arrays because ReadOnlyCollection enumerators are too slow
            // In some cases these collections must be enumerated for every ion
            // allowed in the library specturm.
            rp.startFinder = filter.FragmentRangeFirst;
            rp.endFinder   = filter.FragmentRangeLast;

            // Get library settings
            Tolerance    = libraries.IonMatchTolerance;
            rp.tolerance = Tolerance;
            rp.pick      = tranSettings.Libraries.Pick;
            int ionMatchCount = libraries.IonCount;

            // If no library filtering will happen, return all rankings for view in the UI
            if (!useFilter || rp.pick == TransitionLibraryPick.none)
            {
                if (rp.pick == TransitionLibraryPick.none)
                {
                    rp.pick = TransitionLibraryPick.all;
                }
                ionMatchCount = -1;
            }

            // Get instrument settings
            rp.minMz = instrument.MinMz;
            rp.maxMz = instrument.MaxMz;

            // Get the library spectrum mass-intensity pairs
            IList <SpectrumPeaksInfo.MI> listMI = info.Peaks;

            // Because sorting and matching observed ions with predicted
            // ions appear as bottlenecks in a profiler, a minimum number
            // of peaks may be supplied to allow the use of a 2-phase linear
            // filter that can significantly reduce the number of peaks
            // needing the O(n*log(n)) sorting and the O(n*m) matching.

            int   len             = listMI.Count;
            float intensityCutoff = 0;

            if (minPeaks != -1)
            {
                // Start searching for good cut-off at mean intensity.
                double totalIntensity = info.Intensities.Sum();

                FindIntensityCutoff(listMI, 0, (float)(totalIntensity / len) * 2, minPeaks, 1, ref intensityCutoff, ref len);
            }

            // Create filtered peak array storing original index for m/z ordering
            // to avoid needing to sort to return to this order.
            RankedMI[] arrayRMI = new RankedMI[len];
            // Detect when m/z values are out of order, and use the expensive sort
            // by m/z to correct this.
            double lastMz = double.MinValue;
            bool   sortMz = false;

            for (int i = 0, j = 0, lenOrig = listMI.Count; i < lenOrig; i++)
            {
                SpectrumPeaksInfo.MI mi = listMI[i];
                if (mi.Intensity >= intensityCutoff || intensityCutoff == 0)
                {
                    arrayRMI[j] = new RankedMI(mi, j);
                    j++;
                }
                if (ionMatchCount == -1)
                {
                    if (mi.Mz < lastMz)
                    {
                        sortMz = true;
                    }
                    lastMz = mi.Mz;
                }
            }

            // The one expensive sort is used to determine rank order
            // by intensity, or m/z in case of a tie.
            Array.Sort(arrayRMI, OrderIntensityDesc);

            RankedMI[] arrayResult = new RankedMI[ionMatchCount != -1 ? ionMatchCount : arrayRMI.Length];

            foreach (RankedMI rmi in arrayRMI)
            {
                rmi.CalculateRank(rp);

                // If not filtering for only the highest ionMatchCount ranks
                if (ionMatchCount == -1)
                {
                    // Put the ranked record back where it started in the
                    // m/z ordering to avoid a second sort.
                    arrayResult[rmi.IndexMz] = rmi;
                }
                // Otherwise, if this ion was ranked, add it to the result array
                else if (rmi.Rank > 0)
                {
                    int countRanks = rmi.Rank;
                    arrayResult[countRanks - 1] = rmi;
                    // And stop when the array is full
                    if (countRanks == ionMatchCount)
                    {
                        break;
                    }
                }
            }

            // Is this a theoretical library with no intensity variation? If so it can't be ranked.
            // If it has any interesting peak annotations, pass those through
            if (rp.Ranked == 0 && arrayRMI.All(rmi => rmi.Intensity == arrayRMI[0].Intensity))
            {
                // Only do this if we have been asked to limit the ions matched, and there are any annotations
                if (ionMatchCount != -1 && arrayRMI.Any(rmi => rmi.HasAnnotations))
                {
                    // Pass through anything with an annotation as being of probable interest
                    arrayResult   = arrayRMI.Where(rmi => rmi.HasAnnotations).ToArray();
                    ionMatchCount = -1;
                }
            }

            // If not enough ranked ions were found, fill the rest of the results array
            if (ionMatchCount != -1)
            {
                for (int i = rp.Ranked; i < ionMatchCount; i++)
                {
                    arrayResult[i] = RankedMI.EMPTY;
                }
            }
            // If all ions are to be included, and some were found out of order, then
            // the expensive full sort by m/z is necesary.
            else if (sortMz)
            {
                Array.Sort(arrayResult, OrderMz);
            }

            _spectrum = MakeReadOnly(arrayResult);
        }
예제 #3
0
        public SpectrumRanker(TargetInfo targetInfo, SrmSettings settings,
                              FragmentFilter fragmentFilter)
        {
            TargetInfoObj     = targetInfo;
            FragmentFilterObj = fragmentFilter;
            var             groupDocNode = TargetInfoObj.TransitionGroupDocNode;
            TransitionGroup group        = groupDocNode.TransitionGroup;
            bool            isProteomic  = group.IsProteomic;

            bool limitRanks =
                groupDocNode.IsCustomIon && // For small molecules, cap the number of ranked ions displayed if we don't have any peak metadata
                groupDocNode.Transitions.Any(t => string.IsNullOrEmpty(t.FragmentIonName));

            RankLimit = limitRanks ? settings.TransitionSettings.Libraries.IonCount : (int?)null;

            // Get necessary mass calculators and masses
            var            labelType    = targetInfo.SpectrumLabelType;
            var            lookupMods   = targetInfo.LookupMods;
            var            calcMatchPre = settings.GetPrecursorCalc(labelType, lookupMods);
            var            calcMatch    = isProteomic ? settings.GetFragmentCalc(labelType, lookupMods) : settings.GetDefaultFragmentCalc();
            var            calcPredict  = isProteomic ? settings.GetFragmentCalc(group.LabelType, lookupMods) : calcMatch;
            MoleculeMasses moleculeMasses;

            if (null != lookupMods && lookupMods.HasCrosslinks)
            {
                moleculeMasses = GetCrosslinkMasses(settings);
            }
            else
            {
                if (isProteomic && Sequence.IsProteomic)
                {
                    moleculeMasses = new MoleculeMasses(
                        SequenceMassCalc.GetMZ(calcMatchPre.GetPrecursorMass(Sequence), PrecursorAdduct),
                        new IonMasses(calcMatch.GetPrecursorFragmentMass(Sequence),
                                      calcMatch.GetFragmentIonMasses(Sequence)));
                }
                else if (!isProteomic && !Sequence.IsProteomic)
                {
                    string isotopicFormula;
                    var    knownFragments = new List <MatchedFragmentIon>();
                    foreach (var tran in groupDocNode.Transitions)
                    {
                        if (tran.Transition.IsNonPrecursorNonReporterCustomIon())
                        {
                            knownFragments.Add(new MatchedFragmentIon(IonType.custom, knownFragments.Count + 1,
                                                                      tran.Transition.Adduct,
                                                                      tran.GetFragmentIonName(CultureInfo.CurrentCulture,
                                                                                              settings.TransitionSettings.Libraries.IonMatchTolerance),
                                                                      null,
                                                                      tran.Mz));
                        }
                    }

                    var ionMasses =
                        new IonMasses(calcMatch.GetPrecursorFragmentMass(Sequence), IonTable <TypedMass> .EMPTY)
                        .ChangeKnownFragments(knownFragments);
                    moleculeMasses =
                        new MoleculeMasses(
                            SequenceMassCalc.GetMZ(
                                calcMatchPre.GetPrecursorMass(Sequence.Molecule, null, PrecursorAdduct,
                                                              out isotopicFormula), PrecursorAdduct), ionMasses);
                }
                else
                {
                    moleculeMasses = new MoleculeMasses(0.0,
                                                        new IonMasses(TypedMass.ZERO_MONO_MASSH, IonTable <TypedMass> .EMPTY));
                }

                if (!ReferenceEquals(calcPredict, calcMatch))
                {
                    var ionTable = moleculeMasses.MatchIonMasses.FragmentMasses;
                    if (Sequence.IsProteomic
                        ) // CONSIDER - eventually we may be able to predict fragments for small molecules?
                    {
                        ionTable = calcPredict.GetFragmentIonMasses(Sequence);
                    }
                    moleculeMasses =
                        moleculeMasses.ChangePredictIonMasses(new IonMasses(
                                                                  calcPredict.GetPrecursorFragmentMass(Sequence),
                                                                  ionTable));
                }
            }

            MoleculeMassesObj = moleculeMasses;

            // Get values of interest from the settings.
            TransitionSettings = settings.TransitionSettings;

            // Get potential losses to all fragments in this peptide
            PotentialLosses = TransitionGroup.CalcPotentialLosses(Sequence, settings.PeptideSettings.Modifications,
                                                                  lookupMods, MassType);
        }