diff --git a/java/src/org/broadinstitute/sting/gatk/walkers/phasing/DoublyLinkedList.java b/java/src/org/broadinstitute/sting/gatk/walkers/phasing/CloneableIteratorLinkedList.java similarity index 71% rename from java/src/org/broadinstitute/sting/gatk/walkers/phasing/DoublyLinkedList.java rename to java/src/org/broadinstitute/sting/gatk/walkers/phasing/CloneableIteratorLinkedList.java index d90544460..4ec940f4f 100644 --- a/java/src/org/broadinstitute/sting/gatk/walkers/phasing/DoublyLinkedList.java +++ b/java/src/org/broadinstitute/sting/gatk/walkers/phasing/CloneableIteratorLinkedList.java @@ -25,12 +25,17 @@ package org.broadinstitute.sting.gatk.walkers.phasing; import java.util.NoSuchElementException; -public class DoublyLinkedList { - private DoublyLinkedNode first; - private DoublyLinkedNode last; +/* + DoublyLinkedList class is a doubly-linked list, which allows O(1) traversal to next and previous elements in the list. + It is UNIQUE in the fact that its iterator (BidirectionalIterator) can be cloned + to save the current pointer for a later time (while the original iterator can continue to iterate). + */ +public class CloneableIteratorLinkedList { + private CloneableIteratorDoublyLinkedNode first; + private CloneableIteratorDoublyLinkedNode last; private int size; - public DoublyLinkedList() { + public CloneableIteratorLinkedList() { this.first = null; this.last = null; this.size = 0; @@ -45,7 +50,7 @@ public class DoublyLinkedList { } public void addFirst(E e) { - DoublyLinkedNode newNode = new DoublyLinkedNode(e); + CloneableIteratorDoublyLinkedNode newNode = new CloneableIteratorDoublyLinkedNode(e); if (isEmpty()) last = newNode; @@ -59,7 +64,7 @@ public class DoublyLinkedList { } public void addLast(E e) { - DoublyLinkedNode newNode = new DoublyLinkedNode(e); + CloneableIteratorDoublyLinkedNode newNode = new CloneableIteratorDoublyLinkedNode(e); if (isEmpty()) first = newNode; @@ -132,17 +137,17 @@ public class DoublyLinkedList { return true; } - public BidirectionalIterator iterator() { - return new BidirectionalIterator(this); + public CloneableIterator iterator() { + return new CloneableIterator(this); } - private static class DoublyLinkedNode { + private static class CloneableIteratorDoublyLinkedNode { private E element = null; - private DoublyLinkedNode next = null; - private DoublyLinkedNode previous = null; + private CloneableIteratorDoublyLinkedNode next = null; + private CloneableIteratorDoublyLinkedNode previous = null; - public DoublyLinkedNode(E element) { + public CloneableIteratorDoublyLinkedNode(E element) { this.element = element; this.next = null; this.previous = null; @@ -150,16 +155,19 @@ public class DoublyLinkedList { } - public static class BidirectionalIterator implements Cloneable { - private DoublyLinkedNode nextNode; - private DoublyLinkedNode lastNode; + /* + This iterator is unique since it can be cloned to save the current pointer for a later time (while the original iterator can continue to iterate). + */ + public static class CloneableIterator implements Cloneable { + private CloneableIteratorDoublyLinkedNode nextNode; + private CloneableIteratorDoublyLinkedNode lastNode; - private BidirectionalIterator(DoublyLinkedNode nextNode, DoublyLinkedNode lastNode) { + private CloneableIterator(CloneableIteratorDoublyLinkedNode nextNode, CloneableIteratorDoublyLinkedNode lastNode) { this.nextNode = nextNode; this.lastNode = lastNode; } - private BidirectionalIterator(DoublyLinkedList list) { + private CloneableIterator(CloneableIteratorLinkedList list) { this(list.first, list.last); } @@ -195,13 +203,13 @@ public class DoublyLinkedList { return nextNode.element; } - public BidirectionalIterator clone() { + public CloneableIterator clone() { try { super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } - return new BidirectionalIterator(nextNode, lastNode); + return new CloneableIterator(nextNode, lastNode); } } } diff --git a/java/src/org/broadinstitute/sting/gatk/walkers/phasing/Graph.java b/java/src/org/broadinstitute/sting/gatk/walkers/phasing/PhasingGraph.java similarity index 73% rename from java/src/org/broadinstitute/sting/gatk/walkers/phasing/Graph.java rename to java/src/org/broadinstitute/sting/gatk/walkers/phasing/PhasingGraph.java index c15993416..fe2792475 100644 --- a/java/src/org/broadinstitute/sting/gatk/walkers/phasing/Graph.java +++ b/java/src/org/broadinstitute/sting/gatk/walkers/phasing/PhasingGraph.java @@ -28,16 +28,16 @@ import org.broadinstitute.sting.utils.DisjointSet; import java.util.*; // Represents an undirected graph with no self-edges: -public class Graph implements Iterable { +public class PhasingGraph implements Iterable { private Neighbors[] adj; - public Graph(int numVertices) { + public PhasingGraph(int numVertices) { adj = new Neighbors[numVertices]; for (int i = 0; i < numVertices; i++) adj[i] = new Neighbors(); } - public void addEdge(GraphEdge e) { + public void addEdge(PhasingGraphEdge e) { if (e.v1 == e.v2) // do not permit self-edges return; @@ -45,20 +45,20 @@ public class Graph implements Iterable { adj[e.v2].addNeighbor(e); } - public void addEdges(Collection edges) { - for (GraphEdge e : edges) + public void addEdges(Collection edges) { + for (PhasingGraphEdge e : edges) addEdge(e); } - public void removeEdge(GraphEdge e) { + public void removeEdge(PhasingGraphEdge e) { adj[e.v1].removeNeighbor(e); adj[e.v2].removeNeighbor(e); } - public Collection removeAllIncidentEdges(int vertexIndex) { - Collection incidentEdges = new TreeSet(adj[vertexIndex].neighbors); // implemented GraphEdge.compareTo() + public Collection removeAllIncidentEdges(int vertexIndex) { + Collection incidentEdges = new TreeSet(adj[vertexIndex].neighbors); // implemented GraphEdge.compareTo() - for (GraphEdge neighbEdge : incidentEdges) { + for (PhasingGraphEdge neighbEdge : incidentEdges) { if (vertexIndex != neighbEdge.v1) // vertexIndex == neighbEdge.v2 adj[neighbEdge.v1].removeNeighbor(neighbEdge); else if (vertexIndex != neighbEdge.v2) // vertexIndex == neighbEdge.v1 @@ -72,13 +72,13 @@ public class Graph implements Iterable { public DisjointSet getConnectedComponents() { DisjointSet cc = new DisjointSet(adj.length); - for (GraphEdge e : this) + for (PhasingGraphEdge e : this) cc.setUnion(e.v1, e.v2); return cc; } - public Iterator iterator() { + public Iterator iterator() { return new AllEdgesIterator(); } @@ -87,7 +87,7 @@ public class Graph implements Iterable { for (int i = 0; i < adj.length; i++) { sb.append(i + ":"); - for (GraphEdge e : adj[i]) { + for (PhasingGraphEdge e : adj[i]) { sb.append(" " + (e.v1 == i ? e.v2 : e.v1)); } sb.append("\n"); @@ -96,10 +96,10 @@ public class Graph implements Iterable { return sb.toString(); } - private class AllEdgesIterator implements Iterator { + private class AllEdgesIterator implements Iterator { private int curInd; - private Iterator innerIt; - private GraphEdge nextEdge; + private Iterator innerIt; + private PhasingGraphEdge nextEdge; public AllEdgesIterator() { curInd = 0; @@ -116,7 +116,7 @@ public class Graph implements Iterable { innerIt = adj[curInd].iterator(); while (innerIt.hasNext()) { - GraphEdge e = innerIt.next(); + PhasingGraphEdge e = innerIt.next(); if (e.v1 == curInd) { // only want to see each edge once nextEdge = e; return true; @@ -129,11 +129,11 @@ public class Graph implements Iterable { return false; } - public GraphEdge next() { + public PhasingGraphEdge next() { if (!hasNext()) throw new NoSuchElementException(); - GraphEdge tmpEdge = nextEdge; + PhasingGraphEdge tmpEdge = nextEdge; nextEdge = null; return tmpEdge; } @@ -143,22 +143,22 @@ public class Graph implements Iterable { } } - private class Neighbors implements Iterable { - private Set neighbors; + private class Neighbors implements Iterable { + private Set neighbors; public Neighbors() { - this.neighbors = new TreeSet(); // implemented GraphEdge.compareTo() + this.neighbors = new TreeSet(); // implemented GraphEdge.compareTo() } - public void addNeighbor(GraphEdge e) { + public void addNeighbor(PhasingGraphEdge e) { neighbors.add(e); } - public void removeNeighbor(GraphEdge e) { + public void removeNeighbor(PhasingGraphEdge e) { neighbors.remove(e); } - public Iterator iterator() { + public Iterator iterator() { return neighbors.iterator(); } diff --git a/java/src/org/broadinstitute/sting/gatk/walkers/phasing/GraphEdge.java b/java/src/org/broadinstitute/sting/gatk/walkers/phasing/PhasingGraphEdge.java similarity index 87% rename from java/src/org/broadinstitute/sting/gatk/walkers/phasing/GraphEdge.java rename to java/src/org/broadinstitute/sting/gatk/walkers/phasing/PhasingGraphEdge.java index 568195236..56197a85f 100644 --- a/java/src/org/broadinstitute/sting/gatk/walkers/phasing/GraphEdge.java +++ b/java/src/org/broadinstitute/sting/gatk/walkers/phasing/PhasingGraphEdge.java @@ -23,11 +23,14 @@ */ package org.broadinstitute.sting.gatk.walkers.phasing; -public class GraphEdge implements Comparable { +/* + Edge class for PhasingGraph + */ +public class PhasingGraphEdge implements Comparable { protected int v1; protected int v2; - public GraphEdge(int v1, int v2) { + public PhasingGraphEdge(int v1, int v2) { this.v1 = v1; this.v2 = v2; } @@ -40,7 +43,7 @@ public class GraphEdge implements Comparable { return v2; } - public int compareTo(GraphEdge that) { + public int compareTo(PhasingGraphEdge that) { if (this.v1 != that.v1) return (this.v1 - that.v1); @@ -48,7 +51,7 @@ public class GraphEdge implements Comparable { return (this.v2 - that.v2); } - public boolean equals(GraphEdge other) { + public boolean equals(PhasingGraphEdge other) { return (this.compareTo(other) == 0); } diff --git a/java/src/org/broadinstitute/sting/gatk/walkers/phasing/ReadBackedPhasingWalker.java b/java/src/org/broadinstitute/sting/gatk/walkers/phasing/ReadBackedPhasingWalker.java index 050d99a8c..917871d37 100755 --- a/java/src/org/broadinstitute/sting/gatk/walkers/phasing/ReadBackedPhasingWalker.java +++ b/java/src/org/broadinstitute/sting/gatk/walkers/phasing/ReadBackedPhasingWalker.java @@ -93,7 +93,7 @@ public class ReadBackedPhasingWalker extends RodWalker unphasedSiteQueue = null; - private DoublyLinkedList partiallyPhasedSites = null; // the phased VCs to be emitted, and the alignment bases at these positions + private CloneableIteratorLinkedList partiallyPhasedSites = null; // the phased VCs to be emitted, and the alignment bases at these positions private static PreciseNonNegativeDouble ZERO = new PreciseNonNegativeDouble(0.0); @@ -135,7 +135,7 @@ public class ReadBackedPhasingWalker extends RodWalker(); - partiallyPhasedSites = new DoublyLinkedList(); + partiallyPhasedSites = new CloneableIteratorLinkedList(); initializeVcfWriter(); @@ -340,7 +340,7 @@ public class ReadBackedPhasingWalker extends RodWalker prevHetAndInteriorIt = phaseWindow.prevHetAndInteriorIt; + CloneableIteratorLinkedList.CloneableIterator prevHetAndInteriorIt = phaseWindow.prevHetAndInteriorIt; /* Notes: 1. Call to next() advances iterator to next position in partiallyPhasedSites. 2. prevHetGenotype != null, since otherwise prevHetAndInteriorIt would not have been chosen to point to its UnfinishedVariantAndReads. @@ -438,7 +438,7 @@ public class ReadBackedPhasingWalker extends RodWalker prevHetAndInteriorIt = null; + private CloneableIteratorLinkedList.CloneableIterator prevHetAndInteriorIt = null; private int phasingSiteIndex = -1; private Map readsAtHetSites = null; @@ -452,7 +452,7 @@ public class ReadBackedPhasingWalker extends RodWalker listHetGenotypes = new LinkedList(); // Include previously phased sites in the phasing computation: - DoublyLinkedList.BidirectionalIterator phasedIt = partiallyPhasedSites.iterator(); + CloneableIteratorLinkedList.CloneableIterator phasedIt = partiallyPhasedSites.iterator(); while (phasedIt.hasNext()) { UnfinishedVariantAndReads phasedVr = phasedIt.next(); Genotype gt = phasedVr.unfinishedVariant.getGenotype(sample); @@ -577,13 +577,13 @@ public class ReadBackedPhasingWalker extends RodWalker> edgeReads; + private TreeMap> edgeReads; public EdgeToReads() { - this.edgeReads = new TreeMap>(); // implemented GraphEdge.compareTo() + this.edgeReads = new TreeMap>(); // implemented GraphEdge.compareTo() } - public void addRead(GraphEdge e, String readName) { + public void addRead(PhasingGraphEdge e, String readName) { List reads = edgeReads.get(e); if (reads == null) { reads = new LinkedList(); @@ -592,7 +592,7 @@ public class ReadBackedPhasingWalker extends RodWalker getReads(GraphEdge e) { + public List getReads(PhasingGraphEdge e) { return edgeReads.get(e); } } @@ -622,7 +622,7 @@ public class ReadBackedPhasingWalker extends RodWalker removeExtraneousReads(int numHetSites) { - Graph readGraph = new Graph(numHetSites); + PhasingGraph readGraph = new PhasingGraph(numHetSites); EdgeToReads edgeToReads = new EdgeToReads(); Set sitesWithEdges = new TreeSet(); @@ -634,7 +634,7 @@ public class ReadBackedPhasingWalker extends RodWalker removedEdges = readGraph.removeAllIncidentEdges(i); + Collection removedEdges = readGraph.removeAllIncidentEdges(i); // Run-time for efficiently calculating connected components using DisjointSet: O(E) DisjointSet ccAfterRemove = readGraph.getConnectedComponents(); @@ -727,7 +727,7 @@ public class ReadBackedPhasingWalker extends RodWalker