/// <summary> Rotates a 3D point about a specified line segment by a specified angle.
        /// 
        /// The code is based on code available <a href="http://astronomy.swin.edu.au/~pbourke/geometry/rotate/source.c">here</a>.
        /// Positive angles are anticlockwise looking down the axis towards the origin.
        /// Assume right hand coordinate system.
        /// 
        /// </summary>
        /// <param name="atom">The atom to rotate
        /// </param>
        /// <param name="p1"> The  first point of the line segment
        /// </param>
        /// <param name="p2"> The second point of the line segment
        /// </param>
        /// <param name="angle"> The angle to rotate by (in degrees)
        /// </param>
        public static void rotate(IAtom atom, Point3d p1, Point3d p2, double angle)
        {
            double costheta, sintheta;

            Point3d r = new Point3d();

            r.x = p2.x - p1.x;
            r.y = p2.y - p1.y;
            r.z = p2.z - p1.z;
            normalize(r);


            angle = angle * System.Math.PI / 180.0;
            costheta = System.Math.Cos(angle);
            sintheta = System.Math.Sin(angle);

            Point3d p = atom.getPoint3d();
            p.x -= p1.x;
            p.y -= p1.y;
            p.z -= p1.z;

            Point3d q = new Point3d(0, 0, 0);
            q.x += (costheta + (1 - costheta) * r.x * r.x) * p.x;
            q.x += ((1 - costheta) * r.x * r.y - r.z * sintheta) * p.y;
            q.x += ((1 - costheta) * r.x * r.z + r.y * sintheta) * p.z;

            q.y += ((1 - costheta) * r.x * r.y + r.z * sintheta) * p.x;
            q.y += (costheta + (1 - costheta) * r.y * r.y) * p.y;
            q.y += ((1 - costheta) * r.y * r.z - r.x * sintheta) * p.z;

            q.z += ((1 - costheta) * r.x * r.z - r.y * sintheta) * p.x;
            q.z += ((1 - costheta) * r.y * r.z + r.x * sintheta) * p.y;
            q.z += (costheta + (1 - costheta) * r.z * r.z) * p.z;

            q.x += p1.x;
            q.y += p1.y;
            q.z += p1.z;

            atom.setPoint3d(q);
        }
Beispiel #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);
        }
        private IChemModel readChemModel(IChemModel model)
        {
            int[]    atoms       = new int[1];
            double[] atomxs      = new double[1];
            double[] atomys      = new double[1];
            double[] atomzs      = new double[1];
            double[] atomcharges = new double[1];

            int[] bondatomid1 = new int[1];
            int[] bondatomid2 = new int[1];
            int[] bondorder   = new int[1];

            int numberOfAtoms = 0;
            int numberOfBonds = 0;

            try
            {
                System.String line = input.ReadLine();
                while (line != null)
                {
                    SupportClass.Tokenizer st      = new SupportClass.Tokenizer(line);
                    System.String          command = st.NextToken();
                    if ("!Header".Equals(command))
                    {
                        //logger.warn("Ignoring header");
                    }
                    else if ("!Info".Equals(command))
                    {
                        //logger.warn("Ignoring info");
                    }
                    else if ("!Atoms".Equals(command))
                    {
                        //logger.info("Reading atom block");
                        // determine number of atoms to read
                        try
                        {
                            numberOfAtoms = System.Int32.Parse(st.NextToken());
                            //logger.debug("  #atoms: " + numberOfAtoms);
                            atoms       = new int[numberOfAtoms];
                            atomxs      = new double[numberOfAtoms];
                            atomys      = new double[numberOfAtoms];
                            atomzs      = new double[numberOfAtoms];
                            atomcharges = new double[numberOfAtoms];

                            for (int i = 0; i < numberOfAtoms; i++)
                            {
                                line = input.ReadLine();
                                SupportClass.Tokenizer atomInfoFields = new SupportClass.Tokenizer(line);
                                int atomID = System.Int32.Parse(atomInfoFields.NextToken());
                                atoms[atomID] = System.Int32.Parse(atomInfoFields.NextToken());
                                //logger.debug("Set atomic number of atom (" + atomID + ") to: " + atoms[atomID]);
                            }
                        }
                        catch (System.Exception exception)
                        {
                            //logger.error("Error while reading Atoms block");
                            //logger.debug(exception);
                        }
                    }
                    else if ("!Bonds".Equals(command))
                    {
                        //logger.info("Reading bond block");
                        try
                        {
                            // determine number of bonds to read
                            numberOfBonds = System.Int32.Parse(st.NextToken());
                            bondatomid1   = new int[numberOfAtoms];
                            bondatomid2   = new int[numberOfAtoms];
                            bondorder     = new int[numberOfAtoms];

                            for (int i = 0; i < numberOfBonds; i++)
                            {
                                line = input.ReadLine();
                                SupportClass.Tokenizer bondInfoFields = new SupportClass.Tokenizer(line);
                                bondatomid1[i] = System.Int32.Parse(bondInfoFields.NextToken());
                                bondatomid2[i] = System.Int32.Parse(bondInfoFields.NextToken());
                                System.String order = bondInfoFields.NextToken();
                                if ("D".Equals(order))
                                {
                                    bondorder[i] = 2;
                                }
                                else if ("S".Equals(order))
                                {
                                    bondorder[i] = 1;
                                }
                                else if ("T".Equals(order))
                                {
                                    bondorder[i] = 3;
                                }
                                else
                                {
                                    // ignore order, i.e. set to single
                                    //logger.warn("Unrecognized bond order, using single bond instead. Found: " + order);
                                    bondorder[i] = 1;
                                }
                            }
                        }
                        catch (System.Exception exception)
                        {
                            //logger.error("Error while reading Bonds block");
                            //logger.debug(exception);
                        }
                    }
                    else if ("!Coord".Equals(command))
                    {
                        //logger.info("Reading coordinate block");
                        try
                        {
                            for (int i = 0; i < numberOfAtoms; i++)
                            {
                                line = input.ReadLine();
                                SupportClass.Tokenizer atomInfoFields = new SupportClass.Tokenizer(line);
                                int    atomID = System.Int32.Parse(atomInfoFields.NextToken());
                                double x      = System.Double.Parse(atomInfoFields.NextToken());
                                double y      = System.Double.Parse(atomInfoFields.NextToken());
                                double z      = System.Double.Parse(atomInfoFields.NextToken());
                                atomxs[atomID] = x;
                                atomys[atomID] = y;
                                atomzs[atomID] = z;
                            }
                        }
                        catch (System.Exception exception)
                        {
                            //logger.error("Error while reading Coord block");
                            //logger.debug(exception);
                        }
                    }
                    else if ("!Charges".Equals(command))
                    {
                        //logger.info("Reading charges block");
                        try
                        {
                            for (int i = 0; i < numberOfAtoms; i++)
                            {
                                line = input.ReadLine();
                                SupportClass.Tokenizer atomInfoFields = new SupportClass.Tokenizer(line);
                                int    atomID = System.Int32.Parse(atomInfoFields.NextToken());
                                double charge = System.Double.Parse(atomInfoFields.NextToken());
                                atomcharges[atomID] = charge;
                            }
                        }
                        catch (System.Exception exception)
                        {
                            //logger.error("Error while reading Charges block");
                            //logger.debug(exception);
                        }
                    }
                    else if ("!End".Equals(command))
                    {
                        //logger.info("Found end of file");
                        // Store atoms
                        IAtomContainer container = model.Builder.newAtomContainer();
                        for (int i = 0; i < numberOfAtoms; i++)
                        {
                            try
                            {
                                IAtom atom = model.Builder.newAtom(IsotopeFactory.getInstance(container.Builder).getElementSymbol(atoms[i]));
                                atom.AtomicNumber = atoms[i];
                                atom.setPoint3d(new Point3d(atomxs[i], atomys[i], atomzs[i]));
                                atom.setCharge(atomcharges[i]);
                                container.addAtom(atom);
                                //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Object.toString' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
                                //logger.debug("Stored atom: " + atom);
                            }
                            catch (System.Exception exception)
                            {
                                //logger.error("Cannot create an atom with atomic number: " + atoms[i]);
                                //logger.debug(exception);
                            }
                        }

                        // Store bonds
                        for (int i = 0; i < numberOfBonds; i++)
                        {
                            container.addBond(bondatomid1[i], bondatomid2[i], bondorder[i]);
                        }

                        ISetOfMolecules moleculeSet = model.Builder.newSetOfMolecules();
                        moleculeSet.addMolecule(model.Builder.newMolecule(container));
                        model.SetOfMolecules = moleculeSet;

                        return(model);
                    }
                    else
                    {
                        //logger.warn("Skipping line: " + line);
                    }

                    line = input.ReadLine();
                }
            }
            catch (System.Exception exception)
            {
                //logger.error("Error while reading file");
                //logger.debug(exception);
            }

            // this should not happen, file is lacking !End command
            return(null);
        }
        /// <summary> Reads a set of coordinates into ChemFrame.
        ///
        /// </summary>
        /// <param name="model">Description of the Parameter
        /// </param>
        /// <throws>  IOException  if an I/O error occurs </throws>
        /// <throws>  CDKException Description of the Exception </throws>
        private void readCoordinates(IChemModel model)
        {
            ISetOfMolecules moleculeSet = model.Builder.newSetOfMolecules();
            IMolecule       molecule    = model.Builder.newMolecule();

            System.String line = input.ReadLine();
            line = input.ReadLine();
            line = input.ReadLine();
            line = input.ReadLine();
            while (input.Peek() != -1)
            {
                line = input.ReadLine();
                if ((line == null) || (line.IndexOf("-----") >= 0))
                {
                    break;
                }
                int atomicNumber;
                System.IO.StringReader sr = new System.IO.StringReader(line);
                SupportClass.StreamTokenizerSupport token = new SupportClass.StreamTokenizerSupport(sr);
                token.NextToken();

                // ignore first token
                if (token.NextToken() == SupportClass.StreamTokenizerSupport.TT_NUMBER)
                {
                    //UPGRADE_WARNING: Data types in Visual C# might be different.  Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
                    atomicNumber = (int)token.nval;
                    if (atomicNumber == 0)
                    {
                        // Skip dummy atoms. Dummy atoms must be skipped
                        // if frequencies are to be read because Gaussian
                        // does not report dummy atoms in frequencies, and
                        // the number of atoms is used for reading frequencies.
                        continue;
                    }
                }
                else
                {
                    throw new CDKException("Error while reading coordinates: expected integer.");
                }
                token.NextToken();

                // ignore third token
                double x;
                double y;
                double z;
                if (token.NextToken() == SupportClass.StreamTokenizerSupport.TT_NUMBER)
                {
                    x = token.nval;
                }
                else
                {
                    throw new System.IO.IOException("Error reading x coordinate");
                }
                if (token.NextToken() == SupportClass.StreamTokenizerSupport.TT_NUMBER)
                {
                    y = token.nval;
                }
                else
                {
                    throw new System.IO.IOException("Error reading y coordinate");
                }
                if (token.NextToken() == SupportClass.StreamTokenizerSupport.TT_NUMBER)
                {
                    z = token.nval;
                }
                else
                {
                    throw new System.IO.IOException("Error reading z coordinate");
                }
                System.String symbol = "Du";
                try
                {
                    symbol = IsotopeFactory.getInstance(model.Builder).getElementSymbol(atomicNumber);
                }
                catch (System.Exception exception)
                {
                    throw new CDKException("Could not determine element symbol!", exception);
                }
                IAtom atom = model.Builder.newAtom(symbol);
                atom.setPoint3d(new Point3d(x, y, z));
                molecule.addAtom(atom);
            }

            /*
             *  this is the place where we store the atomcount to
             *  be used as a counter in the nmr reading
             */
            atomCount = molecule.AtomCount;
            moleculeSet.addMolecule(molecule);
            model.SetOfMolecules = moleculeSet;
        }
Beispiel #5
0
        /// <summary> Reads the atoms, coordinates and charges.
        ///
        /// <p>IMPORTANT: it does not support the atom list and its negation!
        /// </summary>
        public virtual void readAtomBlock(IAtomContainer readData)
        {
            bool foundEND = false;

            while (Ready && !foundEND)
            {
                System.String command = readCommand();
                if ("END ATOM".Equals(command))
                {
                    // FIXME: should check wether 3D is really 2D
                    foundEND = true;
                }
                else
                {
                    //logger.debug("Parsing atom from: " + command);
                    SupportClass.Tokenizer tokenizer = new SupportClass.Tokenizer(command);
                    IAtom atom = readData.Builder.newAtom("C");
                    // parse the index
                    try
                    {
                        System.String indexString = tokenizer.NextToken();
                        atom.ID = indexString;
                    }
                    catch (System.Exception exception)
                    {
                        System.String error = "Error while parsing atom index";
                        //logger.error(error);
                        //logger.debug(exception);
                        throw new CDKException(error, exception);
                    }
                    // parse the element
                    System.String element   = tokenizer.NextToken();
                    bool          isElement = false;
                    try
                    {
                        isElement = IsotopeFactory.getInstance(atom.Builder).isElement(element);
                    }
                    catch (System.Exception exception)
                    {
                        throw new CDKException("Could not determine if element exists!", exception);
                    }
                    if (isPseudoAtom(element))
                    {
                        atom = readData.Builder.newPseudoAtom(atom);
                    }
                    else if (isElement)
                    {
                        atom.Symbol = element;
                    }
                    else
                    {
                        System.String error = "Cannot parse element of type: " + element;
                        //logger.error(error);
                        throw new CDKException("(Possible CDK bug) " + error);
                    }
                    // parse atom coordinates (in Angstrom)
                    try
                    {
                        System.String xString = tokenizer.NextToken();
                        System.String yString = tokenizer.NextToken();
                        System.String zString = tokenizer.NextToken();
                        double        x       = System.Double.Parse(xString);
                        double        y       = System.Double.Parse(yString);
                        double        z       = System.Double.Parse(zString);
                        atom.setPoint3d(new Point3d(x, y, z));
                        atom.setPoint2d(new Point2d(x, y)); // FIXME: dirty!
                    }
                    catch (System.Exception exception)
                    {
                        System.String error = "Error while parsing atom coordinates";
                        //logger.error(error);
                        //logger.debug(exception);
                        throw new CDKException(error, exception);
                    }
                    // atom-atom mapping
                    System.String mapping = tokenizer.NextToken();
                    if (!mapping.Equals("0"))
                    {
                        //logger.warn("Skipping atom-atom mapping: " + mapping);
                    } // else: default 0 is no mapping defined

                    // the rest are key value things
                    if (command.IndexOf("=") != -1)
                    {
                        System.Collections.Hashtable   options = parseOptions(exhaustStringTokenizer(tokenizer));
                        System.Collections.IEnumerator keys    = options.Keys.GetEnumerator();
                        //UPGRADE_TODO: Method 'java.util.Enumeration.hasMoreElements' 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_javautilEnumerationhasMoreElements'"
                        while (keys.MoveNext())
                        {
                            //UPGRADE_TODO: Method 'java.util.Enumeration.nextElement' 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_javautilEnumerationnextElement'"
                            System.String key           = (System.String)keys.Current;
                            System.String value_Renamed = (System.String)options[key];
                            try
                            {
                                if (key.Equals("CHG"))
                                {
                                    int charge = System.Int32.Parse(value_Renamed);
                                    if (charge != 0)
                                    {
                                        // zero is no charge specified
                                        atom.setFormalCharge(charge);
                                    }
                                }
                                else
                                {
                                    //logger.warn("Not parsing key: " + key);
                                }
                            }
                            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 key/value " + key + "=" + value_Renamed + ": " + exception.Message;
                                //logger.error(error);
                                //logger.debug(exception);
                                throw new CDKException(error, exception);
                            }
                        }
                    }

                    // store atom
                    readData.addAtom(atom);
                    //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Object.toString' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
                    //logger.debug("Added atom: " + atom);
                }
            }
        }
Beispiel #6
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);
        }