Пример #1
0
        public void Add(CMLBond bond)
        {
            var aa = this.Element(XName_CML_bondArray);

            if (aa == null)
            {
                aa = new CMLBondArray();
                this.Add(aa);
            }
            aa.Add(bond);
        }
Пример #2
0
        public void TestCdkBondToCMLBond_Hatch()
        {
            IChemObjectBuilder builder = ChemObjectBuilder.Instance;
            IBond bond = builder.NewBond();

            bond.Order  = BondOrder.Single;
            bond.Stereo = BondStereo.Down;

            Convertor convertor = new Convertor(true, null);
            CMLBond   cmlBond   = convertor.CDKJBondToCMLBond(bond);

            var writer = new StringWriter();
            var d      = new XDocument(cmlBond);

            d.Save(writer);

            string expected = "<bondStereo dictRef=\"cml:H\">H</bondStereo>";
            string actual   = writer.ToString();

            Assert.IsTrue(actual.Contains(expected));
        }
Пример #3
0
        public CMLBond CDKJBondToCMLBond(IBond cdkBond)
        {
            var cmlBond = new CMLBond();

            this.CheckPrefix(cmlBond);
            if (string.IsNullOrEmpty(cdkBond.Id))
            {
                cmlBond.Id = "b" + cdkBond.GetHashCode();
            }
            else
            {
                cmlBond.Id = cdkBond.Id;
            }

            var atomRefArray = new string[cdkBond.Atoms.Count];

            for (int i = 0; i < cdkBond.Atoms.Count; i++)
            {
                var atomID = cdkBond.Atoms[i].Id;
                if (string.IsNullOrEmpty(atomID))
                {
                    atomRefArray[i] = "a" + cdkBond.Atoms[i].GetHashCode().ToString(NumberFormatInfo.InvariantInfo);
                }
                else
                {
                    atomRefArray[i] = atomID;
                }
            }
            if (atomRefArray.Length == 2)
            {
                cmlBond.AtomRefs2 = atomRefArray;
            }
            else
            {
                cmlBond.AtomRefs = atomRefArray;
            }

            var border = cdkBond.Order;

            switch (border)
            {
            case BondOrder.Single:
                cmlBond.Order = "S";
                break;

            case BondOrder.Double:
                cmlBond.Order = "D";
                break;

            case BondOrder.Triple:
                cmlBond.Order = "T";
                break;

            default:
                var scalar = new CMLScalar();
                this.CheckPrefix(scalar);
                scalar.DictRef = "cdk:bondOrder";
                scalar.Title   = "order";
                scalar.SetValue(cdkBond.Order.Numeric());
                cmlBond.Add(scalar);
                break;
            }
            if (cdkBond.IsAromatic)
            {
                var bType = new CMLBondType {
                    DictRef = "cdk:aromaticBond"
                };
                cmlBond.Add(bType);
            }

            switch (cdkBond.Stereo)
            {
            case BondStereo.Up:
            case BondStereo.Down:
                var bondStereo = new CMLBondStereo();
                this.CheckPrefix(bondStereo);
                if (cdkBond.Stereo == BondStereo.Up)
                {
                    bondStereo.DictRef = "cml:W";
                    bondStereo.Value   = "W";
                }
                else
                {
                    bondStereo.DictRef = "cml:H";
                    bondStereo.Value   = "H";
                }
                cmlBond.Add(bondStereo);
                break;
            }
            if (cdkBond.GetProperties().Count > 0)
            {
                WriteProperties(cdkBond, cmlBond);
            }

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

            return(cmlBond);
        }
Пример #4
0
        private CMLMolecule CDKAtomContainerToCMLMolecule(IAtomContainer structure, bool setIDs, bool isRef)
        {
            CMLMolecule 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)
            {
                CMLIdentifier 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++)
                {
                    IAtom   cdkAtom = structure.Atoms[i];
                    CMLAtom 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++)
                {
                    CMLBond 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);
        }