Search in sources :

Example 6 with IBond

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

the class ChainLearning method recurChildrenAdds.

private void recurChildrenAdds(FamilyChainsDB fc, Residue from, Set<Residue> children) {
    for (Residue child : children) {
        List<ChainAdd> adds = fc.getAdds(child);
        // If extensions already exist, jump to the next son.
        if (adds.size() > 0)
            continue;
        List<MappedChain> mcs = this.calculateMapping(fc, child, from, new MappedChain(child));
        // Collections.sort(mcs, new MappedChainComparator());
        MappedChain mc = mcs.get(0);
        // Hydrogen adds
        for (int atomIdx : mc.getAtomsMapping()) {
            IAtom a = mc.getChemObject().getMolecule().getAtom(atomIdx);
            int chainIdx = mc.getAtomsMapping().indexOf(atomIdx);
            Map<Integer, Integer> mapping = mc.getHydrogensMapping();
            int mappingHydrogens = mapping.containsKey(atomIdx) ? mapping.get(atomIdx) : 0;
            int molHydrogens = a.getImplicitHydrogenCount();
            if (mappingHydrogens < molHydrogens)
                adds.add(new HydrogenAdd(from, chainIdx, molHydrogens - mappingHydrogens));
        }
        // Extensions
        IMolecule resMol = mc.getChemObject().getMolecule();
        for (IBond bond : resMol.bonds()) {
            int bondIdx = resMol.getBondNumber(bond);
            if (mc.getBondsMapping().contains(bondIdx))
                continue;
            Extension ext = new Extension(bond);
            List<MappedChain> tmpMappings = Isomorphism.searchFromPreviousMapping(mc, ext, MatchingType.EXACT);
            for (MappedChain tmp : tmpMappings) if (tmp.getBondsMapping().get(tmp.getBondsMapping().size() - 1) == bondIdx) {
                Chain chain = tmp.getChain();
                BondAdd ba = new BondAdd(from, ext, chain.getPosition1(), chain.getPosition2());
                adds.add(ba);
            }
        }
        /**/
        this.recurChildrenAdds(fc, child, fc.getFamily().getChildrenOf(child));
    }
}
Also used : IBond(org.openscience.cdk.interfaces.IBond) IMolecule(org.openscience.cdk.interfaces.IMolecule) Residue(model.Residue) IAtom(org.openscience.cdk.interfaces.IAtom)

Example 7 with IBond

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

the class ChainLearning method createBlocsFromPrevious.

private List<MappedChain> createBlocsFromPrevious(List<MappedChain> prevMbs) {
    List<MappedChain> nextMbs = new ArrayList<>();
    for (MappedChain mb : prevMbs) {
        ChemicalObject co = mb.getChemObject();
        IMolecule mol = co.getMolecule();
        List<Integer> neighbors = mb.getNeighborsBonds(mol);
        // Create a new bloc for each neighbor
        for (int idx : neighbors) {
            // Create bloc
            IBond nb = mol.getBond(idx);
            Extension ext = new Extension(nb);
            // Extension.setAromacityTest(false);
            List<MappedChain> newMbs = Isomorphism.searchFromPreviousMapping(mb, ext, MatchingType.EXACT);
            // Extension.setAromacityTest(true);
            nextMbs.addAll(newMbs);
        }
    }
    return nextMbs;
}
Also used : IMolecule(org.openscience.cdk.interfaces.IMolecule) ArrayList(java.util.ArrayList) IBond(org.openscience.cdk.interfaces.IBond) ChemicalObject(model.ChemicalObject)

Example 8 with IBond

use of org.openscience.cdk.interfaces.IBond 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 9 with IBond

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

the class AtomContainerToNgraph method convert.

public NGraph<GraphInput.InputData> convert(IAtomContainer mol) {
    NGraph<GraphInput.InputData> g = new ListGraph<GraphInput.InputData>();
    int numVertices = mol.getAtomCount();
    Hashtable<String, NVertex<GraphInput.InputData>> vertices = new Hashtable<String, NVertex<GraphInput.InputData>>();
    boolean directGraph = false;
    NVertex<GraphInput.InputData> vertexPrototype = new ListVertex<GraphInput.InputData>();
    for (IBond bond : mol.bonds()) {
        String atom1 = String.valueOf(mol.getAtomNumber(bond.getAtom(0)));
        String atom2 = String.valueOf(mol.getAtomNumber(bond.getAtom(1)));
        NVertex<GraphInput.InputData> v1, v2;
        boolean newNode = false;
        // The first vertex
        if (!vertices.containsKey(atom1)) {
            v1 = vertexPrototype.newOfSameType(new GraphInput.InputData(vertices.size(), atom1));
            vertices.put(v1.data.name, v1);
            g.addVertex(v1);
            newNode = true;
        } else {
            v1 = vertices.get(atom1);
        }
        // The second vertex
        if (!vertices.containsKey(atom2)) {
            v2 = vertexPrototype.newOfSameType(new GraphInput.InputData(vertices.size(), atom2));
            vertices.put(v2.data.name, v2);
            g.addVertex(v2);
            newNode = true;
        } else {
            v2 = vertices.get(atom2);
        }
        boolean edgeExists = false;
        if (!newNode)
            edgeExists = v1.isNeighbor(v2);
        if (!edgeExists)
            g.addEdge(v1, v2);
        else
            directGraph = true;
    }
    if (directGraph)
        System.err.println("You have loaded a  multigraph. Duplicate edges have been removed!");
    int edgelessVertices = numVertices - vertices.size();
    if (edgelessVertices > 0) {
        System.err.println("There are " + edgelessVertices + " vertices which are not connected to other vertices");
    }
    while (numVertices > vertices.size()) {
        NVertex<GraphInput.InputData> v = vertexPrototype.newOfSameType(new GraphInput.InputData(vertices.size(), "New_Vertex_" + vertices.size()));
        vertices.put(v.data.name, v);
        g.addVertex(v);
    }
    confirmProperIDs(g);
    return g;
}
Also used : Hashtable(java.util.Hashtable) GraphInput(nl.uu.cs.treewidth.input.GraphInput) IBond(org.openscience.cdk.interfaces.IBond) ListGraph(nl.uu.cs.treewidth.ngraph.ListGraph) NVertex(nl.uu.cs.treewidth.ngraph.NVertex) ListVertex(nl.uu.cs.treewidth.ngraph.ListVertex)

Example 10 with IBond

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

IBond (org.openscience.cdk.interfaces.IBond)16 IAtom (org.openscience.cdk.interfaces.IAtom)11 ArrayList (java.util.ArrayList)8 IMolecule (org.openscience.cdk.interfaces.IMolecule)8 HashMap (java.util.HashMap)4 Chain (algorithms.isomorphism.chains.Chain)3 BondMapping (algorithms.isomorphism.chains.Extension.BondMapping)3 MappedChain (algorithms.isomorphism.chains.MappedChain)3 HashSet (java.util.HashSet)3 Residue (model.Residue)3 Molecule (org.openscience.cdk.Molecule)3 Match (algorithms.utils.Match)2 ChemicalObject (model.ChemicalObject)2 Bond (org.openscience.cdk.Bond)2 CDKException (org.openscience.cdk.exception.CDKException)2 Extension (algorithms.isomorphism.chains.Extension)1 Hashtable (java.util.Hashtable)1 List (java.util.List)1 Vector (java.util.Vector)1 Monomer (model.Monomer)1