Search in sources :

Example 1 with MonomerLinks

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

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

Aggregations

Monomer (model.Monomer)2 MonomerLinks (model.graph.MonomerGraph.MonomerLinks)2 JSONArray (org.json.simple.JSONArray)2 JSONObject (org.json.simple.JSONObject)2 ArrayList (java.util.ArrayList)1 Residue (model.Residue)1 ContractedGraph (model.graph.ContractedGraph)1 MonomerGraph (model.graph.MonomerGraph)1