]> git.donarmstrong.com Git - samtools.git/blobdiff - bam_tview.c
Merge branch 'vsbuffalo-master' into develop
[samtools.git] / bam_tview.c
index 019e3773cea2730a46d21277fe577b10f284a26b..f8a1f2c3c1eeeef25542316d37af98f9819903fa 100644 (file)
 #include <ctype.h>
 #include <assert.h>
 #include <string.h>
+#include <math.h>
+#include <unistd.h>
 #include "bam.h"
 #include "faidx.h"
-#include "bam_maqcns.h"
+#include "bam2bcf.h"
+#include "sam_header.h"
+#include "khash.h"
+
+KHASH_MAP_INIT_STR(kh_rg, const char *)
 
 char bam_aux_getCEi(bam1_t *b, int i);
 char bam_aux_getCSi(bam1_t *b, int i);
@@ -50,14 +56,16 @@ typedef struct {
        bamFile fp;
        int curr_tid, left_pos;
        faidx_t *fai;
-       bam_maqcns_t *bmc;
+       bcf_callaux_t *bca;
 
-       int ccol, last_pos, row_shift, base_for, color_for, is_dot, l_ref, ins, no_skip;
+       int ccol, last_pos, row_shift, base_for, color_for, is_dot, l_ref, ins, no_skip, show_name;
        char *ref;
+    khash_t(kh_rg) *rg_hash;
 } tview_t;
 
 int tv_pl_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pl, void *data)
 {
+       extern unsigned char bam_nt16_table[256];
        tview_t *tv = (tview_t*)data;
        int i, j, c, rb, attr, max_ins = 0;
        uint32_t call = 0;
@@ -70,11 +78,26 @@ int tv_pl_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pl, void
                mvaddch(1, tv->ccol++, c);
        }
        if (pos%10 == 0 && tv->mcol - tv->ccol >= 10) mvprintw(0, tv->ccol, "%-d", pos+1);
-       // print consensus
-       call = bam_maqcns_call(n, pl, tv->bmc);
+       { // call consensus
+               bcf_callret1_t bcr;
+               int qsum[4], a1, a2, tmp;
+               double p[3], prior = 30;
+               bcf_call_glfgen(n, pl, bam_nt16_table[rb], tv->bca, &bcr);
+               for (i = 0; i < 4; ++i) qsum[i] = bcr.qsum[i]<<2 | i;
+               for (i = 1; i < 4; ++i) // insertion sort
+                       for (j = i; j > 0 && qsum[j] > qsum[j-1]; --j)
+                               tmp = qsum[j], qsum[j] = qsum[j-1], qsum[j-1] = tmp;
+               a1 = qsum[0]&3; a2 = qsum[1]&3;
+               p[0] = bcr.p[a1*5+a1]; p[1] = bcr.p[a1*5+a2] + prior; p[2] = bcr.p[a2*5+a2];
+               if ("ACGT"[a1] != toupper(rb)) p[0] += prior + 3;
+               if ("ACGT"[a2] != toupper(rb)) p[2] += prior + 3;
+               if (p[0] < p[1] && p[0] < p[2]) call = (1<<a1)<<16 | (int)((p[1]<p[2]?p[1]:p[2]) - p[0] + .499);
+               else if (p[2] < p[1] && p[2] < p[0]) call = (1<<a2)<<16 | (int)((p[0]<p[1]?p[0]:p[1]) - p[2] + .499);
+               else call = (1<<a1|1<<a2)<<16 | (int)((p[0]<p[2]?p[0]:p[2]) - p[1] + .499);
+       }
        attr = A_UNDERLINE;
-       c = ",ACMGRSVTWYHKDBN"[call>>28&0xf];
-       i = (call>>8&0xff)/10+1;
+       c = ",ACMGRSVTWYHKDBN"[call>>16&0xf];
+       i = (call&0xffff)/10+1;
        if (i > 4) i = 4;
        attr |= COLOR_PAIR(i);
        if (c == toupper(rb)) c = '.';
@@ -97,23 +120,30 @@ int tv_pl_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pl, void
                                if (!p->is_del) {
                                        if (tv->base_for == TV_BASE_COLOR_SPACE && 
                                                        (c = bam_aux_getCSi(p->b, p->qpos))) {
-                                               c = bam_aux_getCSi(p->b, p->qpos);
                                                // assume that if we found one color, we will be able to get the color error
                                                if (tv->is_dot && '-' == bam_aux_getCEi(p->b, p->qpos)) c = bam1_strand(p->b)? ',' : '.';
+                                       } else {
+                                               if (tv->show_name) {
+                                                       char *name = bam1_qname(p->b);
+                                                       c = (p->qpos + 1 >= p->b->core.l_qname)? ' ' : name[p->qpos];
+                                               } else {
+                                                       c = bam_nt16_rev_table[bam1_seqi(bam1_seq(p->b), p->qpos)];
+                                                       if (tv->is_dot && toupper(c) == toupper(rb)) c = bam1_strand(p->b)? ',' : '.';
+                                               }
                                        }
-                                       else {
-                                               c = bam_nt16_rev_table[bam1_seqi(bam1_seq(p->b), p->qpos)];
-                                               if (tv->is_dot && toupper(c) == toupper(rb)) c = bam1_strand(p->b)? ',' : '.';
-                                       }
-                               } else c = '*';
+                               } else c = p->is_refskip? (bam1_strand(p->b)? '<' : '>') : '*';
                        } else { // padding
                                if (j > p->indel) c = '*';
                                else { // insertion
                                        if (tv->base_for ==  TV_BASE_NUCL) {
-                                               c = bam_nt16_rev_table[bam1_seqi(bam1_seq(p->b), p->qpos + j)];
-                                               if (j == 0 && tv->is_dot && toupper(c) == toupper(rb)) c = bam1_strand(p->b)? ',' : '.';
-                                       }
-                                       else {
+                                               if (tv->show_name) {
+                                                       char *name = bam1_qname(p->b);
+                                                       c = (p->qpos + j + 1 >= p->b->core.l_qname)? ' ' : name[p->qpos + j];
+                                               } else {
+                                                       c = bam_nt16_rev_table[bam1_seqi(bam1_seq(p->b), p->qpos + j)];
+                                                       if (j == 0 && tv->is_dot && toupper(c) == toupper(rb)) c = bam1_strand(p->b)? ',' : '.';
+                                               }
+                                       } else {
                                                c = bam_aux_getCSi(p->b, p->qpos + j);
                                                if (tv->is_dot && '-' == bam_aux_getCEi(p->b, p->qpos + j)) c = bam1_strand(p->b)? ',' : '.';
                                        }
@@ -171,21 +201,46 @@ int tv_pl_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pl, void
        return 0;
 }
 
-tview_t *tv_init(const char *fn, const char *fn_fa)
+tview_t *tv_init(const char *fn, const char *fn_fa, char *samples)
 {
        tview_t *tv = (tview_t*)calloc(1, sizeof(tview_t));
        tv->is_dot = 1;
-       tv->idx = bam_index_load(fn);
-       if (tv->idx == 0) exit(1);
        tv->fp = bam_open(fn, "r");
        bgzf_set_cache_size(tv->fp, 8 * 1024 *1024);
        assert(tv->fp);
        tv->header = bam_header_read(tv->fp);
+       tv->idx = bam_index_load(fn);
+       if (tv->idx == 0) exit(1);
        tv->lplbuf = bam_lplbuf_init(tv_pl_func, tv);
        if (fn_fa) tv->fai = fai_load(fn_fa);
-       tv->bmc = bam_maqcns_init();
+       tv->bca = bcf_call_init(0.83, 13);
        tv->ins = 1;
-       bam_maqcns_prepare(tv->bmc);
+
+    if ( samples ) 
+    {
+        if ( !tv->header->dict ) tv->header->dict = sam_header_parse2(tv->header->text);
+        void *iter = tv->header->dict;
+        const char *key, *val;
+        int n = 0;
+        tv->rg_hash = kh_init(kh_rg);
+        while ( (iter = sam_header2key_val(iter, "RG","ID","SM", &key, &val)) )
+        {
+            if ( !strcmp(samples,key) || (val && !strcmp(samples,val)) )
+            {
+                khiter_t k = kh_get(kh_rg, tv->rg_hash, key);
+                if ( k != kh_end(tv->rg_hash) ) continue;
+                int ret;
+                k = kh_put(kh_rg, tv->rg_hash, key, &ret);
+                kh_value(tv->rg_hash, k) = val;
+                n++;
+            }
+        }
+        if ( !n )
+        {
+            fprintf(stderr,"The sample or read group \"%s\" not present.\n", samples);
+            exit(-1);
+        }
+    }
 
        initscr();
        keypad(stdscr, TRUE);
@@ -195,7 +250,7 @@ tview_t *tv_init(const char *fn, const char *fn_fa)
        tv->mrow = 24; tv->mcol = 80;
        getmaxyx(stdscr, tv->mrow, tv->mcol);
        tv->wgoto = newwin(3, TV_MAX_GOTO + 10, 10, 5);
-       tv->whelp = newwin(28, 40, 5, 5);
+       tv->whelp = newwin(29, 40, 5, 5);
        tv->color_for = TV_COLOR_MAPQ;
        start_color();
        init_pair(1, COLOR_BLUE, COLOR_BLACK);
@@ -216,7 +271,7 @@ void tv_destroy(tview_t *tv)
        endwin();
 
        bam_lplbuf_destroy(tv->lplbuf);
-       bam_maqcns_destroy(tv->bmc);
+       bcf_call_destroy(tv->bca);
        bam_index_destroy(tv->idx);
        if (tv->fai) fai_destroy(tv->fai);
        free(tv->ref);
@@ -228,6 +283,13 @@ void tv_destroy(tview_t *tv)
 int tv_fetch_func(const bam1_t *b, void *data)
 {
        tview_t *tv = (tview_t*)data;
+    if ( tv->rg_hash )
+    {
+        const uint8_t *rg = bam_aux_get(b, "RG");
+        if ( !rg ) return 0; 
+        khiter_t k = kh_get(kh_rg, tv->rg_hash, (const char*)(rg + 1));
+        if ( k == kh_end(tv->rg_hash) ) return 0;
+    }
        if (tv->no_skip) {
                uint32_t *cigar = bam1_cigar(b); // this is cheating...
                int i;
@@ -272,7 +334,7 @@ int tv_draw_aln(tview_t *tv, int tid, int pos)
 
 static void tv_win_goto(tview_t *tv, int *tid, int *pos)
 {
-       char str[256];
+       char str[256], *p;
        int i, l = 0;
        wborder(tv->wgoto, '|', '|', '-', '-', '+', '+', '+', '+');
        mvwprintw(tv->wgoto, 1, 2, "Goto: ");
@@ -280,13 +342,21 @@ static void tv_win_goto(tview_t *tv, int *tid, int *pos)
                int c = wgetch(tv->wgoto);
                wrefresh(tv->wgoto);
                if (c == KEY_BACKSPACE || c == '\010' || c == '\177') {
-                       --l;
+                       if(l > 0) --l;
                } else if (c == KEY_ENTER || c == '\012' || c == '\015') {
                        int _tid = -1, _beg, _end;
-                       bam_parse_region(tv->header, str, &_tid, &_beg, &_end);
-                       if (_tid >= 0) {
-                               *tid = _tid; *pos = _beg;
-                               return;
+                       if (str[0] == '=') {
+                               _beg = strtol(str+1, &p, 10) - 1;
+                               if (_beg > 0) {
+                                       *pos = _beg;
+                                       return;
+                               }
+                       } else {
+                               bam_parse_region(tv->header, str, &_tid, &_beg, &_end);
+                               if (_tid >= 0) {
+                                       *tid = _tid; *pos = _beg;
+                                       return;
+                               }
                        }
                } else if (isgraph(c)) {
                        if (l < TV_MAX_GOTO) str[l++] = c;
@@ -320,6 +390,7 @@ static void tv_win_help(tview_t *tv) {
        mvwprintw(win, r++, 2, "z          Color for cs qual");
        mvwprintw(win, r++, 2, ".          Toggle on/off dot view");
        mvwprintw(win, r++, 2, "s          Toggle on/off ref skip");
+       mvwprintw(win, r++, 2, "r          Toggle on/off rd name");
        mvwprintw(win, r++, 2, "N          Turn on nt view");
        mvwprintw(win, r++, 2, "C          Turn on cs view");
        mvwprintw(win, r++, 2, "i          Toggle on/off ins");
@@ -342,6 +413,7 @@ void tv_loop(tview_t *tv)
                        case '?': tv_win_help(tv); break;
                        case '\033':
                        case 'q': goto end_loop;
+                       case '/': 
                        case 'g': tv_win_goto(tv, &tid, &pos); break;
                        case 'm': tv->color_for = TV_COLOR_MAPQ; break;
                        case 'b': tv->color_for = TV_COLOR_BASEQ; break;
@@ -349,6 +421,7 @@ void tv_loop(tview_t *tv)
                        case 'c': tv->color_for = TV_COLOR_COL; break;
                        case 'z': tv->color_for = TV_COLOR_COLQ; break;
                        case 's': tv->no_skip = !tv->no_skip; break;
+                       case 'r': tv->show_name = !tv->show_name; break;
                        case KEY_LEFT:
                        case 'h': --pos; break;
                        case KEY_RIGHT:
@@ -381,15 +454,49 @@ end_loop:
        return;
 }
 
+void error(const char *format, ...)
+{
+    if ( !format )
+    {
+        fprintf(stderr, "\n");
+        fprintf(stderr, "Usage: bamtk tview [options] <aln.bam> [ref.fasta]\n");
+        fprintf(stderr, "Options:\n");
+        fprintf(stderr, "   -p chr:pos      go directly to this position\n");
+        fprintf(stderr, "   -s STR          display only reads from this sample or grou\n");
+        fprintf(stderr, "\n\n");
+    }
+    else
+    {
+        va_list ap;
+        va_start(ap, format);
+        vfprintf(stderr, format, ap);
+        va_end(ap);
+    }
+    exit(-1);
+}
+
+
 int bam_tview_main(int argc, char *argv[])
 {
        tview_t *tv;
-       if (argc == 1) {
-               fprintf(stderr, "Usage: bamtk tview <aln.bam> [ref.fasta]\n");
-               return 1;
-       }
-       tv = tv_init(argv[1], (argc == 2)? 0 : argv[2]);
-       tv_draw_aln(tv, 0, 0);
+    char *samples=NULL, *position=NULL;
+    int c;
+    while ((c = getopt(argc, argv, "s:p:")) >= 0) {
+        switch (c) {
+            case 's': samples=optarg; break;
+            case 'p': position=optarg; break;
+            default: error(NULL);
+        }
+    }
+       if (argc==optind) error(NULL);
+       tv = tv_init(argv[optind], (optind+1>=argc)? 0 : argv[optind+1], samples);
+    if ( position )
+    {
+        int _tid = -1, _beg, _end;
+        bam_parse_region(tv->header, position, &_tid, &_beg, &_end);
+        if (_tid >= 0) { tv->curr_tid = _tid; tv->left_pos = _beg; }
+    }
+       tv_draw_aln(tv, tv->curr_tid, tv->left_pos);
        tv_loop(tv);
        tv_destroy(tv);
        return 0;