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 int tv_pl_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pl, void *data)
36 tview_t *tv = (tview_t*)data;
37 int i, j, c, rb, attr, max_ins = 0;
39 if (pos < tv->left_pos || tv->ccol > tv->mcol) return 0; // out of screen
41 rb = (tv->ref && pos - tv->left_pos < tv->l_ref)? tv->ref[pos - tv->left_pos] : 'N';
42 for (i = tv->last_pos + 1; i < pos; ++i) {
43 if (i%10 == 0) mvprintw(0, tv->ccol, "%-d", i+1);
44 c = tv->ref? tv->ref[i - tv->left_pos] : 'N';
45 mvaddch(1, tv->ccol++, c);
47 if (pos%10 == 0) mvprintw(0, tv->ccol, "%-d", pos+1);
49 call = bam_maqcns_call(n, pl, tv->bmc);
51 c = ",ACMGRSVTWYHKDBN"[call>>28&0xf];
52 i = (call>>8&0xff)/10+1;
54 attr |= COLOR_PAIR(i);
55 if (c == toupper(rb)) c = '.';
57 mvaddch(2, tv->ccol, c);
59 // calculate maximum insert
60 for (i = 0; i < n; ++i) {
61 const bam_pileup1_t *p = pl + i;
62 if (p->indel > 0 && max_ins < p->indel) max_ins = p->indel;
65 for (j = 0; j <= max_ins; ++j) {
66 for (i = 0; i < n; ++i) {
67 const bam_pileup1_t *p = pl + i;
68 int row = TV_MIN_ALNROW + p->level - tv->row_shift;
71 c = bam_nt16_rev_table[bam1_seqi(bam1_seq(p->b), p->qpos)];
72 if (!tv->is_nucl && toupper(c) == toupper(rb)) c = bam1_strand(p->b)? ',' : '.';
75 if (j > p->indel) c = '*';
77 c = bam_nt16_rev_table[bam1_seqi(bam1_seq(p->b), p->qpos + j)];
78 if (j == 0 && !tv->is_nucl && toupper(c) == toupper(rb)) c = bam1_strand(p->b)? ',' : '.';
81 if (row > TV_MIN_ALNROW && row < tv->mrow) {
84 if (((p->b->core.flag&BAM_FPAIRED) && !(p->b->core.flag&BAM_FPROPER_PAIR))
85 || (p->b->core.flag & BAM_FSECONDARY)) attr |= A_UNDERLINE;
86 if (tv->color_for == TV_COLOR_BASEQ) {
87 x = bam1_qual(p->b)[p->qpos]/10 + 1;
89 attr |= COLOR_PAIR(x);
90 } else if (tv->color_for == TV_COLOR_MAPQ) {
91 x = p->b->core.qual/10 + 1;
93 attr |= COLOR_PAIR(x);
94 } else if (tv->color_for == TV_COLOR_NUCL) {
95 x = bam_nt16_nt4_table[bam1_seqi(bam1_seq(p->b), p->qpos)] + 5;
96 attr |= COLOR_PAIR(x);
99 mvaddch(row, tv->ccol, bam1_strand(p->b)? tolower(c) : toupper(c));
105 attr = COLOR_PAIR(8);
107 mvaddch(1, tv->ccol++, c);
109 } else mvaddch(1, tv->ccol++, c);
115 tview_t *tv_init(const char *fn, const char *fn_fa)
117 tview_t *tv = (tview_t*)calloc(1, sizeof(tview_t));
118 tv->idx = bam_index_load(fn);
119 tv->fp = bam_open(fn, "r");
121 tv->header = bam_header_read(tv->fp);
122 tv->lplbuf = bam_lplbuf_init(tv_pl_func, tv);
123 if (fn_fa) tv->fai = fai_load(fn_fa);
124 tv->bmc = bam_maqcns_init();
125 bam_maqcns_prepare(tv->bmc);
128 keypad(stdscr, TRUE);
132 #ifdef NCURSES_VERSION
133 getmaxyx(stdscr, tv->mrow, tv->mcol);
135 tv->mrow = 80; tv->mcol = 40;
137 tv->wgoto = newwin(3, TV_MAX_GOTO + 10, 10, 5);
138 tv->whelp = newwin(22,40, 5, 5);
139 tv->color_for = TV_COLOR_MAPQ;
141 init_pair(1, COLOR_BLUE, COLOR_BLACK);
142 init_pair(2, COLOR_GREEN, COLOR_BLACK);
143 init_pair(3, COLOR_YELLOW, COLOR_BLACK);
144 init_pair(4, COLOR_WHITE, COLOR_BLACK);
145 init_pair(5, COLOR_GREEN, COLOR_BLACK);
146 init_pair(6, COLOR_CYAN, COLOR_BLACK);
147 init_pair(7, COLOR_YELLOW, COLOR_BLACK);
148 init_pair(8, COLOR_RED, COLOR_BLACK);
149 init_pair(9, COLOR_BLUE, COLOR_BLACK);
153 void tv_destroy(tview_t *tv)
155 delwin(tv->wgoto); delwin(tv->whelp);
158 bam_lplbuf_destroy(tv->lplbuf);
159 bam_maqcns_destroy(tv->bmc);
160 bam_index_destroy(tv->idx);
161 if (tv->fai) fai_destroy(tv->fai);
163 bam_header_destroy(tv->header);
168 int tv_fetch_func(const bam1_t *b, void *data)
170 tview_t *tv = (tview_t*)data;
171 bam_lplbuf_push(b, tv->lplbuf);
175 int tv_draw_aln(tview_t *tv, int tid, int pos)
179 tv->curr_tid = tid; tv->left_pos = pos;
180 tv->last_pos = tv->left_pos - 1;
182 // print ref and consensus
185 if (tv->ref) free(tv->ref);
186 str = (char*)calloc(strlen(tv->header->target_name[tv->curr_tid]) + 30, 1);
187 sprintf(str, "%s:%d-%d", tv->header->target_name[tv->curr_tid], tv->left_pos + 1, tv->left_pos + tv->mcol);
188 tv->ref = fai_fetch(tv->fai, str, &tv->l_ref);
192 bam_lplbuf_reset(tv->lplbuf);
193 bam_fetch(tv->fp, tv->idx, tv->curr_tid, tv->left_pos, tv->left_pos + tv->mcol, tv, tv_fetch_func);
194 bam_lplbuf_push(0, tv->lplbuf);
198 static void tv_win_goto(tview_t *tv, int *tid, int *pos)
202 wborder(tv->wgoto, '|', '|', '-', '-', '+', '+', '+', '+');
203 mvwprintw(tv->wgoto, 1, 2, "Goto: ");
205 int c = wgetch(tv->wgoto);
207 if (c == KEY_BACKSPACE || c == '\010' || c == '\177') {
209 } else if (c == KEY_ENTER || c == '\012' || c == '\015') {
210 int _tid = -1, _beg, _end;
211 bam_parse_region(tv->header, str, &_tid, &_beg, &_end);
213 *tid = _tid; *pos = _beg;
216 } else if (isgraph(c)) {
217 if (l < TV_MAX_GOTO) str[l++] = c;
218 } else if (c == '\027') l = 0;
219 else if (c == '\033') return;
221 for (i = 0; i < TV_MAX_GOTO; ++i) mvwaddch(tv->wgoto, 1, 8 + i, ' ');
222 mvwprintw(tv->wgoto, 1, 8, "%s", str);
226 static void tv_win_help(tview_t *tv) {
228 WINDOW *win = tv->whelp;
229 wborder(win, '|', '|', '-', '-', '+', '+', '+', '+');
230 mvwprintw(win, r++, 2, " -=- Help -=- ");
232 mvwprintw(win, r++, 2, "? This window");
233 mvwprintw(win, r++, 2, "Arrows Small scroll movement");
234 mvwprintw(win, r++, 2, "h,j,k,l Small scroll movement");
235 mvwprintw(win, r++, 2, "H,J,K,L Large scroll movement");
236 mvwprintw(win, r++, 2, "ctrl-H Scroll 1k left");
237 mvwprintw(win, r++, 2, "ctrl-L Scroll 1k right");
238 mvwprintw(win, r++, 2, "space Scroll one screen");
239 mvwprintw(win, r++, 2, "backspace Scroll back one screen");
240 mvwprintw(win, r++, 2, "g Go to specific location");
241 mvwprintw(win, r++, 2, "b Color for base quality");
242 mvwprintw(win, r++, 2, "m Color for mapping qual");
243 mvwprintw(win, r++, 2, "n Color for nucleotide");
244 mvwprintw(win, r++, 2, ". Toggle on/off dot view");
245 mvwprintw(win, r++, 2, "q Exit");
247 mvwprintw(win, r++, 2, "Underline: Secondary or orphan");
248 mvwprintw(win, r++, 2, "Blue: 0-9 Green: 10-19");
249 mvwprintw(win, r++, 2, "Yellow: 20-29 White: >=30");
254 void tv_loop(tview_t *tv)
257 tid = tv->curr_tid; pos = tv->left_pos;
260 if(256 < c) {c = 1 + (c%256);} // Terminal was displaying ctrl-H as 263 via ssh from Mac OS X 10.5 computer
262 case '?': tv_win_help(tv); break;
264 case 'q': goto end_loop;
265 case 'g': tv_win_goto(tv, &tid, &pos); break;
266 case 'b': tv->color_for = TV_COLOR_BASEQ; break;
267 case 'm': tv->color_for = TV_COLOR_MAPQ; break;
268 case 'n': tv->color_for = TV_COLOR_NUCL; break;
270 case 'h': --pos; break;
272 case 'l': ++pos; break;
274 case 'H': pos -= 20; break;
276 case 'L': pos += 20; break;
277 case '.': tv->is_nucl = !tv->is_nucl; break;
278 case '\010': pos -= 1000; break;
279 case '\014': pos += 1000; break;
280 case ' ': pos += tv->mcol; break;
282 case 'j': --tv->row_shift; break;
284 case 'k': ++tv->row_shift; break;
286 case '\177': pos -= tv->mcol; break;
288 case KEY_RESIZE: getmaxyx(stdscr, tv->mrow, tv->mcol); break;
292 if (pos < 0) pos = 0;
293 if (tv->row_shift < 0) tv->row_shift = 0;
294 tv_draw_aln(tv, tid, pos);
300 int bam_tview_main(int argc, char *argv[])
304 fprintf(stderr, "Usage: bamtk tview <aln.bam> [ref.fasta]\n");
307 tv = tv_init(argv[1], (argc == 2)? 0 : argv[2]);
308 tv_draw_aln(tv, 0, 0);