Example #1
0
        public IReadOnlyList <IAtomType> ReadAtomTypes()
        {
            var ret = new List <IAtomType>();

            var doc = XElement.Load(input);

            foreach (var atomTypeElm in doc.Elements(XName_AtomType))
            {
                var anAtomType = builder.NewAtomType("H");
                anAtomType.AtomicNumber = 0;
                anAtomType.AtomTypeName = atomTypeElm.Attribute(XName_ID)?.Value;

                int       piBondCount   = 0;
                int       neighborCount = 0;
                BondOrder maxBondOrder  = BondOrder.Unset;
                double    bondOrderSum  = 0.0;

                foreach (var elm in atomTypeElm.Elements())
                {
                    if (elm.Name == XName_hasElement)
                    {
                        var aa = elm.Attribute(XName_rdf_resource)?.Value;
                        anAtomType.Symbol = aa.Substring(aa.IndexOf('#') + 1);
                    }
                    else if (elm.Name == XName_formalBondType)
                    {
                        neighborCount++;
                        var aa        = elm.Attribute(XName_rdf_resource)?.Value;
                        var bondType  = aa.Substring(aa.IndexOf('#') + 1);
                        int bondOrder = 0;
                        switch (bondType)
                        {
                        case "single":
                            bondOrder = 1;
                            break;

                        case "double":
                            bondOrder = 2;
                            break;

                        case "triple":
                            bondOrder = 3;
                            break;

                        case "quadruple":
                            bondOrder = 4;
                            break;

                        default:
                            throw new Exception();
                        }
                        maxBondOrder  = BondManipulator.GetMaximumBondOrder(maxBondOrder, (BondOrder)bondOrder);
                        piBondCount  += (bondOrder - 1);
                        bondOrderSum += bondOrder;
                    }
                    else if (elm.Name == XName_hybridization)
                    {
                        var aa            = elm.Attribute(XName_rdf_resource)?.Value;
                        var hybridization = aa.Substring(aa.IndexOf('#') + 1);
                        anAtomType.Hybridization = HybridizationTools.ToHybridization(hybridization);
                    }
                    else if (elm.Name == XName_formalCharge)
                    {
                        anAtomType.FormalCharge = int.Parse(elm.Value, NumberFormatInfo.InvariantInfo);
                    }
                    else if (elm.Name == XName_formalNeighbourCount)
                    {
                        neighborCount = int.Parse(elm.Value, NumberFormatInfo.InvariantInfo);
                    }
                    else if (elm.Name == XName_lonePairCount)
                    {
                        anAtomType.SetProperty(CDKPropertyName.LonePairCount, int.Parse(elm.Value, NumberFormatInfo.InvariantInfo));
                    }
                    else if (elm.Name == XName_singleElectronCount)
                    {
                        anAtomType.SetProperty(CDKPropertyName.SingleElectronCount, int.Parse(elm.Value, NumberFormatInfo.InvariantInfo));
                    }
                    else if (elm.Name == XName_piBondCount)
                    {
                        piBondCount = int.Parse(elm.Value, NumberFormatInfo.InvariantInfo);
                    }
                }

                anAtomType.SetProperty(CDKPropertyName.PiBondCount, piBondCount);
                anAtomType.FormalNeighbourCount = neighborCount;
                if (maxBondOrder != BondOrder.Unset)
                {
                    anAtomType.MaxBondOrder = maxBondOrder;
                }
                if (bondOrderSum > 0.1)
                {
                    anAtomType.BondOrderSum = bondOrderSum;
                }

                ret.Add(anAtomType);
            }
            return(ret);
        }
Example #2
0
        /// <summary>
        /// Reads the atom types from the data file.
        /// </summary>
        /// <returns><see cref="IEnumerable{T}"/> with atom types. Is empty if some reading error occurred.</returns>
        public IEnumerable <IAtomType> ReadAtomTypes()
        {
            var settings = new XmlReaderSettings
            {
                ValidationType  = ValidationType.None,
                ValidationFlags = System.Xml.Schema.XmlSchemaValidationFlags.None
            };
            XmlReader reader = XmlReader.Create(input, settings);

            var doc       = XElement.Load(reader);
            var nAtomType = doc.Name.Namespace + "atomType";
            var nAtom     = doc.Name.Namespace + "atom";
            var nLabel    = doc.Name.Namespace + "label";
            var nScalar   = doc.Name.Namespace + "scalar";

            foreach (var elementAtomType in doc.Elements(nAtomType))
            {
                var atomType = builder.NewAtomType("R");
                atomType.AtomTypeName = elementAtomType.Attribute("id")?.Value;
                foreach (var elm in elementAtomType.Descendants())
                {
                    // lazy compare
                    switch (elm.Name.LocalName)
                    {
                    case "atom":
                        atomType.Symbol = elm.Attribute("elementType")?.Value;
                        var sFormalCharge = elm.Attribute("formalCharge")?.Value;
                        if (sFormalCharge != null)
                        {
                            atomType.FormalCharge = int.Parse(sFormalCharge, NumberFormatInfo.InvariantInfo);
                        }
                        break;

                    case "label":
                        var aValue = elm.Attribute("value");
                        if (aValue != null)
                        {
                            if (atomType.AtomTypeName != null)
                            {
                                atomType.Id = atomType.AtomTypeName;
                            }
                            atomType.AtomTypeName = aValue.Value;
                        }
                        break;

                    case "scalar":
                        var dictRef = elm.Attribute("dictRef")?.Value;
                        var value   = string.IsNullOrWhiteSpace(elm.Value) ? null : elm.Value;
                        if (value != null)
                        {
                            switch (dictRef)
                            {
                            case "cdk:bondOrderSum":
                                atomType.BondOrderSum = double.Parse(value, NumberFormatInfo.InvariantInfo);
                                break;

                            case "cdk:maxBondOrder":
                                atomType.MaxBondOrder = (BondOrder)(int)double.Parse(value, NumberFormatInfo.InvariantInfo);
                                break;

                            case "cdk:formalNeighbourCount":
                                atomType.FormalNeighbourCount = (int)double.Parse(value, NumberFormatInfo.InvariantInfo);
                                break;

                            case "cdk:valency":
                                atomType.Valency = (int)double.Parse(value, NumberFormatInfo.InvariantInfo);
                                break;

                            case "cdk:formalCharge":
                                atomType.FormalCharge = (int)double.Parse(value, NumberFormatInfo.InvariantInfo);
                                break;

                            case "cdk:hybridization":
                                atomType.Hybridization = HybridizationTools.ToHybridization(value);
                                break;

                            case "cdk:DA":
                                switch (value)
                                {
                                case "A":
                                    atomType.IsHydrogenBondAcceptor |= true;
                                    break;

                                case "D":
                                    atomType.IsHydrogenBondDonor |= true;
                                    break;

                                default:
                                    Trace.TraceWarning($"Unrecognized H-bond donor/acceptor pattern in configuration file: {value}");
                                    break;
                                }
                                break;

                            case "cdk:sphericalMatcher":
                                atomType.SetProperty(CDKPropertyName.SphericalMatcher, value);
                                break;

                            case "cdk:ringSize":
                                atomType.SetProperty(CDKPropertyName.PartOfRingOfSize, int.Parse(value, NumberFormatInfo.InvariantInfo));
                                break;

                            case "cdk:ringConstant":
                                atomType.SetProperty(CDKPropertyName.ChemicalGroupConstant, int.Parse(value, NumberFormatInfo.InvariantInfo));
                                break;

                            case "cdk:aromaticAtom":
                                atomType.IsAromatic |= true;
                                break;

                            case "emboss:vdwrad":
                                break;

                            case "cdk:piBondCount":
                                atomType.SetProperty(CDKPropertyName.PiBondCount, int.Parse(value, NumberFormatInfo.InvariantInfo));
                                break;

                            case "cdk:lonePairCount":
                                atomType.SetProperty(CDKPropertyName.LonePairCount, int.Parse(value, NumberFormatInfo.InvariantInfo));
                                break;

                            default:
                                break;
                            }
                        }
                        break;

                    default:
                        break;
                    }
                }
                yield return(atomType);
            }
            yield break;
        }