Search in sources :

Example 1 with CDKHydrogenAdder

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

the class SmilesConverter method transform.

public Molecule transform(String smiles, boolean addHydrogens, boolean calculateCoordinate, boolean expliciteHydrogens) throws InvalidSmilesException {
    Molecule imol = null;
    try {
        imol = new Molecule(sp.parseSmiles(smiles));
        // Add hydrogens
        if (addHydrogens) {
            CDKHydrogenAdder adder = CDKHydrogenAdder.getInstance(imol.getBuilder());
            try {
                adder.addImplicitHydrogens(imol);
                if (expliciteHydrogens)
                    AtomContainerManipulator.convertImplicitToExplicitHydrogens(imol);
            } catch (CDKException e) {
                e.printStackTrace();
            }
        } else {
            for (IAtom a : imol.atoms()) a.setImplicitHydrogenCount(0);
            if (!expliciteHydrogens)
                imol = new Molecule(AtomContainerManipulator.removeHydrogens(imol));
        }
        if (calculateCoordinate) {
            StructureDiagramGenerator sdg = new StructureDiagramGenerator(imol);
            try {
                sdg.generateCoordinates();
            } catch (CDKException e) {
                System.err.println(smiles);
                e.printStackTrace();
            }
            imol = new Molecule(sdg.getMolecule());
        }
    } catch (InvalidSmilesException e) {
        throw e;
    }
    return imol;
}
Also used : Molecule(org.openscience.cdk.Molecule) IMolecule(org.openscience.cdk.interfaces.IMolecule) CDKException(org.openscience.cdk.exception.CDKException) CDKHydrogenAdder(org.openscience.cdk.tools.CDKHydrogenAdder) InvalidSmilesException(org.openscience.cdk.exception.InvalidSmilesException) IAtom(org.openscience.cdk.interfaces.IAtom) StructureDiagramGenerator(org.openscience.cdk.layout.StructureDiagramGenerator)

Aggregations

Molecule (org.openscience.cdk.Molecule)1 CDKException (org.openscience.cdk.exception.CDKException)1 InvalidSmilesException (org.openscience.cdk.exception.InvalidSmilesException)1 IAtom (org.openscience.cdk.interfaces.IAtom)1 IMolecule (org.openscience.cdk.interfaces.IMolecule)1 StructureDiagramGenerator (org.openscience.cdk.layout.StructureDiagramGenerator)1 CDKHydrogenAdder (org.openscience.cdk.tools.CDKHydrogenAdder)1