Esempio n. 1
0
        private CMLMolecule CDKAtomContainerToCMLMolecule(IAtomContainer structure, bool setIDs, bool isRef)
        {
            var cmlMolecule = new CMLMolecule();

            if (useCMLIDs && setIDs)
            {
                IDCreator.CreateIDs(structure);
            }

            this.CheckPrefix(cmlMolecule);
            if (!string.IsNullOrEmpty(structure.Id))
            {
                if (!isRef)
                {
                    cmlMolecule.Id = structure.Id;
                }
                else
                {
                    cmlMolecule.Ref = structure.Id;
                }
            }

            if (structure.Title != null)
            {
                cmlMolecule.Title = structure.Title;
            }
            if (structure.GetProperty <string>(CDKPropertyName.InChI) != null)
            {
                var ident = new CMLIdentifier
                {
                    Convention = "iupac:inchi"
                };
                ident.SetAttributeValue(CMLElement.Attribute_value, structure.GetProperty <string>(CDKPropertyName.InChI));
                cmlMolecule.Add(ident);
            }
            if (!isRef)
            {
                for (int i = 0; i < structure.Atoms.Count; i++)
                {
                    var cdkAtom = structure.Atoms[i];
                    var cmlAtom = CDKAtomToCMLAtom(structure, cdkAtom);
                    if (structure.GetConnectedSingleElectrons(cdkAtom).Count() > 0)
                    {
                        cmlAtom.SpinMultiplicity = structure.GetConnectedSingleElectrons(cdkAtom).Count() + 1;
                    }
                    cmlMolecule.Add(cmlAtom);
                }
                for (int i = 0; i < structure.Bonds.Count; i++)
                {
                    var cmlBond = CDKJBondToCMLBond(structure.Bonds[i]);
                    cmlMolecule.Add(cmlBond);
                }
            }

            // ok, output molecular properties, but not TITLE, INCHI, or DictRef's
            var props = structure.GetProperties();

            foreach (var propKey in props.Keys)
            {
                if (propKey is string key)
                {
                    // but only if a string
                    if (!isRef)
                    {
                        object value = props[key];
                        switch (key)
                        {
                        case CDKPropertyName.Title:
                        case CDKPropertyName.InChI:
                            break;

                        default:
                            // ok, should output this
                            var scalar = new CMLScalar();
                            this.CheckPrefix(scalar);
                            scalar.DictRef = "cdk:molecularProperty";
                            scalar.Title   = (string)key;
                            scalar.SetValue(value.ToString());
                            cmlMolecule.Add(scalar);
                            break;
                        }
                    }
                    // FIXME: At the moment the order writing the formula is into properties
                    // but it should be that IMolecularFormula is a extension of IAtomContainer
                    if (!isRef && string.Equals(key, CDKPropertyName.Formula, StringComparison.Ordinal))
                    {
                        switch (props[key])
                        {
                        case IMolecularFormula cdkFormula:
                        {
                            var cmlFormula   = new CMLFormula();
                            var isotopesList = MolecularFormulaManipulator.PutInOrder(MolecularFormulaManipulator.OrderEle, cdkFormula);
                            foreach (var isotope in isotopesList)
                            {
                                cmlFormula.Add(isotope.Symbol, cdkFormula.GetCount(isotope));
                            }
                            cmlMolecule.Add(cmlFormula);
                        }
                        break;

                        case IMolecularFormulaSet cdkFormulaSet:
                            foreach (var cdkFormula in cdkFormulaSet)
                            {
                                var isotopesList = MolecularFormulaManipulator.PutInOrder(MolecularFormulaManipulator.OrderEle, cdkFormula);
                                var cmlFormula   = new CMLFormula {
                                    DictRef = "cdk:possibleMachts"
                                };
                                foreach (var isotope in isotopesList)
                                {
                                    cmlFormula.Add(isotope.Symbol, cdkFormula.GetCount(isotope));
                                }
                                cmlMolecule.Add(cmlFormula);
                            }
                            break;
                        }
                    }
                }
            }

            foreach (var element in customizers.Keys)
            {
                var customizer = customizers[element];
                try
                {
                    customizer.Customize(structure, cmlMolecule);
                }
                catch (Exception exception)
                {
                    Trace.TraceError($"Error while customizing CML output with customizer: {customizer.GetType().Name}");
                    Debug.WriteLine(exception);
                }
            }
            return(cmlMolecule);
        }