public void TestGetIonFormula() { SequenceMassCalc sequenceMassCalc = new SequenceMassCalc(MassType.Monoisotopic); Assert.AreEqual(147.11, sequenceMassCalc.GetPrecursorMass("K"), .1); Assert.AreEqual("C6H14N2O2", sequenceMassCalc.GetMolecularFormula("K")); var label13C6K = new StaticMod("label13C6K", "K", null, LabelAtoms.C13); sequenceMassCalc.AddStaticModifications(new [] { label13C6K }); Assert.AreEqual(153.11, sequenceMassCalc.GetPrecursorMass("K"), .1); Assert.AreEqual("C'6H14N2O2", sequenceMassCalc.GetMolecularFormula("K")); var label15N2K = new StaticMod("label15N2K", "K", null, LabelAtoms.N15); sequenceMassCalc.AddStaticModifications(new[] { label15N2K }); Assert.AreEqual(155.11, sequenceMassCalc.GetPrecursorMass("K"), .1); Assert.AreEqual("C'6H14N'2O2", sequenceMassCalc.GetMolecularFormula("K")); var labelLaK = new StaticMod("labelLaK", "K", null, "La"); sequenceMassCalc.AddStaticModifications(new[] { labelLaK }); Assert.AreEqual(294.033, sequenceMassCalc.GetPrecursorMass("K"), .1); Assert.AreEqual("C'6H14LaN'2O2", sequenceMassCalc.GetMolecularFormula("K")); // Check our ability to handle strangely constructed chemical formulas Assert.AreEqual(Molecule.Parse("C12H9S2P0").ToString(), Molecule.Parse("C12H9S2").ToString()); // P0 is weird Assert.AreEqual(Molecule.Parse("C12H9S2P1").ToString(), Molecule.Parse("C12H9S2P").ToString()); // P1 is weird Assert.AreEqual(Molecule.Parse("C12H9S0P").ToString(), Molecule.Parse("C12H9P").ToString()); // S0 is weird, and not at end }
public void TestSingleAminoAcidLinkedPeptide() { var srmSettings = SrmSettingsList.GetDefault(); var mainPeptide = new Peptide("A"); var staticMod = new StaticMod("crosslinker", null, null, "-C2"); var linkedPeptide = new LinkedPeptide(new Peptide("D"), 0, null); var mainTransitionGroup = new TransitionGroup(mainPeptide, Adduct.SINGLY_PROTONATED, IsotopeLabelType.light); var mainTransitionGroupDocNode = new TransitionGroupDocNode(mainTransitionGroup, Annotations.EMPTY, srmSettings, null, null, ExplicitTransitionGroupValues.EMPTY, null, new TransitionDocNode[0], false); var modsWithoutLinkedPeptide = new ExplicitMods(mainPeptide, new[] { new ExplicitMod(0, staticMod), }, new TypedExplicitModifications[0]); Assert.AreEqual("C3H7NO2", AminoAcidFormulas.Default.GetFormula("A").ToString()); Assert.AreEqual("C3H7NO2", mainTransitionGroupDocNode.GetNeutralFormula(srmSettings, null).Molecule.ToString()); Assert.AreEqual("CH7NO2", mainTransitionGroupDocNode.GetNeutralFormula(srmSettings, modsWithoutLinkedPeptide).Molecule.ToString()); Assert.AreEqual("C4H7NO4", AminoAcidFormulas.Default.GetFormula("D").ToString()); var modsWithLinkedPeptide = new ExplicitMods(mainPeptide, new[] { new ExplicitMod(0, staticMod).ChangeLinkedPeptide(linkedPeptide) }, new TypedExplicitModifications[0]); Assert.AreEqual("C5H14N2O6", mainTransitionGroupDocNode.GetNeutralFormula(srmSettings, modsWithLinkedPeptide).Molecule.ToString()); var mainComplexFragmentIon = new ComplexFragmentIon(new Transition(mainTransitionGroup, IonType.precursor, mainPeptide.Length - 1, 0, Adduct.SINGLY_PROTONATED), null, modsWithLinkedPeptide.Crosslinks); var linkedComplexFragmentIon = new ComplexFragmentIon( new Transition(linkedPeptide.GetTransitionGroup(IsotopeLabelType.light, Adduct.SINGLY_PROTONATED), IonType.precursor, linkedPeptide.Peptide.Length - 1, 0, Adduct.SINGLY_PROTONATED), null, LinkedPeptide.EMPTY_CROSSLINK_STRUCTURE); var complexFragmentIon = mainComplexFragmentIon.AddChild(new ModificationSite(0, staticMod.Name), linkedComplexFragmentIon); var transition = complexFragmentIon.MakeTransitionDocNode(srmSettings, modsWithLinkedPeptide, null); var sequenceMassCalc = new SequenceMassCalc(MassType.Monoisotopic); var expectedMz = sequenceMassCalc.GetPrecursorMass("A") + sequenceMassCalc.GetPrecursorMass("D") - 24 - BioMassCalc.MassProton; Assert.AreEqual(expectedMz, transition.Mz, .00001); }
public static double CalculateFeature(EspFeatureDb.FeatureDef feature, Target target) { var seq = target.Sequence ?? string.Empty; switch (feature) { case EspFeatureDb.FeatureDef.length: return(seq.Length); case EspFeatureDb.FeatureDef.mass: return(MASS_CALC.GetPrecursorMass(seq)); case EspFeatureDb.FeatureDef.pI: return(PiCalc.Calculate(seq)); case EspFeatureDb.FeatureDef.AVG_Gas_phase_basicity: return(GasPhaseBasicityCalc.Calculate(seq).Average()); case EspFeatureDb.FeatureDef.nAcidic: return(AminoAcid.Count(seq, 'D', 'E')); // Amino acid case EspFeatureDb.FeatureDef.nBasic: return(AminoAcid.Count(seq, 'R', 'H', 'K')); // Amino acid default: return(EspFeatureDb.CalculateFeature(feature, seq)); } }
/// <summary> /// Test only method for creating a <see cref="BiblioSpecLibrary"/> file /// from another loaded <see cref="Library"/>. Should this move into test project? /// </summary> /// <param name="streamManager">Provides access to the file system</param> /// <param name="path">Path to write to</param> /// <param name="library">The loaded library to use as a data source</param> public static void Write(IStreamManager streamManager, string path, Library library) { using (FileSaver fs = new FileSaver(path, streamManager)) using (Stream outStream = streamManager.CreateStream(fs.SafeName, FileMode.Create, true)) { outStream.Write(BitConverter.GetBytes(library.SpectrumCount), 0, sizeof(int)); // num_spectra outStream.Write(BitConverter.GetBytes(0), 0, sizeof(int)); // filtered outStream.Write(BitConverter.GetBytes(1), 0, sizeof(int)); // version1 outStream.Write(BitConverter.GetBytes(1), 0, sizeof(int)); // version2 outStream.Write(BitConverter.GetBytes(0), 0, sizeof(int)); // next_id SequenceMassCalc calc = new SequenceMassCalc(MassType.Monoisotopic); byte[] seqBuffer = new byte[1024]; int scanNum = 1; foreach (var key in library.Keys) { SpectrumPeaksInfo peaksInfo; if (!library.TryLoadSpectrum(key, out peaksInfo)) { continue; } string sequence = key.Sequence; // Only works for unmodified sequence Debug.Assert(!key.IsModified); double precursorMH = calc.GetPrecursorMass(sequence); int charge = key.Charge; float precursorMz = (float)SequenceMassCalc.GetMZ(precursorMH, charge); outStream.Write(BitConverter.GetBytes(scanNum), 0, sizeof(int)); // scan_num outStream.Write(BitConverter.GetBytes(2), 0, sizeof(int)); // scan_type outStream.Write(BitConverter.GetBytes(precursorMz), 0, sizeof(float)); // pre_mz outStream.Write(BitConverter.GetBytes(charge), 0, sizeof(int)); // scan_type outStream.Write(BitConverter.GetBytes(0f), 0, sizeof(int)); // r_time outStream.Write(BitConverter.GetBytes(peaksInfo.Peaks.Length), 0, sizeof(int)); // num_peaks outStream.Write(BitConverter.GetBytes(0), 0, sizeof(int)); // 32-bit peak_ptr outStream.Write(BitConverter.GetBytes(sequence.Length), 0, sizeof(int)); // seq_len outStream.Write(BitConverter.GetBytes(0), 0, sizeof(int)); // annot outStream.Write(BitConverter.GetBytes(scanNum), 0, sizeof(int)); // copies (bogus value for ranking) outStream.Write(BitConverter.GetBytes(0), 0, sizeof(int)); // lib_id scanNum++; // Sequence int len = sequence.Length; seqBuffer[len] = 0; Encoding.UTF8.GetBytes(sequence, 0, len, seqBuffer, 0); outStream.Write(seqBuffer, 0, len + 1); // Modifications const string zeros = "000000000000000000000000000000000000000000000000000"; // Not L10N Encoding.UTF8.GetBytes(zeros.Substring(0, len), 0, len, seqBuffer, 0); outStream.Write(seqBuffer, 0, len + 1); // Peaks foreach (var mi in peaksInfo.Peaks) { outStream.Write(BitConverter.GetBytes((float)mi.Mz), 0, sizeof(float)); outStream.Write(BitConverter.GetBytes(mi.Intensity), 0, sizeof(float)); } } streamManager.Finish(outStream); fs.Commit(); } }
/// <summary> /// Test only method for creating a <see cref="XHunterLibrary"/> file /// from another loaded <see cref="Library"/>. Should this move into test project? /// </summary> /// <param name="streamManager">Provides access to the file system</param> /// <param name="path">Path to write to</param> /// <param name="library">The loaded library to use as a data source</param> /// <param name="lowIntensity">True to use 20 lowest intensity peaks for bad spectrum</param> public static void Write(IStreamManager streamManager, string path, Library library, bool lowIntensity) { using (FileSaver fs = new FileSaver(path, streamManager)) using (Stream outStream = streamManager.CreateStream(fs.SafeName, FileMode.Create, true)) { outStream.Write(BitConverter.GetBytes(0), 0, sizeof(int)); outStream.Write(BitConverter.GetBytes(library.SpectrumCount), 0, sizeof(int)); byte[] header = new byte[256 - 8]; const string headerText = @"HLF v=2 s=test.hlf d=2009.02.04"; Encoding.UTF8.GetBytes(headerText, 0, headerText.Length, header, 0); outStream.Write(header, 0, header.Length); SequenceMassCalc calc = new SequenceMassCalc(MassType.Monoisotopic); byte[] seqBuffer = new byte[1024]; foreach (var key in library.Keys) { SpectrumPeaksInfo peaksInfo; if (!library.TryLoadSpectrum(key, out peaksInfo)) { continue; } // Fake X! Hunter filtering by choosing just the to 20 peaks SpectrumPeaksInfo.MI[] peaks = peaksInfo.Peaks.ToArray(); // Sort by intensity if (lowIntensity) { Array.Sort(peaks, (p1, p2) => Comparer.Default.Compare(p1.Intensity, p2.Intensity)); // ascending } else { Array.Sort(peaks, (p1, p2) => Comparer.Default.Compare(p2.Intensity, p1.Intensity)); // descending } float maxI = peaks.Length == 0 ? 0 : peaks[0].Intensity; // Take 20 most intense peaks SpectrumPeaksInfo.MI[] peaksFiltered = new SpectrumPeaksInfo.MI[Math.Min(20, peaks.Length)]; Array.Copy(peaks, peaksFiltered, peaksFiltered.Length); // Resort by m/z (ineffient, but this is test code) Array.Sort(peaksFiltered, (p1, p2) => Comparer.Default.Compare(p1.Mz, p2.Mz)); double totalI = 0; byte[] peakBytes = new byte[(sizeof(byte) + sizeof(float)) * peaksFiltered.Length]; for (int i = 0; i < peaksFiltered.Length; i++) { var mi = peaksFiltered[i]; // Calculate the X! Hunter processed intensity value float intensity = 100f * mi.Intensity / maxI; totalI += intensity; // Fill the peaks buffer peakBytes[i] = (byte)(int)intensity; Array.Copy(BitConverter.GetBytes((float)mi.Mz), 0, peakBytes, peaksFiltered.Length + i * 4, sizeof(float)); } var sequence = key.Target.ToString(); // Only works for unmodified sequence Debug.Assert(!key.IsModified); double precursorMH = calc.GetPrecursorMass(sequence); outStream.Write(BitConverter.GetBytes(precursorMH), 0, sizeof(double)); outStream.Write(BitConverter.GetBytes(key.Charge), 0, sizeof(int)); // Value rounded for consistent serialization round-tripping float i2 = (float)Math.Round(Math.Sqrt(totalI), 4); outStream.Write(BitConverter.GetBytes(i2), 0, sizeof(float)); outStream.Write(BitConverter.GetBytes(0.0001f), 0, sizeof(float)); outStream.Write(BitConverter.GetBytes(sequence.Length), 0, sizeof(int)); // Sequence Encoding.UTF8.GetBytes(sequence, 0, sequence.Length, seqBuffer, 0); outStream.Write(seqBuffer, 0, sequence.Length); // Peaks outStream.Write(BitConverter.GetBytes(peaksFiltered.Length), 0, sizeof(int)); outStream.Write(peakBytes, 0, peakBytes.Length); // Modifications outStream.Write(BitConverter.GetBytes(0), 0, sizeof(int)); // Homologs outStream.Write(BitConverter.GetBytes(0), 0, sizeof(int)); } streamManager.Finish(outStream); fs.Commit(); } }
public void CalcMass(SequenceMassCalc massCalc) { MassH = massCalc.GetPrecursorMass(Sequence); }
public void CalcMass(SequenceMassCalc massCalc) { MassH = massCalc.GetPrecursorMass(Sequence); }
/// <summary> /// Test only method for creating a <see cref="BiblioSpecLibrary"/> file /// from another loaded <see cref="Library"/>. Should this move into test project? /// </summary> /// <param name="streamManager">Provides access to the file system</param> /// <param name="path">Path to write to</param> /// <param name="library">The loaded library to use as a data source</param> public static void Write(IStreamManager streamManager, string path, Library library) { using (FileSaver fs = new FileSaver(path, streamManager)) using (Stream outStream = streamManager.CreateStream(fs.SafeName, FileMode.Create, true)) { outStream.Write(BitConverter.GetBytes(library.SpectrumCount), 0, sizeof(int)); // num_spectra outStream.Write(BitConverter.GetBytes(0), 0, sizeof(int)); // filtered outStream.Write(BitConverter.GetBytes(1), 0, sizeof(int)); // version1 outStream.Write(BitConverter.GetBytes(1), 0, sizeof(int)); // version2 outStream.Write(BitConverter.GetBytes(0), 0, sizeof(int)); // next_id SequenceMassCalc calc = new SequenceMassCalc(MassType.Monoisotopic); byte[] seqBuffer = new byte[1024]; int scanNum = 1; foreach (var key in library.Keys) { SpectrumPeaksInfo peaksInfo; if (!library.TryLoadSpectrum(key, out peaksInfo)) continue; string sequence = key.Sequence; // Only works for unmodified sequence Debug.Assert(!key.IsModified); double precursorMH = calc.GetPrecursorMass(sequence); int charge = key.Charge; float precursorMz = (float) SequenceMassCalc.GetMZ(precursorMH, charge); outStream.Write(BitConverter.GetBytes(scanNum), 0, sizeof(int)); // scan_num outStream.Write(BitConverter.GetBytes(2), 0, sizeof(int)); // scan_type outStream.Write(BitConverter.GetBytes(precursorMz), 0, sizeof(float)); // pre_mz outStream.Write(BitConverter.GetBytes(charge), 0, sizeof(int)); // scan_type outStream.Write(BitConverter.GetBytes(0f), 0, sizeof(int)); // r_time outStream.Write(BitConverter.GetBytes(peaksInfo.Peaks.Length), 0, sizeof(int)); // num_peaks outStream.Write(BitConverter.GetBytes(0), 0, sizeof(int)); // 32-bit peak_ptr outStream.Write(BitConverter.GetBytes(sequence.Length), 0, sizeof(int)); // seq_len outStream.Write(BitConverter.GetBytes(0), 0, sizeof(int)); // annot outStream.Write(BitConverter.GetBytes(scanNum), 0, sizeof(int)); // copies (bogus value for ranking) outStream.Write(BitConverter.GetBytes(0), 0, sizeof(int)); // lib_id scanNum++; // Sequence int len = sequence.Length; seqBuffer[len] = 0; Encoding.UTF8.GetBytes(sequence, 0, len, seqBuffer, 0); outStream.Write(seqBuffer, 0, len + 1); // Modifications const string zeros = "000000000000000000000000000000000000000000000000000"; // Not L10N Encoding.UTF8.GetBytes(zeros.Substring(0, len), 0, len, seqBuffer, 0); outStream.Write(seqBuffer, 0, len + 1); // Peaks foreach (var mi in peaksInfo.Peaks) { outStream.Write(BitConverter.GetBytes((float)mi.Mz), 0, sizeof(float)); outStream.Write(BitConverter.GetBytes(mi.Intensity), 0, sizeof(float)); } } streamManager.Finish(outStream); fs.Commit(); } }