]> git.donarmstrong.com Git - samtools.git/commitdiff
Merge remote branch 'remotes/pierre/master' into develop
authorPetr Danecek <pd3@sanger.ac.uk>
Mon, 28 Jan 2013 09:07:23 +0000 (09:07 +0000)
committerPetr Danecek <pd3@sanger.ac.uk>
Mon, 28 Jan 2013 09:07:23 +0000 (09:07 +0000)
Makefile
bam_tview.c
bam_tview.h [new file with mode: 0644]
bam_tview_curses.c [new file with mode: 0644]
bam_tview_html.c [new file with mode: 0644]
bcftools/call1.c
bcftools/prob1.c

index dbd90af737a7f360df90e110f66a30a915bba435..2f51bfcb920aef7cfada148a1743c1f1fa9b47db 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -9,7 +9,8 @@ LOBJS=          bgzf.o kstring.o bam_aux.o bam.o bam_import.o sam.o bam_index.o \
 AOBJS=         bam_tview.o bam_plcmd.o sam_view.o \
                        bam_rmdup.o bam_rmdupse.o bam_mate.o bam_stat.o bam_color.o \
                        bamtk.o kaln.o bam2bcf.o bam2bcf_indel.o errmod.o sample.o \
-                       cut_target.o phase.o bam2depth.o padding.o bedcov.o bamshuf.o
+                       cut_target.o phase.o bam2depth.o padding.o bedcov.o bamshuf.o \
+                       bam_tview_curses.o bam_tview_html.o
 PROG=          samtools
 INCLUDES=      -I.
 SUBDIRS=       . bcftools misc
@@ -46,10 +47,10 @@ samtools:lib-recur $(AOBJS)
                $(CC) $(CFLAGS) -o $@ $(AOBJS) $(LDFLAGS) libbam.a -Lbcftools -lbcf $(LIBPATH) $(LIBCURSES) -lm -lz -lpthread
 
 razip:razip.o razf.o $(KNETFILE_O)
-               $(CC) $(CFLAGS) -o $@ razf.o razip.o $(KNETFILE_O) -lz
+               $(CC) $(CFLAGS) -o $@ $^ -lz
 
 bgzip:bgzip.o bgzf.o $(KNETFILE_O)
-               $(CC) $(CFLAGS) -o $@ bgzf.o bgzip.o $(KNETFILE_O) -lz -lpthread
+               $(CC) $(CFLAGS) -o $@ $^ -lz -lpthread
 
 bgzf.o:bgzf.c bgzf.h
                $(CC) -c $(CFLAGS) $(DFLAGS) -DBGZF_CACHE $(INCLUDES) bgzf.c -o $@
@@ -62,7 +63,9 @@ bam_pileup.o:bam.h razf.h ksort.h
 bam_plcmd.o:bam.h faidx.h bcftools/bcf.h bam2bcf.h
 bam_index.o:bam.h khash.h ksort.h razf.h bam_endian.h
 bam_lpileup.o:bam.h ksort.h
-bam_tview.o:bam.h faidx.h
+bam_tview.o:bam.h faidx.h bam_tview.h
+bam_tview_curses.o:bam.h faidx.h bam_tview.h
+bam_tview_html.o:bam.h faidx.h bam_tview.h
 bam_sort.o:bam.h ksort.h razf.h
 bam_md.o:bam.h faidx.h
 sam_header.o:sam_header.h khash.h
index f8a1f2c3c1eeeef25542316d37af98f9819903fa..ede6b0c87f57d17ad25b5933da85cedd881f719b 100644 (file)
@@ -1,67 +1,81 @@
-#undef _HAVE_CURSES
-
-#if _CURSES_LIB == 0
-#elif _CURSES_LIB == 1
-#include <curses.h>
-#ifndef NCURSES_VERSION
-#warning "_CURSES_LIB=1 but NCURSES_VERSION not defined; tview is NOT compiled"
-#else
-#define _HAVE_CURSES
-#endif
-#elif _CURSES_LIB == 2
-#include <xcurses.h>
-#define _HAVE_CURSES
-#else
-#warning "_CURSES_LIB is not 0, 1 or 2; tview is NOT compiled"
-#endif
-
-#ifdef _HAVE_CURSES
-#include <ctype.h>
 #include <assert.h>
-#include <string.h>
-#include <math.h>
-#include <unistd.h>
-#include "bam.h"
-#include "faidx.h"
-#include "bam2bcf.h"
-#include "sam_header.h"
-#include "khash.h"
-
-KHASH_MAP_INIT_STR(kh_rg, const char *)
+#include "bam_tview.h"
 
-char bam_aux_getCEi(bam1_t *b, int i);
-char bam_aux_getCSi(bam1_t *b, int i);
-char bam_aux_getCQi(bam1_t *b, int i);
-
-#define TV_MIN_ALNROW 2
-#define TV_MAX_GOTO  40
-#define TV_LOW_MAPQ  10
+int base_tv_init(tview_t* tv,const char *fn, const char *fn_fa, const char *samples)
+       {
+       assert(tv!=NULL);
+       assert(fn!=NULL);
+       tv->mrow = 24; tv->mcol = 80;
+       tv->color_for = TV_COLOR_MAPQ;
+       tv->is_dot = 1;
+       
+       tv->fp = bam_open(fn, "r");
+       if(tv->fp==0)
+               {
+               fprintf(stderr,"bam_open %s. %s\n", fn,fn_fa);
+               exit(EXIT_FAILURE);
+               }
+       bgzf_set_cache_size(tv->fp, 8 * 1024 *1024);
+       assert(tv->fp);
+       
+       tv->header = bam_header_read(tv->fp);
+       if(tv->header==0)
+               {
+               fprintf(stderr,"Cannot read '%s'.\n", fn);
+               exit(EXIT_FAILURE);
+               }
+       tv->idx = bam_index_load(fn);
+       if (tv->idx == 0)
+               {
+               fprintf(stderr,"Cannot read index for '%s'.\n", fn);
+               exit(EXIT_FAILURE);
+               }
+       tv->lplbuf = bam_lplbuf_init(tv_pl_func, tv);
+       if (fn_fa) tv->fai = fai_load(fn_fa);
+       tv->bca = bcf_call_init(0.83, 13);
+       tv->ins = 1;
 
-#define TV_COLOR_MAPQ   0
-#define TV_COLOR_BASEQ  1
-#define TV_COLOR_NUCL   2
-#define TV_COLOR_COL    3
-#define TV_COLOR_COLQ   4
+    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(EXIT_FAILURE);
+        }
+    }
 
-#define TV_BASE_NUCL 0
-#define TV_BASE_COLOR_SPACE 1
+       return 0;
+       }
 
-typedef struct {
-       int mrow, mcol;
-       WINDOW *wgoto, *whelp;
 
-       bam_index_t *idx;
-       bam_lplbuf_t *lplbuf;
-       bam_header_t *header;
-       bamFile fp;
-       int curr_tid, left_pos;
-       faidx_t *fai;
-       bcf_callaux_t *bca;
+void base_tv_destroy(tview_t* tv)
+       {
+       bam_lplbuf_destroy(tv->lplbuf);
+       bcf_call_destroy(tv->bca);
+       bam_index_destroy(tv->idx);
+       if (tv->fai) fai_destroy(tv->fai);
+       free(tv->ref);
+       bam_header_destroy(tv->header);
+       bam_close(tv->fp);
+       }
 
-       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)
 {
@@ -73,11 +87,11 @@ int tv_pl_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pl, void
        // print referece
        rb = (tv->ref && pos - tv->left_pos < tv->l_ref)? tv->ref[pos - tv->left_pos] : 'N';
        for (i = tv->last_pos + 1; i < pos; ++i) {
-               if (i%10 == 0 && tv->mcol - tv->ccol >= 10) mvprintw(0, tv->ccol, "%-d", i+1);
+               if (i%10 == 0 && tv->mcol - tv->ccol >= 10) tv->my_mvprintw(tv,0, tv->ccol, "%-d", i+1);
                c = tv->ref? tv->ref[i - tv->left_pos] : 'N';
-               mvaddch(1, tv->ccol++, c);
+               tv->my_mvaddch(tv,1, tv->ccol++, c);
        }
-       if (pos%10 == 0 && tv->mcol - tv->ccol >= 10) mvprintw(0, tv->ccol, "%-d", pos+1);
+       if (pos%10 == 0 && tv->mcol - tv->ccol >= 10) tv->my_mvprintw(tv,0, tv->ccol, "%-d", pos+1);
        { // call consensus
                bcf_callret1_t bcr;
                int qsum[4], a1, a2, tmp;
@@ -95,15 +109,15 @@ int tv_pl_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pl, void
                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;
+       attr = tv->my_underline(tv);
        c = ",ACMGRSVTWYHKDBN"[call>>16&0xf];
        i = (call&0xffff)/10+1;
        if (i > 4) i = 4;
-       attr |= COLOR_PAIR(i);
+       attr |= tv->my_colorpair(tv,i);
        if (c == toupper(rb)) c = '.';
-       attron(attr);
-       mvaddch(2, tv->ccol, c);
-       attroff(attr);
+       tv->my_attron(tv,attr);
+       tv->my_mvaddch(tv,2, tv->ccol, c);
+       tv->my_attroff(tv,attr);
        if(tv->ins) {
                // calculate maximum insert
                for (i = 0; i < n; ++i) {
@@ -153,18 +167,18 @@ int tv_pl_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pl, void
                                int x;
                                attr = 0;
                                if (((p->b->core.flag&BAM_FPAIRED) && !(p->b->core.flag&BAM_FPROPER_PAIR))
-                                               || (p->b->core.flag & BAM_FSECONDARY)) attr |= A_UNDERLINE;
+                                               || (p->b->core.flag & BAM_FSECONDARY)) attr |= tv->my_underline(tv);
                                if (tv->color_for == TV_COLOR_BASEQ) {
                                        x = bam1_qual(p->b)[p->qpos]/10 + 1;
                                        if (x > 4) x = 4;
-                                       attr |= COLOR_PAIR(x);
+                                       attr |= tv->my_colorpair(tv,x);
                                } else if (tv->color_for == TV_COLOR_MAPQ) {
                                        x = p->b->core.qual/10 + 1;
                                        if (x > 4) x = 4;
-                                       attr |= COLOR_PAIR(x);
+                                       attr |= tv->my_colorpair(tv,x);
                                } else if (tv->color_for == TV_COLOR_NUCL) {
                                        x = bam_nt16_nt4_table[bam1_seqi(bam1_seq(p->b), p->qpos)] + 5;
-                                       attr |= COLOR_PAIR(x);
+                                       attr |= tv->my_colorpair(tv,x);
                                } else if(tv->color_for == TV_COLOR_COL) {
                                        x = 0;
                                        switch(bam_aux_getCSi(p->b, p->qpos)) {
@@ -176,109 +190,33 @@ int tv_pl_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pl, void
                                                default: x = bam_nt16_nt4_table[bam1_seqi(bam1_seq(p->b), p->qpos)]; break;
                                        }
                                        x+=5;
-                                       attr |= COLOR_PAIR(x);
+                                       attr |= tv->my_colorpair(tv,x);
                                } else if(tv->color_for == TV_COLOR_COLQ) {
                                        x = bam_aux_getCQi(p->b, p->qpos);
                                        if(0 == x) x = bam1_qual(p->b)[p->qpos];
                                        x = x/10 + 1;
                                        if (x > 4) x = 4;
-                                       attr |= COLOR_PAIR(x);
+                                       attr |= tv->my_colorpair(tv,x);
                                }
-                               attron(attr);
-                               mvaddch(row, tv->ccol, bam1_strand(p->b)? tolower(c) : toupper(c));
-                               attroff(attr);
+                               tv->my_attron(tv,attr);
+                               tv->my_mvaddch(tv,row, tv->ccol, bam1_strand(p->b)? tolower(c) : toupper(c));
+                               tv->my_attroff(tv,attr);
                        }
                }
                c = j? '*' : rb;
                if (c == '*') {
-                       attr = COLOR_PAIR(8);
-                       attron(attr);
-                       mvaddch(1, tv->ccol++, c);
-                       attroff(attr);
-               } else mvaddch(1, tv->ccol++, c);
+                       attr = tv->my_colorpair(tv,8);
+                       tv->my_attron(tv,attr);
+                       tv->my_mvaddch(tv,1, tv->ccol++, c);
+                       tv->my_attroff(tv,attr);
+               } else tv->my_mvaddch(tv,1, tv->ccol++, c);
        }
        tv->last_pos = pos;
        return 0;
 }
 
-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->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->bca = bcf_call_init(0.83, 13);
-       tv->ins = 1;
 
-    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);
-       clear();
-       noecho();
-       cbreak();
-       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(29, 40, 5, 5);
-       tv->color_for = TV_COLOR_MAPQ;
-       start_color();
-       init_pair(1, COLOR_BLUE, COLOR_BLACK);
-       init_pair(2, COLOR_GREEN, COLOR_BLACK);
-       init_pair(3, COLOR_YELLOW, COLOR_BLACK);
-       init_pair(4, COLOR_WHITE, COLOR_BLACK);
-       init_pair(5, COLOR_GREEN, COLOR_BLACK);
-       init_pair(6, COLOR_CYAN, COLOR_BLACK);
-       init_pair(7, COLOR_YELLOW, COLOR_BLACK);
-       init_pair(8, COLOR_RED, COLOR_BLACK);
-       init_pair(9, COLOR_BLUE, COLOR_BLACK);
-       return tv;
-}
-
-void tv_destroy(tview_t *tv)
-{
-       delwin(tv->wgoto); delwin(tv->whelp);
-       endwin();
 
-       bam_lplbuf_destroy(tv->lplbuf);
-       bcf_call_destroy(tv->bca);
-       bam_index_destroy(tv->idx);
-       if (tv->fai) fai_destroy(tv->fai);
-       free(tv->ref);
-       bam_header_destroy(tv->header);
-       bam_close(tv->fp);
-       free(tv);
-}
 
 int tv_fetch_func(const bam1_t *b, void *data)
 {
@@ -302,10 +240,11 @@ int tv_fetch_func(const bam1_t *b, void *data)
        return 0;
 }
 
-int tv_draw_aln(tview_t *tv, int tid, int pos)
-{
+int base_draw_aln(tview_t *tv, int tid, int pos)
+       {
+       assert(tv!=NULL);
        // reset
-       clear();
+       tv->my_clear(tv);
        tv->curr_tid = tid; tv->left_pos = pos;
        tv->last_pos = tv->left_pos - 1;
        tv->ccol = 0;
@@ -313,7 +252,10 @@ int tv_draw_aln(tview_t *tv, int tid, int pos)
        if (tv->fai) {
                char *str;
                if (tv->ref) free(tv->ref);
+               assert(tv->curr_tid>=0);
+               
                str = (char*)calloc(strlen(tv->header->target_name[tv->curr_tid]) + 30, 1);
+               assert(str!=NULL);
                sprintf(str, "%s:%d-%d", tv->header->target_name[tv->curr_tid], tv->left_pos + 1, tv->left_pos + tv->mcol);
                tv->ref = fai_fetch(tv->fai, str, &tv->l_ref);
                free(str);
@@ -325,136 +267,17 @@ int tv_draw_aln(tview_t *tv, int tid, int pos)
 
        while (tv->ccol < tv->mcol) {
                int pos = tv->last_pos + 1;
-               if (pos%10 == 0 && tv->mcol - tv->ccol >= 10) mvprintw(0, tv->ccol, "%-d", pos+1);
-               mvaddch(1, tv->ccol++, (tv->ref && pos < tv->l_ref)? tv->ref[pos - tv->left_pos] : 'N');
+               if (pos%10 == 0 && tv->mcol - tv->ccol >= 10) tv->my_mvprintw(tv,0, tv->ccol, "%-d", pos+1);
+               tv->my_mvaddch(tv,1, tv->ccol++, (tv->ref && pos < tv->l_ref)? tv->ref[pos - tv->left_pos] : 'N');
                ++tv->last_pos;
        }
        return 0;
 }
 
-static void tv_win_goto(tview_t *tv, int *tid, int *pos)
-{
-       char str[256], *p;
-       int i, l = 0;
-       wborder(tv->wgoto, '|', '|', '-', '-', '+', '+', '+', '+');
-       mvwprintw(tv->wgoto, 1, 2, "Goto: ");
-       for (;;) {
-               int c = wgetch(tv->wgoto);
-               wrefresh(tv->wgoto);
-               if (c == KEY_BACKSPACE || c == '\010' || c == '\177') {
-                       if(l > 0) --l;
-               } else if (c == KEY_ENTER || c == '\012' || c == '\015') {
-                       int _tid = -1, _beg, _end;
-                       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;
-               } else if (c == '\027') l = 0;
-               else if (c == '\033') return;
-               str[l] = '\0';
-               for (i = 0; i < TV_MAX_GOTO; ++i) mvwaddch(tv->wgoto, 1, 8 + i, ' ');
-               mvwprintw(tv->wgoto, 1, 8, "%s", str);
-       }
-}
 
-static void tv_win_help(tview_t *tv) {
-       int r = 1;
-       WINDOW *win = tv->whelp;
-       wborder(win, '|', '|', '-', '-', '+', '+', '+', '+');
-       mvwprintw(win, r++, 2, "        -=-    Help    -=- ");
-       r++;
-       mvwprintw(win, r++, 2, "?          This window");
-       mvwprintw(win, r++, 2, "Arrows     Small scroll movement");
-       mvwprintw(win, r++, 2, "h,j,k,l    Small scroll movement");
-       mvwprintw(win, r++, 2, "H,J,K,L    Large scroll movement");
-       mvwprintw(win, r++, 2, "ctrl-H     Scroll 1k left");
-       mvwprintw(win, r++, 2, "ctrl-L     Scroll 1k right");
-       mvwprintw(win, r++, 2, "space      Scroll one screen");
-       mvwprintw(win, r++, 2, "backspace  Scroll back one screen");
-       mvwprintw(win, r++, 2, "g          Go to specific location");
-       mvwprintw(win, r++, 2, "m          Color for mapping qual");
-       mvwprintw(win, r++, 2, "n          Color for nucleotide");
-       mvwprintw(win, r++, 2, "b          Color for base quality");
-       mvwprintw(win, r++, 2, "c          Color for cs color");
-       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");
-       mvwprintw(win, r++, 2, "q          Exit");
-       r++;
-       mvwprintw(win, r++, 2, "Underline:      Secondary or orphan");
-       mvwprintw(win, r++, 2, "Blue:    0-9    Green: 10-19");
-       mvwprintw(win, r++, 2, "Yellow: 20-29   White: >=30");
-       wrefresh(win);
-       wgetch(win);
-}
 
-void tv_loop(tview_t *tv)
-{
-       int tid, pos;
-       tid = tv->curr_tid; pos = tv->left_pos;
-       while (1) {
-               int c = getch();
-               switch (c) {
-                       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;
-                       case 'n': tv->color_for = TV_COLOR_NUCL; break;
-                       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:
-                       case 'l': ++pos; break;
-                       case KEY_SLEFT:
-                       case 'H': pos -= 20; break;
-                       case KEY_SRIGHT:
-                       case 'L': pos += 20; break;
-                       case '.': tv->is_dot = !tv->is_dot; break;
-                       case 'N': tv->base_for = TV_BASE_NUCL; break;
-                       case 'C': tv->base_for = TV_BASE_COLOR_SPACE; break;
-                       case 'i': tv->ins = !tv->ins; break;
-                       case '\010': pos -= 1000; break;
-                       case '\014': pos += 1000; break;
-                       case ' ': pos += tv->mcol; break;
-                       case KEY_UP:
-                       case 'j': --tv->row_shift; break;
-                       case KEY_DOWN:
-                       case 'k': ++tv->row_shift; break;
-                       case KEY_BACKSPACE:
-                       case '\177': pos -= tv->mcol; break;
-                       case KEY_RESIZE: getmaxyx(stdscr, tv->mrow, tv->mcol); break;
-                       default: continue;
-               }
-               if (pos < 0) pos = 0;
-               if (tv->row_shift < 0) tv->row_shift = 0;
-               tv_draw_aln(tv, tid, pos);
-       }
-end_loop:
-       return;
-}
 
-void error(const char *format, ...)
+static void error(const char *format, ...)
 {
     if ( !format )
     {
@@ -462,7 +285,8 @@ void error(const char *format, ...)
         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, "   -s STR          display only reads from this sample or group\n");
+        fprintf(stderr, "   -d display      (H)tml or (C)urses or (T)ext \n");
         fprintf(stderr, "\n\n");
     }
     else
@@ -475,38 +299,70 @@ void error(const char *format, ...)
     exit(-1);
 }
 
+enum dipsay_mode {display_ncurses,display_html,display_text};
+extern tview_t* curses_tv_init(const char *fn, const char *fn_fa, const char *samples);
+extern tview_t* html_tv_init(const char *fn, const char *fn_fa, const char *samples);
+extern tview_t* text_tv_init(const char *fn, const char *fn_fa, const char *samples);
 
 int bam_tview_main(int argc, char *argv[])
-{
-       tview_t *tv;
+       {
+       int view_mode=display_ncurses;
+       tview_t* tv=NULL;
     char *samples=NULL, *position=NULL;
     int c;
-    while ((c = getopt(argc, argv, "s:p:")) >= 0) {
+    while ((c = getopt(argc, argv, "s:p:d:")) >= 0) {
         switch (c) {
             case 's': samples=optarg; break;
             case 'p': position=optarg; break;
+            case 'd':
+               {
+               switch(optarg[0])
+                       {
+                       case 'H': case 'h': view_mode=display_html;break;
+                       case 'T': case 't': view_mode=display_text;break;
+                       case 'C': case 'c': view_mode=display_ncurses;break;
+                       default: view_mode=display_ncurses;break;
+                       }
+               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;
-}
-#else // #ifdef _HAVE_CURSES
-#include <stdio.h>
-#warning "No curses library is available; tview is disabled."
-int bam_tview_main(int argc, char *argv[])
-{
-       fprintf(stderr, "[bam_tview_main] The ncurses library is unavailable; tview is not compiled.\n");
-       return 1;
-}
-#endif // #ifdef _HAVE_CURSES
+       
+       switch(view_mode)
+               {
+               case display_ncurses:
+                       {
+                       tv = curses_tv_init(argv[optind], (optind+1>=argc)? 0 : argv[optind+1], samples);
+                       break;
+                       }
+               case display_text:
+                       {
+                       tv = text_tv_init(argv[optind], (optind+1>=argc)? 0 : argv[optind+1], samples);
+                       break;
+                       }
+               case display_html:
+                       {
+                       tv = html_tv_init(argv[optind], (optind+1>=argc)? 0 : argv[optind+1], samples);
+                       break;
+                       }
+               }
+       if(tv==NULL)
+               {
+               error("cannot create view");
+               return EXIT_FAILURE;
+               }
+       
+       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->my_drawaln(tv, tv->curr_tid, tv->left_pos);
+       tv->my_loop(tv);
+       tv->my_destroy(tv);
+       
+       return EXIT_SUCCESS;
+       }
diff --git a/bam_tview.h b/bam_tview.h
new file mode 100644 (file)
index 0000000..80f0464
--- /dev/null
@@ -0,0 +1,75 @@
+#ifndef BAM_TVIEW_H
+#define BAM_TVIEW_H
+
+#include <ctype.h>
+#include <assert.h>
+#include <string.h>
+#include <math.h>
+#include <unistd.h>
+#include <stdarg.h>
+#include "bam.h"
+#include "faidx.h"
+#include "bam2bcf.h"
+#include "sam_header.h"
+#include "khash.h"
+
+KHASH_MAP_INIT_STR(kh_rg, const char *)
+
+typedef struct AbstractTview {
+       int mrow, mcol;
+       
+       bam_index_t *idx;
+       bam_lplbuf_t *lplbuf;
+       bam_header_t *header;
+       bamFile fp;
+       int curr_tid, left_pos;
+       faidx_t *fai;
+       bcf_callaux_t *bca;
+
+       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;
+    /* callbacks */
+    void (*my_destroy)(struct AbstractTview* );
+    void (*my_mvprintw)(struct AbstractTview* ,int,int,const char*,...);
+    void (*my_mvaddch)(struct AbstractTview*,int,int,int);
+    void (*my_attron)(struct AbstractTview*,int);
+    void (*my_attroff)(struct AbstractTview*,int);
+    void (*my_clear)(struct AbstractTview*);
+    int (*my_colorpair)(struct AbstractTview*,int);
+    int (*my_drawaln)(struct AbstractTview*,int,int);
+    int (*my_loop)(struct AbstractTview*);
+    int (*my_underline)(struct AbstractTview*);
+} tview_t;
+
+
+char bam_aux_getCEi(bam1_t *b, int i);
+char bam_aux_getCSi(bam1_t *b, int i);
+char bam_aux_getCQi(bam1_t *b, int i);
+
+#define TV_MIN_ALNROW 2
+#define TV_MAX_GOTO  40
+#define TV_LOW_MAPQ  10
+
+#define TV_COLOR_MAPQ   0
+#define TV_COLOR_BASEQ  1
+#define TV_COLOR_NUCL   2
+#define TV_COLOR_COL    3
+#define TV_COLOR_COLQ   4
+
+#define TV_BASE_NUCL 0
+#define TV_BASE_COLOR_SPACE 1
+
+int tv_pl_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pl, void *data);
+int base_tv_init(tview_t*,const char *fn, const char *fn_fa, const char *samples);
+void base_tv_destroy(tview_t*);
+int base_draw_aln(tview_t *tv, int tid, int pos);
+
+typedef struct Tixel
+       {
+       int ch;
+       int attributes;
+       }tixel_t;
+
+#endif
+
diff --git a/bam_tview_curses.c b/bam_tview_curses.c
new file mode 100644 (file)
index 0000000..4fdd1fb
--- /dev/null
@@ -0,0 +1,297 @@
+#undef _HAVE_CURSES
+
+#if _CURSES_LIB == 0
+#elif _CURSES_LIB == 1
+#include <curses.h>
+#ifndef NCURSES_VERSION
+#warning "_CURSES_LIB=1 but NCURSES_VERSION not defined; tview is NOT compiled"
+#else
+#define _HAVE_CURSES
+#endif
+#elif _CURSES_LIB == 2
+#include <xcurses.h>
+#define _HAVE_CURSES
+#else
+#warning "_CURSES_LIB is not 0, 1 or 2; tview is NOT compiled"
+#endif
+
+
+#include "bam_tview.h"
+
+#ifdef _HAVE_CURSES
+
+
+
+typedef struct CursesTview {
+       tview_t view;
+       WINDOW *wgoto, *whelp;
+       } curses_tview_t;
+
+
+
+
+#define FROM_TV(ptr) ((curses_tview_t*)ptr)
+
+static void curses_destroy(tview_t* base)
+       {
+       curses_tview_t* tv=(curses_tview_t*)base;
+
+       
+       delwin(tv->wgoto); delwin(tv->whelp);
+       endwin();
+
+       base_tv_destroy(base);
+       
+       free(tv);
+       }
+
+/*
+ void (*my_mvprintw)(struct AbstractTview* ,int,int,const char*,...);
+    void (*my_)(struct AbstractTview*,int,int,int);
+    void (*my_attron)(struct AbstractTview*,int);
+    void (*my_attroff)(struct AbstractTview*,int);
+    void (*my_clear)(struct AbstractTview*);
+    int (*my_colorpair)(struct AbstractTview*,int);
+*/
+
+static void curses_mvprintw(struct AbstractTview* tv,int y ,int x,const char* fmt,...)
+       {
+       unsigned int size=tv->mcol+2;
+       char* str=malloc(size);
+       if(str==0) exit(EXIT_FAILURE);
+       va_list argptr;
+       va_start(argptr, fmt);
+       vsnprintf(str,size, fmt, argptr);
+       va_end(argptr);
+       mvprintw(y,x,str);
+       free(str);
+       }
+
+static void curses_mvaddch(struct AbstractTview* tv,int y,int x,int ch)
+       {
+       mvaddch(y,x,ch);
+       }
+       
+static void curses_attron(struct AbstractTview* tv,int flag)
+    {
+    attron(flag);
+    }
+static void curses_attroff(struct AbstractTview* tv,int flag)
+    {
+    attroff(flag);
+    }
+static void curses_clear(struct AbstractTview* tv)
+    {
+    clear();
+    }
+    
+static int curses_colorpair(struct AbstractTview* tv,int flag)
+    {
+    return COLOR_PAIR(flag);
+    }
+
+static int curses_drawaln(struct AbstractTview* tv, int tid, int pos)
+    {
+    return base_draw_aln(tv,  tid, pos);
+    }
+
+
+
+static void tv_win_goto(curses_tview_t *tv, int *tid, int *pos)
+       {
+       char str[256], *p;
+       int i, l = 0;
+       tview_t *base=(tview_t*)tv;
+       wborder(tv->wgoto, '|', '|', '-', '-', '+', '+', '+', '+');
+       mvwprintw(tv->wgoto, 1, 2, "Goto: ");
+       for (;;) {
+               int c = wgetch(tv->wgoto);
+               wrefresh(tv->wgoto);
+               if (c == KEY_BACKSPACE || c == '\010' || c == '\177') {
+                       if(l > 0) --l;
+               } else if (c == KEY_ENTER || c == '\012' || c == '\015') {
+                       int _tid = -1, _beg, _end;
+                       if (str[0] == '=') {
+                               _beg = strtol(str+1, &p, 10) - 1;
+                               if (_beg > 0) {
+                                       *pos = _beg;
+                                       return;
+                               }
+                       } else {
+                               bam_parse_region(base->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;
+               } else if (c == '\027') l = 0;
+               else if (c == '\033') return;
+               str[l] = '\0';
+               for (i = 0; i < TV_MAX_GOTO; ++i) mvwaddch(tv->wgoto, 1, 8 + i, ' ');
+               mvwprintw(tv->wgoto, 1, 8, "%s", str);
+       }
+}
+
+
+
+
+static void tv_win_help(curses_tview_t *tv) {
+       int r = 1;
+       tview_t* base=(tview_t*)base;
+       WINDOW *win = tv->whelp;
+       wborder(win, '|', '|', '-', '-', '+', '+', '+', '+');
+       mvwprintw(win, r++, 2, "        -=-    Help    -=- ");
+       r++;
+       mvwprintw(win, r++, 2, "?          This window");
+       mvwprintw(win, r++, 2, "Arrows     Small scroll movement");
+       mvwprintw(win, r++, 2, "h,j,k,l    Small scroll movement");
+       mvwprintw(win, r++, 2, "H,J,K,L    Large scroll movement");
+       mvwprintw(win, r++, 2, "ctrl-H     Scroll 1k left");
+       mvwprintw(win, r++, 2, "ctrl-L     Scroll 1k right");
+       mvwprintw(win, r++, 2, "space      Scroll one screen");
+       mvwprintw(win, r++, 2, "backspace  Scroll back one screen");
+       mvwprintw(win, r++, 2, "g          Go to specific location");
+       mvwprintw(win, r++, 2, "m          Color for mapping qual");
+       mvwprintw(win, r++, 2, "n          Color for nucleotide");
+       mvwprintw(win, r++, 2, "b          Color for base quality");
+       mvwprintw(win, r++, 2, "c          Color for cs color");
+       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");
+       mvwprintw(win, r++, 2, "q          Exit");
+       r++;
+       mvwprintw(win, r++, 2, "Underline:      Secondary or orphan");
+       mvwprintw(win, r++, 2, "Blue:    0-9    Green: 10-19");
+       mvwprintw(win, r++, 2, "Yellow: 20-29   White: >=30");
+       wrefresh(win);
+       wgetch(win);
+}
+
+static int curses_underline(tview_t* tv)
+       {
+       return A_UNDERLINE;
+       }
+       
+static int curses_loop(tview_t* tv)
+       {
+       int tid, pos;
+       curses_tview_t *CTV=(curses_tview_t *)tv;
+       tid = tv->curr_tid; pos = tv->left_pos;
+       while (1) {
+               int c = getch();
+               switch (c) {
+                       case '?': tv_win_help(CTV); break;
+                       case '\033':
+                       case 'q': goto end_loop;
+                       case '/': 
+                       case 'g': tv_win_goto(CTV, &tid, &pos); break;
+                       case 'm': tv->color_for = TV_COLOR_MAPQ; break;
+                       case 'b': tv->color_for = TV_COLOR_BASEQ; break;
+                       case 'n': tv->color_for = TV_COLOR_NUCL; break;
+                       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:
+                       case 'l': ++pos; break;
+                       case KEY_SLEFT:
+                       case 'H': pos -= 20; break;
+                       case KEY_SRIGHT:
+                       case 'L': pos += 20; break;
+                       case '.': tv->is_dot = !tv->is_dot; break;
+                       case 'N': tv->base_for = TV_BASE_NUCL; break;
+                       case 'C': tv->base_for = TV_BASE_COLOR_SPACE; break;
+                       case 'i': tv->ins = !tv->ins; break;
+                       case '\010': pos -= 1000; break;
+                       case '\014': pos += 1000; break;
+                       case ' ': pos += tv->mcol; break;
+                       case KEY_UP:
+                       case 'j': --tv->row_shift; break;
+                       case KEY_DOWN:
+                       case 'k': ++tv->row_shift; break;
+                       case KEY_BACKSPACE:
+                       case '\177': pos -= tv->mcol; break;
+                       case KEY_RESIZE: getmaxyx(stdscr, tv->mrow, tv->mcol); break;
+                       default: continue;
+               }
+               if (pos < 0) pos = 0;
+               if (tv->row_shift < 0) tv->row_shift = 0;
+               tv->my_drawaln(tv, tid, pos);
+       }
+end_loop:
+       return 0;
+}
+
+
+
+
+tview_t* curses_tv_init(const char *fn, const char *fn_fa, const char *samples)
+       {
+       curses_tview_t *tv = (curses_tview_t*)calloc(1, sizeof(curses_tview_t));
+       tview_t* base=(tview_t*)tv;
+       if(tv==0)
+               {
+               fprintf(stderr,"Calloc failed\n");
+               return 0;
+               }
+       
+       base_tv_init(base,fn,fn_fa,samples);
+       /* initialize callbacks */
+#define SET_CALLBACK(fun) base->my_##fun=curses_##fun;
+       SET_CALLBACK(destroy);
+       SET_CALLBACK(mvprintw);
+       SET_CALLBACK(mvaddch);
+       SET_CALLBACK(attron);
+       SET_CALLBACK(attroff);
+       SET_CALLBACK(clear);
+       SET_CALLBACK(colorpair);
+       SET_CALLBACK(drawaln);
+       SET_CALLBACK(loop);
+       SET_CALLBACK(underline);
+#undef SET_CALLBACK
+
+       initscr();
+       keypad(stdscr, TRUE);
+       clear();
+       noecho();
+       cbreak();
+       
+       getmaxyx(stdscr, base->mrow, base->mcol);
+       tv->wgoto = newwin(3, TV_MAX_GOTO + 10, 10, 5);
+       tv->whelp = newwin(29, 40, 5, 5);
+       
+       start_color();
+       init_pair(1, COLOR_BLUE, COLOR_BLACK);
+       init_pair(2, COLOR_GREEN, COLOR_BLACK);
+       init_pair(3, COLOR_YELLOW, COLOR_BLACK);
+       init_pair(4, COLOR_WHITE, COLOR_BLACK);
+       init_pair(5, COLOR_GREEN, COLOR_BLACK);
+       init_pair(6, COLOR_CYAN, COLOR_BLACK);
+       init_pair(7, COLOR_YELLOW, COLOR_BLACK);
+       init_pair(8, COLOR_RED, COLOR_BLACK);
+       init_pair(9, COLOR_BLUE, COLOR_BLACK);
+       return base;
+       }
+
+
+#else // #ifdef _HAVE_CURSES
+#include <stdio.h>
+#warning "No curses library is available; tview with curses is disabled."
+
+extern tview_t* text_tv_init(const char *fn, const char *fn_fa, const char *samples);
+
+tview_t* curses_tv_init(const char *fn, const char *fn_fa, const char *samples)
+       {
+       return text_tv_init(fn,fn_fa,samples);
+       }
+#endif // #ifdef _HAVE_CURSES
+
+       
diff --git a/bam_tview_html.c b/bam_tview_html.c
new file mode 100644 (file)
index 0000000..f52b4c3
--- /dev/null
@@ -0,0 +1,349 @@
+#include <unistd.h>
+#include "bam_tview.h"
+
+#define UNDERLINE_FLAG 10
+
+typedef struct HtmlTview {
+       tview_t view;
+       int row_count;
+       tixel_t** screen;
+       FILE* out;
+       int attributes;/* color... */
+       } html_tview_t;
+
+#define FROM_TV(ptr) ((html_tview_t*)ptr)
+
+static void html_destroy(tview_t* base)
+       {
+       int i;
+       html_tview_t* tv=(html_tview_t*)base;
+       if(tv->screen!=NULL)
+               {
+               for(i=0;i< tv->row_count;++i) free(tv->screen[i]);
+               free(tv->screen);
+               }
+       base_tv_destroy(base);
+       free(tv);
+       }
+
+/*
+ void (*my_mvprintw)(struct AbstractTview* ,int,int,const char*,...);
+    void (*my_)(struct AbstractTview*,int,int,int);
+    void (*my_attron)(struct AbstractTview*,int);
+    void (*my_attroff)(struct AbstractTview*,int);
+    void (*my_clear)(struct AbstractTview*);
+    int (*my_colorpair)(struct AbstractTview*,int);
+*/
+
+static void html_mvprintw(struct AbstractTview* tv,int y ,int x,const char* fmt,...)
+       {
+       int i,nchars=0;
+       unsigned int size=tv->mcol+2;
+       char* str=malloc(size);
+       if(str==0) exit(EXIT_FAILURE);
+       va_list argptr;
+       va_start(argptr, fmt);
+       nchars=vsnprintf(str,size, fmt, argptr);
+       va_end(argptr);
+       
+       for(i=0;i< nchars;++i)
+               {
+               tv->my_mvaddch(tv,y,x+i,str[i]);
+               }
+       free(str);
+       }
+
+static void html_mvaddch(struct AbstractTview* tv,int y,int x,int ch)
+       {
+       tixel_t* row=NULL;
+       html_tview_t* ptr=FROM_TV(tv);
+       if( x >= tv->mcol ) return; //out of screen
+       while(ptr->row_count<=y)
+               {
+               int x;
+               row=(tixel_t*)calloc(tv->mcol,sizeof(tixel_t));
+               if(row==0)  exit(EXIT_FAILURE);
+               for(x=0;x<tv->mcol;++x) {row[x].ch=' ';row[x].attributes=0;}
+               ptr->screen=(tixel_t**)realloc(ptr->screen,sizeof(tixel_t*)*(ptr->row_count+1));
+               ptr->screen[ptr->row_count++]=row;
+               }
+       row=ptr->screen[y];
+       row[x].ch=ch;
+       row[x].attributes=ptr->attributes;
+       }
+       
+static void html_attron(struct AbstractTview* tv,int flag)
+    {
+    html_tview_t* ptr=FROM_TV(tv);
+    ptr->attributes |=  flag;
+
+
+    }
+   
+static void html_attroff(struct AbstractTview* tv,int flag)
+    {
+    html_tview_t* ptr=FROM_TV(tv);
+    ptr->attributes &= ~(flag);
+    }
+    
+static void html_clear(struct AbstractTview* tv)
+    {
+    html_tview_t* ptr=FROM_TV(tv);
+    if(ptr->screen!=NULL)
+       {
+       int i;
+       for(i=0;i< ptr->row_count;++i) free(ptr->screen[i]);
+       free(ptr->screen);
+       ptr->screen=NULL;
+       }
+    ptr->row_count=0;
+    ptr->attributes=0;
+    }
+    
+static int html_colorpair(struct AbstractTview* tv,int flag)
+    {
+    return (1 << (flag));
+    }
+
+static int html_drawaln(struct AbstractTview* tv, int tid, int pos)
+    {
+    int y,x;
+    html_tview_t* ptr=FROM_TV(tv);
+    html_clear(tv);
+    base_draw_aln(tv,  tid, pos);
+    fputs("<html><head>",ptr->out);
+    fprintf(ptr->out,"<title>%s:%d</title>",
+       tv->header->target_name[tid],
+       pos+1
+       );
+    //style
+   
+    fputs("<style type='text/css'>\n",ptr->out);
+    fputs(".tviewbody { margin:5px; background-color:white;text-align:center;}\n",ptr->out);
+    fputs(".tviewtitle {text-align:center;}\n",ptr->out);
+    fputs(".tviewpre { margin:5px; background-color:white;}\n",ptr->out);
+    #define CSS(id,col) fprintf(ptr->out,".tviewc%d {color:%s;}\n.tviewcu%d {color:%s;text-decoration:underline;}\n",id,col,id,col);
+        CSS(0, "black");
+       CSS(1, "blue");
+       CSS(2, "green");
+       CSS(3, "yellow");
+       CSS(4, "black");
+       CSS(5, "green");
+       CSS(6, "cyan");
+       CSS(7, "yellow");
+       CSS(8, "red");
+       CSS(9, "blue");
+    #undef CSS
+    fputs("</style>",ptr->out);
+    
+    fputs("</head><body>",ptr->out);
+    
+      fprintf(ptr->out,"<div class='tviewbody'><div class='tviewtitle'>%s:%d</div>",
+       tv->header->target_name[tid],
+       pos+1
+       );
+    
+    fputs("<pre class='tviewpre'>",ptr->out);
+    for(y=0;y< ptr->row_count;++y)
+       {
+       
+       for(x=0;x< tv->mcol;++x)
+               {
+               
+               
+               if(x== 0 || ptr->screen[y][x].attributes != ptr->screen[y][x-1].attributes)
+                       {
+                       int css=0;
+                       fprintf(ptr->out,"<span");
+                       while(css<32)
+                               {
+                               //if(y>1) fprintf(stderr,"css=%d pow2=%d vs %d\n",css,(1 << (css)),ptr->screen[y][x].attributes);
+                               if(( (ptr->screen[y][x].attributes) & (1 << (css)))!=0)
+                                       {
+                                       
+                                       fprintf(ptr->out," class='tviewc%s%d'",
+                                               (( (ptr->screen[y][x].attributes) & (1 << (UNDERLINE_FLAG)) )!=0?"u":""),
+                                               css);
+                                       break;
+                                       }
+                               ++css;
+                               }
+
+
+                       fputs(">",ptr->out);
+                       }
+               
+               int ch=ptr->screen[y][x].ch;
+               switch(ch)
+                       {
+                       case '<': fputs("&lt;",ptr->out);break;
+                       case '>': fputs("&gt;",ptr->out);break;
+                       case '&': fputs("&amp;",ptr->out);break;
+                       default: fputc(ch,ptr->out); break;
+                       }
+               
+               
+               if(x+1 == tv->mcol  || ptr->screen[y][x].attributes!=ptr->screen[y][x+1].attributes)
+                       {
+                       fputs("</span>",ptr->out);
+                       }
+               }
+       if(y+1 < ptr->row_count) fputs("<br/>",ptr->out);
+       }
+    fputs("</pre></div></body></html>",ptr->out);
+    return 0;
+    }
+
+
+#define ANSI_COLOR_RED "\x1b[31m"
+#define ANSI_COLOR_GREEN "\x1b[32m"
+#define ANSI_COLOR_YELLOW "\x1b[33m"
+#define ANSI_COLOR_BLUE "\x1b[34m"
+#define ANSI_COLOR_MAGENTA "\x1b[35m"
+#define ANSI_COLOR_CYAN "\x1b[36m"
+#define ANSI_COLOR_BLACK "\x1b[0m"
+#define ANSI_COLOR_RESET ANSI_COLOR_BLACK
+
+#define ANSI_UNDERLINE_SET "\033[4m"
+#define ANSI_UNDERLINE_UNSET "\033[0m"
+
+static int text_drawaln(struct AbstractTview* tv, int tid, int pos)
+    {
+    int y,x;
+    html_tview_t* ptr=FROM_TV(tv);
+    html_clear(tv);
+    base_draw_aln(tv,  tid, pos); 
+    int is_term= isatty(fileno(ptr->out));
+    
+    for(y=0;y< ptr->row_count;++y)
+       {
+       for(x=0;x< tv->mcol;++x)
+               {
+               if(is_term)
+                       {
+                       int css=0;
+                       while(css<32)
+                               {
+                               if(( (ptr->screen[y][x].attributes) & (1 << (css)))!=0)
+                                       {
+                                       break;
+                                       }
+                               ++css;
+                               }
+                       switch(css)
+                               {
+                               //CSS(0, "black");
+                               case 1: fputs(ANSI_COLOR_BLUE,ptr->out); break;
+                               case 2: fputs(ANSI_COLOR_GREEN,ptr->out); break;
+                               case 3: fputs(ANSI_COLOR_YELLOW,ptr->out); break;
+                               //CSS(4, "black");
+                               case 5: fputs(ANSI_COLOR_GREEN,ptr->out); break;
+                               case 6: fputs(ANSI_COLOR_CYAN,ptr->out); break;
+                               case 7: fputs(ANSI_COLOR_YELLOW,ptr->out); break;
+                               case 8: fputs(ANSI_COLOR_RED,ptr->out); break;
+                               case 9: fputs(ANSI_COLOR_BLUE,ptr->out); break;
+                               default:break;
+                               }
+                       if(( (ptr->screen[y][x].attributes) & (1 << (UNDERLINE_FLAG)))!=0)
+                               {
+                               fputs(ANSI_UNDERLINE_SET,ptr->out);
+                               }
+                       
+                       }
+               
+               
+               int ch=ptr->screen[y][x].ch;
+
+               fputc(ch,ptr->out);
+               if(is_term)
+                       {
+                       fputs(ANSI_COLOR_RESET,ptr->out);
+                       if(( (ptr->screen[y][x].attributes) & (1 << (UNDERLINE_FLAG)))!=0)
+                               {
+                               fputs(ANSI_UNDERLINE_UNSET,ptr->out);
+                               }
+                       }
+               }
+       fputc('\n',ptr->out);
+       }
+    return 0;
+    }
+
+
+static int html_loop(tview_t* tv)
+       {
+       //tv->my_drawaln(tv, tv->curr_tid, tv->left_pos);
+       return 0;       
+       }
+
+static int html_underline(tview_t* tv)
+       {
+       return (1 << UNDERLINE_FLAG);   
+       }
+
+/*
+static void init_pair(html_tview_t *tv,int id_ge_1, const char* pen, const char* paper)
+       {
+       
+       }
+*/
+
+tview_t* html_tv_init(const char *fn, const char *fn_fa, const char *samples)
+       {
+       char* colstr=getenv("COLUMNS");
+       html_tview_t *tv = (html_tview_t*)calloc(1, sizeof(html_tview_t));
+       tview_t* base=(tview_t*)tv;
+       if(tv==0)
+               {
+               fprintf(stderr,"Calloc failed\n");
+               return 0;
+               }
+       tv->row_count=0;
+       tv->screen=NULL;
+       tv->out=stdout;
+       tv->attributes=0;
+       base_tv_init(base,fn,fn_fa,samples);
+       /* initialize callbacks */
+#define SET_CALLBACK(fun) base->my_##fun=html_##fun;
+       SET_CALLBACK(destroy);
+       SET_CALLBACK(mvprintw);
+       SET_CALLBACK(mvaddch);
+       SET_CALLBACK(attron);
+       SET_CALLBACK(attroff);
+       SET_CALLBACK(clear);
+       SET_CALLBACK(colorpair);
+       SET_CALLBACK(drawaln);
+       SET_CALLBACK(loop);
+       SET_CALLBACK(underline);
+#undef SET_CALLBACK
+
+       
+       if(colstr!=0)
+               {
+               base->mcol=atoi(colstr);
+               if(base->mcol<10) base->mcol=80;
+               }
+       base->mrow=99999;
+       
+/*
+       init_pair(tv,1, "blue", "white");
+       init_pair(tv,2, "green", "white");
+       init_pair(tv,3, "yellow", "white");
+       init_pair(tv,4, "white", "white");
+       init_pair(tv,5, "green", "white");
+       init_pair(tv,6, "cyan", "white");
+       init_pair(tv,7, "yellow", "white");
+       init_pair(tv,8, "red", "white");
+       init_pair(tv,9, "blue", "white");
+       */
+       return base;
+       }
+
+
+tview_t* text_tv_init(const char *fn, const char *fn_fa, const char *samples)
+       {
+       tview_t* tv=html_tv_init(fn,fn_fa,samples);
+       tv->my_drawaln=text_drawaln;
+       return tv;
+       }
+
index 22ff2aca91be76f564e7bd3825abddcd4616ba42..14a1570a13d6820152359e583c9c69d42d1021dd 100644 (file)
@@ -298,6 +298,9 @@ int bcfview(int argc, char *argv[])
        extern int bcf_trio_call(uint32_t *prep, const bcf1_t *b, int *llr, int64_t *gt);
        extern int bcf_pair_call(const bcf1_t *b);
        extern int bcf_min_diff(const bcf1_t *b);
+       extern int bcf_p1_get_M(bcf_p1aux_t *b);
+
+       extern gzFile bcf_p1_fp_lk;
 
        bcf_t *bp, *bout = 0;
        bcf1_t *b, *blast;
@@ -313,7 +316,7 @@ int bcfview(int argc, char *argv[])
        memset(&vc, 0, sizeof(viewconf_t));
        vc.prior_type = vc.n1 = -1; vc.theta = 1e-3; vc.pref = 0.5; vc.indel_frac = -1.; vc.n_perm = 0; vc.min_perm_p = 0.01; vc.min_smpl_frac = 0; vc.min_lrt = 1; vc.min_ma_lrt = -1;
        memset(qcnt, 0, 8 * 256);
-       while ((c = getopt(argc, argv, "FN1:l:cC:eHAGvbSuP:t:p:QgLi:IMs:D:U:X:d:T:Ywm:")) >= 0) {
+       while ((c = getopt(argc, argv, "FN1:l:cC:eHAGvbSuP:t:p:QgLi:IMs:D:U:X:d:T:Ywm:K:")) >= 0) {
                switch (c) {
                case '1': vc.n1 = atoi(optarg); break;
                case 'l': vc.bed = bed_read(optarg); break;
@@ -343,6 +346,7 @@ int bcfview(int argc, char *argv[])
                case 'C': vc.min_lrt = atof(optarg); break;
                case 'X': vc.min_perm_p = atof(optarg); break;
                case 'd': vc.min_smpl_frac = atof(optarg); break;
+               case 'K': bcf_p1_fp_lk = gzopen(optarg, "w"); break;
                case 's': vc.subsam = read_samples(optarg, &vc.n_sub);
                        vc.ploidy = calloc(vc.n_sub + 1, 1);
                        for (tid = 0; tid < vc.n_sub; ++tid) vc.ploidy[tid] = vc.subsam[tid][strlen(vc.subsam[tid]) + 1];
@@ -466,6 +470,10 @@ int bcfview(int argc, char *argv[])
                        }
                }
        }
+       if (bcf_p1_fp_lk && p1) {
+               int32_t M = bcf_p1_get_M(p1);
+               gzwrite(bcf_p1_fp_lk, &M, 4);
+       }
        while (vcf_read(bp, hin, b) > 0) {
                int is_indel, cons_llr = -1;
                int64_t cons_gt = -1;
@@ -514,7 +522,7 @@ int bcfview(int argc, char *argv[])
                        int i;
                        for (i = 0; i < 9; ++i) em[i] = -1.;
                }
-        if ( !(vc.flag&VC_KEEPALT) && vc.flag&VC_CALL && vc.min_ma_lrt>=0 )
+        if ( !(vc.flag&VC_KEEPALT) && (vc.flag&VC_CALL) && vc.min_ma_lrt>=0 )
         {
             bcf_p1_set_ploidy(b, p1); // could be improved: do this per site to allow pseudo-autosomal regions
             int gts = call_multiallelic_gt(b,p1,vc.min_ma_lrt);
@@ -522,7 +530,11 @@ int bcfview(int argc, char *argv[])
         }
                else if (vc.flag & VC_CALL) { // call variants
                        bcf_p1rst_t pr;
-                       int calret = bcf_p1_cal(b, (em[7] >= 0 && em[7] < vc.min_lrt), p1, &pr);
+                       int calret;
+                       gzwrite(bcf_p1_fp_lk, &b->tid, 4);
+                       gzwrite(bcf_p1_fp_lk, &b->pos, 4);
+                       gzwrite(bcf_p1_fp_lk, &em[0], sizeof(double));
+                       calret = bcf_p1_cal(b, (em[7] >= 0 && em[7] < vc.min_lrt), p1, &pr);
                        if (n_processed % 100000 == 0) {
                                fprintf(stderr, "[%s] %ld sites processed.\n", __func__, (long)n_processed);
                                bcf_p1_dump_afs(p1);
@@ -567,6 +579,8 @@ int bcfview(int argc, char *argv[])
                } else bcf_fix_gt(b);
                vcf_write(bout, hout, b);
        }
+
+       if (bcf_p1_fp_lk) gzclose(bcf_p1_fp_lk);
        if (vc.prior_file) free(vc.prior_file);
        if (vc.flag & VC_CALL) bcf_p1_dump_afs(p1);
        if (hin != hout) bcf_hdr_destroy(hout);
index 5051cc3cece3770e4e4e61e1934c8cfff79998dc..ffa608e29d6408c03a858f3db3daba162a1e9ed2 100644 (file)
@@ -5,6 +5,7 @@
 #include <errno.h>
 #include <assert.h>
 #include <limits.h>
+#include <zlib.h>
 #include "prob1.h"
 #include "kstring.h"
 
@@ -15,6 +16,8 @@ KSTREAM_INIT(gzFile, gzread, 16384)
 #define MC_EM_EPS 1e-5
 #define MC_DEF_INDEL 0.15
 
+gzFile bcf_p1_fp_lk;
+
 unsigned char seq_nt4_table[256] = {
        4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
        4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4,  4, 4, 4, 4, 
@@ -165,6 +168,8 @@ bcf_p1aux_t *bcf_p1_init(int n, uint8_t *ploidy)
        return ma;
 }
 
+int bcf_p1_get_M(bcf_p1aux_t *b) { return b->M; }
+
 int bcf_p1_set_n1(bcf_p1aux_t *b, int n1)
 {
        if (n1 == 0 || n1 >= b->n) return -1;
@@ -598,6 +603,8 @@ static void mc_cal_y_core(bcf_p1aux_t *ma, int beg)
                }
        }
        if (z[0] != ma->z) memcpy(ma->z, z[0], sizeof(double) * (ma->M + 1));
+       if (bcf_p1_fp_lk)
+               gzwrite(bcf_p1_fp_lk, ma->z, sizeof(double) * (ma->M + 1));
 }
 
 static void mc_cal_y(bcf_p1aux_t *ma)