Search in sources :

Example 1 with AtomContainer

use of org.openscience.cdk.AtomContainer in project ErtlFunctionalGroupsFinder by zielesny.

the class ErtlFunctionalGroupsFinderTest method buildFunctionalGroup.

private IAtomContainer buildFunctionalGroup(String string) {
    IAtom a1, a2, a3, a4, a5, a6, a7, a8, a9;
    IBond b1, b2, b3, b4, b5, b6, b7, b8, b9;
    IChemObjectBuilder builder = DefaultChemObjectBuilder.getInstance();
    IAtomContainer container;
    // custom templates
    switch(string) {
        case "NarR3":
            a1 = builder.newInstance(IPseudoAtom.class, "R");
            a2 = builder.newInstance(IPseudoAtom.class, "R");
            a3 = builder.newInstance(IPseudoAtom.class, "R");
            a4 = builder.newInstance(IAtom.class, "N");
            a4.setIsAromatic(true);
            b1 = builder.newInstance(IBond.class, a1, a4, Order.SINGLE);
            b2 = builder.newInstance(IBond.class, a2, a4, Order.SINGLE);
            b3 = builder.newInstance(IBond.class, a3, a4, Order.SINGLE);
            container = new AtomContainer();
            container.setAtoms(new IAtom[] { a1, a2, a3, a4 });
            container.setBonds(new IBond[] { b1, b2, b3 });
            return container;
        case "SarR2":
            a1 = builder.newInstance(IPseudoAtom.class, "R");
            a2 = builder.newInstance(IPseudoAtom.class, "R");
            a3 = builder.newInstance(IAtom.class, "S");
            a3.setIsAromatic(true);
            b1 = builder.newInstance(IBond.class, a1, a3, Order.SINGLE);
            b2 = builder.newInstance(IBond.class, a2, a3, Order.SINGLE);
            container = new AtomContainer();
            container.setAtoms(new IAtom[] { a1, a2, a3 });
            container.setBonds(new IBond[] { b1, b2 });
            return container;
        case "OarR2":
            a1 = builder.newInstance(IPseudoAtom.class, "R");
            a2 = builder.newInstance(IPseudoAtom.class, "R");
            a3 = builder.newInstance(IAtom.class, "O");
            a3.setIsAromatic(true);
            b1 = builder.newInstance(IBond.class, a1, a3, Order.SINGLE);
            b2 = builder.newInstance(IBond.class, a2, a3, Order.SINGLE);
            container = new AtomContainer();
            container.setAtoms(new IAtom[] { a1, a2, a3 });
            container.setBonds(new IBond[] { b1, b2 });
            return container;
        // smiles
        default:
            try {
                SmilesParser smilesParser = new SmilesParser(DefaultChemObjectBuilder.getInstance());
                try {
                    if (string.equals("[c]=O"))
                        smilesParser.kekulise(false);
                    container = smilesParser.parseSmiles(string);
                } catch (InvalidSmilesException e) {
                    smilesParser.kekulise(false);
                    container = smilesParser.parseSmiles(string);
                }
                for (IAtom a : container.atoms()) {
                    if (a instanceof PseudoAtom) {
                        a.setSymbol("R");
                    }
                }
                return container;
            } catch (InvalidSmilesException e) {
                throw new IllegalArgumentException("Input string '" + string + " could not be found as a template " + "and is not a valid SMILES string.");
            }
    }
}
Also used : SmilesParser(org.openscience.cdk.smiles.SmilesParser) AtomContainer(org.openscience.cdk.AtomContainer) InvalidSmilesException(org.openscience.cdk.exception.InvalidSmilesException) PseudoAtom(org.openscience.cdk.PseudoAtom)

Example 2 with AtomContainer

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

the class SLN2ChemObject method slnContainerToAtomContainer.

public IAtomContainer slnContainerToAtomContainer(SLNContainer slnContainer) {
    clearAllErrorsAndWarnings();
    IAtomContainer container = new AtomContainer();
    Map<SLNAtom, IAtom> convertedAtoms = new HashMap<SLNAtom, IAtom>();
    for (int i = 0; i < slnContainer.getAtomCount(); i++) {
        SLNAtom slnAtom = (SLNAtom) slnContainer.getAtom(i);
        IAtom atom = slnAtomToAtom(slnAtom);
        if (currentConversionWarning != null)
            conversionWarnings.add(currentConversionWarning + " for atom: " + (i + 1));
        if (atom == null) {
            conversionErrors.add(currentConversionError + " for atom: " + (i + 1));
            continue;
        }
        container.addAtom(atom);
        convertedAtoms.put(slnAtom, atom);
    }
    for (int i = 0; i < slnContainer.getBondCount(); i++) {
        SLNBond slnBbond = (SLNBond) slnContainer.getBond(i);
        IBond bond = slnBondToBond(slnBbond);
        if (currentConversionWarning != null)
            conversionWarnings.add(currentConversionWarning + " for bond: " + (i + 1));
        if (bond == null) {
            conversionErrors.add(currentConversionError + " for bond: " + (i + 1));
            continue;
        }
        IAtom[] newAtoms = new IAtom[2];
        newAtoms[0] = convertedAtoms.get(slnBbond.getAtom(0));
        newAtoms[1] = convertedAtoms.get(slnBbond.getAtom(1));
        if (newAtoms[0] == null || newAtoms[1] == null)
            // one of the atoms is not converted
            continue;
        bond.setAtoms(newAtoms);
        container.addBond(bond);
    }
    try {
        processMolecule(container);
    } catch (Exception x) {
        conversionErrors.add(x.getMessage());
    }
    return container;
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) QueryAtomContainer(org.openscience.cdk.isomorphism.matchers.QueryAtomContainer) AtomContainer(org.openscience.cdk.AtomContainer) IQueryAtomContainer(org.openscience.cdk.isomorphism.matchers.IQueryAtomContainer) HashMap(java.util.HashMap) SLNBond(ambit2.sln.SLNBond) IBond(org.openscience.cdk.interfaces.IBond) SLNAtom(ambit2.sln.SLNAtom) IAtom(org.openscience.cdk.interfaces.IAtom)

Example 3 with AtomContainer

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

the class ChemObjectFactory method condenseStructures.

public IAtomContainer condenseStructures(IAtomContainer str1, int str1At0, int str1At1, IAtomContainer str2, int str2At0, int str2At1) throws Exception {
    IAtom at0 = null;
    IAtom at1 = null;
    IAtomContainer mol = new AtomContainer();
    // clone str1
    for (int i = 0; i < str1.getAtomCount(); i++) {
        IAtom at = (IAtom) str1.getAtom(i).clone();
        mol.addAtom(at);
        if (str1At0 == i)
            at0 = at;
        if (str1At1 == i)
            at1 = at;
    }
    for (int i = 0; i < str1.getBondCount(); i++) {
        IBond bo = str1.getBond(i);
        int at0Num = str1.getAtomNumber(bo.getAtom(0));
        int at1Num = str1.getAtomNumber(bo.getAtom(1));
        IBond newBo = new Bond();
        newBo.setOrder(bo.getOrder());
        newBo.setAtom(mol.getAtom(at0Num), 0);
        newBo.setAtom(mol.getAtom(at1Num), 1);
        mol.addBond(newBo);
    }
    HashMap<IAtom, IAtom> map = new HashMap<IAtom, IAtom>();
    // clone str2
    for (int i = 0; i < str2.getAtomCount(); i++) {
        IAtom at = str2.getAtom(i);
        if (str2At0 == i)
            map.put(at, at0);
        else if (str2At1 == i)
            map.put(at, at1);
        else {
            IAtom new_at = (IAtom) str2.getAtom(i).clone();
            mol.addAtom(new_at);
            map.put(at, new_at);
        }
    }
    for (int i = 0; i < str2.getBondCount(); i++) {
        IBond bo = str2.getBond(i);
        IAtom boAt0 = bo.getAtom(0);
        IAtom boAt1 = bo.getAtom(1);
        int at0Num = str2.getAtomNumber(boAt0);
        int at1Num = str2.getAtomNumber(boAt1);
        if (((at0Num == str2At0) && (at1Num == str2At1)) || ((at0Num == str2At1) && (at1Num == str2At0))) {
            // This is the condensation bond
            continue;
        } else {
            IBond newBo = new Bond();
            newBo.setOrder(bo.getOrder());
            newBo.setAtom(map.get(boAt0), 0);
            newBo.setAtom(map.get(boAt1), 1);
            mol.addBond(newBo);
        }
    }
    return mol;
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) IQueryAtomContainer(org.openscience.cdk.isomorphism.matchers.IQueryAtomContainer) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) AtomContainer(org.openscience.cdk.AtomContainer) HashMap(java.util.HashMap) IBond(org.openscience.cdk.interfaces.IBond) Bond(org.openscience.cdk.Bond) IBond(org.openscience.cdk.interfaces.IBond) IAtom(org.openscience.cdk.interfaces.IAtom)

Example 4 with AtomContainer

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

the class SMIRKSManager method getCloneStructure.

IAtomContainer getCloneStructure(IAtomContainer target) throws Exception {
    IAtomContainer mol = new AtomContainer();
    IAtom[] newAtoms = new IAtom[target.getAtomCount()];
    IBond[] newBonds = new IBond[target.getBondCount()];
    // Clone atoms
    for (int i = 0; i < target.getAtomCount(); i++) {
        IAtom a = target.getAtom(i);
        IAtom a1 = cloneAtom(a);
        mol.addAtom(a1);
        newAtoms[i] = a1;
    }
    // Clone bonds
    for (int i = 0; i < target.getBondCount(); i++) {
        IBond b = target.getBond(i);
        IBond b1 = MoleculeTools.newBond(target.getBuilder());
        IAtom[] a01 = new IAtom[2];
        int ind0 = target.getAtomNumber(b.getAtom(0));
        int ind1 = target.getAtomNumber(b.getAtom(1));
        a01[0] = mol.getAtom(ind0);
        a01[1] = mol.getAtom(ind1);
        b1.setAtoms(a01);
        b1.setOrder(b.getOrder());
        boolean FlagArom = b.getFlag(CDKConstants.ISAROMATIC);
        b1.setFlag(CDKConstants.ISAROMATIC, FlagArom);
        mol.addBond(b1);
        newBonds[i] = b1;
    }
    // Clone stereo elements
    for (IStereoElement element : target.stereoElements()) {
        if (element instanceof DoubleBondStereochemistry) {
            DoubleBondStereochemistry dbs0 = (DoubleBondStereochemistry) element;
            DoubleBondStereochemistry dbs = StereoChemUtils.cloneDoubleBondStereochemistry(dbs0, target, mol, newAtoms, newBonds);
            if (dbs != null)
                mol.addStereoElement(dbs);
            continue;
        }
        if (element instanceof TetrahedralChirality) {
            TetrahedralChirality thc0 = (TetrahedralChirality) element;
            TetrahedralChirality thc = StereoChemUtils.cloneTetrahedralChirality(thc0, target, mol, newAtoms);
            if (thc != null)
                mol.addStereoElement(thc);
            continue;
        }
    // TODO handle ExtendedTetrahedral (... allene like chiral centers)
    }
    return mol;
}
Also used : TetrahedralChirality(org.openscience.cdk.stereo.TetrahedralChirality) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) DoubleBondStereochemistry(org.openscience.cdk.stereo.DoubleBondStereochemistry) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) AtomContainer(org.openscience.cdk.AtomContainer) IQueryAtomContainer(org.openscience.cdk.isomorphism.matchers.IQueryAtomContainer) IBond(org.openscience.cdk.interfaces.IBond) IAtom(org.openscience.cdk.interfaces.IAtom) IStereoElement(org.openscience.cdk.interfaces.IStereoElement)

Example 5 with AtomContainer

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

the class TestUtilities method testHydrogenCount.

void testHydrogenCount() throws Exception {
    IAtomContainer mol = new AtomContainer();
    mol.addAtom(new Atom("C"));
    /**
     * https://sourceforge.net/tracker/?func=detail&aid=3020065&group_id=
     * 20024&atid=120024 Integer hc = mol.getAtom(0).getHydrogenCount();
     */
    Integer hc = mol.getAtom(0).getImplicitHydrogenCount();
    Integer fc = mol.getAtom(0).getFormalCharge();
    Integer nc = mol.getAtom(0).getFormalNeighbourCount();
    Integer v = mol.getAtom(0).getValency();
    System.out.println("getHydrogenCount() = " + hc);
    System.out.println("getFormalCharge() = " + fc);
    System.out.println("getFormalNeighbourCount() = " + nc);
    System.out.println("getValency() = " + v);
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) AtomContainer(org.openscience.cdk.AtomContainer) QueryAtomContainer(org.openscience.cdk.isomorphism.matchers.QueryAtomContainer) IQueryAtomContainer(org.openscience.cdk.isomorphism.matchers.IQueryAtomContainer) AnyAtom(org.openscience.cdk.isomorphism.matchers.smarts.AnyAtom) IQueryAtom(org.openscience.cdk.isomorphism.matchers.IQueryAtom) Atom(org.openscience.cdk.Atom) AliphaticSymbolAtom(org.openscience.cdk.isomorphism.matchers.smarts.AliphaticSymbolAtom) IAtom(org.openscience.cdk.interfaces.IAtom)

Aggregations

AtomContainer (org.openscience.cdk.AtomContainer)743 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)730 Test (org.junit.Test)641 IAtom (org.openscience.cdk.interfaces.IAtom)556 Atom (org.openscience.cdk.Atom)523 PseudoAtom (org.openscience.cdk.PseudoAtom)206 IBond (org.openscience.cdk.interfaces.IBond)136 Bond (org.openscience.cdk.Bond)90 Point2d (javax.vecmath.Point2d)86 InputStream (java.io.InputStream)80 IQueryAtomContainer (org.openscience.cdk.isomorphism.matchers.IQueryAtomContainer)61 QueryAtomContainer (org.openscience.cdk.isomorphism.matchers.QueryAtomContainer)57 MDLV2000Reader (org.openscience.cdk.io.MDLV2000Reader)44 SimpleChemObjectReaderTest (org.openscience.cdk.test.io.SimpleChemObjectReaderTest)43 ByteArrayInputStream (java.io.ByteArrayInputStream)41 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)32 SlowTest (org.openscience.cdk.test.SlowTest)32 Point3d (javax.vecmath.Point3d)30 IPseudoAtom (org.openscience.cdk.interfaces.IPseudoAtom)29 DoubleArrayResult (org.openscience.cdk.qsar.result.DoubleArrayResult)23