}//*/

        public IEnumerable <ProductMatch> GetProductMZs(DBOptions options, GraphML_List <MsMsPeak> peaks)//, List<double> theoretical_product_mzs = null)
        {
            // speed optimizations
            //double[] experimental_masses = Query.spectrum.Masses;
            //GraphML_List<MSPeak> peaks = Query.spectrum.Peaks;
            int num_experimental_peaks = peaks.Count;

            TotalTheoreticalProducts = 0;
            TotalWeightedProducts    = 0;
            //New version that should include charged ions

            //foreach (ProductMatch matchTheo in AllFragmentSearch.ComputeAllFragments(Peptide, Query.precursor.Charge, options))
            foreach (ProductMatch matchTheo in options.fragments.ComputeFragmentsFast(Peptide.GetMasses(), Query.precursor.Charge, options))
            //foreach (ProductMatch matchTheo in options.fragments.ComputeFragments(Peptide, Query.precursor.Charge, options))
            {
                TotalTheoreticalProducts++;
                TotalWeightedProducts += matchTheo.weight;//++;
                //TODO test what is most common between charged ions, and uncharged ions
                //for (int charge = 1; charge <= Query.precursor.Charge; charge++)
                //for (int charge = Query.precursor.Charge; charge >= 1; charge--)
                //{
                double massDiff = options.productMassTolerance.Value;
                double bestMz   = -1;
                double bestInt  = 0;

                foreach (int index in Query.spectrum.GetIndexOfMZInRange(matchTheo.theoMz, options.productMassTolerance))
                {
                    if (peaks[index].Charge <= 0 || peaks[index].Charge == matchTheo.charge)
                    {
                        double diff = Numerics.CalculateMassError(peaks[index].MZ, matchTheo.theoMz, options.productMassTolerance.Units);
                        //double diff = matchTheo.theoMz - peaks[index].MZ;// experimental_masses[index];//TODO DALTON ONLY : add product mass tolerance unit test
                        if (Math.Abs(diff) < options.productMassTolerance.Value)
                        {
                            if (Math.Abs(diff) < Math.Abs(massDiff))//TODO Priority to intensity, or precision?
                            {
                                massDiff = diff;
                                bestMz   = peaks[index].MZ;
                            }
                            //if (peaks[index].Intensity > bestInt)
                            bestInt += peaks[index].Intensity;
                        }
                    }
                }
                if (bestMz >= 0)
                {
                    ProductMatch pMatch = new ProductMatch();
                    pMatch.weight              = matchTheo.weight;
                    pMatch.theoMz              = matchTheo.theoMz; // Utilities.MZFromMzSingleCharge(theoMass, charge);
                    pMatch.obsMz               = bestMz;           // experimental_masses[bestIndex];
                    pMatch.mass_diff           = massDiff;
                    pMatch.obsIntensity        = bestInt;          // Intensities[bestIndex];
                    pMatch.charge              = matchTheo.charge; // peaks[bestIndex].Charge;
                    pMatch.Fragment            = matchTheo.Fragment;
                    pMatch.fragmentPos         = matchTheo.fragmentPos;
                    pMatch.normalizedIntensity = pMatch.obsIntensity / (Query.spectrum.InjectionTime * Query.spectrum.PrecursorIntensityPerMilliSecond);
                    yield return(pMatch);
                    //break;
                }
            }
        }
示例#2
0
 public static int DescendingWeightComparison(ProductMatch left, ProductMatch right)
 {
     if (left.weight == right.weight)
     {
         return(-left.obsIntensity.CompareTo(right.obsIntensity));
     }
     return(-left.weight.CompareTo(right.weight));
 }
示例#3
0
 public ProductMatch(ProductMatch match)
 {
     this.theoMz              = match.theoMz;
     this.obsMz               = match.obsMz;
     this.obsIntensity        = match.obsIntensity;
     this.normalizedIntensity = match.normalizedIntensity;
     this.mass_diff           = match.mass_diff;
     this.charge              = match.charge;
     this.Fragment            = match.Fragment;
     this.fragmentPos         = match.fragmentPos;
     this.weight              = match.weight;
 }
示例#4
0
        public IEnumerable <ProductMatch> ComputeFragmentsFast(double[] masses, int precursorKnownCharge, DBOptions dbOptions)
        {
            int maxCharge;

            if (precursorKnownCharge > 1)
            {
                maxCharge = precursorKnownCharge - 1;
            }
            else
            {
                maxCharge = 1;
            }

            ProductMatch match  = new ProductMatch();
            double       cumulN = 0.0; // Constants.WATER_MONOISOTOPIC_MASS;
            double       cumulC = 0.0; // Constants.WATER_MONOISOTOPIC_MASS;

            for (int r = 0; r < masses.Length; r++)
            {
                cumulN += masses[r];
                cumulC += masses[masses.Length - r - 1];

                foreach (FragmentClass fragment in this)
                {
                    if (fragment.IsReverse)
                    {
                        match.fragmentPos = masses.Length - r;
                    }
                    else
                    {
                        match.fragmentPos = r + 1;
                    }
                    match.Fragment = fragment;

                    foreach (double product_mass in fragment.ComputeFragment(cumulC, cumulN))
                    {
                        match.weight = fragment.Distribution;//TODO Adjust this value by computing overall impact (times 10?)

                        for (int c = maxCharge; c > 0; c--)
                        {
                            match.theoMz = Numerics.MZFromMass(product_mass, c);
                            match.charge = c;
                            yield return(match);
                        }
                    }
                }
            }
        }
        public IEnumerable <ProductMatch> EnumerateAllProductMz(IEnumerable <ProductType> productTypes, Precursor precursor)
        {
            ProductMatch match = new ProductMatch();

            for (int r = 1; r < Length; r++)
            {
                foreach (ProductType product_type in productTypes)
                {
                    double product_mass = CalculateProductMass(product_type, r);

                    for (int c = precursor.Charge; c > 0; c--)
                    {
                        match.theoMz = Numerics.MZFromMass(product_mass, c);
                        match.charge = c;

                        //for (int iso = 0; iso < precursor.Isotopes.Count + 1; iso++)
                        yield return(match);// product_mz;// +Utilities.IsotopicMassShift(iso, c);
                    }
                }
            }
        }
示例#6
0
        public List <ProductMatch> GetCombinedSpectrum(DBOptions dbOptions, Dictionary <double, int> DicOfCommonPM = null)
        {
            Dictionary <PeptideSpectrumMatch, double> DicOfPsmFactor = new Dictionary <PeptideSpectrumMatch, double>();// this.ComputeMsMsNormalizationFactors();
            //Dictionary<ProductMatch, double> DicOfProductMsMsFactor = new Dictionary<ProductMatch, double>();
            Dictionary <string, Dictionary <PeptideSpectrumMatch, ProductMatch> > DicOfProducts = new Dictionary <string, Dictionary <PeptideSpectrumMatch, ProductMatch> >();

            double avgProbability = 0;

            foreach (PeptideSpectrumMatch psm in this)
            {
                bool usedPsm = false;
                foreach (ProductMatch match in psm.AllProductMatches)
                {
                    if (DicOfCommonPM == null || DicOfCommonPM.ContainsKey(match.theoMz))
                    {
                        string key = match.Fragment.Name + "|" + match.fragmentPos + "|" + match.charge;
                        if (!DicOfProducts.ContainsKey(key))
                        {
                            DicOfProducts.Add(key, new Dictionary <PeptideSpectrumMatch, ProductMatch>());
                        }
                        DicOfProducts[key].Add(psm, match);
                        //DicOfProductMsMsFactor.Add(match, DicOfPsmFactor[psm]);
                        usedPsm = true;
                    }
                }
                if (usedPsm)
                {
                    DicOfPsmFactor.Add(psm, psm.ProbabilityScore());//.MatchingIntensity);
                    avgProbability += psm.ProbabilityScore();
                }
            }
            avgProbability /= (double)DicOfPsmFactor.Count;

            double avgNormedInt          = 0;
            List <ProductMatch> products = new List <ProductMatch>();

            //if (DicOfProductMsMsFactor.Count > 0)
            {
                foreach (Dictionary <PeptideSpectrumMatch, ProductMatch> matchList in DicOfProducts.Values)
                {
                    ProductMatch newPM = null;
                    if (matchList.Count > 0)
                    {
                        double sumPsmFactor = 0;
                        foreach (PeptideSpectrumMatch psm in matchList.Keys)
                        {
                            if (psm.ProbabilityScore() > avgProbability)//Keep only above average spectrum
                            {
                                ProductMatch pm = matchList[psm];
                                if (newPM == null)
                                {
                                    newPM = new ProductMatch(pm);
                                    newPM.obsIntensity        = 0;
                                    newPM.normalizedIntensity = 0;
                                }
                                newPM.obsIntensity += pm.obsIntensity * DicOfPsmFactor[psm];// +pm.obsIntensity * DicOfProductMsMsFactor[pm];
                                //newPM.obsIntensity += pm.obsIntensity + pm.obsIntensity * DicOfProductMsMsFactor[pm];
                                newPM.normalizedIntensity += pm.normalizedIntensity * DicOfPsmFactor[psm];
                                sumPsmFactor += DicOfPsmFactor[psm];
                            }
                        }
                        if (sumPsmFactor > 0 && newPM != null)
                        {
                            newPM.obsIntensity        /= sumPsmFactor;
                            newPM.normalizedIntensity /= sumPsmFactor;

                            newPM.weight  = matchList.Count * newPM.normalizedIntensity;
                            avgNormedInt += newPM.normalizedIntensity;
                            products.Add(newPM);
                        }
                    }
                }
                if (products.Count > 0)
                {
                    avgNormedInt /= (double)products.Count;
                }

                //Add missed important fragments
                if (DicOfCommonPM != null)
                {
                    foreach (double mz in DicOfCommonPM.Keys)
                    {
                        bool found = false;
                        foreach (ProductMatch match in products)
                        {
                            if (match.theoMz == mz)
                            {
                                found = true;
                            }
                        }
                        if (!found)
                        {
                            double       sumPsmFactor = 0;
                            ProductMatch newMatch     = new ProductMatch();
                            newMatch.theoMz              = mz;
                            newMatch.weight              = 0;
                            newMatch.obsIntensity        = 0;
                            newMatch.normalizedIntensity = 0;
                            foreach (PeptideSpectrumMatch psm in DicOfPsmFactor.Keys)
                            {
                                foreach (MsMsPeak peak in psm.Query.spectrum.Peaks)
                                {
                                    if (Math.Abs(Proteomics.Utilities.Numerics.CalculateMassError(peak.MZ, mz, dbOptions.productMassTolerance.Units)) <= dbOptions.productMassTolerance.Value)
                                    {
                                        newMatch.obsIntensity        += peak.Intensity * DicOfPsmFactor[psm];
                                        newMatch.normalizedIntensity += (peak.Intensity / (psm.Query.spectrum.PrecursorIntensityPerMilliSecond * psm.Query.spectrum.InjectionTime)) * DicOfPsmFactor[psm];
                                        sumPsmFactor    += DicOfPsmFactor[psm];
                                        newMatch.weight += 1;
//                                        newMatch.obsIntensity += peak.Intensity;// + peak.Intensity * DicOfPsmFactor[psm];
//                                        newMatch.normalizedIntensity += peak.Intensity / (psm.Query.spectrum.PrecursorIntensityPerMilliSecond * psm.Query.spectrum.InjectionTime);
                                    }
                                }
                            }
                            if (newMatch.weight > 0)
                            {
                                newMatch.obsIntensity        /= sumPsmFactor;
                                newMatch.normalizedIntensity /= sumPsmFactor;
                            }
                            newMatch.weight *= newMatch.normalizedIntensity;
                            products.Add(newMatch);
                        }
                    }
                }

                //Keep only most intense fragments (5% of average normalized intensity)
                foreach (ProductMatch pm in products)
                {
                    if (pm.normalizedIntensity < avgNormedInt * 0.1)//0.05
                    {
                        pm.normalizedIntensity = 0;
                        pm.obsIntensity        = 0;
                    }
                }
            }

            return(products);
        }
示例#7
0
        }//*/

        /*
         * private static IEnumerable<double> fragmentsTypesCTerm(double cTermCumul)
         * {
         *  yield return cTermCumul + Constants.OXYGEN_MASS * 2 + Constants.HYDROGEN_MASS + Constants.CARBON_MASS;//X
         *  yield return cTermCumul + Constants.OXYGEN_MASS + Constants.HYDROGEN_MASS * 3;//Y
         *  yield return cTermCumul + Constants.OXYGEN_MASS + Constants.HYDROGEN_MASS - Constants.NITROGEN_MASS - Constants.HYDROGEN_MASS;//Z
         * }    //*/
        public static IEnumerable <ProductMatch> ComputeAllFragments(Peptide peptide, int precursorKnownCharge, DBOptions dbOptions)
        {
            int maxCharge;

            if (precursorKnownCharge > 1)
            {
                maxCharge = precursorKnownCharge - 1;
            }
            else
            {
                maxCharge = 1;
            }
            FragmentA fragNTerm = new FragmentA();
            FragmentX fragCTerm = new FragmentX();

            double[]     masses = peptide.GetMasses();
            ProductMatch match  = new ProductMatch();

            match.weight = 1;
            double cumulN = 0.0; // Constants.WATER_MONOISOTOPIC_MASS;
            double cumulC = 0.0; // Constants.WATER_MONOISOTOPIC_MASS;

            for (int r = 0; r < masses.Length; r++)
            {
                cumulN += masses[r];
                cumulC += masses[masses.Length - r - 1];

                foreach (double product_mass in fragmentsTypesCTerm(cumulC))
                {
                    match.fragmentPos = masses.Length - r;
                    match.Fragment    = fragCTerm;
                    for (int c = maxCharge; c > 0; c--)
                    {
                        match.theoMz = Numerics.MZFromMass(product_mass, c);
                        match.charge = c;
                        yield return(match);
                        //match.theoMz = Numerics.MZFromMass(product_mass - Constants.WATER_MONOISOTOPIC_MASS, c);
                        //yield return match;
                    }
                }

                foreach (double product_mass in fragmentsTypesNTerm(cumulN))
                {
                    match.fragmentPos = r + 1;
                    match.Fragment    = fragNTerm;
                    for (int c = maxCharge; c > 0; c--)
                    {
                        match.theoMz = Numerics.MZFromMass(product_mass, c);
                        match.charge = c;
                        yield return(match);

                        match.theoMz = Numerics.MZFromMass(product_mass - Constants.WATER_MONOISOTOPIC_MASS, c);
                        yield return(match);

                        match.theoMz = Numerics.MZFromMass(product_mass - Constants.AMONIA_MASS, c);
                        yield return(match);
                    }
                }
            }
            //Single charge immonium ions
            //Water Loss
            //Amonia Loss
            //Internal fragments
            //Fragment types
        }
示例#8
0
        public IEnumerable <ProductMatch> ComputeFragments(Peptide peptide, int precursorKnownCharge, DBOptions dbOptions)
        {
            int maxCharge;

            if (precursorKnownCharge > 1)
            {
                maxCharge = precursorKnownCharge - 1;
            }
            else
            {
                maxCharge = precursorKnownCharge;
            }

            string       sequence = peptide.BaseSequence;
            ProductMatch match    = new ProductMatch();

            double cumulativeNTerminalMass = 0; // 1.007276 + 15.9949;//TODO Add NTerminal modifications here
            double cumulativeCTerminalMass = 0; // 1.007276;//TODO Add CTerminal modifications here
            string cumulSeq    = "";
            string cumulRevSeq = "";

            for (int r = 1; r <= sequence.Length; r++)
            {
                match.fragmentPos = r;
                //Peptide mods, computed during digestion
                //TODO Only search for modifications that were seen in the spectrum
                double tmp = 0;
                cumulSeq += sequence[r - 1];
                if (peptide.FixedModifications != null && peptide.FixedModifications.ContainsKey(r + 1))
                {
                    foreach (Modification mod in peptide.FixedModifications[r + 1])
                    {
                        tmp += mod.MonoisotopicMassShift;
                    }
                }
                if (peptide.VariableModifications != null && peptide.VariableModifications.ContainsKey(r + 1))
                {
                    tmp += peptide.VariableModifications[r + 1].MonoisotopicMassShift;
                }
                cumulativeNTerminalMass += AminoAcidMasses.GetMonoisotopicMass(sequence[r - 1]) + tmp;

                tmp          = 0;
                cumulRevSeq += sequence[sequence.Length - r];
                if (peptide.FixedModifications != null && peptide.FixedModifications.ContainsKey(sequence.Length + 2 - r))
                {
                    foreach (Modification mod in peptide.FixedModifications[sequence.Length + 2 - r])
                    {
                        tmp += mod.MonoisotopicMassShift;
                    }
                }
                if (peptide.VariableModifications != null && peptide.VariableModifications.ContainsKey(sequence.Length + 2 - r))
                {
                    tmp += peptide.VariableModifications[sequence.Length + 2 - r].MonoisotopicMassShift;
                }
                cumulativeCTerminalMass += AminoAcidMasses.GetMonoisotopicMass(sequence[sequence.Length - r]) + tmp;

                for (int c = maxCharge; c > 0; c--)
                {
                    match.charge = c;
                    foreach (FragmentClass fragment in this)
                    {
                        if (fragment.IsReverse)
                        {
                            match.fragmentPos = 1 + sequence.Length - r;
                        }
                        else
                        {
                            match.fragmentPos = r;
                        }
                        match.Fragment = fragment;

                        foreach (double product_mass in fragment.ComputeFragment(cumulativeCTerminalMass, cumulativeNTerminalMass))
                        {
                            match.weight = fragment.Distribution;//TODO Adjust this value by computing overall impact (times 10?)
                            //ModLess
                            match.theoMz = Numerics.MZFromMass(product_mass, c);
                            yield return(match);

                            //Added mods
                            if (dbOptions.addFragmentMods)
                            {
                                foreach (Modification mod in FragmentDictionary.Fragments.Values)
                                {
                                    if ((fragment.IsReverse ? cumulRevSeq : cumulSeq).Contains(mod.AminoAcid))
                                    {
                                        //!!!!match.Fragment = mod;
                                        match.weight = fragment.Distribution * mod.Probability;
                                        match.theoMz = Numerics.MZFromMass(product_mass + mod.MonoisotopicMassShift, c);
                                        yield return(match);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (dbOptions.addFragmentLoss)
            {
                match.charge   = 1;
                match.Fragment = FragLossObject;
                foreach (Modification mod in FragmentDictionary.AAFragments.Values)
                {
                    if (peptide.BaseSequence.Contains(mod.AminoAcid))
                    {
                        match.fragmentPos = peptide.BaseSequence.IndexOf(mod.AminoAcid);
                        match.theoMz      = mod.MonoisotopicMassShift;// Numerics.MZFromMass(mod.MonoisotopicMassShift, 1);
                        match.weight      = mod.Probability;
                        yield return(match);
                    }
                }
            }
        }