]> git.donarmstrong.com Git - samtools.git/blob - bam_tview.c
* samtools-0.1.5-18 (r423)
[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;
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                                         }
104                                         else {
105                                                 c = bam_nt16_rev_table[bam1_seqi(bam1_seq(p->b), p->qpos)];
106                                                 if (tv->is_dot && toupper(c) == toupper(rb)) c = bam1_strand(p->b)? ',' : '.';
107                                         }
108                                 } else c = '*';
109                         } else { // padding
110                                 if (j > p->indel) c = '*';
111                                 else { // insertion
112                                         if (tv->base_for ==  TV_BASE_NUCL) {
113                                                 c = bam_nt16_rev_table[bam1_seqi(bam1_seq(p->b), p->qpos + j)];
114                                                 if (j == 0 && tv->is_dot && toupper(c) == toupper(rb)) c = bam1_strand(p->b)? ',' : '.';
115                                         }
116                                         else {
117                                                 c = bam_aux_getCSi(p->b, p->qpos + j);
118                                                 if (tv->is_dot && '-' == bam_aux_getCEi(p->b, p->qpos + j)) c = bam1_strand(p->b)? ',' : '.';
119                                         }
120                                 }
121                         }
122                         if (row > TV_MIN_ALNROW && row < tv->mrow) {
123                                 int x;
124                                 attr = 0;
125                                 if (((p->b->core.flag&BAM_FPAIRED) && !(p->b->core.flag&BAM_FPROPER_PAIR))
126                                                 || (p->b->core.flag & BAM_FSECONDARY)) attr |= A_UNDERLINE;
127                                 if (tv->color_for == TV_COLOR_BASEQ) {
128                                         x = bam1_qual(p->b)[p->qpos]/10 + 1;
129                                         if (x > 4) x = 4;
130                                         attr |= COLOR_PAIR(x);
131                                 } else if (tv->color_for == TV_COLOR_MAPQ) {
132                                         x = p->b->core.qual/10 + 1;
133                                         if (x > 4) x = 4;
134                                         attr |= COLOR_PAIR(x);
135                                 } else if (tv->color_for == TV_COLOR_NUCL) {
136                                         x = bam_nt16_nt4_table[bam1_seqi(bam1_seq(p->b), p->qpos)] + 5;
137                                         attr |= COLOR_PAIR(x);
138                                 } else if(tv->color_for == TV_COLOR_COL) {
139                                         x = 0;
140                                         switch(bam_aux_getCSi(p->b, p->qpos)) {
141                                                 case '0': x = 0; break;
142                                                 case '1': x = 1; break;
143                                                 case '2': x = 2; break;
144                                                 case '3': x = 3; break;
145                                                 case '4': x = 4; break;
146                                                 default: x = bam_nt16_nt4_table[bam1_seqi(bam1_seq(p->b), p->qpos)]; break;
147                                         }
148                                         x+=5;
149                                         attr |= COLOR_PAIR(x);
150                                 } else if(tv->color_for == TV_COLOR_COLQ) {
151                                         x = bam_aux_getCQi(p->b, p->qpos);
152                                         if(0 == x) x = bam1_qual(p->b)[p->qpos];
153                                         x = x/10 + 1;
154                                         if (x > 4) x = 4;
155                                         attr |= COLOR_PAIR(x);
156                                 }
157                                 attron(attr);
158                                 mvaddch(row, tv->ccol, bam1_strand(p->b)? tolower(c) : toupper(c));
159                                 attroff(attr);
160                         }
161                 }
162                 c = j? '*' : rb;
163                 if (c == '*') {
164                         attr = COLOR_PAIR(8);
165                         attron(attr);
166                         mvaddch(1, tv->ccol++, c);
167                         attroff(attr);
168                 } else mvaddch(1, tv->ccol++, c);
169         }
170         tv->last_pos = pos;
171         return 0;
172 }
173
174 tview_t *tv_init(const char *fn, const char *fn_fa)
175 {
176         tview_t *tv = (tview_t*)calloc(1, sizeof(tview_t));
177         tv->is_dot = 1;
178         tv->idx = bam_index_load(fn);
179         if (tv->idx == 0) exit(1);
180         tv->fp = bam_open(fn, "r");
181         bgzf_set_cache_size(tv->fp, 8 * 1024 *1024);
182         assert(tv->fp);
183         tv->header = bam_header_read(tv->fp);
184         tv->lplbuf = bam_lplbuf_init(tv_pl_func, tv);
185         if (fn_fa) tv->fai = fai_load(fn_fa);
186         tv->bmc = bam_maqcns_init();
187         tv->ins = 1;
188         bam_maqcns_prepare(tv->bmc);
189
190         initscr();
191         keypad(stdscr, TRUE);
192         clear();
193         noecho();
194         cbreak();
195         tv->mrow = 24; tv->mcol = 80;
196         getmaxyx(stdscr, tv->mrow, tv->mcol);
197         tv->wgoto = newwin(3, TV_MAX_GOTO + 10, 10, 5);
198         tv->whelp = newwin(28, 40, 5, 5);
199         tv->color_for = TV_COLOR_MAPQ;
200         start_color();
201         init_pair(1, COLOR_BLUE, COLOR_BLACK);
202         init_pair(2, COLOR_GREEN, COLOR_BLACK);
203         init_pair(3, COLOR_YELLOW, COLOR_BLACK);
204         init_pair(4, COLOR_WHITE, COLOR_BLACK);
205         init_pair(5, COLOR_GREEN, COLOR_BLACK);
206         init_pair(6, COLOR_CYAN, COLOR_BLACK);
207         init_pair(7, COLOR_YELLOW, COLOR_BLACK);
208         init_pair(8, COLOR_RED, COLOR_BLACK);
209         init_pair(9, COLOR_BLUE, COLOR_BLACK);
210         return tv;
211 }
212
213 void tv_destroy(tview_t *tv)
214 {
215         delwin(tv->wgoto); delwin(tv->whelp);
216         endwin();
217
218         bam_lplbuf_destroy(tv->lplbuf);
219         bam_maqcns_destroy(tv->bmc);
220         bam_index_destroy(tv->idx);
221         if (tv->fai) fai_destroy(tv->fai);
222         free(tv->ref);
223         bam_header_destroy(tv->header);
224         bam_close(tv->fp);
225         free(tv);
226 }
227
228 int tv_fetch_func(const bam1_t *b, void *data)
229 {
230         tview_t *tv = (tview_t*)data;
231         if (tv->no_skip) {
232                 uint32_t *cigar = bam1_cigar(b); // this is cheating...
233                 int i;
234                 for (i = 0; i <b->core.n_cigar; ++i) {
235                         if ((cigar[i]&0xf) == BAM_CREF_SKIP)
236                                 cigar[i] = cigar[i]>>4<<4 | BAM_CDEL;
237                 }
238         }
239         bam_lplbuf_push(b, tv->lplbuf);
240         return 0;
241 }
242
243 int tv_draw_aln(tview_t *tv, int tid, int pos)
244 {
245         // reset
246         clear();
247         tv->curr_tid = tid; tv->left_pos = pos;
248         tv->last_pos = tv->left_pos - 1;
249         tv->ccol = 0;
250         // print ref and consensus
251         if (tv->fai) {
252                 char *str;
253                 if (tv->ref) free(tv->ref);
254                 str = (char*)calloc(strlen(tv->header->target_name[tv->curr_tid]) + 30, 1);
255                 sprintf(str, "%s:%d-%d", tv->header->target_name[tv->curr_tid], tv->left_pos + 1, tv->left_pos + tv->mcol);
256                 tv->ref = fai_fetch(tv->fai, str, &tv->l_ref);
257                 free(str);
258         }
259         // draw aln
260         bam_lplbuf_reset(tv->lplbuf);
261         bam_fetch(tv->fp, tv->idx, tv->curr_tid, tv->left_pos, tv->left_pos + tv->mcol, tv, tv_fetch_func);
262         bam_lplbuf_push(0, tv->lplbuf);
263
264         while (tv->ccol < tv->mcol) {
265                 int pos = tv->last_pos + 1;
266                 if (pos%10 == 0 && tv->mcol - tv->ccol >= 10) mvprintw(0, tv->ccol, "%-d", pos+1);
267                 mvaddch(1, tv->ccol++, (tv->ref && pos < tv->l_ref)? tv->ref[pos - tv->left_pos] : 'N');
268                 ++tv->last_pos;
269         }
270         return 0;
271 }
272
273 static void tv_win_goto(tview_t *tv, int *tid, int *pos)
274 {
275         char str[256];
276         int i, l = 0;
277         wborder(tv->wgoto, '|', '|', '-', '-', '+', '+', '+', '+');
278         mvwprintw(tv->wgoto, 1, 2, "Goto: ");
279         for (;;) {
280                 int c = wgetch(tv->wgoto);
281                 wrefresh(tv->wgoto);
282                 if (c == KEY_BACKSPACE || c == '\010' || c == '\177') {
283                         --l;
284                 } else if (c == KEY_ENTER || c == '\012' || c == '\015') {
285                         int _tid = -1, _beg, _end;
286                         bam_parse_region(tv->header, str, &_tid, &_beg, &_end);
287                         if (_tid >= 0) {
288                                 *tid = _tid; *pos = _beg;
289                                 return;
290                         }
291                 } else if (isgraph(c)) {
292                         if (l < TV_MAX_GOTO) str[l++] = c;
293                 } else if (c == '\027') l = 0;
294                 else if (c == '\033') return;
295                 str[l] = '\0';
296                 for (i = 0; i < TV_MAX_GOTO; ++i) mvwaddch(tv->wgoto, 1, 8 + i, ' ');
297                 mvwprintw(tv->wgoto, 1, 8, "%s", str);
298         }
299 }
300
301 static void tv_win_help(tview_t *tv) {
302         int r = 1;
303         WINDOW *win = tv->whelp;
304         wborder(win, '|', '|', '-', '-', '+', '+', '+', '+');
305         mvwprintw(win, r++, 2, "        -=-    Help    -=- ");
306         r++;
307         mvwprintw(win, r++, 2, "?          This window");
308         mvwprintw(win, r++, 2, "Arrows     Small scroll movement");
309         mvwprintw(win, r++, 2, "h,j,k,l    Small scroll movement");
310         mvwprintw(win, r++, 2, "H,J,K,L    Large scroll movement");
311         mvwprintw(win, r++, 2, "ctrl-H     Scroll 1k left");
312         mvwprintw(win, r++, 2, "ctrl-L     Scroll 1k right");
313         mvwprintw(win, r++, 2, "space      Scroll one screen");
314         mvwprintw(win, r++, 2, "backspace  Scroll back one screen");
315         mvwprintw(win, r++, 2, "g          Go to specific location");
316         mvwprintw(win, r++, 2, "m          Color for mapping qual");
317         mvwprintw(win, r++, 2, "n          Color for nucleotide");
318         mvwprintw(win, r++, 2, "b          Color for base quality");
319         mvwprintw(win, r++, 2, "c          Color for cs color");
320         mvwprintw(win, r++, 2, "z          Color for cs qual");
321         mvwprintw(win, r++, 2, ".          Toggle on/off dot view");
322         mvwprintw(win, r++, 2, "s          Toggle on/off ref skip");
323         mvwprintw(win, r++, 2, "N          Turn on nt view");
324         mvwprintw(win, r++, 2, "C          Turn on cs view");
325         mvwprintw(win, r++, 2, "i          Toggle on/off ins");
326         mvwprintw(win, r++, 2, "q          Exit");
327         r++;
328         mvwprintw(win, r++, 2, "Underline:      Secondary or orphan");
329         mvwprintw(win, r++, 2, "Blue:    0-9    Green: 10-19");
330         mvwprintw(win, r++, 2, "Yellow: 20-29   White: >=30");
331         wrefresh(win);
332         wgetch(win);
333 }
334
335 void tv_loop(tview_t *tv)
336 {
337         int tid, pos;
338         tid = tv->curr_tid; pos = tv->left_pos;
339         while (1) {
340                 int c = getch();
341                 switch (c) {
342                         case '?': tv_win_help(tv); break;
343                         case '\033':
344                         case 'q': goto end_loop;
345                         case 'g': tv_win_goto(tv, &tid, &pos); break;
346                         case 'm': tv->color_for = TV_COLOR_MAPQ; break;
347                         case 'b': tv->color_for = TV_COLOR_BASEQ; break;
348                         case 'n': tv->color_for = TV_COLOR_NUCL; break;
349                         case 'c': tv->color_for = TV_COLOR_COL; break;
350                         case 'z': tv->color_for = TV_COLOR_COLQ; break;
351                         case 's': tv->no_skip = !tv->no_skip; break;
352                         case KEY_LEFT:
353                         case 'h': --pos; break;
354                         case KEY_RIGHT:
355                         case 'l': ++pos; break;
356                         case KEY_SLEFT:
357                         case 'H': pos -= 20; break;
358                         case KEY_SRIGHT:
359                         case 'L': pos += 20; break;
360                         case '.': tv->is_dot = !tv->is_dot; break;
361                         case 'N': tv->base_for = TV_BASE_NUCL; break;
362                         case 'C': tv->base_for = TV_BASE_COLOR_SPACE; break;
363                         case 'i': tv->ins = !tv->ins; break;
364                         case '\010': pos -= 1000; break;
365                         case '\014': pos += 1000; break;
366                         case ' ': pos += tv->mcol; break;
367                         case KEY_UP:
368                         case 'j': --tv->row_shift; break;
369                         case KEY_DOWN:
370                         case 'k': ++tv->row_shift; break;
371                         case KEY_BACKSPACE:
372                         case '\177': pos -= tv->mcol; break;
373                         case KEY_RESIZE: getmaxyx(stdscr, tv->mrow, tv->mcol); break;
374                         default: continue;
375                 }
376                 if (pos < 0) pos = 0;
377                 if (tv->row_shift < 0) tv->row_shift = 0;
378                 tv_draw_aln(tv, tid, pos);
379         }
380 end_loop:
381         return;
382 }
383
384 int bam_tview_main(int argc, char *argv[])
385 {
386         tview_t *tv;
387         if (argc == 1) {
388                 fprintf(stderr, "Usage: bamtk tview <aln.bam> [ref.fasta]\n");
389                 return 1;
390         }
391         tv = tv_init(argv[1], (argc == 2)? 0 : argv[2]);
392         tv_draw_aln(tv, 0, 0);
393         tv_loop(tv);
394         tv_destroy(tv);
395         return 0;
396 }
397 #else // #ifdef _HAVE_CURSES
398 #include <stdio.h>
399 #warning "No curses library is available; tview is disabled."
400 int bam_tview_main(int argc, char *argv[])
401 {
402         fprintf(stderr, "[bam_tview_main] The ncurses library is unavailable; tview is not compiled.\n");
403         return 1;
404 }
405 #endif // #ifdef _HAVE_CURSES