示例#1
0
        /// <summary>
        /// Basic overview of how chemical formulas can be used and modified
        /// </summary>
        private static void ChemicalFormulaExamples()
        {
            Console.WriteLine("**Chemical Formula Examples**");

            // Simple chemical formula creation
            ChemicalFormula formula1 = new ChemicalFormula("C2H3NO");
            WriteFormulaToConsole(formula1);

            // Input order does not matter
            ChemicalFormula formula2 = new ChemicalFormula("NH3C2O");
            WriteFormulaToConsole(formula2);

            // Formulas are identicial if they have the exact same type of elements and count
            Console.WriteLine("Are {0} and {1} equivalent? {2}", formula1, formula2, formula1.Equals(formula2));

            // You can modify exisiting chemical formulas in many ways.
            // You can add a chemical formula to a chemical formula
            formula1.Add(formula2);
            WriteFormulaToConsole(formula1);
            Console.WriteLine("Are {0} and {1} equivalent? {2}", formula1, formula2, formula1.Equals(formula2));

            // You can completely remove an element from a chemical formula
            formula1.Remove("C");
            WriteFormulaToConsole(formula1);

            // Even negative values are possible if not physically possible
            formula1.Remove(formula2);
            WriteFormulaToConsole(formula1);

            // Implicit arithmetic is also possible (supports +, -, and *)
            ChemicalFormula formula3 = formula2 - formula1;
            WriteFormulaToConsole(formula3);
            ChemicalFormula formula4 = formula3 + formula1;
            WriteFormulaToConsole(formula4);
            ChemicalFormula formula5 = formula2*5;
            WriteFormulaToConsole(formula5);

            // Formulas consist of a dictionary of isotopes and count, and by default, the most common (abundant) isotope of an element
            // is included (i.e. Carbon 12 instead of Carbon 13). You can explicitly state that you want another isotope in a chemical formula
            // by this notation: <Element Symbol>{<Mass Number>} (e.g. C{13}, N{15}, etc..)

            formula1 = new ChemicalFormula("C2C{13}2H3NO");
            formula2 = new ChemicalFormula("C4H3NO");

            WriteFormulaToConsole(formula1);
            WriteFormulaToConsole(formula2);

            Console.WriteLine("Are {0} and {1} equivalent? {2}", formula1, formula2, formula1.Equals(formula2));

            // No need to specify the mass number of the most abundant isotope for an element
            formula3 = new ChemicalFormula("C{12}2C2H3NO");
            formula4 = new ChemicalFormula("C4H3NO");
            Console.WriteLine("Are {0} and {1} equivalent? {2}", formula3, formula4, formula3.Equals(formula4));
        }
示例#2
0
        public void TestNullArguments()
        {
            ChemicalFormula     formulaA = new ChemicalFormula("CO");
            IHasChemicalFormula ok       = null;

            Assert.AreEqual("item", Assert.Throws <ArgumentNullException>(() => { formulaA.Add(ok); }).ParamName);
            ChemicalFormula ok2 = null;

            Assert.AreEqual("formula", Assert.Throws <ArgumentNullException>(() => { formulaA.Add(ok2); }).ParamName);
            Assert.AreEqual("other", Assert.Throws <ArgumentNullException>(() => { new ChemicalFormula(ok2); }).ParamName);
            Element ok3 = null;

            Assert.AreEqual("element", Assert.Throws <ArgumentNullException>(() => { formulaA.AddPrincipalIsotopesOf(ok3, 0); }).ParamName);
            Assert.AreEqual("item", Assert.Throws <ArgumentNullException>(() => { formulaA.Remove(ok); }).ParamName);
            Assert.AreEqual("formula", Assert.Throws <ArgumentNullException>(() => { formulaA.Remove(ok2); }).ParamName);
            Assert.AreEqual("formula", Assert.Throws <ArgumentNullException>(() => { formulaA.IsSubsetOf(ok2); }).ParamName);
            Assert.AreEqual("formula", Assert.Throws <ArgumentNullException>(() => { formulaA.IsSupersetOf(ok2); }).ParamName);
            Assert.AreEqual("element", Assert.Throws <ArgumentNullException>(() => { formulaA.CountWithIsotopes(ok3); }).ParamName);
            Assert.AreEqual("element", Assert.Throws <ArgumentNullException>(() => { formulaA.CountSpecificIsotopes(ok3, 0); }).ParamName);
            Assert.IsFalse(formulaA.Equals(ok2));
            IEnumerable <IHasChemicalFormula> ok4 = null;

            Assert.AreEqual("formulas", Assert.Throws <ArgumentNullException>(() => { ChemicalFormula.Combine(ok4); }).ParamName);
            Assert.AreEqual("element", Assert.Throws <ArgumentNullException>(() => { PeriodicTable.Add(ok3); }).ParamName);

            Assert.AreEqual("formula", Assert.Throws <ArgumentNullException>(() => { new IsotopicDistribution(ok2); }).ParamName);

            IHasMass ok5 = null;

            Assert.AreEqual("objectWithMass", Assert.Throws <ArgumentNullException>(() => { ok5.ToMZ(0); }).ParamName);

            var ok7 = new PhysicalObjectWithChemicalFormula("C");
        }
示例#3
0
        public void ChemicalFormulaCompareTest()
        {
            var provider = new MockElementProvider();
            var formula  = ChemicalFormula.Water(provider);

            var formula2 = ChemicalFormula.Water(provider);

            IElement h        = provider.GetElement("H");
            IElement o        = provider.GetElement("O");
            var      formula3 = new ChemicalFormula(new[]
            {
                new EntityCardinality <IElement>(h, 1),
                new EntityCardinality <IElement>(o, 2),
            });

            Assert.AreEqual(formula, formula2);
            Assert.AreNotEqual(formula, formula3);

            // Equality when element object is different.
            IElement        anotherH = new Element(1, "H", h.Isotopes);
            IElement        anotherO = new Element(1, "O", o.Isotopes);
            ChemicalFormula formula4 = new ChemicalFormula(new[]
            {
                new EntityCardinality <IElement>(anotherH, 2),
                new EntityCardinality <IElement>(anotherO, 1),
            });

            Assert.IsTrue(formula.Equals(formula4));
            Assert.IsTrue(formula2.Equals(formula4));
            Assert.IsFalse(formula3.Equals(formula4));
        }
        public override bool Equals(object o)
        {
            ModificationWithMassAndCf m = o as ModificationWithMassAndCf;

            return(m != null &&
                   base.Equals(m) &&
                   chemicalFormula.Equals(m.chemicalFormula));
        }
示例#5
0
        public static void Multiply()
        {
            ChemicalFormula formulaA = ChemicalFormula.ParseFormula("C2H3NO");

            formulaA.Multiply(2);
            ChemicalFormula formulaB = ChemicalFormula.ParseFormula("C4H6N2O2");

            Assert.AreEqual(formulaA, formulaB);

            Assert.IsFalse(formulaA.Equals(null));
        }
示例#6
0
        /// <summary>
        /// Resolve Methods()
        /// if all 'values' are the same this returns the one value otherwise you get a separated list of all values in their original order.
        /// for example:
        /// Notches 1,1,1,1 returns as 1
        /// Notches 1,0,1,0 returns as 1|0|1|0
        /// </summary>
        internal static (string ResolvedString, ChemicalFormula ResolvedValue) Resolve(IEnumerable <IEnumerable <Modification> > enumerable)
        {
            var             list             = enumerable.ToList();
            ChemicalFormula firstChemFormula = new ChemicalFormula();

            foreach (var firstMods in list[0])
            {
                if (firstMods == null || firstMods.ChemicalFormula == null)
                {
                    return("unknown", null);
                }
                firstChemFormula.Add(firstMods.ChemicalFormula);
            }

            bool equals = true;
            List <ChemicalFormula> formulas = new List <ChemicalFormula>();

            foreach (var anEnum in list)
            {
                ChemicalFormula fhere = new ChemicalFormula();
                foreach (var mod in anEnum)
                {
                    if (mod == null || mod.ChemicalFormula == null)
                    {
                        return("unknown", null);
                    }
                    fhere.Add(mod.ChemicalFormula);
                }
                if (!firstChemFormula.Equals(fhere))
                {
                    equals = false;
                }
                formulas.Add(fhere);
            }
            if (!equals)
            {
                var returnString = GlobalVariables.CheckLengthOfOutput(string.Join("|", formulas.Select(b => b.Formula)));
                return(returnString, null);
            }
            else
            {
                return(firstChemFormula.Formula, firstChemFormula);
            }
        }
        private static (string, ChemicalFormula) Resolve(IEnumerable <IEnumerable <ModificationWithMassAndCf> > enumerable)
        {
            ChemicalFormula f = new ChemicalFormula();
            {
                var firstEnum = enumerable.First();
                foreach (var mod in firstEnum)
                {
                    if (mod == null)
                    {
                        return("unknown", null);
                    }
                    f.Add(mod.chemicalFormula);
                }
            }
            bool equals = true;
            List <ChemicalFormula> formulas = new List <ChemicalFormula>();

            foreach (var anEnum in enumerable)
            {
                ChemicalFormula fhere = new ChemicalFormula();
                foreach (var mod in anEnum)
                {
                    if (mod == null)
                    {
                        return("unknown", null);
                    }
                    fhere.Add(mod.chemicalFormula);
                }
                if (!f.Equals(fhere))
                {
                    equals = false;
                }
                formulas.Add(fhere);
            }
            if (!equals)
            {
                var returnString = GlobalVariables.CheckLengthOfOutput(string.Join("|", formulas.Select(b => b.Formula)));
                return(returnString, null);
            }
            else
            {
                return(f.Formula, f);
            }
        }
示例#8
0
        public void PsiModIsotope()
        {
            var parser = new PsiModOboParser();

            PsiModTerm[] modifications = parser.Parse(PsiModOboParserTest.GetFilePath()).ToArray();

            PsiModTerm psiMod402 = modifications.Single(x => x.Id == "MOD:00402");
            IProteoformModificationLookup psiModLookup = PsiModModificationLookup.CreateFromModifications(new[] { psiMod402 },
                                                                                                          _elementProvider);

            this.FindById(psiModLookup, ProFormaKey.Identifier, ProFormaEvidenceType.PsiMod, 402, "MOD:");

            var mod = psiModLookup.GetModification(new ProFormaDescriptor(ProFormaKey.Identifier,
                                                                          ProFormaEvidenceType.PsiMod, "MOD:00402"));

            ChemicalFormula chemicalFormula = new ChemicalFormula(
                new IEntityCardinality <IElement>[]
            {
                new EntityCardinality <IElement>(_elementProvider.GetElement("C"), 22),
                new EntityCardinality <IElement>(_elementProvider.GetElement("H", 1), 30),
                new EntityCardinality <IElement>(_elementProvider.GetElement("H", 2), 8),
                new EntityCardinality <IElement>(_elementProvider.GetElement("N"), 4),
                new EntityCardinality <IElement>(_elementProvider.GetElement("O"), 6),
                new EntityCardinality <IElement>(_elementProvider.GetElement("S"), 1),
            });

            Assert.IsInstanceOf(typeof(IHasChemicalFormula), mod);
            var formulaMod = (IHasChemicalFormula)mod;

            Assert.IsTrue(chemicalFormula.Equals(formulaMod.GetChemicalFormula()));

            Assert.IsInstanceOf(typeof(IIdentifiable), mod);
            var idMod = (IIdentifiable)mod;

            Assert.AreEqual("MOD:00402", idMod.Id);
            Assert.AreEqual("Gygi ICAT(TM) d8 modified cysteine", idMod.Name);
        }
示例#9
0
        public void EqualsFalse()
        {
            ChemicalFormula formulaA = new ChemicalFormula("OCHHCHN");

            Assert.IsFalse(formulaA.Equals("C"));
        }
示例#10
0
        public void TestEquality()
        {
            ChemicalFormula formulaB = new ChemicalFormula("CO");

            Assert.IsTrue(formulaB.Equals(formulaB));
        }
示例#11
0
        public static void EqualsFalse()
        {
            ChemicalFormula formulaA = ChemicalFormula.ParseFormula("OCHHCHN");

            Assert.IsFalse(formulaA.Equals("C"));
        }
示例#12
0
        /// <summary>
        /// Basic overview of how chemical formulas can be used and modified
        /// </summary>
        private static void ChemicalFormulaExamples()
        {
            Console.WriteLine("**Chemical Formula Examples**");

            // Simple chemical formula creation
            ChemicalFormula formula1 = new ChemicalFormula("C2H3NO");

            WriteFormulaToConsole(formula1);


            // Input order does not matter
            ChemicalFormula formula2 = new ChemicalFormula("NH3C2O");

            WriteFormulaToConsole(formula2);

            // Formulas are identicial if they have the exact same type of elements and count
            Console.WriteLine("Are {0} and {1} equivalent? {2}", formula1, formula2, formula1.Equals(formula2));

            // You can modify exisiting chemical formulas in many ways.
            // You can add a chemical formula to a chemical formula
            formula1.Add(formula2);
            WriteFormulaToConsole(formula1);
            Console.WriteLine("Are {0} and {1} equivalent? {2}", formula1, formula2, formula1.Equals(formula2));

            // You can completely remove an element from a chemical formula
            formula1.Remove("C");
            WriteFormulaToConsole(formula1);

            // Even negative values are possible if not physically possible
            formula1.Remove(formula2);
            WriteFormulaToConsole(formula1);

            // Implicit arithmetic is also possible (supports +, -, and *)
            ChemicalFormula formula3 = formula2 - formula1;

            WriteFormulaToConsole(formula3);
            ChemicalFormula formula4 = formula3 + formula1;

            WriteFormulaToConsole(formula4);
            ChemicalFormula formula5 = formula2 * 5;

            WriteFormulaToConsole(formula5);

            // Formulas consist of a dictionary of isotopes and count, and by default, the most common (abundant) isotope of an element
            // is included (i.e. Carbon 12 instead of Carbon 13). You can explicitly state that you want another isotope in a chemical formula
            // by this notation: <Element Symbol>{<Mass Number>} (e.g. C{13}, N{15}, etc..)

            formula1 = new ChemicalFormula("C2C{13}2H3NO");
            formula2 = new ChemicalFormula("C4H3NO");


            WriteFormulaToConsole(formula1);
            WriteFormulaToConsole(formula2);

            Console.WriteLine("Are {0} and {1} equivalent? {2}", formula1, formula2, formula1.Equals(formula2));

            // No need to specify the mass number of the most abundant isotope for an element
            formula3 = new ChemicalFormula("C{12}2C2H3NO");
            formula4 = new ChemicalFormula("C4H3NO");
            Console.WriteLine("Are {0} and {1} equivalent? {2}", formula3, formula4, formula3.Equals(formula4));
        }
示例#13
0
        public void IsotopesTest()
        {
            var      provider = new MockElementProvider();
            IElement h        = provider.GetElement(1);
            IElement h1       = provider.GetElement(1, 1);
            IElement h2       = provider.GetElement(1, 2);

            // These two are equal.
            ChemicalFormula formula0 = new ChemicalFormula(new[]
            {
                new EntityCardinality <IElement>(h, 2)
            });
            ChemicalFormula formula1 = new ChemicalFormula(new[]
            {
                new EntityCardinality <IElement>(h, 2)
            });

            ChemicalFormula formula2 = new ChemicalFormula(new[]
            {
                new EntityCardinality <IElement>(h1, 2)
            });

            // These two are equal.
            ChemicalFormula formula3 = new ChemicalFormula(new[]
            {
                new EntityCardinality <IElement>(h, 1),
                new EntityCardinality <IElement>(h1, 1)
            });
            ChemicalFormula formula4 = new ChemicalFormula(new[]
            {
                new EntityCardinality <IElement>(h1, 1),
                new EntityCardinality <IElement>(h, 1)
            });

            ChemicalFormula formula5 = new ChemicalFormula(new[]
            {
                new EntityCardinality <IElement>(h1, 1),
                new EntityCardinality <IElement>(h2, 1)
            });

            ChemicalFormula[] formulas = new[]
            {
                formula0, formula1, formula2, formula3, formula4, formula5,
            };

            for (int i = 0; i < 6; i++)
            {
                for (int j = i; j < 6; j++)
                {
                    ChemicalFormula fi = formulas[i];
                    ChemicalFormula fj = formulas[j];

                    if (
                        i == j ||
                        (i == 0 && j == 1) ||
                        (i == 3 && j == 4)
                        )
                    {
                        Assert.IsTrue(fi.Equals(fj));
                    }
                    else
                    {
                        Assert.IsFalse(fi.Equals(fj));
                    }
                }
            }
        }