Logo Search packages:      
Sourcecode: cdk version File versions  Download package

IChemFile org::openscience::cdk::io::PMPReader::readChemFile ( IChemFile  chemFile  )  [inline, private]

Private method that actually parses the input to read a ChemFile object.

Each PMP frame is stored as a Crystal in a ChemModel. The PMP file is stored as a ChemSequence of ChemModels.

Returns:
A ChemFile containing the data parsed from input.

Definition at line 183 of file PMPReader.java.

References org::openscience::cdk::interfaces::ICrystal::add(), org::openscience::cdk::interfaces::IAtomContainer::addAtom(), org::openscience::cdk::interfaces::IAtomContainer::addBond(), org::openscience::cdk::interfaces::IChemSequence::addChemModel(), org::openscience::cdk::interfaces::IChemFile::addChemSequence(), org::openscience::cdk::tools::LoggingTool::debug(), org::openscience::cdk::tools::LoggingTool::error(), org::openscience::cdk::interfaces::IAtomContainer::getAtom(), org::openscience::cdk::interfaces::IAtomContainer::getAtomCount(), org::openscience::cdk::interfaces::IChemObject::getBuilder(), org::openscience::cdk::interfaces::IChemObject::getProperty(), org::openscience::cdk::interfaces::IElement::getSymbol(), org::openscience::cdk::interfaces::IChemObjectBuilder::newAtom(), org::openscience::cdk::interfaces::IChemObjectBuilder::newAtomContainer(), org::openscience::cdk::interfaces::IChemObjectBuilder::newBond(), org::openscience::cdk::interfaces::IChemObjectBuilder::newChemModel(), org::openscience::cdk::interfaces::IChemObjectBuilder::newChemSequence(), org::openscience::cdk::interfaces::IChemObjectBuilder::newCrystal(), org::openscience::cdk::graph::rebond::RebondTool::rebond(), org::openscience::cdk::interfaces::ICrystal::setA(), org::openscience::cdk::interfaces::ICrystal::setB(), org::openscience::cdk::interfaces::ICrystal::setC(), org::openscience::cdk::interfaces::IAtomType::setCovalentRadius(), org::openscience::cdk::interfaces::IChemModel::setCrystal(), org::openscience::cdk::interfaces::IAtom::setPoint3d(), org::openscience::cdk::interfaces::ICrystal::setSpaceGroup(), org::openscience::cdk::interfaces::IElement::setSymbol(), and org::openscience::cdk::tools::LoggingTool::warn().

Referenced by read().

                                                       {
        IChemSequence chemSequence = chemFile.getBuilder().newChemSequence();
        IChemModel chemModel = chemFile.getBuilder().newChemModel();
        ICrystal crystal = chemFile.getBuilder().newCrystal();

        try {
            String line = readLine();
            while (input.ready() && line != null) {
                if (line.startsWith("%%Header Start")) {
                    // parse Header section
                    while (input.ready() && line != null && !(line.startsWith("%%Header End"))) {
                        if (line.startsWith("%%Version Number")) {
                            String version = readLine().trim();
                            if (!version.equals("3.00")) {
                                logger.error("The PMPReader only supports PMP files with version 3.00");
                                return null;
                            }
                        }
                        line = readLine();
                    }
                } else if (line.startsWith("%%Model Start")) {
                    // parse Model section
                  modelStructure = chemFile.getBuilder().newAtomContainer();
                    while (input.ready() && line != null && !(line.startsWith("%%Model End"))) {
                        Matcher objHeaderMatcher = objHeader.matcher(line);
                        if (objHeaderMatcher.matches()) {
                            String object = objHeaderMatcher.group(2);
                            constructObject(chemFile.getBuilder(), object);
                            int id = Integer.parseInt(objHeaderMatcher.group(1));
                            // logger.debug(object + " id: " + id);
                            line = readLine();
                            while (input.ready() && line != null && !(line.trim().equals(")"))) {
                                // parse object command (or new object header)
                                Matcher objCommandMatcher = objCommand.matcher(line);
                                objHeaderMatcher = objHeader.matcher(line);
                                if (objHeaderMatcher.matches()) {
                                    // ok, forget about nesting and hope for the best
                                    object = objHeaderMatcher.group(2);
                                    id = Integer.parseInt(objHeaderMatcher.group(1));
                                    constructObject(chemFile.getBuilder(), object);
                                } else if (objCommandMatcher.matches()) {
                                    String format = objCommandMatcher.group(1);
                                    String command = objCommandMatcher.group(2);
                                    String field = objCommandMatcher.group(3);
                                    
                                    processModelCommand(object, command, format, field);
                                } else {
                                    logger.warn("Skipping line: " + line);
                                }
                                line = readLine();
                            }
                            if (chemObject instanceof IAtom) {
                                atomids.put(new Integer(id), new Integer(modelStructure.getAtomCount()));
                                atomZOrders.put(new Integer((String)chemObject.getProperty(PMP_ZORDER)), new Integer(id));
                                atomGivenIds.put(new Integer((String)chemObject.getProperty(PMP_ID)), new Integer(id));
                                modelStructure.addAtom((IAtom)chemObject);
//                            } else if (chemObject instanceof IBond) {
//                                bondids.put(new Integer(id), new Integer(molecule.getAtomCount()));
//                                molecule.addBond((IBond)chemObject);
                            } else {
                                logger.error("chemObject is not initialized or of bad class type");
                            }
                            // logger.debug(molecule.toString());
                        }
                        line = readLine();
                    }
                    if (line.startsWith("%%Model End")) {
                        // during the Model Start, all bonds are cached as PMP files might
                        // define bonds *before* the involved atoms :(
                        // the next lines dump the cache into the atom container

//                      bondids.put(new Integer(id), new Integer(molecule.getAtomCount()));
//                      molecule.addBond((IBond)chemObject);
                        int bondsFound = bondids.size();
                        logger.debug("Found #bonds: ", bondsFound);
                        logger.debug("#atom ones: ", bondAtomOnes.size());
                        logger.debug("#atom twos: ", bondAtomTwos.size());
                        logger.debug("#orders: ", bondOrders.size());
                        Iterator bonds = bondids.keySet().iterator();
                        while (bonds.hasNext()) {
                              Integer index = (Integer)bonds.next();
                              double order = (bondOrders.get(index) != null ? ((Double)bondOrders.get(index)).doubleValue() : 1.0);
                              logger.debug("index: ", index);
                              logger.debug("ones: ", bondAtomOnes.get(index));
                              IAtom atom1 = modelStructure.getAtom(
                                    ((Integer)atomids.get(
                                          (Integer)bondAtomOnes.get(index)
                                    )).intValue()
                              );
                              IAtom atom2 = modelStructure.getAtom(
                                    ((Integer)atomids.get(
                                          (Integer)bondAtomTwos.get(index)
                                    )).intValue()
                              );
                              IBond bond = modelStructure.getBuilder().newBond(atom1, atom2, order);
                              modelStructure.addBond(bond);
                        }
                    }
                } else if (line.startsWith("%%Traj Start")) {
                    chemSequence = chemFile.getBuilder().newChemSequence();
                    double energyFragment = 0.0;
                    double energyTotal = 0.0;
                    int Z = 1;
                    while (input.ready() && line != null && !(line.startsWith("%%Traj End"))) {
                        if (line.startsWith("%%Start Frame")) {
                            chemModel = chemFile.getBuilder().newChemModel();
                            crystal = chemFile.getBuilder().newCrystal();
                            while (input.ready() && line != null && !(line.startsWith("%%End Frame"))) {
                                // process frame data
                              if (line.startsWith("%%Atom Coords")) {
                                    // calculate Z: as it is not explicitely given, try to derive it from the
                                    // energy per fragment and the total energy
                                    if (energyFragment != 0.0 && energyTotal != 0.0) {
                                          Z = (int)Math.round(energyTotal/energyFragment);
                                          logger.debug("Z derived from energies: ", Z);
                                    }
                                    // add atomC as atoms to crystal
                                    int expatoms = modelStructure.getAtomCount();
                                    for (int molCount = 1; molCount<=Z; molCount++) {
                                          IAtomContainer clone = modelStructure.getBuilder().newAtomContainer();
                                          for (int i=0; i < expatoms; i++) {
                                                line = readLine();
                                                IAtom a = clone.getBuilder().newAtom();
                                                StringTokenizer st = new StringTokenizer(line, " ");
                                                a.setPoint3d(
                                                      new Point3d(
                                                            Double.parseDouble(st.nextToken()),
                                                            Double.parseDouble(st.nextToken()),
                                                            Double.parseDouble(st.nextToken())
                                                      )
                                                );
                                                a.setCovalentRadius(0.6);
                                                IAtom modelAtom = modelStructure.getAtom(((Integer)atomids.get(atomGivenIds.get(new Integer(i+1)))).intValue());
                                                a.setSymbol(modelAtom.getSymbol());
                                                clone.addAtom(a);
                                          }
                                          rebonder.rebond(clone);
                                          crystal.add(clone);
                                    }
                                } else if (line.startsWith("%%E/Frag")) {
                                    line = readLine().trim();
                                    energyFragment = Double.parseDouble(line);
                                } else if (line.startsWith("%%Tot E")) {
                                    line = readLine().trim();
                                    energyTotal = Double.parseDouble(line);
                                } else if (line.startsWith("%%Lat Vects")) {
                                    StringTokenizer st;
                                    line = readLine();
                                    st = new StringTokenizer(line, " ");
                                    crystal.setA(new Vector3d(
                                        Double.parseDouble(st.nextToken()),
                                        Double.parseDouble(st.nextToken()),
                                        Double.parseDouble(st.nextToken())
                                    ));
                                    line = readLine();
                                    st = new StringTokenizer(line, " ");
                                    crystal.setB(new Vector3d(
                                        Double.parseDouble(st.nextToken()),
                                        Double.parseDouble(st.nextToken()),
                                        Double.parseDouble(st.nextToken())
                                    ));
                                    line = readLine();
                                    st = new StringTokenizer(line, " ");
                                    crystal.setC(new Vector3d(
                                        Double.parseDouble(st.nextToken()),
                                        Double.parseDouble(st.nextToken()),
                                        Double.parseDouble(st.nextToken())
                                    ));
                                } else if (line.startsWith("%%Space Group")) {
                                    line = readLine().trim();
                                    /* standardize space group name.
                                       See Crystal.setSpaceGroup() */
                                    if ("P 21 21 21 (1)".equals(line)) {
                                        crystal.setSpaceGroup("P 2_1 2_1 2_1");
                                    } else {
                                        crystal.setSpaceGroup("P1");
                                    }
                                } else {
                                }
                                line = readLine();
                            }
                            chemModel.setCrystal(crystal);
                            chemSequence.addChemModel(chemModel);
                        }
                        line = readLine();
                    }
                    chemFile.addChemSequence(chemSequence);
                } else {
                    // disregard line
                }
                // read next line
                line = readLine();
            }
        } catch (IOException e) {
            logger.error("An IOException happened: ", e.getMessage());
            logger.debug(e);
            chemFile = null;
        } catch (CDKException e) {
            logger.error("An CDKException happened: ", e.getMessage());
            logger.debug(e);
            chemFile = null;
            }

        return chemFile;
    }


Generated by  Doxygen 1.6.0   Back to index