Search in sources :

Example 11 with Monomer

use of model.Monomer in project Smiles2Monomers by yoann-dufresne.

the class MonomersJsonLoader method objectFromJson.

@Override
protected Monomer objectFromJson(JSONObject jso) {
    String name = jso.containsKey("name") ? (String) jso.get("name") : jso.containsKey("code") ? (String) jso.get("code") : null;
    String desc = jso.containsKey("desc") ? (String) jso.get("desc") : jso.containsKey("code") ? (String) jso.get("code") : null;
    if ("".equals((String) jso.get("smiles"))) {
        System.err.println("No smiles for " + ((String) jso.get("name")));
        return null;
    } else if (((String) jso.get("smiles")).contains(".")) {
        System.err.println("The smiles for " + ((String) jso.get("name")) + " contains character '.'");
        System.err.println("The '.' means that the smiles is composed of more than one molecule.");
        System.err.println("Please split the smiles in two distinct smiles.");
        return null;
    }
    Monomer mono = new Monomer(name, desc, (String) jso.get("smiles"));
    if (mono.getMolecule().getAtomCount() < 2)
        return null;
    // System.out.println(mono);
    return mono;
}
Also used : Monomer(model.Monomer)

Example 12 with Monomer

use of model.Monomer in project Smiles2Monomers by yoann-dufresne.

the class PolymersJsonLoader method objectFromJson.

@SuppressWarnings("unchecked")
@Override
protected Polymer objectFromJson(JSONObject obj) {
    if ("".equals((String) obj.get("smiles"))) {
        System.err.println("No smiles for " + ((String) obj.get("name")));
        return null;
    } else if (((String) obj.get("smiles")).contains(".")) {
        System.err.println("The smiles for " + ((String) obj.get("name")) + " contains character '.'");
        System.err.println("The '.' means that the smiles is composed of more than one molecule.");
        System.err.println("Please split the smiles in two distinct smiles.");
        return null;
    }
    // --- Parse the graph ---
    JSONArray verticies;
    JSONArray edges;
    if (obj.containsKey("graph")) {
        JSONObject graph = (JSONObject) obj.get("graph");
        // Parse the s2m graph format
        verticies = (JSONArray) graph.get("V");
        edges = (JSONArray) graph.get("E");
    } else if (obj.containsKey("structure")) {
        verticies = new JSONArray();
        edges = new JSONArray();
        // Parse the Norine graph format
        String text = (String) ((JSONObject) obj.get("structure")).get("graph");
        String[] split = text.split("@");
        // The verticies
        verticies = new JSONArray();
        for (String val : split[0].split(",")) verticies.add(val);
        // The edges
        edges = new JSONArray();
        for (int idx = 1; idx < split.length; idx++) {
            String[] links = split[idx].split(",");
            for (String val : links) {
                int link_to = new Integer(val);
                if (idx - 1 < link_to) {
                    JSONArray edge = new JSONArray();
                    edge.add(idx - 1);
                    edge.add(link_to);
                    edges.add(edge);
                }
            }
        }
    } else {
        verticies = new JSONArray();
        edges = new JSONArray();
    }
    // --- Parse the monomer list ---
    Monomer[] monomers = new Monomer[verticies.size()];
    for (int i = 0; i < monomers.length; i++) {
        String name = (String) verticies.get(i);
        try {
            monomers[i] = this.monos.contains(name) ? this.monos.getObject(name) : new Monomer(name, "", "");
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }
    // Create the graph in memory
    MonomerGraph g = new MonomerGraph(monomers);
    for (Object o : edges) {
        JSONArray link = (JSONArray) o;
        g.createLink(((Number) link.get(0)).intValue(), ((Number) link.get(1)).intValue());
    }
    int id = 0;
    if (obj.get("id") instanceof String) {
        String sid = (String) obj.get("id");
        id = sid.startsWith("NOR") ? new Integer(sid.substring(3)) : sid.hashCode();
    } else
        id = ((Number) obj.get("id")).intValue();
    Polymer pep = new Polymer(id, (String) obj.get("name"), (String) obj.get("smiles"), monomers);
    pep.setGraph(g);
    return pep;
}
Also used : JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) MonomerGraph(model.graph.MonomerGraph) Polymer(model.Polymer) JSONObject(org.json.simple.JSONObject) Monomer(model.Monomer)

Example 13 with Monomer

use of model.Monomer in project Smiles2Monomers by yoann-dufresne.

the class PolymersJsonLoader method graphToJson.

@SuppressWarnings("unchecked")
public JSONObject graphToJson(MonomerGraph g) {
    JSONObject jso = new JSONObject();
    JSONArray nodes = new JSONArray();
    for (Monomer m : g.nodes) {
        nodes.add(m.getCode());
    }
    jso.put("V", nodes);
    JSONArray edges = new JSONArray();
    List<MonomerLinks> added = new ArrayList<>(g.links.size());
    for (MonomerLinks ml : g.links) {
        boolean contains = false;
        for (MonomerLinks old : added) if ((old.mono1 == ml.mono1 && old.mono2 == ml.mono2) || (old.mono2 == ml.mono1 && old.mono1 == ml.mono2)) {
            contains = true;
            break;
        }
        if (!contains) {
            added.add(ml);
            JSONArray edge = new JSONArray();
            edge.add(ml.mono1);
            edge.add(ml.mono2);
            edges.add(edge);
        }
    }
    jso.put("E", edges);
    return jso;
}
Also used : MonomerLinks(model.graph.MonomerGraph.MonomerLinks) JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) ArrayList(java.util.ArrayList) Monomer(model.Monomer)

Example 14 with Monomer

use of model.Monomer in project Smiles2Monomers by yoann-dufresne.

the class ImagesGeneration method generateMonomerImages.

public void generateMonomerImages(File imgsDirectory, MonomersDB monoDB) {
    // Monomer images directory
    File monoImgsDirectory = new File(imgsDirectory.getPath() + "/monomers");
    if (!monoImgsDirectory.exists())
        monoImgsDirectory.mkdir();
    // Monomers imgages generation.
    PictureGenerator pg = new PictureGenerator();
    for (Monomer m : monoDB.getObjects()) {
        File monoImg = new File(monoImgsDirectory.getPath() + "/" + m.getName() + ".png");
        if (!monoImg.exists())
            pg.createPNG(m.getMolecule(), monoImg);
    }
}
Also used : Monomer(model.Monomer) File(java.io.File)

Example 15 with Monomer

use of model.Monomer in project Smiles2Monomers by yoann-dufresne.

the class ContractedGraph method toMonomerGraph.

/**
 * Transform this contracted graph into a classical monomer graph.
 * @param fams The family database needed to always refer the same monomer of a family.
 * @return The monomer graph
 */
public MonomerGraph toMonomerGraph(FamilyDB fams) {
    // Transformation to a monomer graph
    List<Vertex> verticiesOrder = new ArrayList<>();
    List<Monomer> monos = new ArrayList<>();
    List<Residue> residues = new ArrayList<>();
    for (Object o : this.vertexSet()) {
        Vertex v = (Vertex) o;
        verticiesOrder.add(v);
        Residue res = v.res;
        if (res != null) {
            Family fam = null;
            try {
                fam = fams.getObject(res.getMonoName());
            } catch (NullPointerException e) {
                e.printStackTrace();
            }
            monos.add(fam.getPrincipalMonomer());
            residues.add(res);
        } else {
            monos.add(null);
            residues.add(null);
        }
    }
    Monomer[] array = new Monomer[monos.size()];
    for (int i = 0; i < monos.size(); i++) array[i] = monos.get(i);
    Residue[] resArray = new Residue[monos.size()];
    for (int i = 0; i < monos.size(); i++) resArray[i] = residues.get(i);
    MonomerGraph monoGraph = new MonomerGraph(array, resArray);
    for (Object o : this.edgeSet()) {
        LabeledEdge e = (LabeledEdge) o;
        int mono1 = verticiesOrder.indexOf((Vertex) e.getSource());
        int mono2 = verticiesOrder.indexOf((Vertex) e.getTarget());
        monoGraph.createLink(mono1, mono2, e.sourceLabel, e.targetLabel);
    }
    return monoGraph;
}
Also used : ArrayList(java.util.ArrayList) Residue(model.Residue) Family(model.Family) Monomer(model.Monomer)

Aggregations

Monomer (model.Monomer)16 Residue (model.Residue)6 ArrayList (java.util.ArrayList)5 Family (model.Family)5 HashMap (java.util.HashMap)4 JSONArray (org.json.simple.JSONArray)4 JSONObject (org.json.simple.JSONObject)4 Polymer (model.Polymer)3 InvalidSmilesException (org.openscience.cdk.exception.InvalidSmilesException)3 MonomersDB (db.MonomersDB)2 Rule (model.Rule)2 MonomerGraph (model.graph.MonomerGraph)2 MonomerLinks (model.graph.MonomerGraph.MonomerLinks)2 Before (org.junit.Before)2 IAtom (org.openscience.cdk.interfaces.IAtom)2 IMolecule (org.openscience.cdk.interfaces.IMolecule)2 ResidueCreator (algorithms.ResidueCreator)1 Chain (algorithms.isomorphism.chains.Chain)1 Extension (algorithms.isomorphism.chains.Extension)1 MappedChain (algorithms.isomorphism.chains.MappedChain)1