Search in sources :

Example 1 with MonomerGraph

use of model.graph.MonomerGraph in project Smiles2Monomers by yoann-dufresne.

the class CoveragesJsonLoader method getJSONGraph.

@SuppressWarnings("unchecked")
private JSONObject getJSONGraph(Coverage cov) {
    ContractedGraph cg = new ContractedGraph(cov);
    MonomerGraph mg = cg.toMonomerGraph(families);
    JSONObject graph = new JSONObject();
    // Monomers
    JSONArray monos = new JSONArray();
    for (Monomer mono : mg.nodes) if (mono != null)
        monos.add(mono.getName());
    else
        monos.add("?");
    graph.put("monos", monos);
    // Residues (equivalent to monomers)
    JSONArray residues = new JSONArray();
    for (Residue res : mg.residues) if (res != null)
        residues.add(res.getId());
    else
        residues.add("?");
    graph.put("residues", residues);
    // Links
    JSONArray links = new JSONArray();
    for (MonomerLinks ml : mg.links) {
        JSONObject link = new JSONObject();
        JSONArray idxs = new JSONArray();
        idxs.add(ml.mono1);
        idxs.add(ml.mono2);
        link.put("idxs", idxs);
        JSONArray type = new JSONArray();
        type.add(ml.label1);
        type.add(ml.label2);
        link.put("types", type);
        links.add(link);
    }
    graph.put("links", links);
    return graph;
}
Also used : MonomerLinks(model.graph.MonomerGraph.MonomerLinks) JSONObject(org.json.simple.JSONObject) Residue(model.Residue) ContractedGraph(model.graph.ContractedGraph) JSONArray(org.json.simple.JSONArray) MonomerGraph(model.graph.MonomerGraph) Monomer(model.Monomer)

Example 2 with MonomerGraph

use of model.graph.MonomerGraph in project Smiles2Monomers by yoann-dufresne.

the class Monomer method getGraph.

@Override
public MonomerGraph getGraph() {
    Monomer[] nodes = { this };
    MonomerGraph g = new MonomerGraph(nodes);
    return g;
}
Also used : MonomerGraph(model.graph.MonomerGraph)

Example 3 with MonomerGraph

use of model.graph.MonomerGraph 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)

Aggregations

MonomerGraph (model.graph.MonomerGraph)3 Monomer (model.Monomer)2 JSONArray (org.json.simple.JSONArray)2 JSONObject (org.json.simple.JSONObject)2 Polymer (model.Polymer)1 Residue (model.Residue)1 ContractedGraph (model.graph.ContractedGraph)1 MonomerLinks (model.graph.MonomerGraph.MonomerLinks)1