private Interaction[] filterInteractions(Interaction[] interactions) { LinkedList<Interaction> l = new LinkedList<Interaction>(); for (int i = 0; i < interactions.length; i++) { Interaction inter = interactions[i]; if (inter.getEdge() != null) { l.add(inter); } } return l.toArray(new Interaction[0]); }
public ChIAPETNetwork getIntersection(Connection conn, long n1, long n2) throws Exception { _aid = 0; _iid = 0; InteractionData i1 = getAnchors(conn, n1); InteractionData i2 = getAnchors(conn, n2); TreeMap<Integer, LinkedList<Anchor>> n1anchors = i1.nodetoanchors; TreeMap<Integer, LinkedList<Anchor>> n2anchors = i2.nodetoanchors; String t1 = "chiapet.Nodes_" + n1; String t2 = "chiapet.Nodes_" + n2; String sql = "SELECT DISTINCT n1.id AS n1id, n2.id AS n2id FROM " + t1 + " AS n1, " + t2 + " AS n2 WHERE n1.chr=n2.chr AND n1.end >= n2.start AND n2.end >= n1.start"; PreparedStatement ps = conn.prepareStatement(sql); NodeIntersection ni = new NodeIntersection(); ResultSet rs = ps.executeQuery(); while (rs.next()) { int n1id = rs.getInt(1); int n2id = rs.getInt(2); ni.addIntersection(n1id, n2id); } rs.close(); ps.close(); TreeMap<Integer, Integer> c1 = ni.getN1EdgeMapping(); TreeMap<Integer, Integer> c2 = ni.getN2EdgeMapping(); TreeMap<Integer, Node> nodes = ni.getOuterNodes(n1anchors, n2anchors); int[] counts = ni.getTotalIntersectingNodes(); System.out.println("N1 Intersection: " + counts[0] + "N2 Intersection: " + counts[1]); String[] iedges = getIntersectionEdgeMap(conn, n1, toArray(c1), n2, toArray(c2)); LinkedList<Edge> edges = new LinkedList<Edge>(); int eid = 0; for (int i = 0; i < iedges.length; i++) { String cur = iedges[i]; String[] split = cur.split("_"); Node node1 = nodes.get(Integer.parseInt(split[0])); Node node2 = nodes.get(Integer.parseInt(split[1])); TreeMap<Integer, Interaction> interactions = new TreeMap<Integer, Interaction>(); Anchor[] a = node1.getAnchors(); for (int j = 0; j < a.length; j++) { Interaction inter = a[j].getInteraction(); interactions.put(inter.getId(), inter); } a = node2.getAnchors(); for (int j = 0; j < a.length; j++) { Interaction inter = a[j].getInteraction(); interactions.put(inter.getId(), inter); } LinkedList<Interaction> edgeinteractions = new LinkedList<Interaction>(); for (Iterator<Interaction> it = interactions.values().iterator(); it.hasNext(); ) { Interaction next = it.next(); Anchor[] ia = next.getAnchors(); Node a0n = ia[0].getNode(); Node a1n = ia[1].getNode(); if (a0n != null && a0n.equals(node1)) { if (a1n != null && a1n.equals(node2)) { edgeinteractions.add(next); } } else if (a0n != null && a0n.equals(node2)) { if (a1n != null && a1n.equals(node1)) { edgeinteractions.add(next); } } } if (edgeinteractions.size() > 1) { Edge e = new Edge(eid++, node1, node2, edgeinteractions.toArray(new Interaction[0])); for (Iterator<Interaction> it = edgeinteractions.iterator(); it.hasNext(); ) { it.next().setEdge(e); } edges.add(e); node1.addEdge(e); node2.addEdge(e); } else { throw new Exception("Expecting at least two interactions."); } } i1.anchors.addAll(i2.anchors); Anchor[] allanchors = i1.anchors.toArray(new Anchor[0]); i1.interactions.addAll(i2.interactions); Interaction[] allinteractions = i1.interactions.toArray(new Interaction[0]); Node[] nodearray = nodes.values().toArray(new Node[0]); Util u = new Util(); nodearray = u.filterNodes(nodearray); ConnectedComponentExtraction cce = new ConnectedComponentExtraction(); ConnectedComponent[] components = cce.getCCs(nodearray, allinteractions.length + 1, 0, 0); return new ChIAPETNetwork( filterAnchors(allanchors), filterInteractions(allinteractions), nodearray, edges.toArray(new Edge[0]), components, new NetworkParameters(-1, -1, -1, -1, -1, -1, -1, -1)); }