Added lots of assertions to check for problems. The current GenomeLoc needs to be cleaned up and refactored but at least it runs. We need unit tests ASAP

git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@392 348d0f76-0448-11de-a6fe-93d51630548a
This commit is contained in:
depristo 2009-04-14 00:53:08 +00:00
parent bb666ce392
commit 11377ef390
1 changed files with 34 additions and 19 deletions

View File

@ -46,12 +46,20 @@ public class GenomeLoc implements Comparable<GenomeLoc> {
return contigInfo != null; return contigInfo != null;
} }
public static GenomeLoc getFirstLocation()
{
assert contigInfo != null && contigInfo.size() != 0;
return new GenomeLoc(contigInfo.getSequence(0).getSequenceName(), 0, 0);
}
public static SAMSequenceRecord getContigInfo( final String contig ) { public static SAMSequenceRecord getContigInfo( final String contig ) {
return contigInfo.getSequence(contig); return contigInfo.getSequence(contig);
} }
public static int getContigIndex( final String contig ) { public static int getContigIndex( final String contig ) {
// if the conig isn't found, a null is returned which will NPE here (which is what we want right now) assert contigInfo.getSequenceIndex(contig) != -1;
return contigInfo.getSequenceIndex(contig); return contigInfo.getSequenceIndex(contig);
} }
@ -96,6 +104,7 @@ public class GenomeLoc implements Comparable<GenomeLoc> {
public static boolean setupRefContigOrdering(final SAMSequenceDictionary seqDict) { public static boolean setupRefContigOrdering(final SAMSequenceDictionary seqDict) {
if (seqDict == null) { // we couldn't load the reference dictionary if (seqDict == null) { // we couldn't load the reference dictionary
logger.info("Failed to load reference dictionary, falling back to lexicographic order for contigs"); logger.info("Failed to load reference dictionary, falling back to lexicographic order for contigs");
Utils.scareUser("Failed to load reference dictionary");
return false; return false;
} else { } else {
contigInfo = seqDict; contigInfo = seqDict;
@ -114,18 +123,26 @@ public class GenomeLoc implements Comparable<GenomeLoc> {
// //
// -------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------
public GenomeLoc( int contigIndex, final long start, final long stop ) { public GenomeLoc( int contigIndex, final long start, final long stop ) {
assert contigInfo != null;
assert contigIndex >= 0 && contigIndex < contigInfo.size() : "ContigIndex " + contigIndex + " is bad " + contigInfo.size();
assert start >= 0;
assert stop >= 0;
this.contigIndex = contigIndex; this.contigIndex = contigIndex;
this.start = start; this.start = start;
this.stop = stop; this.stop = stop;
} }
public GenomeLoc( String contig, final long start, final long stop ) { public GenomeLoc(final SAMRecord read) {
this(read.getReferenceIndex(), read.getAlignmentStart(), read.getAlignmentEnd());
}
public GenomeLoc( final String contig, final long start, final long stop ) {
this(contigInfo.getSequenceIndex(contig), start, stop); this(contigInfo.getSequenceIndex(contig), start, stop);
} }
public GenomeLoc( final String contig, final long pos ) { public GenomeLoc( final String contig, final long pos ) {
this(contig, pos, pos ); this(contig, pos, pos);
} }
public GenomeLoc( final int contig, final long pos ) { public GenomeLoc( final int contig, final long pos ) {
@ -136,11 +153,6 @@ public class GenomeLoc implements Comparable<GenomeLoc> {
this( toCopy.contigIndex, toCopy.getStart(), toCopy.getStop() ); this( toCopy.contigIndex, toCopy.getStart(), toCopy.getStop() );
} }
// TODO: why isn't this just a constructor?
public static GenomeLoc genomicLocationOf(final SAMRecord read) {
return new GenomeLoc(read.getReferenceIndex(), read.getAlignmentStart(), read.getAlignmentEnd());
}
// -------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------
// //
// Parsing string representations // Parsing string representations
@ -326,11 +338,15 @@ public class GenomeLoc implements Comparable<GenomeLoc> {
// Accessors and setters // Accessors and setters
// //
public final String getContig() { public final String getContig() {
if (contigInfo != null && contigInfo.getSequence(this.contigIndex) != null) { assert this.contigIndex != -1;
return contigInfo.getSequence(this.contigIndex).getSequenceName();
} assert contigInfo.getSequence(this.contigIndex).getSequenceName() != null;
return contigInfo.getSequence(this.contigIndex).getSequenceName();
//if (contigInfo != null && contigInfo.getSequence(this.contigIndex) != null) {
// return contigInfo.getSequence(this.contigIndex).getSequenceName();
//}
return null; //return null;
} }
public final int getContigIndex() { return this.contigIndex; } public final int getContigIndex() { return this.contigIndex; }
@ -345,7 +361,6 @@ public class GenomeLoc implements Comparable<GenomeLoc> {
return String.format("%s:%d-%d", getContig(), getStart(), getStop()); return String.format("%s:%d-%d", getContig(), getStart(), getStop());
} }
public final boolean isUnmapped() { return this.contigIndex == SAMRecord.NO_ALIGNMENT_REFERENCE_INDEX; } public final boolean isUnmapped() { return this.contigIndex == SAMRecord.NO_ALIGNMENT_REFERENCE_INDEX; }
public final boolean throughEndOfContigP() { return this.stop == Integer.MAX_VALUE; } public final boolean throughEndOfContigP() { return this.stop == Integer.MAX_VALUE; }
public final boolean atBeginningOfContigP() { return this.start == 1; } public final boolean atBeginningOfContigP() { return this.start == 1; }
@ -439,7 +454,8 @@ public class GenomeLoc implements Comparable<GenomeLoc> {
// Comparison operations // Comparison operations
// //
// TODO: get rid of this method because it's sloooooooooooooow // TODO: get rid of this method because it's sloooooooooooooow
public static int compareContigs( final String thisContig, final String thatContig ) @Deprecated
public static int compareContigs( final String thisContig, final String thatContig )
{ {
if ( thisContig == thatContig ) if ( thisContig == thatContig )
{ {
@ -450,7 +466,7 @@ public class GenomeLoc implements Comparable<GenomeLoc> {
//assert getContigIndex(thisContig) != -1;// : this; //assert getContigIndex(thisContig) != -1;// : this;
//assert getContigIndex(thatContig) != -1;// : that; //assert getContigIndex(thatContig) != -1;// : that;
if ( hasKnownContigOrdering() ) if ( hasKnownContigOrdering() )
{ {
int thisIndex = getContigIndex(thisContig); int thisIndex = getContigIndex(thisContig);
int thatIndex = getContigIndex(thatContig); int thatIndex = getContigIndex(thatContig);
@ -462,7 +478,7 @@ public class GenomeLoc implements Comparable<GenomeLoc> {
// Use regular sorted order // Use regular sorted order
return thisContig.compareTo(thatContig); return thisContig.compareTo(thatContig);
} }
else else
{ {
// this is always bigger if that is in the key set // this is always bigger if that is in the key set
return 1; return 1;
@ -472,14 +488,14 @@ public class GenomeLoc implements Comparable<GenomeLoc> {
{ {
return -1; return -1;
} }
else else
{ {
if ( thisIndex < thatIndex ) return -1; if ( thisIndex < thatIndex ) return -1;
if ( thisIndex > thatIndex ) return 1; if ( thisIndex > thatIndex ) return 1;
return 0; return 0;
} }
} }
else else
{ {
return thisContig.compareTo(thatContig); return thisContig.compareTo(thatContig);
} }
@ -489,7 +505,6 @@ public class GenomeLoc implements Comparable<GenomeLoc> {
return (this.contigIndex == that.contigIndex)?0:((this.contigIndex < that.contigIndex)?-1:1); return (this.contigIndex == that.contigIndex)?0:((this.contigIndex < that.contigIndex)?-1:1);
} }
public int compareTo( GenomeLoc that ) { public int compareTo( GenomeLoc that ) {
if ( this == that ) return 0; if ( this == that ) return 0;