Example #1
0
        private static Atom GetAtom(XElement cmlElement)
        {
            Atom atom = new Atom();

            atom.Messages = new List <string>();
            string message   = "";
            string atomLabel = cmlElement.Attribute(CMLConstants.AttributeId)?.Value;

            Point p = CMLHelper.GetPosn(cmlElement, out message);

            if (!string.IsNullOrEmpty(message))
            {
                atom.Messages.Add(message);
            }

            atom.Id       = atomLabel;
            atom.Position = p;

            ElementBase e = CMLHelper.GetElementOrFunctionalGroup(cmlElement, out message);

            if (!string.IsNullOrEmpty(message))
            {
                atom.Messages.Add(message);
            }

            if (e != null)
            {
                atom.Element       = e;
                atom.FormalCharge  = CMLHelper.GetFormalCharge(cmlElement);
                atom.IsotopeNumber = CMLHelper.GetIsotopeNumber(cmlElement);
                atom.ExplicitC     = CMLHelper.GetExplicit(cmlElement);
            }

            return(atom);
        }
Example #2
0
        public Model Import(object data, List <string> protectedLabels = null)
        {
            Model newModel = new Model();

            if (data != null)
            {
                XDocument modelDoc = XDocument.Parse((string)data);
                var       root     = modelDoc.Root;

                // Only import if not null
                var customXmlPartGuid = CMLHelper.GetCustomXmlPartGuid(root);
                if (customXmlPartGuid != null && !string.IsNullOrEmpty(customXmlPartGuid.Value))
                {
                    newModel.CustomXmlPartGuid = customXmlPartGuid.Value;
                }

                var moleculeElements = CMLHelper.GetMolecules(root);

                foreach (XElement meElement in moleculeElements)
                {
                    var newMol = GetMolecule(meElement);

                    AddMolecule(newModel, newMol);
                    newMol.Parent = (IChemistryContainer)newModel;
                }

                // Force all ConciseFormulas to be (re) calculated
                newModel.CalculateFormula();

                #region Handle 1D Labels

                newModel.SetMissingIds();
                if (protectedLabels != null && protectedLabels.Count >= 1)
                {
                    newModel.SetProtectedLabels(protectedLabels);
                }
                else
                {
                    newModel.Relabel(true);
                }

                #endregion Handle 1D Labels

                // Calculate dynamic properties
                newModel.Refresh();
            }

            return(newModel);
        }
Example #3
0
        private static Bond GetBond(XElement cmlElement, Dictionary <string, string> reverseAtomLookup)
        {
            Bond bond = new Bond();

            string[] atomRefs = cmlElement.Attribute(CMLConstants.AttributeAtomRefs2)?.Value.Split(' ');
            if (atomRefs?.Length == 2)
            {
                bond.StartAtomInternalId = reverseAtomLookup[atomRefs[0]];
                bond.EndAtomInternalId   = reverseAtomLookup[atomRefs[1]];
            }
            var bondRef = cmlElement.Attribute(CMLConstants.AttributeId)?.Value;

            bond.Id    = bondRef ?? bond.Id;
            bond.Order = cmlElement.Attribute(CMLConstants.AttributeOrder)?.Value;

            var stereoElems = CMLHelper.GetStereo(cmlElement);

            if (stereoElems.Any())
            {
                var stereo = stereoElems[0].Value;

                bond.Stereo = Globals.StereoFromString(stereo);
            }
            Globals.BondDirection?dir = null;

            var dirAttr = cmlElement.Attribute(CMLNamespaces.c4w + CMLConstants.AttributePlacement);

            if (dirAttr != null)
            {
                Globals.BondDirection temp;

                if (Enum.TryParse(dirAttr.Value, out temp))
                {
                    dir = temp;
                }
            }

            if (dir != null)
            {
                bond.Placement = dir.Value;
            }

            return(bond);
        }
Example #4
0
        public static Molecule GetMolecule(XElement cmlElement)
        {
            Molecule molecule = new Molecule();

            string showBracketsValue = cmlElement.Attribute(CMLNamespaces.c4w + CMLConstants.AttributeShowMoleculeBrackets)?.Value;

            if (!string.IsNullOrEmpty(showBracketsValue))
            {
                molecule.ShowMoleculeBrackets = bool.Parse(showBracketsValue);
            }

            string idValue = cmlElement.Attribute(CMLConstants.AttributeId)?.Value;

            if (!string.IsNullOrEmpty(idValue))
            {
                molecule.Id = idValue;
            }

            string countValue = cmlElement.Attribute(CMLConstants.AttributeCount)?.Value;

            if (!string.IsNullOrEmpty(countValue))
            {
                molecule.Count = int.Parse(countValue);
            }

            string chargeValue = cmlElement.Attribute(CMLConstants.AttributeFormalCharge)?.Value;

            if (!string.IsNullOrEmpty(chargeValue))
            {
                molecule.FormalCharge = int.Parse(chargeValue);
            }

            string spinValue = cmlElement.Attribute(CMLConstants.AttributeSpinMultiplicity)?.Value;

            if (!string.IsNullOrEmpty(spinValue))
            {
                molecule.SpinMultiplicity = int.Parse(spinValue);
            }

            molecule.Errors   = new List <string>();
            molecule.Warnings = new List <string>();

            List <XElement> childMolecules = CMLHelper.GetMolecules(cmlElement);

            List <XElement> atomElements    = CMLHelper.GetAtoms(cmlElement);
            List <XElement> bondElements    = CMLHelper.GetBonds(cmlElement);
            List <XElement> nameElements    = CMLHelper.GetNames(cmlElement);
            List <XElement> formulaElements = CMLHelper.GetFormulas(cmlElement);
            List <XElement> labelElements   = CMLHelper.GetLabels(cmlElement);

            foreach (XElement childElement in childMolecules)
            {
                Molecule newMol = GetMolecule(childElement);
                molecule.AddMolecule(newMol);
                newMol.Parent = molecule;
            }

            Dictionary <string, string> reverseAtomLookup = new Dictionary <string, string>();

            foreach (XElement atomElement in atomElements)
            {
                Atom newAtom = GetAtom(atomElement);
                if (newAtom.Messages.Count > 0)
                {
                    molecule.Errors.AddRange(newAtom.Messages);
                }

                molecule.AddAtom(newAtom);
                reverseAtomLookup[newAtom.Id] = newAtom.InternalId;
                newAtom.Parent = molecule;
            }

            foreach (XElement bondElement in bondElements)
            {
                Bond newBond = GetBond(bondElement, reverseAtomLookup);

                if (newBond.Messages.Count > 0)
                {
                    molecule.Errors.AddRange(newBond.Messages);
                }

                molecule.AddBond(newBond);
                newBond.Parent = molecule;
            }

            foreach (XElement formulaElement in formulaElements)
            {
                var formula = GetFormula(formulaElement);
                if (formula.IsValid)
                {
                    molecule.Formulas.Add(formula);
                }
            }

            foreach (XElement nameElement in nameElements)
            {
                var name = GetName(nameElement);
                if (name.IsValid)
                {
                    molecule.Names.Add(name);
                }
            }

            Molecule copy = molecule.Copy();

            copy.SplitIntoChildren();

            // If copy now contains (child) molecules, replace original
            if (copy.Molecules.Count > 1)
            {
                molecule = copy;
            }

            foreach (var labelElement in labelElements)
            {
                var label = GetLabel(labelElement);
                if (label.IsValid)
                {
                    molecule.Labels.Add(label);
                }
            }

            // Fix Invalid data; If only one atom
            if (molecule.Atoms.Count == 1)
            {
                // Remove ExplicitC flag
                molecule.Atoms.First().Value.ExplicitC = null;

                // Remove invalid molecule properties
                molecule.ShowMoleculeBrackets = false;
                molecule.SpinMultiplicity     = null;
                molecule.FormalCharge         = null;
                molecule.Count = null;
            }

            molecule.RebuildRings();

            return(molecule);
        }