Skip to content
Permalink
e37ee19ff8
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
130 lines (118 sloc) 3.93 KB
package edu.uconn.tripoint.export;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import quin.network.Location;
import edu.uconn.tripoint.graph.Edge;
import edu.uconn.tripoint.graph.Node;
import edu.uconn.tripoint.pathway.Gene;
import edu.uconn.tripoint.pathway.Pathway;
import edu.uconn.tripoint.pathway.PathwayEdge;
import edu.uconn.tripoint.triangulation.TriangulatedGene;
/**
*
* Exports the pathways into a cytoscape format.
*
*/
public class CytoscapeExport {
/**
*
* @param p
* @param genes
* @param noncoding
* @param outdir
*/
public void exportToCytoscapeJSON(Pathway p, Map<Gene, TriangulatedGene> genes, Map<Gene, List<Location>> noncoding, String outdir, String prefix){
Map<Gene, Integer> geneids = new TreeMap<Gene, Integer>();
int nindex = 0;
Map<Integer, CytoscapeNode> nl = new TreeMap<Integer, CytoscapeNode>();
for(Iterator<Node<Gene,PathwayEdge>> it = p.getNodes().iterator(); it.hasNext();){
Node<Gene,PathwayEdge> next = it.next();
int curnindex = nindex++;
Gene curgene = next.getData();
geneids.put(curgene, curnindex);
int nccount = 0;
if(noncoding.get(curgene) != null){
nccount = noncoding.get(curgene).size();
}
nl.put(curnindex,new PathwayGeneCytoscapeNode(curnindex, p.getId(), genes.get(curgene), nccount));
}
int eindex = 0;
List<CytoscapeEdge> el = new LinkedList<CytoscapeEdge>();
for(Iterator<Edge<Gene,PathwayEdge>> it = p.getEdges().iterator(); it.hasNext();){
Edge<Gene,PathwayEdge> edge = it.next();
Gene src = edge.getSource().getData();
Gene dest = edge.getDestination().getData();
String type = getStringType(edge.getData().getType());
el.add(new PathwayCytoscapeEdge(eindex, nl.get(geneids.get(src)), nl.get(geneids.get(dest)), type));
}
for(Iterator<Gene> git = geneids.keySet().iterator(); git.hasNext();){
Gene next = git.next();
List<Location> l = noncoding.get(next);
if(l != null){
for(Iterator<Location> lit = l.iterator(); lit.hasNext();){
int curnindex = nindex++;
CytoscapeNode n = new NonCodingCytoscapeNode(curnindex, lit.next());
nl.put(curnindex,n);
el.add(new PathwayCytoscapeEdge(eindex++, nl.get(geneids.get(next)), n, "NonCoding"));
}
}
}
String outdir2 = outdir;
if(!outdir.endsWith("/")){
outdir2 += "/";
}
String filename = p.getId();
filename = filename.substring(filename.indexOf(":")+1).replaceAll("[^A-Za-z0-9]", "");
filename = filename.substring(0, Math.min(filename.length(), 16));
try {
writeJSONFile(outdir2+prefix+"_"+filename+".json", p, nl, el);
} catch (IOException e) {
e.printStackTrace();
}
}
private String getStringType(int type){
if(type == PathwayEdge.ACTIVATION){
return "Activation";
}
else if(type == PathwayEdge.INHIBITION){
return "Inhibition";
}
else{
return "Association";
}
}
private void writeJSONFile(String outfile, Pathway p, Map<Integer, CytoscapeNode> nodes, List<CytoscapeEdge> edges) throws IOException{
BufferedWriter bw = new BufferedWriter(new FileWriter(outfile));
bw.write("{\n");
bw.write("\"data\": {\"name\": \""+p.getId()+"\"},\n");
bw.write("\"elements\" : {\n");
bw.write("\"nodes\" : [");
Iterator<CytoscapeNode> it = nodes.values().iterator();
if(it.hasNext()){
bw.write(it.next().toJSON()+"\n");
}
while(it.hasNext()){
bw.write(","+it.next().toJSON()+"\n");
}
bw.write("],\n");
bw.write("\"edges\" : [\n");
Iterator<CytoscapeEdge> ite = edges.iterator();
if(ite.hasNext()){
bw.write(ite.next().toJSON()+"\n");
}
while(ite.hasNext()){
bw.write(","+ite.next().toJSON()+"\n");
}
bw.write("]\n");
bw.write("}\n");
bw.write("}\n");
bw.flush();
bw.close();
}
}