Example #1
0
        public void AddAtomicCount(AtomicCount cnt, double monoMass, double avgMass)
        {
            var numElements = ElementalComposition.Count;

            for (var elemNum = 0; elemNum < numElements; elemNum++)
            {
                if (ElementalComposition[elemNum].Index == cnt.Index)
                {
                    ElementalComposition[elemNum].NumCopies += cnt.NumCopies;
                    MonoisotopicMass += monoMass;
                    AverageMass      += avgMass;
                    TotalAtomCount   += cnt.NumCopies;
                    return;
                }
            }
            ElementalComposition.Add(cnt);
            MonoisotopicMass += monoMass;
            AverageMass      += avgMass;
            TotalAtomCount   += cnt.NumCopies;
        }
Example #2
0
 public AtomicCount(AtomicCount ac)
 {
     Index     = ac.Index;
     NumCopies = ac.NumCopies;
 }
Example #3
0
        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;
            }
        }