Search in sources :

Example 1 with AtomContainerSet

use of org.openscience.cdk.silent.AtomContainerSet in project ambit-mirror by ideaconsult.

the class DelimitedFileReader method readSetOfMolecules.

private IAtomContainerSet readSetOfMolecules() {
    try {
        IAtomContainerSet som = new AtomContainerSet();
        IteratingDelimitedFileReader reader = new IteratingDelimitedFileReader(input, format);
        while (reader.hasNext()) {
            Object object = reader.next();
            if (object instanceof IAtomContainer)
                som.addAtomContainer((IAtomContainer) object);
        }
        reader.close();
        return som;
    } catch (Exception x) {
        logger.error(x);
        return null;
    }
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) IChemObject(org.openscience.cdk.interfaces.IChemObject) AtomContainerSet(org.openscience.cdk.silent.AtomContainerSet) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) CDKException(org.openscience.cdk.exception.CDKException) IOException(java.io.IOException)

Example 2 with AtomContainerSet

use of org.openscience.cdk.silent.AtomContainerSet in project ambit-mirror by ideaconsult.

the class MoleculeEditAction method getMoleculeForEdit.

protected IAtomContainerSet getMoleculeForEdit(IAtomContainer atomContainer) throws Exception {
    if (atomContainer == null)
        return null;
    if (atomContainer instanceof QueryAtomContainer) {
        return null;
    }
    IAtomContainerSet molecules = ConnectivityChecker.partitionIntoMolecules(atomContainer);
    IAtomContainerSet m = new AtomContainerSet();
    for (int i = 0; i < molecules.getAtomContainerCount(); i++) {
        IAtomContainer a = molecules.getAtomContainer(i);
        if (!GeometryTools.has2DCoordinates(a)) {
            if (sdg == null)
                sdg = new StructureDiagramGenerator();
            sdg.setMolecule((IAtomContainer) a);
            sdg.generateCoordinates(new Vector2d(0, 1));
            molecules.replaceAtomContainer(i, sdg.getMolecule());
        }
        m.addAtomContainer(molecules.getAtomContainer(i));
    }
    if (// otherwise JChemPaint crashes
    m.getAtomContainerCount() == 0)
        m.addAtomContainer(MoleculeTools.newMolecule(DefaultChemObjectBuilder.getInstance()));
    return m;
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) Vector2d(javax.vecmath.Vector2d) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) QueryAtomContainer(org.openscience.cdk.isomorphism.matchers.QueryAtomContainer) AtomContainerSet(org.openscience.cdk.silent.AtomContainerSet) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) StructureDiagramGenerator(org.openscience.cdk.layout.StructureDiagramGenerator)

Example 3 with AtomContainerSet

use of org.openscience.cdk.silent.AtomContainerSet in project ambit-mirror by ideaconsult.

the class StructureEditorProcessor method process.

@Override
public String process(IStructureRecord record) throws AmbitException {
    try {
        MOL_TYPE mtype = MOL_TYPE.valueOf(record.getFormat());
        IAtomContainer mol = null;
        switch(mtype) {
            case SDF:
                {
                    MoleculeReader reader = new MoleculeReader();
                    mol = reader.process(record);
                    AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(mol);
                    reader = null;
                    break;
                }
            case CSV:
                {
                    // smiles
                    SmilesParserWrapper p = SmilesParserWrapper.getInstance(SMILES_PARSER.CDK);
                    mol = p.parseSmiles(record.getContent());
                    break;
                }
            default:
                throw new AmbitException("Unsupported format");
        }
        // actions
        if (mol != null && mol.getAtomCount() > 0)
            switch(command) {
                case layout:
                    {
                        IAtomContainerSet molecules = new AtomContainerSet();
                        CompoundImageTools cit = new CompoundImageTools();
                        cit.generate2D(mol, true, molecules);
                        mol = MoleculeTools.newAtomContainer(mol.getBuilder());
                        for (IAtomContainer m : molecules.atomContainers()) {
                            mol.add(m);
                        }
                        break;
                    }
                case aromatize:
                    if (mol != null)
                        CDKHueckelAromaticityDetector.detectAromaticity(mol);
                    break;
                case dearomatize:
                    if (mol != null) {
                        Kekulization.kekulize(mol);
                        for (IBond bond : mol.bonds()) bond.setFlag(CDKConstants.ISAROMATIC, false);
                    }
                    break;
                default:
                    throw new AmbitException("Unknown command");
            }
        StringWriter w = new StringWriter();
        SDFWriter writer = new SDFWriter(w);
        writer.write(mol);
        writer.close();
        return w.toString();
    } catch (AmbitException x) {
        throw x;
    } catch (Exception x) {
        throw new AmbitException(x);
    }
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) MoleculeReader(ambit2.core.processors.structure.MoleculeReader) StringWriter(java.io.StringWriter) MOL_TYPE(ambit2.base.interfaces.IStructureRecord.MOL_TYPE) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) IBond(org.openscience.cdk.interfaces.IBond) SDFWriter(org.openscience.cdk.io.SDFWriter) SmilesParserWrapper(ambit2.core.smiles.SmilesParserWrapper) AtomContainerSet(org.openscience.cdk.silent.AtomContainerSet) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) AmbitException(net.idea.modbcum.i.exceptions.AmbitException)

Example 4 with AtomContainerSet

use of org.openscience.cdk.silent.AtomContainerSet in project cdk by cdk.

the class CrossoverMachineTest method testdoCrossover_IAtomContainer.

@Test
public void testdoCrossover_IAtomContainer() throws Exception {
    String filename = "c10h16isomers.smi";
    InputStream ins = this.getClass().getResourceAsStream(filename);
    SMILESReader reader = new SMILESReader(ins);
    IAtomContainerSet som = reader.read(new AtomContainerSet());
    Assert.assertEquals("We must have read 99 structures", 99, som.getAtomContainerCount());
    CrossoverMachine cm = new CrossoverMachine();
    String correctFormula = "C10H16";
    int errorcount = 0;
    for (int i = 0; i < som.getAtomContainerCount(); i++) {
        int[] hydrogencount1 = new int[4];
        for (IAtom atom : som.getAtomContainer(i).atoms()) {
            hydrogencount1[atom.getImplicitHydrogenCount()]++;
        }
        for (int k = i + 1; k < som.getAtomContainerCount(); k++) {
            try {
                List<IAtomContainer> result = cm.doCrossover(som.getAtomContainer(i), som.getAtomContainer(k));
                int[] hydrogencount2 = new int[4];
                for (IAtom atom : som.getAtomContainer(k).atoms()) {
                    hydrogencount2[atom.getImplicitHydrogenCount()]++;
                }
                Assert.assertEquals("Result size must be 2", 2, result.size());
                for (int l = 0; l < 2; l++) {
                    IAtomContainer ac = result.get(l);
                    Assert.assertTrue("Result must be connected", ConnectivityChecker.isConnected(ac));
                    Assert.assertEquals("Molecular formula must be the same as" + "of the input", MolecularFormulaManipulator.getString(MolecularFormulaManipulator.getMolecularFormula(ac)), correctFormula);
                    int[] hydrogencountresult = new int[4];
                    int hcounttotal = 0;
                    for (IAtom atom : result.get(l).atoms()) {
                        hydrogencountresult[atom.getImplicitHydrogenCount()]++;
                        hcounttotal += atom.getImplicitHydrogenCount();
                    }
                    if (hydrogencount1[0] == hydrogencount2[0])
                        Assert.assertEquals("Hydrogen count of the result must" + " be same as of input", hydrogencount1[0], hydrogencountresult[0]);
                    if (hydrogencount1[1] == hydrogencount2[1])
                        Assert.assertEquals("Hydrogen count of the result must" + " be same as of input", hydrogencount1[1], hydrogencountresult[1]);
                    if (hydrogencount1[2] == hydrogencount2[2])
                        Assert.assertEquals("Hydrogen count of the result must" + " be same as of input", hydrogencount1[2], hydrogencountresult[2]);
                    if (hydrogencount1[3] == hydrogencount2[3])
                        Assert.assertEquals("Hydrogen count of the result must" + " be same as of input", hydrogencount1[3], hydrogencountresult[3]);
                    Assert.assertEquals(16, hcounttotal);
                }
            } catch (CDKException ex) {
                errorcount++;
            }
        }
    }
    Assert.assertTrue("We tolerate up to 300 errors", errorcount < 300);
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) InputStream(java.io.InputStream) CDKException(org.openscience.cdk.exception.CDKException) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) SMILESReader(org.openscience.cdk.io.SMILESReader) AtomContainerSet(org.openscience.cdk.silent.AtomContainerSet) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) IAtom(org.openscience.cdk.interfaces.IAtom) Test(org.junit.Test) SlowTest(org.openscience.cdk.test.SlowTest)

Example 5 with AtomContainerSet

use of org.openscience.cdk.silent.AtomContainerSet in project cdk by cdk.

the class CML2WriterTest method testMoleculeSetID.

@Test
public void testMoleculeSetID() throws Exception {
    StringWriter writer = new StringWriter();
    IAtomContainerSet moleculeSet = new AtomContainerSet();
    moleculeSet.setID("ms0");
    CMLWriter cmlWriter = new CMLWriter(writer);
    cmlWriter.write(moleculeSet);
    cmlWriter.close();
    String cmlContent = writer.toString();
    logger.debug("****************************** testReactionCustomization()");
    logger.debug(cmlContent);
    logger.debug("******************************");
    Assert.assertTrue(cmlContent.contains("<moleculeList convention=\"cdk:moleculeSet\" id=\"ms0"));
}
Also used : StringWriter(java.io.StringWriter) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) AtomContainerSet(org.openscience.cdk.silent.AtomContainerSet) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) CMLWriter(org.openscience.cdk.io.CMLWriter) Test(org.junit.Test)

Aggregations

IAtomContainerSet (org.openscience.cdk.interfaces.IAtomContainerSet)7 AtomContainerSet (org.openscience.cdk.silent.AtomContainerSet)7 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)5 StringWriter (java.io.StringWriter)3 AmbitException (net.idea.modbcum.i.exceptions.AmbitException)3 CDKException (org.openscience.cdk.exception.CDKException)3 Test (org.junit.Test)2 IAtom (org.openscience.cdk.interfaces.IAtom)2 IBond (org.openscience.cdk.interfaces.IBond)2 SDFWriter (org.openscience.cdk.io.SDFWriter)2 MOL_TYPE (ambit2.base.interfaces.IStructureRecord.MOL_TYPE)1 STRUC_TYPE (ambit2.base.interfaces.IStructureRecord.STRUC_TYPE)1 MoleculeReader (ambit2.core.processors.structure.MoleculeReader)1 SmilesParserWrapper (ambit2.core.smiles.SmilesParserWrapper)1 Dimension (java.awt.Dimension)1 Graphics2D (java.awt.Graphics2D)1 Rectangle (java.awt.Rectangle)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Point2d (javax.vecmath.Point2d)1