9 #include "bam_maqcns.h"
11 char bam_aux_getCEi(bam1_t *b, int i);
12 char bam_aux_getCSi(bam1_t *b, int i);
13 char bam_aux_getCQi(bam1_t *b, int i);
15 #define TV_MIN_ALNROW 2
16 #define TV_MAX_GOTO 40
17 #define TV_LOW_MAPQ 10
19 #define TV_COLOR_MAPQ 0
20 #define TV_COLOR_BASEQ 1
21 #define TV_COLOR_NUCL 2
22 #define TV_COLOR_COL 3
23 #define TV_COLOR_COLQ 4
25 #define TV_BASE_NUCL 0
26 #define TV_BASE_COLOR_SPACE 1
30 WINDOW *wgoto, *whelp;
36 int curr_tid, left_pos;
40 int ccol, last_pos, row_shift, base_for, color_for, is_dot, l_ref, ins;
44 int tv_pl_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pl, void *data)
46 tview_t *tv = (tview_t*)data;
47 int i, j, c, rb, attr, max_ins = 0;
49 if (pos < tv->left_pos || tv->ccol > tv->mcol) return 0; // out of screen
51 rb = (tv->ref && pos - tv->left_pos < tv->l_ref)? tv->ref[pos - tv->left_pos] : 'N';
52 for (i = tv->last_pos + 1; i < pos; ++i) {
53 if (i%10 == 0) mvprintw(0, tv->ccol, "%-d", i+1);
54 c = tv->ref? tv->ref[i - tv->left_pos] : 'N';
55 mvaddch(1, tv->ccol++, c);
57 if (pos%10 == 0) mvprintw(0, tv->ccol, "%-d", pos+1);
59 call = bam_maqcns_call(n, pl, tv->bmc);
61 c = ",ACMGRSVTWYHKDBN"[call>>28&0xf];
62 i = (call>>8&0xff)/10+1;
64 attr |= COLOR_PAIR(i);
65 if (c == toupper(rb)) c = '.';
67 mvaddch(2, tv->ccol, c);
70 // calculate maximum insert
71 for (i = 0; i < n; ++i) {
72 const bam_pileup1_t *p = pl + i;
73 if (p->indel > 0 && max_ins < p->indel) max_ins = p->indel;
77 for (j = 0; j <= max_ins; ++j) {
78 for (i = 0; i < n; ++i) {
79 const bam_pileup1_t *p = pl + i;
80 int row = TV_MIN_ALNROW + p->level - tv->row_shift;
83 if (tv->base_for == TV_BASE_COLOR_SPACE &&
84 (c = bam_aux_getCSi(p->b, p->qpos))) {
85 c = bam_aux_getCSi(p->b, p->qpos);
86 // assume that if we found one color, we will be able to get the color error
87 if (tv->is_dot && '-' == bam_aux_getCEi(p->b, p->qpos)) c = bam1_strand(p->b)? ',' : '.';
90 c = bam_nt16_rev_table[bam1_seqi(bam1_seq(p->b), p->qpos)];
91 if (tv->is_dot && toupper(c) == toupper(rb)) c = bam1_strand(p->b)? ',' : '.';
95 if (j > p->indel) c = '*';
97 if (tv->base_for == TV_BASE_NUCL) {
98 c = bam_nt16_rev_table[bam1_seqi(bam1_seq(p->b), p->qpos + j)];
99 if (j == 0 && tv->is_dot && toupper(c) == toupper(rb)) c = bam1_strand(p->b)? ',' : '.';
102 c = bam_aux_getCSi(p->b, p->qpos + j);
103 if (tv->is_dot && '-' == bam_aux_getCEi(p->b, p->qpos + j)) c = bam1_strand(p->b)? ',' : '.';
107 if (row > TV_MIN_ALNROW && row < tv->mrow) {
110 if (((p->b->core.flag&BAM_FPAIRED) && !(p->b->core.flag&BAM_FPROPER_PAIR))
111 || (p->b->core.flag & BAM_FSECONDARY)) attr |= A_UNDERLINE;
112 if (tv->color_for == TV_COLOR_BASEQ) {
113 x = bam1_qual(p->b)[p->qpos]/10 + 1;
115 attr |= COLOR_PAIR(x);
116 } else if (tv->color_for == TV_COLOR_MAPQ) {
117 x = p->b->core.qual/10 + 1;
119 attr |= COLOR_PAIR(x);
120 } else if (tv->color_for == TV_COLOR_NUCL) {
121 x = bam_nt16_nt4_table[bam1_seqi(bam1_seq(p->b), p->qpos)] + 5;
122 attr |= COLOR_PAIR(x);
123 } else if(tv->color_for == TV_COLOR_COL) {
125 switch(bam_aux_getCSi(p->b, p->qpos)) {
126 case '0': x = 0; break;
127 case '1': x = 1; break;
128 case '2': x = 2; break;
129 case '3': x = 3; break;
130 case '4': x = 4; break;
131 default: x = bam_nt16_nt4_table[bam1_seqi(bam1_seq(p->b), p->qpos)]; break;
134 attr |= COLOR_PAIR(x);
135 } else if(tv->color_for == TV_COLOR_COLQ) {
136 x = bam_aux_getCQi(p->b, p->qpos);
137 if(0 == x) x = bam1_qual(p->b)[p->qpos];
140 attr |= COLOR_PAIR(x);
143 mvaddch(row, tv->ccol, bam1_strand(p->b)? tolower(c) : toupper(c));
149 attr = COLOR_PAIR(8);
151 mvaddch(1, tv->ccol++, c);
153 } else mvaddch(1, tv->ccol++, c);
159 tview_t *tv_init(const char *fn, const char *fn_fa)
161 tview_t *tv = (tview_t*)calloc(1, sizeof(tview_t));
163 tv->idx = bam_index_load(fn);
164 tv->fp = bam_open(fn, "r");
166 tv->header = bam_header_read(tv->fp);
167 tv->lplbuf = bam_lplbuf_init(tv_pl_func, tv);
168 if (fn_fa) tv->fai = fai_load(fn_fa);
169 tv->bmc = bam_maqcns_init();
171 bam_maqcns_prepare(tv->bmc);
174 keypad(stdscr, TRUE);
178 #ifdef NCURSES_VERSION
179 getmaxyx(stdscr, tv->mrow, tv->mcol);
181 tv->mrow = 80; tv->mcol = 40;
183 tv->wgoto = newwin(3, TV_MAX_GOTO + 10, 10, 5);
184 tv->whelp = newwin(27, 40, 5, 5);
185 tv->color_for = TV_COLOR_MAPQ;
187 init_pair(1, COLOR_BLUE, COLOR_BLACK);
188 init_pair(2, COLOR_GREEN, COLOR_BLACK);
189 init_pair(3, COLOR_YELLOW, COLOR_BLACK);
190 init_pair(4, COLOR_WHITE, COLOR_BLACK);
191 init_pair(5, COLOR_GREEN, COLOR_BLACK);
192 init_pair(6, COLOR_CYAN, COLOR_BLACK);
193 init_pair(7, COLOR_YELLOW, COLOR_BLACK);
194 init_pair(8, COLOR_RED, COLOR_BLACK);
195 init_pair(9, COLOR_BLUE, COLOR_BLACK);
199 void tv_destroy(tview_t *tv)
201 delwin(tv->wgoto); delwin(tv->whelp);
204 bam_lplbuf_destroy(tv->lplbuf);
205 bam_maqcns_destroy(tv->bmc);
206 bam_index_destroy(tv->idx);
207 if (tv->fai) fai_destroy(tv->fai);
209 bam_header_destroy(tv->header);
214 int tv_fetch_func(const bam1_t *b, void *data)
216 tview_t *tv = (tview_t*)data;
217 bam_lplbuf_push(b, tv->lplbuf);
221 int tv_draw_aln(tview_t *tv, int tid, int pos)
225 tv->curr_tid = tid; tv->left_pos = pos;
226 tv->last_pos = tv->left_pos - 1;
228 // print ref and consensus
231 if (tv->ref) free(tv->ref);
232 str = (char*)calloc(strlen(tv->header->target_name[tv->curr_tid]) + 30, 1);
233 sprintf(str, "%s:%d-%d", tv->header->target_name[tv->curr_tid], tv->left_pos + 1, tv->left_pos + tv->mcol);
234 tv->ref = fai_fetch(tv->fai, str, &tv->l_ref);
238 bam_lplbuf_reset(tv->lplbuf);
239 bam_fetch(tv->fp, tv->idx, tv->curr_tid, tv->left_pos, tv->left_pos + tv->mcol, tv, tv_fetch_func);
240 bam_lplbuf_push(0, tv->lplbuf);
244 static void tv_win_goto(tview_t *tv, int *tid, int *pos)
248 wborder(tv->wgoto, '|', '|', '-', '-', '+', '+', '+', '+');
249 mvwprintw(tv->wgoto, 1, 2, "Goto: ");
251 int c = wgetch(tv->wgoto);
253 if (c == KEY_BACKSPACE || c == '\010' || c == '\177') {
255 } else if (c == KEY_ENTER || c == '\012' || c == '\015') {
256 int _tid = -1, _beg, _end;
257 bam_parse_region(tv->header, str, &_tid, &_beg, &_end);
259 *tid = _tid; *pos = _beg;
262 } else if (isgraph(c)) {
263 if (l < TV_MAX_GOTO) str[l++] = c;
264 } else if (c == '\027') l = 0;
265 else if (c == '\033') return;
267 for (i = 0; i < TV_MAX_GOTO; ++i) mvwaddch(tv->wgoto, 1, 8 + i, ' ');
268 mvwprintw(tv->wgoto, 1, 8, "%s", str);
272 static void tv_win_help(tview_t *tv) {
274 WINDOW *win = tv->whelp;
275 wborder(win, '|', '|', '-', '-', '+', '+', '+', '+');
276 mvwprintw(win, r++, 2, " -=- Help -=- ");
278 mvwprintw(win, r++, 2, "? This window");
279 mvwprintw(win, r++, 2, "Arrows Small scroll movement");
280 mvwprintw(win, r++, 2, "h,j,k,l Small scroll movement");
281 mvwprintw(win, r++, 2, "H,J,K,L Large scroll movement");
282 mvwprintw(win, r++, 2, "ctrl-H Scroll 1k left");
283 mvwprintw(win, r++, 2, "ctrl-L Scroll 1k right");
284 mvwprintw(win, r++, 2, "space Scroll one screen");
285 mvwprintw(win, r++, 2, "backspace Scroll back one screen");
286 mvwprintw(win, r++, 2, "g Go to specific location");
287 mvwprintw(win, r++, 2, "m Color for mapping qual");
288 mvwprintw(win, r++, 2, "n Color for nucleotide");
289 mvwprintw(win, r++, 2, "b Color for base quality");
290 mvwprintw(win, r++, 2, "c Color for cs color");
291 mvwprintw(win, r++, 2, "z Color for cs qual");
292 mvwprintw(win, r++, 2, ". Toggle on/off dot view");
293 mvwprintw(win, r++, 2, "N Turn on nt view");
294 mvwprintw(win, r++, 2, "C Turn on cs view");
295 mvwprintw(win, r++, 2, "i Toggle on/off ins");
296 mvwprintw(win, r++, 2, "q Exit");
298 mvwprintw(win, r++, 2, "Underline: Secondary or orphan");
299 mvwprintw(win, r++, 2, "Blue: 0-9 Green: 10-19");
300 mvwprintw(win, r++, 2, "Yellow: 20-29 White: >=30");
305 void tv_loop(tview_t *tv)
308 tid = tv->curr_tid; pos = tv->left_pos;
311 //if(256 < c) {c = 1 + (c%256);} // Terminal was displaying ctrl-H as 263 via ssh from Mac OS X 10.5 computer
313 case '?': tv_win_help(tv); break;
315 case 'q': goto end_loop;
316 case 'g': tv_win_goto(tv, &tid, &pos); break;
317 case 'm': tv->color_for = TV_COLOR_MAPQ; break;
318 case 'b': tv->color_for = TV_COLOR_BASEQ; break;
319 case 'n': tv->color_for = TV_COLOR_NUCL; break;
320 case 'c': tv->color_for = TV_COLOR_COL; break;
321 case 'z': tv->color_for = TV_COLOR_COLQ; break;
323 case 'h': --pos; break;
325 case 'l': ++pos; break;
327 case 'H': pos -= 20; break;
329 case 'L': pos += 20; break;
330 case '.': tv->is_dot = !tv->is_dot; break;
331 case 'N': tv->base_for = TV_BASE_NUCL; break;
332 case 'C': tv->base_for = TV_BASE_COLOR_SPACE; break;
333 case 'i': tv->ins = !tv->ins; break;
334 case '\010': pos -= 1000; break;
335 case '\014': pos += 1000; break;
336 case ' ': pos += tv->mcol; break;
338 case 'j': --tv->row_shift; break;
340 case 'k': ++tv->row_shift; break;
342 case '\177': pos -= tv->mcol; break;
344 case KEY_RESIZE: getmaxyx(stdscr, tv->mrow, tv->mcol); break;
348 if (pos < 0) pos = 0;
349 if (tv->row_shift < 0) tv->row_shift = 0;
350 tv_draw_aln(tv, tid, pos);
356 int bam_tview_main(int argc, char *argv[])
360 fprintf(stderr, "Usage: bamtk tview <aln.bam> [ref.fasta]\n");
363 tv = tv_init(argv[1], (argc == 2)? 0 : argv[2]);
364 tv_draw_aln(tv, 0, 0);
369 #else // #ifdef NCURSES_VERSION
370 #warning "The ncurses library is unavailable; tview is disabled."
371 int bam_tview_main(int argc, char *argv[])
373 fprintf(stderr, "[bam_tview_main] The ncurses library is unavailable; tview is not compiled.\n");
377 #endif // #ifndef _NO_CURSES