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