private static Dictionary<string, double> ParseFormula(string formula) { var parsedFormula = new Dictionary<string, double>(); var regexSymbols = @"\d+"; //digit at least once var regexNumbers = @"[A-Z]{1}"; //not a digit var pSymbols = new Regex(regexSymbols); var pNumbers = new Regex(regexNumbers); var symbols = pSymbols.Split(formula).Where(s => !string.IsNullOrEmpty(s)).ToArray(); var numbers = pNumbers.Split(formula); var numberCount = 1; for (var i = 0; i < symbols.Length; i++) { //create temporary atom with symbol and "configure" it IAtom a = new Atom(symbols[i]); var isofac = IsotopeFactory.getInstance(new ChemObject().getBuilder()); isofac.configure(a); //fix if the digit is not written if (string.IsNullOrEmpty(numbers[numberCount]) && numberCount > 0) { numbers[numberCount] = "1"; } var mass = a.getExactMass().doubleValue(); mass = mass * double.Parse(numbers[numberCount], CultureInfo.InvariantCulture); numberCount++; parsedFormula[symbols[i]] = mass; } return parsedFormula; }
public static Dictionary<string, double> ParseFormula(IMolecularFormula formula) { var parsedFormula = new Dictionary<string, double>(); var elements = MolecularFormulaManipulator.elements(formula); foreach (var element in elements.ToWindowsEnumerable<IElement>()) { var elementCount = MolecularFormulaManipulator.getElementCount(formula, element); var symbol = element.getSymbol(); var a = new Atom(symbol); var isofac = IsotopeFactory.getInstance(new ChemObject().getBuilder()); isofac.configure(a); var mass = a.getExactMass().doubleValue(); mass = mass * elementCount; parsedFormula[symbol] = mass; } return parsedFormula; }
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; }