public virtual void TestGetIsotope_Number_Clone() { IMolecularFormula mf = Builder.NewMolecularFormula(); IIsotope carb = Builder.NewIsotope("C"); IIsotope flu = Builder.NewIsotope("F"); IIsotope h1 = Builder.NewIsotope("H"); mf.Add(carb); mf.Add(flu); mf.Add(h1, 3); object clone = mf.Clone(); Assert.IsTrue(clone is IMolecularFormula); IMolecularFormula cloneFormula = (IMolecularFormula)clone; Assert.AreEqual(1, cloneFormula.GetCount(carb)); Assert.AreEqual(1, cloneFormula.GetCount(flu)); Assert.AreEqual(3, cloneFormula.GetCount(h1)); // In a List the objects are not stored in the same order than called // Assert.AreEqual("C", cloneFormula.Isotopes[0].Symbol); // Assert.AreEqual("F", cloneFormula.Isotopes[1].Symbol); // Assert.AreEqual("H", cloneFormula.Isotopes[2].Symbol); }
/// <summary> /// Clear the isotope information from isotopes that are major (e.g. /// <sup>12</sup>C, <sup>1</sup>H, etc). /// </summary> /// <param name="formula">the formula</param> public static void ClearMajorIsotopes(IMolecularFormula formula) { var isotopesToRemove = new List <IIsotope>(); var isotopesToAdd = new List <Tuple <IIsotope, int> >(); foreach (var iso in formula.Isotopes.Where(n => IsMajor(n))) { var count = formula.GetCount(iso); isotopesToRemove.Add(iso); iso.MassNumber = null; // may be immutable var iso_ = iso; if (iso_.MassNumber != null) { iso_ = formula.Builder.NewIsotope(iso_.Symbol); } iso_.ExactMass = null; iso_.Abundance = null; isotopesToAdd.Add(new Tuple <IIsotope, int>(iso_, count)); } foreach (var isotope in isotopesToRemove) { formula.Remove(isotope); } foreach (var t in isotopesToAdd) { formula.Add(t.Item1, t.Item2); } }
public virtual void TestGetIsotopeCount_IIsotope_Occurr() { IMolecularFormula mf = Builder.NewMolecularFormula(); IIsotope carb = Builder.NewIsotope("C"); IIsotope flu = Builder.NewIsotope("F"); IIsotope h1 = Builder.NewIsotope("H"); mf.Add(carb); mf.Add(flu); mf.Add(h1, 3); Assert.AreEqual(3, mf.IsotopesCount); Assert.AreEqual(1, mf.GetCount(carb)); Assert.AreEqual(1, mf.GetCount(flu)); Assert.AreEqual(3, mf.GetCount(h1)); }
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"))); }
public virtual void TestAddIsotope_IIsotope_int() { IMolecularFormula mf = Builder.NewMolecularFormula(); IIsotope carb = Builder.NewIsotope("C"); IIsotope flu = Builder.NewIsotope("F"); IIsotope h1 = Builder.NewIsotope("H"); mf.Add(carb); mf.Add(flu); mf.Add(h1, 3); Assert.AreEqual(3, mf.IsotopesCount); Assert.AreEqual(1, mf.GetCount(carb)); Assert.AreEqual(1, mf.GetCount(flu)); Assert.AreEqual(3, mf.GetCount(h1)); // In a List the objects are not stored in the same order than called // Assert.AreEqual("C", mf.Isotopes[0].Symbol); // Assert.AreEqual("F", mf.Isotopes[1].Symbol); // Assert.AreEqual("H", mf.Isotopes[2].Symbol); }
public virtual void TestClone_Isotopes() { IMolecularFormula mf = Builder.NewMolecularFormula(); IIsotope carb = Builder.NewIsotope("C"); IIsotope flu = Builder.NewIsotope("F"); IIsotope h1 = Builder.NewIsotope("H"); mf.Add(carb); mf.Add(flu); mf.Add(h1, 3); Assert.AreEqual(3, mf.IsotopesCount); Assert.AreEqual(1, mf.GetCount(carb)); Assert.AreEqual(1, mf.GetCount(flu)); Assert.AreEqual(3, mf.GetCount(h1)); object clone = mf.Clone(); Assert.IsTrue(clone is IMolecularFormula); Assert.AreEqual(mf.IsotopesCount, ((IMolecularFormula)clone).IsotopesCount); Assert.AreEqual(3, ((IMolecularFormula)clone).IsotopesCount); }
/// <summary> /// Adds an molecularFormula to this MolecularFormula. /// </summary> /// <param name="formula">The molecularFormula to be added to this chemObject</param> /// <returns>The IMolecularFormula</returns> public IMolecularFormula Add(IMolecularFormula formula) { foreach (var newIsotope in formula.Isotopes) { Add(newIsotope, formula.GetCount(newIsotope)); } if (formula.Charge != null) { if (Charge != null) { Charge += formula.Charge; } else { Charge = formula.Charge; } } return(this); }
/// <summary> /// Method to extract the Ring Double Bond Equivalents (RDB) value. It test all possible /// oxidation states. /// </summary> /// <param name="formula">The IMolecularFormula object</param> /// <returns>The RDBE value</returns> public virtual IReadOnlyList <double> GetRDBEValue(IMolecularFormula formula) { var RDBEList = new List <double>(); // The number of combinations with repetition // (v+n-1)!/[n!(v-1)!] int nE = 0; // number of elements to change var nV = new List <int>(); // number of valence changing foreach (var isotope in formula.Isotopes) { int[] valence = GetOxidationState(formula.Builder.NewAtom(isotope.Symbol)); if (valence.Length != 1) { for (int i = 0; i < valence.Length; i++) { nV.Add(valence[i]); } nE += MolecularFormulaManipulator.GetElementCount(formula, ChemicalElement.OfSymbol(isotope.Symbol)); } } double RDBE = 0; if (nE == 0) { foreach (var isotope in formula.Isotopes) { var valence = GetOxidationState(formula.Builder.NewAtom(isotope.Symbol)); var value = (valence[0] - 2) * formula.GetCount(isotope) / 2.0; RDBE += value; } RDBE += 1; RDBEList.Add(RDBE); } else { double RDBE_1 = 0; foreach (var isotope in formula.Isotopes) { var valence = GetOxidationState(formula.Builder.NewAtom(isotope.Symbol)); var value = (valence[0] - 2) * formula.GetCount(isotope) * 0.5; RDBE_1 += value; } var valences = new string[nV.Count]; for (int i = 0; i < valences.Length; i++) { valences[i] = nV[i].ToString(NumberFormatInfo.InvariantInfo); } var c = new Combinations(valences, nE); while (c.HasMoreElements()) { double RDBE_int = 0.0; var combo = (object[])c.NextElement(); for (int i = 0; i < combo.Length; i++) { var value = (int.Parse((string)combo[i], NumberFormatInfo.InvariantInfo) - 2) / 2; RDBE_int += value; } RDBE = 1 + RDBE_1 + RDBE_int; RDBEList.Add(RDBE); } } return(RDBEList); }