From 11377ef390e54abd207b9c01313ada7428967453 Mon Sep 17 00:00:00 2001 From: depristo Date: Tue, 14 Apr 2009 00:53:08 +0000 Subject: [PATCH] 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 --- .../broadinstitute/sting/utils/GenomeLoc.java | 53 ++++++++++++------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/java/src/org/broadinstitute/sting/utils/GenomeLoc.java b/java/src/org/broadinstitute/sting/utils/GenomeLoc.java index e7073b038..f18aceb28 100644 --- a/java/src/org/broadinstitute/sting/utils/GenomeLoc.java +++ b/java/src/org/broadinstitute/sting/utils/GenomeLoc.java @@ -46,12 +46,20 @@ public class GenomeLoc implements Comparable { 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 ) { return contigInfo.getSequence(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); } @@ -96,6 +104,7 @@ public class GenomeLoc implements Comparable { public static boolean setupRefContigOrdering(final SAMSequenceDictionary seqDict) { if (seqDict == null) { // we couldn't load the reference dictionary logger.info("Failed to load reference dictionary, falling back to lexicographic order for contigs"); + Utils.scareUser("Failed to load reference dictionary"); return false; } else { contigInfo = seqDict; @@ -114,18 +123,26 @@ public class GenomeLoc implements Comparable { // // -------------------------------------------------------------------------------------------------------------- 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.start = start; 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); } public GenomeLoc( final String contig, final long pos ) { - this(contig, pos, pos ); + this(contig, pos, pos); } public GenomeLoc( final int contig, final long pos ) { @@ -136,11 +153,6 @@ public class GenomeLoc implements Comparable { 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 @@ -326,11 +338,15 @@ public class GenomeLoc implements Comparable { // Accessors and setters // public final String getContig() { - if (contigInfo != null && contigInfo.getSequence(this.contigIndex) != null) { - return contigInfo.getSequence(this.contigIndex).getSequenceName(); - } + assert this.contigIndex != -1; + + 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; } @@ -345,7 +361,6 @@ public class GenomeLoc implements Comparable { return String.format("%s:%d-%d", getContig(), getStart(), getStop()); } - 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 atBeginningOfContigP() { return this.start == 1; } @@ -439,7 +454,8 @@ public class GenomeLoc implements Comparable { // Comparison operations // // 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 ) { @@ -450,7 +466,7 @@ public class GenomeLoc implements Comparable { //assert getContigIndex(thisContig) != -1;// : this; //assert getContigIndex(thatContig) != -1;// : that; - if ( hasKnownContigOrdering() ) + if ( hasKnownContigOrdering() ) { int thisIndex = getContigIndex(thisContig); int thatIndex = getContigIndex(thatContig); @@ -462,7 +478,7 @@ public class GenomeLoc implements Comparable { // Use regular sorted order return thisContig.compareTo(thatContig); } - else + else { // this is always bigger if that is in the key set return 1; @@ -472,14 +488,14 @@ public class GenomeLoc implements Comparable { { return -1; } - else + else { if ( thisIndex < thatIndex ) return -1; if ( thisIndex > thatIndex ) return 1; return 0; } } - else + else { return thisContig.compareTo(thatContig); } @@ -489,7 +505,6 @@ public class GenomeLoc implements Comparable { return (this.contigIndex == that.contigIndex)?0:((this.contigIndex < that.contigIndex)?-1:1); } - public int compareTo( GenomeLoc that ) { if ( this == that ) return 0;