Search in sources :

Example 16 with IChemObject

use of org.openscience.cdk.interfaces.IChemObject in project ambit-mirror by ideaconsult.

the class TestTautomersFromSDF method test_CHEMBLSDF.

public void test_CHEMBLSDF(String resource) throws Exception {
    URL uri = TestTautomersFromSDF.class.getResource(resource);
    Assert.assertNotNull(uri);
    File infile = new File(uri.getFile());
    Assert.assertNotNull(infile);
    Assert.assertTrue(infile.exists());
    MoleculeReader molReader = new MoleculeReader();
    TautomerProcessor tautomers = new TautomerProcessor(null);
    tautomers.setCallback(new DefaultAmbitProcessor<IAtomContainer, IAtomContainer>() {

        int no_rank;

        @Override
        public IAtomContainer process(IAtomContainer target) throws Exception {
            Object rank = target.getProperty(TautomerConst.TAUTOMER_RANK);
            if (rank == null) {
                rank = target.getProperty(TautomerConst.CACTVS_ENERGY_RANK);
                if (rank == null) {
                    System.out.println();
                    no_rank++;
                    if (no_rank > 1)
                        throw new Exception(String.format("More than one (%d) structure without rank ! [%s]", no_rank, target.getProperties()));
                }
            }
            return target;
        }

        @Override
        public void close() throws Exception {
            Assert.assertTrue(no_rank <= 1);
        }
    });
    IIteratingChemObjectReader<IChemObject> reader = null;
    try {
        reader = FileInputState.getReader(infile);
        // InputStreamReader(in));
        while (reader.hasNext()) {
            if (reader instanceof IRawReader) {
                IStructureRecord record = ((IRawReader<IStructureRecord>) reader).nextRecord();
                IAtomContainer mol = molReader.process(record);
                IAtomContainer tautomer = tautomers.process(mol);
                tautomer.addProperties(mol.getProperties());
                // return normalizer.process(record);
                StringWriter w = new StringWriter();
                SDFWriter sdfwriter = new SDFWriter(w);
                sdfwriter.write(tautomer);
                sdfwriter.close();
                record.setContent(w.toString());
                record.setType(STRUC_TYPE.D2noH);
            // System.out.println(record.getContent());
            }
        }
    } catch (Exception x) {
        throw x;
    } finally {
        if (reader != null)
            reader.close();
    }
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) TautomerProcessor(ambit2.tautomers.processor.TautomerProcessor) SDFWriter(org.openscience.cdk.io.SDFWriter) URL(java.net.URL) IStructureRecord(ambit2.base.interfaces.IStructureRecord) MoleculeReader(ambit2.core.processors.structure.MoleculeReader) StringWriter(java.io.StringWriter) IChemObject(org.openscience.cdk.interfaces.IChemObject) IChemObject(org.openscience.cdk.interfaces.IChemObject) IRawReader(ambit2.core.io.IRawReader) File(java.io.File)

Example 17 with IChemObject

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

the class QueryAtomContainerCreator method suppressQueryHydrogens.

public static IAtomContainer suppressQueryHydrogens(IAtomContainer mol) {
    // pre-checks
    for (IAtom atom : mol.atoms()) {
        if (!(AtomRef.deref(atom) instanceof QueryAtom))
            throw new IllegalArgumentException("Non-query atoms found!");
    }
    for (IBond bond : mol.bonds()) {
        if (!(BondRef.deref(bond) instanceof QueryBond))
            throw new IllegalArgumentException("Non-query bonds found!");
    }
    Map<IChemObject, IChemObject> plainHydrogens = new HashMap<>();
    for (IAtom atom : mol.atoms()) {
        int hcnt = 0;
        for (IAtom nbor : mol.getConnectedAtomsList(atom)) {
            QueryAtom qnbor = (QueryAtom) AtomRef.deref(nbor);
            if (mol.getConnectedBondsCount(nbor) == 1 && isSimpleHydrogen(qnbor.getExpression())) {
                hcnt++;
                plainHydrogens.put(nbor, atom);
            }
        }
        if (hcnt > 0) {
            QueryAtom qatom = (QueryAtom) AtomRef.deref(atom);
            Expr e = qatom.getExpression();
            Expr hexpr = new Expr();
            for (int i = 0; i < hcnt; i++) hexpr.and(new Expr(Expr.Type.TOTAL_H_COUNT, i).negate());
            e.and(hexpr);
        }
    }
    // nothing to do
    if (plainHydrogens.isEmpty())
        return mol;
    IAtomContainer res = new QueryAtomContainer(mol.getBuilder());
    for (IAtom atom : mol.atoms()) {
        if (!plainHydrogens.containsKey(atom))
            res.addAtom(atom);
    }
    for (IBond bond : mol.bonds()) {
        if (!plainHydrogens.containsKey(bond.getBegin()) && !plainHydrogens.containsKey(bond.getEnd()))
            res.addBond(bond);
    }
    for (IStereoElement se : mol.stereoElements()) {
        res.addStereoElement(se.map(plainHydrogens));
    }
    return res;
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) HashMap(java.util.HashMap) IBond(org.openscience.cdk.interfaces.IBond) IChemObject(org.openscience.cdk.interfaces.IChemObject) IAtom(org.openscience.cdk.interfaces.IAtom) IStereoElement(org.openscience.cdk.interfaces.IStereoElement)

Example 18 with IChemObject

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

the class ChemObjectTestHelper method testClone_ChemObjectListeners.

public static void testClone_ChemObjectListeners(IChemObject chemObject) throws Exception {
    ChemObjectListener listener = new ChemObjectListener();
    chemObject.addListener(listener);
    IChemObject chemObject2 = (IChemObject) chemObject.clone();
    // test lack of cloning of listeners
    Assert.assertEquals(0, chemObject.getListenerCount());
    Assert.assertEquals(0, chemObject2.getListenerCount());
}
Also used : IChemObject(org.openscience.cdk.interfaces.IChemObject)

Example 19 with IChemObject

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

the class MappingTest method testMapping_IChemObject_IChemObject.

@Test
public void testMapping_IChemObject_IChemObject() {
    IChemObject object = newChemObject();
    IMapping mapping = new Mapping(object.getBuilder().newInstance(IAtom.class), object.getBuilder().newInstance(IAtom.class));
    Assert.assertNotNull(mapping);
}
Also used : IChemObject(org.openscience.cdk.interfaces.IChemObject) IMapping(org.openscience.cdk.interfaces.IMapping) IAtom(org.openscience.cdk.interfaces.IAtom) IMapping(org.openscience.cdk.interfaces.IMapping) Test(org.junit.Test) AbstractMappingTest(org.openscience.cdk.test.interfaces.AbstractMappingTest)

Example 20 with IChemObject

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

the class BondTest method testBond_IAtom_IAtom.

@Test
public void testBond_IAtom_IAtom() {
    IChemObject object = newChemObject();
    IAtom c = object.getBuilder().newInstance(IAtom.class, "C");
    IAtom o = object.getBuilder().newInstance(IAtom.class, "O");
    IBond bond = new Bond(c, o);
    Assert.assertEquals(2, bond.getAtomCount());
    Assert.assertEquals(c, bond.getBegin());
    Assert.assertEquals(o, bond.getEnd());
    Assert.assertEquals(IBond.Order.SINGLE, bond.getOrder());
    Assert.assertEquals(IBond.Stereo.NONE, bond.getStereo());
}
Also used : IChemObject(org.openscience.cdk.interfaces.IChemObject) IBond(org.openscience.cdk.interfaces.IBond) IBond(org.openscience.cdk.interfaces.IBond) IAtom(org.openscience.cdk.interfaces.IAtom) Test(org.junit.Test) AbstractBondTest(org.openscience.cdk.test.interfaces.AbstractBondTest)

Aggregations

IChemObject (org.openscience.cdk.interfaces.IChemObject)195 Test (org.junit.Test)139 IAtom (org.openscience.cdk.interfaces.IAtom)61 IBond (org.openscience.cdk.interfaces.IBond)51 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)32 HashMap (java.util.HashMap)21 SuppleAtomContainer (ambit2.core.groups.SuppleAtomContainer)16 AbstractBondTest (org.openscience.cdk.test.interfaces.AbstractBondTest)16 ArrayList (java.util.ArrayList)15 ISGroup (ambit2.core.groups.ISGroup)14 IStereoElement (org.openscience.cdk.interfaces.IStereoElement)12 InputStream (java.io.InputStream)11 Sgroup (org.openscience.cdk.sgroup.Sgroup)11 IAtomContainerSet (org.openscience.cdk.interfaces.IAtomContainerSet)9 ISingleElectron (org.openscience.cdk.interfaces.ISingleElectron)9 Map (java.util.Map)8 CDKException (org.openscience.cdk.exception.CDKException)8 ChemObject (org.openscience.cdk.ChemObject)7 IElement (org.openscience.cdk.interfaces.IElement)7 IOException (java.io.IOException)6