Search in sources :

Example 16 with IChemModel

use of org.openscience.cdk.interfaces.IChemModel 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 17 with IChemModel

use of org.openscience.cdk.interfaces.IChemModel 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 18 with IChemModel

use of org.openscience.cdk.interfaces.IChemModel 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)

Example 19 with IChemModel

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

the class AbstractChemModelTest method testSetRingSet_IRingSet.

@Test
public void testSetRingSet_IRingSet() {
    IChemModel chemModel = (IChemModel) newChemObject();
    IRingSet crystal = chemModel.getBuilder().newInstance(IRingSet.class);
    chemModel.setRingSet(crystal);
    Assert.assertEquals(crystal, chemModel.getRingSet());
}
Also used : IRingSet(org.openscience.cdk.interfaces.IRingSet) IChemModel(org.openscience.cdk.interfaces.IChemModel) Test(org.junit.Test)

Example 20 with IChemModel

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

the class AbstractChemModelTest method testToString.

@Test
public void testToString() {
    IChemModel model = (IChemModel) newChemObject();
    String description = model.toString();
    for (int i = 0; i < description.length(); i++) {
        Assert.assertTrue(description.charAt(i) != '\n');
        Assert.assertTrue(description.charAt(i) != '\r');
    }
}
Also used : IChemModel(org.openscience.cdk.interfaces.IChemModel) Test(org.junit.Test)

Aggregations

IChemModel (org.openscience.cdk.interfaces.IChemModel)138 Test (org.junit.Test)97 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)85 IChemSequence (org.openscience.cdk.interfaces.IChemSequence)80 IAtomContainerSet (org.openscience.cdk.interfaces.IAtomContainerSet)53 IChemFile (org.openscience.cdk.interfaces.IChemFile)50 InputStream (java.io.InputStream)46 ChemFile (org.openscience.cdk.ChemFile)30 CMLReader (org.openscience.cdk.io.CMLReader)26 IAtom (org.openscience.cdk.interfaces.IAtom)24 ICrystal (org.openscience.cdk.interfaces.ICrystal)21 IOException (java.io.IOException)19 IRingSet (org.openscience.cdk.interfaces.IRingSet)18 CDKException (org.openscience.cdk.exception.CDKException)17 IReaction (org.openscience.cdk.interfaces.IReaction)17 IReactionSet (org.openscience.cdk.interfaces.IReactionSet)17 MDLV2000Reader (org.openscience.cdk.io.MDLV2000Reader)13 SimpleChemObjectReaderTest (org.openscience.cdk.test.io.SimpleChemObjectReaderTest)12 AbstractChemModelTest (org.openscience.cdk.test.interfaces.AbstractChemModelTest)11 ChemObject (org.openscience.cdk.ChemObject)10