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