Search in sources :

Example 6 with IAtom

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

the class BonbMatchingTests method setUp.

@Before
public void setUp() throws Exception {
    // cNH
    IAtom a2 = new Atom("C");
    a2.setImplicitHydrogenCount(0);
    a2.setFlag(CDKConstants.ISAROMATIC, true);
    IAtom a1 = new Atom("N");
    a1.setImplicitHydrogenCount(1);
    this.bond1 = new Bond(a1, a2, Order.DOUBLE);
}
Also used : Bond(org.openscience.cdk.Bond) IAtom(org.openscience.cdk.interfaces.IAtom) Atom(org.openscience.cdk.Atom) IAtom(org.openscience.cdk.interfaces.IAtom) Before(org.junit.Before)

Example 7 with IAtom

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

the class ResidueJsonLoader method objectFromJson.

@Override
protected Family objectFromJson(JSONObject obj) {
    Residue res = new Residue((String) obj.get("mono"), (String) obj.get("smarts"), true);
    res.setIdx(((Number) obj.get("id")).intValue());
    JSONArray array = (JSONArray) obj.get("links");
    for (Object o : array) {
        JSONObject jso = (JSONObject) o;
        String name = (String) jso.get("name");
        Rule rule = null;
        try {
            rule = this.rules.getObject(name);
        } catch (NullPointerException e) {
            System.err.println("Unknown link " + name);
        }
        int idx = ((Number) jso.get("atom")).intValue();
        IAtom ia = res.getMolecule().getAtom(idx);
        res.addLink(ia, rule);
    }
    // Family construction
    Family fam = new Family();
    try {
        for (String name : ((String) obj.get("family")).split("€")) {
            Monomer m = this.monos.getObject(name);
            fam.addMonomer(m);
        }
    } catch (NullPointerException e) {
        System.err.println("Unloaded residue " + res.getMonoName());
    }
    fam.addResidue(res);
    for (Object jso : (JSONArray) obj.get("depandances")) {
        int idx = ((Number) jso).intValue();
        fam.addDependance(idx, new Integer(res.getId()));
    }
    return fam;
}
Also used : JSONArray(org.json.simple.JSONArray) JSONObject(org.json.simple.JSONObject) Residue(model.Residue) Family(model.Family) JSONObject(org.json.simple.JSONObject) Rule(model.Rule) Monomer(model.Monomer) IAtom(org.openscience.cdk.interfaces.IAtom)

Example 8 with IAtom

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

the class MonomersSerialization method deserializeObject.

private void deserializeObject(Monomer mono, ObjectInputStream ois) {
    try {
        int nbAtoms = ois.readInt();
        if (nbAtoms != mono.getMolecule().getAtomCount()) {
            System.err.println("Bad serialized value for atom count in " + mono.getCode());
        }
        for (IAtom a : mono.getMolecule().atoms()) {
            Point2d p = new Point2d(ois.readDouble(), ois.readDouble());
            a.setPoint2d(p);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : Point2d(javax.vecmath.Point2d) IOException(java.io.IOException) IAtom(org.openscience.cdk.interfaces.IAtom)

Example 9 with IAtom

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

the class MySMILES method createDFS.

private void createDFS(DFS dfs, AtomNode current, AtomNode from) {
    current.state = State.CHECK;
    dfs.path.add(current);
    List<AtomNode> toExplore = new ArrayList<MySMILES.AtomNode>();
    // For all neighbors save enighbors to explore
    for (IAtom a : dfs.mol.getConnectedAtomsList(current.atom)) {
        AtomNode nei = dfs.nodes.get(a);
        if (from != null && nei.atom == from.atom)
            continue;
        switch(nei.state) {
            case FREE:
                toExplore.add(nei);
                nei.state = State.TOEXPLORE;
            case TOEXPLORE:
                // TODO better smiles with less () and cycles first
                break;
            default:
                if (nei.rings == null)
                    nei.rings = new ArrayList<Integer>();
                nei.rings.add(dfs.ringNumber);
                if (current.rings == null)
                    current.rings = new ArrayList<Integer>();
                current.rings.add(dfs.ringNumber++);
                break;
        }
    }
    if (toExplore.size() == 0)
        return;
    // Sort neighbors to explore.
    List<List<AtomNode>> sortedNeighbors = dfs.sort(toExplore, current, dfs.mol);
    for (List<AtomNode> similar : sortedNeighbors) {
        HashMap<List<AtomNode>, DFS> clones = new HashMap<>();
        // Clone if there are most than one possibility
        List<List<AtomNode>> alternatives = this.getAllAlternatives(similar);
        for (List<AtomNode> alt : alternatives) clones.put(alt, dfs.clone());
        // Explore all alternatives for equivalent nodes
        DFS bestClone = null;
        for (List<AtomNode> alternative : alternatives) {
            DFS clone = clones.get(alternative);
            for (AtomNode node : alternative) {
                node = clone.nodes.get(node.atom);
                // To avoid 2 usages of same node if there are rings
                if (node.state != State.TOEXPLORE) {
                    clones.remove(node);
                    continue;
                }
                clone.path.add(new TextNode("("));
                this.createDFS(clone, node, current);
                clone.path.add(new TextNode(")"));
            }
            // Select best clone
            if (bestClone == null)
                bestClone = clone;
            else if (bestClone.toString().compareTo(clone.toString()) > 0)
                bestClone = clone;
        }
        // Transformation of the current DFS with the best clone extensions.
        for (int idx = dfs.path.size(); idx < bestClone.path.size(); idx++) {
            Node node = bestClone.path.get(idx);
            if (node instanceof AtomNode) {
                AtomNode cloneNode = (AtomNode) node;
                IAtom atom = cloneNode.atom;
                AtomNode an = dfs.nodes.get(atom);
                an.state = cloneNode.state;
                an.rings = cloneNode.rings;
                an.sortLink = cloneNode.sortLink;
                dfs.path.add(an);
            } else {
                dfs.path.add(new TextNode(((TextNode) node).txt));
            }
        }
        dfs.ringNumber = bestClone.ringNumber;
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) IAtom(org.openscience.cdk.interfaces.IAtom)

Example 10 with IAtom

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

the class IsomorphismTests method initMappingTest.

@Test
public void initMappingTest() {
    List<MappedChain> mbs = Isomorphism.searchFromPreviousMapping(this.mb0, this.ext1, MatchingType.STRONG);
    if (mbs.size() != 2)
        fail("2 matches needed");
    boolean isGood = true;
    for (MappedChain mb : mbs) {
        IMolecule mol = mb.getChemObject().getMolecule();
        IAtom a1 = (mol.getAtom(mb.getAtomsMapping().get(0)));
        IAtom a2 = (mol.getAtom(mb.getAtomsMapping().get(1)));
        if (!((a1.getSymbol().equals("C") && a2.getSymbol().equals("S")) || (a1.getSymbol().equals("S") && a2.getSymbol().equals("C"))))
            isGood = false;
    }
    Assert.assertTrue(isGood);
}
Also used : MappedChain(algorithms.isomorphism.chains.MappedChain) IMolecule(org.openscience.cdk.interfaces.IMolecule) IAtom(org.openscience.cdk.interfaces.IAtom) Test(org.junit.Test)

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