Ejemplo n.º 1
0
        /// <summary> Performs a breadthFirstSearch in an AtomContainer starting with a
        /// particular sphere, which usually consists of one start atom. While
        /// searching the graph, the method marks each visited atom. It then puts all
        /// the atoms connected to the atoms in the given sphere into a new vector
        /// which forms the sphere to search for the next recursive method call. All
        /// atoms that have been visited are put into a molecule container. This
        /// breadthFirstSearch does thus find the connected graph for a given start
        /// atom.
        ///
        /// </summary>
        /// <param name="ac">      The AtomContainer to be searched
        /// </param>
        /// <param name="sphere">  A sphere of atoms to start the search with
        /// </param>
        /// <param name="molecule">A molecule into which all the atoms and bonds are stored
        /// that are found during search
        /// </param>
        public static void breadthFirstSearch(IAtomContainer ac, System.Collections.ArrayList sphere, IMolecule molecule, int max)
        {
            IAtom atom;
            IAtom nextAtom;

            System.Collections.ArrayList newSphere = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));
            for (int f = 0; f < sphere.Count; f++)
            {
                atom = (IAtom)sphere[f];
                //System.out.println("atoms  "+ atom + f);
                //System.out.println("sphere size  "+ sphere.size());
                molecule.addAtom(atom);
                // first copy LonePair's and SingleElectron's of this Atom as they need
                // to be copied too
                IElectronContainer[] eContainers = ac.getConnectedElectronContainers(atom);
                //System.out.println("found #ec's: " + eContainers.length);
                for (int i = 0; i < eContainers.Length; i++)
                {
                    if (!(eContainers[i] is IBond))
                    {
                        // ok, no bond, thus LonePair or SingleElectron
                        // System.out.println("adding non bond " + eContainers[i]);
                        molecule.addElectronContainer(eContainers[i]);
                    }
                }
                // now look at bonds
                IBond[] bonds = ac.getConnectedBonds(atom);
                for (int g = 0; g < bonds.Length; g++)
                {
                    if (!bonds[g].getFlag(CDKConstants.VISITED))
                    {
                        molecule.addBond(bonds[g]);
                        bonds[g].setFlag(CDKConstants.VISITED, true);
                    }
                    nextAtom = bonds[g].getConnectedAtom(atom);
                    if (!nextAtom.getFlag(CDKConstants.VISITED))
                    {
                        //					System.out.println("wie oft???");
                        newSphere.Add(nextAtom);
                        nextAtom.setFlag(CDKConstants.VISITED, true);
                    }
                }
                if (max > -1 && molecule.AtomCount > max)
                {
                    return;
                }
            }
            if (newSphere.Count > 0)
            {
                breadthFirstSearch(ac, newSphere, molecule, max);
            }
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        /// <summary>  Read a Molecule from a file in MDL sd format
        ///
        /// </summary>
        /// <returns>    The Molecule that was read from the MDL file.
        /// </returns>
        private IMolecule readMolecule(IMolecule molecule)
        {
            //logger.debug("Reading new molecule");
            int linecount     = 0;
            int atoms         = 0;
            int bonds         = 0;
            int atom1         = 0;
            int atom2         = 0;
            int order         = 0;
            int stereo        = 0;
            int RGroupCounter = 1;
            int Rnumber       = 0;

            System.String[] rGroup = null;
            double          x      = 0.0;
            double          y      = 0.0;
            double          z      = 0.0;
            double          totalZ = 0.0;
            //int[][] conMat = new int[0][0];
            //String help;
            IBond bond;
            IAtom atom;

            System.String line = "";

            try
            {
                //logger.info("Reading header");
                line = input.ReadLine(); linecount++;
                if (line == null)
                {
                    return(null);
                }
                //logger.debug("Line " + linecount + ": " + line);

                if (line.StartsWith("$$$$"))
                {
                    //logger.debug("File is empty, returning empty molecule");
                    return(molecule);
                }
                if (line.Length > 0)
                {
                    molecule.setProperty(CDKConstants.TITLE, line);
                }
                line = input.ReadLine(); linecount++;
                //logger.debug("Line " + linecount + ": " + line);
                line = input.ReadLine(); linecount++;
                //logger.debug("Line " + linecount + ": " + line);
                if (line.Length > 0)
                {
                    molecule.setProperty(CDKConstants.REMARK, line);
                }

                //logger.info("Reading rest of file");
                line = input.ReadLine(); linecount++;
                //logger.debug("Line " + linecount + ": " + line);
                atoms = System.Int32.Parse(line.Substring(0, (3) - (0)).Trim());
                //logger.debug("Atomcount: " + atoms);
                bonds = System.Int32.Parse(line.Substring(3, (6) - (3)).Trim());
                //logger.debug("Bondcount: " + bonds);

                // read ATOM block
                //logger.info("Reading atom block");
                for (int f = 0; f < atoms; f++)
                {
                    line = input.ReadLine(); linecount++;
                    //UPGRADE_TODO: The differences in the format  of parameters for constructor 'java.lang.Double.Double'  may cause compilation errors.  "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'"
                    x = System.Double.Parse(line.Substring(0, (10) - (0)).Trim());
                    //UPGRADE_TODO: The differences in the format  of parameters for constructor 'java.lang.Double.Double'  may cause compilation errors.  "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'"
                    y = System.Double.Parse(line.Substring(10, (20) - (10)).Trim());
                    //UPGRADE_TODO: The differences in the format  of parameters for constructor 'java.lang.Double.Double'  may cause compilation errors.  "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'"
                    z       = System.Double.Parse(line.Substring(20, (30) - (20)).Trim());
                    totalZ += System.Math.Abs(z); // *all* values should be zero, not just the sum
                    //logger.debug("Coordinates: " + x + "; " + y + "; " + z);
                    System.String element = line.Substring(31, (34) - (31)).Trim();

                    //logger.debug("Atom type: ", element);
                    if (IsotopeFactory.getInstance(molecule.Builder).isElement(element))
                    {
                        atom = IsotopeFactory.getInstance(molecule.Builder).configure(molecule.Builder.newAtom(element));
                    }
                    else
                    {
                        //logger.debug("Atom ", element, " is not an regular element. Creating a PseudoAtom.");
                        //check if the element is R
                        rGroup = element.Split(new char[] { '^', 'R' }); // ????
                        if (rGroup.Length > 1)
                        {
                            try
                            {
                                Rnumber       = System.Int32.Parse(rGroup[(rGroup.Length - 1)]);
                                RGroupCounter = Rnumber;
                            }
                            catch (System.Exception ex)
                            {
                                Rnumber = RGroupCounter;
                                RGroupCounter++;
                            }
                            element = "R" + Rnumber;
                        }
                        atom = molecule.Builder.newPseudoAtom(element);
                    }

                    // store as 3D for now, convert to 2D (if totalZ == 0.0) later
                    atom.setPoint3d(new Point3d(x, y, z));

                    // parse further fields
                    System.String massDiffString = line.Substring(34, (36) - (34)).Trim();
                    //logger.debug("Mass difference: ", massDiffString);
                    if (!(atom is IPseudoAtom))
                    {
                        try
                        {
                            int massDiff = System.Int32.Parse(massDiffString);
                            if (massDiff != 0)
                            {
                                IIsotope major = IsotopeFactory.getInstance(molecule.Builder).getMajorIsotope(element);
                                atom.AtomicNumber = major.AtomicNumber + massDiff;
                            }
                        }
                        catch (System.Exception exception)
                        {
                            //logger.error("Could not parse mass difference field");
                        }
                    }
                    else
                    {
                        //logger.error("Cannot set mass difference for a non-element!");
                    }


                    System.String chargeCodeString = line.Substring(36, (39) - (36)).Trim();
                    //logger.debug("Atom charge code: ", chargeCodeString);
                    int chargeCode = System.Int32.Parse(chargeCodeString);
                    if (chargeCode == 0)
                    {
                        // uncharged species
                    }
                    else if (chargeCode == 1)
                    {
                        atom.setFormalCharge(+3);
                    }
                    else if (chargeCode == 2)
                    {
                        atom.setFormalCharge(+2);
                    }
                    else if (chargeCode == 3)
                    {
                        atom.setFormalCharge(+1);
                    }
                    else if (chargeCode == 4)
                    {
                    }
                    else if (chargeCode == 5)
                    {
                        atom.setFormalCharge(-1);
                    }
                    else if (chargeCode == 6)
                    {
                        atom.setFormalCharge(-2);
                    }
                    else if (chargeCode == 7)
                    {
                        atom.setFormalCharge(-3);
                    }

                    try
                    {
                        // read the mmm field as position 61-63
                        System.String reactionAtomIDString = line.Substring(60, (63) - (60)).Trim();
                        //logger.debug("Parsing mapping id: ", reactionAtomIDString);
                        try
                        {
                            int reactionAtomID = System.Int32.Parse(reactionAtomIDString);
                            if (reactionAtomID != 0)
                            {
                                atom.ID = reactionAtomIDString;
                            }
                        }
                        catch (System.Exception exception)
                        {
                            //logger.error("Mapping number ", reactionAtomIDString, " is not an integer.");
                            //logger.debug(exception);
                        }
                    }
                    catch (System.Exception exception)
                    {
                        // older mol files don't have all these fields...
                        //logger.warn("A few fields are missing. Older MDL MOL file?");
                    }

                    molecule.addAtom(atom);
                }

                // convert to 2D, if totalZ == 0
                if (totalZ == 0.0 && !forceReadAs3DCoords.Set)
                {
                    //logger.info("Total 3D Z is 0.0, interpreting it as a 2D structure");
                    IAtom[] atomsToUpdate = molecule.Atoms;
                    for (int f = 0; f < atomsToUpdate.Length; f++)
                    {
                        IAtom   atomToUpdate = atomsToUpdate[f];
                        Point3d p3d          = atomToUpdate.getPoint3d();
                        atomToUpdate.setPoint2d(new Point2d(p3d.x, p3d.y));
                        atomToUpdate.setPoint3d(null);
                    }
                }

                // read BOND block
                //logger.info("Reading bond block");
                for (int f = 0; f < bonds; f++)
                {
                    line  = input.ReadLine(); linecount++;
                    atom1 = System.Int32.Parse(line.Substring(0, (3) - (0)).Trim());
                    atom2 = System.Int32.Parse(line.Substring(3, (6) - (3)).Trim());
                    order = System.Int32.Parse(line.Substring(6, (9) - (6)).Trim());
                    if (line.Length > 12)
                    {
                        stereo = System.Int32.Parse(line.Substring(9, (12) - (9)).Trim());
                    }
                    else
                    {
                        //logger.warn("Missing expected stereo field at line: " + line);
                    }
                    //if (//logger.DebugEnabled)
                    //{
                    //    //logger.debug("Bond: " + atom1 + " - " + atom2 + "; order " + order);
                    //}
                    if (stereo == 1)
                    {
                        // MDL up bond
                        stereo = CDKConstants.STEREO_BOND_UP;
                    }
                    else if (stereo == 6)
                    {
                        // MDL down bond
                        stereo = CDKConstants.STEREO_BOND_DOWN;
                    }
                    else if (stereo == 4)
                    {
                        //MDL bond undefined
                        stereo = CDKConstants.STEREO_BOND_UNDEFINED;
                    }
                    // interpret CTfile's special bond orders
                    IAtom a1 = molecule.getAtomAt(atom1 - 1);
                    IAtom a2 = molecule.getAtomAt(atom2 - 1);
                    if (order == 4)
                    {
                        // aromatic bond
                        bond = molecule.Builder.newBond(a1, a2, CDKConstants.BONDORDER_AROMATIC, stereo);
                        // mark both atoms and the bond as aromatic
                        bond.setFlag(CDKConstants.ISAROMATIC, true);
                        a1.setFlag(CDKConstants.ISAROMATIC, true);
                        a2.setFlag(CDKConstants.ISAROMATIC, true);
                        molecule.addBond(bond);
                    }
                    else
                    {
                        bond = molecule.Builder.newBond(a1, a2, (double)order, stereo);
                        molecule.addBond(bond);
                    }
                }

                // read PROPERTY block
                //logger.info("Reading property block");
                while (true)
                {
                    line = input.ReadLine(); linecount++;
                    if (line == null)
                    {
                        throw new CDKException("The expected property block is missing!");
                    }
                    if (line.StartsWith("M  END"))
                    {
                        break;
                    }

                    bool lineRead = false;
                    if (line.StartsWith("M  CHG"))
                    {
                        // FIXME: if this is encountered for the first time, all
                        // atom charges should be set to zero first!
                        int infoCount             = System.Int32.Parse(line.Substring(6, (9) - (6)).Trim());
                        SupportClass.Tokenizer st = new SupportClass.Tokenizer(line.Substring(9));
                        for (int i = 1; i <= infoCount; i++)
                        {
                            System.String token      = st.NextToken();
                            int           atomNumber = System.Int32.Parse(token.Trim());
                            token = st.NextToken();
                            int charge = System.Int32.Parse(token.Trim());
                            molecule.getAtomAt(atomNumber - 1).setFormalCharge(charge);
                        }
                    }
                    else if (line.StartsWith("M  ISO"))
                    {
                        try
                        {
                            System.String          countString = line.Substring(6, (9) - (6)).Trim();
                            int                    infoCount   = System.Int32.Parse(countString);
                            SupportClass.Tokenizer st          = new SupportClass.Tokenizer(line.Substring(9));
                            for (int i = 1; i <= infoCount; i++)
                            {
                                int atomNumber = System.Int32.Parse(st.NextToken().Trim());
                                int absMass    = System.Int32.Parse(st.NextToken().Trim());
                                if (absMass != 0)
                                {
                                    IAtom isotope = molecule.getAtomAt(atomNumber - 1);
                                    isotope.MassNumber = absMass;
                                }
                            }
                        }
                        catch (System.FormatException exception)
                        {
                            //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.getMessage' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
                            System.String error = "Error (" + exception.Message + ") while parsing line " + linecount + ": " + line + " in property block.";
                            //logger.error(error);
                            throw new CDKException("NumberFormatException in isotope information on line: " + line, exception);
                        }
                    }
                    else if (line.StartsWith("M  RAD"))
                    {
                        try
                        {
                            System.String          countString = line.Substring(6, (9) - (6)).Trim();
                            int                    infoCount   = System.Int32.Parse(countString);
                            SupportClass.Tokenizer st          = new SupportClass.Tokenizer(line.Substring(9));
                            for (int i = 1; i <= infoCount; i++)
                            {
                                int atomNumber       = System.Int32.Parse(st.NextToken().Trim());
                                int spinMultiplicity = System.Int32.Parse(st.NextToken().Trim());
                                if (spinMultiplicity > 1)
                                {
                                    IAtom radical = molecule.getAtomAt(atomNumber - 1);
                                    for (int j = 2; j <= spinMultiplicity; j++)
                                    {
                                        // 2 means doublet -> one unpaired electron
                                        // 3 means triplet -> two unpaired electron
                                        molecule.addElectronContainer(molecule.Builder.newSingleElectron(radical));
                                    }
                                }
                            }
                        }
                        catch (System.FormatException exception)
                        {
                            //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.getMessage' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
                            System.String error = "Error (" + exception.Message + ") while parsing line " + linecount + ": " + line + " in property block.";
                            //logger.error(error);
                            throw new CDKException("NumberFormatException in radical information on line: " + line, exception);
                        }
                    }
                    else if (line.StartsWith("G  "))
                    {
                        try
                        {
                            System.String atomNumberString = line.Substring(3, (6) - (3)).Trim();
                            int           atomNumber       = System.Int32.Parse(atomNumberString);
                            //String whatIsThisString = line.substring(6,9).trim();

                            System.String atomName = input.ReadLine();

                            // convert Atom into a PseudoAtom
                            IAtom       prevAtom   = molecule.getAtomAt(atomNumber - 1);
                            IPseudoAtom pseudoAtom = molecule.Builder.newPseudoAtom(atomName);
                            if (prevAtom.getPoint2d() != null)
                            {
                                pseudoAtom.setPoint2d(prevAtom.getPoint2d());
                            }
                            if (prevAtom.getPoint3d() != null)
                            {
                                pseudoAtom.setPoint3d(prevAtom.getPoint3d());
                            }
                            AtomContainerManipulator.replaceAtomByAtom(molecule, prevAtom, pseudoAtom);
                        }
                        catch (System.FormatException exception)
                        {
                            //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.toString' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
                            System.String error = "Error (" + exception.ToString() + ") while parsing line " + linecount + ": " + line + " in property block.";
                            //logger.error(error);
                            throw new CDKException("NumberFormatException in group information on line: " + line, exception);
                        }
                    }
                    if (!lineRead)
                    {
                        //logger.warn("Skipping line in property block: ", line);
                    }
                }
            }
            catch (CDKException exception)
            {
                //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.getMessage' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
                System.String error = "Error while parsing line " + linecount + ": " + line + " -> " + exception.Message;
                //logger.error(error);
                //logger.debug(exception);
                throw exception;
            }
            catch (System.Exception exception)
            {
                //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.getMessage' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
                System.String error = "Error while parsing line " + linecount + ": " + line + " -> " + exception.Message;
                //logger.error(error);
                //logger.debug(exception);
                throw new CDKException(error, exception);
            }
            return(molecule);
        }
Ejemplo n.º 4
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;
        }
Ejemplo n.º 5
0
 /// <summary> Performs a breadthFirstSearch in an AtomContainer starting with a
 /// particular sphere, which usually consists of one start atom. While
 /// searching the graph, the method marks each visited atom. It then puts all
 /// the atoms connected to the atoms in the given sphere into a new vector
 /// which forms the sphere to search for the next recursive method call. All
 /// atoms that have been visited are put into a molecule container. This
 /// breadthFirstSearch does thus find the connected graph for a given start
 /// atom.
 /// 
 /// </summary>
 /// <param name="ac">      The AtomContainer to be searched
 /// </param>
 /// <param name="sphere">  A sphere of atoms to start the search with
 /// </param>
 /// <param name="molecule">A molecule into which all the atoms and bonds are stored
 /// that are found during search
 /// </param>
 public static void breadthFirstSearch(IAtomContainer ac, System.Collections.ArrayList sphere, IMolecule molecule, int max)
 {
     IAtom atom;
     IAtom nextAtom;
     System.Collections.ArrayList newSphere = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));
     for (int f = 0; f < sphere.Count; f++)
     {
         atom = (IAtom)sphere[f];
         //System.out.println("atoms  "+ atom + f);
         //System.out.println("sphere size  "+ sphere.size());
         molecule.addAtom(atom);
         // first copy LonePair's and SingleElectron's of this Atom as they need
         // to be copied too
         IElectronContainer[] eContainers = ac.getConnectedElectronContainers(atom);
         //System.out.println("found #ec's: " + eContainers.length);
         for (int i = 0; i < eContainers.Length; i++)
         {
             if (!(eContainers[i] is IBond))
             {
                 // ok, no bond, thus LonePair or SingleElectron
                 // System.out.println("adding non bond " + eContainers[i]);
                 molecule.addElectronContainer(eContainers[i]);
             }
         }
         // now look at bonds
         IBond[] bonds = ac.getConnectedBonds(atom);
         for (int g = 0; g < bonds.Length; g++)
         {
             if (!bonds[g].getFlag(CDKConstants.VISITED))
             {
                 molecule.addBond(bonds[g]);
                 bonds[g].setFlag(CDKConstants.VISITED, true);
             }
             nextAtom = bonds[g].getConnectedAtom(atom);
             if (!nextAtom.getFlag(CDKConstants.VISITED))
             {
                 //					System.out.println("wie oft???");
                 newSphere.Add(nextAtom);
                 nextAtom.setFlag(CDKConstants.VISITED, true);
             }
         }
         if (max > -1 && molecule.AtomCount > max)
             return;
     }
     if (newSphere.Count > 0)
     {
         breadthFirstSearch(ac, newSphere, molecule, max);
     }
 }
Ejemplo n.º 6
0
        /// <summary> Produces an AtomContainer without explicit Hs but with H count from one with Hs.
        /// The new molecule is a deep copy.
        ///
        /// </summary>
        /// <param name="atomContainer">The AtomContainer from which to remove the hydrogens
        /// </param>
        /// <returns>              The molecule without Hs.
        /// </returns>
        /// <cdk.keyword>          hydrogen, removal </cdk.keyword>
        public static IAtomContainer removeHydrogens(IAtomContainer atomContainer)
        {
            //UPGRADE_TODO: Class 'java.util.HashMap' was converted to 'System.Collections.Hashtable' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilHashMap'"
            System.Collections.IDictionary map    = new System.Collections.Hashtable(); // maps original atoms to clones.
            System.Collections.IList       remove = new System.Collections.ArrayList(); // lists removed Hs.

            // Clone atoms except those to be removed.
            IMolecule mol   = atomContainer.Builder.newMolecule();
            int       count = atomContainer.AtomCount;

            for (int i = 0; i < count; i++)
            {
                // Clone/remove this atom?
                IAtom atom = atomContainer.getAtomAt(i);
                if (!atom.Symbol.Equals("H"))
                {
                    IAtom clonedAtom = null;
                    try
                    {
                        clonedAtom = (IAtom)atom.Clone();
                    }
                    //UPGRADE_NOTE: Exception 'java.lang.CloneNotSupportedException' was converted to 'System.Exception' which has different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1100'"
                    catch (System.Exception e)
                    {
                        // TODO Auto-generated catch block
                        SupportClass.WriteStackTrace(e, Console.Error);
                    }
                    clonedAtom.setHydrogenCount(0);
                    mol.addAtom(clonedAtom);
                    map[atom] = clonedAtom;
                }
                else
                {
                    remove.Add(atom); // maintain list of removed H.
                }
            }

            // Clone bonds except those involving removed atoms.
            count = atomContainer.getBondCount();
            for (int i = 0; i < count; i++)
            {
                // Check bond.
                //UPGRADE_NOTE: Final was removed from the declaration of 'bond '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
                IBond   bond        = atomContainer.getBondAt(i);
                IAtom[] atoms       = bond.getAtoms();
                bool    removedBond = false;
                //UPGRADE_NOTE: Final was removed from the declaration of 'length '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
                int length = atoms.Length;
                for (int k = 0; k < length; k++)
                {
                    if (remove.Contains(atoms[k]))
                    {
                        removedBond = true;
                        break;
                    }
                }

                // Clone/remove this bond?
                if (!removedBond)
                // if (!remove.contains(atoms[0]) && !remove.contains(atoms[1]))
                {
                    IBond clone = null;
                    try
                    {
                        clone = (IBond)atomContainer.getBondAt(i).Clone();
                    }
                    //UPGRADE_NOTE: Exception 'java.lang.CloneNotSupportedException' was converted to 'System.Exception' which has different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1100'"
                    catch (System.Exception e)
                    {
                        // TODO Auto-generated catch block
                        SupportClass.WriteStackTrace(e, Console.Error);
                    }
                    clone.setAtoms(new IAtom[] { (IAtom)map[atoms[0]], (IAtom)map[atoms[1]] });
                    mol.addBond(clone);
                }
            }

            // Recompute hydrogen counts of neighbours of removed Hydrogens.
            //UPGRADE_TODO: Method 'java.util.Iterator.hasNext' was converted to 'System.Collections.IEnumerator.MoveNext' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilIteratorhasNext'"
            for (System.Collections.IEnumerator i = remove.GetEnumerator(); i.MoveNext();)
            {
                // Process neighbours.
                //UPGRADE_TODO: Method 'java.util.Iterator.next' was converted to 'System.Collections.IEnumerator.Current' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilIteratornext'"
                //UPGRADE_TODO: Method 'java.util.Iterator.hasNext' was converted to 'System.Collections.IEnumerator.MoveNext' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilIteratorhasNext'"
                for (System.Collections.IEnumerator n = atomContainer.getConnectedAtomsVector((IAtom)i.Current).GetEnumerator(); n.MoveNext();)
                {
                    //UPGRADE_NOTE: Final was removed from the declaration of 'neighb '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
                    //UPGRADE_TODO: Method 'java.util.Iterator.next' was converted to 'System.Collections.IEnumerator.Current' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilIteratornext'"
                    IAtom neighb = (IAtom)map[n.Current];
                    neighb.setHydrogenCount(neighb.getHydrogenCount() + 1);
                }
            }
            mol.Properties = atomContainer.Properties;
            mol.Flags      = atomContainer.Flags;

            return(mol);
        }
Ejemplo n.º 7
0
        /// <summary>  Private method that actually parses the input to read a ChemFile
        /// object. In its current state it is able to read all the molecules
        /// (if more than one is present) in the specified HIN file. These are
        /// placed in a SetOfMolecules object which in turn is placed in a ChemModel
        /// which in turn is placed in a ChemSequence object and which is finally
        /// placed in a ChemFile object and returned to the user.
        ///
        /// </summary>
        /// <returns> A ChemFile containing the data parsed from input.
        /// </returns>
        private IChemFile readChemFile(IChemFile file)
        {
            IChemSequence   chemSequence   = file.Builder.newChemSequence();
            IChemModel      chemModel      = file.Builder.newChemModel();
            ISetOfMolecules setOfMolecules = file.Builder.newSetOfMolecules();

            System.String info;

            SupportClass.Tokenizer tokenizer;

            try
            {
                System.String line;

                // read in header info
                while (true)
                {
                    line = input.ReadLine();
                    if (line.IndexOf("mol ") == 0)
                    {
                        info = getMolName(line);
                        break;
                    }
                }


                // start the actual molecule data - may be multiple molecule
                line = input.ReadLine();
                while (true)
                {
                    if (line == null)
                    {
                        break; // end of file
                    }
                    if (line.IndexOf(';') == 0)
                    {
                        continue; // comment line
                    }
                    if (line.IndexOf("mol ") == 0)
                    {
                        info = getMolName(line);
                        line = input.ReadLine();
                    }
                    IMolecule m = file.Builder.newMolecule();
                    m.setProperty(CDKConstants.TITLE, info);

                    // Each elemnt of cons is an ArrayList of length 3 which stores
                    // the start and end indices and bond order of each bond
                    // found in the HIN file. Before adding bonds we need to reduce
                    // the number of bonds so as not to count the same bond twice
                    System.Collections.ArrayList cons = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));

                    // read data for current molecule
                    int atomSerial = 0;
                    while (true)
                    {
                        if (line.IndexOf("endmol ") >= 0)
                        {
                            break;
                        }
                        if (line.IndexOf(';') == 0)
                        {
                            continue; // comment line
                        }
                        tokenizer = new SupportClass.Tokenizer(line, " ");

                        int             ntoken = tokenizer.Count;
                        System.String[] toks   = new System.String[ntoken];
                        for (int i = 0; i < ntoken; i++)
                        {
                            toks[i] = tokenizer.NextToken();
                        }

                        System.String sym    = new System.Text.StringBuilder(toks[3]).ToString();
                        double        charge = System.Double.Parse(toks[6]);
                        double        x      = System.Double.Parse(toks[7]);
                        double        y      = System.Double.Parse(toks[8]);
                        double        z      = System.Double.Parse(toks[9]);
                        int           nbond  = System.Int32.Parse(toks[10]);

                        IAtom atom = file.Builder.newAtom(sym, new Point3d(x, y, z));
                        atom.setCharge(charge);

                        for (int j = 11; j < (11 + nbond * 2); j += 2)
                        {
                            double bo = 1;
                            int    s  = System.Int32.Parse(toks[j]) - 1; // since atoms start from 1 in the file
                            char   bt = toks[j + 1][0];
                            switch (bt)
                            {
                            case 's':
                                bo = 1;
                                break;

                            case 'd':
                                bo = 2;
                                break;

                            case 't':
                                bo = 3;
                                break;

                            case 'a':
                                bo = 1.5;
                                break;
                            }
                            System.Collections.ArrayList ar = new System.Collections.ArrayList(3);
                            ar.Add((System.Int32)atomSerial);
                            ar.Add((System.Int32)s);
                            ar.Add((double)bo);
                            cons.Add(ar);
                        }
                        m.addAtom(atom);
                        atomSerial++;
                        line = input.ReadLine();
                    }

                    // before storing the molecule lets include the connections
                    // First we reduce the number of bonds stored, since we have
                    // stored both, say, C1-H1 and H1-C1.
                    System.Collections.ArrayList blist = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));
                    for (int i = 0; i < cons.Count; i++)
                    {
                        System.Collections.ArrayList ar = (System.Collections.ArrayList)cons[i];

                        // make a reversed list
                        System.Collections.ArrayList arev = new System.Collections.ArrayList(3);
                        arev.Add(ar[1]);
                        arev.Add(ar[0]);
                        arev.Add(ar[2]);

                        // Now see if ar or arev are already in blist
                        if (blist.Contains(ar) || blist.Contains(arev))
                        {
                            continue;
                        }
                        else
                        {
                            blist.Add(ar);
                        }
                    }

                    // now just store all the bonds we have
                    for (int i = 0; i < blist.Count; i++)
                    {
                        System.Collections.ArrayList ar = (System.Collections.ArrayList)blist[i];
                        int    s  = ((System.Int32)ar[0]);
                        int    e  = ((System.Int32)ar[1]);
                        double bo = ((System.Double)ar[2]);
                        m.addBond(s, e, bo);
                    }

                    setOfMolecules.addMolecule(m);
                    line = input.ReadLine(); // read in the 'mol N'
                }

                // got all the molecule in the HIN file (hopefully!)
                chemModel.SetOfMolecules = setOfMolecules;
                chemSequence.addChemModel(chemModel);
                file.addChemSequence(chemSequence);
            }
            catch (System.IO.IOException e)
            {
                // should make some noise now
                file = null;
            }
            return(file);
        }