internal static void ConvertElementTableToFormula( ref clsElementIsotopes elemental_isotope_composition, Hashtable elementCounts, out DeconToolsV2.MolecularFormula formula) { var elements = elementCounts.Keys.GetEnumerator(); formula = new DeconToolsV2.MolecularFormula(); while (elements.MoveNext()) { // Get the next element symbol in the table var element = (string)elements.Current; // Put it in a character array var count = (int)elementCounts[element]; // find the index of the element in the AtomicInformation var index = elemental_isotope_composition.GetElementIndex(element); if (index == -1) { throw new ApplicationException(string.Concat("Unknown element ", element)); } var atomic_count = new DeconToolsV2.AtomicCount(index, count); var mono_mass = elemental_isotope_composition.ElementalIsotopesList[index].Isotopes[0].Mass * count; var avg_mass = elemental_isotope_composition.ElementalIsotopesList[index].AverageMass * count; formula.AddAtomicCount(atomic_count, mono_mass, avg_mass); } }
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); }
public MercuryIsotopeDistribution(MercuryIsotopeDistribution mercDistribution) { ElementalIsotopeComposition = mercDistribution.ElementalIsotopeComposition; ChargeCarrierMass = mercDistribution.ChargeCarrierMass; ApType = mercDistribution.ApType; MercurySize = mercDistribution.MercurySize; PointsPerAmu = mercDistribution.PointsPerAmu; }
public void SetMolecularFormula(string molFormula, clsElementIsotopes atomicInformation) { Formula = molFormula; var formula = molFormula; TotalAtomCount = 0; MonoisotopicMass = 0; ElementalComposition.Clear(); var validator = new Regex(@"^(\s*[A-Z][a-z]?\s*\d*\.?\d*)*\s*$", RegexOptions.CultureInvariant); if (!validator.IsMatch(molFormula)) { throw new Exception("Molecular formula specified was invalid. Format required is one or more " + "groups similar to \"[atomic symbol][count]\", where atomic symbol is an " + "uppercase letter that may be followed by a lowercase letter, and count " + "is a number that consists of numeric digits and maybe one decimal point."); } var grouper = new Regex(@"([A-Z][a-z]?)\s*(\d*\.?\d*)", RegexOptions.CultureInvariant); var groups = grouper.Matches(molFormula); foreach (Match group in groups) { var match = group.Groups; // match[0] returns the entire match, match[1] and match[2] will have the individual groups var atomicSymbol = match[1].Value; var countStr = match[2].Value; double count = 1; if (!string.IsNullOrWhiteSpace(countStr)) { count = Convert.ToDouble(countStr.Trim()); } // now we should have a symbol and a count. Lets get the atomicity of the atom. var elementIndex = atomicInformation.GetElementIndex(atomicSymbol); if (elementIndex == -1) { // theres an error. two decimals. var errorStr = "Molecular Formula specified was incorrect. Symbol in formula was not recognize from elements provided: "; errorStr += atomicSymbol; throw new Exception(errorStr); } ElementalComposition.Add(new AtomicCount(elementIndex, count)); TotalAtomCount += count; MonoisotopicMass += atomicInformation.ElementalIsotopesList[elementIndex].Isotopes[0].Mass * count; AverageMass += atomicInformation.ElementalIsotopesList[elementIndex].AverageMass * count; } /* * // Regular expressions simplify this code so much. See above. * var formulaLength = formula.Length; * var index = 0; * //var numAtoms = atomicInformation.GetNumElements(); * while (index < formulaLength) * { * while (index < formulaLength && (formula[index] == ' ' || formula[index] == '\t')) * { * index++; * } * * var startIndex = index; * var stopIndex = startIndex; * var symbolChar = formula[stopIndex]; * if (!(symbolChar >= 'A' && symbolChar <= 'Z') && !(symbolChar >= 'a' && symbolChar <= 'z')) * { * var errorStr = "Molecular Formula specified was incorrect at position: " + (stopIndex + 1) + * ". Should have element symbol there"; * throw new Exception(errorStr); * } * * while ((symbolChar >= 'A' && symbolChar <= 'Z') || (symbolChar >= 'a' && symbolChar <= 'z')) * { * stopIndex++; * if (stopIndex == formulaLength) * break; * symbolChar = formula[stopIndex]; * } * * var atomicSymbol = formula.Substring(startIndex, stopIndex - startIndex); * index = stopIndex; * while (index < formulaLength && (formula[index] == ' ' || formula[index] == '\t')) * { * index++; * } * double count = 0; * if (index == formulaLength) * { * // assume that the last symbol had a 1. * count = 1; * } * startIndex = index; * stopIndex = startIndex; * symbolChar = formula[stopIndex]; * var decimalFound = false; * while ((symbolChar >= '0' && symbolChar <= '9') || symbolChar == '.') * { * if (symbolChar == '.') * { * if (decimalFound) * { * // theres an error. two decimals. * var errorStr = "Molecular Formula specified was incorrect at position: " + * (stopIndex + 1) + ". Two decimal points present"; * throw new Exception(errorStr); * } * decimalFound = true; * } * stopIndex++; * if (stopIndex == formulaLength) * break; * symbolChar = formula[stopIndex]; * } * if (startIndex == stopIndex) * { * count = 1; * } * else * { * count = Convert.ToDouble(formula.Substring(startIndex, stopIndex - startIndex)); * //count = Helpers.atof(formula.Substring(index)); * } * // now we should have a symbol and a count. Lets get the atomicity of the atom. * var elementIndex = atomicInformation.GetElementIndex(atomicSymbol); * if (elementIndex == -1) * { * // theres an error. two decimals. * var errorStr = * "Molecular Formula specified was incorrect. Symbol in formula was not recognize from elements provided: "; * errorStr += atomicSymbol; * throw new Exception(errorStr); * } * ElementalComposition.Add(new AtomicCount(elementIndex, count)); * index = stopIndex; * TotalAtomCount += count; * MonoisotopicMass += atomicInformation.ElementalIsotopesList[elementIndex].Isotopes[0].Mass * count; * AverageMass += atomicInformation.ElementalIsotopesList[elementIndex].AverageMass * count; * } */ }
public MolecularFormula(string formula, clsElementIsotopes atomicInformation) { SetMolecularFormula(formula, atomicInformation); }
public void SetMolecularFormula(string molFormula, clsElementIsotopes atomicInformation) { Formula = molFormula; var formula = molFormula; TotalAtomCount = 0; ElementalComposition.Clear(); var formulaLength = formula.Length; var index = 0; //var numAtoms = atomicInformation.GetNumElements(); MonoisotopicMass = 0; while (index < formulaLength) { while (index < formulaLength && (formula[index] == ' ' || formula[index] == '\t')) { index++; } var startIndex = index; var stopIndex = startIndex; var symbolChar = formula[stopIndex]; if (!(symbolChar >= 'A' && symbolChar <= 'Z') && !(symbolChar >= 'a' && symbolChar <= 'z')) { var errorStr = "Molecular Formula specified was incorrect at position: " + (stopIndex + 1) + ". Should have element symbol there"; throw new Exception(errorStr); } while ((symbolChar >= 'A' && symbolChar <= 'Z') || (symbolChar >= 'a' && symbolChar <= 'z')) { stopIndex++; if (stopIndex == formulaLength) { break; } symbolChar = formula[stopIndex]; } var atomicSymbol = formula.Substring(startIndex, stopIndex - startIndex); index = stopIndex; while (index < formulaLength && (formula[index] == ' ' || formula[index] == '\t')) { index++; } double count = 0; if (index == formulaLength) { // assume that the last symbol had a 1. count = 1; } startIndex = index; stopIndex = startIndex; symbolChar = formula[stopIndex]; var decimalFound = false; while ((symbolChar >= '0' && symbolChar <= '9') || symbolChar == '.') { if (symbolChar == '.') { if (decimalFound) { // theres an error. two decimals. var errorStr = "Molecular Formula specified was incorrect at position: " + (stopIndex + 1) + ". Two decimal points present"; throw new Exception(errorStr); } decimalFound = true; } stopIndex++; if (stopIndex == formulaLength) { break; } symbolChar = formula[stopIndex]; } if (startIndex == stopIndex) { count = 1; } else { count = Helpers.atof(formula.Substring(index)); } // now we should have a symbol and a count. Lets get the atomicity of the atom. var elementIndex = atomicInformation.GetElementIndex(atomicSymbol); if (elementIndex == -1) { // theres an error. two decimals. var errorStr = "Molecular Formula specified was incorrect. Symbol in formula was not recognize from elements provided: "; errorStr += atomicSymbol; throw new Exception(errorStr); } var currentAtom = new AtomicCount(elementIndex, count); ElementalComposition.Add(currentAtom); index = stopIndex; TotalAtomCount += count; MonoisotopicMass += atomicInformation.ElementalIsotopesList[elementIndex].Isotopes[0].Mass * count; AverageMass += atomicInformation.ElementalIsotopesList[elementIndex].AverageMass * count; } }
public void SetElementalIsotopeComposition(clsElementIsotopes isoComp) { ElementalIsotopeComposition = isoComp; }