Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move bad coordinates check #911

Merged
merged 2 commits into from
Feb 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test case in which end == 0?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also add test cases for the other overloads of query(), since they have difference interval conventions (the String version is 1-based closed, the 3-arg version is 0-based closed-open!)

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