Restore folder

git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@4370 348d0f76-0448-11de-a6fe-93d51630548a
This commit is contained in:
fromer 2010-09-28 23:46:07 +00:00
parent 7c909bef82
commit dfb5143a41
7 changed files with 2334 additions and 0 deletions

View File

@ -0,0 +1,72 @@
/*
* Copyright (c) 2010, The Broad Institute
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
package org.broadinstitute.sting.playground.gatk.walkers.phasing;
import org.broad.tribble.util.variantcontext.Allele;
import org.broad.tribble.util.variantcontext.Genotype;
import org.broadinstitute.sting.utils.exceptions.ReviewedStingException;
import java.util.ArrayList;
import java.util.List;
public class AllelePair {
private Allele top;
private Allele bottom;
public AllelePair(Genotype gt) {
if (gt.getPloidy() != 2)
throw new ReviewedStingException("AllelePair must have ploidy of 2!");
this.top = gt.getAllele(0);
this.bottom = gt.getAllele(1);
}
public Allele getTopAllele() {
return top;
}
public Allele getBottomAllele() {
return bottom;
}
public void swapAlleles() {
Allele tmp = top;
top = bottom;
bottom = tmp;
}
public List<Allele> getAllelesAsList() {
List<Allele> allList = new ArrayList<Allele>(2);
allList.add(0, top);
allList.add(1, bottom);
return allList;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Top:\t" + top.getBaseString() + "\n");
sb.append("Bot:\t" + bottom.getBaseString() + "\n");
return sb.toString();
}
}

View File

@ -0,0 +1,101 @@
package org.broadinstitute.sting.playground.gatk.walkers.phasing;
import java.util.*;
/*
* Copyright (c) 2010, The Broad Institute
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
public class DisjointSet {
private ItemNode[] nodes;
public DisjointSet(int numItems) {
this.nodes = new ItemNode[numItems];
for (int i = 0; i < numItems; i++)
this.nodes[i] = new ItemNode(i);
}
public int findSet(int itemIndex) {
// Find itemIndex's root Node:
ItemNode curNode = nodes[itemIndex];
while (curNode.parent != null)
curNode = curNode.parent;
ItemNode root = curNode;
// Perform path compression:
curNode = nodes[itemIndex];
while (curNode != root) {
ItemNode next = curNode.parent;
curNode.parent = root;
curNode = next;
}
return root.itemIndex;
}
public boolean inSameSet(int x, int y) {
return (x == y || nodes[x].parent == nodes[y].parent || findSet(x) == findSet(y));
}
public Set<Integer> inSameSetAs(int x, Collection<Integer> testSet) {
Set<Integer> sameSetInds = new TreeSet<Integer>();
int xSet = findSet(x);
for (int t : testSet) {
if (findSet(t) == xSet)
sameSetInds.add(t);
}
return sameSetInds;
}
public void setUnion(int x, int y) {
link(findSet(x), findSet(y));
}
private void link(int x, int y) {
if (x == y)
return;
// Union by rank:
if (nodes[x].rank > nodes[y].rank) {
nodes[y].parent = nodes[x];
}
else { // nodes[x].rank <= nodes[y].rank
nodes[x].parent = nodes[y];
if (nodes[x].rank == nodes[y].rank)
nodes[y].rank++;
}
}
private class ItemNode {
private int itemIndex;
private ItemNode parent;
private int rank;
public ItemNode(int itemIndex) {
this.itemIndex = itemIndex;
this.parent = null;
this.rank = 0;
}
}
}

View File

@ -0,0 +1,208 @@
package org.broadinstitute.sting.playground.gatk.walkers.phasing;
import java.util.NoSuchElementException;
/*
* Copyright (c) 2010, The Broad Institute
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
public class DoublyLinkedList<E> {
private DoublyLinkedNode<E> first;
private DoublyLinkedNode<E> last;
private int size;
public DoublyLinkedList() {
this.first = null;
this.last = null;
this.size = 0;
}
public boolean isEmpty() {
return first == null;
}
public int size() {
return size;
}
public void addFirst(E e) {
DoublyLinkedNode<E> newNode = new DoublyLinkedNode<E>(e);
if (isEmpty())
last = newNode;
else {
first.previous = newNode;
newNode.next = first;
}
first = newNode;
size++;
}
public void addLast(E e) {
DoublyLinkedNode<E> newNode = new DoublyLinkedNode<E>(e);
if (isEmpty())
first = newNode;
else {
last.next = newNode;
newNode.previous = last;
}
last = newNode;
size++;
}
public E removeFirst() {
if (isEmpty())
throw new NoSuchElementException();
E e = first.element;
if (first.next == null)
last = null;
else
first.next.previous = null;
first = first.next;
size--;
return e;
}
public E removeLast() {
if (isEmpty())
throw new NoSuchElementException();
E e = last.element;
if (last.previous == null)
first = null;
else
last.previous.next = null;
last = last.previous;
size--;
return e;
}
public E getFirst() {
if (isEmpty())
throw new NoSuchElementException();
return first.element;
}
public E getLast() {
if (isEmpty())
throw new NoSuchElementException();
return last.element;
}
public E peek() {
if (isEmpty())
return null;
return getFirst();
}
public E remove() {
return removeFirst();
}
public boolean add(E e) {
addLast(e);
return true;
}
public BidirectionalIterator<E> iterator() {
return new BidirectionalIterator<E>(this);
}
private static class DoublyLinkedNode<E> {
private E element = null;
private DoublyLinkedNode<E> next = null;
private DoublyLinkedNode<E> previous = null;
public DoublyLinkedNode(E element) {
this.element = element;
this.next = null;
this.previous = null;
}
}
public static class BidirectionalIterator<E> implements Cloneable {
private DoublyLinkedNode<E> nextNode;
private DoublyLinkedNode<E> lastNode;
private BidirectionalIterator(DoublyLinkedNode<E> nextNode, DoublyLinkedNode<E> lastNode) {
this.nextNode = nextNode;
this.lastNode = lastNode;
}
private BidirectionalIterator(DoublyLinkedList<E> list) {
this(list.first, list.last);
}
public boolean hasNext() {
return nextNode != null;
}
public E next() {
if (!hasNext())
throw new NoSuchElementException();
E e = nextNode.element;
nextNode = nextNode.next;
return e;
}
public boolean hasPrevious() {
if (nextNode != null)
return nextNode.previous != null;
return lastNode != null;
}
public E previous() {
if (!hasPrevious())
throw new NoSuchElementException();
if (nextNode != null)
nextNode = nextNode.previous;
else
nextNode = lastNode;
return nextNode.element;
}
public BidirectionalIterator<E> clone() {
try {
super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
return new BidirectionalIterator<E>(nextNode, lastNode);
}
}
}

View File

@ -0,0 +1,168 @@
package org.broadinstitute.sting.playground.gatk.walkers.phasing;
import java.util.*;
/*
* Copyright (c) 2010, The Broad Institute
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
// Represents an undirected graph with no self-edges:
public class Graph implements Iterable<GraphEdge> {
private Neighbors[] adj;
public Graph(int numVertices) {
adj = new Neighbors[numVertices];
for (int i = 0; i < numVertices; i++)
adj[i] = new Neighbors();
}
public void addEdge(GraphEdge e) {
if (e.v1 == e.v2) // do not permit self-edges
return;
adj[e.v1].addNeighbor(e);
adj[e.v2].addNeighbor(e);
}
public void addEdges(Collection<GraphEdge> edges) {
for (GraphEdge e : edges)
addEdge(e);
}
public void removeEdge(GraphEdge e) {
adj[e.v1].removeNeighbor(e);
adj[e.v2].removeNeighbor(e);
}
public Collection<GraphEdge> removeAllIncidentEdges(int vertexIndex) {
Collection<GraphEdge> incidentEdges = new TreeSet<GraphEdge>(adj[vertexIndex].neighbors); // implemented GraphEdge.compareTo()
for (GraphEdge neighbEdge : incidentEdges) {
if (vertexIndex != neighbEdge.v1) // vertexIndex == neighbEdge.v2
adj[neighbEdge.v1].removeNeighbor(neighbEdge);
else if (vertexIndex != neighbEdge.v2) // vertexIndex == neighbEdge.v1
adj[neighbEdge.v2].removeNeighbor(neighbEdge);
}
adj[vertexIndex].clearAllNeighbors();
return incidentEdges;
}
public DisjointSet getConnectedComponents() {
DisjointSet cc = new DisjointSet(adj.length);
for (GraphEdge e : this)
cc.setUnion(e.v1, e.v2);
return cc;
}
public Iterator<GraphEdge> iterator() {
return new AllEdgesIterator();
}
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < adj.length; i++) {
sb.append(i + ":");
for (GraphEdge e : adj[i]) {
sb.append(" " + (e.v1 == i ? e.v2 : e.v1));
}
sb.append("\n");
}
return sb.toString();
}
private class AllEdgesIterator implements Iterator<GraphEdge> {
private int curInd;
private Iterator<GraphEdge> innerIt;
private GraphEdge nextEdge;
public AllEdgesIterator() {
curInd = 0;
innerIt = null;
nextEdge = null;
}
public boolean hasNext() {
if (nextEdge != null)
return true;
for (; curInd < adj.length; curInd++) {
if (innerIt == null)
innerIt = adj[curInd].iterator();
while (innerIt.hasNext()) {
GraphEdge e = innerIt.next();
if (e.v1 == curInd) { // only want to see each edge once
nextEdge = e;
return true;
}
}
innerIt = null;
}
return false;
}
public GraphEdge next() {
if (!hasNext())
throw new NoSuchElementException();
GraphEdge tmpEdge = nextEdge;
nextEdge = null;
return tmpEdge;
}
public void remove() {
throw new UnsupportedOperationException();
}
}
private class Neighbors implements Iterable<GraphEdge> {
private Set<GraphEdge> neighbors;
public Neighbors() {
this.neighbors = new TreeSet<GraphEdge>(); // implemented GraphEdge.compareTo()
}
public void addNeighbor(GraphEdge e) {
neighbors.add(e);
}
public void removeNeighbor(GraphEdge e) {
neighbors.remove(e);
}
public Iterator<GraphEdge> iterator() {
return neighbors.iterator();
}
public void clearAllNeighbors() {
neighbors.clear();
}
}
}

View File

@ -0,0 +1,61 @@
package org.broadinstitute.sting.playground.gatk.walkers.phasing;
/*
* Copyright (c) 2010, The Broad Institute
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
public class GraphEdge implements Comparable<GraphEdge> {
protected int v1;
protected int v2;
public GraphEdge(int v1, int v2) {
this.v1 = v1;
this.v2 = v2;
}
public int getV1() {
return v1;
}
public int getV2() {
return v2;
}
public int compareTo(GraphEdge that) {
if (this.v1 != that.v1)
return (this.v1 - that.v1);
// this.v1 == that.v1:
return (this.v2 - that.v2);
}
public boolean equals(GraphEdge other) {
return (this.compareTo(other) == 0);
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("(").append(v1).append(", ").append(v2).append(")");
return sb.toString();
}
}

View File

@ -0,0 +1,75 @@
/*
* Copyright (c) 2010, The Broad Institute
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
package org.broadinstitute.sting.playground.gatk.walkers.phasing;
import org.broad.tribble.util.variantcontext.Allele;
import org.broad.tribble.util.variantcontext.Genotype;
import org.broadinstitute.sting.utils.BaseUtils;
import org.broadinstitute.sting.utils.exceptions.ReviewedStingException;
public class SNPallelePair extends AllelePair {
public SNPallelePair(Genotype gt) {
super(gt);
if (getTopAllele().getBases().length != 1)
throw new ReviewedStingException("LOGICAL ERROR: SNPallelePair may not contain non-SNP site!");
if (getBottomAllele().getBases().length != 1)
throw new ReviewedStingException("LOGICAL ERROR: SNPallelePair may not contain non-SNP site!");
}
public byte getTopBase() {
byte[] topBases = getTopAllele().getBases();
return getSingleBase(topBases);
}
public byte getBottomBase() {
byte[] bottomBases = getBottomAllele().getBases();
return getSingleBase(bottomBases);
}
public boolean matchesTopBase(byte base) {
return BaseUtils.basesAreEqual(base, getTopBase());
}
public byte getOtherBase(byte base) {
byte topBase = getTopBase();
byte botBase = getBottomBase();
if (BaseUtils.basesAreEqual(base, topBase))
return botBase;
else if (BaseUtils.basesAreEqual(base, botBase))
return topBase;
else
throw new ReviewedStingException("LOGICAL ERROR: base MUST match either TOP or BOTTOM!");
}
public static byte getSingleBase(byte[] bases) {
return bases[0];
}
public static byte getSingleBase(Allele all) {
return getSingleBase(all.getBases());
}
}