Example #1
0
        /// <summary>
        /// Reads a text based configuration file.
        /// </summary>
        /// <returns>A <see cref="IEnumerable{IAtomType}"/> with read <see cref="IAtomType"/>'s.</returns>
        /// <exception cref="IOException">when a problem occurred with reading from the <see cref="GetStream()"/></exception>
        public IEnumerable <IAtomType> ReadAtomTypes()
        {
            if (GetStream() == null)
            {
                SetStream(ResourceLoader.GetAsStream(configFile));
            }

            using (var reader = new StreamReader(GetStream()))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    if (line.StartsWithChar('#'))
                    {
                        continue;
                    }
                    var tokens = line.Split("\t ,;".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    if (tokens.Length != 9)
                    {
                        throw new IOException("AtomTypeTable.ReadAtypes: " + "Wrong Number of fields");
                    }

                    IAtomType atomType;
                    try
                    {
                        var name     = tokens[0];
                        var rootType = tokens[1];
                        var san      = tokens[2];
                        var sam      = tokens[3];
                        // skip the vdw radius value
                        var scovalent = tokens[5];
                        var sColorR   = tokens[6];
                        var sColorG   = tokens[7];
                        var sColorB   = tokens[8];

                        var mass         = double.Parse(sam, NumberFormatInfo.InvariantInfo);
                        var covalent     = double.Parse(scovalent, NumberFormatInfo.InvariantInfo);
                        var atomicNumber = int.Parse(san, NumberFormatInfo.InvariantInfo);
                        var colorR       = int.Parse(sColorR, NumberFormatInfo.InvariantInfo);
                        var colorG       = int.Parse(sColorG, NumberFormatInfo.InvariantInfo);
                        var colorB       = int.Parse(sColorB, NumberFormatInfo.InvariantInfo);

                        atomType = builder.NewAtomType(name, rootType);
                        atomType.AtomicNumber   = atomicNumber;
                        atomType.ExactMass      = mass;
                        atomType.CovalentRadius = covalent;
                        atomType.SetProperty(CDKPropertyName.Color, CDKPropertyName.RGB2Int(colorR, colorG, colorB));
                    }
                    catch (FormatException)
                    {
                        throw new IOException("AtomTypeTable.ReadAtypes: " + "Malformed Number");
                    }
                    yield return(atomType);
                }
            }
            yield break;
        }
        /// <summary>
        /// Read and stores the atom types in a vector
        /// </summary>
        /// <exception cref="Exception">Description of the Exception</exception>
        private void SetAtomTypes()
        {
            string name     = "";
            string rootType = "";
            //int an = 0;
            int rl      = 255;
            int gl      = 20;
            int bl      = 147;
            int maxbond = 0;
            int atomNr  = 0;

            double mass = 0.0;

            st.MoveNext();
            string sid = st.Current; st.MoveNext();

            rootType = st.Current; st.MoveNext();
            string smaxbond = st.Current; st.MoveNext();
            string satomNr  = st.Current; st.MoveNext();
            string smass    = st.Current; st.MoveNext();

            name = st.Current; st.MoveNext();

            try
            {
                maxbond = int.Parse(smaxbond, NumberFormatInfo.InvariantInfo);
                mass    = double.Parse(smass, NumberFormatInfo.InvariantInfo);
                atomNr  = int.Parse(satomNr, NumberFormatInfo.InvariantInfo);
            }
            catch (FormatException)
            {
                throw new IOException("AtomTypeTable.ReadAtypes: " + "Malformed Number");
            }

            IAtomType atomType = builder.NewAtomType(name, rootType);

            atomType.AtomicNumber         = atomNr;
            atomType.ExactMass            = mass;
            atomType.MassNumber           = MassNumber(atomNr, mass);
            atomType.FormalNeighbourCount = maxbond;
            atomType.Symbol = rootType;
            var co = CDKPropertyName.RGB2Int(rl, gl, bl);

            atomType.SetProperty(CDKPropertyName.Color, co);
            atomType.AtomTypeName = sid;
            atomTypes.Add(atomType);
        }