diff --git a/src/main/java/htsjdk/tribble/readers/TabixReader.java b/src/main/java/htsjdk/tribble/readers/TabixReader.java index 93a2ac3160..66b5babcb2 100644 --- a/src/main/java/htsjdk/tribble/readers/TabixReader.java +++ b/src/main/java/htsjdk/tribble/readers/TabixReader.java @@ -446,16 +446,16 @@ public String next() throws IOException { } /** - * Return - * @param tid Sequence id - * @param beg beginning of interval, genomic coords - * @param end end of interval, genomic coords - * @return an iterator over the lines within the specified interval + * Get an iterator for an interval specified by the sequence id and begin and end coordinates + * @param tid Sequence id, if non-existent returns EOF_ITERATOR + * @param beg beginning of interval, genomic coords (0-based, closed-open) + * @param end end of interval, genomic coords (0-based, closed-open) + * @return an iterator over the specified interval */ public Iterator query(final int tid, final int beg, final int end) { TPair64[] off, chunks; long min_off; - if(tid< 0 || tid>=this.mIndex.length) return EOF_ITERATOR; + if (tid < 0 || beg < 0 || end <= 0 || tid >= this.mIndex.length) return EOF_ITERATOR; TIndex idx = mIndex[tid]; int[] bins = new int[MAX_BIN]; int i, l, n_off, n_bins = reg2bins(beg, end, bins); @@ -510,25 +510,23 @@ public Iterator query(final int tid, final int beg, final int end) { * * @see #parseReg(String) * @param reg A region string of the form acceptable by {@link #parseReg(String)} - * @return + * @return an iterator over the specified interval */ public Iterator query(final String reg) { int[] x = parseReg(reg); - if(x[0]<0) return EOF_ITERATOR; return query(x[0], x[1], x[2]); } /** - * + * Get an iterator for an interval specified by the sequence id and begin and end coordinates * @see #parseReg(String) * @param reg a chromosome * @param start start interval * @param end end interval - * @return a tabix iterator + * @return a tabix iterator over the specified interval */ - public Iterator query(final String reg,int start,int end) { - int tid=this.chr2tid(reg); - if(tid==-1) return EOF_ITERATOR; + public Iterator query(final String reg, int start, int end) { + int tid = this.chr2tid(reg); return query(tid, start, end); } diff --git a/src/test/java/htsjdk/tribble/readers/TabixReaderTest.java b/src/test/java/htsjdk/tribble/readers/TabixReaderTest.java index b8ec6773f0..f530369a5d 100644 --- a/src/test/java/htsjdk/tribble/readers/TabixReaderTest.java +++ b/src/test/java/htsjdk/tribble/readers/TabixReaderTest.java @@ -79,11 +79,14 @@ public void testIterators() throws IOException { iter=tabixReader.query("UN:1-100"); Assert.assertNotNull(iter); Assert.assertNull(iter.next()); - - + iter=tabixReader.query("1:10-1"); Assert.assertNotNull(iter); Assert.assertNull(iter.next()); + + iter=tabixReader.query("chr2:0-1"); + Assert.assertNotNull(iter); + Assert.assertNull(iter.next()); iter=tabixReader.query(999999,9,9); Assert.assertNotNull(iter); @@ -92,9 +95,36 @@ public void testIterators() throws IOException { iter=tabixReader.query("1",Integer.MAX_VALUE-1,Integer.MAX_VALUE); Assert.assertNotNull(iter); Assert.assertNull(iter.next()); - + + iter = tabixReader.query("1", -1, Integer.MAX_VALUE); + Assert.assertNotNull(iter); + Assert.assertNull(iter.next()); + + iter = tabixReader.query("1", Integer.MAX_VALUE, -1); + Assert.assertNotNull(iter); + Assert.assertNull(iter.next()); + + iter = tabixReader.query("1", Integer.MAX_VALUE, 0); + Assert.assertNotNull(iter); + Assert.assertNull(iter.next()); + + iter = tabixReader.query("1:100-1000"); + Assert.assertNotNull(iter); + Assert.assertNotNull(iter.next()); + Assert.assertNull(iter.next()); + final int pos_snp_in_vcf_chr1=327; - + + iter = tabixReader.query("1:" + pos_snp_in_vcf_chr1 + "-" + pos_snp_in_vcf_chr1); + Assert.assertNotNull(iter); + Assert.assertNotNull(iter.next()); + Assert.assertNull(iter.next()); + + iter = tabixReader.query("1:" + pos_snp_in_vcf_chr1); + Assert.assertNotNull(iter); + Assert.assertNotNull(iter.next()); + Assert.assertNull(iter.next()); + iter=tabixReader.query("1",pos_snp_in_vcf_chr1,pos_snp_in_vcf_chr1); Assert.assertNotNull(iter); Assert.assertNotNull(iter); @@ -107,7 +137,6 @@ public void testIterators() throws IOException { iter=tabixReader.query("1",pos_snp_in_vcf_chr1+1,pos_snp_in_vcf_chr1+1); Assert.assertNotNull(iter); Assert.assertNull(iter.next()); - } @@ -155,7 +184,6 @@ public void testRemoteQuery() throws IOException { nRecords++; } Assert.assertTrue(nRecords > 0); - } /**