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
17 #define TV_COLOR_COL 3
18 #define TV_COLOR_COLQ 4
20 #define TV_BASE_NUCL 0
21 #define TV_BASE_COLOR_SPACE 1
25 WINDOW *wgoto, *whelp;
31 int curr_tid, left_pos;
35 int ccol, last_pos, row_shift, base_for, color_for, is_dot, l_ref, ins;
39 int tv_pl_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pl, void *data)
41 tview_t *tv = (tview_t*)data;
42 int i, j, c, rb, attr, max_ins = 0;
44 if (pos < tv->left_pos || tv->ccol > tv->mcol) return 0; // out of screen
46 rb = (tv->ref && pos - tv->left_pos < tv->l_ref)? tv->ref[pos - tv->left_pos] : 'N';
47 for (i = tv->last_pos + 1; i < pos; ++i) {
48 if (i%10 == 0) mvprintw(0, tv->ccol, "%-d", i+1);
49 c = tv->ref? tv->ref[i - tv->left_pos] : 'N';
50 mvaddch(1, tv->ccol++, c);
52 if (pos%10 == 0) mvprintw(0, tv->ccol, "%-d", pos+1);
54 call = bam_maqcns_call(n, pl, tv->bmc);
56 c = ",ACMGRSVTWYHKDBN"[call>>28&0xf];
57 i = (call>>8&0xff)/10+1;
59 attr |= COLOR_PAIR(i);
60 if (c == toupper(rb)) c = '.';
62 mvaddch(2, tv->ccol, c);
65 // calculate maximum insert
66 for (i = 0; i < n; ++i) {
67 const bam_pileup1_t *p = pl + i;
68 if (p->indel > 0 && max_ins < p->indel) max_ins = p->indel;
72 for (j = 0; j <= max_ins; ++j) {
73 for (i = 0; i < n; ++i) {
74 const bam_pileup1_t *p = pl + i;
75 int row = TV_MIN_ALNROW + p->level - tv->row_shift;
78 if (tv->base_for == TV_BASE_COLOR_SPACE &&
79 (c = bam_aux_getCSi(p->b, p->qpos))) {
80 c = bam_aux_getCSi(p->b, p->qpos);
81 // assume that if we found one color, we will be able to get the color error
82 if (tv->is_dot && '-' == bam_aux_getCEi(p->b, p->qpos)) c = bam1_strand(p->b)? ',' : '.';
85 c = bam_nt16_rev_table[bam1_seqi(bam1_seq(p->b), p->qpos)];
86 if (tv->is_dot && toupper(c) == toupper(rb)) c = bam1_strand(p->b)? ',' : '.';
90 if (j > p->indel) c = '*';
92 if (tv->base_for == TV_BASE_NUCL) {
93 c = bam_nt16_rev_table[bam1_seqi(bam1_seq(p->b), p->qpos + j)];
94 if (j == 0 && tv->is_dot && toupper(c) == toupper(rb)) c = bam1_strand(p->b)? ',' : '.';
97 c = bam_aux_getCSi(p->b, p->qpos + j);
98 if (tv->is_dot && '-' == bam_aux_getCEi(p->b, p->qpos + j)) c = bam1_strand(p->b)? ',' : '.';
102 if (row > TV_MIN_ALNROW && row < tv->mrow) {
105 if (((p->b->core.flag&BAM_FPAIRED) && !(p->b->core.flag&BAM_FPROPER_PAIR))
106 || (p->b->core.flag & BAM_FSECONDARY)) attr |= A_UNDERLINE;
107 if (tv->color_for == TV_COLOR_BASEQ) {
108 x = bam1_qual(p->b)[p->qpos]/10 + 1;
110 attr |= COLOR_PAIR(x);
111 } else if (tv->color_for == TV_COLOR_MAPQ) {
112 x = p->b->core.qual/10 + 1;
114 attr |= COLOR_PAIR(x);
115 } else if (tv->color_for == TV_COLOR_NUCL) {
116 x = bam_nt16_nt4_table[bam1_seqi(bam1_seq(p->b), p->qpos)] + 5;
117 attr |= COLOR_PAIR(x);
118 } else if(tv->color_for == TV_COLOR_COL) {
120 switch(bam_aux_getCSi(p->b, p->qpos)) {
121 case '0': x = 0; break;
122 case '1': x = 1; break;
123 case '2': x = 2; break;
124 case '3': x = 3; break;
125 case '4': x = 4; break;
126 default: x = bam_nt16_nt4_table[bam1_seqi(bam1_seq(p->b), p->qpos)]; break;
129 attr |= COLOR_PAIR(x);
130 } else if(tv->color_for == TV_COLOR_COLQ) {
131 x = bam_aux_getCQi(p->b, p->qpos);
132 if(0 == x) x = bam1_qual(p->b)[p->qpos];
135 attr |= COLOR_PAIR(x);
138 mvaddch(row, tv->ccol, bam1_strand(p->b)? tolower(c) : toupper(c));
144 attr = COLOR_PAIR(8);
146 mvaddch(1, tv->ccol++, c);
148 } else mvaddch(1, tv->ccol++, c);
154 tview_t *tv_init(const char *fn, const char *fn_fa)
156 tview_t *tv = (tview_t*)calloc(1, sizeof(tview_t));
157 tv->idx = bam_index_load(fn);
158 tv->fp = bam_open(fn, "r");
160 tv->header = bam_header_read(tv->fp);
161 tv->lplbuf = bam_lplbuf_init(tv_pl_func, tv);
162 if (fn_fa) tv->fai = fai_load(fn_fa);
163 tv->bmc = bam_maqcns_init();
165 bam_maqcns_prepare(tv->bmc);
168 keypad(stdscr, TRUE);
172 #ifdef NCURSES_VERSION
173 getmaxyx(stdscr, tv->mrow, tv->mcol);
175 tv->mrow = 80; tv->mcol = 40;
177 tv->wgoto = newwin(3, TV_MAX_GOTO + 10, 10, 5);
178 tv->whelp = newwin(27, 40, 5, 5);
179 tv->color_for = TV_COLOR_MAPQ;
181 init_pair(1, COLOR_BLUE, COLOR_BLACK);
182 init_pair(2, COLOR_GREEN, COLOR_BLACK);
183 init_pair(3, COLOR_YELLOW, COLOR_BLACK);
184 init_pair(4, COLOR_WHITE, COLOR_BLACK);
185 init_pair(5, COLOR_GREEN, COLOR_BLACK);
186 init_pair(6, COLOR_CYAN, COLOR_BLACK);
187 init_pair(7, COLOR_YELLOW, COLOR_BLACK);
188 init_pair(8, COLOR_RED, COLOR_BLACK);
189 init_pair(9, COLOR_BLUE, COLOR_BLACK);
193 void tv_destroy(tview_t *tv)
195 delwin(tv->wgoto); delwin(tv->whelp);
198 bam_lplbuf_destroy(tv->lplbuf);
199 bam_maqcns_destroy(tv->bmc);
200 bam_index_destroy(tv->idx);
201 if (tv->fai) fai_destroy(tv->fai);
203 bam_header_destroy(tv->header);
208 int tv_fetch_func(const bam1_t *b, void *data)
210 tview_t *tv = (tview_t*)data;
211 bam_lplbuf_push(b, tv->lplbuf);
215 int tv_draw_aln(tview_t *tv, int tid, int pos)
219 tv->curr_tid = tid; tv->left_pos = pos;
220 tv->last_pos = tv->left_pos - 1;
222 // print ref and consensus
225 if (tv->ref) free(tv->ref);
226 str = (char*)calloc(strlen(tv->header->target_name[tv->curr_tid]) + 30, 1);
227 sprintf(str, "%s:%d-%d", tv->header->target_name[tv->curr_tid], tv->left_pos + 1, tv->left_pos + tv->mcol);
228 tv->ref = fai_fetch(tv->fai, str, &tv->l_ref);
232 bam_lplbuf_reset(tv->lplbuf);
233 bam_fetch(tv->fp, tv->idx, tv->curr_tid, tv->left_pos, tv->left_pos + tv->mcol, tv, tv_fetch_func);
234 bam_lplbuf_push(0, tv->lplbuf);
238 static void tv_win_goto(tview_t *tv, int *tid, int *pos)
242 wborder(tv->wgoto, '|', '|', '-', '-', '+', '+', '+', '+');
243 mvwprintw(tv->wgoto, 1, 2, "Goto: ");
245 int c = wgetch(tv->wgoto);
247 if (c == KEY_BACKSPACE || c == '\010' || c == '\177') {
249 } else if (c == KEY_ENTER || c == '\012' || c == '\015') {
250 int _tid = -1, _beg, _end;
251 bam_parse_region(tv->header, str, &_tid, &_beg, &_end);
253 *tid = _tid; *pos = _beg;
256 } else if (isgraph(c)) {
257 if (l < TV_MAX_GOTO) str[l++] = c;
258 } else if (c == '\027') l = 0;
259 else if (c == '\033') return;
261 for (i = 0; i < TV_MAX_GOTO; ++i) mvwaddch(tv->wgoto, 1, 8 + i, ' ');
262 mvwprintw(tv->wgoto, 1, 8, "%s", str);
266 static void tv_win_help(tview_t *tv) {
268 WINDOW *win = tv->whelp;
269 wborder(win, '|', '|', '-', '-', '+', '+', '+', '+');
270 mvwprintw(win, r++, 2, " -=- Help -=- ");
272 mvwprintw(win, r++, 2, "? This window");
273 mvwprintw(win, r++, 2, "Arrows Small scroll movement");
274 mvwprintw(win, r++, 2, "h,j,k,l Small scroll movement");
275 mvwprintw(win, r++, 2, "H,J,K,L Large scroll movement");
276 mvwprintw(win, r++, 2, "ctrl-H Scroll 1k left");
277 mvwprintw(win, r++, 2, "ctrl-L Scroll 1k right");
278 mvwprintw(win, r++, 2, "space Scroll one screen");
279 mvwprintw(win, r++, 2, "backspace Scroll back one screen");
280 mvwprintw(win, r++, 2, "g Go to specific location");
281 mvwprintw(win, r++, 2, "m Color for mapping qual");
282 mvwprintw(win, r++, 2, "n Color for nucleotide");
283 mvwprintw(win, r++, 2, "b Color for base quality");
284 mvwprintw(win, r++, 2, "c Color for cs color");
285 mvwprintw(win, r++, 2, "z Color for cs qual");
286 mvwprintw(win, r++, 2, ". Toggle on/off dot view");
287 mvwprintw(win, r++, 2, "N Turn on nt view");
288 mvwprintw(win, r++, 2, "C Turn on cs view");
289 mvwprintw(win, r++, 2, "i Toggle on/off ins");
290 mvwprintw(win, r++, 2, "q Exit");
292 mvwprintw(win, r++, 2, "Underline: Secondary or orphan");
293 mvwprintw(win, r++, 2, "Blue: 0-9 Green: 10-19");
294 mvwprintw(win, r++, 2, "Yellow: 20-29 White: >=30");
299 void tv_loop(tview_t *tv)
302 tid = tv->curr_tid; pos = tv->left_pos;
305 if(256 < c) {c = 1 + (c%256);} // Terminal was displaying ctrl-H as 263 via ssh from Mac OS X 10.5 computer
307 case '?': tv_win_help(tv); break;
309 case 'q': goto end_loop;
310 case 'g': tv_win_goto(tv, &tid, &pos); break;
311 case 'm': tv->color_for = TV_COLOR_MAPQ; break;
312 case 'b': tv->color_for = TV_COLOR_BASEQ; break;
313 case 'n': tv->color_for = TV_COLOR_NUCL; break;
314 case 'c': tv->color_for = TV_COLOR_COL; break;
315 case 'z': tv->color_for = TV_COLOR_COLQ; break;
317 case 'h': --pos; break;
319 case 'l': ++pos; break;
321 case 'H': pos -= 20; break;
323 case 'L': pos += 20; break;
324 case '.': tv->is_dot = !tv->is_dot; break;
325 case 'N': tv->base_for = TV_BASE_NUCL; break;
326 case 'C': tv->base_for = TV_BASE_COLOR_SPACE; break;
327 case 'i': tv->ins = !tv->ins; break;
328 case '\010': pos -= 1000; break;
329 case '\014': pos += 1000; break;
330 case ' ': pos += tv->mcol; break;
332 case 'j': --tv->row_shift; break;
334 case 'k': ++tv->row_shift; break;
336 case '\177': pos -= tv->mcol; break;
338 case KEY_RESIZE: getmaxyx(stdscr, tv->mrow, tv->mcol); break;
342 if (pos < 0) pos = 0;
343 if (tv->row_shift < 0) tv->row_shift = 0;
344 tv_draw_aln(tv, tid, pos);
350 int bam_tview_main(int argc, char *argv[])
354 fprintf(stderr, "Usage: bamtk tview <aln.bam> [ref.fasta]\n");
357 tv = tv_init(argv[1], (argc == 2)? 0 : argv[2]);
358 tv_draw_aln(tv, 0, 0);