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));
158 tv->idx = bam_index_load(fn);
159 tv->fp = bam_open(fn, "r");
161 tv->header = bam_header_read(tv->fp);
162 tv->lplbuf = bam_lplbuf_init(tv_pl_func, tv);
163 if (fn_fa) tv->fai = fai_load(fn_fa);
164 tv->bmc = bam_maqcns_init();
166 bam_maqcns_prepare(tv->bmc);
169 keypad(stdscr, TRUE);
173 #ifdef NCURSES_VERSION
174 getmaxyx(stdscr, tv->mrow, tv->mcol);
176 tv->mrow = 80; tv->mcol = 40;
178 tv->wgoto = newwin(3, TV_MAX_GOTO + 10, 10, 5);
179 tv->whelp = newwin(27, 40, 5, 5);
180 tv->color_for = TV_COLOR_MAPQ;
182 init_pair(1, COLOR_BLUE, COLOR_BLACK);
183 init_pair(2, COLOR_GREEN, COLOR_BLACK);
184 init_pair(3, COLOR_YELLOW, COLOR_BLACK);
185 init_pair(4, COLOR_WHITE, COLOR_BLACK);
186 init_pair(5, COLOR_GREEN, COLOR_BLACK);
187 init_pair(6, COLOR_CYAN, COLOR_BLACK);
188 init_pair(7, COLOR_YELLOW, COLOR_BLACK);
189 init_pair(8, COLOR_RED, COLOR_BLACK);
190 init_pair(9, COLOR_BLUE, COLOR_BLACK);
194 void tv_destroy(tview_t *tv)
196 delwin(tv->wgoto); delwin(tv->whelp);
199 bam_lplbuf_destroy(tv->lplbuf);
200 bam_maqcns_destroy(tv->bmc);
201 bam_index_destroy(tv->idx);
202 if (tv->fai) fai_destroy(tv->fai);
204 bam_header_destroy(tv->header);
209 int tv_fetch_func(const bam1_t *b, void *data)
211 tview_t *tv = (tview_t*)data;
212 bam_lplbuf_push(b, tv->lplbuf);
216 int tv_draw_aln(tview_t *tv, int tid, int pos)
220 tv->curr_tid = tid; tv->left_pos = pos;
221 tv->last_pos = tv->left_pos - 1;
223 // print ref and consensus
226 if (tv->ref) free(tv->ref);
227 str = (char*)calloc(strlen(tv->header->target_name[tv->curr_tid]) + 30, 1);
228 sprintf(str, "%s:%d-%d", tv->header->target_name[tv->curr_tid], tv->left_pos + 1, tv->left_pos + tv->mcol);
229 tv->ref = fai_fetch(tv->fai, str, &tv->l_ref);
233 bam_lplbuf_reset(tv->lplbuf);
234 bam_fetch(tv->fp, tv->idx, tv->curr_tid, tv->left_pos, tv->left_pos + tv->mcol, tv, tv_fetch_func);
235 bam_lplbuf_push(0, tv->lplbuf);
239 static void tv_win_goto(tview_t *tv, int *tid, int *pos)
243 wborder(tv->wgoto, '|', '|', '-', '-', '+', '+', '+', '+');
244 mvwprintw(tv->wgoto, 1, 2, "Goto: ");
246 int c = wgetch(tv->wgoto);
248 if (c == KEY_BACKSPACE || c == '\010' || c == '\177') {
250 } else if (c == KEY_ENTER || c == '\012' || c == '\015') {
251 int _tid = -1, _beg, _end;
252 bam_parse_region(tv->header, str, &_tid, &_beg, &_end);
254 *tid = _tid; *pos = _beg;
257 } else if (isgraph(c)) {
258 if (l < TV_MAX_GOTO) str[l++] = c;
259 } else if (c == '\027') l = 0;
260 else if (c == '\033') return;
262 for (i = 0; i < TV_MAX_GOTO; ++i) mvwaddch(tv->wgoto, 1, 8 + i, ' ');
263 mvwprintw(tv->wgoto, 1, 8, "%s", str);
267 static void tv_win_help(tview_t *tv) {
269 WINDOW *win = tv->whelp;
270 wborder(win, '|', '|', '-', '-', '+', '+', '+', '+');
271 mvwprintw(win, r++, 2, " -=- Help -=- ");
273 mvwprintw(win, r++, 2, "? This window");
274 mvwprintw(win, r++, 2, "Arrows Small scroll movement");
275 mvwprintw(win, r++, 2, "h,j,k,l Small scroll movement");
276 mvwprintw(win, r++, 2, "H,J,K,L Large scroll movement");
277 mvwprintw(win, r++, 2, "ctrl-H Scroll 1k left");
278 mvwprintw(win, r++, 2, "ctrl-L Scroll 1k right");
279 mvwprintw(win, r++, 2, "space Scroll one screen");
280 mvwprintw(win, r++, 2, "backspace Scroll back one screen");
281 mvwprintw(win, r++, 2, "g Go to specific location");
282 mvwprintw(win, r++, 2, "m Color for mapping qual");
283 mvwprintw(win, r++, 2, "n Color for nucleotide");
284 mvwprintw(win, r++, 2, "b Color for base quality");
285 mvwprintw(win, r++, 2, "c Color for cs color");
286 mvwprintw(win, r++, 2, "z Color for cs qual");
287 mvwprintw(win, r++, 2, ". Toggle on/off dot view");
288 mvwprintw(win, r++, 2, "N Turn on nt view");
289 mvwprintw(win, r++, 2, "C Turn on cs view");
290 mvwprintw(win, r++, 2, "i Toggle on/off ins");
291 mvwprintw(win, r++, 2, "q Exit");
293 mvwprintw(win, r++, 2, "Underline: Secondary or orphan");
294 mvwprintw(win, r++, 2, "Blue: 0-9 Green: 10-19");
295 mvwprintw(win, r++, 2, "Yellow: 20-29 White: >=30");
300 void tv_loop(tview_t *tv)
303 tid = tv->curr_tid; pos = tv->left_pos;
306 if(256 < c) {c = 1 + (c%256);} // Terminal was displaying ctrl-H as 263 via ssh from Mac OS X 10.5 computer
308 case '?': tv_win_help(tv); break;
310 case 'q': goto end_loop;
311 case 'g': tv_win_goto(tv, &tid, &pos); break;
312 case 'm': tv->color_for = TV_COLOR_MAPQ; break;
313 case 'b': tv->color_for = TV_COLOR_BASEQ; break;
314 case 'n': tv->color_for = TV_COLOR_NUCL; break;
315 case 'c': tv->color_for = TV_COLOR_COL; break;
316 case 'z': tv->color_for = TV_COLOR_COLQ; break;
318 case 'h': --pos; break;
320 case 'l': ++pos; break;
322 case 'H': pos -= 20; break;
324 case 'L': pos += 20; break;
325 case '.': tv->is_dot = !tv->is_dot; break;
326 case 'N': tv->base_for = TV_BASE_NUCL; break;
327 case 'C': tv->base_for = TV_BASE_COLOR_SPACE; break;
328 case 'i': tv->ins = !tv->ins; break;
329 case '\010': pos -= 1000; break;
330 case '\014': pos += 1000; break;
331 case ' ': pos += tv->mcol; break;
333 case 'j': --tv->row_shift; break;
335 case 'k': ++tv->row_shift; break;
337 case '\177': pos -= tv->mcol; break;
339 case KEY_RESIZE: getmaxyx(stdscr, tv->mrow, tv->mcol); break;
343 if (pos < 0) pos = 0;
344 if (tv->row_shift < 0) tv->row_shift = 0;
345 tv_draw_aln(tv, tid, pos);
351 int bam_tview_main(int argc, char *argv[])
355 fprintf(stderr, "Usage: bamtk tview <aln.bam> [ref.fasta]\n");
358 tv = tv_init(argv[1], (argc == 2)? 0 : argv[2]);
359 tv_draw_aln(tv, 0, 0);