]> git.donarmstrong.com Git - samtools.git/blob - bam_tview.c
* samtools-0.1.4-18 (r363)
[samtools.git] / bam_tview.c
1 #ifndef _NO_CURSES
2 #include <curses.h>
3 #ifdef NCURSES_VERSION
4 #include <ctype.h>
5 #include <assert.h>
6 #include <string.h>
7 #include "bam.h"
8 #include "faidx.h"
9 #include "bam_maqcns.h"
10
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);
14
15 #define TV_MIN_ALNROW 2
16 #define TV_MAX_GOTO  40
17 #define TV_LOW_MAPQ  10
18
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
24
25 #define TV_BASE_NUCL 0
26 #define TV_BASE_COLOR_SPACE 1
27
28 typedef struct {
29         int mrow, mcol;
30         WINDOW *wgoto, *whelp;
31
32         bam_index_t *idx;
33         bam_lplbuf_t *lplbuf;
34         bam_header_t *header;
35         bamFile fp;
36         int curr_tid, left_pos;
37         faidx_t *fai;
38         bam_maqcns_t *bmc;
39
40         int ccol, last_pos, row_shift, base_for, color_for, is_dot, l_ref, ins;
41         char *ref;
42 } tview_t;
43
44 int tv_pl_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pl, void *data)
45 {
46         tview_t *tv = (tview_t*)data;
47         int i, j, c, rb, attr, max_ins = 0;
48         uint32_t call = 0;
49         if (pos < tv->left_pos || tv->ccol > tv->mcol) return 0; // out of screen
50         // print referece
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);
56         }
57         if (pos%10 == 0) mvprintw(0, tv->ccol, "%-d", pos+1);
58         // print consensus
59         call = bam_maqcns_call(n, pl, tv->bmc);
60         attr = A_UNDERLINE;
61         c = ",ACMGRSVTWYHKDBN"[call>>28&0xf];
62         i = (call>>8&0xff)/10+1;
63         if (i > 4) i = 4;
64         attr |= COLOR_PAIR(i);
65         if (c == toupper(rb)) c = '.';
66         attron(attr);
67         mvaddch(2, tv->ccol, c);
68         attroff(attr);
69         if(tv->ins) {
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;
74                 }
75         }
76         // core loop
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;
81                         if (j == 0) {
82                                 if (!p->is_del) {
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)? ',' : '.';
88                                         }
89                                         else {
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)? ',' : '.';
92                                         }
93                                 } else c = '*';
94                         } else { // padding
95                                 if (j > p->indel) c = '*';
96                                 else { // insertion
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)? ',' : '.';
100                                         }
101                                         else {
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)? ',' : '.';
104                                         }
105                                 }
106                         }
107                         if (row > TV_MIN_ALNROW && row < tv->mrow) {
108                                 int x;
109                                 attr = 0;
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;
114                                         if (x > 4) x = 4;
115                                         attr |= COLOR_PAIR(x);
116                                 } else if (tv->color_for == TV_COLOR_MAPQ) {
117                                         x = p->b->core.qual/10 + 1;
118                                         if (x > 4) x = 4;
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) {
124                                         x = 0;
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;
132                                         }
133                                         x+=5;
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];
138                                         x = x/10 + 1;
139                                         if (x > 4) x = 4;
140                                         attr |= COLOR_PAIR(x);
141                                 }
142                                 attron(attr);
143                                 mvaddch(row, tv->ccol, bam1_strand(p->b)? tolower(c) : toupper(c));
144                                 attroff(attr);
145                         }
146                 }
147                 c = j? '*' : rb;
148                 if (c == '*') {
149                         attr = COLOR_PAIR(8);
150                         attron(attr);
151                         mvaddch(1, tv->ccol++, c);
152                         attroff(attr);
153                 } else mvaddch(1, tv->ccol++, c);
154         }
155         tv->last_pos = pos;
156         return 0;
157 }
158
159 tview_t *tv_init(const char *fn, const char *fn_fa)
160 {
161         tview_t *tv = (tview_t*)calloc(1, sizeof(tview_t));
162         tv->is_dot = 1;
163         tv->idx = bam_index_load(fn);
164         tv->fp = bam_open(fn, "r");
165         bgzf_set_cache_size(tv->fp, 8 * 1024 *1024);
166         assert(tv->fp);
167         tv->header = bam_header_read(tv->fp);
168         tv->lplbuf = bam_lplbuf_init(tv_pl_func, tv);
169         if (fn_fa) tv->fai = fai_load(fn_fa);
170         tv->bmc = bam_maqcns_init();
171         tv->ins = 1;
172         bam_maqcns_prepare(tv->bmc);
173
174         initscr();
175         keypad(stdscr, TRUE);
176         clear();
177         noecho();
178         cbreak();
179 #ifdef NCURSES_VERSION
180         getmaxyx(stdscr, tv->mrow, tv->mcol);
181 #else
182         tv->mrow = 80; tv->mcol = 40;
183 #endif
184         tv->wgoto = newwin(3, TV_MAX_GOTO + 10, 10, 5);
185         tv->whelp = newwin(27, 40, 5, 5);
186         tv->color_for = TV_COLOR_MAPQ;
187         start_color();
188         init_pair(1, COLOR_BLUE, COLOR_BLACK);
189         init_pair(2, COLOR_GREEN, COLOR_BLACK);
190         init_pair(3, COLOR_YELLOW, COLOR_BLACK);
191         init_pair(4, COLOR_WHITE, COLOR_BLACK);
192         init_pair(5, COLOR_GREEN, COLOR_BLACK);
193         init_pair(6, COLOR_CYAN, COLOR_BLACK);
194         init_pair(7, COLOR_YELLOW, COLOR_BLACK);
195         init_pair(8, COLOR_RED, COLOR_BLACK);
196         init_pair(9, COLOR_BLUE, COLOR_BLACK);
197         return tv;
198 }
199
200 void tv_destroy(tview_t *tv)
201 {
202         delwin(tv->wgoto); delwin(tv->whelp);
203         endwin();
204
205         bam_lplbuf_destroy(tv->lplbuf);
206         bam_maqcns_destroy(tv->bmc);
207         bam_index_destroy(tv->idx);
208         if (tv->fai) fai_destroy(tv->fai);
209         free(tv->ref);
210         bam_header_destroy(tv->header);
211         bam_close(tv->fp);
212         free(tv);
213 }
214
215 int tv_fetch_func(const bam1_t *b, void *data)
216 {
217         tview_t *tv = (tview_t*)data;
218         bam_lplbuf_push(b, tv->lplbuf);
219         return 0;
220 }
221
222 int tv_draw_aln(tview_t *tv, int tid, int pos)
223 {
224         // reset
225         clear();
226         tv->curr_tid = tid; tv->left_pos = pos;
227         tv->last_pos = tv->left_pos - 1;
228         tv->ccol = 0;
229         // print ref and consensus
230         if (tv->fai) {
231                 char *str;
232                 if (tv->ref) free(tv->ref);
233                 str = (char*)calloc(strlen(tv->header->target_name[tv->curr_tid]) + 30, 1);
234                 sprintf(str, "%s:%d-%d", tv->header->target_name[tv->curr_tid], tv->left_pos + 1, tv->left_pos + tv->mcol);
235                 tv->ref = fai_fetch(tv->fai, str, &tv->l_ref);
236                 free(str);
237         }
238         // draw aln
239         bam_lplbuf_reset(tv->lplbuf);
240         bam_fetch(tv->fp, tv->idx, tv->curr_tid, tv->left_pos, tv->left_pos + tv->mcol, tv, tv_fetch_func);
241         bam_lplbuf_push(0, tv->lplbuf);
242         return 0;
243 }
244
245 static void tv_win_goto(tview_t *tv, int *tid, int *pos)
246 {
247         char str[256];
248         int i, l = 0;
249         wborder(tv->wgoto, '|', '|', '-', '-', '+', '+', '+', '+');
250         mvwprintw(tv->wgoto, 1, 2, "Goto: ");
251         for (;;) {
252                 int c = wgetch(tv->wgoto);
253                 wrefresh(tv->wgoto);
254                 if (c == KEY_BACKSPACE || c == '\010' || c == '\177') {
255                         --l;
256                 } else if (c == KEY_ENTER || c == '\012' || c == '\015') {
257                         int _tid = -1, _beg, _end;
258                         bam_parse_region(tv->header, str, &_tid, &_beg, &_end);
259                         if (_tid >= 0) {
260                                 *tid = _tid; *pos = _beg;
261                                 return;
262                         }
263                 } else if (isgraph(c)) {
264                         if (l < TV_MAX_GOTO) str[l++] = c;
265                 } else if (c == '\027') l = 0;
266                 else if (c == '\033') return;
267                 str[l] = '\0';
268                 for (i = 0; i < TV_MAX_GOTO; ++i) mvwaddch(tv->wgoto, 1, 8 + i, ' ');
269                 mvwprintw(tv->wgoto, 1, 8, "%s", str);
270         }
271 }
272
273 static void tv_win_help(tview_t *tv) {
274         int r = 1;
275         WINDOW *win = tv->whelp;
276         wborder(win, '|', '|', '-', '-', '+', '+', '+', '+');
277         mvwprintw(win, r++, 2, "        -=-    Help    -=- ");
278         r++;
279         mvwprintw(win, r++, 2, "?          This window");
280         mvwprintw(win, r++, 2, "Arrows     Small scroll movement");
281         mvwprintw(win, r++, 2, "h,j,k,l    Small scroll movement");
282         mvwprintw(win, r++, 2, "H,J,K,L    Large scroll movement");
283         mvwprintw(win, r++, 2, "ctrl-H     Scroll 1k left");
284         mvwprintw(win, r++, 2, "ctrl-L     Scroll 1k right");
285         mvwprintw(win, r++, 2, "space      Scroll one screen");
286         mvwprintw(win, r++, 2, "backspace  Scroll back one screen");
287         mvwprintw(win, r++, 2, "g          Go to specific location");
288         mvwprintw(win, r++, 2, "m          Color for mapping qual");
289         mvwprintw(win, r++, 2, "n          Color for nucleotide");
290         mvwprintw(win, r++, 2, "b          Color for base quality");
291         mvwprintw(win, r++, 2, "c          Color for cs color");
292         mvwprintw(win, r++, 2, "z          Color for cs qual");
293         mvwprintw(win, r++, 2, ".          Toggle on/off dot view");
294         mvwprintw(win, r++, 2, "N          Turn on nt view");
295         mvwprintw(win, r++, 2, "C          Turn on cs view");
296         mvwprintw(win, r++, 2, "i          Toggle on/off ins");
297         mvwprintw(win, r++, 2, "q          Exit");
298         r++;
299         mvwprintw(win, r++, 2, "Underline:      Secondary or orphan");
300         mvwprintw(win, r++, 2, "Blue:    0-9    Green: 10-19");
301         mvwprintw(win, r++, 2, "Yellow: 20-29   White: >=30");
302         wrefresh(win);
303         wgetch(win);
304 }
305
306 void tv_loop(tview_t *tv)
307 {
308         int tid, pos;
309         tid = tv->curr_tid; pos = tv->left_pos;
310         while (1) {
311                 int c = getch();
312                 //if(256 < c) {c = 1 + (c%256);} // Terminal was displaying ctrl-H as 263 via ssh from Mac OS X 10.5 computer 
313                 switch (c) {
314                         case '?': tv_win_help(tv); break;
315                         case '\033':
316                         case 'q': goto end_loop;
317                         case 'g': tv_win_goto(tv, &tid, &pos); break;
318                         case 'm': tv->color_for = TV_COLOR_MAPQ; break;
319                         case 'b': tv->color_for = TV_COLOR_BASEQ; break;
320                         case 'n': tv->color_for = TV_COLOR_NUCL; break;
321                         case 'c': tv->color_for = TV_COLOR_COL; break;
322                         case 'z': tv->color_for = TV_COLOR_COLQ; break;
323                         case KEY_LEFT:
324                         case 'h': --pos; break;
325                         case KEY_RIGHT:
326                         case 'l': ++pos; break;
327                         case KEY_SLEFT:
328                         case 'H': pos -= 20; break;
329                         case KEY_SRIGHT:
330                         case 'L': pos += 20; break;
331                         case '.': tv->is_dot = !tv->is_dot; break;
332                         case 'N': tv->base_for = TV_BASE_NUCL; break;
333                         case 'C': tv->base_for = TV_BASE_COLOR_SPACE; break;
334                         case 'i': tv->ins = !tv->ins; break;
335                         case '\010': pos -= 1000; break;
336                         case '\014': pos += 1000; break;
337                         case ' ': pos += tv->mcol; break;
338                         case KEY_UP:
339                         case 'j': --tv->row_shift; break;
340                         case KEY_DOWN:
341                         case 'k': ++tv->row_shift; break;
342                         case KEY_BACKSPACE:
343                         case '\177': pos -= tv->mcol; break;
344 #ifdef KEY_RESIZE
345                         case KEY_RESIZE: getmaxyx(stdscr, tv->mrow, tv->mcol); break;
346 #endif
347                         default: continue;
348                 }
349                 if (pos < 0) pos = 0;
350                 if (tv->row_shift < 0) tv->row_shift = 0;
351                 tv_draw_aln(tv, tid, pos);
352         }
353 end_loop:
354         return;
355 }
356
357 int bam_tview_main(int argc, char *argv[])
358 {
359         tview_t *tv;
360         if (argc == 1) {
361                 fprintf(stderr, "Usage: bamtk tview <aln.bam> [ref.fasta]\n");
362                 return 1;
363         }
364         tv = tv_init(argv[1], (argc == 2)? 0 : argv[2]);
365         tv_draw_aln(tv, 0, 0);
366         tv_loop(tv);
367         tv_destroy(tv);
368         return 0;
369 }
370 #else // #ifdef NCURSES_VERSION
371 #warning "The ncurses library is unavailable; tview is disabled."
372 int bam_tview_main(int argc, char *argv[])
373 {
374         fprintf(stderr, "[bam_tview_main] The ncurses library is unavailable; tview is not compiled.\n");
375         return 1;
376 }
377 #endif
378 #endif // #ifndef _NO_CURSES