Search in sources :

Example 21 with IAtom

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

the class Extension method createBond.

private void createBond(String name0, int h0, boolean arom0, String name1, int h1, boolean arom1, Order ord) {
    if (arom0)
        name0 = name0.toLowerCase();
    if (arom1)
        name1 = name1.toLowerCase();
    List<String> names = new ArrayList<>();
    names.add(name0 + ":" + h0);
    names.add(name1 + ":" + h1);
    Collections.sort(names);
    // this.inverted = !names.get(0).equals(name0 + ":" + h0);
    IAtom c0 = null;
    String n0 = names.get(0);
    if (Extension.savedAtoms.containsKey(n0))
        c0 = Extension.savedAtoms.get(n0);
    else {
        String[] split = n0.split(":");
        char first = Character.toUpperCase(split[0].charAt(0));
        String name = first + split[0].substring(1);
        c0 = new Atom(name);
        c0.setFlag(CDKConstants.ISAROMATIC, Character.isLowerCase(split[0].charAt(0)));
        c0.setImplicitHydrogenCount(new Integer(split[1]));
        Extension.savedAtoms.put(n0, c0);
    }
    IAtom c1 = null;
    String n1 = names.get(1);
    if (Extension.savedAtoms.containsKey(n1))
        c1 = Extension.savedAtoms.get(n1);
    else {
        String[] split = n1.split(":");
        char first = Character.toUpperCase(split[0].charAt(0));
        String name = first + split[0].substring(1);
        c1 = new Atom(name);
        c1.setFlag(CDKConstants.ISAROMATIC, Character.isLowerCase(split[0].charAt(0)));
        c1.setImplicitHydrogenCount(new Integer(split[1]));
        Extension.savedAtoms.put(n1, c1);
    }
    String key = names.get(0) + ord + names.get(1);
    if (Extension.savedBonds.containsKey(key)) {
        this.bond = Extension.savedBonds.get(key);
    } else {
        this.bond = new Bond(c0, c1, ord);
        Extension.savedBonds.put(key, this.bond);
    }
}
Also used : ArrayList(java.util.ArrayList) Bond(org.openscience.cdk.Bond) IBond(org.openscience.cdk.interfaces.IBond) IAtom(org.openscience.cdk.interfaces.IAtom) Atom(org.openscience.cdk.Atom) IAtom(org.openscience.cdk.interfaces.IAtom)

Example 22 with IAtom

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

the class DeepenMatcher method transform.

/**
 * Transformation of mappings from tmp molecule to entire molecule.
 */
public static Match transform(Match match, IMolecule sub, IMolecule entire) {
    Match transformed = new Match(match.getResidue());
    // Atoms & Hydrogens
    for (int idx : match.getAtoms()) {
        IAtom a = sub.getAtom(idx);
        int realIdx = entire.getAtomNumber(a);
        transformed.addAtom(realIdx);
        transformed.addHydrogens(realIdx, match.getHydrogensFrom(idx));
    }
    // Bonds
    for (int idx : match.getBonds()) {
        IBond b = sub.getBond(idx);
        transformed.addBond(entire.getBondNumber(b));
    }
    return transformed;
}
Also used : IBond(org.openscience.cdk.interfaces.IBond) IAtom(org.openscience.cdk.interfaces.IAtom) Match(algorithms.utils.Match)

Example 23 with IAtom

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

the class PictureGenerator method transform.

/**/
public Molecule transform(String smiles) throws InvalidSmilesException {
    Molecule imol = null;
    imol = new Molecule(this.sp.parseSmiles(smiles));
    for (IAtom a : imol.atoms()) a.setImplicitHydrogenCount(0);
    StructureDiagramGenerator sdg = new StructureDiagramGenerator(imol);
    try {
        sdg.generateCoordinates();
    } catch (CDKException e) {
        System.err.println(smiles);
        e.printStackTrace();
    }
    imol = new Molecule(sdg.getMolecule());
    return imol;
}
Also used : Molecule(org.openscience.cdk.Molecule) IMolecule(org.openscience.cdk.interfaces.IMolecule) CDKException(org.openscience.cdk.exception.CDKException) IAtom(org.openscience.cdk.interfaces.IAtom) StructureDiagramGenerator(org.openscience.cdk.layout.StructureDiagramGenerator)

Example 24 with IAtom

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

the class SmilesGenerator method isValidDoubleBondConfiguration.

/**
 *  Tells if a certain bond is center of a valid double bond configuration.
 *
 *@param  container  The atomcontainer.
 *@param  bond       The bond.
 *@return            true=is a potential configuration, false=is not.
 */
public boolean isValidDoubleBondConfiguration(IAtomContainer container, IBond bond) {
    IAtom atom0 = bond.getAtom(0);
    IAtom atom1 = bond.getAtom(1);
    List<IAtom> connectedAtoms = container.getConnectedAtomsList(atom0);
    IAtom from = null;
    for (IAtom connectedAtom : connectedAtoms) {
        if (connectedAtom != atom1) {
            from = connectedAtom;
        }
    }
    boolean[] array = new boolean[container.getBondCount()];
    for (int i = 0; i < array.length; i++) {
        array[i] = true;
    }
    if (isStartOfDoubleBond(container, atom0, from, array) && isEndOfDoubleBond(container, atom1, atom0, array) && !bond.getFlag(CDKConstants.ISAROMATIC)) {
        return (true);
    } else {
        return (false);
    }
}
Also used : IAtom(org.openscience.cdk.interfaces.IAtom)

Example 25 with IAtom

use of org.openscience.cdk.interfaces.IAtom 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

IAtom (org.openscience.cdk.interfaces.IAtom)46 IMolecule (org.openscience.cdk.interfaces.IMolecule)16 IBond (org.openscience.cdk.interfaces.IBond)13 ArrayList (java.util.ArrayList)10 Residue (model.Residue)8 Rule (model.Rule)6 Bond (org.openscience.cdk.Bond)6 Molecule (org.openscience.cdk.Molecule)6 MappedChain (algorithms.isomorphism.chains.MappedChain)5 HashMap (java.util.HashMap)5 CDKException (org.openscience.cdk.exception.CDKException)5 Match (algorithms.utils.Match)4 Test (org.junit.Test)4 Chain (algorithms.isomorphism.chains.Chain)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Vector (java.util.Vector)3 Family (model.Family)3 JSONObject (org.json.simple.JSONObject)3 Before (org.junit.Before)3