示例#1
0
        /// <summary>
        /// Creates a molecule from a CML representation
        /// </summary>
        /// <param name="cmlElement">CML containing the molecule representation</param>
        /// <returns>Molecule object</returns>
        private Molecule CreateMolecule(XElement cmlElement)
        {
            var m = new Molecule();

            m.Id = cmlElement.Attribute("id")?.Value;

            var childMolecules = CML.GetMolecules(cmlElement);

            var atomElements    = CML.GetAtoms(cmlElement);
            var bondElements    = CML.GetBonds(cmlElement);
            var nameElements    = GetNames(cmlElement);
            var formulaElements = GetFormulas(cmlElement);

            Dictionary <string, Atom> newAtoms = new Dictionary <string, Atom>();

            foreach (XElement childElement in childMolecules)
            {
                var newMol = CreateMolecule(childElement);
                m.Molecules.Add(newMol);
            }

            foreach (XElement atomElement in atomElements)
            {
                var newAtom = CreateAtom(atomElement);
                newAtoms[newAtom.Id] = newAtom; //store the reference to help us build bonds
                m.Atoms.Add(newAtom);
            }

            foreach (XElement bondElement in bondElements)
            {
                var newBond = CreateBond(bondElement);

                string[] atomRefs = bondElement.Attribute("atomRefs2")?.Value.Split(' ');
                if (atomRefs?.Length == 2)
                {
                    newBond.StartAtom = newAtoms[atomRefs[0]];
                    newBond.EndAtom   = newAtoms[atomRefs[1]];
                    m.Bonds.Add(newBond);
                }
            }

            foreach (XElement nameElement in nameElements)
            {
                var newName = XmlToName(nameElement);
                m.ChemicalNames.Add(newName);
            }

            foreach (XElement formulaElement in formulaElements)
            {
                // Only import Concise Once
                if (string.IsNullOrEmpty(m.ConciseFormula))
                {
                    m.ConciseFormula = XmlToConsiseFormula(formulaElement);
                }

                var formula = XmlToFormula(formulaElement);
                if (formula.IsValid)
                {
                    m.Formulas.Add(formula);
                }
            }

            return(m);
        }
示例#2
0
        /// <summary>
        /// Creates a molecule from a CML representation
        /// </summary>
        /// <param name="cmlElement">CML containing the molecule representation</param>
        /// <returns>Molecule object</returns>
        private Molecule CreateMolecule(XElement cmlElement)
        {
            var m = new Molecule();

            m.Id = cmlElement.Attribute("id")?.Value;

            var childMolecules = CML.GetMolecules(cmlElement);

            var atomElements    = CML.GetAtoms(cmlElement);
            var bondElements    = CML.GetBonds(cmlElement);
            var nameElements    = GetNames(cmlElement);
            var formulaElements = GetFormulas(cmlElement);

            Dictionary <string, Atom> newAtoms = new Dictionary <string, Atom>();

            foreach (XElement childElement in childMolecules)
            {
                var newMol = CreateMolecule(childElement);
                m.Molecules.Add(newMol);
            }

            foreach (XElement atomElement in atomElements)
            {
                List <string> messages = new List <string>();
                var           newAtom  = CreateAtom(atomElement, out messages);
                if (messages.Count > 0)
                {
                    m.Errors.AddRange(messages);
                }

                if (newAtom != null)
                {
                    newAtoms[newAtom.Id] = newAtom; //store the reference to help us build bonds
                    m.Atoms.Add(newAtom);
                }
            }

            foreach (XElement bondElement in bondElements)
            {
                string message = "";
                var    newBond = CreateBond(bondElement, out message);
                if (!string.IsNullOrEmpty(message))
                {
                    m.Errors.Add(message);
                }

                string[] atomRefs = bondElement.Attribute("atomRefs2")?.Value.Split(' ');
                if (atomRefs?.Length == 2)
                {
                    Atom startAtom = null;
                    if (newAtoms.ContainsKey(atomRefs[0]))
                    {
                        startAtom = newAtoms[atomRefs[0]];
                    }
                    else
                    {
                        m.Errors.Add($"Can't find atom '{atomRefs[0]}' of {bondElement}");
                    }

                    Atom endAtom = null;
                    if (newAtoms.ContainsKey(atomRefs[1]))
                    {
                        endAtom = newAtoms[atomRefs[1]];
                    }
                    else
                    {
                        m.Errors.Add($"Can't find atom '{atomRefs[1]}' of {bondElement}");
                    }

                    if (startAtom != null && endAtom != null)
                    {
                        newBond.StartAtom = startAtom;
                        newBond.EndAtom   = endAtom;
                        m.Bonds.Add(newBond);
                    }
                }
            }

            foreach (XElement nameElement in nameElements)
            {
                var newName = XmlToName(nameElement);
                m.ChemicalNames.Add(newName);
            }

            foreach (XElement formulaElement in formulaElements)
            {
                // Only import Concise Once
                if (string.IsNullOrEmpty(m.ConciseFormula))
                {
                    m.ConciseFormula = XmlToConsiseFormula(formulaElement);
                }

                var formula = XmlToFormula(formulaElement);
                if (formula.IsValid)
                {
                    m.Formulas.Add(formula);
                }
            }

            return(m);
        }