Example #1
0
  private void makeIndex(BlockCompressedInputStream fp) throws Exception {
    int last_bin, save_bin;
    int last_coor, last_tid, save_tid;
    long save_off, last_off, lineno = 0, offset0 = (long) -1;
    String str;

    save_bin = save_tid = last_tid = last_bin = 0xffffffff; // Was unsigned in C implementation.
    save_off = last_off = 0;
    last_coor = 0xffffffff; // Should be unsigned.
    while ((str = readLine(fp)) != null) {
      ++lineno;
      if (lineno <= mSkip || str.charAt(0) == mMeta) {
        last_off = fp.getFilePointer();
        continue;
      }
      TIntv intv = getIntv(str);
      if (intv.beg < 0 || intv.end < 0) {
        throw new Exception("The indexes overlap or are out of bounds.");
      }
      if (last_tid != intv.tid) { // change of chromosomes
        if (last_tid > intv.tid) {
          throw new Exception(
              String.format(
                  "The chromosome blocks are not continuous at line %d, is the file sorted? [pos %d].",
                  lineno, intv.beg + 1));
        }
        last_tid = intv.tid;
        last_bin = 0xffffffff;
      } else if (last_coor > intv.beg) {
        throw new Exception(String.format("File out of order at line %d.", lineno));
      }
      long tmp = insertLinear(linearIndex.get(intv.tid), intv.beg, intv.end, last_off);
      if (last_off == 0) offset0 = tmp;
      if (intv.bin != last_bin) { // then possibly write the binning index
        if (save_bin != 0xffffffff) { // save_bin==0xffffffffu only happens to the first record
          insertBinning(binningIndex.get(save_tid), save_bin, save_off, last_off);
        }
        save_off = last_off;
        save_bin = last_bin = intv.bin;
        save_tid = intv.tid;
        if (save_tid < 0) break;
      }
      if (fp.getFilePointer() <= last_off) {
        throw new Exception(String.format("Bug in BGZF: %x < %x.", fp.getFilePointer(), last_off));
      }
      last_off = fp.getFilePointer();
      last_coor = intv.beg;
    }
    if (save_tid >= 0)
      insertBinning(binningIndex.get(save_tid), save_bin, save_off, fp.getFilePointer());
    mergeChunks();
    fillMissing();
    if (offset0 != (long) -1 && !linearIndex.isEmpty() && linearIndex.get(0) != null) {
      int beg = (int) (offset0 >> 32), end = (int) (offset0 & 0xffffffff);
      for (int i = beg; i <= end; ++i) {
        linearIndex.get(0).set(i, 0L);
      }
    }
  }
Example #2
0
 public void createIndex(File fn) throws Exception {
   BlockCompressedInputStream fp = new BlockCompressedInputStream(fn);
   makeIndex(fp);
   fp.close();
   File indexFile = new File(fn + ".tbi");
   BlockCompressedOutputStream fpidx = new BlockCompressedOutputStream(indexFile);
   saveIndex(fpidx);
   fpidx.close();
 }