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