Skip to content

Commit

Permalink
implemented seekn(); not tested
Browse files Browse the repository at this point in the history
  • Loading branch information
lh3 committed Jun 1, 2015
1 parent ce51f71 commit 65fd23d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
7 changes: 7 additions & 0 deletions hts.c
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,13 @@ int hts_itr_next(BGZF *fp, hts_itr_t *iter, void *r, hts_readrec_f readrec, void
return ret;
}

int hts_idx_seekn_aux(BGZF *fp, const hts_idx_t *idx, int64_t r)
{
if (idx->ridx.n == 0 || r >= idx->n_rec) return -1;
bgzf_seek(fp, idx->ridx.offset[r>>idx->rec_shift], SEEK_SET);
return r & (((1<<idx->rec_shift) - 1));
}

/**********************
*** Retrieve index ***
**********************/
Expand Down
1 change: 1 addition & 0 deletions hts.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ extern "C" {
typedef int (*hts_readrec_f)(BGZF*, void*, void*, int*, int*, int*);
typedef int (*hts_name2id_f)(void*, const char*);

int hts_idx_seekn_aux(BGZF *fp, const hts_idx_t *idx, int64_t n);
hts_itr_t *hts_itr_querys(const hts_idx_t *idx, const char *reg, hts_name2id_f getid, void *hdr);
int hts_itr_next(BGZF *fp, hts_itr_t *iter, void *r, hts_readrec_f readrec, void *hdr);

Expand Down
15 changes: 15 additions & 0 deletions vcf.c
Original file line number Diff line number Diff line change
Expand Up @@ -1192,3 +1192,18 @@ int bcf_append_info_ints(const bcf_hdr_t *h, bcf1_t *b, const char *key, int n,
return 0;
}

int bcf_seekn(BGZF *fp, const hts_idx_t *idx, int64_t r)
{
int skip, ret = 0;
skip = hts_idx_seekn_aux(fp, idx, r);
if (skip > 0) {
bcf1_t *b;
b = bcf_init1();
do {
ret = bcf_read1(fp, b);
--skip;
} while (skip > 0 && ret >= 0);
bcf_destroy1(b);
}
return ret;
}
1 change: 1 addition & 0 deletions vcf.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ extern "C" {
#define bcf_index_load(fn) hts_idx_load(fn, HTS_FMT_CSI)

int bcf_index_build(const char *fn, int min_shift);
int bcf_seekn(BGZF *fp, const hts_idx_t *idx, int64_t n);

/***************
*** VCF I/O ***
Expand Down

0 comments on commit 65fd23d

Please sign in to comment.