Skip to content

Commit

Permalink
Make tview (and bits of pileup) work with 64 bit positions
Browse files Browse the repository at this point in the history
  • Loading branch information
daviesrob authored and valeriuo committed Oct 30, 2019
1 parent 8ed92fb commit 7dfe8c5
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 33 deletions.
2 changes: 1 addition & 1 deletion bam_lpileup.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void bam_lplbuf_reset(bam_lplbuf_t *buf)
buf->n_nodes = 0;
}

static int tview_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pl, void *data)
static int tview_func(uint32_t tid, hts_pos_t pos, int n, const bam_pileup1_t *pl, void *data)
{
bam_lplbuf_t *tv = (bam_lplbuf_t*)data;
freenode_t *p;
Expand Down
2 changes: 1 addition & 1 deletion bam_lpileup.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ typedef struct __bam_lplbuf_t bam_lplbuf_t;

#ifndef BAM_PILEUP_F_DEFINED
#define BAM_PILEUP_F_DEFINED
typedef int (*bam_pileup_f)(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pl, void *data);
typedef int (*bam_pileup_f)(uint32_t tid, hts_pos_t pos, int n, const bam_pileup1_t *pl, void *data);
#endif //BAM_PILEUP_F_DEFINED


Expand Down
5 changes: 3 additions & 2 deletions bam_plbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ void bam_plbuf_destroy(bam_plbuf_t *buf)

int bam_plbuf_push(const bam1_t *b, bam_plbuf_t *buf)
{
int ret, n_plp, tid, pos;
int ret, n_plp, tid;
hts_pos_t pos;
const bam_pileup1_t *plp;
ret = bam_plp_push(buf->iter, b);
if (ret < 0) return ret;
while ((plp = bam_plp_next(buf->iter, &tid, &pos, &n_plp)) != 0)
while ((plp = bam_plp64_next(buf->iter, &tid, &pos, &n_plp)) != 0)
buf->func(tid, pos, n_plp, plp, buf->data);
return 0;
}
2 changes: 1 addition & 1 deletion bam_plbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ DEALINGS IN THE SOFTWARE. */

#ifndef BAM_PILEUP_F_DEFINED
#define BAM_PILEUP_F_DEFINED
typedef int (*bam_pileup_f)(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pl, void *data);
typedef int (*bam_pileup_f)(uint32_t tid, hts_pos_t pos, int n, const bam_pileup1_t *pl, void *data);
#endif //BAM_PILEUP_F_DEFINED

typedef struct {
Expand Down
31 changes: 18 additions & 13 deletions bam_tview.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,21 +166,24 @@ void base_tv_destroy(tview_t* tv)
}


int tv_pl_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pl, void *data)
int tv_pl_func(uint32_t tid, hts_pos_t pos, int n, const bam_pileup1_t *pl, void *data)
{
tview_t *tv = (tview_t*)data;
int i, j, c, rb, attr, max_ins = 0;
hts_pos_t cp;
int i, j, c, rb, attr, max_ins = 0, interval;
uint32_t call = 0;
kstring_t ks = KS_INITIALIZE;
if (pos < tv->left_pos || tv->ccol > tv->mcol) return 0; // out of screen
// print reference
rb = (tv->ref && pos - tv->left_pos < tv->l_ref)? tv->ref[pos - tv->left_pos] : 'N';
for (i = tv->last_pos + 1; i < pos; ++i) {
if (i%10 == 0 && tv->mcol - tv->ccol >= 10) tv->my_mvprintw(tv,0, tv->ccol, "%-d", i+1);
c = tv->ref? tv->ref[i - tv->left_pos] : 'N';
for (cp = tv->last_pos + 1; cp < pos; ++cp) {
interval = cp < 1000000000 ? 10 : 20;
if (cp%interval == 0 && tv->mcol - tv->ccol >= 10) tv->my_mvprintw(tv,0, tv->ccol, "%-"PRIhts_pos, cp+1);
c = tv->ref? tv->ref[cp - tv->left_pos] : 'N';
tv->my_mvaddch(tv,1, tv->ccol++, c);
}
if (pos%10 == 0 && tv->mcol - tv->ccol >= 10) tv->my_mvprintw(tv,0, tv->ccol, "%-d", pos+1);
interval = pos < 1000000000 ? 10 : 20;
if (pos%interval == 0 && tv->mcol - tv->ccol >= 10) tv->my_mvprintw(tv,0, tv->ccol, "%-"PRIhts_pos, pos+1);
{ // call consensus
bcf_callret1_t bcr;
memset(&bcr, 0, sizeof bcr);
Expand Down Expand Up @@ -342,7 +345,7 @@ static int tv_push_aln(const bam1_t *b, tview_t *tv)
return 0;
}

int base_draw_aln(tview_t *tv, int tid, int pos)
int base_draw_aln(tview_t *tv, int tid, hts_pos_t pos)
{
assert(tv!=NULL);
// reset
Expand All @@ -359,8 +362,8 @@ int base_draw_aln(tview_t *tv, int tid, int pos)
const char *ref_name = sam_hdr_tid2name(tv->header, tv->curr_tid);
str = (char*)calloc(strlen(ref_name) + 30, 1);
assert(str!=NULL);
sprintf(str, "%s:%d-%d", ref_name, tv->left_pos + 1, tv->left_pos + tv->mcol);
tv->ref = fai_fetch(tv->fai, str, &tv->l_ref);
sprintf(str, "%s:%"PRIhts_pos"-%"PRIhts_pos, ref_name, tv->left_pos + 1, tv->left_pos + tv->mcol);
tv->ref = fai_fetch64(tv->fai, str, &tv->l_ref);
free(str);
if ( !tv->ref )
{
Expand All @@ -378,8 +381,9 @@ int base_draw_aln(tview_t *tv, int tid, int pos)
bam_lplbuf_push(0, tv->lplbuf);

while (tv->ccol < tv->mcol) {
int pos = tv->last_pos + 1;
if (pos%10 == 0 && tv->mcol - tv->ccol >= 10) tv->my_mvprintw(tv,0, tv->ccol, "%-d", pos+1);
hts_pos_t pos = tv->last_pos + 1;
int interval = pos < 1000000000 ? 10 : 20;
if (pos%interval == 0 && tv->mcol - tv->ccol >= 10) tv->my_mvprintw(tv,0, tv->ccol, "%-"PRIhts_pos, pos+1);
tv->my_mvaddch(tv,1, tv->ccol++, (tv->ref && pos < tv->l_ref)? tv->ref[pos - tv->left_pos] : 'N');
++tv->last_pos;
}
Expand Down Expand Up @@ -493,8 +497,9 @@ int bam_tview_main(int argc, char *argv[])

if ( position )
{
int tid, beg, end;
char *name_lim = (char *) hts_parse_reg(position, &beg, &end);
int tid;
hts_pos_t beg, end;
char *name_lim = (char *) hts_parse_reg64(position, &beg, &end);
if (name_lim) *name_lim = '\0';
else beg = 0; // region parsing failed, but possibly a seq named "foo:a"
tid = bam_name2id(tv->header, position);
Expand Down
10 changes: 5 additions & 5 deletions bam_tview.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ typedef struct AbstractTview {
bam_lplbuf_t* lplbuf;
sam_hdr_t* header;
samFile* fp;
int curr_tid, left_pos;
faidx_t* fai;
bcf_callaux_t* bca;

int ccol, last_pos, row_shift, base_for, color_for, is_dot, l_ref, ins;
hts_pos_t left_pos, last_pos, l_ref;
int curr_tid, ccol, row_shift, base_for, color_for, is_dot, ins;
int no_skip, show_name, inverse;
char *ref;
/* maps @RG ID => SM (sample), in practice only used to determine whether a particular RG is in the list of allowed ones */
Expand All @@ -66,7 +66,7 @@ typedef struct AbstractTview {
void (*my_attroff)(struct AbstractTview*,int);
void (*my_clear)(struct AbstractTview*);
int (*my_colorpair)(struct AbstractTview*,int);
int (*my_drawaln)(struct AbstractTview*,int,int);
int (*my_drawaln)(struct AbstractTview*,int,hts_pos_t);
int (*my_loop)(struct AbstractTview*);
int (*my_underline)(struct AbstractTview*);
} tview_t;
Expand All @@ -89,12 +89,12 @@ char bam_aux_getCQi(bam1_t *b, int i);
#define TV_BASE_NUCL 0
#define TV_BASE_COLOR_SPACE 1

int tv_pl_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pl, void *data);
int tv_pl_func(uint32_t tid, hts_pos_t pos, int n, const bam_pileup1_t *pl, void *data);
// Added fn_idx to arguments as index file
int base_tv_init(tview_t*,const char *fn, const char *fn_fa, const char *fn_idx,
const char *samples, const htsFormat *fmt);
void base_tv_destroy(tview_t*);
int base_draw_aln(tview_t *tv, int tid, int pos);
int base_draw_aln(tview_t *tv, int tid, hts_pos_t pos);

typedef struct Tixel
{
Expand Down
14 changes: 8 additions & 6 deletions bam_tview_curses.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ static int curses_colorpair(struct AbstractTview* tv,int flag) {
return COLOR_PAIR(flag);
}

static int curses_drawaln(struct AbstractTview* tv, int tid, int pos) {
static int curses_drawaln(struct AbstractTview* tv, int tid, hts_pos_t pos) {
return base_draw_aln(tv, tid, pos);
}

static void tv_win_goto(curses_tview_t *tv, int *tid, int *pos) {
static void tv_win_goto(curses_tview_t *tv, int *tid, hts_pos_t *pos) {
char str[256], *p;
int i, l = 0;
tview_t *base=(tview_t*)tv;
Expand All @@ -153,15 +153,16 @@ static void tv_win_goto(curses_tview_t *tv, int *tid, int *pos) {
if (c == KEY_BACKSPACE || c == '\010' || c == '\177') {
if(l > 0) --l;
} else if (c == KEY_ENTER || c == '\012' || c == '\015') {
int _tid = -1, _beg, _end;
int _tid = -1;
hts_pos_t _beg, _end;
if (str[0] == '=') {
_beg = strtol(str+1, &p, 10) - 1;
_beg = strtoll(str+1, &p, 10) - 1;
if (_beg > 0) {
*pos = _beg;
return;
}
} else {
char *name_lim = (char *) hts_parse_reg(str, &_beg, &_end);
char *name_lim = (char *) hts_parse_reg64(str, &_beg, &_end);
if (name_lim) {
char name_terminator = *name_lim;
*name_lim = '\0';
Expand Down Expand Up @@ -234,7 +235,8 @@ static int curses_underline(tview_t* tv) {
}

static int curses_loop(tview_t* tv) {
int tid, pos;
int tid;
hts_pos_t pos;
curses_tview_t *CTV=(curses_tview_t *)tv;
tid = tv->curr_tid; pos = tv->left_pos;
while (1) {
Expand Down
8 changes: 4 additions & 4 deletions bam_tview_html.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,14 @@ static int html_colorpair(struct AbstractTview* tv,int flag)
return (1 << (flag));
}

static int html_drawaln(struct AbstractTview* tv, int tid, int pos)
static int html_drawaln(struct AbstractTview* tv, int tid, hts_pos_t pos)
{
int y,x;
html_tview_t* ptr=FROM_TV(tv);
html_clear(tv);
base_draw_aln(tv, tid, pos);
fputs("<html><head>",ptr->out);
fprintf(ptr->out,"<title>%s:%d</title>",
fprintf(ptr->out,"<title>%s:%"PRIhts_pos"</title>",
sam_hdr_tid2name(tv->header, tid),
pos+1
);
Expand All @@ -164,7 +164,7 @@ static int html_drawaln(struct AbstractTview* tv, int tid, int pos)

fputs("</head><body>",ptr->out);

fprintf(ptr->out,"<div class='tviewbody'><div class='tviewtitle'>%s:%d</div>",
fprintf(ptr->out,"<div class='tviewbody'><div class='tviewtitle'>%s:%"PRIhts_pos"</div>",
sam_hdr_tid2name(tv->header, tid),
pos+1
);
Expand Down Expand Up @@ -233,7 +233,7 @@ static int html_drawaln(struct AbstractTview* tv, int tid, int pos)
#define ANSI_UNDERLINE_SET "\033[4m"
#define ANSI_UNDERLINE_UNSET "\033[0m"

static int text_drawaln(struct AbstractTview* tv, int tid, int pos)
static int text_drawaln(struct AbstractTview* tv, int tid, hts_pos_t pos)
{
int y,x;
html_tview_t* ptr=FROM_TV(tv);
Expand Down

0 comments on commit 7dfe8c5

Please sign in to comment.