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