Search in sources :

Example 6 with Bond

use of org.openscience.cdk.Bond in project Smiles2Monomers by yoann-dufresne.

the class Chain method getMolecule.

/**
 * construct recursively a molecule corresponding to the bloc.
 * Attention, this function is not very efficient because of multiple molecules creation.
 * @return Bloc molecule. Can return null if there are problems of compatibility between sub-bloc and extension positions.
 */
public IMolecule getMolecule() {
    if (this.mol != null)
        return this.mol;
    IMolecule mol = null;
    this.atoms = new ArrayList<>();
    // Sub-bloc molecule
    if (this.getSize() > 1) {
        IMolecule subMol = this.subBlc.getMolecule();
        mol = new Molecule();
        for (IAtom a : subMol.atoms()) mol.addAtom(a);
        for (IBond b : subMol.bonds()) mol.addBond(b);
        for (IAtom a : this.subBlc.atoms) {
            IAtom cloneA = mol.getAtom(subMol.getAtomNumber(a));
            this.atoms.add(cloneA);
        }
    } else {
        mol = new Molecule();
    }
    // Extension of mol
    IAtom a1 = this.ext.getBond().getAtom(0);
    if (this.position1 == -1) {
        try {
            a1 = (IAtom) a1.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        mol.addAtom(a1);
        this.atoms.add(a1);
        // Hydrogens
        int hydrogens = this.ext.getBond().getAtom(0).getImplicitHydrogenCount();
        a1.setImplicitHydrogenCount(hydrogens);
        // Aromatic
        if (this.ext.getBond().getAtom(0).getFlag(CDKConstants.ISAROMATIC))
            a1.setFlag(CDKConstants.ISAROMATIC, true);
        else
            a1.setFlag(CDKConstants.ISAROMATIC, false);
    } else {
        if (a1.getAtomicNumber() == this.atoms.get(this.position1).getAtomicNumber())
            a1 = this.atoms.get(this.position1);
        else
            return null;
    }
    IAtom a2 = this.ext.getBond().getAtom(1);
    if (this.position2 == -1) {
        try {
            a2 = (IAtom) a2.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        mol.addAtom(a2);
        this.atoms.add(a2);
        // Hydrogen
        int hydrogens = this.ext.getBond().getAtom(1).getImplicitHydrogenCount();
        a2.setImplicitHydrogenCount(hydrogens);
        // Aromatic
        if (this.ext.getBond().getAtom(1).getFlag(CDKConstants.ISAROMATIC))
            a2.setFlag(CDKConstants.ISAROMATIC, true);
        else
            a2.setFlag(CDKConstants.ISAROMATIC, false);
    } else if (a2.getAtomicNumber() == this.atoms.get(this.position2).getAtomicNumber())
        a2 = this.atoms.get(this.position2);
    else
        return null;
    mol.addBond(new Bond(a1, a2, this.ext.getBond().getOrder()));
    this.mol = mol;
    return mol;
}
Also used : Molecule(org.openscience.cdk.Molecule) IMolecule(org.openscience.cdk.interfaces.IMolecule) IMolecule(org.openscience.cdk.interfaces.IMolecule) IBond(org.openscience.cdk.interfaces.IBond) Bond(org.openscience.cdk.Bond) IBond(org.openscience.cdk.interfaces.IBond) IAtom(org.openscience.cdk.interfaces.IAtom)

Aggregations

Bond (org.openscience.cdk.Bond)6 IAtom (org.openscience.cdk.interfaces.IAtom)6 IBond (org.openscience.cdk.interfaces.IBond)4 Before (org.junit.Before)3 Chain (algorithms.isomorphism.chains.Chain)2 Extension (algorithms.isomorphism.chains.Extension)2 ArrayList (java.util.ArrayList)2 Atom (org.openscience.cdk.Atom)2 Atom (org.openscience.cdk.silent.Atom)2 MappedChain (algorithms.isomorphism.chains.MappedChain)1 HashMap (java.util.HashMap)1 Monomer (model.Monomer)1 Polymer (model.Polymer)1 Molecule (org.openscience.cdk.Molecule)1 IMolecule (org.openscience.cdk.interfaces.IMolecule)1