コード例 #1
0
    /// <summary>
    /// Adds molecular formula into a list.
    /// </summary>
    /// <param name="formulas">The molecular fomula collection.</param>
    /// <param name="value">A molecular formula instance to add.</param>
    /// <param name="count">The count of the value.</param>
    /// <param name="another">Another optional molecular formula instance to add.</param>
    /// <param name="countForAnother">The count of another.</param>
    /// <param name="other">The optional third molecular formula instance to add</param>
    /// <param name="countForLast">The count of the third molecular formula instance to add.</param>
    public static void Add(this ICollection <MolecularFormula> formulas, MolecularFormula value, int count, MolecularFormula another = null, int countForAnother = 1, MolecularFormula other = null, int countForLast = 1)
    {
        if (formulas is null)
        {
            return;
        }
        if (value != null)
        {
            for (var i = 0; i < count; i++)
            {
                formulas.Add(value);
            }
        }

        if (another != null)
        {
            for (var i = 0; i < countForAnother; i++)
            {
                formulas.Add(another);
            }
        }

        if (other != null)
        {
            for (var i = 0; i < countForLast; i++)
            {
                formulas.Add(other);
            }
        }
    }
コード例 #2
0
 public clsAveragine(clsAveragine a)
 {
     _useTag           = a._useTag;
     _tagMass          = a._tagMass;
     _averagineFormula = new MolecularFormula(a._averagineFormula);
     _tagFormula       = a._tagFormula;
 }
コード例 #3
0
        public void TestParseFormulaNew()
        {
            string formula = "C4.9384 H7.7583 N1.3577 O1.4773 S0.0417";
            //string formula = "Ct H7.7583 N1.3577 O1.4773 S0.0417";
            var elements = new clsElementIsotopes();
            //var molecule = new MolecularFormula(formula, elements);
            var molecule = new MolecularFormula();

            molecule.SetMolecularFormula(formula, elements);

            var results  = molecule.ElementalComposition;
            var carbon   = results[0];
            var hydrogen = results[1];
            var nitrogen = results[2];
            var oxygen   = results[3];
            var sulfur   = results[4];

            Assert.AreEqual(carbon.Index, 5);
            Assert.AreEqual(carbon.NumCopies, 4.9384);
            Assert.AreEqual(hydrogen.Index, 0);
            Assert.AreEqual(hydrogen.NumCopies, 7.7583);
            Assert.AreEqual(nitrogen.Index, 6);
            Assert.AreEqual(nitrogen.NumCopies, 1.3577);
            Assert.AreEqual(oxygen.Index, 7);
            Assert.AreEqual(oxygen.NumCopies, 1.4773);
            Assert.AreEqual(sulfur.Index, 15);
            Assert.AreEqual(sulfur.NumCopies, 0.0417);
        }
コード例 #4
0
        public void TestRemove_IMolecularFormulaSet_MolecularFormulaRange()
        {
            var formulaRange = new MolecularFormulaRange();

            formulaRange.AddIsotope(builder.NewIsotope("C"), 0, 4);
            formulaRange.AddIsotope(builder.NewIsotope("H"), 0, 12);
            formulaRange.AddIsotope(builder.NewIsotope("N"), 0, 2);

            var mf2 = new MolecularFormula();

            mf2.Add(builder.NewIsotope("C"), 1);
            mf2.Add(builder.NewIsotope("H"), 11);
            mf2.Add(builder.NewIsotope("N"), 1);

            var mf1 = new MolecularFormula();

            mf1.Add(builder.NewIsotope("C"), 3);
            mf1.Add(builder.NewIsotope("H"), 10);

            var formulaSet = new MolecularFormulaSet
            {
                mf1,
                mf2
            };

            var newMFSet = MolecularFormulaSetManipulator.Remove(formulaSet, formulaRange);

            /* the mf2 is excluded from the limits. It doesn't contain N */

            Assert.AreEqual(2, newMFSet.Count());
        }
コード例 #5
0
        public override void TestValidate_IMolecularFormula()
        {
            var formula = new MolecularFormula();

            formula.Add(ifac.GetMajorIsotope("C"), 5);
            formula.Add(ifac.GetMajorIsotope("H"), 13);
            formula.Add(ifac.GetMajorIsotope("N"), 2);
            formula.Add(ifac.GetMajorIsotope("O"), 2);
            formula.Charge = 0;

            // experimental results

            var spectrum = new List <double[]>
            {
                new double[] { 133.0977, 100.00 },
                new double[] { 134.09475, 0.6 },
                new double[] { 134.1010, 5.4 }
            };

            var rule       = new IsotopePatternRule();
            var parameters = new object[2];

            parameters[0]   = spectrum;
            parameters[1]   = 0.001;
            rule.Parameters = parameters;

            Assert.AreEqual(0.9433, rule.Validate(formula), 0.001);
        }
コード例 #6
0
        public void TestRemove_IMolecularFormulaSet_IMolecularFormula_IMolecularFormula()
        {
            var formulaMin = new MolecularFormula();

            formulaMin.Add(builder.NewIsotope("C"), 1);
            formulaMin.Add(builder.NewIsotope("H"), 1);
            formulaMin.Add(builder.NewIsotope("O"), 1);

            var formulaMax = new MolecularFormula();

            formulaMax.Add(builder.NewIsotope("C"), 4);
            formulaMax.Add(builder.NewIsotope("H"), 12);
            formulaMax.Add(builder.NewIsotope("N"), 2);

            var mf1 = new MolecularFormula();

            mf1.Add(builder.NewIsotope("C"), 3);
            mf1.Add(builder.NewIsotope("H"), 10);
            mf1.Add(builder.NewIsotope("N"), 1);

            var formulaSet = new MolecularFormulaSet {
                mf1
            };
            var newMFSet = MolecularFormulaSetManipulator.Remove(formulaSet, formulaMin, formulaMax);

            Assert.IsNull(newMFSet);
        }
コード例 #7
0
        public void TestRemove_1()
        {
            var formulaMin = new MolecularFormula();

            formulaMin.Add(builder.NewIsotope("C"), 1);
            formulaMin.Add(builder.NewIsotope("H"), 1);
            formulaMin.Add(builder.NewIsotope("N"), 1);

            var formulaMax = new MolecularFormula();

            formulaMax.Add(builder.NewIsotope("C"), 4);
            formulaMax.Add(builder.NewIsotope("H"), 12);
            formulaMax.Add(builder.NewIsotope("N"), 2);

            var mf1 = new MolecularFormula();

            mf1.Add(builder.NewIsotope("C"), 3);
            mf1.Add(builder.NewIsotope("H"), 10);
            mf1.Add(builder.NewIsotope("N"), 1);

            var mf2 = new MolecularFormula();

            mf2.Add(builder.NewIsotope("C"), 1);
            mf2.Add(builder.NewIsotope("H"), 1);
            mf2.Add(builder.NewIsotope("N"), 1);

            var mf3 = new MolecularFormula();

            mf3.Add(builder.NewIsotope("C"), 4);
            mf3.Add(builder.NewIsotope("H"), 12);
            mf3.Add(builder.NewIsotope("N"), 2);

            var mf4 = new MolecularFormula();

            mf4.Add(builder.NewIsotope("C"), 7);
            mf4.Add(builder.NewIsotope("H"), 10);
            mf4.Add(builder.NewIsotope("N"), 1);

            var formulaSet = new MolecularFormulaSet
            {
                mf1,
                mf2,
                mf3,
                mf4
            };

            var newMFSet = MolecularFormulaSetManipulator.Remove(formulaSet, formulaMin, formulaMax);

            /* the mf4 is excluded from the limits */

            Assert.AreEqual(3, newMFSet.Count());
            Assert.AreEqual(MolecularFormulaManipulator.GetString(mf1),
                            MolecularFormulaManipulator.GetString(newMFSet[0]));
            Assert.AreEqual(MolecularFormulaManipulator.GetString(mf2),
                            MolecularFormulaManipulator.GetString(newMFSet[1]));
            Assert.AreEqual(MolecularFormulaManipulator.GetString(mf3),
                            MolecularFormulaManipulator.GetString(newMFSet[2]));
        }
コード例 #8
0
ファイル: CML2WriterTest.cs プロジェクト: roddickchen/NCDK
        public void TestReactionSchemeWithFormula()
        {
            var writer  = new StringWriter();
            var scheme1 = builder.NewReactionScheme();

            scheme1.Id = "rs0";

            var reaction = builder.NewReaction();

            reaction.Id = "r1";
            var moleculeA = reaction.Builder.NewAtomContainer();

            moleculeA.Id = "A";
            var formula = new MolecularFormula();

            formula.Add(reaction.Builder.NewIsotope("C"), 10);
            formula.Add(reaction.Builder.NewIsotope("H"), 15);
            formula.Add(reaction.Builder.NewIsotope("N"), 2);
            formula.Add(reaction.Builder.NewIsotope("O"), 1);
            moleculeA.SetProperty(CDKPropertyName.Formula, formula);
            var moleculeB = reaction.Builder.NewAtomContainer();

            moleculeB.Id = "B";
            reaction.Reactants.Add(moleculeA);
            reaction.Products.Add(moleculeB);

            scheme1.Add(reaction);

            var reaction2 = reaction.Builder.NewReaction();

            reaction2.Id = "r2";
            var moleculeC = reaction.Builder.NewAtomContainer();

            moleculeC.Id = "C";
            reaction2.Reactants.Add(moleculeB);
            reaction2.Products.Add(moleculeC);

            scheme1.Add(reaction2);

            using (var cmlWriter = new CMLWriter(writer))
            {
                cmlWriter.Write(scheme1);
            }
            var cmlContent = writer.ToString();

            Debug.WriteLine("****************************** TestReactionCustomization()");
            Debug.WriteLine(cmlContent);
            Debug.WriteLine("******************************");
            Assert.IsTrue(cmlContent.IndexOf("<reactionScheme id=\"rs0") != -1);
            Assert.IsTrue(cmlContent.IndexOf("<reaction id=\"r1") != -1);
            Assert.IsTrue(cmlContent.IndexOf("<reaction id=\"r2") != -1);
            Assert.IsTrue(cmlContent.IndexOf("<molecule id=\"A") != -1);
            Assert.IsTrue(cmlContent.IndexOf("<formula concise=") != -1);
            Assert.IsTrue(cmlContent.IndexOf("<molecule id=\"B") != -1);
            Assert.IsTrue(cmlContent.IndexOf("<molecule id=\"C") != -1);
        }
コード例 #9
0
 public override int GetHashCode()
 {
     unchecked
     {
         var hashCode = CID;
         hashCode = (hashCode * 397) ^ (MolecularFormula != null ? MolecularFormula.GetHashCode() : 0);
         hashCode = (hashCode * 397) ^ (InChIKey != null ? InChIKey.GetHashCode() : 0);
         hashCode = (hashCode * 397) ^ ExactMass.GetHashCode();
         return(hashCode);
     }
 }
コード例 #10
0
        public void TestDefaultValidTrue()
        {
            IRule rule = new MMElementRule();

            var formula = new MolecularFormula();

            formula.Add(builder.NewIsotope("C"), 2);
            formula.Add(builder.NewIsotope("H"), 6);

            Assert.AreEqual(1.0, rule.Validate(formula), 0.0001);
        }
コード例 #11
0
        public void TestDefaultValidFalseSetParam()
        {
            IRule rule    = new ChargeRule();
            var   formula = new MolecularFormula();

            formula.Add(builder.NewIsotope("C"), 2);
            formula.Add(builder.NewIsotope("H"), 200);
            formula.Charge  = 1;
            rule.Parameters = new object[] { -1.0 };

            Assert.AreEqual(0.0, rule.Validate(formula), 0.0001);
        }
コード例 #12
0
ファイル: FormulaRuleTest.cs プロジェクト: roddickchen/NCDK
        public virtual void TestValidate_IMolecularFormula()
        {
            var rule             = GetRule();
            IMolecularFormula mf = new MolecularFormula();

            mf.Add(new Isotope("C", 13));
            mf.Add(new Isotope("H", 2), 4);
            rule.Validate(mf);

            // can it handle an empty MF?
            rule.Validate(new MolecularFormula());
        }
コード例 #13
0
        public void TestDefaultValidFalse()
        {
            IRule rule = new ToleranceRangeRule();

            var      formula = new MolecularFormula();
            IIsotope carb    = builder.NewIsotope("C");

            carb.ExactMass = 12.00;
            IIsotope cl = builder.NewIsotope("Cl");

            cl.ExactMass = 34.96885268;
            formula.Add(carb);
            formula.Add(cl);

            Assert.AreEqual(0.0, rule.Validate(formula), 0.0001);
        }
コード例 #14
0
ファイル: ElementRuleTest.cs プロジェクト: roddickchen/NCDK
        public void TestDefaultValidFalseSetParam()
        {
            IRule rule = new ElementRule();

            var formula = new MolecularFormula();

            formula.Add(builder.NewIsotope("C"), 2);
            formula.Add(builder.NewIsotope("H"), 6);

            MolecularFormulaRange mfRange = new MolecularFormulaRange();

            mfRange.AddIsotope(builder.NewIsotope("C"), 1, 2);
            mfRange.AddIsotope(builder.NewIsotope("H"), 1, 2);

            rule.Parameters = new object[] { mfRange };

            Assert.AreEqual(0.0, rule.Validate(formula), 0.0001);
        }
コード例 #15
0
        public void TestGetMaximalFormula_MolecularFormulaRange_IChemObjectBuilder()
        {
            var mf1 = new MolecularFormula(); /* C4H12NO4 */

            mf1.Add(builder.NewIsotope("C"), 4);
            mf1.Add(builder.NewIsotope("H"), 12);
            mf1.Add(builder.NewIsotope("N"), 1);
            mf1.Add(builder.NewIsotope("O"), 4);

            var mf2 = new MolecularFormula(); /* C7H20N4O2 */

            mf2.Add(builder.NewIsotope("C"), 7);
            mf2.Add(builder.NewIsotope("H"), 20);
            mf2.Add(builder.NewIsotope("N"), 4);
            mf2.Add(builder.NewIsotope("O"), 2);

            var mf3 = new MolecularFormula(); /* C9H5O7 */

            mf3.Add(builder.NewIsotope("C"), 9);
            mf3.Add(builder.NewIsotope("H"), 5);
            mf3.Add(builder.NewIsotope("O"), 7);

            var mfSet = new MolecularFormulaSet
            {
                mf1,
                mf2,
                mf3
            };

            MolecularFormulaRange mfRange = MolecularFormulaRangeManipulator.GetRange(mfSet);
            IMolecularFormula     formula = MolecularFormulaRangeManipulator.GetMaximalFormula(mfRange, builder);

            /* Result: C4-9H5-20N0-4O2-7 */

            Assert.AreEqual(4, mfRange.GetIsotopes().Count());
            Assert.AreEqual(formula.GetCount(builder.NewIsotope("C")),
                            mfRange.GetIsotopeCountMax(builder.NewIsotope("C")));
            Assert.AreEqual(formula.GetCount(builder.NewIsotope("H")),
                            mfRange.GetIsotopeCountMax(builder.NewIsotope("H")));
            Assert.AreEqual(formula.GetCount(builder.NewIsotope("N")),
                            mfRange.GetIsotopeCountMax(builder.NewIsotope("N")));
            Assert.AreEqual(formula.GetCount(builder.NewIsotope("O")),
                            mfRange.GetIsotopeCountMax(builder.NewIsotope("O")));
        }
コード例 #16
0
        public void TestGetRange_IMolecularFormulaSet()
        {
            var mf1 = new MolecularFormula(); /* C4H12NO4 */

            mf1.Add(builder.NewIsotope("C"), 4);
            mf1.Add(builder.NewIsotope("H"), 12);
            mf1.Add(builder.NewIsotope("N"), 1);
            mf1.Add(builder.NewIsotope("O"), 4);

            var mf2 = new MolecularFormula(); /* C7H20N4O2 */

            mf2.Add(builder.NewIsotope("C"), 7);
            mf2.Add(builder.NewIsotope("H"), 20);
            mf2.Add(builder.NewIsotope("N"), 4);
            mf2.Add(builder.NewIsotope("O"), 2);

            var mf3 = new MolecularFormula(); /* C9H5O7 */

            mf3.Add(builder.NewIsotope("C"), 9);
            mf3.Add(builder.NewIsotope("H"), 5);
            mf3.Add(builder.NewIsotope("O"), 7);

            var mfSet = new MolecularFormulaSet
            {
                mf1,
                mf2,
                mf3
            };

            MolecularFormulaRange mfRange = MolecularFormulaRangeManipulator.GetRange(mfSet);

            /* Result: C4-9H5-20N0-4O2-7 */

            Assert.AreEqual(4, mfRange.GetIsotopes().Count());
            Assert.AreEqual(4, mfRange.GetIsotopeCountMin(builder.NewIsotope("C")));
            Assert.AreEqual(9, mfRange.GetIsotopeCountMax(builder.NewIsotope("C")));
            Assert.AreEqual(5, mfRange.GetIsotopeCountMin(builder.NewIsotope("H")));
            Assert.AreEqual(20, mfRange.GetIsotopeCountMax(builder.NewIsotope("H")));
            Assert.AreEqual(0, mfRange.GetIsotopeCountMin(builder.NewIsotope("N")));
            Assert.AreEqual(4, mfRange.GetIsotopeCountMax(builder.NewIsotope("N")));
            Assert.AreEqual(2, mfRange.GetIsotopeCountMin(builder.NewIsotope("O")));
            Assert.AreEqual(7, mfRange.GetIsotopeCountMax(builder.NewIsotope("O")));
        }
コード例 #17
0
        public void TestIsTheSame_IIsotope_IIsotope()
        {
            MolecularFormula mf          = new MolecularFormula();
            IIsotope         carb        = Builder.NewIsotope("C");
            IIsotope         anotherCarb = Builder.NewIsotope("C");
            IIsotope         h           = Builder.NewIsotope("H");

            carb.ExactMass        = 12.0;
            anotherCarb.ExactMass = 12.0;
            h.ExactMass           = 1.0;

            carb.Abundance        = 34.0;
            anotherCarb.Abundance = 34.0;
            h.Abundance           = 99.0;

            Assert.IsTrue(MolecularFormula.IsTheSame(carb, carb));
            Assert.IsTrue(MolecularFormula.IsTheSame(carb, anotherCarb));
            Assert.IsFalse(MolecularFormula.IsTheSame(carb, h));
        }
コード例 #18
0
        public void TestContains_IMolecularFormulaSet_IMolecularFormula()
        {
            var mf1 = new MolecularFormula();

            mf1.Add(builder.NewIsotope("C"), 4);
            mf1.Add(builder.NewIsotope("H"), 12);
            mf1.Add(builder.NewIsotope("N"), 1);
            mf1.Add(builder.NewIsotope("O"), 4);

            var mf3 = new MolecularFormula();

            mf3.Add(builder.NewIsotope("C"), 9);
            mf3.Add(builder.NewIsotope("H"), 5);
            mf3.Add(builder.NewIsotope("O"), 7);

            var formulaSet = new MolecularFormulaSet
            {
                mf1,
                mf3
            };

            var mf2 = new MolecularFormula();

            mf2.Add(builder.NewIsotope("C"), 4);
            mf2.Add(builder.NewIsotope("H"), 12);
            mf2.Add(builder.NewIsotope("N"), 1);
            mf2.Add(builder.NewIsotope("O"), 4);

            var mf4 = new MolecularFormula();

            mf4.Add(builder.NewIsotope("C"), 4);
            var hyd = builder.NewIsotope("H");

            hyd.ExactMass = 2.0032342;
            mf4.Add(hyd, 12);
            mf4.Add(builder.NewIsotope("N"), 1);
            mf4.Add(builder.NewIsotope("O"), 4);

            Assert.IsTrue(MolecularFormulaSetManipulator.Contains(formulaSet, mf2));
            Assert.IsFalse(MolecularFormulaSetManipulator.Contains(formulaSet, mf4));
        }
コード例 #19
0
        public void TestDefaultValidFalse_SetParam()
        {
            IRule rule = new ToleranceRangeRule();

            var      formula = new MolecularFormula();
            IIsotope carb    = builder.NewIsotope("C");

            carb.ExactMass = 12.00;
            IIsotope cl = builder.NewIsotope("Cl");

            cl.ExactMass = 34.96885268;
            formula.Add(carb);
            formula.Add(cl);

            object[] parameters = new object[2];
            parameters[0]   = 46.0; // real -> 46.96885268
            parameters[1]   = 0.00005;
            rule.Parameters = parameters;

            Assert.AreEqual(0.0, rule.Validate(formula), 0.0001);
        }
コード例 #20
0
        public void TestRemove_3()
        {
            var formulaMin = new MolecularFormula();

            formulaMin.Add(builder.NewIsotope("C"), 1);
            formulaMin.Add(builder.NewIsotope("H"), 1);
            formulaMin.Add(builder.NewIsotope("N"), 1);

            var formulaMax = new MolecularFormula();

            formulaMax.Add(builder.NewIsotope("C"), 4);
            formulaMax.Add(builder.NewIsotope("H"), 12);
            formulaMax.Add(builder.NewIsotope("N"), 2);

            var mf1 = new MolecularFormula();

            mf1.Add(builder.NewIsotope("C"), 3);
            mf1.Add(builder.NewIsotope("H"), 10);
            mf1.Add(builder.NewIsotope("N"), 1);

            var mf2 = new MolecularFormula();

            mf2.Add(builder.NewIsotope("C"), 1);
            mf2.Add(builder.NewIsotope("H"), 1);
            mf2.Add(builder.NewIsotope("O"), 1);

            var formulaSet = new MolecularFormulaSet
            {
                mf1,
                mf2
            };

            var newMFSet = MolecularFormulaSetManipulator.Remove(formulaSet, formulaMin, formulaMax);

            /* the mf2 is excluded from the limits. It doesn't contain N */

            Assert.AreEqual(1, newMFSet.Count());
            Assert.AreEqual(MolecularFormulaManipulator.GetString(mf1),
                            MolecularFormulaManipulator.GetString(newMFSet[0]));
        }
コード例 #21
0
        public void TestGetMinOccurrenceElements_IMolecularFormulaSet()
        {
            var mf1 = new MolecularFormula(); /* C4H12NO4 */

            mf1.Add(builder.NewIsotope("C"), 4);
            mf1.Add(builder.NewIsotope("H"), 12);
            mf1.Add(builder.NewIsotope("N"), 1);
            mf1.Add(builder.NewIsotope("O"), 4);

            var mf2 = new MolecularFormula(); /* C7H20N4O2 */

            mf2.Add(builder.NewIsotope("C"), 7);
            mf2.Add(builder.NewIsotope("H"), 20);
            mf2.Add(builder.NewIsotope("N"), 4);
            mf2.Add(builder.NewIsotope("O"), 2);

            var mf3 = new MolecularFormula(); /* C9H5O7 */

            mf3.Add(builder.NewIsotope("C"), 9);
            mf3.Add(builder.NewIsotope("H"), 5);
            mf3.Add(builder.NewIsotope("O"), 7);

            var mfSet = new MolecularFormulaSet
            {
                mf1,
                mf2,
                mf3
            };

            var molecularFormula = MolecularFormulaSetManipulator.GetMinOccurrenceElements(mfSet);

            /* Result: C4H5NO2 */

            Assert.AreEqual(12, MolecularFormulaManipulator.GetAtomCount(molecularFormula));
            Assert.AreEqual(4, molecularFormula.Isotopes.Count());
            Assert.AreEqual(4, molecularFormula.GetCount(builder.NewIsotope("C")));
            Assert.AreEqual(5, molecularFormula.GetCount(builder.NewIsotope("H")));
            Assert.AreEqual(1, molecularFormula.GetCount(builder.NewIsotope("N")));
            Assert.AreEqual(2, molecularFormula.GetCount(builder.NewIsotope("O")));
        }
コード例 #22
0
        public void CalculateMasses(MolecularFormula formula)
        {
            MonoMw    = 0;
            AverageMw = 0;
            var numElementsFound = formula.NumElements;

            for (var j = 0; j < numElementsFound; j++)
            {
                var elementIndex = formula.ElementalComposition[j].Index;
                var atomicity    = (int)formula.ElementalComposition[j].NumCopies;

                if (atomicity == 0)
                {
                    continue;
                }
                //int numIsotopes = ElementalIsotopeComposition.ElementalIsotopesList[elementIndex].NumberOfIsotopes;
                var monoMw = ElementalIsotopeComposition.ElementalIsotopesList[elementIndex].Isotopes[0].Mass;
                var avgMw  = ElementalIsotopeComposition.ElementalIsotopesList[elementIndex].AverageMass;
                MonoMw    += atomicity * monoMw;
                AverageMw += atomicity * avgMw;
            }
        }
コード例 #23
0
        public void CalcVariancesAndMassRange(int charge, MolecularFormula formula)
        {
            MassVariance = 0;
            var numElementsFound = formula.NumElements;

            for (var elementNum = 0; elementNum < numElementsFound; elementNum++)
            {
                var elementIndex      = formula.ElementalComposition[elementNum].Index;
                var atomicity         = (int)formula.ElementalComposition[elementNum].NumCopies;
                var elementalVariance = ElementalIsotopeComposition.ElementalIsotopesList[elementIndex].MassVariance;
                MassVariance += elementalVariance * atomicity;
            }

            if (charge == 0)
            {
                _massRange = (int)(Math.Sqrt(1 + MassVariance) * 10);
            }
            else
            {
                _massRange = (int)(Math.Sqrt(1 + MassVariance) * 10.0 / charge);
            }
            /* +/- 5 sd's : Multiply charged */

            /* Set to nearest (upper) power of 2 */
            for (var i = 1024; i > 0; i /= 2)
            {
                if (i < _massRange)
                {
                    _massRange = i * 2;
                    i          = 0;
                }
            }
            if (_massRange <= 0)
            {
                _massRange = 1;
            }
        }
コード例 #24
0
        public void TestValidBromine()
        {
            var spectrum = new List <double[]>
            {
                new double[] { 157.8367, 51.399 },
                new double[] { 159.8346, 100.00 },
                new double[] { 161.8326, 48.639 }
            };

            var rule       = new IsotopePatternRule();
            var parameters = new object[2];

            parameters[0]   = spectrum;
            parameters[1]   = 0.001;
            rule.Parameters = parameters;

            var formula = new MolecularFormula();

            formula.Add(ifac.GetMajorIsotope("C"), 2);
            formula.Add(ifac.GetMajorIsotope("Br"), 2);
            formula.Charge = 0;

            Assert.AreEqual(0.0, rule.Validate(formula), 0.0001);
        }
コード例 #25
0
		public static IMolecularFormula GetMolecularFormula(IAtomContainer atomContainer)
		{
			var formula = new MolecularFormula();
			var charge = 0;
			var hydrogen = new Atom("H");

			foreach (var iAtom in atomContainer.atoms().ToWindowsEnumerable<IAtom>())
			{
				formula.addIsotope(iAtom);
				charge += iAtom.getFormalCharge().intValue();
				var implicitHydrogenCount = iAtom.getImplicitHydrogenCount();
				var implicitHydrogenCountValue = implicitHydrogenCount != null ? implicitHydrogenCount.intValue() : (int?) null;

				if (implicitHydrogenCountValue.HasValue && implicitHydrogenCountValue.Value > 0)
				{
					formula.addIsotope(hydrogen, implicitHydrogenCountValue.Value);
				}
			}

			formula.setCharge(new Integer(charge));
			return formula;
		}
コード例 #26
0
ファイル: ElementTest.cs プロジェクト: nuscien/trivial
        public void TestMolecularFormula()
        {
            var s = "NonExist";

            Assert.IsNull(MolecularFormula.TryParse(s));

            s = "H2CO3";
            var m = MolecularFormula.Parse(s);

            Assert.AreEqual(s.Length, m.ToString().Length);
            s = m.ToString();
            Assert.IsTrue(s.Contains("CO"));
            Assert.AreEqual(6, m.Count);
            Assert.AreEqual(0, m.IndexOf("H"));
            Assert.AreEqual(-1, m.IndexOf("H", 2));
            Assert.AreEqual(1, m.IndexOf(ChemicalElement.C));
            Assert.AreEqual(1, m.IndexOf(ChemicalElement.C, 1));
            Assert.AreEqual(1, m.GetCount("C"));
            Assert.AreEqual(3, m.GetCount(ChemicalElement.O));
            Assert.IsFalse(m.IsIon);
            m = MolecularFormula.Parse(s);
            Assert.AreEqual(s, m.ToString());
            Assert.AreEqual(6, m.Count);
            m = new MolecularFormula(m.Items);
            Assert.AreEqual(s, m.ToString());

            s = "SO₄²ˉ";
            m = MolecularFormula.Parse(s);
            Assert.AreEqual(s, m.ToString());
            Assert.IsTrue(m.IsIon);
            Assert.AreEqual(-2, m.ChargeNumber);

            m = MolecularFormula.Parse("R-COOH");
            Assert.AreEqual(2, m.GetCount(ChemicalElement.C));
            Assert.AreEqual(2, m.GetCount(ChemicalElement.O));
            Assert.AreEqual(4, m.GetCount(ChemicalElement.H));

            m = ChemicalElement.Fe * 2 + 3 * ChemicalElement.O;
            Assert.AreEqual(2, m.GetCount("Fe"));
            Assert.AreEqual(3, m.GetCount(ChemicalElement.O));
            m = ChemicalElement.C + ChemicalElement.O;
            Assert.AreEqual(1, m.GetCount("C"));
            Assert.AreEqual(1, m.GetCount(ChemicalElement.O));
            m += ChemicalElement.O;
            Assert.AreEqual(1, m.GetCount("C"));
            Assert.AreEqual(2, m.GetCount(ChemicalElement.O));
            m = ChemicalElement.H + m;
            Assert.AreEqual(1, m.GetCount("H"));
            Assert.AreEqual(4, m.Count);

            m = ChemicalElement.H * 2 + ChemicalElement.O;
            var col = new List <MolecularFormula>
            {
                { m, 2, ChemicalElement.Na }
            };

            Assert.AreEqual(3, col.Count);
            Console.WriteLine(MolecularFormula.ToString(col));
            Assert.IsTrue(MolecularFormula.ToString(col).Length > 6);
            col.RemoveAt(2);
            Assert.IsTrue(MolecularFormula.ConservationOfMass(
                              new List <MolecularFormula>
            {
                4 * ChemicalElement.H,
                ChemicalElement.O * 2
            },
                              col));
            Assert.IsFalse(MolecularFormula.ConservationOfMass(
                               new List <MolecularFormula>
            {
                ChemicalElement.C + ChemicalElement.O * 2
            },
                               new List <MolecularFormula>
            {
                ChemicalElement.Na,
                ChemicalElement.Cl
            }));
            Assert.IsFalse(MolecularFormula.ConservationOfMass(
                               new List <MolecularFormula>
            {
                new MolecularFormula(new List <MolecularFormula.Item>
                {
                    new MolecularFormula.Item(ChemicalElement.Cl)
                }, -1)
            },
                               new List <MolecularFormula>
            {
                new MolecularFormula(ChemicalElement.Cl)
            }));
        }
コード例 #27
0
        public void CalcFrequencies(int charge, int numPoints, MolecularFormula formula)
        {
            int    i;
            int    j, k;
            double real, imag, freq, x, theta, r, tempr;

            var numElementsInEntity = formula.NumElements;

            /* Calculate first half of Frequency Domain (+)masses */
            for (i = 1; i <= numPoints / 2; i++)
            {
                freq  = (double)(i - 1) / _massRange;
                r     = 1;
                theta = 0;
                for (j = 0; j < numElementsInEntity; j++)
                {
                    var elementIndex = formula.ElementalComposition[j].Index;
                    var atomicity    = (int)formula.ElementalComposition[j].NumCopies;
                    var numIsotopes  = ElementalIsotopeComposition.ElementalIsotopesList[elementIndex].NumberOfIsotopes;
                    var averageMass  = ElementalIsotopeComposition.ElementalIsotopesList[elementIndex].AverageMass;
                    real = imag = 0.0;
                    for (k = 0; k < numIsotopes; k++)
                    {
                        double wrapFreq         = 0;
                        var    isotopeAbundance =
                            ElementalIsotopeComposition.ElementalIsotopesList[elementIndex].Isotopes[k].Probability;
                        if (numIsotopes > 1)
                        {
                            wrapFreq =
                                ElementalIsotopeComposition.ElementalIsotopesList[elementIndex].Isotopes[k].Mass /
                                charge - averageMass / charge;
                            if (wrapFreq < 0)
                            {
                                wrapFreq += _massRange;
                            }
                        }
                        x     = 2 * Pi * wrapFreq * freq;
                        real += isotopeAbundance * Math.Cos(x);
                        imag += isotopeAbundance * Math.Sin(x);
                    }
                    /* Convert to polar coordinates, r then theta */
                    tempr = Math.Sqrt(real * real + imag * imag);
                    r    *= Math.Pow(tempr, atomicity);
                    if (real > 0)
                    {
                        theta += atomicity * Math.Atan(imag / real);
                    }
                    else if (real < 0)
                    {
                        theta += atomicity * (Math.Atan(imag / real) + Pi);
                    }
                    else if (imag > 0)
                    {
                        theta += atomicity * Pi / 2;
                    }
                    else
                    {
                        theta += atomicity * -Pi / 2;
                    }
                }
                /* Convert back to real:imag coordinates and store */
                _frequencyData[i - 1] = new Complex(r * Math.Cos(theta), r * Math.Sin(theta));
            } /* end for(i) */

            /* Calculate second half of Frequency Domain (-)masses */
            for (i = numPoints / 2 + 1; i <= numPoints; i++)
            {
                freq  = (double)(i - numPoints - 1) / _massRange;
                r     = 1;
                theta = 0;

                for (j = 0; j < numElementsInEntity; j++)
                {
                    var elementIndex = formula.ElementalComposition[j].Index;
                    var atomicity    = (int)formula.ElementalComposition[j].NumCopies;
                    var numIsotopes  = ElementalIsotopeComposition.ElementalIsotopesList[elementIndex].NumberOfIsotopes;
                    var averageMass  = ElementalIsotopeComposition.ElementalIsotopesList[elementIndex].AverageMass;

                    real = imag = 0;
                    for (k = 0; k < numIsotopes; k++)
                    {
                        double wrapFreq         = 0;
                        var    isotopeAbundance =
                            ElementalIsotopeComposition.ElementalIsotopesList[elementIndex].Isotopes[k].Probability;
                        if (numIsotopes > 1)
                        {
                            wrapFreq =
                                ElementalIsotopeComposition.ElementalIsotopesList[elementIndex].Isotopes[k].Mass /
                                charge - averageMass / charge;
                            if (wrapFreq < 0)
                            {
                                wrapFreq += _massRange;
                            }
                        }

                        x     = 2 * Pi * wrapFreq * freq;
                        real += isotopeAbundance * Math.Cos(x);
                        imag += isotopeAbundance * Math.Sin(x);
                    }

                    /* Convert to polar coordinates, r then theta */
                    tempr = Math.Sqrt(real * real + imag * imag);
                    r    *= Math.Pow(tempr, atomicity);
                    if (real > 0)
                    {
                        theta += atomicity * Math.Atan(imag / real);
                    }
                    else if (real < 0)
                    {
                        theta += atomicity * (Math.Atan(imag / real) + Pi);
                    }
                    else if (imag > 0)
                    {
                        theta += atomicity * Pi / 2;
                    }
                    else
                    {
                        theta -= atomicity * Pi / 2;
                    }
                } /* end for(j) */

                /* Convert back to real:imag coordinates and store */
                _frequencyData[i - 1] = new Complex(r * Math.Cos(theta), r * Math.Sin(theta));
            } /* end of for(i) */
        }
コード例 #28
0
        /// <summary>
        ///     Calculate the isotope distribution
        /// </summary>
        /// <param name="charge"></param>
        /// <param name="resolution"></param>
        /// <param name="formula"></param>
        /// <param name="x">mz values of isotopic profile that are above the threshold</param>
        /// <param name="y">intensity values of isotopic profile that are above the threshold</param>
        /// <param name="threshold"></param>
        /// <param name="isotopeMzs">peak top mz's for the peaks of the isotopic profile</param>
        /// <param name="isotopeIntensities">peak top intensities's for the peaks of the isotopic profile</param>
        /// <param name="debug"></param>
        public void CalculateDistribution(int charge, double resolution, MolecularFormula formula, out List <double> x,
                                          out List <double> y, double threshold, out List <double> isotopeMzs, out List <double> isotopeIntensities,
                                          bool debug = false)
        {
            /* Calculate mono and average mass */
            CalculateMasses(formula);
            if (debug)
            {
                Console.Error.WriteLine("MonoMW =" + MonoMw + " AverageMW =" + AverageMw);
            }

            /* Calculate mass range to use based on molecular variance */
            CalcVariancesAndMassRange(charge, formula);
            if (debug)
            {
                Console.Error.WriteLine("Variance =" + MassVariance + " Mass Range =" + _massRange);
            }

            _minMz       = AverageMw / charge + (ChargeCarrierMass - MercuryCache.ElectronMass) - _massRange * 1.0 / 2;
            _maxMz       = _minMz + _massRange;
            PointsPerAmu = MercurySize / _massRange; /* Use maximum of 2048 real, 2048 imag points */

            // calculate Ap_subscript to from requested Res
            double aPSubscript;

            if (charge == 0)
            {
                aPSubscript = AverageMw / resolution * MercurySize * 2.0 / _massRange;
            }
            else
            {
                aPSubscript = AverageMw / (resolution * Math.Abs(charge)) * MercurySize * 2.0 / _massRange;
            }

            /* Allocate memory for Axis arrays */
            var numPoints = _massRange * PointsPerAmu;

            _frequencyData = new Complex[numPoints];

            if (charge == 0)
            {
                charge = 1;
            }

            if (debug)
            {
                Console.Error.WriteLine("MINMZ = " + _minMz + " MAXMZ = " + _maxMz);
                Console.Error.WriteLine("Num Points per AMU = " + PointsPerAmu);
            }

            CalcFrequencies(charge, numPoints, formula);

            /* Apodize data */
            double apResolution = 1; /* Resolution used in apodization. Not used yet */

            Apodize(numPoints, apResolution, aPSubscript);

            Fourier.Inverse(_frequencyData, FourierOptions.NumericalRecipes);
            // myers changes this line to Realft(FreqData,NumPoints,-1);

            /*
             *  [gord] 'OutputData' fills 'x', 'y'
             *  [gord] x = mz values of isotopic profile that are above the threshold
             *  [gord] y = intensity values of isotopic profile that are above the threshold
             *  [gord] isotopeMzs = peak top mz's for the peaks of the isotopic profile
             *  [gord] isotopeIntensities = peak top intensities's for the peaks of the isotopic profile
             */
            OutputData(numPoints, charge, out x, out y, threshold, out isotopeMzs, out isotopeIntensities);

            //NormalizeToPercentIons(vect_y);
        }
コード例 #29
0
        public void TestMolecularFormula()
        {
            IMolecularFormula mf = new MolecularFormula();

            Assert.IsNotNull(mf);
        }
コード例 #30
0
 private void reportFormula(StringBuilder sb, MolecularFormula formula)
 {
     sb.Append(formula.ToFormulaString());
     sb.Append("\n");
 }
コード例 #31
0
        /// <summary>
        ///     calculates the fit score for a peak against a molecular formula.
        /// </summary>
        /// <param name="peakData"> variable which stores the data itself</param>
        /// <param name="chargeState"> charge state at which we want to compute the peak.</param>
        /// <param name="peak"> peak for which we want to compute the fit function.</param>
        /// <param name="formula">stores the formula we want to fit.</param>
        /// <param name="deleteIntensityThreshold">intensity of least isotope to delete.</param>
        /// <param name="minTheoreticalIntensityForScore">minimum intensity of point to consider for scoring purposes.</param>
        /// <param name="debug">if debugging output is enabled</param>
        public double GetFitScore(PeakData peakData, int chargeState, ThrashV1Peak peak, MolecularFormula formula,
                                  double deleteIntensityThreshold, double minTheoreticalIntensityForScore, bool debug = false)
        {
            if (chargeState <= 0)
            {
                Console.WriteLine("Negative value for charge state. " + chargeState);
                Environment.Exit(1);
            }
            if (debug)
            {
                Console.WriteLine("Getting isotope distribution for formula = " + formula + " mz = " + peak.Mz +
                                  " charge = " + chargeState);
            }

            var resolution = peak.Mz / peak.FWHM;

            IsotopeDistribution.ChargeCarrierMass = ChargeCarrierMass;
            IsotopeDistribution.ApType            = ApodizationType.Gaussian;

            TheoreticalDistIntensities.Clear();
            TheoreticalDistMzs.Clear();

            IsotopeDistribution.CalculateDistribution(chargeState, resolution, formula, out TheoreticalDistMzs,
                                                      out TheoreticalDistIntensities, deleteIntensityThreshold, out IsotopeMzs, out IsotopeIntensities, debug);

            var delta = peak.Mz - IsotopeDistribution.MaxPeakMz;

            if (debug)
            {
                Console.WriteLine("Going for first fit");
            }

            int pointsUsed;

            return(FitScore(peakData, chargeState, peak, delta, minTheoreticalIntensityForScore, out pointsUsed,
                            debug));
        }
コード例 #32
0
		/**
		 * Gets the neutral losses which are stored in a file.
		 * 
		 * @return the neutral losses
		 */

		private void ReadInNeutralLosses()
		{
			neutralLoss = new Dictionary<double, NeutralLoss>();

			const string lossesString = "Ion Mode	Exact DM	Topological fragment	Elemental Composition	H-Difference	Furthest Distance	Atom to start Hydrogen Connected to Start Atom	Substance Class Identifier\n"
			                            + "+ -	18.01056	OH	H2O	-1	3	O	1	Non-specific (OH)\n"
			                            + "+	46.00548	COOH	HCOOH	-1	3	O	0	Aliphatic Acids, Amino Acid Conjugates …\n"
			                            + "+ -	17.02655	NH2	NH3	-1	3	N	2	not sure\n"
			                            + "+ -	27.01090	CN	HCN	-1	3	N	0	Alkaloids\n"
			                            + "+ -	30.01056	COH	CH2O	-1	3	O	0	Methoxylated aromatic compounds\n"
			                            + "#+ -	27.99491	COH	CO	1	3	O	0	Flavonoids, Coumarins, Chromones ….\n"
			                            + "#+ -	43.98983	COOH	CO2	1	3	O	0	Aromatic acids, Lactones \n"
			                            + "#-	79.95681	SO3H	SO3	1	3	S	0	0	-sulfated compounds\n"
			                            + "#+ -	162.05282	C6H11O5	C6H10O5	1	6	O	0	Glucosides";

			var lines = lossesString.Split('\n');

			//Read File Line By Line, skipping header
			foreach (var lineArray in lines.Skip(1).Where(l => !l.StartsWith("#")).Select(l => l.Split('\t')))
			{
				int mode;
				switch (lineArray[0])
				{
					case "+ -":
						mode = 0;
						break;
					case "-":
						mode = -1;
						break;
					default:
						mode = 1;
						break;
				}
				var mfT = new MolecularFormula();
				var mfE = new MolecularFormula();
				var nl = new NeutralLoss(MolecularFormulaManipulator.getMolecularFormula(lineArray[3], mfE), MolecularFormulaManipulator.getMolecularFormula(lineArray[2], mfT), mode, Integer.parseInt(lineArray[4]), Integer.parseInt(lineArray[5]), lineArray[6], Integer.parseInt(lineArray[7]));
                var deltaM = Double.Parse(lineArray[1], CultureInfo.InvariantCulture);
				neutralLoss[deltaM] = nl;
			}
		}