# Uploaded 0.1.13 to Sid.
[samtools.git] / bam_tview.c
1 #undef _HAVE_CURSES
2
3 #if _CURSES_LIB == 0
4 #elif _CURSES_LIB == 1
5 #include <curses.h>
6 #ifndef NCURSES_VERSION
7 #warning "_CURSES_LIB=1 but NCURSES_VERSION not defined; tview is NOT compiled"
8 #else
9 #define _HAVE_CURSES
10 #endif
11 #elif _CURSES_LIB == 2
12 #include <xcurses.h>
13 #define _HAVE_CURSES
14 #else
15 #warning "_CURSES_LIB is not 0, 1 or 2; tview is NOT compiled"
16 #endif
17
18 #ifdef _HAVE_CURSES
19 #include <ctype.h>
20 #include <assert.h>
21 #include <string.h>
22 #include "bam.h"
23 #include "faidx.h"
24 #include "bam_maqcns.h"
25
26 char bam_aux_getCEi(bam1_t *b, int i);
27 char bam_aux_getCSi(bam1_t *b, int i);
28 char bam_aux_getCQi(bam1_t *b, int i);
29
30 #define TV_MIN_ALNROW 2
31 #define TV_MAX_GOTO  40
32 #define TV_LOW_MAPQ  10
33
34 #define TV_COLOR_MAPQ   0
35 #define TV_COLOR_BASEQ  1
36 #define TV_COLOR_NUCL   2
37 #define TV_COLOR_COL    3
38 #define TV_COLOR_COLQ   4
39
40 #define TV_BASE_NUCL 0
41 #define TV_BASE_COLOR_SPACE 1
42
43 typedef struct {
44         int mrow, mcol;
45         WINDOW *wgoto, *whelp;
46
47         bam_index_t *idx;
48         bam_lplbuf_t *lplbuf;
49         bam_header_t *header;
50         bamFile fp;
51         int curr_tid, left_pos;
52         faidx_t *fai;
53         bam_maqcns_t *bmc;
54
55         int ccol, last_pos, row_shift, base_for, color_for, is_dot, l_ref, ins, no_skip, show_name;
56         char *ref;
57 } tview_t;
58
59 int tv_pl_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pl, void *data)
60 {
61         tview_t *tv = (tview_t*)data;
62         int i, j, c, rb, attr, max_ins = 0;
63         uint32_t call = 0;
64         if (pos < tv->left_pos || tv->ccol > tv->mcol) return 0; // out of screen
65         // print referece
66         rb = (tv->ref && pos - tv->left_pos < tv->l_ref)? tv->ref[pos - tv->left_pos] : 'N';
67         for (i = tv->last_pos + 1; i < pos; ++i) {
68                 if (i%10 == 0 && tv->mcol - tv->ccol >= 10) mvprintw(0, tv->ccol, "%-d", i+1);
69                 c = tv->ref? tv->ref[i - tv->left_pos] : 'N';
70                 mvaddch(1, tv->ccol++, c);
71         }
72         if (pos%10 == 0 && tv->mcol - tv->ccol >= 10) mvprintw(0, tv->ccol, "%-d", pos+1);
73         // print consensus
74         call = bam_maqcns_call(n, pl, tv->bmc);
75         attr = A_UNDERLINE;
76         c = ",ACMGRSVTWYHKDBN"[call>>28&0xf];
77         i = (call>>8&0xff)/10+1;
78         if (i > 4) i = 4;
79         attr |= COLOR_PAIR(i);
80         if (c == toupper(rb)) c = '.';
81         attron(attr);
82         mvaddch(2, tv->ccol, c);
83         attroff(attr);
84         if(tv->ins) {
85                 // calculate maximum insert
86                 for (i = 0; i < n; ++i) {
87                         const bam_pileup1_t *p = pl + i;
88                         if (p->indel > 0 && max_ins < p->indel) max_ins = p->indel;
89                 }
90         }
91         // core loop
92         for (j = 0; j <= max_ins; ++j) {
93                 for (i = 0; i < n; ++i) {
94                         const bam_pileup1_t *p = pl + i;
95                         int row = TV_MIN_ALNROW + p->level - tv->row_shift;
96                         if (j == 0) {
97                                 if (!p->is_del) {
98                                         if (tv->base_for == TV_BASE_COLOR_SPACE && 
99                                                         (c = bam_aux_getCSi(p->b, p->qpos))) {
100                                                 c = bam_aux_getCSi(p->b, p->qpos);
101                                                 // assume that if we found one color, we will be able to get the color error
102                                                 if (tv->is_dot && '-' == bam_aux_getCEi(p->b, p->qpos)) c = bam1_strand(p->b)? ',' : '.';
103                                         } else {
104                                                 if (tv->show_name) {
105                                                         char *name = bam1_qname(p->b);
106                                                         c = (p->qpos + 1 >= p->b->core.l_qname)? ' ' : name[p->qpos];
107                                                 } else {
108                                                         c = bam_nt16_rev_table[bam1_seqi(bam1_seq(p->b), p->qpos)];
109                                                         if (tv->is_dot && toupper(c) == toupper(rb)) c = bam1_strand(p->b)? ',' : '.';
110                                                 }
111                                         }
112                                 } else c = p->is_refskip? (bam1_strand(p->b)? '<' : '>') : '*';
113                         } else { // padding
114                                 if (j > p->indel) c = '*';
115                                 else { // insertion
116                                         if (tv->base_for ==  TV_BASE_NUCL) {
117                                                 if (tv->show_name) {
118                                                         char *name = bam1_qname(p->b);
119                                                         c = (p->qpos + j + 1 >= p->b->core.l_qname)? ' ' : name[p->qpos + j];
120                                                 } else {
121                                                         c = bam_nt16_rev_table[bam1_seqi(bam1_seq(p->b), p->qpos + j)];
122                                                         if (j == 0 && tv->is_dot && toupper(c) == toupper(rb)) c = bam1_strand(p->b)? ',' : '.';
123                                                 }
124                                         } else {
125                                                 c = bam_aux_getCSi(p->b, p->qpos + j);
126                                                 if (tv->is_dot && '-' == bam_aux_getCEi(p->b, p->qpos + j)) c = bam1_strand(p->b)? ',' : '.';
127                                         }
128                                 }
129                         }
130                         if (row > TV_MIN_ALNROW && row < tv->mrow) {
131                                 int x;
132                                 attr = 0;
133                                 if (((p->b->core.flag&BAM_FPAIRED) && !(p->b->core.flag&BAM_FPROPER_PAIR))
134                                                 || (p->b->core.flag & BAM_FSECONDARY)) attr |= A_UNDERLINE;
135                                 if (tv->color_for == TV_COLOR_BASEQ) {
136                                         x = bam1_qual(p->b)[p->qpos]/10 + 1;
137                                         if (x > 4) x = 4;
138                                         attr |= COLOR_PAIR(x);
139                                 } else if (tv->color_for == TV_COLOR_MAPQ) {
140                                         x = p->b->core.qual/10 + 1;
141                                         if (x > 4) x = 4;
142                                         attr |= COLOR_PAIR(x);
143                                 } else if (tv->color_for == TV_COLOR_NUCL) {
144                                         x = bam_nt16_nt4_table[bam1_seqi(bam1_seq(p->b), p->qpos)] + 5;
145                                         attr |= COLOR_PAIR(x);
146                                 } else if(tv->color_for == TV_COLOR_COL) {
147                                         x = 0;
148                                         switch(bam_aux_getCSi(p->b, p->qpos)) {
149                                                 case '0': x = 0; break;
150                                                 case '1': x = 1; break;
151                                                 case '2': x = 2; break;
152                                                 case '3': x = 3; break;
153                                                 case '4': x = 4; break;
154                                                 default: x = bam_nt16_nt4_table[bam1_seqi(bam1_seq(p->b), p->qpos)]; break;
155                                         }
156                                         x+=5;
157                                         attr |= COLOR_PAIR(x);
158                                 } else if(tv->color_for == TV_COLOR_COLQ) {
159                                         x = bam_aux_getCQi(p->b, p->qpos);
160                                         if(0 == x) x = bam1_qual(p->b)[p->qpos];
161                                         x = x/10 + 1;
162                                         if (x > 4) x = 4;
163                                         attr |= COLOR_PAIR(x);
164                                 }
165                                 attron(attr);
166                                 mvaddch(row, tv->ccol, bam1_strand(p->b)? tolower(c) : toupper(c));
167                                 attroff(attr);
168                         }
169                 }
170                 c = j? '*' : rb;
171                 if (c == '*') {
172                         attr = COLOR_PAIR(8);
173                         attron(attr);
174                         mvaddch(1, tv->ccol++, c);
175                         attroff(attr);
176                 } else mvaddch(1, tv->ccol++, c);
177         }
178         tv->last_pos = pos;
179         return 0;
180 }
181
182 tview_t *tv_init(const char *fn, const char *fn_fa)
183 {
184         tview_t *tv = (tview_t*)calloc(1, sizeof(tview_t));
185         tv->is_dot = 1;
186         tv->fp = bam_open(fn, "r");
187         bgzf_set_cache_size(tv->fp, 8 * 1024 *1024);
188         assert(tv->fp);
189         tv->header = bam_header_read(tv->fp);
190         tv->idx = bam_index_load(fn);
191         if (tv->idx == 0) exit(1);
192         tv->lplbuf = bam_lplbuf_init(tv_pl_func, tv);
193         if (fn_fa) tv->fai = fai_load(fn_fa);
194         tv->bmc = bam_maqcns_init();
195         tv->ins = 1;
196         bam_maqcns_prepare(tv->bmc);
197
198         initscr();
199         keypad(stdscr, TRUE);
200         clear();
201         noecho();
202         cbreak();
203         tv->mrow = 24; tv->mcol = 80;
204         getmaxyx(stdscr, tv->mrow, tv->mcol);
205         tv->wgoto = newwin(3, TV_MAX_GOTO + 10, 10, 5);
206         tv->whelp = newwin(29, 40, 5, 5);
207         tv->color_for = TV_COLOR_MAPQ;
208         start_color();
209         init_pair(1, COLOR_BLUE, COLOR_BLACK);
210         init_pair(2, COLOR_GREEN, COLOR_BLACK);
211         init_pair(3, COLOR_YELLOW, COLOR_BLACK);
212         init_pair(4, COLOR_WHITE, COLOR_BLACK);
213         init_pair(5, COLOR_GREEN, COLOR_BLACK);
214         init_pair(6, COLOR_CYAN, COLOR_BLACK);
215         init_pair(7, COLOR_YELLOW, COLOR_BLACK);
216         init_pair(8, COLOR_RED, COLOR_BLACK);
217         init_pair(9, COLOR_BLUE, COLOR_BLACK);
218         return tv;
219 }
220
221 void tv_destroy(tview_t *tv)
222 {
223         delwin(tv->wgoto); delwin(tv->whelp);
224         endwin();
225
226         bam_lplbuf_destroy(tv->lplbuf);
227         bam_maqcns_destroy(tv->bmc);
228         bam_index_destroy(tv->idx);
229         if (tv->fai) fai_destroy(tv->fai);
230         free(tv->ref);
231         bam_header_destroy(tv->header);
232         bam_close(tv->fp);
233         free(tv);
234 }
235
236 int tv_fetch_func(const bam1_t *b, void *data)
237 {
238         tview_t *tv = (tview_t*)data;
239         if (tv->no_skip) {
240                 uint32_t *cigar = bam1_cigar(b); // this is cheating...
241                 int i;
242                 for (i = 0; i <b->core.n_cigar; ++i) {
243                         if ((cigar[i]&0xf) == BAM_CREF_SKIP)
244                                 cigar[i] = cigar[i]>>4<<4 | BAM_CDEL;
245                 }
246         }
247         bam_lplbuf_push(b, tv->lplbuf);
248         return 0;
249 }
250
251 int tv_draw_aln(tview_t *tv, int tid, int pos)
252 {
253         // reset
254         clear();
255         tv->curr_tid = tid; tv->left_pos = pos;
256         tv->last_pos = tv->left_pos - 1;
257         tv->ccol = 0;
258         // print ref and consensus
259         if (tv->fai) {
260                 char *str;
261                 if (tv->ref) free(tv->ref);
262                 str = (char*)calloc(strlen(tv->header->target_name[tv->curr_tid]) + 30, 1);
263                 sprintf(str, "%s:%d-%d", tv->header->target_name[tv->curr_tid], tv->left_pos + 1, tv->left_pos + tv->mcol);
264                 tv->ref = fai_fetch(tv->fai, str, &tv->l_ref);
265                 free(str);
266         }
267         // draw aln
268         bam_lplbuf_reset(tv->lplbuf);
269         bam_fetch(tv->fp, tv->idx, tv->curr_tid, tv->left_pos, tv->left_pos + tv->mcol, tv, tv_fetch_func);
270         bam_lplbuf_push(0, tv->lplbuf);
271
272         while (tv->ccol < tv->mcol) {
273                 int pos = tv->last_pos + 1;
274                 if (pos%10 == 0 && tv->mcol - tv->ccol >= 10) mvprintw(0, tv->ccol, "%-d", pos+1);
275                 mvaddch(1, tv->ccol++, (tv->ref && pos < tv->l_ref)? tv->ref[pos - tv->left_pos] : 'N');
276                 ++tv->last_pos;
277         }
278         return 0;
279 }
280
281 static void tv_win_goto(tview_t *tv, int *tid, int *pos)
282 {
283         char str[256], *p;
284         int i, l = 0;
285         wborder(tv->wgoto, '|', '|', '-', '-', '+', '+', '+', '+');
286         mvwprintw(tv->wgoto, 1, 2, "Goto: ");
287         for (;;) {
288                 int c = wgetch(tv->wgoto);
289                 wrefresh(tv->wgoto);
290                 if (c == KEY_BACKSPACE || c == '\010' || c == '\177') {
291                         --l;
292                 } else if (c == KEY_ENTER || c == '\012' || c == '\015') {
293                         int _tid = -1, _beg, _end;
294                         if (str[0] == '=') {
295                                 _beg = strtol(str+1, &p, 10) - 1;
296                                 if (_beg > 0) {
297                                         *pos = _beg;
298                                         return;
299                                 }
300                         } else {
301                                 bam_parse_region(tv->header, str, &_tid, &_beg, &_end);
302                                 if (_tid >= 0) {
303                                         *tid = _tid; *pos = _beg;
304                                         return;
305                                 }
306                         }
307                 } else if (isgraph(c)) {
308                         if (l < TV_MAX_GOTO) str[l++] = c;
309                 } else if (c == '\027') l = 0;
310                 else if (c == '\033') return;
311                 str[l] = '\0';
312                 for (i = 0; i < TV_MAX_GOTO; ++i) mvwaddch(tv->wgoto, 1, 8 + i, ' ');
313                 mvwprintw(tv->wgoto, 1, 8, "%s", str);
314         }
315 }
316
317 static void tv_win_help(tview_t *tv) {
318         int r = 1;
319         WINDOW *win = tv->whelp;
320         wborder(win, '|', '|', '-', '-', '+', '+', '+', '+');
321         mvwprintw(win, r++, 2, "        -=-    Help    -=- ");
322         r++;
323         mvwprintw(win, r++, 2, "?          This window");
324         mvwprintw(win, r++, 2, "Arrows     Small scroll movement");
325         mvwprintw(win, r++, 2, "h,j,k,l    Small scroll movement");
326         mvwprintw(win, r++, 2, "H,J,K,L    Large scroll movement");
327         mvwprintw(win, r++, 2, "ctrl-H     Scroll 1k left");
328         mvwprintw(win, r++, 2, "ctrl-L     Scroll 1k right");
329         mvwprintw(win, r++, 2, "space      Scroll one screen");
330         mvwprintw(win, r++, 2, "backspace  Scroll back one screen");
331         mvwprintw(win, r++, 2, "g          Go to specific location");
332         mvwprintw(win, r++, 2, "m          Color for mapping qual");
333         mvwprintw(win, r++, 2, "n          Color for nucleotide");
334         mvwprintw(win, r++, 2, "b          Color for base quality");
335         mvwprintw(win, r++, 2, "c          Color for cs color");
336         mvwprintw(win, r++, 2, "z          Color for cs qual");
337         mvwprintw(win, r++, 2, ".          Toggle on/off dot view");
338         mvwprintw(win, r++, 2, "s          Toggle on/off ref skip");
339         mvwprintw(win, r++, 2, "r          Toggle on/off rd name");
340         mvwprintw(win, r++, 2, "N          Turn on nt view");
341         mvwprintw(win, r++, 2, "C          Turn on cs view");
342         mvwprintw(win, r++, 2, "i          Toggle on/off ins");
343         mvwprintw(win, r++, 2, "q          Exit");
344         r++;
345         mvwprintw(win, r++, 2, "Underline:      Secondary or orphan");
346         mvwprintw(win, r++, 2, "Blue:    0-9    Green: 10-19");
347         mvwprintw(win, r++, 2, "Yellow: 20-29   White: >=30");
348         wrefresh(win);
349         wgetch(win);
350 }
351
352 void tv_loop(tview_t *tv)
353 {
354         int tid, pos;
355         tid = tv->curr_tid; pos = tv->left_pos;
356         while (1) {
357                 int c = getch();
358                 switch (c) {
359                         case '?': tv_win_help(tv); break;
360                         case '\033':
361                         case 'q': goto end_loop;
362                         case '/': 
363                         case 'g': tv_win_goto(tv, &tid, &pos); break;
364                         case 'm': tv->color_for = TV_COLOR_MAPQ; break;
365                         case 'b': tv->color_for = TV_COLOR_BASEQ; break;
366                         case 'n': tv->color_for = TV_COLOR_NUCL; break;
367                         case 'c': tv->color_for = TV_COLOR_COL; break;
368                         case 'z': tv->color_for = TV_COLOR_COLQ; break;
369                         case 's': tv->no_skip = !tv->no_skip; break;
370                         case 'r': tv->show_name = !tv->show_name; break;
371                         case KEY_LEFT:
372                         case 'h': --pos; break;
373                         case KEY_RIGHT:
374                         case 'l': ++pos; break;
375                         case KEY_SLEFT:
376                         case 'H': pos -= 20; break;
377                         case KEY_SRIGHT:
378                         case 'L': pos += 20; break;
379                         case '.': tv->is_dot = !tv->is_dot; break;
380                         case 'N': tv->base_for = TV_BASE_NUCL; break;
381                         case 'C': tv->base_for = TV_BASE_COLOR_SPACE; break;
382                         case 'i': tv->ins = !tv->ins; break;
383                         case '\010': pos -= 1000; break;
384                         case '\014': pos += 1000; break;
385                         case ' ': pos += tv->mcol; break;
386                         case KEY_UP:
387                         case 'j': --tv->row_shift; break;
388                         case KEY_DOWN:
389                         case 'k': ++tv->row_shift; break;
390                         case KEY_BACKSPACE:
391                         case '\177': pos -= tv->mcol; break;
392                         case KEY_RESIZE: getmaxyx(stdscr, tv->mrow, tv->mcol); break;
393                         default: continue;
394                 }
395                 if (pos < 0) pos = 0;
396                 if (tv->row_shift < 0) tv->row_shift = 0;
397                 tv_draw_aln(tv, tid, pos);
398         }
399 end_loop:
400         return;
401 }
402
403 int bam_tview_main(int argc, char *argv[])
404 {
405         tview_t *tv;
406         if (argc == 1) {
407                 fprintf(stderr, "Usage: bamtk tview <aln.bam> [ref.fasta]\n");
408                 return 1;
409         }
410         tv = tv_init(argv[1], (argc == 2)? 0 : argv[2]);
411         tv_draw_aln(tv, 0, 0);
412         tv_loop(tv);
413         tv_destroy(tv);
414         return 0;
415 }
416 #else // #ifdef _HAVE_CURSES
417 #include <stdio.h>
418 #warning "No curses library is available; tview is disabled."
419 int bam_tview_main(int argc, char *argv[])
420 {
421         fprintf(stderr, "[bam_tview_main] The ncurses library is unavailable; tview is not compiled.\n");
422         return 1;
423 }
424 #endif // #ifdef _HAVE_CURSES