8 #include "bam_maqcns.h"
10 #define TV_MIN_ALNROW 2
11 #define TV_MAX_GOTO 40
12 #define TV_LOW_MAPQ 10
14 #define TV_COLOR_MAPQ 0
15 #define TV_COLOR_BASEQ 1
16 #define TV_COLOR_NUCL 2
20 WINDOW *wgoto, *whelp;
26 int curr_tid, left_pos;
30 int ccol, last_pos, row_shift, color_for, is_nucl, l_ref;
34 char bam_nt16_nt4_table[] = { 4, 0, 1, 4, 2, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4 };
36 int tv_pl_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pl, void *data)
38 tview_t *tv = (tview_t*)data;
39 int i, j, c, rb, attr, max_ins = 0;
41 if (pos < tv->left_pos || tv->ccol > tv->mcol) return 0; // out of screen
43 rb = (tv->ref && pos - tv->left_pos < tv->l_ref)? tv->ref[pos - tv->left_pos] : 'N';
44 for (i = tv->last_pos + 1; i < pos; ++i) {
45 if (i%10 == 0) mvprintw(0, tv->ccol, "%-d", i+1);
46 c = tv->ref? tv->ref[i - tv->left_pos] : 'N';
47 mvaddch(1, tv->ccol++, c);
49 if (pos%10 == 0) mvprintw(0, tv->ccol, "%-d", pos+1);
51 call = bam_maqcns_call(n, pl, tv->bmc);
53 c = ",ACMGRSVTWYHKDBN"[call>>28&0xf];
54 i = (call>>8&0xff)/10+1;
56 attr |= COLOR_PAIR(i);
57 if (c == toupper(rb)) c = '.';
59 mvaddch(2, tv->ccol, c);
61 // calculate maximum insert
62 for (i = 0; i < n; ++i) {
63 const bam_pileup1_t *p = pl + i;
64 if (p->indel > 0 && max_ins < p->indel) max_ins = p->indel;
67 for (j = 0; j <= max_ins; ++j) {
68 for (i = 0; i < n; ++i) {
69 const bam_pileup1_t *p = pl + i;
70 int row = TV_MIN_ALNROW + p->level - tv->row_shift;
73 c = bam_nt16_rev_table[bam1_seqi(bam1_seq(p->b), p->qpos)];
74 if (!tv->is_nucl && toupper(c) == toupper(rb)) c = bam1_strand(p->b)? ',' : '.';
77 if (j > p->indel) c = '*';
79 c = bam_nt16_rev_table[bam1_seqi(bam1_seq(p->b), p->qpos + j)];
80 if (j == 0 && !tv->is_nucl && toupper(c) == toupper(rb)) c = bam1_strand(p->b)? ',' : '.';
83 if (row > TV_MIN_ALNROW && row < tv->mrow) {
86 if (((p->b->core.flag&BAM_FPAIRED) && !(p->b->core.flag&BAM_FPROPER_PAIR))
87 || (p->b->core.flag & BAM_FSECONDARY)) attr |= A_UNDERLINE;
88 if (tv->color_for == TV_COLOR_BASEQ) {
89 x = bam1_qual(p->b)[p->qpos]/10 + 1;
91 attr |= COLOR_PAIR(x);
92 } else if (tv->color_for == TV_COLOR_MAPQ) {
93 x = p->b->core.qual/10 + 1;
95 attr |= COLOR_PAIR(x);
96 } else if (tv->color_for == TV_COLOR_NUCL) {
97 x = bam_nt16_nt4_table[bam1_seqi(bam1_seq(p->b), p->qpos)] + 5;
98 attr |= COLOR_PAIR(x);
101 mvaddch(row, tv->ccol, bam1_strand(p->b)? tolower(c) : toupper(c));
107 attr = COLOR_PAIR(8);
109 mvaddch(1, tv->ccol++, c);
111 } else mvaddch(1, tv->ccol++, c);
117 tview_t *tv_init(const char *fn, const char *fn_fa)
119 tview_t *tv = (tview_t*)calloc(1, sizeof(tview_t));
120 tv->idx = bam_index_load(fn);
121 tv->fp = bam_open(fn, "r");
123 tv->header = bam_header_read(tv->fp);
124 tv->lplbuf = bam_lplbuf_init(tv_pl_func, tv);
125 if (fn_fa) tv->fai = fai_load(fn_fa);
126 tv->bmc = bam_maqcns_init();
127 bam_maqcns_prepare(tv->bmc);
130 keypad(stdscr, TRUE);
134 #ifdef NCURSES_VERSION
135 getmaxyx(stdscr, tv->mrow, tv->mcol);
137 tv->mrow = 80; tv->mcol = 40;
139 tv->wgoto = newwin(3, TV_MAX_GOTO + 10, 10, 5);
140 tv->whelp = newwin(22,40, 5, 5);
141 tv->color_for = TV_COLOR_MAPQ;
143 init_pair(1, COLOR_BLUE, COLOR_BLACK);
144 init_pair(2, COLOR_GREEN, COLOR_BLACK);
145 init_pair(3, COLOR_YELLOW, COLOR_BLACK);
146 init_pair(4, COLOR_WHITE, COLOR_BLACK);
147 init_pair(5, COLOR_GREEN, COLOR_BLACK);
148 init_pair(6, COLOR_CYAN, COLOR_BLACK);
149 init_pair(7, COLOR_YELLOW, COLOR_BLACK);
150 init_pair(8, COLOR_RED, COLOR_BLACK);
151 init_pair(9, COLOR_BLUE, COLOR_BLACK);
155 void tv_destroy(tview_t *tv)
157 delwin(tv->wgoto); delwin(tv->whelp);
160 bam_lplbuf_destroy(tv->lplbuf);
161 bam_maqcns_destroy(tv->bmc);
162 bam_index_destroy(tv->idx);
163 if (tv->fai) fai_destroy(tv->fai);
165 bam_header_destroy(tv->header);
170 int tv_fetch_func(const bam1_t *b, void *data)
172 tview_t *tv = (tview_t*)data;
173 bam_lplbuf_push(b, tv->lplbuf);
177 int tv_draw_aln(tview_t *tv, int tid, int pos)
182 tv->curr_tid = tid; tv->left_pos = pos;
183 tv->last_pos = tv->left_pos - 1;
185 // print ref and consensus
188 if (tv->ref) free(tv->ref);
189 str = (char*)calloc(strlen(tv->header->target_name[tv->curr_tid]) + 30, 1);
190 sprintf(str, "%s:%d-%d", tv->header->target_name[tv->curr_tid], tv->left_pos + 1, tv->left_pos + tv->mcol);
191 tv->ref = fai_fetch(tv->fai, str, &tv->l_ref);
195 bam_lplbuf_reset(tv->lplbuf);
196 bam_fetch(tv->fp, tv->idx, tv->curr_tid, tv->left_pos, tv->left_pos + tv->mcol, tv, tv_fetch_func);
197 bam_lplbuf_push(0, tv->lplbuf);
201 static void tv_win_goto(tview_t *tv, int *tid, int *pos)
205 wborder(tv->wgoto, '|', '|', '-', '-', '+', '+', '+', '+');
206 mvwprintw(tv->wgoto, 1, 2, "Goto: ");
208 int c = wgetch(tv->wgoto);
210 if (c == KEY_BACKSPACE || c == '\010' || c == '\177') {
212 } else if (c == KEY_ENTER || c == '\012' || c == '\015') {
213 int _tid = -1, _beg, _end;
214 bam_parse_region(tv->header, str, &_tid, &_beg, &_end);
216 *tid = _tid; *pos = _beg;
219 } else if (isgraph(c)) {
220 if (l < TV_MAX_GOTO) str[l++] = c;
221 } else if (c == '\027') l = 0;
222 else if (c == '\033') return;
224 for (i = 0; i < TV_MAX_GOTO; ++i) mvwaddch(tv->wgoto, 1, 8 + i, ' ');
225 mvwprintw(tv->wgoto, 1, 8, "%s", str);
229 static void tv_win_help(tview_t *tv) {
231 WINDOW *win = tv->whelp;
232 wborder(win, '|', '|', '-', '-', '+', '+', '+', '+');
233 mvwprintw(win, r++, 2, " -=- Help -=- ");
235 mvwprintw(win, r++, 2, "? This window");
236 mvwprintw(win, r++, 2, "Arrows Small scroll movement");
237 mvwprintw(win, r++, 2, "h,j,k,l Small scroll movement");
238 mvwprintw(win, r++, 2, "H,J,K,L Large scroll movement");
239 mvwprintw(win, r++, 2, "ctrl-H Scroll 1k left");
240 mvwprintw(win, r++, 2, "ctrl-L Scroll 1k right");
241 mvwprintw(win, r++, 2, "space Scroll one screen");
242 mvwprintw(win, r++, 2, "backspace Scroll back one screen");
243 mvwprintw(win, r++, 2, "g Go to specific location");
244 mvwprintw(win, r++, 2, "b Color for base quality");
245 mvwprintw(win, r++, 2, "m Color for mapping qual");
246 mvwprintw(win, r++, 2, "n Color for nucleotide");
247 mvwprintw(win, r++, 2, ". Toggle on/off dot view");
248 mvwprintw(win, r++, 2, "q Exit");
250 mvwprintw(win, r++, 2, "Underline: Secondary or orphan");
251 mvwprintw(win, r++, 2, "Blue: 0-9 Green: 10-19");
252 mvwprintw(win, r++, 2, "Yellow: 20-29 White: >=30");
257 void tv_loop(tview_t *tv)
260 tid = tv->curr_tid; pos = tv->left_pos;
264 case '?': tv_win_help(tv); break;
266 case 'q': goto end_loop;
267 case 'g': tv_win_goto(tv, &tid, &pos); break;
268 case 'b': tv->color_for = TV_COLOR_BASEQ; break;
269 case 'm': tv->color_for = TV_COLOR_MAPQ; break;
270 case 'n': tv->color_for = TV_COLOR_NUCL; break;
272 case 'h': --pos; break;
274 case 'l': ++pos; break;
276 case 'H': pos -= 20; break;
278 case 'L': pos += 20; break;
279 case '.': tv->is_nucl = !tv->is_nucl; break;
280 case '\010': pos -= 1000; break;
281 case '\014': pos += 1000; break;
282 case ' ': pos += tv->mcol; break;
284 case 'j': --tv->row_shift; break;
286 case 'k': ++tv->row_shift; break;
288 case '\177': pos -= tv->mcol; break;
290 case KEY_RESIZE: getmaxyx(stdscr, tv->mrow, tv->mcol); break;
294 if (pos < 0) pos = 0;
295 if (tv->row_shift < 0) tv->row_shift = 0;
296 tv_draw_aln(tv, tid, pos);
302 int bam_tview_main(int argc, char *argv[])
306 fprintf(stderr, "Usage: bamtk tview <aln.bam> [ref.fasta]\n");
309 tv = tv_init(argv[1], (argc == 2)? 0 : argv[2]);
310 tv_draw_aln(tv, 0, 0);