Exemplo n.º 1
0
        public void TestReplaceIsotopes()
        {
            ChemicalFormula formulaA = new ChemicalFormula("CC{13}2H3NO");

            formulaA.Replace(PeriodicTable.GetElement("C")[13], PeriodicTable.GetElement("C")[12]);
            Assert.AreEqual("CC{12}2H3NO", formulaA.Formula);
        }
Exemplo n.º 2
0
        public void ChemicalForulaMyTest()
        {
            ChemicalFormula formula = new ChemicalFormula();

            formula.Add(new ChemicalFormula("C3H5NO"));
            Assert.AreEqual(PeriodicTable.GetElement("C").PrincipalIsotope.AtomicMass * 3 + PeriodicTable.GetElement("H").PrincipalIsotope.AtomicMass * 5 + PeriodicTable.GetElement("N").PrincipalIsotope.AtomicMass + PeriodicTable.GetElement("O").PrincipalIsotope.AtomicMass, formula.MonoisotopicMass);
        }
Exemplo n.º 3
0
        private static void BenchmarkTimeGettingElementFromPeriodicTable()
        {
            Console.WriteLine("Starting benchmark BenchmarkTimeGettingElementFromPeriodicTable");

            int numRepetitions = 100000000;

            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Restart();
            for (int i = 0; i < numRepetitions; i++)
            {
                var a = PeriodicTable.GetElement(1);
                var b = a.Protons + a.AverageMass + 4;
            }
            stopWatch.Stop();
            Console.WriteLine("Time for getting by atomic number: " + stopWatch.Elapsed);

            stopWatch.Restart();
            for (int i = 0; i < numRepetitions; i++)
            {
                var a = PeriodicTable.GetElement("H");
                var b = a.Protons + a.AverageMass + 4;
            }
            stopWatch.Stop();
            Console.WriteLine("Time for getting by atomic symbol: " + stopWatch.Elapsed);

            Console.WriteLine("Benchmark BenchmarkTimeGettingElementFromPeriodicTable finished");
        }
        public void RemoveElementCompletelyFromFromulaWithHeavyIsotope()
        {
            ChemicalFormula formulaA = new ChemicalFormula("C2C{13}H3NO");
            ChemicalFormula formulaB = new ChemicalFormula("H3NO");

            formulaA.Remove(PeriodicTable.GetElement("C"));

            Assert.AreEqual(formulaA, formulaB);
        }
Exemplo n.º 5
0
        public static void RemoveElementFromFromula()
        {
            ChemicalFormula formulaA = ChemicalFormula.ParseFormula("C2H3NO");
            ChemicalFormula formulaB = ChemicalFormula.ParseFormula("C2HNO");

            formulaA.Remove(PeriodicTable.GetElement("H"), 2);

            Assert.AreEqual(formulaA, formulaB);
        }
Exemplo n.º 6
0
        public void RemoveIsotopeCompletelyFromFromula()
        {
            ChemicalFormula formulaA = new ChemicalFormula("C2H3NO");
            ChemicalFormula formulaB = new ChemicalFormula("C2NO");

            formulaA.RemoveIsotopesOf(PeriodicTable.GetElement("H"));

            Assert.AreEqual(formulaA, formulaB);
        }
Exemplo n.º 7
0
        public void RemoveNegativeElementFromFromula()
        {
            ChemicalFormula formulaA = new ChemicalFormula("C2H3NO");
            ChemicalFormula formulaB = new ChemicalFormula("C2H5NO");

            formulaA.Remove(PeriodicTable.GetElement("H"), -2);

            Assert.AreEqual(formulaA, formulaB);
        }
Exemplo n.º 8
0
        public void AddIsotopeWithExistingMassNumber()
        {
            Element al = PeriodicTable.GetElement("Al");

            Assert.Throws <ArgumentException>(() =>
            {
                al.AddIsotope(27, 28, 1);
            }, "Isotope with mass number " + 28 + " already exists");
        }
Exemplo n.º 9
0
        public void RemoveZeroIsotopeFromFromula()
        {
            ChemicalFormula formulaA = new ChemicalFormula("C2H3NO");
            ChemicalFormula formulaB = new ChemicalFormula("C2H3NO");

            formulaA.Remove(PeriodicTable.GetElement("H")[1], 0);

            Assert.AreEqual(formulaA, formulaB);
        }
Exemplo n.º 10
0
        public static void RemoveElementCompletelyFromFromula()
        {
            ChemicalFormula formulaA = ChemicalFormula.ParseFormula("C2H3NO");
            ChemicalFormula formulaB = ChemicalFormula.ParseFormula("C2NO");

            formulaA.RemoveIsotopesOf(PeriodicTable.GetElement("H"));

            Assert.AreEqual(formulaB, formulaA);
        }
Exemplo n.º 11
0
        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]));
        }
Exemplo n.º 12
0
        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);
        }
Exemplo n.º 13
0
        public void AddZeroIsotopeToFormula()
        {
            ChemicalFormula formulaA = new ChemicalFormula("C2H3NO");
            ChemicalFormula formulaB = new ChemicalFormula("C2H3NO");

            Isotope h1 = PeriodicTable.GetElement("H")[1];

            formulaA.Add(h1, 0);

            Assert.AreEqual(formulaA, formulaB);
        }
Exemplo n.º 14
0
        public void AddZeroElementToFormula()
        {
            ChemicalFormula formulaA = new ChemicalFormula("C2H3NO");
            ChemicalFormula formulaB = new ChemicalFormula("C2H3NO");

            Element n = PeriodicTable.GetElement("N");

            formulaA.Add(n, 0);

            Assert.AreEqual(formulaA, formulaB);
        }
Exemplo n.º 15
0
        public void AddLargeElementToFormula()
        {
            ChemicalFormula formulaA = new ChemicalFormula("C2H3NO");
            ChemicalFormula formulaB = new ChemicalFormula("C2H3NOFe");

            Element fe = PeriodicTable.GetElement("Fe");

            formulaA.Add(fe, 1);

            Assert.AreEqual(formulaA, formulaB);
        }
Exemplo n.º 16
0
        public static void AddElementToFormula()
        {
            ChemicalFormula formulaA = ChemicalFormula.ParseFormula("C2H3NO");
            ChemicalFormula formulaB = ChemicalFormula.ParseFormula("C2H3N2O");

            Element n = PeriodicTable.GetElement(7);

            formulaA.Add(n, 1);

            Assert.AreEqual(formulaA, formulaB);
        }
Exemplo n.º 17
0
        public static void AddIsotopeToFormula()
        {
            ChemicalFormula formulaA = ChemicalFormula.ParseFormula("C2H3NO");
            ChemicalFormula formulaB = ChemicalFormula.ParseFormula("C2H3H{1}NO");

            Isotope h1 = PeriodicTable.GetElement("H")[1];

            formulaA.Add(h1, 1);

            Assert.AreEqual(formulaA, formulaB);
        }
        public void AddLargeIsotopeToFormula()
        {
            ChemicalFormula formulaA = new ChemicalFormula("C2H3NO");
            ChemicalFormula formulaB = new ChemicalFormula("C2H3NOFe");

            Isotope fe = PeriodicTable.GetElement("Fe").PrincipalIsotope;

            formulaA.Add(fe, 1);

            Assert.AreEqual(formulaA, formulaB);
        }
Exemplo n.º 19
0
        public static void LoadElements()
        {
            // has the periodic table already been loaded?
            if (PeriodicTable.GetElement(1) != null)
            {
                return;
            }

            // periodic table has not been loaded yet - load it
            PeriodicTableLoader.Load();
        }
Exemplo n.º 20
0
        public void AddNegativeIsotopeToFormula()
        {
            ChemicalFormula formulaA = new ChemicalFormula("C2HH{1}2NO");
            ChemicalFormula formulaB = new ChemicalFormula("C2HNO");

            Isotope h1 = PeriodicTable.GetElement("H")[1];

            formulaA.Add(h1, -2);

            Assert.AreEqual(formulaA, formulaB);
        }
Exemplo n.º 21
0
        public void PeptideCountElements()
        {
            Peptide pep = new Peptide("G");

            pep.AddModification(new OldSchoolModification(1));
            Assert.AreEqual(5, pep.ElementCountWithIsotopes("H"));

            pep.AddModification(new OldSchoolChemicalFormulaModification(ChemicalFormula.ParseFormula("H{1}")));
            Assert.AreEqual(5, pep.ElementCountWithIsotopes("H")); // NOTHING HAS BEEN ADDED!

            pep.AddModification(new OldSchoolChemicalFormulaModification(ChemicalFormula.ParseFormula("H{1}"), ModificationSites.G));
            Assert.AreEqual(6, pep.ElementCountWithIsotopes("H"));

            Isotope isotope = PeriodicTable.GetElement("H").PrincipalIsotope;

            Assert.AreEqual(1, pep.SpecificIsotopeCount(isotope));
        }
Exemplo n.º 22
0
 /// <summary>
 /// Subtract the mass of a proton for every formal charge on a modification.
 /// </summary>
 /// <param name="_monoisotopicMass"></param>
 /// <param name="_chemicalFormula"></param>
 /// <param name="_databaseReference"></param>
 /// <param name="formalChargesDictionary"></param>
 /// <returns></returns>
 private static double AdjustMonoIsotopicMassForFormalCharge(double?_monoisotopicMass, ChemicalFormula _chemicalFormula, Dictionary <string, IList <string> > _databaseReference, Dictionary <string, int> formalChargesDictionary)
 {
     foreach (var dbAndAccession in _databaseReference.SelectMany(b => b.Value.Select(c => b.Key + "; " + c)))
     {
         if (formalChargesDictionary.ContainsKey(dbAndAccession))
         {
             if (_monoisotopicMass.HasValue)
             {
                 _monoisotopicMass -= formalChargesDictionary[dbAndAccession] * Constants.ProtonMass;
             }
             if (_chemicalFormula != null)
             {
                 _chemicalFormula.Remove(PeriodicTable.GetElement("H"), formalChargesDictionary[dbAndAccession]);
             }
             break;
         }
     }
     return((double)_monoisotopicMass);
 }
Exemplo n.º 23
0
        private static void BenchmarkGettingIsotopes()
        {
            Console.WriteLine("Starting benchmark BenchmarkGettingIsotopes");

            int numRepetitions = 10000000;

            Stopwatch stopWatch = new Stopwatch();

            long a = 0;

            stopWatch.Restart();
            for (int i = 0; i < numRepetitions; i++)
            {
                a += PeriodicTable.GetElement(20).Isotopes.Count();
            }
            stopWatch.Stop();
            Console.WriteLine("Time for getting isotopes1: " + stopWatch.Elapsed + " a = " + a);

            Console.WriteLine("Benchmark BenchmarkGettingIsotopes finished");
        }
Exemplo n.º 24
0
        public ChemicalFormulaModification ToHeavyModification(bool c, bool n)
        {
            var formula = new ChemicalFormula();

            if (c)
            {
                Element carbon   = PeriodicTable.GetElement("C");
                int     carbon12 = ChemicalFormula.Count(carbon[12]);
                formula.Add(carbon[12], -carbon12);
                formula.Add(carbon[13], carbon12);
            }

            if (n)
            {
                Element nitrogen   = PeriodicTable.GetElement("N");
                int     nitrogen14 = ChemicalFormula.Count(nitrogen[14]);
                formula.Add(nitrogen[14], -nitrogen14);
                formula.Add(nitrogen[15], nitrogen14);
            }

            return(new ChemicalFormulaModification(formula, "#", Site));
        }
Exemplo n.º 25
0
        /// <summary>
        /// Get a ModificationWithLocation from string representations of a modification specification. Returns null if the string representation is not recognized.
        /// </summary>
        /// <param name="specification"></param>
        /// <returns></returns>
        private static IEnumerable <Modification> ReadMod(List <string> specification, Dictionary <string, int> formalChargesDictionary)
        {
            // UniProt-specific fields
            string uniprotAC = null;
            string uniprotFT = null;

            // Other fields
            string          id     = null;
            List <string>   motifs = null;
            string          terminusLocalizationString = null;
            ChemicalFormula correctionFormula          = null;
            double?         monoisotopicMass           = null;
            var             externalDatabaseLinks      = new Dictionary <string, IList <string> >();
            List <string>   keywords = null;

            // Custom fields
            List <double> neutralLosses    = null;
            List <double> diagnosticIons   = null;
            string        modificationType = null;

            foreach (string line in specification)
            {
                if (line.Length >= 2)
                {
                    switch (line.Substring(0, 2))
                    {
                    case "ID":     // Mandatory
                        id = line.Substring(5);
                        break;

                    case "AC":     // Do not use! Only present in UniProt ptmlist
                        uniprotAC = line.Substring(5);
                        break;

                    case "FT":     // Optional
                        uniprotFT = line.Substring(5);
                        break;

                    case "TG":     // Which amino acid(s) or motifs is the modification on
                        motifs = new List <string>(line.Substring(5).TrimEnd('.').Split(new string[] { " or " }, StringSplitOptions.None));
                        break;

                    case "PP":     // Terminus localization
                        terminusLocalizationString = line.Substring(5);
                        break;

                    case "CF":     // Correction formula
                        correctionFormula = ChemicalFormula.ParseFormula(line.Substring(5).Replace(" ", string.Empty));
                        break;

                    case "MM":     // Monoisotopic mass difference. Might not precisely correspond to formula!
                    {
                        if (!double.TryParse(line.Substring(5), NumberStyles.Any, CultureInfo.InvariantCulture, out double thisMM))
                        {
                            throw new MzLibException(line.Substring(5) + " is not a valid monoisotopic mass");
                        }
                        monoisotopicMass = thisMM;
                    }
                    break;

                    case "DR":     // External database links!
                    {
                        var splitString = line.Substring(5).TrimEnd('.').Split(new string[] { "; " }, StringSplitOptions.None);
                        if (externalDatabaseLinks.TryGetValue(splitString[0], out IList <string> val))
                        {
                            val.Add(splitString[1]);
                        }
                        else
                        {
                            externalDatabaseLinks.Add(splitString[0], new List <string> {
                                    splitString[1]
                                });
                        }
                    }
                    break;

                    case "KW":     // ; Separated keywords
                    {
                        keywords = new List <string>(line.Substring(5).TrimEnd('.').Split(new string[] { "; " }, StringSplitOptions.None));
                    }
                    break;

                    // NOW CUSTOM FIELDS:

                    case "NL":     // Netural Losses. If field doesn't exist, single equal to 0
                        try
                        {
                            neutralLosses = new List <double>(line.Substring(5).Split(new string[] { " or " }, StringSplitOptions.RemoveEmptyEntries).Select(b => ChemicalFormula.ParseFormula(b).MonoisotopicMass));
                        }
                        catch (MzLibException)
                        {
                            neutralLosses = new List <double>(line.Substring(5).Split(new string[] { " or " }, StringSplitOptions.RemoveEmptyEntries).Select(b => double.Parse(b, CultureInfo.InvariantCulture)));
                        }
                        break;

                    case "DI":     // Masses of diagnostic ions. Might just be "DI"!!! If field doesn't exist, create an empty list!
                        try
                        {
                            diagnosticIons = new List <double>(line.Substring(5).Split(new string[] { " or " }, StringSplitOptions.RemoveEmptyEntries).Select(b => ChemicalFormula.ParseFormula(b).MonoisotopicMass));
                        }
                        catch (MzLibException)
                        {
                            diagnosticIons = new List <double>(line.Substring(5).Split(new string[] { " or " }, StringSplitOptions.RemoveEmptyEntries).Select(b => double.Parse(b, CultureInfo.InvariantCulture)));
                        }
                        break;

                    case "MT":     // Modification Type. If the field doesn't exist, set to the database name
                        modificationType = line.Substring(5);
                        break;

                    case "//":
                        if (id == null)
                        {
                            throw new MzLibException("id is null");
                        }
                        if ("CROSSLNK".Equals(uniprotFT))     // Ignore crosslinks
                        {
                            break;
                        }
                        if (uniprotAC != null)
                        {
                            modificationType = "UniProt";
                            externalDatabaseLinks.Add("UniProt", new List <string> {
                                uniprotAC
                            });
                        }
                        if (modificationType == null)
                        {
                            throw new MzLibException("modificationType of " + id + " is null");
                        }
                        if (!monoisotopicMass.HasValue && correctionFormula != null)
                        {
                            monoisotopicMass = correctionFormula.MonoisotopicMass;
                        }

                        foreach (var dbAndAccession in externalDatabaseLinks.SelectMany(b => b.Value.Select(c => b.Key + "; " + c)))
                        {
                            if (formalChargesDictionary.ContainsKey(dbAndAccession))
                            {
                                if (monoisotopicMass.HasValue)
                                {
                                    monoisotopicMass -= formalChargesDictionary[dbAndAccession] * Constants.protonMass;
                                }
                                if (correctionFormula != null)
                                {
                                    correctionFormula.Remove(PeriodicTable.GetElement("H"), formalChargesDictionary[dbAndAccession]);
                                }
                                break;
                            }
                        }
                        if (terminusLocalizationString == null || motifs == null)
                        {
                            yield return(new Modification(id, modificationType));
                        }
                        else if (ModificationWithLocation.terminusLocalizationTypeCodes.TryGetValue(terminusLocalizationString, out TerminusLocalization terminusLocalization))
                        {
                            foreach (var singleTarget in motifs)
                            {
                                string theMotif;
                                if (aminoAcidCodes.TryGetValue(singleTarget, out char possibleMotifChar))
                                {
                                    theMotif = possibleMotifChar.ToString();
                                }
                                else
                                {
                                    theMotif = singleTarget;
                                }
                                if (ModificationMotif.TryGetMotif(theMotif, out ModificationMotif motif))
                                {
                                    var idToUse = id;
                                    // Augment id if mulitple motifs!
                                    // Add id to keywords
                                    if (motifs.Count != 1)
                                    {
                                        if (keywords == null)
                                        {
                                            keywords = new List <string> {
                                                id
                                            }
                                        }
                                        ;
                                        else
                                        {
                                            keywords.Add(id);
                                        }
                                        idToUse += " on " + motif;
                                    }

                                    // Add the modification!

                                    if (!monoisotopicMass.HasValue)
                                    {
                                        // Return modification
                                        yield return(new ModificationWithLocation(idToUse, modificationType, motif, terminusLocalization, externalDatabaseLinks, keywords));
                                    }
                                    else
                                    {
                                        if (correctionFormula == null)
                                        {
                                            // Return modification with mass
                                            yield return(new ModificationWithMass(idToUse, modificationType, motif, terminusLocalization, monoisotopicMass.Value, externalDatabaseLinks,
                                                                                  keywords,
                                                                                  neutralLosses,
                                                                                  diagnosticIons));
                                        }
                                        else
                                        {
                                            // Return modification with complete information!
                                            yield return(new ModificationWithMassAndCf(idToUse, modificationType, motif, terminusLocalization, correctionFormula, monoisotopicMass.Value, externalDatabaseLinks, keywords,
                                                                                       neutralLosses,
                                                                                       diagnosticIons));
                                        }
                                    }
                                }
                                else
                                {
                                    throw new MzLibException("Could not get motif from " + singleTarget);
                                }
                            }
                        }
                        else
                        {
                            throw new MzLibException("Could not get modification site from " + terminusLocalizationString);
                        }
                        break;
                    }
                }
            }
        }
Exemplo n.º 26
0
        public void ContainsIsotopesOfYe()
        {
            ChemicalFormula formulaA = new ChemicalFormula("CC{13}H3NO");

            Assert.IsTrue(formulaA.ContainsIsotopesOf(PeriodicTable.GetElement("C")));
        }
Exemplo n.º 27
0
        public void AverageMass()
        {
            ChemicalFormula formulaA = new ChemicalFormula("C");

            Assert.AreEqual(PeriodicTable.GetElement("C").AverageMass, formulaA.AverageMass);
        }
Exemplo n.º 28
0
 public void InvalidChemicalElement()
 {
     Assert.Throws <KeyNotFoundException>(() => { Element e = PeriodicTable.GetElement("Faa"); });
 }
Exemplo n.º 29
0
        public void ContainsSpecificIsotope()
        {
            ChemicalFormula formulaA = new ChemicalFormula("C2H5NOO{16}");

            Assert.IsTrue(formulaA.ContainsSpecificIsotope(PeriodicTable.GetElement("O")[16]));
        }
Exemplo n.º 30
0
 public void InvalidElementIsotope()
 {
     Assert.IsNull(PeriodicTable.GetElement("C")[100]);
 }