Ejemplo n.º 1
0
        private void WriteProperties(IChemObject obj, CMLElement cmlElement)
        {
            var props = obj.GetProperties();

            foreach (var key in props.Keys)
            {
                var stringKey = (string)key;
                switch (stringKey)
                {
                case CDKPropertyName.Title:
                    // don't output this one. It's covered by AddTitle()
                    break;

                default:
                    if (!(stringKey.StartsWith("org.openscience.cdk", StringComparison.Ordinal)))
                    {
                        object value  = props[key];
                        var    scalar = new CMLScalar();
                        this.CheckPrefix(scalar);
                        scalar.Title = (string)key;
                        scalar.Value = value.ToString();
                        cmlElement.Add(scalar);
                    }
                    break;
                }
            }
        }
Ejemplo n.º 2
0
        public static CMLScalar CreateScalar(string dictRef, double param, string unitRef, double error)
        {
            CMLScalar scalar = new CMLScalar(param)
            {
                DictRef = dictRef
            };

            if (unitRef != null)
            {
                scalar.Units = unitRef;
            }
            if (!double.IsNaN(error))
            {
                scalar.ErrorValue = error;
            }
            return(scalar);
        }
Ejemplo n.º 3
0
        public void SetCellParameters(double[] parameters, double[] error)
        {
            if (parameters == null || parameters.Length != 6)
            {
                throw new ApplicationException("Must have 6 cell parameters");
            }
            var cellParamVector = this.Elements(XName_CML_scalar);
            var count           = cellParamVector.Count();

            if (count == 0)
            {
                for (int i = 0; i < 6; i++)
                {
                    CMLScalar cellParam = CMLCrystal.CreateScalar(
                        CRYSTAL_DICT_REFS[i], parameters[i], CRYSTAL_DICT_UNITS[i],
                        double.NaN);
                    this.Add(cellParam);
                }
            }
            else if (count == 6)
            {
                int i = 0;
                foreach (var e in cellParamVector)
                {
                    CMLScalar cellParam = (CMLScalar)e;
                    cellParam.SetValue(parameters[i]);
                    if (error != null)
                    {
                        cellParam.ErrorValue = parameters[i];
                    }
                    cellParam.DictRef = CRYSTAL_DICT_REFS[i];
                    i++;
                }
            }
            else
            {
                throw new ApplicationException(
                          "Corrupted cell parameters: must be exactly 6 (found: "
                          + cellParamVector.Count());
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Customize Molecule.
        /// </summary>
        /// <param name="molecule"></param>
        /// <param name="nodeToAdd"></param>
        public void Customize(IAtomContainer molecule, object nodeToAdd)
        {
            if (!(nodeToAdd is CMLMolecule))
            {
                throw new CDKException("NodeToAdd must be of type nu.xom.Element!");
            }

            //The nodeToAdd
            CMLMolecule molToCustomize = (CMLMolecule)nodeToAdd;

            if ((molecule is MDMolecule))
            {
                MDMolecule mdmol = (MDMolecule)molecule;
                molToCustomize.Convention = "md:mdMolecule";
                molToCustomize.SetAttributeValue(XNamespace.Xmlns + "md", NS_MD.NamespaceName);

                //Residues
                if (mdmol.GetResidues().Count > 0)
                {
                    foreach (var residue in mdmol.GetResidues())
                    {
                        int number = residue.GetNumber();

                        CMLMolecule resMol = new CMLMolecule
                        {
                            DictRef = "md:residue",
                            Title   = residue.Name
                        };

                        //Append resNo
                        CMLScalar residueNumber = new CMLScalar(number);
                        residueNumber.SetAttributeValue(Attribute_dictRef, "md:resNumber");
                        resMol.Add(residueNumber);

                        // prefix for residue atom id
                        string rprefix = "r" + number;
                        //Append atoms
                        CMLAtomArray ar = new CMLAtomArray();
                        for (int i = 0; i < residue.Atoms.Count; i++)
                        {
                            CMLAtom cmlAtom = new CMLAtom
                            {
                                //                        Console.Out.WriteLine("atom ID: "+ residue.Atoms[i].Id);
                                //                        cmlAtom.AddAttribute(new Attribute("ref", residue.Atoms[i].Id));
                                // the next thing is better, but  exception
                                //
                                // setRef to keep consistent usage
                                // setId to satisfy Jumbo 54. need for all atoms to have id
                                Ref = residue.Atoms[i].Id,
                                Id  = rprefix + "_" + residue.Atoms[i].Id
                            };
                            ar.Add(cmlAtom);
                        }
                        resMol.Add(ar);

                        molToCustomize.Add(resMol);
                    }
                }

                //Chargegroups
                if (mdmol.GetChargeGroups().Count > 0)
                {
                    foreach (var chargeGroup in mdmol.GetChargeGroups())
                    {
                        int number = chargeGroup.GetNumber();

                        //FIXME: persist the ChargeGroup
                        CMLMolecule cgMol = new CMLMolecule
                        {
                            DictRef = "md:chargeGroup"
                        };
                        // etc: add name, refs to atoms etc

                        //Append chgrpNo
                        CMLScalar cgNo = new CMLScalar(number)
                        {
                            DictRef = "md:cgNumber"
                        };
                        cgMol.Add(cgNo);

                        // prefix for residue atom id
                        string cprefix = "cg" + number;

                        //Append atoms from chargeGroup as it is an AC
                        CMLAtomArray ar = new CMLAtomArray();
                        for (int i = 0; i < chargeGroup.Atoms.Count; i++)
                        {
                            CMLAtom cmlAtom = new CMLAtom
                            {
                                // setRef to keep consistent usage
                                // setId to satisfy Jumbo 5.4 need for all atoms to have id
                                Ref = chargeGroup.Atoms[i].Id,
                                Id  = cprefix + "_" + chargeGroup.Atoms[i].Id
                            };

                            //Append switching atom?
                            if (chargeGroup.Atoms[i].Equals(chargeGroup.GetSwitchingAtom()))
                            {
                                CMLScalar scalar = new CMLScalar
                                {
                                    DictRef = "md:switchingAtom"
                                };
                                cmlAtom.Add(scalar);
                            }
                            ar.Add(cmlAtom);
                        }
                        cgMol.Add(ar);

                        molToCustomize.Add(cgMol);
                    }
                }
            }
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
0
        public CMLAtom CDKAtomToCMLAtom(IAtomContainer container, IAtom cdkAtom)
        {
            var cmlAtom = new CMLAtom();

            this.CheckPrefix(cmlAtom);
            AddAtomID(cdkAtom, cmlAtom);
            AddDictRef(cdkAtom, cmlAtom);
            cmlAtom.ElementType = cdkAtom.Symbol;
            if (cdkAtom is IPseudoAtom)
            {
                var label = ((IPseudoAtom)cdkAtom).Label;
                if (label != null)
                {
                    cmlAtom.Title = label;
                }
                cmlAtom.ElementType = "Du";
            }
            Map2DCoordsToCML(cmlAtom, cdkAtom);
            Map3DCoordsToCML(cmlAtom, cdkAtom);
            MapFractionalCoordsToCML(cmlAtom, cdkAtom);

            var formalCharge = cdkAtom.FormalCharge;

            if (formalCharge != null)
            {
                cmlAtom.FormalCharge = formalCharge.Value;
            }

            // CML's hydrogen count consists of the sum of implicit and explicit
            // hydrogens (see bug #1655045).
            var totalHydrogen = cdkAtom.ImplicitHydrogenCount;

            if (totalHydrogen != null)
            {
                if (container != null)
                {
                    foreach (var bond in container.GetConnectedBonds(cdkAtom))
                    {
                        foreach (var atom in bond.Atoms)
                        {
                            if (AtomicNumbers.H.Equals(atom.AtomicNumber) && atom != cdkAtom)
                            {
                                totalHydrogen++;
                            }
                        }
                    }
                } // else: it is the implicit hydrogen count
                cmlAtom.HydrogenCount = totalHydrogen.Value;
            }     // else: don't report it, people can count the explicit Hs themselves

            var massNumber = cdkAtom.MassNumber;

            if (!(cdkAtom is IPseudoAtom))
            {
                if (massNumber != null)
                {
                    cmlAtom.IsotopeNumber = massNumber.Value;
                }
            }

            if (cdkAtom.Charge != null)
            {
                var scalar = new CMLScalar();
                this.CheckPrefix(scalar);
                scalar.DictRef = "cdk:partialCharge";
                scalar.SetValue(cdkAtom.Charge.Value);
                cmlAtom.Add(scalar);
            }
            WriteProperties(cdkAtom, cmlAtom);

            if (cdkAtom.IsAromatic)
            {
                var aromAtom = new CMLScalar {
                    DictRef = "cdk:aromaticAtom"
                };
                cmlAtom.Add(aromAtom);
            }

            foreach (var element in customizers.Keys)
            {
                var customizer = customizers[element];
                try
                {
                    customizer.Customize(cdkAtom, cmlAtom);
                }
                catch (Exception exception)
                {
                    Trace.TraceError("Error while customizing CML output with customizer: ", customizer.GetType().Name);
                    Debug.WriteLine(exception);
                }
            }
            return(cmlAtom);
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
0
        private CMLReaction CDKReactionToCMLReaction(IReaction reaction, bool setIDs)
        {
            var cmlReaction = new CMLReaction();

            if (useCMLIDs && setIDs)
            {
                IDCreator.CreateIDs(reaction);
            }
            if (!string.IsNullOrEmpty(reaction.Id))
            {
                cmlReaction.Id = reaction.Id;
            }

            var props = reaction.GetProperties();

            foreach (var key in props.Keys)
            {
                var value = props[key];
                if (value is string)
                {
                    if (!string.Equals(key.ToString(), CDKPropertyName.Title, StringComparison.Ordinal))
                    {
                        var scalar = new CMLScalar();
                        this.CheckPrefix(scalar);
                        scalar.DictRef = "cdk:reactionProperty";
                        scalar.Title   = key.ToString();
                        scalar.SetValue(value.ToString());
                        cmlReaction.Add(scalar);
                    }
                }
            }

            // reactants
            var cmlReactants = new CMLReactantList();

            foreach (var reactant in reaction.Reactants)
            {
                var cmlReactant = new CMLReactant();
                cmlReactant.Add(CDKAtomContainerToCMLMolecule(reactant));
                cmlReactants.Add(cmlReactant);
            }

            // products
            var cmlProducts = new CMLProductList();

            foreach (var product in reaction.Products)
            {
                var cmlProduct = new CMLProduct();
                cmlProduct.Add(CDKAtomContainerToCMLMolecule(product));
                cmlProducts.Add(cmlProduct);
            }

            // substance
            var cmlSubstances = new CMLSubstanceList();

            foreach (var agent in reaction.Agents)
            {
                var cmlSubstance = new CMLSubstance();
                cmlSubstance.Add(CDKAtomContainerToCMLMolecule(agent));
                cmlSubstances.Add(cmlSubstance);
            }
            if (reaction.Id != null)
            {
                cmlReaction.Id = reaction.Id;
            }

            cmlReaction.Add(cmlReactants);
            cmlReaction.Add(cmlProducts);
            cmlReaction.Add(cmlSubstances);
            return(cmlReaction);
        }
Ejemplo n.º 9
0
        public void Customize(IAtom atom, object nodeToAdd)
        {
            if (!(nodeToAdd is XElement))
            {
                throw new CDKException("NodeToAdd must be of type nu.xom.Element!");
            }

            XElement element = (XElement)nodeToAdd;

            if (atom is IPDBAtom pdbAtom)
            {
                if (HasContent(pdbAtom.AltLoc))
                {
                    var scalar = new CMLScalar
                    {
                        DictRef = "pdb:altLoc"
                    };
                    scalar.Add(pdbAtom.AltLoc);
                    element.Add(scalar);
                }

                if (HasContent(pdbAtom.ChainID))
                {
                    var scalar = new CMLScalar
                    {
                        DictRef = "pdb:chainID"
                    };
                    scalar.Add(pdbAtom.ChainID);
                    element.Add(scalar);
                }

                {
                    var scalar = new CMLScalar
                    {
                        DictRef = "pdb:hetAtom"
                    };
                    scalar.Add("" + pdbAtom.HetAtom);
                    element.Add(scalar);
                }

                if (HasContent(pdbAtom.ICode))
                {
                    var scalar = new CMLScalar
                    {
                        DictRef = "pdb:iCode"
                    };
                    scalar.Add(pdbAtom.ICode);
                    element.Add(scalar);
                }

                if (HasContent(pdbAtom.Name))
                {
                    var scalar = new CMLLabel
                    {
                        DictRef = "pdb:name"
                    };
                    scalar.Add(pdbAtom.Name);
                    element.Add(scalar);
                }

                {
                    var scalar = new CMLScalar
                    {
                        DictRef = "pdb:oxt"
                    };
                    scalar.Add("" + pdbAtom.Oxt);
                    element.Add(scalar);
                }

                if (HasContent(pdbAtom.Record))
                {
                    var scalar = new CMLScalar
                    {
                        DictRef = "pdb:record"
                    };
                    scalar.Add(pdbAtom.Record);
                    element.Add(scalar);
                }

                if (HasContent(pdbAtom.ResName))
                {
                    var scalar = new CMLScalar
                    {
                        DictRef = "pdb:resName"
                    };
                    scalar.Add(pdbAtom.ResName);
                    element.Add(scalar);
                }

                if (HasContent(pdbAtom.ResSeq))
                {
                    var scalar = new CMLScalar
                    {
                        DictRef = "pdb:resSeq"
                    };
                    scalar.Add(pdbAtom.ResSeq);
                    element.Add(scalar);
                }

                if (HasContent(pdbAtom.SegID))
                {
                    var scalar = new CMLScalar
                    {
                        DictRef = "pdb:segID"
                    };
                    scalar.Add(pdbAtom.SegID);
                    element.Add(scalar);
                }

                if (pdbAtom.Serial != 0)
                {
                    var scalar = new CMLScalar
                    {
                        DictRef = "pdb:serial"
                    };
                    scalar.Add("" + pdbAtom.Serial);
                    element.Add(scalar);
                }

                if (pdbAtom.TempFactor != -1.0)
                {
                    var scalar = new CMLScalar
                    {
                        DictRef = "pdb:tempFactor"
                    };
                    scalar.Add("" + pdbAtom.TempFactor);
                    element.Add(scalar);
                }

                element.SetAttributeValue("occupancy", "" + pdbAtom.Occupancy);

                // remove isotope info
                element.SetAttributeValue("isotopeNumber", null);
            }
        }