Search in sources :

Example 6 with IChemSequence

use of org.openscience.cdk.interfaces.IChemSequence in project cdk by cdk.

the class PMPReader method readChemFile.

/**
 *  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.
 *
 * @return A ChemFile containing the data parsed from input.
 */
private IChemFile readChemFile(IChemFile chemFile) {
    IChemSequence chemSequence;
    IChemModel chemModel;
    ICrystal crystal;
    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().newInstance(IAtomContainer.class);
                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(id, modelStructure.getAtomCount());
                            atomGivenIds.put(Integer.valueOf(chemObject.getProperty(PMP_ID)), id);
                            modelStructure.addAtom((IAtom) chemObject);
                        } else if (chemObject instanceof IBond) {
                        // ignored: bonds may be defined before their
                        // atoms so their handling is deferred until the
                        // end of the model
                        } else {
                            logger.error("chemObject is not initialized or of bad class type");
                        }
                    // logger.debug(molecule.toString());
                    }
                    line = readLine();
                }
                assert line != null;
                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());
                    for (Integer index : bondids.keySet()) {
                        double order = (bondOrders.get(index) != null ? bondOrders.get(index) : 1.0);
                        logger.debug("index: ", index);
                        logger.debug("ones: ", bondAtomOnes.get(index));
                        IAtom atom1 = modelStructure.getAtom(atomids.get(bondAtomOnes.get(index)));
                        IAtom atom2 = modelStructure.getAtom(atomids.get(bondAtomTwos.get(index)));
                        IBond bond = modelStructure.getBuilder().newInstance(IBond.class, atom1, atom2);
                        if (order == 1.0) {
                            bond.setOrder(IBond.Order.SINGLE);
                        } else if (order == 2.0) {
                            bond.setOrder(IBond.Order.DOUBLE);
                        } else if (order == 3.0) {
                            bond.setOrder(IBond.Order.TRIPLE);
                        } else if (order == 4.0) {
                            bond.setOrder(IBond.Order.QUADRUPLE);
                        }
                        modelStructure.addBond(bond);
                    }
                }
            } else if (line.startsWith("%%Traj Start")) {
                chemSequence = chemFile.getBuilder().newInstance(IChemSequence.class);
                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().newInstance(IChemModel.class);
                        crystal = chemFile.getBuilder().newInstance(ICrystal.class);
                        while (input.ready() && line != null && !(line.startsWith("%%End Frame"))) {
                            // process frame data
                            if (line.startsWith("%%Atom Coords")) {
                                // 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().newInstance(IAtomContainer.class);
                                    for (int i = 0; i < expatoms; i++) {
                                        line = readLine();
                                        IAtom a = clone.getBuilder().newInstance(IAtom.class);
                                        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(atomids.get(atomGivenIds.get(i + 1)));
                                        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");
                                }
                            }
                            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;
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) Matcher(java.util.regex.Matcher) CDKException(org.openscience.cdk.exception.CDKException) IChemModel(org.openscience.cdk.interfaces.IChemModel) IBond(org.openscience.cdk.interfaces.IBond) IOException(java.io.IOException) StringTokenizer(java.util.StringTokenizer) ICrystal(org.openscience.cdk.interfaces.ICrystal) Vector3d(javax.vecmath.Vector3d) Point3d(javax.vecmath.Point3d) IChemSequence(org.openscience.cdk.interfaces.IChemSequence) IAtom(org.openscience.cdk.interfaces.IAtom)

Example 7 with IChemSequence

use of org.openscience.cdk.interfaces.IChemSequence in project cdk by cdk.

the class XYZReader method readChemFile.

// private procedures
/**
 *  Private method that actually parses the input to read a ChemFile
 *  object.
 *
 * @return A ChemFile containing the data parsed from input.
 */
private IChemFile readChemFile(IChemFile file) {
    IChemSequence chemSequence = file.getBuilder().newInstance(IChemSequence.class);
    int number_of_atoms;
    StringTokenizer tokenizer;
    try {
        String line = input.readLine();
        while (input.ready() && line != null) {
            // parse frame by frame
            tokenizer = new StringTokenizer(line, "\t ,;");
            String token = tokenizer.nextToken();
            number_of_atoms = Integer.parseInt(token);
            String info = input.readLine();
            IChemModel chemModel = file.getBuilder().newInstance(IChemModel.class);
            IAtomContainerSet setOfMolecules = file.getBuilder().newInstance(IAtomContainerSet.class);
            IAtomContainer m = file.getBuilder().newInstance(IAtomContainer.class);
            m.setTitle(info);
            for (int i = 0; i < number_of_atoms; i++) {
                line = input.readLine();
                if (line == null)
                    break;
                if (line.startsWith("#") && line.length() > 1) {
                    Object comment = m.getProperty(CDKConstants.COMMENT);
                    if (comment == null) {
                        comment = "";
                    }
                    comment = comment + line.substring(1).trim();
                    m.setProperty(CDKConstants.COMMENT, comment);
                    logger.debug("Found and set comment: ", comment);
                    // a comment line does not count as an atom
                    i--;
                } else {
                    double x, y, z;
                    double charge = 0.0f;
                    tokenizer = new StringTokenizer(line, "\t ,;");
                    int fields = tokenizer.countTokens();
                    if (fields < 4) {
                    // this is an error but cannot throw exception
                    } else {
                        String atomtype = tokenizer.nextToken();
                        x = new Double(tokenizer.nextToken());
                        y = new Double(tokenizer.nextToken());
                        z = new Double(tokenizer.nextToken());
                        if (fields == 8)
                            charge = new Double(tokenizer.nextToken());
                        IAtom atom = file.getBuilder().newInstance(IAtom.class, atomtype, new Point3d(x, y, z));
                        atom.setCharge(charge);
                        m.addAtom(atom);
                    }
                }
            }
            setOfMolecules.addAtomContainer(m);
            chemModel.setMoleculeSet(setOfMolecules);
            chemSequence.addChemModel(chemModel);
            line = input.readLine();
        }
        file.addChemSequence(chemSequence);
    } catch (IOException e) {
        // should make some noise now
        file = null;
        logger.error("Error while reading file: ", e.getMessage());
        logger.debug(e);
    }
    return file;
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) IChemModel(org.openscience.cdk.interfaces.IChemModel) IOException(java.io.IOException) StringTokenizer(java.util.StringTokenizer) Point3d(javax.vecmath.Point3d) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) IChemObject(org.openscience.cdk.interfaces.IChemObject) IChemSequence(org.openscience.cdk.interfaces.IChemSequence) IAtom(org.openscience.cdk.interfaces.IAtom)

Example 8 with IChemSequence

use of org.openscience.cdk.interfaces.IChemSequence in project cdk by cdk.

the class CTXReader method readChemFile.

private IChemFile readChemFile() throws CDKException {
    IChemSequence seq = file.getBuilder().newInstance(IChemSequence.class);
    IChemModel model = file.getBuilder().newInstance(IChemModel.class);
    IAtomContainerSet containerSet = file.getBuilder().newInstance(IAtomContainerSet.class);
    IAtomContainer container = file.getBuilder().newInstance(IAtomContainer.class);
    int lineNumber = 0;
    try {
        String line = input.readLine();
        while (input.ready() && line != null) {
            logger.debug((lineNumber++) + ": ", line);
            String command;
            if (isCommand(line)) {
                command = getCommand(line);
                int lineCount = getContentLinesCount(line);
                if ("ATOMS".equals(command)) {
                    processAtomsBlock(lineCount, container);
                } else if ("BONDS".equals(command)) {
                    processBondsBlock(lineCount, container);
                } else if ("IDENT".equals(command)) {
                    processIdentBlock(lineCount, container);
                } else if ("NAME".equals(command)) {
                    processNameBlock(lineCount, container);
                } else {
                    // skip lines
                    logger.warn("Dropping block: ", command);
                    for (int i = 0; i < lineCount; i++) {
                        line = input.readLine();
                        if (line == null)
                            throw new CDKException("End of input while skipping lines!");
                    }
                }
            } else {
                logger.warn("Unexpected content at line: ", lineNumber);
            }
            line = input.readLine();
        }
        containerSet.addAtomContainer(container);
        model.setMoleculeSet(containerSet);
        seq.addChemModel(model);
        file.addChemSequence(seq);
    } catch (Exception exception) {
        String message = "Error while parsing CTX file: " + exception.getMessage();
        logger.error(message);
        logger.debug(exception);
        throw new CDKException(message, exception);
    }
    return file;
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) CDKException(org.openscience.cdk.exception.CDKException) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) IChemModel(org.openscience.cdk.interfaces.IChemModel) IChemSequence(org.openscience.cdk.interfaces.IChemSequence) CDKException(org.openscience.cdk.exception.CDKException) IOException(java.io.IOException)

Example 9 with IChemSequence

use of org.openscience.cdk.interfaces.IChemSequence in project cdk by cdk.

the class Gaussian03Reader method readChemFile.

private IChemFile readChemFile(IChemFile chemFile) throws CDKException {
    IChemSequence sequence = readChemSequence(chemFile.getBuilder().newInstance(IChemSequence.class));
    chemFile.addChemSequence(sequence);
    return chemFile;
}
Also used : IChemSequence(org.openscience.cdk.interfaces.IChemSequence)

Example 10 with IChemSequence

use of org.openscience.cdk.interfaces.IChemSequence in project cdk by cdk.

the class PCCompoundASNReader method readChemFile.

// private procedures
private IChemFile readChemFile(IChemFile file) throws Exception {
    IChemSequence chemSequence = file.getBuilder().newInstance(IChemSequence.class);
    IChemModel chemModel = file.getBuilder().newInstance(IChemModel.class);
    IAtomContainerSet moleculeSet = file.getBuilder().newInstance(IAtomContainerSet.class);
    molecule = file.getBuilder().newInstance(IAtomContainer.class);
    atomIDs = new HashMap<>();
    String line = input.readLine();
    while (input.ready() && line != null) {
        if (line.indexOf('{') != -1) {
            processBlock(line);
        } else {
            logger.warn("Skipping non-block: " + line);
        }
        line = input.readLine();
    }
    moleculeSet.addAtomContainer(molecule);
    chemModel.setMoleculeSet(moleculeSet);
    chemSequence.addChemModel(chemModel);
    file.addChemSequence(chemSequence);
    return file;
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) IChemModel(org.openscience.cdk.interfaces.IChemModel) IChemSequence(org.openscience.cdk.interfaces.IChemSequence)

Aggregations

IChemSequence (org.openscience.cdk.interfaces.IChemSequence)91 IChemModel (org.openscience.cdk.interfaces.IChemModel)79 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)58 Test (org.junit.Test)53 IChemFile (org.openscience.cdk.interfaces.IChemFile)45 InputStream (java.io.InputStream)39 IAtomContainerSet (org.openscience.cdk.interfaces.IAtomContainerSet)32 ChemFile (org.openscience.cdk.ChemFile)24 CMLReader (org.openscience.cdk.io.CMLReader)24 IOException (java.io.IOException)18 CDKException (org.openscience.cdk.exception.CDKException)16 IAtom (org.openscience.cdk.interfaces.IAtom)13 MDLV2000Reader (org.openscience.cdk.io.MDLV2000Reader)13 ICrystal (org.openscience.cdk.interfaces.ICrystal)11 ChemObject (org.openscience.cdk.ChemObject)10 IRingSet (org.openscience.cdk.interfaces.IRingSet)9 ByteArrayInputStream (java.io.ByteArrayInputStream)7 Point3d (javax.vecmath.Point3d)7 Element (nu.xom.Element)6 IChemObject (org.openscience.cdk.interfaces.IChemObject)6