Search in sources :

Example 1 with TestMethod

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

the class SmilesGenerator method createSMILESWithoutCheckForMultipleMolecules.

/**
 *  Generate canonical SMILES from the <code>molecule</code>. This method
 *  canonicaly lables the molecule but dose not perform any checks on the
 *  chemical validity of the molecule. Does not care about multiple molecules.
 *  IMPORTANT: A precomputed Set of All Rings (SAR) can be passed to this
 *  SmilesGenerator in order to avoid recomputing it. Use setRings() to
 *  assign the SAR.
 *
 * @param  molecule                 The molecule to evaluate.
 * @param  chiral                   true=SMILES will be chiral, false=SMILES
 *      will not be chiral.
 * @param  doubleBondConfiguration  Should E/Z configurations be read at these positions? If the flag at position X is set to true,
 *                                  an E/Z configuration will be written from coordinates around bond X, if false, it will be ignored.
 *                                  If flag is true for a bond which does not constitute a valid double bond configuration, it will be
 *                                  ignored (meaning setting all to true will create E/Z indication will be pu in the smiles wherever
 *                                  possible, but note the coordinates might be arbitrary).
 * @exception  CDKException         At least one atom has no Point2D;
 *      coordinates are needed for creating the chiral smiles. This excpetion
 *      can only be thrown if chiral smiles is created, ignore it if you want a
 *      non-chiral smiles (createSMILES(AtomContainer) does not throw an
 *      exception).
 *@see                             org.openscience.cdk.graph.invariant.CanonicalLabeler#canonLabel(IAtomContainer)
 * @return the SMILES representation of the molecule
 */
@TestMethod("testCreateSMILESWithoutCheckForMultipleMolecules_withDetectAromaticity,testCreateSMILESWithoutCheckForMultipleMolecules_withoutDetectAromaticity")
public synchronized String createSMILESWithoutCheckForMultipleMolecules(IAtomContainer molecule, boolean chiral, boolean[] doubleBondConfiguration) throws CDKException {
    if (molecule.getAtomCount() == 0) {
        return "";
    }
    canLabler.canonLabel(molecule);
    brokenBonds.clear();
    ringMarker = 0;
    IAtom start = null;
    for (int i = 0; i < molecule.getAtomCount(); i++) {
        IAtom atom = molecule.getAtom(i);
        if (chiral && atom.getPoint2d() == null) {
            throw new CDKException("Atom number " + i + " has no 2D coordinates, but 2D coordinates are needed for creating chiral smiles");
        }
        // logger.debug("Setting all VISITED flags to false");
        atom.setFlag(CDKConstants.VISITED, false);
        if ((Long) atom.getProperty("CanonicalLable") == 1) {
            start = atom;
        }
    }
    // detect aromaticity
    if (useAromaticityFlag || chiral) {
        if (rings == null) {
            if (ringFinder == null) {
                ringFinder = new AllRingsFinder();
            }
            rings = ringFinder.findAllRings(molecule);
        }
        AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(molecule);
        CDKHueckelAromaticityDetector.detectAromaticity(molecule);
    }
    if (chiral && rings.getAtomContainerCount() > 0) {
        List<?> v = RingPartitioner.partitionRings(rings);
        // logger.debug("RingSystems: " + v.size());
        for (int i = 0; i < v.size(); i++) {
            int counter = 0;
            Iterator<IAtomContainer> containers = RingSetManipulator.getAllAtomContainers((IRingSet) v.get(i)).iterator();
            while (containers.hasNext()) {
                IAtomContainer allrings = (IAtomContainer) containers.next();
                for (int k = 0; k < allrings.getAtomCount(); k++) {
                    if (!BondTools.isStereo(molecule, allrings.getAtom(k)) && hasWedges(molecule, allrings.getAtom(k)) != null) {
                        IBond bond = molecule.getBond(allrings.getAtom(k), hasWedges(molecule, allrings.getAtom(k)));
                        if (bond.getStereo() == IBond.Stereo.UP) {
                            allrings.getAtom(k).setProperty(RING_CONFIG, UP);
                        } else {
                            allrings.getAtom(k).setProperty(RING_CONFIG, DOWN);
                        }
                        counter++;
                    }
                }
                if (counter == 1) {
                    for (int k = 0; k < allrings.getAtomCount(); k++) {
                        IBond bond = molecule.getBond(allrings.getAtom(k), hasWedges(molecule, allrings.getAtom(k)));
                        if (bond != null) {
                            if (bond.getStereo() == IBond.Stereo.UP) {
                                allrings.getAtom(k).setProperty(RING_CONFIG, UP);
                            } else {
                                allrings.getAtom(k).setProperty(RING_CONFIG, DOWN);
                            }
                        }
                    }
                }
            }
        }
    }
    StringBuffer l = new StringBuffer();
    createSMILES(start, l, molecule, chiral, doubleBondConfiguration, useAromaticityFlag);
    rings = null;
    // remove all CanonicalLable/InvariancePair props
    for (int k = 0; k < molecule.getAtomCount(); k++) {
        molecule.getAtom(k).removeProperty("CanonicalLable");
        molecule.getAtom(k).removeProperty("InvariancePair");
    }
    return l.toString();
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) CDKException(org.openscience.cdk.exception.CDKException) IRingSet(org.openscience.cdk.interfaces.IRingSet) IBond(org.openscience.cdk.interfaces.IBond) AllRingsFinder(org.openscience.cdk.ringsearch.AllRingsFinder) IAtom(org.openscience.cdk.interfaces.IAtom) TestMethod(org.openscience.cdk.annotations.TestMethod)

Aggregations

TestMethod (org.openscience.cdk.annotations.TestMethod)1 CDKException (org.openscience.cdk.exception.CDKException)1 IAtom (org.openscience.cdk.interfaces.IAtom)1 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)1 IBond (org.openscience.cdk.interfaces.IBond)1 IRingSet (org.openscience.cdk.interfaces.IRingSet)1 AllRingsFinder (org.openscience.cdk.ringsearch.AllRingsFinder)1