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