X-Git-Url: http://woldlab.caltech.edu/gitweb/?p=tabix.git;a=blobdiff_plain;f=TabixReader.java;h=0d359c5968ef218020b22bf3394fb049da16fcf6;hp=acaff24ef0349b91f393093d5151b6bc32ca1a9d;hb=HEAD;hpb=c70c92b7a385548d8e670638a93a083401f49e4d diff --git a/TabixReader.java b/TabixReader.java index acaff24..0d359c5 100644 --- a/TabixReader.java +++ b/TabixReader.java @@ -93,6 +93,8 @@ public class TabixReader private static int reg2bins(final int beg, final int _end, final int[] list) { int i = 0, k, end = _end; + if (beg >= end) return 0; + if (end >= 1<<29) end = 1<<29; --end; list[i++] = 0; for (k = 1 + (beg>>26); k <= 1 + (end>>26); ++k) list[i++] = k; @@ -215,7 +217,7 @@ public class TabixReader int[] ret = new int[3]; colon = reg.indexOf(':'); hyphen = reg.indexOf('-'); chr = colon >= 0? reg.substring(0, colon) : reg; - ret[1] = colon >= 0? Integer.parseInt(reg.substring(colon+1, hyphen)) - 1 : 0; + ret[1] = colon >= 0? Integer.parseInt(reg.substring(colon+1, hyphen >= 0? hyphen : reg.length())) - 1 : 0; ret[2] = hyphen >= 0? Integer.parseInt(reg.substring(hyphen+1)) : 0x7fffffff; ret[0] = chr2tid(chr); return ret; @@ -224,17 +226,17 @@ public class TabixReader private TIntv getIntv(final String s) { TIntv intv = new TIntv(); int col = 0, end = 0, beg = 0; - while ((end = s.indexOf('\t', beg)) >= 0) { + while ((end = s.indexOf('\t', beg)) >= 0 || end == -1) { ++col; if (col == mSc) { intv.tid = chr2tid(s.substring(beg, end)); } else if (col == mBc) { - intv.beg = intv.end = Integer.parseInt(s.substring(beg, end)); + intv.beg = intv.end = Integer.parseInt(s.substring(beg, end==-1?s.length():end)); if ((mPreset&0x10000) != 0) ++intv.end; else --intv.beg; if (intv.beg < 0) intv.beg = 0; if (intv.end < 1) intv.end = 1; - } else { // FIXME: SAM/VCF supports are not tested yet + } else { // FIXME: SAM supports are not tested yet if ((mPreset&0xffff) == 0) { // generic if (col == mEc) intv.end = Integer.parseInt(s.substring(beg, end)); @@ -252,22 +254,25 @@ public class TabixReader intv.end = intv.beg + l; } } else if ((mPreset&0xffff) == 2) { // VCF - if (col == 5) { - String alt = s.substring(beg, end); - int i, max = 1; - for (i = 0; i < alt.length(); ++i) { - if (alt.charAt(i) == 'D') { // deletion - int j; - for (j = i; j < alt.length() && alt.charAt(j) >= '0' && alt.charAt(j) <= '9'; ++j); - int l = Integer.parseInt(alt.substring(i, j)); - if (max < l) max = l; - i = j - 1; - } + String alt; + alt = end >= 0? s.substring(beg, end) : s.substring(beg); + if (col == 4) { // REF + if (alt.length() > 0) intv.end = intv.beg + alt.length(); + } else if (col == 8) { // INFO + int e_off = -1, i = alt.indexOf("END="); + if (i == 0) e_off = 4; + else if (i > 0) { + i = alt.indexOf(";END="); + if (i >= 0) e_off = i + 5; + } + if (e_off > 0) { + i = alt.indexOf(";", e_off); + intv.end = Integer.parseInt(i > e_off? alt.substring(e_off, i) : alt.substring(e_off)); } - intv.end = intv.beg + max; } } } + if (end == -1) break; beg = end + 1; } return intv; @@ -320,7 +325,9 @@ public class TabixReader TIndex idx = mIndex[tid]; int[] bins = new int[MAX_BIN]; int i, l, n_off, n_bins = reg2bins(beg, end, bins); - min_off = (beg>>TAD_LIDX_SHIFT >= idx.l.length)? idx.l[idx.l.length-1] : idx.l[beg>>TAD_LIDX_SHIFT]; + if (idx.l.length > 0) + min_off = (beg>>TAD_LIDX_SHIFT >= idx.l.length)? idx.l[idx.l.length-1] : idx.l[beg>>TAD_LIDX_SHIFT]; + else min_off = 0; for (i = n_off = 0; i < n_bins; ++i) { if ((chunks = idx.b.get(bins[i])) != null) n_off += chunks.length; @@ -332,6 +339,7 @@ public class TabixReader for (int j = 0; j < chunks.length; ++j) if (less64(min_off, chunks[j].v)) off[n_off++] = new TPair64(chunks[j]); + if (n_off == 0) return null; Arrays.sort(off, 0, n_off); // resolve completely contained adjacent blocks for (i = 1, l = 0; i < n_off; ++i) { @@ -378,7 +386,7 @@ public class TabixReader System.out.println(s); } else { // a region is specified; random access TabixReader.Iterator iter = tr.query(args[1]); // get the iterator - while ((s = iter.next()) != null) + while (iter != null && (s = iter.next()) != null) System.out.println(s); } } catch (IOException e) {