Skip to content

Commit

Permalink
Move bad coordinates check (#911)
Browse files Browse the repository at this point in the history
Follow up to #908.
Move the bad coordinates check to the root method, `public Iterator query(final String reg)`. If this public method is used by an application, the bad coordinates will return an `EOF_ITERATOR;`
  • Loading branch information
ronlevine authored and Yossi Farjoun committed Feb 5, 2019
1 parent 942e3d6 commit 3ae552f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 19 deletions.
24 changes: 11 additions & 13 deletions src/main/java/htsjdk/tribble/readers/TabixReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down
40 changes: 34 additions & 6 deletions src/test/java/htsjdk/tribble/readers/TabixReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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());

}


Expand Down Expand Up @@ -155,7 +184,6 @@ public void testRemoteQuery() throws IOException {
nRecords++;
}
Assert.assertTrue(nRecords > 0);

}

/**
Expand Down

0 comments on commit 3ae552f

Please sign in to comment.