示例#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);
        }