Example #1
0
        private void processAtomLoopBlock(System.String firstLine)
        {
            int atomLabel  = -1; // -1 means not found in this block
            int atomSymbol = -1;
            int atomFractX = -1;
            int atomFractY = -1;
            int atomFractZ = -1;
            int atomRealX  = -1;
            int atomRealY  = -1;
            int atomRealZ  = -1;

            System.String line                   = firstLine.Trim();
            int           headerCount            = 0;
            bool          hasParsableInformation = false;

            while (line != null && line[0] == '_')
            {
                headerCount++;
                if (line.Equals("_atom_site_label") || line.Equals("_atom_site_label_atom_id"))
                {
                    atomLabel = headerCount;
                    hasParsableInformation = true;
                    //logger.info("label found in col: " + atomLabel);
                }
                else if (line.StartsWith("_atom_site_fract_x"))
                {
                    atomFractX             = headerCount;
                    hasParsableInformation = true;
                    //logger.info("frac x found in col: " + atomFractX);
                }
                else if (line.StartsWith("_atom_site_fract_y"))
                {
                    atomFractY             = headerCount;
                    hasParsableInformation = true;
                    //logger.info("frac y found in col: " + atomFractY);
                }
                else if (line.StartsWith("_atom_site_fract_z"))
                {
                    atomFractZ             = headerCount;
                    hasParsableInformation = true;
                    //logger.info("frac z found in col: " + atomFractZ);
                }
                else if (line.Equals("_atom_site.Cartn_x"))
                {
                    atomRealX = headerCount;
                    hasParsableInformation = true;
                    //logger.info("cart x found in col: " + atomRealX);
                }
                else if (line.Equals("_atom_site.Cartn_y"))
                {
                    atomRealY = headerCount;
                    hasParsableInformation = true;
                    //logger.info("cart y found in col: " + atomRealY);
                }
                else if (line.Equals("_atom_site.Cartn_z"))
                {
                    atomRealZ = headerCount;
                    hasParsableInformation = true;
                    //logger.info("cart z found in col: " + atomRealZ);
                }
                else if (line.Equals("_atom_site.type_symbol"))
                {
                    atomSymbol             = headerCount;
                    hasParsableInformation = true;
                    //logger.info("type_symbol found in col: " + atomSymbol);
                }
                else
                {
                    //logger.warn("Ignoring atom loop block field: " + line);
                }
                line = input.ReadLine().Trim();
            }
            if (hasParsableInformation == false)
            {
                //logger.info("No parsable info found");
                skipUntilEmptyOrCommentLine(line);
            }
            else
            {
                // now that headers are parsed, read the data
                while (line != null && line.Length > 0 && line[0] != '#')
                {
                    //logger.debug("new row");
                    SupportClass.Tokenizer tokenizer = new SupportClass.Tokenizer(line);
                    if (tokenizer.Count < headerCount)
                    {
                        //logger.warn("Column count mismatch; assuming continued on next line");
                        //logger.debug("Found #expected, #found: " + headerCount + ", " + tokenizer.Count);
                        tokenizer = new SupportClass.Tokenizer(line + input.ReadLine());
                    }
                    int colIndex = 0;
                    // process one row
                    IAtom   atom          = crystal.Builder.newAtom("C");
                    Point3d frac          = new Point3d();
                    Point3d real          = new Point3d();
                    bool    hasFractional = false;
                    bool    hasCartesian  = false;
                    while (tokenizer.HasMoreTokens())
                    {
                        colIndex++;
                        System.String field = tokenizer.NextToken();
                        //logger.debug("Parsing col,token: " + colIndex + "=" + field);
                        if (colIndex == atomLabel)
                        {
                            if (atomSymbol == -1)
                            {
                                // no atom symbol found, use label
                                System.String element = extractFirstLetters(field);
                                atom.Symbol = element;
                            }
                            atom.ID = field;
                        }
                        else if (colIndex == atomFractX)
                        {
                            hasFractional = true;
                            frac.x        = parseIntoDouble(field);
                        }
                        else if (colIndex == atomFractY)
                        {
                            hasFractional = true;
                            frac.y        = parseIntoDouble(field);
                        }
                        else if (colIndex == atomFractZ)
                        {
                            hasFractional = true;
                            frac.z        = parseIntoDouble(field);
                        }
                        else if (colIndex == atomSymbol)
                        {
                            atom.Symbol = field;
                        }
                        else if (colIndex == atomRealX)
                        {
                            hasCartesian = true;
                            //logger.debug("Adding x3: " + parseIntoDouble(field));
                            real.x = parseIntoDouble(field);
                        }
                        else if (colIndex == atomRealY)
                        {
                            hasCartesian = true;
                            //logger.debug("Adding y3: " + parseIntoDouble(field));
                            real.y = parseIntoDouble(field);
                        }
                        else if (colIndex == atomRealZ)
                        {
                            hasCartesian = true;
                            //logger.debug("Adding x3: " + parseIntoDouble(field));
                            real.z = parseIntoDouble(field);
                        }
                    }
                    if (hasCartesian)
                    {
                        Vector3d a = crystal.A;
                        Vector3d b = crystal.B;
                        Vector3d c = crystal.C;
                        frac = CrystalGeometryTools.cartesianToFractional(a, b, c, real);
                        atom.setFractionalPoint3d(frac);
                    }
                    if (hasFractional)
                    {
                        atom.setFractionalPoint3d(frac);
                    }
                    //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("Adding atom: " + atom);
                    crystal.addAtom(atom);

                    // look up next row
                    line = input.ReadLine().Trim();
                }
            }
        }
Example #2
0
        private IChemFile readChemFile(IChemFile file)
        {
            IChemSequence seq     = file.Builder.newChemSequence();
            IChemModel    model   = file.Builder.newChemModel();
            ICrystal      crystal = null;

            int      lineNumber = 0;
            Vector3d a, b, c;

            try
            {
                System.String line = input.ReadLine();
                while (input.Peek() != -1 && line != null)
                {
                    //logger.debug((lineNumber++) + ": ", line);
                    if (line.StartsWith("frame:"))
                    {
                        //logger.debug("found new frame");
                        model   = file.Builder.newChemModel();
                        crystal = file.Builder.newCrystal();

                        // assume the file format is correct

                        //logger.debug("reading spacegroup");
                        line = input.ReadLine();
                        //logger.debug((lineNumber++) + ": ", line);
                        crystal.SpaceGroup = line;

                        //logger.debug("reading unit cell axes");
                        Vector3d axis = new Vector3d();
                        //logger.debug("parsing A: ");
                        line = input.ReadLine();
                        //logger.debug((lineNumber++) + ": ", line);
                        axis.x = FortranFormat.atof(line);
                        line   = input.ReadLine();
                        //logger.debug((lineNumber++) + ": ", line);
                        axis.y = FortranFormat.atof(line);
                        line   = input.ReadLine();
                        //logger.debug((lineNumber++) + ": ", line);
                        axis.z    = FortranFormat.atof(line);
                        crystal.A = axis;
                        axis      = new Vector3d();
                        //logger.debug("parsing B: ");
                        line = input.ReadLine();
                        //logger.debug((lineNumber++) + ": ", line);
                        axis.x = FortranFormat.atof(line);
                        line   = input.ReadLine();
                        //logger.debug((lineNumber++) + ": ", line);
                        axis.y = FortranFormat.atof(line);
                        line   = input.ReadLine();
                        //logger.debug((lineNumber++) + ": ", line);
                        axis.z    = FortranFormat.atof(line);
                        crystal.B = axis;
                        axis      = new Vector3d();
                        //logger.debug("parsing C: ");
                        line = input.ReadLine();
                        //logger.debug((lineNumber++) + ": ", line);
                        axis.x = FortranFormat.atof(line);
                        line   = input.ReadLine();
                        //logger.debug((lineNumber++) + ": ", line);
                        axis.y = FortranFormat.atof(line);
                        line   = input.ReadLine();
                        //logger.debug((lineNumber++) + ": ", line);
                        axis.z    = FortranFormat.atof(line);
                        crystal.C = axis;
                        //logger.debug("Crystal: ", crystal);
                        a = crystal.A;
                        b = crystal.B;
                        c = crystal.C;

                        //logger.debug("Reading number of atoms");
                        line = input.ReadLine();
                        //logger.debug((lineNumber++) + ": ", line);
                        int atomsToRead = System.Int32.Parse(line);

                        //logger.debug("Reading no molecules in assym unit cell");
                        line = input.ReadLine();
                        //logger.debug((lineNumber++) + ": ", line);
                        int Z = System.Int32.Parse(line);
                        crystal.Z = Z;

                        System.String symbol;
                        double        charge;
                        Point3d       cart;
                        for (int i = 1; i <= atomsToRead; i++)
                        {
                            cart = new Point3d();
                            line = input.ReadLine();
                            //logger.debug((lineNumber++) + ": ", line);
                            symbol = line.Substring(0, (line.IndexOf(":")) - (0));
                            charge = System.Double.Parse(line.Substring(line.IndexOf(":") + 1));
                            line   = input.ReadLine();
                            //logger.debug((lineNumber++) + ": ", line);
                            cart.x = System.Double.Parse(line); // x
                            line   = input.ReadLine();
                            //logger.debug((lineNumber++) + ": ", line);
                            cart.y = System.Double.Parse(line); // y
                            line   = input.ReadLine();
                            //logger.debug((lineNumber++) + ": ", line);
                            cart.z = System.Double.Parse(line); // z
                            IAtom atom = file.Builder.newAtom(symbol);
                            atom.setCharge(charge);
                            // convert cartesian coords to fractional
                            Point3d frac = CrystalGeometryTools.cartesianToFractional(a, b, c, cart);
                            atom.setFractionalPoint3d(frac);
                            crystal.addAtom(atom);
                            //logger.debug("Added atom: ", atom);
                        }

                        model.Crystal = crystal;
                        seq.addChemModel(model);
                    }
                    else
                    {
                        //logger.debug("Format seems broken. Skipping these lines:");
                        while (!line.StartsWith("frame:") && input.Peek() != -1 && line != null)
                        {
                            line = input.ReadLine();
                            //logger.debug(lineNumber++ + ": ", line);
                        }
                        //logger.debug("Ok, resynched: found new frame");
                    }
                }
                file.addChemSequence(seq);
            }
            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 message = "Error while parsing CrystClust file: " + exception.Message;
                //logger.error(message);
                //logger.debug(exception);
                throw new CDKException(message, exception);
            }
            return(file);
        }