From e37ee19ff866a184881ee1b329e728804275ea75 Mon Sep 17 00:00:00 2001 From: ajt06004 Date: Fri, 19 Jan 2018 12:56:22 -0500 Subject: [PATCH] Initial Commit --- .classpath | 14 + .gitignore | 1 + .project | 23 + .settings/org.eclipse.jdt.core.prefs | 7 + ....eclipse.wst.common.project.facet.core.xml | 4 + .../uconn/tripoint/export/CytoscapeEdge.java | 29 + .../tripoint/export/CytoscapeExport.java | 130 +++++ .../uconn/tripoint/export/CytoscapeNode.java | 18 + .../export/NonCodingCytoscapeNode.java | 25 + .../export/NonCodingTargetExport.java | 49 ++ .../tripoint/export/PathwayCytoscapeEdge.java | 25 + .../export/PathwayGeneCytoscapeNode.java | 46 ++ .../tripoint/export/TriangulationExport.java | 96 ++++ src/edu/uconn/tripoint/graph/Edge.java | 87 +++ src/edu/uconn/tripoint/graph/Node.java | 68 +++ .../ChromatinInteractionNetwork.java | 97 ++++ .../integration/NonCodingTargets.java | 20 + .../integration/PoissonNonCoding.java | 67 +++ .../uconn/tripoint/integration/Proximal.java | 68 +++ src/edu/uconn/tripoint/integration/Util.java | 161 ++++++ src/edu/uconn/tripoint/pathway/Gene.java | 86 +++ src/edu/uconn/tripoint/pathway/Pathway.java | 205 +++++++ .../uconn/tripoint/pathway/PathwayEdge.java | 91 +++ .../uconn/tripoint/pathway/PathwayHelper.java | 78 +++ .../pathwayimport/GraphiteImport.java | 174 ++++++ .../tripoint/triangulation/FDRCorrection.java | 17 + .../triangulation/ProgramParameters.java | 234 ++++++++ src/edu/uconn/tripoint/triangulation/Run.java | 183 ++++++ .../triangulation/TriangulatedGene.java | 505 +++++++++++++++++ .../tripoint/triangulation/Triangulation.java | 526 ++++++++++++++++++ 30 files changed, 3134 insertions(+) create mode 100644 .classpath create mode 100644 .gitignore create mode 100644 .project create mode 100644 .settings/org.eclipse.jdt.core.prefs create mode 100644 .settings/org.eclipse.wst.common.project.facet.core.xml create mode 100644 src/edu/uconn/tripoint/export/CytoscapeEdge.java create mode 100644 src/edu/uconn/tripoint/export/CytoscapeExport.java create mode 100644 src/edu/uconn/tripoint/export/CytoscapeNode.java create mode 100644 src/edu/uconn/tripoint/export/NonCodingCytoscapeNode.java create mode 100644 src/edu/uconn/tripoint/export/NonCodingTargetExport.java create mode 100644 src/edu/uconn/tripoint/export/PathwayCytoscapeEdge.java create mode 100644 src/edu/uconn/tripoint/export/PathwayGeneCytoscapeNode.java create mode 100644 src/edu/uconn/tripoint/export/TriangulationExport.java create mode 100644 src/edu/uconn/tripoint/graph/Edge.java create mode 100644 src/edu/uconn/tripoint/graph/Node.java create mode 100644 src/edu/uconn/tripoint/integration/ChromatinInteractionNetwork.java create mode 100644 src/edu/uconn/tripoint/integration/NonCodingTargets.java create mode 100644 src/edu/uconn/tripoint/integration/PoissonNonCoding.java create mode 100644 src/edu/uconn/tripoint/integration/Proximal.java create mode 100644 src/edu/uconn/tripoint/integration/Util.java create mode 100644 src/edu/uconn/tripoint/pathway/Gene.java create mode 100644 src/edu/uconn/tripoint/pathway/Pathway.java create mode 100644 src/edu/uconn/tripoint/pathway/PathwayEdge.java create mode 100644 src/edu/uconn/tripoint/pathway/PathwayHelper.java create mode 100644 src/edu/uconn/tripoint/pathwayimport/GraphiteImport.java create mode 100644 src/edu/uconn/tripoint/triangulation/FDRCorrection.java create mode 100644 src/edu/uconn/tripoint/triangulation/ProgramParameters.java create mode 100644 src/edu/uconn/tripoint/triangulation/Run.java create mode 100644 src/edu/uconn/tripoint/triangulation/TriangulatedGene.java create mode 100644 src/edu/uconn/tripoint/triangulation/Triangulation.java diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..96e29a5 --- /dev/null +++ b/.classpath @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..84c048a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/build/ diff --git a/.project b/.project new file mode 100644 index 0000000..27a5884 --- /dev/null +++ b/.project @@ -0,0 +1,23 @@ + + + TriPOINT + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.wst.common.project.facet.core.builder + + + + + + org.eclipse.wst.common.project.facet.core.nature + org.eclipse.jdt.core.javanature + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..295926d --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,7 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/.settings/org.eclipse.wst.common.project.facet.core.xml b/.settings/org.eclipse.wst.common.project.facet.core.xml new file mode 100644 index 0000000..017b6f4 --- /dev/null +++ b/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -0,0 +1,4 @@ + + + + diff --git a/src/edu/uconn/tripoint/export/CytoscapeEdge.java b/src/edu/uconn/tripoint/export/CytoscapeEdge.java new file mode 100644 index 0000000..5559fe0 --- /dev/null +++ b/src/edu/uconn/tripoint/export/CytoscapeEdge.java @@ -0,0 +1,29 @@ +package edu.uconn.tripoint.export; + +public abstract class CytoscapeEdge { + + private int _id; + private CytoscapeNode _n1, _n2; + + public CytoscapeEdge(int id, CytoscapeNode n1, CytoscapeNode n2){ + _id = id; + _n1 = n1; + _n2 = n2; + } + + public int getId(){ + return _id; + } + + public CytoscapeNode getN1(){ + return _n1; + } + + public CytoscapeNode getN2(){ + return _n2; + } + + + public abstract String toJSON(); + +} diff --git a/src/edu/uconn/tripoint/export/CytoscapeExport.java b/src/edu/uconn/tripoint/export/CytoscapeExport.java new file mode 100644 index 0000000..8d1d572 --- /dev/null +++ b/src/edu/uconn/tripoint/export/CytoscapeExport.java @@ -0,0 +1,130 @@ +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 genes, Map> noncoding, String outdir, String prefix){ + Map geneids = new TreeMap(); + int nindex = 0; + Map nl = new TreeMap(); + for(Iterator> it = p.getNodes().iterator(); it.hasNext();){ + Node 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 el = new LinkedList(); + for(Iterator> it = p.getEdges().iterator(); it.hasNext();){ + Edge 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 git = geneids.keySet().iterator(); git.hasNext();){ + Gene next = git.next(); + List l = noncoding.get(next); + if(l != null){ + for(Iterator 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 nodes, List 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 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 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(); + } + +} diff --git a/src/edu/uconn/tripoint/export/CytoscapeNode.java b/src/edu/uconn/tripoint/export/CytoscapeNode.java new file mode 100644 index 0000000..15483ba --- /dev/null +++ b/src/edu/uconn/tripoint/export/CytoscapeNode.java @@ -0,0 +1,18 @@ +package edu.uconn.tripoint.export; + +public abstract class CytoscapeNode { + + private int _id; + + public CytoscapeNode(int id){ + _id = id; + } + + public int getId(){ + return _id; + } + + + public abstract String toJSON(); + +} diff --git a/src/edu/uconn/tripoint/export/NonCodingCytoscapeNode.java b/src/edu/uconn/tripoint/export/NonCodingCytoscapeNode.java new file mode 100644 index 0000000..5b64625 --- /dev/null +++ b/src/edu/uconn/tripoint/export/NonCodingCytoscapeNode.java @@ -0,0 +1,25 @@ +package edu.uconn.tripoint.export; + +import quin.network.Location; + +public class NonCodingCytoscapeNode extends CytoscapeNode{ + + public Location _l; + public NonCodingCytoscapeNode(int id, Location l) { + super(id); + _l = l; + } + + @Override + public String toJSON(){ + StringBuilder sb = new StringBuilder(); + sb.append("{\n"); + sb.append("\"data\" : {\n"); + sb.append("\"id\" : \""+getId()+"\",\n"); + sb.append("\"type\" : \"NonCoding Target\",\n"); + sb.append("\"name\" : \""+_l.getChr()+":"+_l.getStart()+"-"+_l.getEnd()+"\"\n"); + sb.append("}\n"); + sb.append("}\n"); + return sb.toString(); + } +} diff --git a/src/edu/uconn/tripoint/export/NonCodingTargetExport.java b/src/edu/uconn/tripoint/export/NonCodingTargetExport.java new file mode 100644 index 0000000..7842cae --- /dev/null +++ b/src/edu/uconn/tripoint/export/NonCodingTargetExport.java @@ -0,0 +1,49 @@ +package edu.uconn.tripoint.export; + +import java.io.BufferedWriter; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import quin.network.Location; +import edu.uconn.tripoint.pathway.Gene; +import edu.uconn.tripoint.pathway.Pathway; +import edu.uconn.tripoint.triangulation.TriangulatedGene; + +public class NonCodingTargetExport { + + public void ExportNonCodingTargets(Pathway globalpathway, Pathway[] pathways, Collection genes, Map> noncoding, String outdir, String prefix) throws IOException{ + String outdir2 = outdir; + if(!outdir.endsWith("/")){ + outdir2 += "/"; + } + + BufferedWriter bw = new BufferedWriter(new FileWriter(outdir2+prefix+"_noncoding.txt")); + bw.write("Gene ID"+"\t"+ + "Chr"+"\t"+"Start"+"\t"+"End"+"\n"); + for(Iterator it = genes.iterator(); it.hasNext();){ + TriangulatedGene next = it.next(); + writeLine(bw,next, noncoding); + } + + bw.flush(); + bw.close(); + } + + private void writeLine(BufferedWriter bw, TriangulatedGene g, Map> noncoding) throws IOException{ + Gene gene = g.getGene(); + List nc = noncoding.get(gene); + if(nc.size() > 0){ + String id = gene.getId(); + for(Iterator it = nc.iterator(); it.hasNext();){ + Location next = it.next(); + bw.write(id+"\t"+ + next.getChr()+"\t"+next.getStart()+"\t"+next.getEnd()+"\n"); + } + + } + } +} diff --git a/src/edu/uconn/tripoint/export/PathwayCytoscapeEdge.java b/src/edu/uconn/tripoint/export/PathwayCytoscapeEdge.java new file mode 100644 index 0000000..ad892f4 --- /dev/null +++ b/src/edu/uconn/tripoint/export/PathwayCytoscapeEdge.java @@ -0,0 +1,25 @@ +package edu.uconn.tripoint.export; + +public class PathwayCytoscapeEdge extends CytoscapeEdge{ + + private String _type; + + public PathwayCytoscapeEdge(int id, CytoscapeNode n1, CytoscapeNode n2, String type){ + super(id, n1, n2); + _type = type; + } + + public String toJSON(){ + StringBuilder sb = new StringBuilder(); + sb.append("{\n"); + sb.append("\"data\" : {\n"); + sb.append("\"id\" : \""+getId()+"\",\n"); + sb.append("\"source\" : \""+getN1().getId()+"\",\n"); + sb.append("\"target\" : \""+getN2().getId()+"\",\n"); + sb.append("\"type\" : \""+_type+"\"\n"); + sb.append("}\n"); + sb.append("}\n"); + return sb.toString(); + } + +} diff --git a/src/edu/uconn/tripoint/export/PathwayGeneCytoscapeNode.java b/src/edu/uconn/tripoint/export/PathwayGeneCytoscapeNode.java new file mode 100644 index 0000000..05ce374 --- /dev/null +++ b/src/edu/uconn/tripoint/export/PathwayGeneCytoscapeNode.java @@ -0,0 +1,46 @@ +package edu.uconn.tripoint.export; + +import edu.uconn.tripoint.triangulation.TriangulatedGene; + +public class PathwayGeneCytoscapeNode extends CytoscapeNode { + + private String _pathway; + private TriangulatedGene _tg; + private int _noncodingcount; + + public PathwayGeneCytoscapeNode(int id, String pathway, TriangulatedGene tg, int nccount){ + super(id); + _pathway = pathway; + _tg = tg; + _noncodingcount = nccount; + } + + @Override + public String toJSON(){ + StringBuilder sb = new StringBuilder(); + sb.append("{\n"); + sb.append("\"data\" : {\n"); + sb.append("\"id\" : \""+getId()+"\",\n"); + sb.append("\"type\" : \"Pathway Gene\",\n"); + sb.append("\"name\" : \""+_tg.getGene().getId()+"\",\n"); + sb.append("\"pathway\" : \""+_pathway+"\",\n"); + sb.append("\"expression\" : "+_tg.getGene().getValue()+",\n"); + sb.append("\"inconsistency\" : "+_tg.getInconsistencyScore(_pathway)+",\n"); + sb.append("\"inconsistency_pval\" : "+_tg.getInconsistencyPValue(_pathway)+",\n"); + sb.append("\"inconsistency_qval\" : "+_tg.getInconsistencyFDR(_pathway)+",\n"); + sb.append("\"support\" : "+_tg.getSupportScore(_pathway)+",\n"); + sb.append("\"support_pval\" : "+_tg.getSupportPValue(_pathway)+",\n"); + sb.append("\"support_qval\" : "+_tg.getSupportFDR(_pathway)+",\n"); + sb.append("\"consistency\" : "+_tg.getConsistencyScore(_pathway)+",\n"); + sb.append("\"consistency_pval\" : "+_tg.getConsistencyPValue(_pathway)+",\n"); + sb.append("\"consistency_qval\" : "+_tg.getConsistencyFDR(_pathway)+",\n"); + sb.append("\"impact\" : "+_tg.getImpactScore(_pathway)+",\n"); + sb.append("\"impact_pval\" : "+_tg.getImpactPValue(_pathway)+",\n"); + sb.append("\"impact_qval\" : "+_tg.getImpactFDR(_pathway)+",\n"); + sb.append("\"noncoding_count\" : "+_noncodingcount+"\n"); + sb.append("}\n"); + sb.append("}\n"); + return sb.toString(); + } + +} diff --git a/src/edu/uconn/tripoint/export/TriangulationExport.java b/src/edu/uconn/tripoint/export/TriangulationExport.java new file mode 100644 index 0000000..8c05ea4 --- /dev/null +++ b/src/edu/uconn/tripoint/export/TriangulationExport.java @@ -0,0 +1,96 @@ +package edu.uconn.tripoint.export; + +import java.io.BufferedWriter; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import quin.network.Location; +import edu.uconn.tripoint.pathway.Gene; +import edu.uconn.tripoint.pathway.Pathway; +import edu.uconn.tripoint.triangulation.TriangulatedGene; + + +/** + * Exports the Triangulation scores in a tab-delimited text format. + */ +public class TriangulationExport { + + + public void ExportTriangulation(Map> genepathways, Collection genes, Map> noncoding, Map ncpval, String outdir, String prefix) throws IOException{ + String outdir2 = outdir; + if(!outdir.endsWith("/")){ + outdir2 += "/"; + } + + BufferedWriter bw = new BufferedWriter(new FileWriter(outdir2+prefix+"_triangulation.txt")); + bw.write("Gene ID"+"\t"+"Pathway ID"+"\t"+"Expression Value"+"\t"+ + "Inconsistency Score"+"\t"+"Inconsistency p-value"+"\t"+"Inconsistency q-value"+"\t"+ + "Support Score"+"\t"+"Support p-value"+"\t"+"Support q-value"+"\t"+ + "Consistency Score"+"\t"+"Consistency p-value"+"\t"+"Consistency q-value"+"\t"+ + "Impact Score"+"\t"+"Impact p-value"+"\t"+"Impact q-value"+"\t"+ + "Supporting Triangulation Score"+"\t"+"Supporting Triangulation p-value"+"\t"+"Supporting Triangulation q-value"+"\t"+ + "Inconsistent Triangulation Score"+"\t"+"Inconsistent Triangulation p-value"+"\t"+"Inconsistent Triangulation q-value"+"\t"+ + "Number of NonCoding Targets"+"\t"+"NonCoding Poisson p-value"+"\n"); + for(Iterator it = genes.iterator(); it.hasNext();){ + TriangulatedGene next = it.next(); + for(Iterator it2 = genepathways.get(next.getGene()).iterator(); it2.hasNext();){ + Pathway p = it2.next(); + writeLine(bw,p,next,noncoding, ncpval); + } + } + + bw.flush(); + bw.close(); + } + + private void writeLine(BufferedWriter bw, Pathway p, TriangulatedGene g, Map> noncoding, Map ncpval) throws IOException{ + Gene gene = g.getGene(); + + String id = gene.getId(); + String pid = p.getId(); + double value = gene.getValue(); + + double is = g.getInconsistencyScore(pid); + double ip = g.getInconsistencyPValue(pid); + double ifdr = g.getInconsistencyFDR(pid); + + double ss = g.getSupportScore(pid); + double sp = g.getSupportPValue(pid); + double sfdr = g.getSupportFDR(pid); + + double cs = g.getConsistencyScore(pid); + double cp = g.getConsistencyPValue(pid); + double cfdr = g.getConsistencyFDR(pid); + + double ims = g.getImpactScore(pid); + double imp = g.getImpactPValue(pid); + double imfdr = g.getImpactFDR(pid); + + double stris = g.getSupportTriangulationScore(pid); + double strip = g.getSupportTriangulationPValue(pid); + double strifdr = g.getSupportingTriangulationFDR(pid); + + double itris = g.getInconsistentTriangulationScore(pid); + double itrip = g.getInconsistentTriangulationPValue(pid); + double itrifdr = g.getInconsistentTriangulationFDR(pid); + + List l = noncoding.get(gene); + int nccount = l != null ? l.size() : 0; + Double d = ncpval.get(gene); + double ncp = d != null ? d : 1; + + bw.write(id+"\t"+pid+"\t"+value+"\t"+ + is+"\t"+ip+"\t"+ifdr+"\t"+ + ss+"\t"+sp+"\t"+sfdr+"\t"+ + cs+"\t"+cp+"\t"+cfdr+"\t"+ + ims+"\t"+imp+"\t"+imfdr+"\t"+ + stris+"\t"+strip+"\t"+strifdr+"\t"+ + itris+"\t"+itrip+"\t"+itrifdr+"\t"+ + nccount+"\t"+ncp+"\n"); + } + +} diff --git a/src/edu/uconn/tripoint/graph/Edge.java b/src/edu/uconn/tripoint/graph/Edge.java new file mode 100644 index 0000000..db3c024 --- /dev/null +++ b/src/edu/uconn/tripoint/graph/Edge.java @@ -0,0 +1,87 @@ +package edu.uconn.tripoint.graph; + +/** + * + * The edge object associates two Node objects in a graph and stores data specific to the edge. + * + * @param The type of data associated with a node. + * @param The type of data associated with an edge. + */ +public class Edge { + + private Node _n1, _n2; + private ED _data; + private boolean _directed; + + /** + * Creates an instance of an Edge. + * @param n1 The first node to be associated. If directed, it is the source node. + * @param n2 The second node to be associated. If directed, it is the destination node. + * @param data The data stored in this edge object. + * @param directed Boolean value representing whether or not this is a directed edge. + */ + public Edge(Node n1, Node n2, ED data, boolean directed){ + _n1 = n1; + _n2 = n2; + _data = data; + _directed = directed; + } + + /** + * Sets the data stored in this edge. + * @param data The data to be stored in this edge. + */ + public void setData(ED data){ + _data = data; + } + + /** + * Gets the first (source) node in this edge. + * @return The first (source) node in this edge. + */ + public Node getNode1(){ + return _n1; + } + + /** + * Gets the second (destination) node in this edge. + * @return The second (destination) node in this edge. + */ + public Node getNode2(){ + return _n2; + } + + /** + * Gets the data stored in this edge. + * @return The data stored in this edge. + */ + public ED getData(){ + return _data; + } + + /** + * Gets the source node in a directed edge. + * @return The source node in a directed edge. + */ + public Node getSource(){ + return getNode1(); + } + + /** + * Gets the destination node in a directed edge. + * @return The destination node in a directed edge. + */ + public Node getDestination(){ + return getNode2(); + } + + + /** + * Returns whether or not this edge is directed. + * @return TRUE if directed, FALSE if undirected. + */ + public boolean isDirected(){ + return _directed; + } + +} diff --git a/src/edu/uconn/tripoint/graph/Node.java b/src/edu/uconn/tripoint/graph/Node.java new file mode 100644 index 0000000..6a34e53 --- /dev/null +++ b/src/edu/uconn/tripoint/graph/Node.java @@ -0,0 +1,68 @@ +package edu.uconn.tripoint.graph; + +import java.util.LinkedList; +import java.util.List; + +/** + * + * The node object is one element of a graph where a List of Nodes will represent the entirety + * of the graph. Nodes are just the building blocks and it is up to the implementation to + * decide how the overall graph is constructed/stored. + * + * @param The type of data associated with a node. + * @param The type of data associated with an edge. + */ +public class Node { + + private ND _data; + private List> _edges; + + /** + * Creates a Node object that encapsulates the data provided. + * @param data The data stored in this Node. + */ + public Node(ND data){ + _data = data; + _edges = new LinkedList>(); + } + + /** + * Get the data encapsulated in this node. + * @return The data encapsulated in this node. + */ + public ND getData(){ + return _data; + } + + /** + * Sets new data for this node to encapsulate. + * @param data The new data object for this node to encapsulate. + */ + public void setData(ND data){ + _data = data; + } + + /** + * Add an edge to the list of edges connected to this node. + * @param edge The edge instance to add to this node's list of edges. + */ + public void addEdge(Edge edge){ + _edges.add(edge); + } + + /** + * Get the list of edges connected to this node. + * @return The list of edges connected to this node. + */ + public List> getEdges(){ + return _edges; + } + + /** + * Removes an edge from this node. + * @param e The edge instance to remove from this node's list of edges. + */ + public void removeEdge(Edge e){ + _edges.remove(e); + } +} diff --git a/src/edu/uconn/tripoint/integration/ChromatinInteractionNetwork.java b/src/edu/uconn/tripoint/integration/ChromatinInteractionNetwork.java new file mode 100644 index 0000000..3eb82b2 --- /dev/null +++ b/src/edu/uconn/tripoint/integration/ChromatinInteractionNetwork.java @@ -0,0 +1,97 @@ +package edu.uconn.tripoint.integration; + +import java.util.Collection; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; + +import edu.uconn.tripoint.pathway.Gene; +import quin.network.ChIAPETNetwork; +import quin.network.Edge; +import quin.network.Location; +import quin.network.NetworkBuilder; +import quin.network.Node; + +public class ChromatinInteractionNetwork implements NonCodingTargets { + + private ChIAPETNetwork _network; + + /** + * Uses QuIN to generate the Chromatin Interaction Network from 7 column tab delimited file. + * The last column of the interaction file is an integer score. + * @param interactionfile Seven column tab delimited chromatin interaction file. + * @param ext How much to extend the locations provided to intersect with interaction anchors. + */ + public ChromatinInteractionNetwork(String interactionfile, int ext){ + try { + NetworkBuilder nb = new NetworkBuilder(interactionfile); + _network = nb.buildNetwork(ext, 0, 1, 1000000, Integer.MAX_VALUE, 0, 2, Integer.MAX_VALUE); + } catch (Exception e) { + e.printStackTrace(); + } + } + + + /** + * Uses QuIN to generate the Chromatin Interaction Network from 7 column tab delimited file. + * The last column of the interaction file is an integer score. + * @param interactionfile Seven column tab delimited chromatin interaction file. + * @param bedfile Bedfile containing regions to build the interaction network. + * @param ext How much to extend the locations provided to intersect with interaction anchors. + */ + public ChromatinInteractionNetwork(String interactionfile, String bedfile, int ext){ + try { + Util u = new Util(); + Location[] l = u.readLocations(bedfile); + NetworkBuilder nb = new NetworkBuilder(interactionfile); + _network = nb.buildNetwork(l, 1, 1000000, Integer.MAX_VALUE, 0, 2, Integer.MAX_VALUE, 0, false); + } catch (Exception e) { + e.printStackTrace(); + } + } + + + @Override + public Map> getNoncodingTargets(Collection genes, Map> genecoords){ + Util u = new Util(); + quin.network.Util u2 = new quin.network.Util(); + TreeMap sortednodes = u2.getChrStartSorted(_network.getNodes()); + + Location[] allgenelocations = u.getAllLocations(genecoords); + TreeMap sortedgeneloci = u2.getChrStartSorted(allgenelocations); + + Map> rv = new TreeMap>(); + + for(Iterator it = genes.iterator(); it.hasNext();){ + Gene next = it.next(); + Map mappedlocations = new TreeMap(); + if(genecoords.containsKey(next)){ + for(Iterator it2 = genecoords.get(next).iterator(); it2.hasNext();){ + Location nextlocation = it2.next(); + Location[] chrnodes = sortednodes.get(nextlocation.getChr()); + Location[] onodes = new Location[0]; + if(chrnodes != null){ + onodes = u.getOverlappingLoci(chrnodes, nextlocation); + } + for(int i = 0; i < onodes.length; i++){ + Node curnode = (Node)onodes[i]; + Edge[] edges = curnode.getEdges(); + for(int j = 0; j < edges.length; j++){ + Edge curedge = edges[j]; + Node target = curedge.getAdjacentNode(curnode); + if(sortedgeneloci.containsKey(target.getChr()) && u.getOverlappingLoci(sortedgeneloci.get(target.getChr()), target).length == 0){ + mappedlocations.put(target.getChr()+":"+target.getStart()+"-"+target.getEnd(), target); + } + } + } + } + } + rv.put(next, new LinkedList(mappedlocations.values())); + } + + return rv; + } + +} diff --git a/src/edu/uconn/tripoint/integration/NonCodingTargets.java b/src/edu/uconn/tripoint/integration/NonCodingTargets.java new file mode 100644 index 0000000..c5c36b9 --- /dev/null +++ b/src/edu/uconn/tripoint/integration/NonCodingTargets.java @@ -0,0 +1,20 @@ +package edu.uconn.tripoint.integration; + +import java.util.Collection; +import java.util.List; +import java.util.Map; + +import quin.network.Location; +import edu.uconn.tripoint.pathway.Gene; + +public interface NonCodingTargets { + + /** + * Gets a list of non-coding targets which are targeting the gene. + * @param genes The collection of genes to get the targets from. + * @param genecoords The gene coordinates. + * @return A mapping from gene to non-coding targets. + */ + public Map> getNoncodingTargets(Collection genes, Map> genecoords); + +} diff --git a/src/edu/uconn/tripoint/integration/PoissonNonCoding.java b/src/edu/uconn/tripoint/integration/PoissonNonCoding.java new file mode 100644 index 0000000..6af11b0 --- /dev/null +++ b/src/edu/uconn/tripoint/integration/PoissonNonCoding.java @@ -0,0 +1,67 @@ +package edu.uconn.tripoint.integration; + +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.TreeMap; + +import quin.network.Location; +import edu.uconn.tripoint.pathway.Gene; + +public class PoissonNonCoding { + + public Map getPoissonPValues(Map> m){ + double average = 0; + int max = 0; + for(Iterator> it = m.values().iterator(); it.hasNext();){ + int size = it.next().size(); + average += size; + max = Math.max(max, size); + } + average /= m.size(); + + BigDecimal[] distr = getCumulativePoisson(max,average); + + Map rv = new TreeMap(); + for(Iterator>> it = m.entrySet().iterator(); it.hasNext();){ + Entry> e = it.next(); + rv.put(e.getKey(), getPoissonPValue(distr, e.getValue().size(), average)); + } + return rv; + } + + public double getPoissonPValue(BigDecimal[] distr, int k, double l){ + if(k <= l){ + return distr[k].doubleValue(); + } + else{ + return 1-(distr[k]).doubleValue(); + } + } + + public BigDecimal[] getCumulativePoisson(int maxk, double l){ + BigDecimal bdel = new BigDecimal(Math.exp(l)); + BigDecimal bdl = new BigDecimal(l); + + BigDecimal[] numerator = new BigDecimal[maxk+1]; + numerator[0] = new BigDecimal(1); + + BigDecimal[] denominator = new BigDecimal[maxk+1]; + denominator[0] = new BigDecimal(1); + + + BigDecimal[] rv = new BigDecimal[maxk+1]; + rv[0] = numerator[0].divide(bdel,10,RoundingMode.FLOOR); + for(int k = 1; k <= maxk; k++){ + numerator[k] = numerator[k-1].multiply(bdl); + denominator[k] = denominator[k-1].multiply(new BigDecimal(k)); + rv[k] = rv[k-1].add(numerator[k].divide(denominator[k].multiply(bdel),10,RoundingMode.FLOOR)); + } + return rv; + } + + +} diff --git a/src/edu/uconn/tripoint/integration/Proximal.java b/src/edu/uconn/tripoint/integration/Proximal.java new file mode 100644 index 0000000..c3f56c5 --- /dev/null +++ b/src/edu/uconn/tripoint/integration/Proximal.java @@ -0,0 +1,68 @@ +package edu.uconn.tripoint.integration; + +import java.io.IOException; +import java.util.Collection; +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.pathway.Gene; + +public class Proximal implements NonCodingTargets { + + private Map _locations; + private int _distance; + + public Proximal(String locations, int distance){ + try { + _locations = readLocations(locations); + } catch (IOException e) { + e.printStackTrace(); + } + _distance = distance; + } + + private Map readLocations(String file) throws IOException{ + Util u = new Util(); + Location[] l = u.readLocations(file); + quin.network.Util u2 = new quin.network.Util(); + return u2.getChrStartSorted(l); + } + + public void setDistance(int distance){ + _distance = distance; + } + + @Override + public Map> getNoncodingTargets(Collection genes, Map> genecoords){ + Util u = new Util(); + quin.network.Util u2 = new quin.network.Util(); + Location[] allgenelocations = u.getAllLocations(genecoords); + TreeMap sortedgeneloci = u2.getChrStartSorted(allgenelocations); + + Map> rv = new TreeMap>(); + + for(Iterator it = genes.iterator(); it.hasNext();){ + Gene next = it.next(); + Map mappedlocations = new TreeMap(); + for(Iterator it2 = genecoords.get(next).iterator(); it2.hasNext();){ + Location nextlocation = it2.next(); + Location paddedlocation = new Location(nextlocation.getIntegerId(), nextlocation.getChr(), nextlocation.getStart()-_distance, nextlocation.getEnd()+_distance); + Location[] chrloci = _locations.get(nextlocation.getChr()); + Location[] oloci = u.getOverlappingLoci(chrloci, paddedlocation); + for(int i = 0; i < oloci.length; i++){ + Location curloci = oloci[i]; + if(u.getOverlappingLoci(sortedgeneloci.get(curloci.getChr()), curloci).length == 0){ + mappedlocations.put(curloci.getChr()+":"+curloci.getStart()+"-"+curloci.getEnd(), curloci); + } + } + } + rv.put(next, new LinkedList(mappedlocations.values())); + } + + return rv; + } +} diff --git a/src/edu/uconn/tripoint/integration/Util.java b/src/edu/uconn/tripoint/integration/Util.java new file mode 100644 index 0000000..029909d --- /dev/null +++ b/src/edu/uconn/tripoint/integration/Util.java @@ -0,0 +1,161 @@ +package edu.uconn.tripoint.integration; + +import java.io.BufferedReader; +import java.io.FileReader; +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 edu.uconn.tripoint.pathway.Gene; +import quin.network.Location; + +public class Util { + + public Location[] readLocations(String file) throws IOException{ + LinkedList l = new LinkedList(); + BufferedReader br = new BufferedReader(new FileReader(file)); + int id = 0; + while(br.ready()){ + String line = br.readLine(); + String[] split = line.split("\t"); + if(split.length >= 3){ + try { + l.add(new Location(id++, split[0], Integer.parseInt(split[1]), Integer.parseInt(split[2]))); + } + catch(NumberFormatException e){ } + } + } + br.close(); + return l.toArray(new Location[0]); + } + + /** + * Reads gene coordinates from a refflat file which can be obtained from + * UCSC. + * @param refflat The path to the refflat file + * @return A mapping from Gene to Location. + * @throws IOException + */ + public Map> readRefflatGeneCoords(String refflat, int up, int down) throws IOException{ + Map> rv = new TreeMap>(); + + BufferedReader br = new BufferedReader(new FileReader(refflat)); + int id = 0; + while(br.ready()){ + try{ + String line = br.readLine(); + String[] split = line.split("\t"); + String gene = split[0]; + String chr = split[2]; + String strand = split[3]; + int start = Integer.parseInt(split[4]); + int end = Integer.parseInt(split[5]); + Gene g = new Gene(gene); + if(!rv.containsKey(g)){ + rv.put(g, new LinkedList()); + } + if(strand.equals("-")){ + rv.get(g).add(new Location(id++, chr, end-down, end+up)); + } + else{ + rv.get(g).add(new Location(id++, chr, start-up, start+down)); + } + } + catch(NumberFormatException e){ + + } + + } + br.close(); + return rv; + } + + public Location[] getAllLocations(Map> m){ + Map all = new TreeMap(); + for(Iterator> it = m.values().iterator(); it.hasNext();){ + for(Iterator it2 = it.next().iterator(); it2.hasNext();){ + Location next = it2.next(); + all.put(next.getChr()+":"+next.getStart()+"-"+next.getEnd(), next); + } + } + return all.values().toArray(new Location[0]); + } + + /** + * + * @param loci A start sorted array of loci. + * @param l + * @return + */ + public Location[] getOverlappingLoci(Location[] loci, Location l){ + int fi = findFirstIndex(loci, l); + int s = l.getStart(); + int e = l.getEnd(); + if(fi != -1){ + LinkedList rv = new LinkedList(); + for(int i = fi; i < loci.length; i++){ + if(isIn(loci[i], s, e)){ + rv.add(loci[i]); + } + } + return rv.toArray(new Location[0]); + } + else{ + return new Location[0]; + } + } + + private boolean isIn(Location l, int start, int end){ + return l.getEnd() >= start && end >= l.getStart(); + } + + private int findFirstIndex(Location[] loci, Location l){ + int start = l.getStart(); + int end = l.getEnd(); + + //Binary search to get an approximate index + int s = 0; + int e = loci.length-1; + while((e-s) > 1){ + int mi = s+((e-s)/2); + Location ml = loci[mi]; + int mstart = ml.getStart(); + if(mstart < start){ + s = mi; + } + else if(mstart > start){ + e = mi; + } + else{ + s = mi; + e = mi; + break; + } + } + + //Loop to make sure we have the location just before the start position goes beyond + for(int i = s+1; i < loci.length; i++){ + if(loci[i].getStart() > start){ + s = i-1; + break; + } + } + + //Choose position just before or just after depending on the overlap. + //If neither of them do return -1 + int n = s+1; + if(s < loci.length && loci[s].getEnd() > start){ //0base + return s; + } + else if(n < loci.length && loci[n].getStart() <= end && loci[n].getEnd() > start){ //0base + return n; + } + else { + return -1; + } + } + +} diff --git a/src/edu/uconn/tripoint/pathway/Gene.java b/src/edu/uconn/tripoint/pathway/Gene.java new file mode 100644 index 0000000..a82af67 --- /dev/null +++ b/src/edu/uconn/tripoint/pathway/Gene.java @@ -0,0 +1,86 @@ +package edu.uconn.tripoint.pathway; + +import java.util.Comparator; + +/** + * + * Gene/protein related data to store in the nodes of graph/network representations of pathways. + * + */ +public class Gene implements Comparator, Comparable{ + + private String _id; + private double _value; + /** + * Creates a new Gene instance. + * @param id The Gene identifier (i.e. Gene Symbol). + */ + public Gene(String id){ + _id = id; + _value = 0; + } + + /** + * Creates a new Gene instance with a given value/score. + * @param id The Gene identifier (i.e. Gene Symbol). + * @param value The numeric value/score (i.e. Expression/Log2 Fold change) of this gene. + */ + public Gene(String id, double value){ + _id = id; + _value = value; + } + + /** + * Gets the identifier for this gene. + * @return The string identifier for this gene. + */ + public String getId(){ + return _id; + } + + /** + * Gets the value/score of this gene. + * @return The value/score for this gene. + */ + public double getValue(){ + return _value; + } + + /** + * Sets the value/score of this gene. + * @param v The numeric value/score (i.e. Expression/Differential Expression) of this gene. + */ + public void setValue(double v){ + _value = v; + } + + @Override + public int compare(Gene g1, Gene g2) { + int compareto = g1.compareTo(g2); + if(compareto < 0){ + return -1; + } + else if(compareto > 0){ + return 1; + } + else{ + return 0; + } + } + + @Override + public boolean equals(Object o){ + if(o instanceof Gene){ + return getId().equals(((Gene) o).getId()); + } + else{ + return false; + } + } + + @Override + public int compareTo(Gene o) { + return getId().compareTo(o.getId()); + } + +} diff --git a/src/edu/uconn/tripoint/pathway/Pathway.java b/src/edu/uconn/tripoint/pathway/Pathway.java new file mode 100644 index 0000000..89b9d23 --- /dev/null +++ b/src/edu/uconn/tripoint/pathway/Pathway.java @@ -0,0 +1,205 @@ +package edu.uconn.tripoint.pathway; + +import edu.uconn.tripoint.graph.Edge; +import edu.uconn.tripoint.graph.Node; + +import java.util.Collection; +import java.util.Comparator; +import java.util.Map; +import java.util.TreeMap; + +/** + * + * This Pathway object facilitates the construction of a graph/network for a pathway to perform + * pathway enrichment based on topology. + * + */ +public class Pathway { + + private String _id; + private Map> _nodes; + private TreeMap _edgetypekey; + private TreeMap> _edgekey; + + /** + * Creates a new pathway object with an associated identifier (i.e. The pathway name. + * @param id The identifier associated with this pathway. + */ + public Pathway(String id){ + _id = id; + _nodes = new TreeMap>(); + _edgekey = new TreeMap>(); + _edgetypekey = new TreeMap(); + } + + /** + * Gets the identifier for this pathway. + * @return The identifier for this pathway. + */ + public String getId(){ + return _id; + } + + /** + * Adds a gene to this pathway, creating a node in the underlying graph/network. + * If a gene object with the same identifier as one previously added is provided, + * the gene is NOT added to avoid duplicate entries. A gene list should be kept + * by the caller of this method so that expression values can be changed easily + * WITHOUT recreating the graph/network. + * @param g The Gene to add to the pathway. + */ + public void addGene(Gene g) { + if(!_nodes.containsKey(g)){ + Node newnode = new Node(g); + _nodes.put(g, newnode); + } + } + + /** + * Gets the collection of nodes associated with this pathway. + * @return The collection of nodes associated with this pathway. + */ + public Collection> getEdges(){ + return _edgekey.values(); + } + + /** + * Gets the collection of nodes associated with this pathway. + * @return The collection of nodes associated with this pathway. + */ + public Collection> getNodes(){ + return _nodes.values(); + } + + /** + * Gets the node associated with the provided gene. + * @param g The gene object to identify the node to return. + * @return The node associated with the gene. NULL if no node is found. + */ + public Node getNode(Gene g){ + return _nodes.get(g); + } + + + /** + * Adds an edge in the pathway graph. + * @param g1 The first (source) gene to be associated. + * @param g2 The second (destination) gene to be associated. + * @param pe The type of edge. (activation, inhibition, association) + * @throws Exception An exception is thrown if the gene is not found in the pathway. + */ + public void addEdge(Gene g1, Gene g2, PathwayEdge pe) throws Exception{ + //TODO Should check if pathway is built where both directions for associations (undirected) are included + //Both genes must be in the pathway to add an edge. + if(_nodes.containsKey(g1) && _nodes.containsKey(g2)){ + //Get the corresponding nodes. + Node n1 = _nodes.get(g1); + Node n2 = _nodes.get(g2); + + EdgeKey edgeid = new EdgeKey(g1,g2); + EdgeKey cedgeid = new EdgeKey(g2,g1); + int curtype = pe.getType(); + if(_edgetypekey.containsKey(edgeid)){ + int prevtype = _edgetypekey.get(edgeid); + //Check for conflict cases + if((prevtype == PathwayEdge.ACTIVATION && curtype == PathwayEdge.INHIBITION) + || (curtype == PathwayEdge.ACTIVATION && prevtype == PathwayEdge.INHIBITION)){ + + System.out.println("Inconsistency in pathway. Removing:"+edgeid); + Edge e = _edgekey.get(edgeid); + e.getNode1().removeEdge(e); + e.getNode2().removeEdge(e); + _edgetypekey.put(edgeid, -2); //Set to -2 to prevent this type of edge from being added in the future + _edgekey.remove(edgeid); + } + else if(prevtype == PathwayEdge.ASSOCIATION && curtype != PathwayEdge.ASSOCIATION){ //update association edges to more informative activation/inhibition + System.out.println("Updating association in pathway:"+edgeid); + Edge e = _edgekey.get(edgeid); + e.getNode1().removeEdge(e); + e.getNode2().removeEdge(e); + _edgetypekey.put(edgeid, curtype); + _edgekey.remove(edgeid); + Edge newedge = new Edge(n1,n2,pe, true); + _edgekey.put(edgeid, newedge); + } + } + else if(_edgetypekey.containsKey(cedgeid)){ + int prevtype = _edgetypekey.get(cedgeid); + if(prevtype == PathwayEdge.ASSOCIATION && curtype != PathwayEdge.ASSOCIATION){ + System.out.println("Updating association in pathway:"+cedgeid); + Edge e = _edgekey.get(cedgeid); + e.getNode1().removeEdge(e); + e.getNode2().removeEdge(e); + _edgetypekey.remove(cedgeid); + _edgekey.remove(cedgeid); + Edge newedge = new Edge(n1,n2,pe, true); + _edgetypekey.put(edgeid, curtype); + _edgekey.put(edgeid, newedge); + } + } + else{ + Edge newedge = new Edge(n1,n2,pe, curtype != PathwayEdge.ASSOCIATION); + n1.addEdge(newedge); + n2.addEdge(newedge); + _edgetypekey.put(edgeid, curtype); + _edgekey.put(edgeid, newedge); + } + } + else{ + throw new Exception("Missing gene in pathway."); + } + } + + private class EdgeKey implements Comparator, Comparable{ + + private Gene _src; + private Gene _dest; + + public EdgeKey(Gene src, Gene dest){ + _src = src; + _dest = dest; + } + + @Override + public int compareTo(EdgeKey e) { + int srccompare = _src.compareTo(e._src); + if(srccompare == 0){ + return _dest.compareTo(e._dest); + } + else{ + return srccompare; + } + } + + @Override + public int compare(EdgeKey o1, EdgeKey o2) { + int compareto = o1.compareTo(o2); + if(compareto < 0){ + return -1; + } + else if(compareto > 0){ + return 1; + } + else { + return 0; + } + } + + @Override + public boolean equals(Object o){ + if(o instanceof EdgeKey){ + EdgeKey ek = (EdgeKey)o; + return _src.equals(ek._src) && _dest.equals(ek._dest); + } + else{ + return false; + } + } + + @Override + public String toString(){ + return _src.getId()+":"+_dest.getId(); + } + } + +} diff --git a/src/edu/uconn/tripoint/pathway/PathwayEdge.java b/src/edu/uconn/tripoint/pathway/PathwayEdge.java new file mode 100644 index 0000000..d382213 --- /dev/null +++ b/src/edu/uconn/tripoint/pathway/PathwayEdge.java @@ -0,0 +1,91 @@ +package edu.uconn.tripoint.pathway; + +/** + * + * The PathwayEdge type for use with generics used in the graph Edge class. + * + */ +public class PathwayEdge { + + /** + * Activation type. A directed edge. + */ + public static int ACTIVATION = 1; + /** + * Association type. An undirected edge. + */ + public static int ASSOCIATION = 0; + /** + * Inhibition type. A directed edge. + */ + public static int INHIBITION = -1; + + private static PathwayEdge ACTIVATIONEDGE = new PathwayEdge(ACTIVATION); + private static PathwayEdge ASSOCIATIONEDGE = new PathwayEdge(ASSOCIATION); + private static PathwayEdge INHIBITIONEDGE = new PathwayEdge(INHIBITION); + + private int _type; + + /** + * Use the static methods for creating the edge. + * @param type The type of edge. + */ + private PathwayEdge(int type){ + _type = type; + } + + + /** + * Gets the type of the pathway edge. + * @return The type of the pathway edge. + */ + public int getType(){ + return _type; + } + + + /** + * Get the activation pathway edge. + * @return The instance of the activation pathway edge. + */ + public static PathwayEdge ACTIVATION(){ + return ACTIVATIONEDGE; + } + + /** + * Get the inhibition pathway edge. + * @return The instance of the inhibition pathway edge. + */ + public static PathwayEdge INHIBITION(){ + return INHIBITIONEDGE; + } + + /** + * Get the association pathway edge. + * @return The instance of the association pathway edge. + */ + public static PathwayEdge ASSOCIATION(){ + return ASSOCIATIONEDGE; + } + + /** + * Helper method for obtaining the pathway edge for a provided type. + * @param type The type of PathwayEdge to return. + * @return The instance of the PathwayEdge with the input type. + * If the integer does not match any of the types provided in this class NULL is returned. + */ + public static PathwayEdge getPathwayEdge(int type){ + if(type == ACTIVATION){ + return ACTIVATION(); + } + else if(type == INHIBITION){ + return INHIBITION(); + } + else if(type == ASSOCIATION){ + return ASSOCIATION(); + } + else{ + return null; + } + } +} diff --git a/src/edu/uconn/tripoint/pathway/PathwayHelper.java b/src/edu/uconn/tripoint/pathway/PathwayHelper.java new file mode 100644 index 0000000..bb825bd --- /dev/null +++ b/src/edu/uconn/tripoint/pathway/PathwayHelper.java @@ -0,0 +1,78 @@ +package edu.uconn.tripoint.pathway; + +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Set; +import java.util.TreeSet; + +import edu.uconn.tripoint.graph.Edge; +import edu.uconn.tripoint.graph.Node; + +public class PathwayHelper { + + + public List> getInconsistentInconsistencies(Node n, Set seen, double downthresh, double upthresh){ + List> rv = new LinkedList>(); + List> edges = getDownstreamEdges(n); + Set seen2 = new TreeSet(); + for(Iterator> it = edges.iterator(); it.hasNext();){ + Edge next = it.next(); + Node dest = getDestination(n, next); + if(!isConsistent(next,n,dest,downthresh,upthresh) && !seen.contains(dest.getData()) && !seen2.contains(dest.getData())){ + seen2.add(dest.getData()); + rv.add(dest); + } + } + return rv; + } + + public boolean isConsistent(Edge edge, Node src, Node dest, double downthresh, double upthresh){ + int type = edge.getData().getType(); + double sv = src.getData().getValue(); + double dv = dest.getData().getValue(); + if(type == PathwayEdge.ACTIVATION){ + if(sv > upthresh && dv > upthresh){ + return true; + } + else if(sv < downthresh && dv < downthresh){ + return true; + } + } + else if(type == PathwayEdge.INHIBITION){ + if(sv > upthresh && dv < downthresh){ + return true; + } + else if(sv < downthresh && dv > upthresh){ + return true; + } + } + return false; + } + + + public Node getDestination(Node n, Edge e){ + if(e.isDirected()){ + return e.getDestination(); + } + else{ + if(e.getNode1().equals(n)){ + return e.getNode2(); + } + else{ + return e.getNode1(); + } + } + } + + public List> getDownstreamEdges(Node node){ + List> rv = new LinkedList>(); + for(Iterator> it = node.getEdges().iterator(); it.hasNext();){ + Edge next = it.next(); + if(!next.isDirected() || next.getSource().equals(node)){ + rv.add(next); + } + } + return rv; + } +} diff --git a/src/edu/uconn/tripoint/pathwayimport/GraphiteImport.java b/src/edu/uconn/tripoint/pathwayimport/GraphiteImport.java new file mode 100644 index 0000000..45cb44a --- /dev/null +++ b/src/edu/uconn/tripoint/pathwayimport/GraphiteImport.java @@ -0,0 +1,174 @@ +package edu.uconn.tripoint.pathwayimport; + +import java.util.Map; +import java.util.TreeMap; + +import org.rosuda.REngine.REXPMismatchException; +import org.rosuda.REngine.REngineException; +import org.rosuda.REngine.Rserve.RConnection; +import org.rosuda.REngine.Rserve.RserveException; + +import edu.uconn.tripoint.pathway.Gene; +import edu.uconn.tripoint.pathway.Pathway; +import edu.uconn.tripoint.pathway.PathwayEdge; + +/** + * Imports pathways from the Graphite R package using RServe. + * + */ +public class GraphiteImport { + + //All pathways merged into one. + private Pathway _globalpathway; + //Each individual pathway for each pathway in the library + private Pathway[] _pathways; + //All genes in the pathways. Used to update values after import. + private Map _genes; + + /** + * Imports a set of pathways from the Graphite R package. + * Requires RServe to be running in R. + * @param pathwaylibrary + * The name of the graphite pathway library + * to use ("kegg", "reactome", "biocarta", "humancyc", "nci", "panther") + */ + public GraphiteImport(String pathwaylibrary, String gid){ + Map edgetypemapping = getEdgeTypeMapping(); + startRServe(); + RConnection c; + System.out.println("Starting import."); + try { + //Creates an R Connection and loads the pathways in R. + c = new RConnection(); + c.parseAndEval("library(graphite)"); + c.parseAndEval("plib <- pathways(\"hsapiens\", \""+pathwaylibrary+"\")"); + c.parseAndEval("cpathwaylist <- convertIdentifiers(plib, \""+gid+"\")"); + int numpathways = c.parseAndEval("length(cpathwaylist)").asInteger(); + + //Set the global variables + _pathways = new Pathway[numpathways]; + _globalpathway = new Pathway("All "+pathwaylibrary); + _genes = new TreeMap(); + + System.out.println("Creating graphs."); + //Import each pathway + for(int i = 0; i < numpathways; i++){ + int rindex = (i+1); + String pathwayname = c.parseAndEval("names(cpathwaylist)["+rindex+"]").asString(); + c.parseAndEval("curpath = cpathwaylist[["+rindex+"]]"); + c.parseAndEval("ed <- edges(curpath)"); + String[] sources = c.parseAndEval("ed[,2]").asStrings(); + String[] destinations = c.parseAndEval("ed[,4]").asStrings(); + String[] edgetype = c.parseAndEval("ed[,6]").asStrings(); + + System.out.println("Loading "+pathwayname); + Pathway p = new Pathway(pathwaylibrary+":"+pathwayname); + _pathways[i] = p; + + //Add genes/nodes to the graph/network + for(int j = 0; j < sources.length; j++){ + if(!_genes.containsKey(sources[j])){ + _genes.put(sources[j], new Gene(sources[j])); + } + Gene g = _genes.get(sources[j]); + p.addGene(g); + _globalpathway.addGene(g); + } + for(int j = 0; j < destinations.length; j++){ + if(!_genes.containsKey(destinations[j])){ + _genes.put(destinations[j], new Gene(destinations[j])); + } + Gene g = _genes.get(destinations[j]); + p.addGene(g); + _globalpathway.addGene(g); + } + + //Add the edges + for(int j = 0; j < edgetype.length; j++){ + PathwayEdge pe = edgetypemapping.get(edgetype[j]); + if(pe != null){ + Gene g1 = _genes.get(sources[j]); + Gene g2 = _genes.get(destinations[j]); + try { + p.addEdge(g1, g2, pe); + _globalpathway.addEdge(g1,g2,pe); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + } + c.close(); + + } catch (RserveException e) { + e.printStackTrace(); + } catch (REXPMismatchException e) { + e.printStackTrace(); + } catch (REngineException e) { + e.printStackTrace(); + } + + } + + //Contains the mapping of edge types from Graphite to Activation, Inhibition, and Association + private Map getEdgeTypeMapping(){ + Map rv = new TreeMap(); + rv.put("Binding", PathwayEdge.ASSOCIATION()); + rv.put("Control(In: ACTIVATION of BiochemicalReaction)", PathwayEdge.ACTIVATION()); + rv.put("Control(In: ACTIVATION of ComplexAssembly)", PathwayEdge.ACTIVATION()); + rv.put("Control(In: ACTIVATION of Degradation)", PathwayEdge.ACTIVATION()); + rv.put("Control(In: ACTIVATION of Transport)", PathwayEdge.ACTIVATION()); + rv.put("Control(indirect)", null); + rv.put("Control(In: INHIBITION-COMPETITIVE of BiochemicalReaction)", PathwayEdge.INHIBITION()); + rv.put("Control(In: INHIBITION of BiochemicalReaction)", PathwayEdge.INHIBITION()); + rv.put("Control(In: INHIBITION of ComplexAssembly)", PathwayEdge.INHIBITION()); + rv.put("Control(In: INHIBITION of Transport)", PathwayEdge.INHIBITION()); + rv.put("Control(Out: ACTIVATION of ACTIVATION)", PathwayEdge.ACTIVATION()); + rv.put("Control(Out: ACTIVATION of BiochemicalReaction)", PathwayEdge.ACTIVATION()); + rv.put("Control(Out: ACTIVATION of ComplexAssembly)", PathwayEdge.ACTIVATION()); + rv.put("Control(Out: ACTIVATION of TemplateReaction)", PathwayEdge.ACTIVATION()); + rv.put("Control(Out: ACTIVATION of Transport)", PathwayEdge.ACTIVATION()); + rv.put("Control(Out: INHIBITION-COMPETITIVE of BiochemicalReaction)", PathwayEdge.INHIBITION()); + rv.put("Control(Out: INHIBITION of ACTIVATION)", PathwayEdge.INHIBITION()); + rv.put("Control(Out: INHIBITION of BiochemicalReaction)", PathwayEdge.INHIBITION()); + rv.put("Control(Out: INHIBITION of ComplexAssembly)", PathwayEdge.INHIBITION()); + rv.put("Control(Out: INHIBITION of TemplateReaction)", PathwayEdge.INHIBITION()); + rv.put("Control(Out: INHIBITION of Transport)", PathwayEdge.INHIBITION()); + rv.put("Process", PathwayEdge.ACTIVATION()); + rv.put("Process(activation)", PathwayEdge.ACTIVATION()); + rv.put("Process(binding/association)", PathwayEdge.ASSOCIATION()); + rv.put("Process(BiochemicalReaction)", PathwayEdge.ACTIVATION()); + rv.put("Process(demethylation)", PathwayEdge.ASSOCIATION()); + rv.put("Process(dephosphorylation)", PathwayEdge.ASSOCIATION()); + rv.put("Process(dissociation)", null); + rv.put("Process(expression)", PathwayEdge.ACTIVATION()); //Setting this to activation + rv.put("Process(glycosylation)", PathwayEdge.ASSOCIATION()); + rv.put("Process(indirect)", null); + rv.put("Process(indirect effect)", null); + rv.put("Process(inhibition)", PathwayEdge.INHIBITION()); + rv.put("Process(methylation)", PathwayEdge.ASSOCIATION()); + rv.put("Process(missing)", null); + rv.put("Process(missing interaction)", null); + rv.put("Process(phosphorylation)", PathwayEdge.ASSOCIATION()); + rv.put("Process(repression)", PathwayEdge.INHIBITION()); + rv.put("Process(state change)", PathwayEdge.ASSOCIATION()); + return rv; + } + + public Pathway getGlobalPathway(){ + return _globalpathway; + } + + public Pathway[] getPathways(){ + return _pathways; + } + + public Map getGenes(){ + return _genes; + } + + private void startRServe(){ + //TODO start RServe within this application + } + +} diff --git a/src/edu/uconn/tripoint/triangulation/FDRCorrection.java b/src/edu/uconn/tripoint/triangulation/FDRCorrection.java new file mode 100644 index 0000000..c6caae6 --- /dev/null +++ b/src/edu/uconn/tripoint/triangulation/FDRCorrection.java @@ -0,0 +1,17 @@ +package edu.uconn.tripoint.triangulation; + +import org.rosuda.REngine.REXPMismatchException; +import org.rosuda.REngine.REngineException; +import org.rosuda.REngine.Rserve.RConnection; + +public class FDRCorrection { + + public double[] getCorrectedPValues(double[] pvals) throws REngineException, REXPMismatchException{ + RConnection con = new RConnection(); + con.assign("pvals", pvals); + double[] rv = con.parseAndEval("p.adjust(pvals, \"fdr\")").asDoubles(); + con.close(); + return rv; + } + +} diff --git a/src/edu/uconn/tripoint/triangulation/ProgramParameters.java b/src/edu/uconn/tripoint/triangulation/ProgramParameters.java new file mode 100644 index 0000000..6f3bb2a --- /dev/null +++ b/src/edu/uconn/tripoint/triangulation/ProgramParameters.java @@ -0,0 +1,234 @@ +package edu.uconn.tripoint.triangulation; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; + +public class ProgramParameters { + + public final double VERSION = 1.0; + //required + private String _expfile; + private String _db; + private String _outdir; + + //defaults + private double _down; + private double _up; + private double _w; + private double _p1; + private double _p2; + private double _r; + private String _gid; + private int _perm; + private long _rstate; + + //optional + private String _refflat; + private String _chrinter; + private String _nct; + private int _prox = 50000; + + + public ProgramParameters(String[] args){ + //set default values; + _down = -0.05; + _up = 0.05; + _w = 0.2; + _p1 = 0.25; + _p2 = 0.000001; + _r = 0.25; + _gid = "SYMBOL"; + _perm = 1000; + _rstate = System.currentTimeMillis(); + + if(args.length > 0){ + _expfile = args[0]; //First argument will always be the expression file + } + + //TODO Probably better to just use a map and only set values + //if the key already exists + for(int i = 1; i < args.length-1; i += 2){ + String cur = args[i]; + String next = args[i+1]; + if(cur.equals("-down")){ + _down = Double.parseDouble(next); + } + else if(cur.equals("-up")){ + _up = Double.parseDouble(next); + } + else if(cur.equals("-w")){ + _w = Double.parseDouble(next); + } + else if(cur.equals("-p1")){ + _p1 = Double.parseDouble(next); + } + else if(cur.equals("-p2")){ + _p2 = Double.parseDouble(next); + } + else if(cur.equals("-r")){ + _r = Double.parseDouble(next); + } + else if(cur.equals("-gid")){ + _gid = next; + } + else if(cur.equals("-perm")){ + _perm = Integer.parseInt(next); + } + else if(cur.equals("-rstate")){ + _rstate = Long.parseLong(next); + } + else if(cur.equals("-db")){ + _db = next; + } + else if(cur.equals("-o")){ + _outdir = next; + if(!_outdir.endsWith("/")){ + _outdir = _outdir+"/"; + } + } + else if(cur.equals("-ci")){ + _chrinter = next; + } + else if(cur.equals("-nct")){ + _nct = next; + } + else if(cur.equals("-refflat")){ + _refflat = next; + } + else if(cur.equals("-prox")){ + _prox = Integer.parseInt(next); + } + + } + + } + + public String getExpressionFile(){ + return _expfile; + } + + public String getPathwayDB(){ + return _db; + } + + public String getOutputDirectory(){ + return _outdir; + } + + public double getDownThresh(){ + return _down; + } + + public double getUpThresh(){ + return _up; + } + + public double getW(){ + return _w; + } + + public double getP1(){ + return _p1; + } + + public double getP2(){ + return _p2; + } + + public double getR(){ + return _r; + } + + + public String getGID(){ + return _gid; + } + + public String getRefflat(){ + return _refflat; + } + + public String getChrInter(){ + return _chrinter; + } + + public String getNonCodingTargets(){ + return _nct; + } + + public int getProximityDistance(){ + return _prox; + } + + public int getNumPermutations(){ + return _perm; + } + + public long getRandomState(){ + return _rstate; + } + + public boolean hasRequired(){ + return _db != null && _outdir != null && (new File(_expfile)).exists() && (new File(_outdir)).isDirectory(); + } + + public boolean hasNonCoding(){ + return _refflat != null && (_chrinter != null || _nct != null); + } + + public String getConsoleManual(){ + StringBuilder sb = new StringBuilder(); + sb.append("Version: "+VERSION+"\n"); + sb.append("Usage\t\t\tjava -jar tripoint.jar -db -o \n\n"); + sb.append("-db\t\t\tSets pathway database to import pathways from GRAPHITE. Values available: \"kegg\", \"reactome\", \"biocarta\", \"humancyc\", \"nci\", \"panther\" \n\n"); + sb.append("-o\t\t\tSets output directory for saving result files. REQUIRED\n\n"); + sb.append("-gid\t\t\tSets the conversion method for gene identifiers. Refer to 'convertIdentifiers' in GRAPHITE. Default: \"SYMBOL\"\n\n"); + sb.append("-up\t\t\tSets the up-regulated gene threshold. Default: 0.25\n\n"); + sb.append("-down\t\t\tSets the down-regulated gene threshold. Default: -0.25\n\n"); + sb.append("-w\t\t\tSets the weak activation/inhibition factor. Default: 0.5\n\n"); + sb.append("-p1\t\t\tControls the impact of gene expression when calculating measures. Default: 0.5\n\n"); + sb.append("-p2\t\t\tControls the impact of support score normalization in the impact score calculation. Default: 0.5\n\n"); + sb.append("-r\t\t\tControls the rate of exponential decay to control edge distance in impact score calculation. Default: 0.25\n\n"); + sb.append("-perm\t\t\tSets the number of permutations for p-value calculations. Default: 1000\n\n"); + sb.append("-rstate\t\t\tSets the random state for reproducible results. Default: Current Time (milliseconds)\n\n"); + sb.append("-refflat\t\t\tSpecifies the UCSC refflat file to use to identify TSS locations of genes in pathways. Required for identifying non-coding targets\n\n" + + "Gene identifiers will need to match those provided in the expression file\n\n"); + sb.append("-ci\t\t\tSets the tab-delimited file for chromatin interaction data to identify non-coding targets of genes\n\n"); + sb.append("-nct\t\t\tSets the bed file for non-coding targets to be associated with genes" + + " If chromatin interactions are provided these sites will be connected via chromatin interactions, otherwise it will be proximity based\n\n"); + sb.append("-prox\t\t\tSets the proximity based non-coding target threshold which spans the distance both upstream and downstream from the transcription start site. Default: 50000\n\n"); + + return sb.toString(); + } + + public void writeParameters() throws IOException{ + BufferedWriter bw = new BufferedWriter(new FileWriter(_outdir+"Tripoint_Parameters.txt")); + bw.write(getParameters()); + bw.flush(); + bw.close(); + } + + public String getParameters(){ + StringBuilder sb = new StringBuilder(); + sb.append("Version: "+VERSION+"\n"); + sb.append("Expression File: "+_expfile+"\n"); + sb.append("Pathway Database: "+_db+"\n"); + sb.append("Output Directory: "+_outdir+"\n"); + sb.append("Up-Regulated Threshold: "+_up+"\n"); + sb.append("Down-Regulated Threshold: "+_down+"\n"); + sb.append("Weak Activation/Inhibition weight (w): "+_w+"\n"); + sb.append("Expression control parameter (p1): "+_p1+"\n"); + sb.append("Support control parameter (p2): "+_p2+"\n"); + sb.append("Exponential decay rate: "+_r+"\n"); + sb.append("Gene ID Conversion: "+_gid+"\n"); + sb.append("Number of Permutations: "+_perm+"\n"); + sb.append("Random State: "+_rstate+"\n"); + sb.append("Refflat File: "+_refflat+"\n"); + sb.append("Chromatin Interaction File: "+_chrinter+"\n"); + sb.append("Noncoding Target File: "+_nct+"\n"); + sb.append("Proximity distance: "+_prox+"\n"); + return sb.toString(); + } +} diff --git a/src/edu/uconn/tripoint/triangulation/Run.java b/src/edu/uconn/tripoint/triangulation/Run.java new file mode 100644 index 0000000..22e5bf3 --- /dev/null +++ b/src/edu/uconn/tripoint/triangulation/Run.java @@ -0,0 +1,183 @@ +package edu.uconn.tripoint.triangulation; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; + +import quin.network.Location; +import edu.uconn.tripoint.export.CytoscapeExport; +import edu.uconn.tripoint.export.NonCodingTargetExport; +import edu.uconn.tripoint.export.TriangulationExport; +import edu.uconn.tripoint.integration.ChromatinInteractionNetwork; +import edu.uconn.tripoint.integration.PoissonNonCoding; +import edu.uconn.tripoint.integration.Proximal; +import edu.uconn.tripoint.integration.Util; +import edu.uconn.tripoint.pathway.Gene; +import edu.uconn.tripoint.pathway.Pathway; +import edu.uconn.tripoint.pathwayimport.GraphiteImport; + +public class Run { + + public static void main(String[] args){ + + Run r = new Run(); + r.run(args); + + /* + GraphiteImport gi = new GraphiteImport("nci"); + Map g = gi.getGenes(); + Pathway globalpathway = gi.getGlobalPathway(); + Pathway[] pathways = gi.getPathways(); + try { + //read/set gene vale + BufferedReader br = new BufferedReader(new FileReader("C:/Users/Asa/Documents/wikiimport/breastcancer_logfoldchange_symbol.txt")); + while(br.ready()){ + String line= br.readLine(); + String[] split = line.split("\t"); + if(g.containsKey(split[0])){ + Gene gene = g.get(split[0]); + gene.setValue(Double.parseDouble(split[1])); + } + } + br.close(); + Triangulation t = new Triangulation(gi.getGlobalPathway(), gi.getPathways(), g.values()); + + + + //write file: GENE PATHWAY CONSISTENCY IMPACT + Collection c = t.getTriangulatedGenes(); + + BufferedWriter bw = new BufferedWriter(new FileWriter("C:/Users/Asa/Documents/wikiimport/breastcancer_logfoldchange_symbol_triangulated_nci2.txt")); + + for(Iterator it = c.iterator(); it.hasNext();){ + TriangulatedGene next = it.next(); + + double gcs = next.getConsistencyScore(globalpathway.getId()); + double gis = next.getImpactScore(globalpathway.getId()); + bw.write(next.getGene().getId()+"\t"+globalpathway.getId()+"\t"+gcs+"\t"+gis+"\n"); + + for(int i = 0; i < pathways.length; i++){ + double cs = next.getConsistencyScore(pathways[i].getId()); + double is = next.getImpactScore(pathways[i].getId()); + bw.write(next.getGene().getId()+"\t"+pathways[i].getId()+"\t"+cs+"\t"+is+"\n"); + } + + } + + bw.flush(); + bw.close(); + + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + }*/ + } + + public void run(String[] args){ + ProgramParameters params = new ProgramParameters(args); + if(params.hasRequired()){ + //Step 1: Import the pathways from Graphite (needs Rserve started to work) + GraphiteImport gi = new GraphiteImport(params.getPathwayDB(), params.getGID()); + + //Step 2: Set the expression values. + Map g = gi.getGenes(); + Pathway globalpathway = gi.getGlobalPathway(); + Pathway[] pathways = gi.getPathways(); + //read/set gene vale + try { + BufferedReader br = new BufferedReader(new FileReader(params.getExpressionFile())); + while(br.ready()){ + String line= br.readLine(); + String[] split = line.split("\t"); + if(g.containsKey(split[0])){ + Gene gene = g.get(split[0]); + gene.setValue(Double.parseDouble(split[1])); + } + } + br.close(); + } catch (IOException e) { + e.printStackTrace(); + } + + //Step 3: Run Triangulation. + Triangulation t = new Triangulation(); + t.runTriangulation(globalpathway, pathways, g.values(), params.getDownThresh(), params.getUpThresh(), params.getW(), params.getP1(), params.getR()); + + System.out.println("Running permutations:"); + //Step 4: Run Permutations + t.runPermutations(g.values(), params.getNumPermutations(), params.getRandomState(), globalpathway, pathways, params.getDownThresh(), params.getUpThresh(), params.getW(), params.getP1(), params.getP2(), params.getR()); + + + //Optional & Output Steps: + Map> m = new TreeMap>(); + if(params.hasNonCoding()){ + Util u = new Util(); + Map> refflat = null; + try { + refflat = u.readRefflatGeneCoords(params.getRefflat(), 2000, 2000); + } catch (IOException e) { + e.printStackTrace(); + } + if(params.getChrInter() != null){ + //use chromatin interactions + String nct = params.getNonCodingTargets(); + if(nct != null){ + ChromatinInteractionNetwork cin = new ChromatinInteractionNetwork(params.getChrInter(), nct, 500); + m = cin.getNoncodingTargets(g.values(), refflat); + } + else{ + ChromatinInteractionNetwork cin = new ChromatinInteractionNetwork(params.getChrInter(), 500); + m = cin.getNoncodingTargets(g.values(), refflat); + } + } + else{ + //proximity based + Proximal p = new Proximal(params.getNonCodingTargets(), params.getProximityDistance()); + m = p.getNoncodingTargets(g.values(), refflat); + } + //Export Noncoding Targets + NonCodingTargetExport ncte = new NonCodingTargetExport(); + try { + ncte.ExportNonCodingTargets(globalpathway, pathways, t.getTriangulatedGenes(), m, params.getOutputDirectory(), params.getPathwayDB()); + } catch (IOException e) { + e.printStackTrace(); + } + } + + + Map ncp = new TreeMap(); + PoissonNonCoding pnc = new PoissonNonCoding(); + ncp = pnc.getPoissonPValues(m); + + //Export Triangulation + TriangulationExport te = new TriangulationExport(); + try { + te.ExportTriangulation(t.getGenePathways(), t.getTriangulatedGenes(), m, ncp, params.getOutputDirectory(), params.getPathwayDB()); + } catch (IOException e) { + e.printStackTrace(); + } + + //Export Cytoscape Pathways + CytoscapeExport ce = new CytoscapeExport(); + //ce.exportToCytoscapeJSON(globalpathway, t.getTrianguatedGeneMapping(), m, params.getOutputDirectory(),params.getPathwayDB()); + for(int i = 0; i < pathways.length; i++){ + ce.exportToCytoscapeJSON(pathways[i], t.getTrianguatedGeneMapping(), m, params.getOutputDirectory(),params.getPathwayDB()+"_"+i); + } + + try { + params.writeParameters(); + } catch (IOException e) { + e.printStackTrace(); + } + + } + else{ + System.out.println(params.getConsoleManual()); + } + } + + +} diff --git a/src/edu/uconn/tripoint/triangulation/TriangulatedGene.java b/src/edu/uconn/tripoint/triangulation/TriangulatedGene.java new file mode 100644 index 0000000..fb6b74f --- /dev/null +++ b/src/edu/uconn/tripoint/triangulation/TriangulatedGene.java @@ -0,0 +1,505 @@ +package edu.uconn.tripoint.triangulation; + +import java.util.Map; +import java.util.TreeMap; + +import edu.uconn.tripoint.pathway.Gene; + +/** + * + * Encapsulates the scores obtained from the triangulation analysis for each gene. + * + */ +public class TriangulatedGene { + + private Gene _gene; + private Map _supportscores; + private Map _inconsistencyscores; + private Map _consistencyscores; + private Map _impactscores; + private Map _striscores; + private Map _itriscores; + + + //The following variables relate to memory efficient P-Value calculations for each score + private int _permutationcount; + private Map _permutationsupportscores; + private Map _permutationinconsistencyscores; + private Map _permutationconsistencyscores; + private Map _permutationimpactscores; + private Map _supportnullcount; + private Map _inconsistencynullcount; + private Map _consistencynullcount; + private Map _impactnullcount; + private Map _strinullcount; + private Map _itrinullcount; + + + private Map _supportfdr; + private Map _inconsistencyfdr; + private Map _consistencyfdr; + private Map _impactfdr; + private Map _strifdr; + private Map _itrifdr; + + + /** + * Creates a new object for storing triangulation scores + * and generating P-Values for each score. + * @param g The gene represented by the scores calculated. + */ + public TriangulatedGene(Gene g){ + _gene = g; + _supportscores = new TreeMap(); + _inconsistencyscores = new TreeMap(); + _consistencyscores = new TreeMap(); + _impactscores = new TreeMap(); + _striscores = new TreeMap(); + _itriscores = new TreeMap(); + resetPermutations(); + } + + /** + * Gets the gene for which these scores represent. + * @return The gene represented. + */ + public Gene getGene(){ + return _gene; + } + + + /** + * Sets the support score for the gene in a specified pathway. + * @param pathway The pathway for which the score is set. + * @param score The support score for the specified pathway. + */ + public void setSupportScore(String pathway, double score){ + _supportscores.put(pathway, score); + } + + /** + * Gets the support score for a given pathway. + * @param pathway The pathway to get the support score from. + * @return The support score for the given pathway. + */ + public double getSupportScore(String pathway){ + return _supportscores.get(pathway); + } + + /** + * Sets the inconsistency score for the gene in a specified pathway. + * @param pathway The pathway for which the score is set. + * @param score The inconsistency score for the specified pathway. + */ + public void setInconsistencyScore(String pathway, double score){ + _inconsistencyscores.put(pathway, score); + } + + /** + * Gets the inconsistency score for a given pathway. + * @param pathway The pathway to get the inconsistency score from. + * @return The inconsistency score for the given pathway. + */ + public double getInconsistencyScore(String pathway){ + return _inconsistencyscores.get(pathway); + } + + /** + * Sets the consistency score for the gene in a specified pathway. + * @param pathway The pathway for which the score is set. + * @param score The consistency score for the specified pathway. + */ + public void setConsistencyScore(String pathway, double score){ + _consistencyscores.put(pathway, score); + } + + /** + * Gets the consistency score for a given pathway. + * @param pathway The pathway to get the consistency score from. + * @return The consistency score for the given pathway. + */ + public double getConsistencyScore(String pathway){ + return _consistencyscores.get(pathway); + } + + /** + * Sets the impact score for a given pathway. + * @param pathway The pathway for which the score is set. + * @param score The impact score for the specified pathway. + */ + public void setImpactScore(String pathway, double score){ + _impactscores.put(pathway, score); + } + + /** + * Gets the impact score from the specified pathway. + * @param score The impact score for the specific pathway. + */ + public double getImpactScore(String pathway){ + return _impactscores.get(pathway); + } + + + /** + * Sets the Supporting Triangulation score for a given pathway. + * @param pathway The pathway for which the score is set. + * @param score The Supporting Triangulation score for the specified pathway. + */ + public void setSupportTriangulationScore(String pathway, double score){ + _striscores.put(pathway, score); + } + + /** + * Gets the Supporting Triangulation score from the specified pathway. + * @param score The Supporting Triangulation score for the specific pathway. + */ + public double getSupportTriangulationScore(String pathway){ + return _striscores.get(pathway); + } + + /** + * Sets the Inconsistent Triangulation score for a given pathway. + * @param pathway The pathway for which the score is set. + * @param score The Inconsistent Triangulation score for the specified pathway. + */ + public void setInconsistentTriangulationScore(String pathway, double score){ + _itriscores.put(pathway, score); + } + + /** + * Gets the Inconsistent Triangulation score from the specified pathway. + * @param score The Inconsistent Triangulation score for the specific pathway. + */ + public double getInconsistentTriangulationScore(String pathway){ + return _itriscores.get(pathway); + } + + /** + * Resets the permutations in the P-Value calculations for each score. + */ + public void resetPermutations(){ + _permutationcount = 0; + _supportnullcount = new TreeMap(); + _inconsistencynullcount = new TreeMap(); + _consistencynullcount = new TreeMap(); + _impactnullcount = new TreeMap(); + _itrinullcount = new TreeMap(); + _strinullcount = new TreeMap(); + + + _permutationsupportscores = new TreeMap(); + _permutationinconsistencyscores = new TreeMap(); + _permutationconsistencyscores = new TreeMap(); + _permutationimpactscores = new TreeMap(); + + _supportfdr = new TreeMap(); + _inconsistencyfdr = new TreeMap(); + _consistencyfdr = new TreeMap(); + _impactfdr = new TreeMap(); + _itrifdr = new TreeMap(); + _strifdr = new TreeMap(); + } + + /** + * Start a new round of permutations for P-Value calculations. + */ + public void newPermutationRound(){ + _permutationsupportscores = new TreeMap(); + _permutationinconsistencyscores = new TreeMap(); + _permutationcount++; + } + + /** + * Updates the permutation counts (P-Value calculation) for the inconsistency score specified by the pathway. + * @param pathway The pathway for which the score is set. + * @param score The permuted inconsistency score for the specified pathway. + */ + public void updateInconsistencyPermutation(String pathway, double score) { + _permutationinconsistencyscores.put(pathway, score); + updatePermutationScore(_inconsistencyscores, _inconsistencynullcount, pathway, score); + } + + /** + * Gets the current permutation round inconsistency score. + * @param pathway The pathway specified to obtain the score from. + * @return The inconsistency score for the current permutation round. + */ + public double getInconsistencyPermutation(String pathway){ + return _permutationinconsistencyscores.get(pathway); + } + + /** + * Updates the permutation counts (P-Value calculation) for the support score specified by the pathway. + * @param pathway The pathway for which the score is set. + * @param score The permuted support score for the specified pathway. + */ + public void updateSupportPermutation(String pathway, double score) { + _permutationsupportscores.put(pathway, score); + updatePermutationScore(_supportscores, _supportnullcount, pathway, score); + } + + /** + * Gets the current permutation round support score. + * @param pathway The pathway specified to obtain the score from. + * @return The support score for the current permutation round. + */ + public double getSupportPermutation(String pathway){ + return _permutationsupportscores.get(pathway); + } + + /** + * Updates the permutation counts (P-Value calculation) for the consistency score specified by the pathway. + * @param pathway The pathway for which the score is set. + * @param score The permuted consistency score for the specified pathway. + */ + public void updateConsistencyPermutation(String pathway, double score) { + _permutationconsistencyscores.put(pathway,score); + updatePermutationScore(_consistencyscores, _consistencynullcount, pathway, score); + } + + + /** + * Gets the current permutation round consistency score. + * @param pathway The pathway specified to obtain the score from. + * @return The consistency score for the current permutation round. + */ + public double getConsistencyPermutation(String pathway){ + return _permutationconsistencyscores.get(pathway); + } + + + /** + * Updates the permutation counts (P-Value calculation) for the impact score specified by the pathway. + * @param pathway The pathway for which the score is set. + * @param score The permuted impact score for the specified pathway. + */ + public void updateImpactPermutation(String pathway, double score) { + _permutationimpactscores.put(pathway,score); + updatePermutationScore(_impactscores, _impactnullcount, pathway, score); + } + + + /** + * Gets the current permutation round impact score. + * @param pathway The pathway specified to obtain the score from. + * @return The impact score for the current permutation round. + */ + public double getImpactPermutation(String pathway){ + return _permutationimpactscores.get(pathway); + } + + + /** + * Updates the permutation counts (P-Value calculation) for the supporting triangulation score specified by the pathway. + * @param pathway The pathway for which the score is set. + * @param score The permuted supporting triangulation score for the specified pathway. + */ + public void updateSupportTriangulationPermutation(String pathway, double score) { + updatePermutationScore(_striscores, _strinullcount, pathway, score); + } + + /** + * Updates the permutation counts (P-Value calculation) for the inconsistent triangulation score specified by the pathway. + * @param pathway The pathway for which the score is set. + * @param score The permuted impact score for the specified pathway. + */ + public void updateInconsistentTriangulationPermutation(String pathway, double score) { + updatePermutationScore(_itriscores, _itrinullcount, pathway, score); + } + + private void updatePermutationScore(Map obsmap, Map nullcount, String pathway, double score){ + double observed = obsmap.get(pathway); + if(!nullcount.containsKey(pathway)){ + nullcount.put(pathway,0); + } + int curcount = nullcount.get(pathway); + if(observed < 0){ + if(observed >= score){ + nullcount.put(pathway, curcount+1); + } + } + else if(observed > 0){ + if(observed <= score){ + nullcount.put(pathway, curcount+1); + } + } + else{ + nullcount.put(pathway, curcount+1); + } + } + + /** + * Gets the current P-Value for the Inconsistency Score of this gene for the pathway selected. + * @param pathway The pathway selected. + * @return The Inconsistency Score P-Value for the specified Pathway. + */ + public double getInconsistencyPValue(String pathway){ + return getPValue(_inconsistencynullcount.get(pathway)); + } + + /** + * Gets the current P-Value for the Support Score of this gene for the pathway selected. + * @param pathway The pathway selected. + * @return The Support Score P-Value for the specified Pathway. + */ + public double getSupportPValue(String pathway){ + return getPValue(_supportnullcount.get(pathway)); + } + + /** + * Gets the current P-Value for the Consistency Score of this gene for the pathway selected. + * @param pathway The pathway selected. + * @return The Consistency Score P-Value for the specified Pathway. + */ + public double getConsistencyPValue(String pathway){ + return getPValue(_consistencynullcount.get(pathway)); + } + + /** + * Gets the current P-Value for the Impact Score of this gene for the pathway selected. + * @param pathway The pathway selected. + * @return The Impact Score P-Value for the specified Pathway. + */ + public double getImpactPValue(String pathway){ + return getPValue(_impactnullcount.get(pathway)); + } + + /** + * Gets the current P-Value for the support triangulation Score of this gene for the pathway selected. + * @param pathway The pathway selected. + * @return The support triangulation Score P-Value for the specified Pathway. + */ + public double getSupportTriangulationPValue(String pathway){ + return getPValue(_strinullcount.get(pathway)); + } + + + /** + * Gets the current P-Value for the inconsistent triangulation Score of this gene for the pathway selected. + * @param pathway The pathway selected. + * @return The inconsistent triangulation Score P-Value for the specified Pathway. + */ + public double getInconsistentTriangulationPValue(String pathway){ + return getPValue(_itrinullcount.get(pathway)); + } + + + + /** + * Sets the inconsistency FDR value for the pathway specified. + * @param pathway The pathway specified to set the FDR value. + * @param fdr The inconsistency FDR value; + */ + public void setInconsistencyFDR(String pathway, double fdr){ + _inconsistencyfdr.put(pathway, fdr); + } + + /** + * Sets the support FDR value for the pathway specified. + * @param pathway The pathway specified to set the FDR value. + * @param fdr The support FDR value; + */ + public void setSupportFDR(String pathway, double fdr){ + _supportfdr.put(pathway, fdr); + } + + /** + * Sets the consistency FDR value for the pathway specified. + * @param pathway The pathway specified to set the FDR value. + * @param fdr The consistency FDR value; + */ + public void setConsistencyFDR(String pathway, double fdr){ + _consistencyfdr.put(pathway, fdr); + } + + /** + * Sets the impact FDR value for the pathway specified. + * @param pathway The pathway specified to set the FDR value. + * @param fdr The impact FDR value; + */ + public void setImpactFDR(String pathway, double fdr){ + _impactfdr.put(pathway, fdr); + } + + /** + * Sets the support triangulation FDR value for the pathway specified. + * @param pathway The pathway specified to set the FDR value. + * @param fdr The support triangulation FDR value; + */ + public void setSupportTriangulationFDR(String pathway, double fdr){ + _strifdr.put(pathway, fdr); + } + + /** + * Sets the inconsistent triangulation FDR value for the pathway specified. + * @param pathway The pathway specified to set the FDR value. + * @param fdr The inconsistent triangulation FDR value; + */ + public void setInconsistentTriangulationFDR(String pathway, double fdr){ + _itrifdr.put(pathway, fdr); + } + + /** + * Gets the inconsistency adjusted p-FDR value for the specified pathway. + * @param pathway The pathway specified. + * @return The inconsistency FDR value for this gene in the specified pathway. + */ + public double getInconsistencyFDR(String pathway){ + return _inconsistencyfdr.get(pathway); + } + + /** + * Gets the support FDR adjusted p-value for the specified pathway. + * @param pathway The pathway specified. + * @return The support FDR value for this gene in the specified pathway. + */ + public double getSupportFDR(String pathway){ + return _supportfdr.get(pathway); + } + + /** + * Gets the consistency FDR adjusted p-value for the specified pathway. + * @param pathway The pathway specified. + * @return The consistency FDR value for this gene in the specified pathway. + */ + public double getConsistencyFDR(String pathway){ + return _consistencyfdr.get(pathway); + } + + /** + * Gets the impact FDR adjusted p-value for the specified pathway. + * @param pathway The pathway specified. + * @return The FDR value for this gene in the specified pathway. + */ + public double getImpactFDR(String pathway){ + return _impactfdr.get(pathway); + } + + /** + * Gets the Supporting Triangulation FDR adjusted p-value for the specified pathway. + * @param pathway The pathway specified. + * @return The FDR value for this gene in the specified pathway. + */ + public double getSupportingTriangulationFDR(String pathway){ + return _strifdr.get(pathway); + } + + /** + * Gets the Inconsistent Triangulation FDR adjusted p-value for the specified pathway. + * @param pathway The pathway specified. + * @return The FDR value for this gene in the specified pathway. + */ + public double getInconsistentTriangulationFDR(String pathway){ + return _itrifdr.get(pathway); + } + + private double getPValue(Integer nullcount){ + //Add 1 to the numerator and denominator to include the sample + //and to ensure the P-Value is never 0 which is often meaningless + //and informs the minimum P-Value based on the number of permutations. + if(nullcount == null){ + return 1; + } + return ((double)(nullcount+1)/(_permutationcount+1)); + } +} diff --git a/src/edu/uconn/tripoint/triangulation/Triangulation.java b/src/edu/uconn/tripoint/triangulation/Triangulation.java new file mode 100644 index 0000000..05bae5d --- /dev/null +++ b/src/edu/uconn/tripoint/triangulation/Triangulation.java @@ -0,0 +1,526 @@ +package edu.uconn.tripoint.triangulation; + +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.pathway.PathwayHelper; + +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.Set; +import java.util.TreeMap; +import java.util.TreeSet; + +import org.rosuda.REngine.REXPMismatchException; +import org.rosuda.REngine.REngineException; + +public class Triangulation { + + private Map _alltgenes; + private TriangulatedGene[] _tgenearray; + private Map> _genepathways; + public void runTriangulation(Pathway globalpathway, Pathway[] pathways, Collection genes, double downthresh, double upthresh, double w, double p, double r){ + _alltgenes = new TreeMap(); + + //Set a map of genes for log(n) access + TriangulatedGene[] triangulatedgenes = new TriangulatedGene[genes.size()]; + int index = 0; + for(Iterator it = genes.iterator(); it.hasNext();){ + Gene g = it.next(); + TriangulatedGene newgene = new TriangulatedGene(g); + _alltgenes.put(g, newgene); + triangulatedgenes[index++] = newgene; + } + _tgenearray = triangulatedgenes; + _genepathways = getGenePathways(pathways); + + setInconsistencyScores(triangulatedgenes, pathways, downthresh, upthresh, w, p); + setSupportScores(triangulatedgenes, pathways, downthresh, upthresh, w, p); + setConsistencyScores(triangulatedgenes, pathways); + setImpactScores(triangulatedgenes, pathways, downthresh, upthresh, p, r); + + } + + private Map> getGenePathways(Pathway[] pathways){ + Map> rv = new TreeMap>(); + for(int i = 0; i < pathways.length; i++){ + for(Iterator> it = pathways[i].getNodes().iterator(); it.hasNext();){ + Gene curg = it.next().getData(); + if(!rv.containsKey(curg)){ + rv.put(curg, new LinkedList()); + } + rv.get(curg).add(pathways[i]); + } + } + return rv; + } + + public Map> getGenePathways(){ + return _genepathways; + } + + public void runPermutations(Collection genes, int numpermutatons, long seedstart, Pathway globalpathway, Pathway[] pathways, double downthresh, double upthresh, double w, double p1, double p2, double r){ + //Store the original gene values in order + List values = new LinkedList(); + for(Iterator it = genes.iterator(); it.hasNext();){ + values.add(it.next().getValue()); + } + + for(int i = 0; i < _tgenearray.length; i++){ + _tgenearray[i].resetPermutations(); + } + + for(int i = 0; i < numpermutatons; i++){ + List randomlist = new LinkedList(genes); + Collections.shuffle(randomlist, new Random(seedstart+i)); + Iterator it1 = values.iterator(); + Iterator it2 = randomlist.iterator(); + while(it1.hasNext()){ + it2.next().setValue(it1.next()); + } + setPermutationScores(globalpathway, pathways, downthresh, upthresh, w, p1, p2, r); + //System.out.println(i); + if((i+1)%500 == 0){ + System.out.println("Finished "+(i+1)+" permutations."); + } + } + + + //Restore the values + Iterator it1 = values.iterator(); + Iterator it2 = genes.iterator(); + while(it1.hasNext()){ + it2.next().setValue(it1.next()); + } + + try { + setFDRAdjustedValues(_tgenearray, pathways); + } catch (Exception e) { + e.printStackTrace(); + } + } + + + //Set the FDR value using the Benjamini Hochberg Procedure + //The global pathway is omitted from this calculation + private void setFDRAdjustedValues(TriangulatedGene[] genes, Pathway[] pathways) throws Exception{ + + List gl = new LinkedList(); + List pl = new LinkedList(); + List ipvals = new LinkedList(); + List spvals = new LinkedList(); + List cpvals = new LinkedList(); + List impvals = new LinkedList(); + List strivals = new LinkedList(); + List itrivals = new LinkedList(); + + + for(int i = 0; i < genes.length; i++){ + TriangulatedGene tg = genes[i]; + for(Iterator it = _genepathways.get(tg.getGene()).iterator(); it.hasNext();){ + Pathway curpath = it.next(); + gl.add(tg); + pl.add(curpath); + ipvals.add(tg.getInconsistencyPValue(curpath.getId())); + spvals.add(tg.getSupportPValue(curpath.getId())); + cpvals.add(tg.getConsistencyPValue(curpath.getId())); + impvals.add(tg.getImpactPValue(curpath.getId())); + strivals.add(tg.getSupportTriangulationPValue(curpath.getId())); + itrivals.add(tg.getInconsistentTriangulationPValue(curpath.getId())); + + } + } + + FDRCorrection fdrc = new FDRCorrection(); + + try { + double[] iqvals = fdrc.getCorrectedPValues(getArray(ipvals)); + double[] sqvals = fdrc.getCorrectedPValues(getArray(spvals)); + double[] cqvals = fdrc.getCorrectedPValues(getArray(cpvals)); + double[] imqvals = fdrc.getCorrectedPValues(getArray(impvals)); + double[] striqvals = fdrc.getCorrectedPValues(getArray(strivals)); + double[] itriqvals = fdrc.getCorrectedPValues(getArray(itrivals)); + + + Iterator it1 = gl.iterator(); + Iterator it2 = pl.iterator(); + + int index = 0; + while(it1.hasNext() && it2.hasNext()){ + TriangulatedGene tg = it1.next(); + Pathway p = it2.next(); + tg.setInconsistencyFDR(p.getId(), iqvals[index]); + tg.setSupportFDR(p.getId(), sqvals[index]); + tg.setConsistencyFDR(p.getId(), cqvals[index]); + tg.setImpactFDR(p.getId(), imqvals[index]); + tg.setSupportTriangulationFDR(p.getId(), striqvals[index]); + tg.setInconsistentTriangulationFDR(p.getId(), itriqvals[index]); + + index++; + } + if(it1.hasNext() || it2.hasNext()){ + throw new Exception("List sizes do not match."); + } + + + } catch (REngineException e) { + e.printStackTrace(); + } catch (REXPMismatchException e) { + e.printStackTrace(); + } + + + } + + private double[] getArray(List l){ + double[] rv = new double[l.size()]; + int index = 0; + for(Iterator it = l.iterator(); it.hasNext();){ + rv[index++] = it.next(); + } + return rv; + } + + + public Collection getTriangulatedGenes(){ + return _alltgenes.values(); + } + + public Map getTrianguatedGeneMapping(){ + return _alltgenes; + } + + private void setPermutationScores(Pathway globalpathway, Pathway[] pathways, double downthresh, double upthresh, double w, double p, double e, double r){ + for(int i = 0; i < _tgenearray.length; i++){ + _tgenearray[i].newPermutationRound(); + } + + setPermutationInconsistencyScores(_tgenearray, pathways, downthresh, upthresh, w, p); + setPermutationSupportScores(_tgenearray, pathways, downthresh, upthresh, w, p); + setPermutationConsistencyScores(_tgenearray, pathways); + setPermutationImpactScores(_tgenearray, pathways, downthresh, upthresh, p, r); + + setTriangulationScores(_tgenearray, pathways); + + } + + private void setPermutationInconsistencyScores(TriangulatedGene[] genes, Pathway[] pathways, double downthresh, double upthresh, double w, double p){ + for(int i = 0; i < genes.length; i++){ + //genes[i].updateInconsistencyPermutation(globalpathway.getId(), getInconsistencyScore(globalpathway, genes[i], downthresh, upthresh, w, p)); + for(Iterator it = _genepathways.get(genes[i].getGene()).iterator(); it.hasNext();){ + Pathway curpath = it.next(); + genes[i].updateInconsistencyPermutation(curpath.getId(), getInconsistencyScore(curpath, genes[i], downthresh, upthresh, w, p)); + } + } + } + + private void setPermutationSupportScores(TriangulatedGene[] genes, Pathway[] pathways, double downthresh, double upthresh, double w, double p){ + for(int i = 0; i < genes.length; i++){ + //genes[i].updateSupportPermutation(globalpathway.getId(), getSupportScore(globalpathway, genes[i], downthresh, upthresh, w, p)); + for(Iterator it = _genepathways.get(genes[i].getGene()).iterator(); it.hasNext();){ + Pathway curpath = it.next(); + genes[i].updateSupportPermutation(curpath.getId(), getSupportScore(curpath, genes[i], downthresh, upthresh, w, p)); + } + } + } + + private void setPermutationConsistencyScores(TriangulatedGene[] genes, Pathway[] pathways){ + for(int i = 0; i < genes.length; i++){ + for(Iterator it = _genepathways.get(genes[i].getGene()).iterator(); it.hasNext();){ + Pathway curpath = it.next(); + String pathwayid = curpath.getId(); + double support = genes[i].getSupportPermutation(pathwayid); + double inconsistency = genes[i].getInconsistencyPermutation(pathwayid); + genes[i].updateConsistencyPermutation(pathwayid, (support-inconsistency)); + } + } + } + + private void setPermutationImpactScores(TriangulatedGene[] genes, Pathway[] pathways, double downthresh, double upthresh, double p, double rate){ + //String gpathwayid = globalpathway.getId(); + for(int i = 0; i < genes.length; i++){ + //ImpactFunction f = new PermutationImpactFunction(); + //genes[i].updateImpactPermutation(gpathwayid, getImpactScore(genes[i], globalpathway, globalpathway, f, downthresh, upthresh, p1, p2, rate)); + for(Iterator it = _genepathways.get(genes[i].getGene()).iterator(); it.hasNext();){ + Pathway curpath = it.next(); + double impact = getImpactScore(genes[i], curpath, downthresh, upthresh, p, rate); + genes[i].updateImpactPermutation(curpath.getId(), impact); + } + } + } + + + private void setInconsistencyScores(TriangulatedGene[] genes, Pathway[] pathways, double downthresh, double upthresh, double w, double p){ + for(int i = 0; i < genes.length; i++){ + //genes[i].setInconsistencyScore(globalpathway.getId(), getInconsistencyScore(globalpathway, genes[i], downthresh, upthresh, w, p)); + for(Iterator it = _genepathways.get(genes[i].getGene()).iterator(); it.hasNext();){ + Pathway curpath = it.next(); + genes[i].setInconsistencyScore(curpath.getId(), getInconsistencyScore(curpath, genes[i], downthresh, upthresh, w, p)); + } + } + } + + private void setSupportScores(TriangulatedGene[] genes, Pathway[] pathways, double downthresh, double upthresh, double w, double p){ + for(int i = 0; i < genes.length; i++){ + //genes[i].setSupportScore(globalpathway.getId(), getSupportScore(globalpathway, genes[i], downthresh, upthresh, w, p)); + for(Iterator it = _genepathways.get(genes[i].getGene()).iterator(); it.hasNext();){ + Pathway curpath = it.next(); + genes[i].setSupportScore(curpath.getId(), getSupportScore(curpath, genes[i], downthresh, upthresh, w, p)); + } + } + } + + private void setConsistencyScores(TriangulatedGene[] genes, Pathway[] pathways){ + for(int i = 0; i < genes.length; i++){ + for(Iterator it = _genepathways.get(genes[i].getGene()).iterator(); it.hasNext();){ + Pathway curpath = it.next(); + String pathwayid = curpath.getId(); + double support = genes[i].getSupportScore(pathwayid); + double inconsistency = genes[i].getInconsistencyScore(pathwayid); + genes[i].setConsistencyScore(pathwayid, (support-inconsistency)); + } + } + } + + private void setImpactScores(TriangulatedGene[] genes, Pathway[] pathways, double downthresh, double upthresh, double p, double rate){ + //String gpathwayid = globalpathway.getId(); + for(int i = 0; i < genes.length; i++){ + //ImpactFunction f = new ScoreImpactFunction(); + //genes[i].setImpactScore(gpathwayid, getImpactScore(genes[i], globalpathway, f, downthresh, upthresh, p1, p2, rate)); + for(Iterator it = _genepathways.get(genes[i].getGene()).iterator(); it.hasNext();){ + Pathway curpath = it.next(); + double impact = getImpactScore(genes[i], curpath, downthresh, upthresh, p, rate); + genes[i].setImpactScore(curpath.getId(), impact); + } + } + } + + private void setTriangulationScores(TriangulatedGene[] genes, Pathway[] pathways){ + for(int i = 0; i < genes.length; i++){ + for(Iterator it = _genepathways.get(genes[i].getGene()).iterator(); it.hasNext();){ + Pathway curpath = it.next(); + String pathwayid = curpath.getId(); + double consistencyscore = genes[i].getConsistencyScore(pathwayid); + double consistency = genes[i].getConsistencyPValue(pathwayid); + double impact = genes[i].getImpactPValue(pathwayid); + + double stri = consistencyscore <= 0 ? 1 : consistency*0.5+impact*0.5; + double itri = consistencyscore >= 0 ? 1 :consistency*0.5+impact*0.5; + genes[i].setSupportTriangulationScore(pathwayid, stri); + genes[i].setInconsistentTriangulationScore(pathwayid, itri); + } + } + } + + private double getInconsistencyScore(Pathway pathway, TriangulatedGene gene, double downthresh, double upthresh, double w, double p){ + double score = 0; + Node n = pathway.getNode(gene.getGene()); + + if(n != null){ + List> edges = n.getEdges(); + for(Iterator> it = edges.iterator(); it.hasNext();){ + Edge next = it.next(); + if(next.isDirected()){ + Node src = next.getSource(); + if(!src.equals(n)){ + Node dest = next.getDestination(); + int type = next.getData().getType(); + double sv = src.getData().getValue(); + double dv = dest.getData().getValue(); + if(type == PathwayEdge.ACTIVATION){ + if(sv > upthresh && dv < downthresh){ + score += Math.pow(Math.abs(sv), p); + } + else if(sv < downthresh && dv > upthresh){ + score += w*Math.pow(Math.abs(sv), p); + } + } + else if(type == PathwayEdge.INHIBITION){ + if(sv > upthresh && dv > upthresh){ + score += Math.pow(Math.abs(sv), p); + } + else if(sv < downthresh && dv < downthresh){ + score += w*Math.pow(Math.abs(sv), p); + } + } + } + } + } + } + return score; + } + + + + private double getSupportScore(Pathway pathway, TriangulatedGene gene, double downthresh, double upthresh, double w, double p){ + double score = 0; + Node n = pathway.getNode(gene.getGene()); + + if(n != null){ + List> edges = n.getEdges(); + for(Iterator> it = edges.iterator(); it.hasNext();){ + Edge next = it.next(); + if(next.isDirected()){ + Node src = next.getSource(); + if(!src.equals(n)){ + Node dest = next.getDestination(); + int type = next.getData().getType(); + double sv = src.getData().getValue(); + double dv = dest.getData().getValue(); + if(type == PathwayEdge.ACTIVATION){ + if(sv > upthresh && dv > upthresh){ + score += Math.pow(Math.abs(sv), p); + } + else if(sv < downthresh && dv < downthresh){ + score += w*Math.pow(Math.abs(sv), p); + } + } + else if(type == PathwayEdge.INHIBITION){ + if(sv > upthresh && dv < downthresh){ + score += Math.pow(Math.abs(sv), p); + } + else if(sv < downthresh && dv > upthresh){ + score += w*Math.pow(Math.abs(sv), p); + } + } + } + } + } + } + return score; + } + + + + private double getImpactScore(TriangulatedGene g, Pathway pathway, double downthresh, double upthresh, double p, double rate){ + double score = 0; + + Node n = pathway.getNode(g.getGene()); + if(n == null){ + return 0; + } + LinkedList> todo = new LinkedList>(); + LinkedList todoi = new LinkedList(); + todo.addLast(n); + todoi.addLast(0); + + Set seen = new TreeSet(); + seen.add(n.getData()); + + PathwayHelper ph = new PathwayHelper(); + + while(!todo.isEmpty()){ + Node curnode = todo.removeFirst(); + Integer curi = todoi.removeFirst(); + List> downstreamedges = ph.getDownstreamEdges(curnode); + for(Iterator> it = downstreamedges.iterator(); it.hasNext();){ + Edge nextedge = it.next(); + Node dest = ph.getDestination(curnode,nextedge); + if(!seen.contains(dest.getData())){ + if(ph.isConsistent(nextedge, curnode, dest, downthresh, upthresh)){ + todo.add(dest); + todoi.add(curi+1); + seen.add(dest.getData()); + //double support = f.getSupportNormalization(_alltgenes.get(dest.getData()), globalpathway, pathway); + //score += Math.exp(-rate*curi)*(Math.pow(Math.abs(dest.getData().getValue()),p1) + // /Math.pow(Math.max(1, support), p2)); + score += Math.exp(-rate*curi)*(Math.pow(Math.abs(dest.getData().getValue()),p)); + } + /*else if(nextedge.getData().getType() != PathwayEdge.ASSOCIATION){ + //This allows for inconsistencies that have no further downstream impact + //Two inconsistencies identifies these nodes + List> iinodes = ph.getInconsistentInconsistencies(dest,seen,downthresh,upthresh); + for(Iterator> it2 = iinodes.iterator(); it2.hasNext();){ + Node next = it2.next(); + if(!seen.contains(next.getData())){ + todo.add(next); + todoi.add(curi+2); //This is looking ahead by one so we add 2. + seen.add(next.getData()); + //double support = f.getSupportNormalization(_alltgenes.get(next.getData()), globalpathway, pathway); + //score += Math.exp(-rate*(curi+1))*(Math.pow(Math.abs(next.getData().getValue()),p1) + // /Math.pow(Math.max(1, support), p2)); + score += Math.exp(-rate*curi)*(Math.pow(Math.abs(dest.getData().getValue()),p)); + } + } + + }*/ + } + } + } + + + return score; + } + +// private double getImpactScoreNormalizer(TriangulatedGene g, Pathway pathway, double downthresh, double upthresh, double p, double e, double rate){ +// double score = 0; +// +// Node n = pathway.getNode(g.getGene()); +// if(n == null){ +// return 0; +// } +// LinkedList> todo = new LinkedList>(); +// LinkedList todoi = new LinkedList(); +// todo.addLast(n); +// todoi.addLast(0); +// +// Set seen = new TreeSet(); +// seen.add(n.getData()); +// +// PathwayHelper ph = new PathwayHelper(); +// +// while(!todo.isEmpty()){ +// Node curnode = todo.removeFirst(); +// Integer curi = todoi.removeFirst(); +// List> downstreamedges = ph.getDownstreamEdges(curnode); +// for(Iterator> it = downstreamedges.iterator(); it.hasNext();){ +// Edge nextedge = it.next(); +// Node dest = ph.getDestination(curnode,nextedge); +// if(!seen.contains(dest.getData()) && nextedge.getData().getType() != PathwayEdge.ASSOCIATION){ +// todo.add(dest); +// todoi.add(curi+1); +// seen.add(dest.getData()); +// score += Math.exp(-rate*curi)*(Math.pow(Math.abs(dest.getData().getValue()),p)); +// } +// } +// } +// return score; +// } + + + //Interface to eliminate duplicate code when getting + //the support score normalization factor + /*private interface ImpactFunction { + + public double getSupportNormalization(TriangulatedGene g, Pathway global, Pathway local); + + } + + private class ScoreImpactFunction implements ImpactFunction{ + + @Override + public double getSupportNormalization(TriangulatedGene g, Pathway global, Pathway local) { + return g.getSupportScore(global.getId())-g.getSupportScore(local.getId()); + } + + } + + + private class PermutationImpactFunction implements ImpactFunction{ + + @Override + public double getSupportNormalization(TriangulatedGene g, Pathway global, Pathway local) { + return g.getSupportPermutation(global.getId())-g.getSupportPermutation(local.getId()); + } + + }*/ + + +}