private MzmlMzSpectrum CreateSpectrum(ChemicalFormula f, double lowerBound, double upperBound, int minCharge) { IsotopicDistribution isodist = IsotopicDistribution.GetDistribution(f, 0.1, 0.001); MzmlMzSpectrum notActuallyMzS = new MzmlMzSpectrum(isodist.Masses.ToArray(), isodist.Intensities.ToArray(), false); notActuallyMzS.ReplaceXbyApplyingFunction(b => b.X.ToMz(1)); List <double> allMasses = new List <double>(); List <double> allIntensitiess = new List <double>(); while (notActuallyMzS.FirstX > lowerBound) { foreach (var thisPeak in notActuallyMzS) { if (thisPeak.Mz > lowerBound && thisPeak.Mz < upperBound) { allMasses.Add(thisPeak.Mz); allIntensitiess.Add(thisPeak.Intensity); } } minCharge += 1; notActuallyMzS = new MzmlMzSpectrum(isodist.Masses.ToArray(), isodist.Intensities.ToArray(), false); notActuallyMzS.ReplaceXbyApplyingFunction(s => s.X.ToMz(minCharge)); } var allMassesArray = allMasses.ToArray(); var allIntensitiessArray = allIntensitiess.ToArray(); Array.Sort(allMassesArray, allIntensitiessArray); return(new MzmlMzSpectrum(allMassesArray, allIntensitiessArray, false)); }
/// <summary> /// the observed most abundance mass is what the map will be created from. This comes from the spectrum. The most /// abundant mass, etc comes from the values generated by mercury. /// </summary> /// <param name="observedMostAbundantMass"></param> /// <param name="mostAbundantMass"></param> /// <param name="monoMass"></param> /// <param name="averageMass"></param> /// <param name="mostAbundantMz"></param> /// <param name="charge"></param> /// <param name="fwhm"></param> /// <param name="massVariance"></param> /// <param name="numPtsPerAmu"></param> /// <param name="minIntensity"></param> /// <param name="mzs">Isotope m/zs</param> /// <param name="intensities">Isotope intensities</param> public void CacheIsotopeDistribution(double observedMostAbundantMass, double mostAbundantMass, double monoMass, double averageMass, double mostAbundantMz, int charge, double fwhm, double massVariance, int numPtsPerAmu, double minIntensity, List <double> mzs, List <double> intensities) { // we're going to extract the peak tops for this distribution and add them to the array of distributions. var position = _isotopicDistributions.Count; var key = (int)(observedMostAbundantMass + 0.5); if (_cachedIsotopeDistValues.ContainsKey(key)) { _cachedIsotopeDistValues[key].Add(position); } else { _cachedIsotopeDistValues.Add(key, new List <int> { position }); } //var amuPerValue = 1.0 / num_pts_per_amu; var dist = new IsotopicDistribution { Charge = charge, MostAbundantMass = mostAbundantMass, MonoMass = monoMass, AverageMass = averageMass, MassVariance = massVariance, MostAbundantMz = mostAbundantMz, NumIsotopes = mzs.Count }; dist.Mzs.AddRange(mzs); dist.Intensities.AddRange(intensities); _isotopicDistributions.Add(dist); }
private MzSpectrum CreateSpectrum(ChemicalFormula f, double lowerBound, double upperBound, int minCharge) { IsotopicDistribution isodist = IsotopicDistribution.GetDistribution(f, 0.1, 0.001); MzSpectrum notActuallyMzS = new MzSpectrum(isodist.Masses.ToArray(), isodist.Intensities.ToArray(), false); notActuallyMzS.ReplaceXbyApplyingFunction(b => b.Mz.ToMz(1)); List <double> allMasses = new List <double>(); List <double> allIntensitiess = new List <double>(); while (notActuallyMzS.FirstX > lowerBound) { for (int i = 0; i < notActuallyMzS.Size; i++) { if (notActuallyMzS.XArray[i] > lowerBound && notActuallyMzS.XArray[i] < upperBound) { allMasses.Add(notActuallyMzS.XArray[i]); allIntensitiess.Add(notActuallyMzS.YArray[i]); } } minCharge += 1; notActuallyMzS = new MzSpectrum(isodist.Masses.ToArray(), isodist.Intensities.ToArray(), false); notActuallyMzS.ReplaceXbyApplyingFunction(s => s.Mz.ToMz(minCharge)); } var allMassesArray = allMasses.ToArray(); var allIntensitiessArray = allIntensitiess.ToArray(); Array.Sort(allMassesArray, allIntensitiessArray); return(new MzSpectrum(allMassesArray, allIntensitiessArray, false)); }
private MzmlMzSpectrum CreateSpectrum(ChemicalFormula f, double lowerBound, double upperBound, int minCharge) { IsotopicDistribution isodist = IsotopicDistribution.GetDistribution(f, 0.1); return(new MzmlMzSpectrum(isodist.Masses.ToArray(), isodist.Intensities.ToArray(), false)); //massSpectrum1 = massSpectrum1.FilterByNumberOfMostIntense(5); //var chargeToLookAt = minCharge; //var correctedSpectrum = massSpectrum1.NewSpectrumApplyFunctionToX(s => s.ToMz(chargeToLookAt)); //List<double> allMasses = new List<double>(); //List<double> allIntensitiess = new List<double>(); //while (correctedSpectrum.FirstX > lowerBound) //{ // foreach (var thisPeak in correctedSpectrum) // { // if (thisPeak.Mz > lowerBound && thisPeak.Mz < upperBound) // { // allMasses.Add(thisPeak.Mz); // allIntensitiess.Add(thisPeak.Intensity); // } // } // chargeToLookAt += 1; // correctedSpectrum = massSpectrum1.NewSpectrumApplyFunctionToX(s => s.ToMz(chargeToLookAt)); //} //var allMassesArray = allMasses.ToArray(); //var allIntensitiessArray = allIntensitiess.ToArray(); //Array.Sort(allMassesArray, allIntensitiessArray); //return new MzmlMzSpectrum(allMassesArray, allIntensitiessArray, false); }
private DefaultMzSpectrum createSpectrum(ChemicalFormula f, double lowerBound, double upperBound, int minCharge) { IsotopicDistribution isodist = new IsotopicDistribution(f, 0.1); IMzSpectrum <MzPeak> massSpectrum1 = new DefaultMzSpectrum(isodist.Masses.ToArray(), isodist.Intensities.ToArray(), false); massSpectrum1 = massSpectrum1.newSpectrumFilterByNumberOfMostIntense(5); var chargeToLookAt = minCharge; var correctedSpectrum = massSpectrum1.newSpectrumApplyFunctionToX(s => s.ToMassToChargeRatio(chargeToLookAt)); List <double> allMasses = new List <double>(); List <double> allIntensitiess = new List <double>(); while (correctedSpectrum.FirstX > lowerBound) { foreach (var thisPeak in correctedSpectrum) { if (thisPeak.MZ > lowerBound && thisPeak.MZ < upperBound) { allMasses.Add(thisPeak.MZ); allIntensitiess.Add(thisPeak.Intensity); } } chargeToLookAt += 1; correctedSpectrum = massSpectrum1.newSpectrumApplyFunctionToX(s => s.ToMassToChargeRatio(chargeToLookAt)); } var allMassesArray = allMasses.ToArray(); var allIntensitiessArray = allIntensitiess.ToArray(); Array.Sort(allMassesArray, allIntensitiessArray); return(new DefaultMzSpectrum(allMassesArray, allIntensitiessArray, false)); }
public void TestInitialize() { hexnac_form = new ChemicalFormula("C8H13N1O5"); // Testing data from brainpy double[] mass = new double[] { 203.079373, 204.082545, 205.084190, 206.086971 }; double[] intensity = new double[] { 0.901867, 0.084396, 0.012787, 0.000950 }; hexnac_ref = new IsotopicDistribution(mass, intensity); // Testing data generated with mMass ca_form = new ChemicalFormula("C1290H1970N354O394S4"); mass = new double[] { 28856.3995, 28857.40232, 28858.40491, 28859.40789, 28860.41027, 28861.41338, 28862.41566, 28863.41873, 28864.42102, 28865.42396, 28866.42631, 28867.42913, 28868.43154, 28869.43426, 28870.43671, 28871.43936, 28872.44183, 28873.44442, 28874.44689, 28875.44944, 28876.4519, 28877.45443, 28878.45685, 28879.45936, 28880.46169, 28881.4642, 28882.46629, 28883.4688, 28884.47046, 28885.47184, 28886.47026 }; intensity = new double[] { 0.000166925, 0.000537912, 0.00145733, 0.003399197, 0.006991671, 0.012854012, 0.021405595, 0.032612538, 0.045785523, 0.05975158, 0.072715668, 0.083213348, 0.089580389, 0.091474523, 0.088461882, 0.081698453, 0.071838131, 0.060647866, 0.04896059, 0.038109724, 0.028467622, 0.020570442, 0.014305486, 0.009644266, 0.006268659, 0.003949935, 0.002398549, 0.001408038, 0.000795686, 0.000406887, 0.00012157 }; ca_ref = new IsotopicDistribution(mass, intensity); }
private static void OutputMassesAndIntensities(IsotopicDistribution ye, double fineRes, int lowestIndex, int highestIndex) { Console.WriteLine(string.Join(", ", ye.Masses)); Console.WriteLine(string.Join(",", ye.Intensities)); for (int k = lowestIndex; k < highestIndex; k++) { Console.Write(ye.Masses[k] - fineRes / 2); Console.Write(","); Console.Write(ye.Masses[k]); Console.Write(","); Console.Write(ye.Masses[k] + fineRes / 2); Console.Write(","); } Console.WriteLine(); for (int k = lowestIndex; k < highestIndex; k++) { Console.Write(-1); Console.Write(","); Console.Write(ye.Intensities[k]); Console.Write(","); Console.Write(-1); Console.Write(","); } Console.WriteLine(); }
public static void TestIsotopicDistribution() { ChemicalFormula formulaA = ChemicalFormula.ParseFormula("C2H3NO"); var a = IsotopicDistribution.GetDistribution(formulaA); Assert.True(Math.Abs(formulaA.MonoisotopicMass - a.Masses.ToArray()[Array.IndexOf(a.Intensities.ToArray(), a.Intensities.Max())]) < 1e-9); }
public void TestIsotopicDistribution() { ChemicalFormula formulaA = new ChemicalFormula("C2H3NO"); var a = new IsotopicDistribution(formulaA); Assert.True(formulaA.MonoisotopicMass.MassEquals(a.Masses[a.Intensities.IndexOf(a.Intensities.Max())])); }
public static void TestCoIsolation() { List <DigestionMotif> motifs = new List <DigestionMotif> { new DigestionMotif("K", null, 1, null) }; Protease protease = new Protease("CustProtease", CleavageSpecificity.Full, null, null, motifs); ProteaseDictionary.Dictionary.Add(protease.Name, protease); CommonParameters CommonParameters = new CommonParameters(scoreCutoff: 1, deconvolutionIntensityRatio: 50, digestionParams: new DigestionParams(protease.Name, minPeptideLength: 1)); var variableModifications = new List <Modification>(); var fixedModifications = new List <Modification>(); var proteinList = new List <Protein> { new Protein("MNNNKNDNK", null) }; var searchModes = new SinglePpmAroundZeroSearchMode(5); Proteomics.AminoAcidPolymer.Peptide pep1 = new Proteomics.AminoAcidPolymer.Peptide("NNNK"); Proteomics.AminoAcidPolymer.Peptide pep2 = new Proteomics.AminoAcidPolymer.Peptide("NDNK"); var dist1 = IsotopicDistribution.GetDistribution(pep1.GetChemicalFormula(), 0.1, 0.01); var dist2 = IsotopicDistribution.GetDistribution(pep2.GetChemicalFormula(), 0.1, 0.01); MsDataScan[] Scans = new MsDataScan[2]; double[] ms1intensities = new double[] { 0.8, 0.8, 0.2, 0.02, 0.2, 0.02 }; double[] ms1mzs = dist1.Masses.Concat(dist2.Masses).OrderBy(b => b).Select(b => b.ToMz(1)).ToArray(); double selectedIonMz = ms1mzs[1]; MzSpectrum MS1 = new MzSpectrum(ms1mzs, ms1intensities, false); Scans[0] = new MsDataScan(MS1, 1, 1, false, Polarity.Positive, 1.0, new MzRange(300, 2000), "first spectrum", MZAnalyzerType.Unknown, MS1.SumOfAllY, null, null, "scan=1"); double[] ms2intensities = new double[] { 1, 1, 1, 1, 1 }; double[] ms2mzs = new double[] { 146.106.ToMz(1), 228.086.ToMz(1), 229.07.ToMz(1), 260.148.ToMz(1), 342.129.ToMz(1) }; MzSpectrum MS2 = new MzSpectrum(ms2mzs, ms2intensities, false); double isolationMZ = selectedIonMz; Scans[1] = new MsDataScan(MS2, 2, 2, false, Polarity.Positive, 2.0, new MzRange(100, 1500), "second spectrum", MZAnalyzerType.Unknown, MS2.SumOfAllY, null, null, "scan=2", selectedIonMz, null, null, isolationMZ, 2.5, DissociationType.HCD, 1, null); var myMsDataFile = new MsDataFile(Scans, null); var listOfSortedms2Scans = MetaMorpheusTask.GetMs2Scans(myMsDataFile, null, new CommonParameters(deconvolutionIntensityRatio: 50)).OrderBy(b => b.PrecursorMass).ToArray(); PeptideSpectralMatch[] allPsmsArray = new PeptideSpectralMatch[listOfSortedms2Scans.Length];; new ClassicSearchEngine(allPsmsArray, listOfSortedms2Scans, variableModifications, fixedModifications, null, null, null, proteinList, searchModes, CommonParameters, new List <string>()).Run(); // Two matches for this single scan! Corresponding to two co-isolated masses Assert.AreEqual(2, allPsmsArray.Length); Assert.IsTrue(allPsmsArray[0].Score > 1); Assert.AreEqual(2, allPsmsArray[0].ScanNumber); Assert.AreEqual("NNNK", allPsmsArray[0].BaseSequence); Assert.AreEqual("NDNK", allPsmsArray[1].BaseSequence); }
public void Valid_Data_Generates_Valid_Dist() { double[] mass = new double[] { 203.079373, 204.082545, 205.084190, 206.086971 }; double[] intensity = new double[] { 0.901867, 0.084396, 0.012787, 0.000950 }; IsotopicDistribution dist = new IsotopicDistribution(mass, intensity); Assert.AreEqual(4, dist.Length); }
public void Invalid_Data_Generates_Error() { double[] mass = new double[] { 203.079373, 204.082545, 205.084190, 206.086971 }; double[] intensity = new double[] { 0.901867, 0.084396, 0.012787 }; IsotopicDistribution dist; Assert.ThrowsException <ArgumentException>(() => dist = new IsotopicDistribution(mass, intensity)); }
public static void TestPeakSplittingRight() { string fileToWrite = "myMzml.mzML"; string peptide = "PEPTIDE"; double intensity = 1e6; Loaders.LoadElements(Path.Combine(TestContext.CurrentContext.TestDirectory, @"elements.dat")); // generate mzml file // 1 MS1 scan per peptide MsDataScan[] scans = new MsDataScan[10]; double[] intensityMultipliers = { 1, 3, 5, 10, 5, 3, 1, 1, 3, 1 }; for (int s = 0; s < scans.Length; s++) { ChemicalFormula cf = new Proteomics.AminoAcidPolymer.Peptide(peptide).GetChemicalFormula(); IsotopicDistribution dist = IsotopicDistribution.GetDistribution(cf, 0.125, 1e-8); double[] mz = dist.Masses.Select(v => v.ToMz(1)).ToArray(); double[] intensities = dist.Intensities.Select(v => v * intensity * intensityMultipliers[s]).ToArray(); // add the scan scans[s] = new MsDataScan(massSpectrum: new MzSpectrum(mz, intensities, false), oneBasedScanNumber: s + 1, msnOrder: 1, isCentroid: true, polarity: Polarity.Positive, retentionTime: 1.0 + s / 10.0, scanWindowRange: new MzRange(400, 1600), scanFilter: "f", mzAnalyzer: MZAnalyzerType.Orbitrap, totalIonCurrent: intensities.Sum(), injectionTime: 1.0, noiseData: null, nativeId: "scan=" + (s + 1)); } // write the .mzML IO.MzML.MzmlMethods.CreateAndWriteMyMzmlWithCalibratedSpectra(new FakeMsDataFile(scans), Path.Combine(TestContext.CurrentContext.TestDirectory, fileToWrite), false); // set up spectra file info SpectraFileInfo file1 = new SpectraFileInfo(Path.Combine(TestContext.CurrentContext.TestDirectory, fileToWrite), "", 0, 0, 0); // create some PSMs var pg = new ProteinGroup("MyProtein", "gene", "org"); Identification id1 = new Identification(file1, peptide, peptide, new Proteomics.AminoAcidPolymer.Peptide(peptide).MonoisotopicMass, 1.3 + 0.001, 1, new List <ProteinGroup> { pg }); // create the FlashLFQ engine FlashLFQEngine engine = new FlashLFQEngine(new List <Identification> { id1 }); // run the engine var results = engine.Run(); ChromatographicPeak peak = results.Peaks.First().Value.First(); Assert.That(peak.Apex.RetentionTime == 1.3); Assert.That(peak.SplitRT == 1.6); Assert.That(!peak.IsotopicEnvelopes.Any(p => p.RetentionTime > 1.6)); Assert.That(peak.IsotopicEnvelopes.Count == 6); }
public static void I0j1() { ChemicalFormula formula = (ChemicalFormula.ParseFormula("C50O50")); IsotopicDistribution.GetDistribution(formula, 0.01, 0.1); IsotopicDistribution.GetDistribution(formula, 0.01, 0.5); IsotopicDistribution.GetDistribution(formula, 0.01, 0.75); }
public static void ThresholdProbability() { ChemicalFormula formulaA = ChemicalFormula.ParseFormula("CO"); // Only the principal isotopes have joint probability of 0.5! So one result when calcuating isotopic distribution var a = IsotopicDistribution.GetDistribution(formulaA, 0.0001, 0.5); Assert.AreEqual(1, a.Masses.Count()); Assert.IsTrue(Math.Abs((PeriodicTable.GetElement("C").PrincipalIsotope.AtomicMass + PeriodicTable.GetElement("O").PrincipalIsotope.AtomicMass - a.Masses.First())) < 1e-9); }
public void ThresholdProbability() { ChemicalFormula formulaA = new ChemicalFormula("CO"); // Only the principal isotopes have joint probability of 0.5! So one result when calcuating isotopic distribution var a = new IsotopicDistribution(formulaA, 0.0001, 0.5); Assert.AreEqual(1, a.Masses.Count()); Assert.IsTrue((PeriodicTable.GetElement("C").PrincipalIsotope.AtomicMass + PeriodicTable.GetElement("O").PrincipalIsotope.AtomicMass).MassEquals(a.Masses[0])); }
public void IsoTest() { ChemicalFormula formula = new ChemicalFormula("C5H8NO"); IsotopicDistribution d = new IsotopicDistribution(formula, Math.Pow(2, -20)); Assert.AreEqual(324, d.Intensities.Count); d = new IsotopicDistribution(formula, Math.Pow(2, -1)); Assert.AreEqual(17, d.Intensities.Count); }
public static void IsoTest() { ChemicalFormula formula = ChemicalFormula.ParseFormula("C5H8NO"); IsotopicDistribution d = IsotopicDistribution.GetDistribution(formula, Math.Pow(2, -14)); Assert.AreEqual(324, d.Intensities.Count()); d = IsotopicDistribution.GetDistribution(formula, Math.Pow(2, -1)); Assert.AreEqual(17, d.Intensities.Count()); }
public void Mercury_HexNAc() { IIsotopeDistGenerator gen = new Mercury7(); IsotopicDistribution hexnac_mercury = gen.GenerateIsotopicDistribution(hexnac_form); for (int i = 0; i < hexnac_ref.Length; i++) { Assert.AreEqual(hexnac_ref.Masses[i], hexnac_mercury.Masses[i], 0.0001); Assert.AreEqual(hexnac_ref.Intensities[i], hexnac_mercury.Intensities[i], 0.0001); } }
public void Mercury_Carbonic() { IIsotopeDistGenerator gen = new Mercury7(); IsotopicDistribution ca_mercury = gen.GenerateIsotopicDistribution(ca_form); for (int i = 0; i < ca_ref.Length; i++) { // mMass doesn't calculate the intensity of the monoisotopic mass peak so need to offset by 4 Assert.AreEqual(ca_ref.Masses[i], ca_mercury.Masses[i + 4], 0.01); Assert.AreEqual(ca_ref.Intensities[i], ca_mercury.Intensities[i + 4], 0.01); } }
/// <summary> /// /// </summary> /// <param name="argCompond"></param> /// <param name="argTheoreticalMonoIdx"></param> /// <param name="argIntensities"></param> /// <returns></returns> public static double IntensityNormalizationFactorByIsotope(COL.GlycoLib.GlycanCompound argCompond, int argNumOfLabelingSite, double[] argIntensities, float argPurity) { ChemicalFormula MonoChemFormula = new ChemicalFormula(); MonoChemFormula.Add("C", argCompond.Carbon + argCompond.Carbon13); MonoChemFormula.Add("H", argCompond.Hydrogen + argCompond.Deuterium); MonoChemFormula.Add("O", argCompond.Oxygen); //if (argCompond.Carbon13 != 0) //{ // MonoChemFormula.Add("C{13}", argCompond.Carbon13); //} //if (argCompond.Deuterium != 0) //{ // MonoChemFormula.Add("D", argCompond.Deuterium); //} if (argCompond.Sodium != 0) { MonoChemFormula.Add("Na", argCompond.Sodium); } if (argCompond.Nitrogen != 0) { MonoChemFormula.Add("N", argCompond.Nitrogen); } IsotopicDistribution ID = new IsotopicDistribution(); MZPeak[] Peaks = ID.CalculateDistribuition(MonoChemFormula, 7, IsotopicDistribution.Normalization.BasePeak).GetPeaks().ToArray(); double[] isotopeRatio = new double[7]; for (int i = 0; i < 7; i++) { isotopeRatio[i] = Peaks[i].Intensity; } double[] CorrectedIntensities = (double[] )argIntensities.Clone(); //Isotope Correction for (int i = 0; i <= 2; i++) { double Ratio = CorrectedIntensities[i] / isotopeRatio[0]; for (int j = i; j < 7; j++) { CorrectedIntensities[j] = CorrectedIntensities[j] - (isotopeRatio[j - i] * Ratio); } } double isotopeCorrectionFactor = CorrectedIntensities[3] / argIntensities[3]; // Purity Correction //double PurityCorrection = Math.Pow(argPurity, argNumOfLabelingSite); return(isotopeCorrectionFactor); }
private IsotopicDistribution GetDistribution(IChemicalFormula formula, double fineResolution, double minProbability, double molecularWeightResolution) { var a = GetNewFineAndMergeResolutions(fineResolution); fineResolution = a.Item1; double _mergeFineResolution = a.Item2; var elementalComposition = new List <List <Composition> >(); // Get all the unique elements that might have isotopes foreach (var elementAndCount in formula.GetElements()) { int count = elementAndCount.Count; var isotopeComposition = new List <Composition>(); foreach (Isotope isotope in elementAndCount.Entity.Isotopes.OrderBy(iso => iso.AtomicMass)) { var c = new Composition { Atoms = count, MolecularWeight = isotope.AtomicMass, Power = isotope.AtomicMass, Probability = isotope.RelativeAbundance }; isotopeComposition.Add(c); } elementalComposition.Add(isotopeComposition); } foreach (List <Composition> compositions in elementalComposition) { double sumProb = compositions.Sum(t => t.Probability); foreach (Composition composition in compositions) { composition.Probability /= sumProb; composition.LogProbability = Math.Log(composition.Probability); composition.Power = Math.Floor(composition.MolecularWeight / molecularWeightResolution + 0.5); } } IsotopicDistribution dist = CalculateFineGrain(elementalComposition, molecularWeightResolution, _mergeFineResolution, fineResolution, minProbability); // RTF: Not handling isotopes right now //double additionalMass = 0; //foreach (var isotopeAndCount in formula.Isotopes) // additionalMass += isotopeAndCount.Key.AtomicMass * isotopeAndCount.Value; //for (int i = 0; i < dist.masses.Length; i++) // dist.masses[i] += additionalMass; return(dist); }
public static double CalculateProteoformMass(string sequence, List <Ptm> ptm_combination) { if (Sweet.lollipop.theoretical_database.aaIsotopeMassList == null) { Sweet.lollipop.theoretical_database.populate_aa_mass_dictionary(); } if (!Sweet.lollipop.most_abundant_mass) { double proteoformMass = 18.010565; // start with water char[] aminoAcids = sequence.ToCharArray(); List <double> aaMasses = new List <double>(); for (int i = 0; i < sequence.Length; i++) { if (Sweet.lollipop.theoretical_database.aaIsotopeMassList.ContainsKey(aminoAcids[i])) { aaMasses.Add(Sweet.lollipop.theoretical_database.aaIsotopeMassList[aminoAcids[i]]); } } return(proteoformMass + aaMasses.Sum() + ptm_combination.Sum(p => (double)p.modification.MonoisotopicMass)); } //if most abundant mass, calculate iso distrubution, set modified/unmodified masses to most abundant. var formula = new Proteomics.AminoAcidPolymer.Peptide(sequence).GetChemicalFormula(); // append mod formulas foreach (var mod in ptm_combination) { var modCf = mod.modification.ChemicalFormula; if (modCf != null) { formula.Add(modCf); } } // Calculate isotopic distribution of the full peptide var dist = IsotopicDistribution.GetDistribution(formula, 0.1, 1e-12); double[] masses = dist.Masses.ToArray(); double[] intensities = dist.Intensities.ToArray(); double max = intensities.Max(); int modeMassIndex = Array.IndexOf(intensities, max); return(masses[modeMassIndex]); }
public void TestCoIsolation() { Peptide pep1 = new Peptide("AAAAAA"); Peptide pep2 = new Peptide("AAA[H]AAA"); var dist1 = IsotopicDistribution.GetDistribution(pep1.GetChemicalFormula(), 0.1, 0.01); var dist2 = IsotopicDistribution.GetDistribution(pep2.GetChemicalFormula(), 0.1, 0.01); MsDataScan[] Scans = new MsDataScan[2]; double[] ms1intensities = new double[] { 0.8, 0.8, 0.2, 0.02, 0.2, 0.02 }; double[] ms1mzs = dist1.Masses.Concat(dist2.Masses).OrderBy(b => b).Select(b => b.ToMz(1)).ToArray(); double selectedIonMz = ms1mzs[1]; MzSpectrum MS1 = new MzSpectrum(ms1mzs, ms1intensities, false); Scans[0] = new MsDataScan(MS1, 1, 1, false, Polarity.Positive, 1.0, new MzRange(300, 2000), "first spectrum", MZAnalyzerType.Unknown, MS1.SumOfAllY, null, null, null); // Horrible fragmentation, but we don't care about this! double[] ms2intensities = new double[] { 1000 }; double[] ms2mzs = new double[] { 1000 }; MzSpectrum MS2 = new MzSpectrum(ms2mzs, ms2intensities, false); double isolationMZ = selectedIonMz; Scans[1] = new MsDataScan(MS2, 2, 2, false, Polarity.Positive, 2.0, new MzRange(100, 1500), "second spectrum", MZAnalyzerType.Unknown, MS2.SumOfAllY, null, null, null, selectedIonMz, null, null, isolationMZ, 2.5, DissociationType.HCD, 1, null); var myMsDataFile = new FakeMsDataFile(Scans); var cool = myMsDataFile.GetAllScansList().Last(); int maxAssumedChargeState = 1; Tolerance massTolerance = Tolerance.ParseToleranceString("10 PPM"); var isolatedMasses = cool.GetIsolatedMassesAndCharges(myMsDataFile.GetOneBasedScan(cool.OneBasedPrecursorScanNumber.Value).MassSpectrum, 1, maxAssumedChargeState, 10, 5).ToList(); Assert.AreEqual(2, isolatedMasses.Count); Assert.AreEqual(2, isolatedMasses.Count(b => b.charge == 1)); Assert.AreEqual(pep1.MonoisotopicMass, isolatedMasses.Select(b => b.peaks.First().Item1.ToMass(b.charge)).Min(), 1e-9); Assert.AreEqual(pep2.MonoisotopicMass, isolatedMasses.Select(b => b.peaks.First().Item1.ToMass(b.charge)).Max(), 1e-9); Assert.AreEqual(pep1.MonoisotopicMass, isolatedMasses.Select(b => b.monoisotopicMass.ToMz(b.charge).ToMass(b.charge)).Min(), 1e-9); Assert.AreEqual(pep2.MonoisotopicMass, isolatedMasses.Select(b => b.monoisotopicMass.ToMz(b.charge).ToMass(b.charge)).Max(), 1e-9); }
private static void BenchmarkIsotopicDistribution() { Console.WriteLine("Starting benchmark BenchmarkIsotopicDistribution"); int numRepetitions = 100; Stopwatch stopWatch = new Stopwatch(); var a = ChemicalFormula.ParseFormula("H100C100N100O100S100"); double b = 0; stopWatch.Restart(); for (int i = 0; i < numRepetitions; i++) { b += IsotopicDistribution.GetDistribution(a).Intensities.First(); } stopWatch.Stop(); Console.WriteLine("Time for generating isotopic distributions: " + stopWatch.Elapsed + " a = " + a); Console.WriteLine("Benchmark BenchmarkIsotopicDistribution finished"); }
static IsoDecon() { //AVERAGINE const double averageC = 4.9384; const double averageH = 7.7583; const double averageO = 1.4773; const double averageN = 1.3577; const double averageS = 0.0417; const double fineRes = 0.125; const double minRes = 1e-8; for (int i = 0; i < numAveraginesToGenerate; i++) { double averagineMultiplier = (i + 1) / 2.0; //Console.Write("numAveragines = " + numAveragines); ChemicalFormula chemicalFormula = new ChemicalFormula(); chemicalFormula.Add("C", Convert.ToInt32(averageC * averagineMultiplier)); chemicalFormula.Add("H", Convert.ToInt32(averageH * averagineMultiplier)); chemicalFormula.Add("O", Convert.ToInt32(averageO * averagineMultiplier)); chemicalFormula.Add("N", Convert.ToInt32(averageN * averagineMultiplier)); chemicalFormula.Add("S", Convert.ToInt32(averageS * averagineMultiplier)); { var chemicalFormulaReg = chemicalFormula; IsotopicDistribution ye = IsotopicDistribution.GetDistribution(chemicalFormulaReg, fineRes, minRes); var masses = ye.Masses.ToArray(); var intensities = ye.Intensities.ToArray(); Array.Sort(intensities, masses); Array.Reverse(intensities); Array.Reverse(masses); mostIntenseMasses[i] = masses[0]; diffToMonoisotopic[i] = masses[0] - chemicalFormulaReg.MonoisotopicMass; allMasses[i] = masses; allIntensities[i] = intensities; } } }
public void TestIsotopicDistribution3() { ChemicalFormula formulaA = new ChemicalFormula("CO"); // Distinguish between O and C isotope masses var a = new IsotopicDistribution(formulaA, 0.0001); Assert.AreEqual(6, a.Masses.Count()); // Do not distinguish between O and C isotope masses new IsotopicDistribution(formulaA, 0.001); // Do not distinguish between O and C isotope masses var b = new IsotopicDistribution(formulaA); Assert.AreEqual(4, b.Masses.Count()); new IsotopicDistribution(formulaA, 0.1); PhysicalObjectWithChemicalFormula formulaB = new PhysicalObjectWithChemicalFormula("CO"); new IsotopicDistribution(formulaB.ThisChemicalFormula, 1); }
static void Main(string[] args) { UsefulProteomicsDatabases.Loaders.LoadElements("elements.dat"); var tol = 0.01; var Compound1 = new Peptide("NNNNN"); var regularMZ = new IsotopicDistribution(Compound1.GetChemicalFormula(), 0.1, 0.001).Masses.Select(b => b.ToMassToChargeRatio(1)).ToList(); Console.WriteLine("NNNNN mz: " + string.Join(", ", regularMZ)); var withAmmoniaLoss = Compound1.GetChemicalFormula(); withAmmoniaLoss.Add(new ChemicalFormula("N-1H-2")); var withAmmoniaLossMZ = new IsotopicDistribution(withAmmoniaLoss, 0.1, 0.001).Masses.Select(b => b.ToMassToChargeRatio(1)).ToList(); Console.WriteLine("withAmmoniaLoss mz: " + string.Join(", ", withAmmoniaLossMZ)); var deamidated = Compound1.GetChemicalFormula(); deamidated.Add(new ChemicalFormula("H-1N-1O")); var deamidatedMZ = new IsotopicDistribution(deamidated, 0.1, 0.001).Masses.Select(b => b.ToMassToChargeRatio(1)).ToList(); Console.WriteLine("deamidated mz: " + string.Join(", ", deamidatedMZ)); List <List <double> > allDistributions = new List <List <double> >() { regularMZ, withAmmoniaLossMZ, deamidatedMZ }; foreach (var arg in args) { var file = new ThermoRawFile(arg); file.Open(); string output_filepath = file.FilePath.Remove(file.FilePath.IndexOf(".raw"), ".raw".Length); //removes .raw at end for output using (System.IO.StreamWriter shiftsFile = new System.IO.StreamWriter(output_filepath + ".tsv")) { shiftsFile.WriteLine(file.FilePath); Console.WriteLine(file.FilePath); foreach (var scan in file) { if (scan.MsnOrder == 1) { double bestIntensity = 0; double monoError = double.NaN; foreach (var dist in allDistributions) { ThermoMzPeak monoisotopicPeak = null; try { monoisotopicPeak = scan.MassSpectrum.newSpectrumExtract(dist[0] - tol, dist[0] + tol).PeakWithHighestY; } catch { } if (monoisotopicPeak != null && bestIntensity < monoisotopicPeak.Intensity) { bestIntensity = monoisotopicPeak.Intensity; monoError = dist[0] - monoisotopicPeak.MZ; } } shiftsFile.WriteLine(scan.ScanNumber + "\t" + monoError); } } } } }
protected override MetaMorpheusEngineResults RunSpecific() { Status("Extracting data points:"); // The final training point list int numMs1MassChargeCombinationsConsidered = 0; int numMs1MassChargeCombinationsThatAreIgnoredBecauseOfTooManyPeaks = 0; int numMs2MassChargeCombinationsConsidered = 0; int numMs2MassChargeCombinationsThatAreIgnoredBecauseOfTooManyPeaks = 0; List <LabeledMs1DataPoint> Ms1List = new List <LabeledMs1DataPoint>(); List <LabeledMs2DataPoint> Ms2List = new List <LabeledMs2DataPoint>(); int numIdentifications = goodIdentifications.Count; // Loop over identifications HashSet <string> sequences = new HashSet <string>(); object lockObj = new object(); object lockObj2 = new object(); Parallel.ForEach(Partitioner.Create(0, numIdentifications), fff => { for (int matchIndex = fff.Item1; matchIndex < fff.Item2; matchIndex++) { PeptideSpectralMatch identification = goodIdentifications[matchIndex]; // Each identification has an MS2 spectrum attached to it. int ms2scanNumber = identification.ScanNumber; int peptideCharge = identification.ScanPrecursorCharge; if (identification.FullSequence == null) { continue; } var representativeSinglePeptide = identification.CompactPeptides.First().Value.Item2.First(); // Get the peptide, don't forget to add the modifications!!!! var SequenceWithChemicalFormulas = representativeSinglePeptide.SequenceWithChemicalFormulas; if (SequenceWithChemicalFormulas == null || representativeSinglePeptide.allModsOneIsNterminus.Any(b => b.Value.neutralLosses.Count != 1 || b.Value.neutralLosses.First() != 0)) { continue; } Proteomics.Peptide coolPeptide = new Proteomics.Peptide(SequenceWithChemicalFormulas); var ms2tuple = SearchMS2Spectrum(myMsDataFile.GetOneBasedScan(ms2scanNumber) as IMsDataScanWithPrecursor <IMzSpectrum <IMzPeak> >, coolPeptide, peptideCharge, identification); // If MS2 has low evidence for peptide, skip and go to next one if (ms2tuple.Item4 < numFragmentsNeededForEveryIdentification) { continue; } lock (lockObj2) { Ms2List.AddRange(ms2tuple.Item1); numMs2MassChargeCombinationsConsidered += ms2tuple.Item2; numMs2MassChargeCombinationsThatAreIgnoredBecauseOfTooManyPeaks += ms2tuple.Item3; if (sequences.Contains(identification.FullSequence)) { continue; // Do not search same sequence multiple times in MS1 scans } sequences.Add(identification.FullSequence); } // Calculate isotopic distribution of the full peptide var dist = IsotopicDistribution.GetDistribution(coolPeptide.GetChemicalFormula(), fineResolutionForIsotopeDistCalculation, 0.001); double[] theoreticalMasses = dist.Masses.ToArray(); double[] theoreticalIntensities = dist.Intensities.ToArray(); Array.Sort(theoreticalIntensities, theoreticalMasses, Comparer <double> .Create((x, y) => y.CompareTo(x))); var ms1tupleBack = SearchMS1Spectra(theoreticalMasses, theoreticalIntensities, ms2scanNumber, -1, peptideCharge, identification); var ms1tupleForward = SearchMS1Spectra(theoreticalMasses, theoreticalIntensities, ms2scanNumber, 1, peptideCharge, identification); lock (lockObj) { Ms1List.AddRange(ms1tupleBack.Item1); numMs1MassChargeCombinationsConsidered += ms1tupleBack.Item2; numMs1MassChargeCombinationsThatAreIgnoredBecauseOfTooManyPeaks += ms1tupleBack.Item3; Ms1List.AddRange(ms1tupleForward.Item1); numMs1MassChargeCombinationsConsidered += ms1tupleForward.Item2; numMs1MassChargeCombinationsThatAreIgnoredBecauseOfTooManyPeaks += ms1tupleForward.Item3; } } }); return(new DataPointAquisitionResults(this, Ms1List, Ms2List, numMs1MassChargeCombinationsConsidered, numMs1MassChargeCombinationsThatAreIgnoredBecauseOfTooManyPeaks, numMs2MassChargeCombinationsConsidered, numMs2MassChargeCombinationsThatAreIgnoredBecauseOfTooManyPeaks )); }
private (List <LabeledMs2DataPoint>, int, int, int) SearchMS2Spectrum(IMsDataScanWithPrecursor <IMzSpectrum <IMzPeak> > ms2DataScan, Proteomics.Peptide peptide, int peptideCharge, PeptideSpectralMatch identification) { List <LabeledMs2DataPoint> result = new List <LabeledMs2DataPoint>(); int numMs2MassChargeCombinationsConsidered = 0; int numMs2MassChargeCombinationsThatAreIgnoredBecauseOfTooManyPeaks = 0; int numFragmentsIdentified = 0; if (ms2DataScan.MassSpectrum.Size == 0) { return(result, numMs2MassChargeCombinationsConsidered, numMs2MassChargeCombinationsThatAreIgnoredBecauseOfTooManyPeaks, numFragmentsIdentified); } // Key: mz value, Value: error var addedPeaks = new Dictionary <double, double>(); var countForThisMS2 = 0; var countForThisMS2a = 0; var scanWindowRange = ms2DataScan.ScanWindowRange; IHasChemicalFormula[] fragmentList = peptide.Fragment(fragmentTypesForCalibration, true).OfType <IHasChemicalFormula>().ToArray(); foreach (var fragment in fragmentList) { bool fragmentIdentified = false; bool computedIsotopologues = false; double[] masses = new double[0]; double[] intensities = new double[0]; // First look for monoisotopic masses, do not compute distribution spectrum! for (int chargeToLookAt = 1; chargeToLookAt <= peptideCharge; chargeToLookAt++) { var monoisotopicMZ = fragment.MonoisotopicMass.ToMz(chargeToLookAt); if (monoisotopicMZ > scanWindowRange.Maximum) { continue; } if (monoisotopicMZ < scanWindowRange.Minimum) { break; } var closestPeakMZ = ms2DataScan.MassSpectrum.GetClosestPeakXvalue(monoisotopicMZ); if (mzToleranceForMs2Search.Within(closestPeakMZ.Value, monoisotopicMZ) && !computedIsotopologues) { var dist = IsotopicDistribution.GetDistribution(fragment.ThisChemicalFormula, fineResolutionForIsotopeDistCalculation, 0.001); masses = dist.Masses.ToArray(); intensities = dist.Intensities.ToArray(); Array.Sort(intensities, masses, Comparer <double> .Create((x, y) => y.CompareTo(x))); computedIsotopologues = true; break; } } if (computedIsotopologues) { bool startingToAdd = false; for (int chargeToLookAt = 1; chargeToLookAt <= peptideCharge; chargeToLookAt++) { if (masses.First().ToMz(chargeToLookAt) > scanWindowRange.Maximum) { continue; } if (masses.Last().ToMz(chargeToLookAt) < scanWindowRange.Minimum) { break; } var trainingPointsToAverage = new List <LabeledMs2DataPoint>(); foreach (double a in masses) { double theMZ = a.ToMz(chargeToLookAt); var npwr = ms2DataScan.MassSpectrum.NumPeaksWithinRange(mzToleranceForMs2Search.GetMinimumValue(theMZ), mzToleranceForMs2Search.GetMaximumValue(theMZ)); if (npwr == 0) { break; } numMs2MassChargeCombinationsConsidered++; if (npwr > 1) { numMs2MassChargeCombinationsThatAreIgnoredBecauseOfTooManyPeaks++; continue; } var closestPeakIndex = ms2DataScan.MassSpectrum.GetClosestPeakIndex(theMZ); var closestPeakMZ = ms2DataScan.MassSpectrum.XArray[closestPeakIndex.Value]; if (!addedPeaks.ContainsKey(closestPeakMZ)) { addedPeaks.Add(closestPeakMZ, Math.Abs(closestPeakMZ - theMZ)); trainingPointsToAverage.Add(new LabeledMs2DataPoint(closestPeakMZ, double.NaN, double.NaN, double.NaN, Math.Log(ms2DataScan.MassSpectrum.YArray[closestPeakIndex.Value]), theMZ, null)); } } // If started adding and suddnely stopped, go to next one, no need to look at higher charges if (trainingPointsToAverage.Count == 0 && startingToAdd) { break; } if (trainingPointsToAverage.Count < Math.Min(minMS2isotopicPeaksNeededForConfirmedIdentification, intensities.Count())) { } else { startingToAdd = true; if (!fragmentIdentified) { fragmentIdentified = true; numFragmentsIdentified += 1; } countForThisMS2 += trainingPointsToAverage.Count; countForThisMS2a++; result.Add(new LabeledMs2DataPoint(trainingPointsToAverage.Select(b => b.mz).Average(), ms2DataScan.RetentionTime, Math.Log(ms2DataScan.TotalIonCurrent), ms2DataScan.InjectionTime.HasValue ? Math.Log(ms2DataScan.InjectionTime.Value) : double.NaN, trainingPointsToAverage.Select(b => b.logIntensity).Average(), trainingPointsToAverage.Select(b => b.expectedMZ).Average(), identification)); } } } } return(result, numMs2MassChargeCombinationsConsidered, numMs2MassChargeCombinationsThatAreIgnoredBecauseOfTooManyPeaks, numFragmentsIdentified); }