Exemple #1
0
        /// <summary> Read a Reaction from a file in MDL RXN format
        ///
        /// </summary>
        /// <returns>  The Reaction that was read from the MDL file.
        /// </returns>
        private IMolecule readMolecule(IMolecule molecule)
        {
            AtomTypeFactory atFactory = null;

            try
            {
                atFactory = AtomTypeFactory.getInstance("mol2_atomtypes.xml", molecule.Builder);
            }
            catch (System.Exception exception)
            {
                System.String error = "Could not instantiate an AtomTypeFactory";
                //logger.error(error);
                //logger.debug(exception);
                throw new CDKException(error, exception);
            }
            try
            {
                System.String line      = input.ReadLine();
                int           atomCount = 0;
                int           bondCount = 0;
                while (line != null)
                {
                    if (line.StartsWith("@<TRIPOS>MOLECULE"))
                    {
                        //logger.info("Reading molecule block");
                        // second line has atom/bond counts?
                        input.ReadLine(); // disregard the name line
                        System.String          counts    = input.ReadLine();
                        SupportClass.Tokenizer tokenizer = new SupportClass.Tokenizer(counts);
                        try
                        {
                            atomCount = System.Int32.Parse(tokenizer.NextToken());
                        }
                        catch (System.FormatException nfExc)
                        {
                            System.String error = "Error while reading atom count from MOLECULE block";
                            //logger.error(error);
                            //logger.debug(nfExc);
                            throw new CDKException(error, nfExc);
                        }
                        if (tokenizer.HasMoreTokens())
                        {
                            try
                            {
                                bondCount = System.Int32.Parse(tokenizer.NextToken());
                            }
                            catch (System.FormatException nfExc)
                            {
                                System.String error = "Error while reading atom and bond counts";
                                //logger.error(error);
                                //logger.debug(nfExc);
                                throw new CDKException(error, nfExc);
                            }
                        }
                        else
                        {
                            bondCount = 0;
                        }
                        //logger.info("Reading #atoms: ", atomCount);
                        //logger.info("Reading #bonds: ", bondCount);

                        //logger.warn("Not reading molecule qualifiers");
                    }
                    else if (line.StartsWith("@<TRIPOS>ATOM"))
                    {
                        //logger.info("Reading atom block");
                        for (int i = 0; i < atomCount; i++)
                        {
                            line = input.ReadLine().Trim();
                            SupportClass.Tokenizer tokenizer = new SupportClass.Tokenizer(line);
                            tokenizer.NextToken(); // disregard the id token
                            System.String nameStr     = tokenizer.NextToken();
                            System.String xStr        = tokenizer.NextToken();
                            System.String yStr        = tokenizer.NextToken();
                            System.String zStr        = tokenizer.NextToken();
                            System.String atomTypeStr = tokenizer.NextToken();
                            IAtomType     atomType    = atFactory.getAtomType(atomTypeStr);
                            if (atomType == null)
                            {
                                atomType = atFactory.getAtomType("X");
                                //logger.error("Could not find specified atom type: ", atomTypeStr);
                            }
                            IAtom atom = molecule.Builder.newAtom("X");
                            atom.ID           = nameStr;
                            atom.AtomTypeName = atomTypeStr;
                            atFactory.configure(atom);
                            try
                            {
                                double x = System.Double.Parse(xStr);
                                double y = System.Double.Parse(yStr);
                                double z = System.Double.Parse(zStr);
                                atom.setPoint3d(new Point3d(x, y, z));
                            }
                            catch (System.FormatException nfExc)
                            {
                                System.String error = "Error while reading atom coordinates";
                                //logger.error(error);
                                //logger.debug(nfExc);
                                throw new CDKException(error, nfExc);
                            }
                            molecule.addAtom(atom);
                        }
                    }
                    else if (line.StartsWith("@<TRIPOS>BOND"))
                    {
                        //logger.info("Reading bond block");
                        for (int i = 0; i < bondCount; i++)
                        {
                            line = input.ReadLine();
                            SupportClass.Tokenizer tokenizer = new SupportClass.Tokenizer(line);
                            tokenizer.NextToken(); // disregard the id token
                            System.String atom1Str = tokenizer.NextToken();
                            System.String atom2Str = tokenizer.NextToken();
                            System.String orderStr = tokenizer.NextToken();
                            try
                            {
                                int    atom1 = System.Int32.Parse(atom1Str);
                                int    atom2 = System.Int32.Parse(atom2Str);
                                double order = 0;
                                if ("1".Equals(orderStr))
                                {
                                    order = CDKConstants.BONDORDER_AROMATIC;
                                }
                                else if ("2".Equals(orderStr))
                                {
                                    order = CDKConstants.BONDORDER_DOUBLE;
                                }
                                else if ("3".Equals(orderStr))
                                {
                                    order = CDKConstants.BONDORDER_TRIPLE;
                                }
                                else if ("am".Equals(orderStr))
                                {
                                    order = CDKConstants.BONDORDER_SINGLE;
                                }
                                else if ("ar".Equals(orderStr))
                                {
                                    order = CDKConstants.BONDORDER_AROMATIC;
                                }
                                else if ("du".Equals(orderStr))
                                {
                                    order = CDKConstants.BONDORDER_SINGLE;
                                }
                                else if ("un".Equals(orderStr))
                                {
                                    order = CDKConstants.BONDORDER_SINGLE;
                                }
                                else if ("nc".Equals(orderStr))
                                {
                                    // not connected
                                    order = 0;
                                }
                                if (order != 0)
                                {
                                    molecule.addBond(atom1 - 1, atom2 - 1, order);
                                }
                            }
                            catch (System.FormatException nfExc)
                            {
                                System.String error = "Error while reading bond information";
                                //logger.error(error);
                                //logger.debug(nfExc);
                                throw new CDKException(error, nfExc);
                            }
                        }
                    }
                    line = input.ReadLine();
                }
            }
            catch (System.IO.IOException exception)
            {
                System.String error = "Error while reading general structure";
                //logger.error(error);
                //logger.debug(exception);
                throw new CDKException(error, exception);
            }
            return(molecule);
        }
Exemple #2
0
        /// <summary> Creates an <code>Atom</code> and sets properties to their values from
        /// the ATOM record. If the line is shorter than 80 characters, the information
        /// past 59 characters is treated as optional. If the line is shorter than 59
        /// characters, a <code>RuntimeException</code> is thrown.
        ///
        /// </summary>
        /// <param name="cLine"> the PDB ATOM record.
        /// </param>
        /// <returns> the <code>Atom</code> created from the record.
        /// </returns>
        /// <throws>  RuntimeException if the line is too short (less than 59 characters). </throws>
        private PDBAtom readAtom(string cLine, int lineLength)
        {
            // a line looks like:
            // 0         1         2         3         4
            // 01234567890123456789012345678901234567890123456789012345678901234567890123456789
            // ATOM      1  O5*   C A   1      20.662  36.632  23.475  1.00 10.00      114D  45
            // ATOM   1186 1H   ALA     1      10.105   5.945  -6.630  1.00  0.00      1ALE1288

            if (lineLength < 59)
            {
                throw new System.SystemException("PDBReader error during readAtom(): line too short");
            }
            string elementSymbol = cLine.Substring(12, 4).Trim();

            if (elementSymbol.Length == 2)
            {
                // ensure that the second char is lower case
                elementSymbol = elementSymbol[0] + elementSymbol.Substring(1).ToLower();
            }
            string rawAtomName = cLine.Substring(12, 4).Trim();
            string resName     = cLine.Substring(17, 3).Trim();

            try
            {
                IAtomType type = pdbFactory.getAtomType(resName + "." + rawAtomName);
                elementSymbol = type.Symbol;
            }
            catch (NoSuchAtomTypeException e)
            {
                // try 1 char
                try
                {
                    elementSymbol = rawAtomName.Substring(0, 1);//pdbFactory.getAtomType(rawAtomName.Substring(0, 1)).Symbol;
                }
                catch
                {
                    elementSymbol = "Xx";
                }
                //System.Console.Out.WriteLine("Did not recognize PDB atom type: " + resName + "." + rawAtomName);
            }
            PDBAtom oAtom = new PDBAtom(elementSymbol, new Point3d(System.Double.Parse(cLine.Substring(30, (38) - (30))), System.Double.Parse(cLine.Substring(38, (46) - (38))), System.Double.Parse(cLine.Substring(46, (54) - (46)))));

            oAtom.Record       = cLine;
            oAtom.Serial       = System.Int32.Parse(cLine.Substring(6, 5).Trim());
            oAtom.Name         = rawAtomName.Trim();
            oAtom.AltLoc       = cLine.Substring(16, 1).Trim();
            oAtom.ResName      = resName;
            oAtom.ChainID      = cLine.Substring(21, (22) - (21)).Trim();
            oAtom.ResSeq       = cLine.Substring(22, (26) - (22)).Trim();
            oAtom.ICode        = cLine.Substring(26, (27) - (26)).Trim();
            oAtom.AtomTypeName = oAtom.ResName + "." + rawAtomName;
            if (lineLength >= 59)
            {
                string frag = cLine.Substring(54, (60) - (54)).Trim();
                if (frag.Length > 0)
                {
                    oAtom.Occupancy = System.Double.Parse(frag);
                }
            }
            if (lineLength >= 65)
            {
                string frag = cLine.Substring(60, (66) - (60)).Trim();
                if (frag.Length > 0)
                {
                    oAtom.TempFactor = System.Double.Parse(frag);
                }
            }
            if (lineLength >= 75)
            {
                oAtom.SegID = cLine.Substring(72, (76) - (72)).Trim();
            }
            //		if (lineLength >= 78) {
            //            oAtom.setSymbol((new String(cLine.substring(76, 78))).trim());
            //		}
            if (lineLength >= 79)
            {
                string frag = cLine.Substring(78, (80) - (78)).Trim();
                if (frag.Length > 0)
                {
                    oAtom.setCharge(System.Double.Parse(frag));
                }
            }

            /*************************************************************************************
             * It sets a flag in the property content of an atom,
             * which is used when bonds are created to check if the atom is an OXT-record => needs
             * special treatment.
             */
            string oxt = cLine.Substring(13, (16) - (13)).Trim();

            if (oxt.Equals("OXT"))
            {
                oAtom.Oxt = true;
            }
            else
            {
                oAtom.Oxt = false;
            }
            /*************************************************************************************/

            return(oAtom);
        }