annotate PsiCLASS-1.0.2/samtools-0.1.19/bam_tview.c @ 0:903fc43d6227 draft default tip

Uploaded
author lsong10
date Fri, 26 Mar 2021 16:52:45 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1 #include <assert.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
2 #include "bam_tview.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
3
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
4 int base_tv_init(tview_t* tv,const char *fn, const char *fn_fa, const char *samples)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
5 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
6 assert(tv!=NULL);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
7 assert(fn!=NULL);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
8 tv->mrow = 24; tv->mcol = 80;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
9 tv->color_for = TV_COLOR_MAPQ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
10 tv->is_dot = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
11
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
12 tv->fp = bam_open(fn, "r");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
13 if(tv->fp==0)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
14 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
15 fprintf(stderr,"bam_open %s. %s\n", fn,fn_fa);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
16 exit(EXIT_FAILURE);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
17 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
18 bgzf_set_cache_size(tv->fp, 8 * 1024 *1024);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
19 assert(tv->fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
20
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
21 tv->header = bam_header_read(tv->fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
22 if(tv->header==0)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
23 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
24 fprintf(stderr,"Cannot read '%s'.\n", fn);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
25 exit(EXIT_FAILURE);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
26 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
27 tv->idx = bam_index_load(fn);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
28 if (tv->idx == 0)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
29 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
30 fprintf(stderr,"Cannot read index for '%s'.\n", fn);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
31 exit(EXIT_FAILURE);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
32 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
33 tv->lplbuf = bam_lplbuf_init(tv_pl_func, tv);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
34 if (fn_fa) tv->fai = fai_load(fn_fa);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
35 tv->bca = bcf_call_init(0.83, 13);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
36 tv->ins = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
37
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
38 if ( samples )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
39 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
40 if ( !tv->header->dict ) tv->header->dict = sam_header_parse2(tv->header->text);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
41 void *iter = tv->header->dict;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
42 const char *key, *val;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
43 int n = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
44 tv->rg_hash = kh_init(kh_rg);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
45 while ( (iter = sam_header2key_val(iter, "RG","ID","SM", &key, &val)) )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
46 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
47 if ( !strcmp(samples,key) || (val && !strcmp(samples,val)) )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
48 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
49 khiter_t k = kh_get(kh_rg, tv->rg_hash, key);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
50 if ( k != kh_end(tv->rg_hash) ) continue;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
51 int ret;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
52 k = kh_put(kh_rg, tv->rg_hash, key, &ret);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
53 kh_value(tv->rg_hash, k) = val;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
54 n++;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
55 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
56 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
57 if ( !n )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
58 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
59 fprintf(stderr,"The sample or read group \"%s\" not present.\n", samples);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
60 exit(EXIT_FAILURE);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
61 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
62 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
63
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
64 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
65 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
66
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
67
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
68 void base_tv_destroy(tview_t* tv)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
69 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
70 bam_lplbuf_destroy(tv->lplbuf);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
71 bcf_call_destroy(tv->bca);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
72 bam_index_destroy(tv->idx);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
73 if (tv->fai) fai_destroy(tv->fai);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
74 free(tv->ref);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
75 bam_header_destroy(tv->header);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
76 bam_close(tv->fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
77 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
78
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
79
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
80 int tv_pl_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pl, void *data)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
81 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
82 extern unsigned char bam_nt16_table[256];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
83 tview_t *tv = (tview_t*)data;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
84 int i, j, c, rb, attr, max_ins = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
85 uint32_t call = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
86 if (pos < tv->left_pos || tv->ccol > tv->mcol) return 0; // out of screen
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
87 // print referece
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
88 rb = (tv->ref && pos - tv->left_pos < tv->l_ref)? tv->ref[pos - tv->left_pos] : 'N';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
89 for (i = tv->last_pos + 1; i < pos; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
90 if (i%10 == 0 && tv->mcol - tv->ccol >= 10) tv->my_mvprintw(tv,0, tv->ccol, "%-d", i+1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
91 c = tv->ref? tv->ref[i - tv->left_pos] : 'N';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
92 tv->my_mvaddch(tv,1, tv->ccol++, c);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
93 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
94 if (pos%10 == 0 && tv->mcol - tv->ccol >= 10) tv->my_mvprintw(tv,0, tv->ccol, "%-d", pos+1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
95 { // call consensus
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
96 bcf_callret1_t bcr;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
97 int qsum[4], a1, a2, tmp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
98 double p[3], prior = 30;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
99 bcf_call_glfgen(n, pl, bam_nt16_table[rb], tv->bca, &bcr);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
100 for (i = 0; i < 4; ++i) qsum[i] = bcr.qsum[i]<<2 | i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
101 for (i = 1; i < 4; ++i) // insertion sort
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
102 for (j = i; j > 0 && qsum[j] > qsum[j-1]; --j)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
103 tmp = qsum[j], qsum[j] = qsum[j-1], qsum[j-1] = tmp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
104 a1 = qsum[0]&3; a2 = qsum[1]&3;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
105 p[0] = bcr.p[a1*5+a1]; p[1] = bcr.p[a1*5+a2] + prior; p[2] = bcr.p[a2*5+a2];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
106 if ("ACGT"[a1] != toupper(rb)) p[0] += prior + 3;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
107 if ("ACGT"[a2] != toupper(rb)) p[2] += prior + 3;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
108 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);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
109 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);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
110 else call = (1<<a1|1<<a2)<<16 | (int)((p[0]<p[2]?p[0]:p[2]) - p[1] + .499);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
111 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
112 attr = tv->my_underline(tv);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
113 c = ",ACMGRSVTWYHKDBN"[call>>16&0xf];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
114 i = (call&0xffff)/10+1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
115 if (i > 4) i = 4;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
116 attr |= tv->my_colorpair(tv,i);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
117 if (c == toupper(rb)) c = '.';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
118 tv->my_attron(tv,attr);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
119 tv->my_mvaddch(tv,2, tv->ccol, c);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
120 tv->my_attroff(tv,attr);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
121 if(tv->ins) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
122 // calculate maximum insert
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
123 for (i = 0; i < n; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
124 const bam_pileup1_t *p = pl + i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
125 if (p->indel > 0 && max_ins < p->indel) max_ins = p->indel;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
126 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
127 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
128 // core loop
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
129 for (j = 0; j <= max_ins; ++j) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
130 for (i = 0; i < n; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
131 const bam_pileup1_t *p = pl + i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
132 int row = TV_MIN_ALNROW + p->level - tv->row_shift;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
133 if (j == 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
134 if (!p->is_del) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
135 if (tv->base_for == TV_BASE_COLOR_SPACE &&
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
136 (c = bam_aux_getCSi(p->b, p->qpos))) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
137 // assume that if we found one color, we will be able to get the color error
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
138 if (tv->is_dot && '-' == bam_aux_getCEi(p->b, p->qpos)) c = bam1_strand(p->b)? ',' : '.';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
139 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
140 if (tv->show_name) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
141 char *name = bam1_qname(p->b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
142 c = (p->qpos + 1 >= p->b->core.l_qname)? ' ' : name[p->qpos];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
143 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
144 c = bam_nt16_rev_table[bam1_seqi(bam1_seq(p->b), p->qpos)];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
145 if (tv->is_dot && toupper(c) == toupper(rb)) c = bam1_strand(p->b)? ',' : '.';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
146 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
147 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
148 } else c = p->is_refskip? (bam1_strand(p->b)? '<' : '>') : '*';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
149 } else { // padding
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
150 if (j > p->indel) c = '*';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
151 else { // insertion
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
152 if (tv->base_for == TV_BASE_NUCL) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
153 if (tv->show_name) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
154 char *name = bam1_qname(p->b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
155 c = (p->qpos + j + 1 >= p->b->core.l_qname)? ' ' : name[p->qpos + j];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
156 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
157 c = bam_nt16_rev_table[bam1_seqi(bam1_seq(p->b), p->qpos + j)];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
158 if (j == 0 && tv->is_dot && toupper(c) == toupper(rb)) c = bam1_strand(p->b)? ',' : '.';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
159 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
160 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
161 c = bam_aux_getCSi(p->b, p->qpos + j);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
162 if (tv->is_dot && '-' == bam_aux_getCEi(p->b, p->qpos + j)) c = bam1_strand(p->b)? ',' : '.';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
163 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
164 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
165 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
166 if (row > TV_MIN_ALNROW && row < tv->mrow) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
167 int x;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
168 attr = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
169 if (((p->b->core.flag&BAM_FPAIRED) && !(p->b->core.flag&BAM_FPROPER_PAIR))
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
170 || (p->b->core.flag & BAM_FSECONDARY)) attr |= tv->my_underline(tv);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
171 if (tv->color_for == TV_COLOR_BASEQ) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
172 x = bam1_qual(p->b)[p->qpos]/10 + 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
173 if (x > 4) x = 4;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
174 attr |= tv->my_colorpair(tv,x);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
175 } else if (tv->color_for == TV_COLOR_MAPQ) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
176 x = p->b->core.qual/10 + 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
177 if (x > 4) x = 4;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
178 attr |= tv->my_colorpair(tv,x);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
179 } else if (tv->color_for == TV_COLOR_NUCL) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
180 x = bam_nt16_nt4_table[bam1_seqi(bam1_seq(p->b), p->qpos)] + 5;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
181 attr |= tv->my_colorpair(tv,x);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
182 } else if(tv->color_for == TV_COLOR_COL) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
183 x = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
184 switch(bam_aux_getCSi(p->b, p->qpos)) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
185 case '0': x = 0; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
186 case '1': x = 1; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
187 case '2': x = 2; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
188 case '3': x = 3; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
189 case '4': x = 4; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
190 default: x = bam_nt16_nt4_table[bam1_seqi(bam1_seq(p->b), p->qpos)]; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
191 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
192 x+=5;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
193 attr |= tv->my_colorpair(tv,x);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
194 } else if(tv->color_for == TV_COLOR_COLQ) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
195 x = bam_aux_getCQi(p->b, p->qpos);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
196 if(0 == x) x = bam1_qual(p->b)[p->qpos];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
197 x = x/10 + 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
198 if (x > 4) x = 4;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
199 attr |= tv->my_colorpair(tv,x);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
200 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
201 tv->my_attron(tv,attr);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
202 tv->my_mvaddch(tv,row, tv->ccol, bam1_strand(p->b)? tolower(c) : toupper(c));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
203 tv->my_attroff(tv,attr);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
204 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
205 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
206 c = j? '*' : rb;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
207 if (c == '*') {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
208 attr = tv->my_colorpair(tv,8);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
209 tv->my_attron(tv,attr);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
210 tv->my_mvaddch(tv,1, tv->ccol++, c);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
211 tv->my_attroff(tv,attr);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
212 } else tv->my_mvaddch(tv,1, tv->ccol++, c);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
213 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
214 tv->last_pos = pos;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
215 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
216 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
217
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
218
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
219
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
220
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
221 int tv_fetch_func(const bam1_t *b, void *data)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
222 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
223 tview_t *tv = (tview_t*)data;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
224 if ( tv->rg_hash )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
225 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
226 const uint8_t *rg = bam_aux_get(b, "RG");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
227 if ( !rg ) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
228 khiter_t k = kh_get(kh_rg, tv->rg_hash, (const char*)(rg + 1));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
229 if ( k == kh_end(tv->rg_hash) ) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
230 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
231 if (tv->no_skip) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
232 uint32_t *cigar = bam1_cigar(b); // this is cheating...
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
233 int i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
234 for (i = 0; i <b->core.n_cigar; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
235 if ((cigar[i]&0xf) == BAM_CREF_SKIP)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
236 cigar[i] = cigar[i]>>4<<4 | BAM_CDEL;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
237 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
238 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
239 bam_lplbuf_push(b, tv->lplbuf);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
240 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
241 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
242
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
243 int base_draw_aln(tview_t *tv, int tid, int pos)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
244 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
245 assert(tv!=NULL);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
246 // reset
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
247 tv->my_clear(tv);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
248 tv->curr_tid = tid; tv->left_pos = pos;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
249 tv->last_pos = tv->left_pos - 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
250 tv->ccol = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
251 // print ref and consensus
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
252 if (tv->fai) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
253 char *str;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
254 if (tv->ref) free(tv->ref);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
255 assert(tv->curr_tid>=0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
256
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
257 str = (char*)calloc(strlen(tv->header->target_name[tv->curr_tid]) + 30, 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
258 assert(str!=NULL);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
259 sprintf(str, "%s:%d-%d", tv->header->target_name[tv->curr_tid], tv->left_pos + 1, tv->left_pos + tv->mcol);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
260 tv->ref = fai_fetch(tv->fai, str, &tv->l_ref);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
261 free(str);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
262 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
263 // draw aln
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
264 bam_lplbuf_reset(tv->lplbuf);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
265 bam_fetch(tv->fp, tv->idx, tv->curr_tid, tv->left_pos, tv->left_pos + tv->mcol, tv, tv_fetch_func);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
266 bam_lplbuf_push(0, tv->lplbuf);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
267
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
268 while (tv->ccol < tv->mcol) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
269 int pos = tv->last_pos + 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
270 if (pos%10 == 0 && tv->mcol - tv->ccol >= 10) tv->my_mvprintw(tv,0, tv->ccol, "%-d", pos+1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
271 tv->my_mvaddch(tv,1, tv->ccol++, (tv->ref && pos < tv->l_ref)? tv->ref[pos - tv->left_pos] : 'N');
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
272 ++tv->last_pos;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
273 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
274 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
275 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
276
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
277
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
278
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
279
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
280 static void error(const char *format, ...)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
281 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
282 if ( !format )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
283 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
284 fprintf(stderr, "\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
285 fprintf(stderr, "Usage: bamtk tview [options] <aln.bam> [ref.fasta]\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
286 fprintf(stderr, "Options:\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
287 fprintf(stderr, " -d display output as (H)tml or (C)urses or (T)ext \n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
288 fprintf(stderr, " -p chr:pos go directly to this position\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
289 fprintf(stderr, " -s STR display only reads from this sample or group\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
290 fprintf(stderr, "\n\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
291 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
292 else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
293 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
294 va_list ap;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
295 va_start(ap, format);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
296 vfprintf(stderr, format, ap);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
297 va_end(ap);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
298 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
299 exit(-1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
300 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
301
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
302 enum dipsay_mode {display_ncurses,display_html,display_text};
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
303 extern tview_t* curses_tv_init(const char *fn, const char *fn_fa, const char *samples);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
304 extern tview_t* html_tv_init(const char *fn, const char *fn_fa, const char *samples);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
305 extern tview_t* text_tv_init(const char *fn, const char *fn_fa, const char *samples);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
306
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
307 int bam_tview_main(int argc, char *argv[])
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
308 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
309 int view_mode=display_ncurses;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
310 tview_t* tv=NULL;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
311 char *samples=NULL, *position=NULL;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
312 int c;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
313 while ((c = getopt(argc, argv, "s:p:d:")) >= 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
314 switch (c) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
315 case 's': samples=optarg; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
316 case 'p': position=optarg; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
317 case 'd':
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
318 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
319 switch(optarg[0])
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
320 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
321 case 'H': case 'h': view_mode=display_html;break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
322 case 'T': case 't': view_mode=display_text;break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
323 case 'C': case 'c': view_mode=display_ncurses;break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
324 default: view_mode=display_ncurses;break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
325 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
326 break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
327 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
328 default: error(NULL);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
329 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
330 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
331 if (argc==optind) error(NULL);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
332
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
333 switch(view_mode)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
334 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
335 case display_ncurses:
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
336 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
337 tv = curses_tv_init(argv[optind], (optind+1>=argc)? 0 : argv[optind+1], samples);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
338 break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
339 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
340 case display_text:
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
341 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
342 tv = text_tv_init(argv[optind], (optind+1>=argc)? 0 : argv[optind+1], samples);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
343 break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
344 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
345 case display_html:
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
346 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
347 tv = html_tv_init(argv[optind], (optind+1>=argc)? 0 : argv[optind+1], samples);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
348 break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
349 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
350 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
351 if(tv==NULL)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
352 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
353 error("cannot create view");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
354 return EXIT_FAILURE;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
355 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
356
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
357 if ( position )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
358 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
359 int _tid = -1, _beg, _end;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
360 bam_parse_region(tv->header, position, &_tid, &_beg, &_end);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
361 if (_tid >= 0) { tv->curr_tid = _tid; tv->left_pos = _beg; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
362 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
363 tv->my_drawaln(tv, tv->curr_tid, tv->left_pos);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
364 tv->my_loop(tv);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
365 tv->my_destroy(tv);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
366
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
367 return EXIT_SUCCESS;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
368 }