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;
}
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;
}
Aggregations