4 int base_tv_init(tview_t* tv,const char *fn, const char *fn_fa, const char *samples)
8 tv->mrow = 24; tv->mcol = 80;
9 tv->color_for = TV_COLOR_MAPQ;
11 fprintf(stderr,"Opening '%s'\n",fn);
12 tv->fp = bam_open(fn, "r");
15 fprintf(stderr,"bam_open %s. %s\n", fn,fn_fa);
18 bgzf_set_cache_size(tv->fp, 8 * 1024 *1024);
20 fprintf(stderr,"Opening header '%s'\n",fn);
21 tv->header = bam_header_read(tv->fp);
24 fprintf(stderr,"Cannot read '%s'.\n", fn);
27 tv->idx = bam_index_load(fn);
28 if (tv->idx == 0) exit(1);
29 tv->lplbuf = bam_lplbuf_init(tv_pl_func, tv);
30 if (fn_fa) tv->fai = fai_load(fn_fa);
31 tv->bca = bcf_call_init(0.83, 13);
36 if ( !tv->header->dict ) tv->header->dict = sam_header_parse2(tv->header->text);
37 void *iter = tv->header->dict;
38 const char *key, *val;
40 tv->rg_hash = kh_init(kh_rg);
41 while ( (iter = sam_header2key_val(iter, "RG","ID","SM", &key, &val)) )
43 if ( !strcmp(samples,key) || (val && !strcmp(samples,val)) )
45 khiter_t k = kh_get(kh_rg, tv->rg_hash, key);
46 if ( k != kh_end(tv->rg_hash) ) continue;
48 k = kh_put(kh_rg, tv->rg_hash, key, &ret);
49 kh_value(tv->rg_hash, k) = val;
55 fprintf(stderr,"The sample or read group \"%s\" not present.\n", samples);
64 void base_tv_destroy(tview_t* tv)
66 bam_lplbuf_destroy(tv->lplbuf);
67 bcf_call_destroy(tv->bca);
68 bam_index_destroy(tv->idx);
69 if (tv->fai) fai_destroy(tv->fai);
71 bam_header_destroy(tv->header);
76 int tv_pl_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pl, void *data)
78 extern unsigned char bam_nt16_table[256];
79 tview_t *tv = (tview_t*)data;
80 int i, j, c, rb, attr, max_ins = 0;
82 if (pos < tv->left_pos || tv->ccol > tv->mcol) return 0; // out of screen
84 rb = (tv->ref && pos - tv->left_pos < tv->l_ref)? tv->ref[pos - tv->left_pos] : 'N';
85 for (i = tv->last_pos + 1; i < pos; ++i) {
86 if (i%10 == 0 && tv->mcol - tv->ccol >= 10) tv->my_mvprintw(tv,0, tv->ccol, "%-d", i+1);
87 c = tv->ref? tv->ref[i - tv->left_pos] : 'N';
88 tv->my_mvaddch(tv,1, tv->ccol++, c);
90 if (pos%10 == 0 && tv->mcol - tv->ccol >= 10) tv->my_mvprintw(tv,0, tv->ccol, "%-d", pos+1);
93 int qsum[4], a1, a2, tmp;
94 double p[3], prior = 30;
95 bcf_call_glfgen(n, pl, bam_nt16_table[rb], tv->bca, &bcr);
96 for (i = 0; i < 4; ++i) qsum[i] = bcr.qsum[i]<<2 | i;
97 for (i = 1; i < 4; ++i) // insertion sort
98 for (j = i; j > 0 && qsum[j] > qsum[j-1]; --j)
99 tmp = qsum[j], qsum[j] = qsum[j-1], qsum[j-1] = tmp;
100 a1 = qsum[0]&3; a2 = qsum[1]&3;
101 p[0] = bcr.p[a1*5+a1]; p[1] = bcr.p[a1*5+a2] + prior; p[2] = bcr.p[a2*5+a2];
102 if ("ACGT"[a1] != toupper(rb)) p[0] += prior + 3;
103 if ("ACGT"[a2] != toupper(rb)) p[2] += prior + 3;
104 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);
105 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);
106 else call = (1<<a1|1<<a2)<<16 | (int)((p[0]<p[2]?p[0]:p[2]) - p[1] + .499);
108 attr = BAM_TVIEW_UNDERLINE;
109 c = ",ACMGRSVTWYHKDBN"[call>>16&0xf];
110 i = (call&0xffff)/10+1;
112 attr |= tv->my_colorpair(tv,i);
113 if (c == toupper(rb)) c = '.';
114 tv->my_attron(tv,attr);
115 tv->my_mvaddch(tv,2, tv->ccol, c);
116 tv->my_attroff(tv,attr);
118 // calculate maximum insert
119 for (i = 0; i < n; ++i) {
120 const bam_pileup1_t *p = pl + i;
121 if (p->indel > 0 && max_ins < p->indel) max_ins = p->indel;
125 for (j = 0; j <= max_ins; ++j) {
126 for (i = 0; i < n; ++i) {
127 const bam_pileup1_t *p = pl + i;
128 int row = TV_MIN_ALNROW + p->level - tv->row_shift;
131 if (tv->base_for == TV_BASE_COLOR_SPACE &&
132 (c = bam_aux_getCSi(p->b, p->qpos))) {
133 // assume that if we found one color, we will be able to get the color error
134 if (tv->is_dot && '-' == bam_aux_getCEi(p->b, p->qpos)) c = bam1_strand(p->b)? ',' : '.';
137 char *name = bam1_qname(p->b);
138 c = (p->qpos + 1 >= p->b->core.l_qname)? ' ' : name[p->qpos];
140 c = bam_nt16_rev_table[bam1_seqi(bam1_seq(p->b), p->qpos)];
141 if (tv->is_dot && toupper(c) == toupper(rb)) c = bam1_strand(p->b)? ',' : '.';
144 } else c = p->is_refskip? (bam1_strand(p->b)? '<' : '>') : '*';
146 if (j > p->indel) c = '*';
148 if (tv->base_for == TV_BASE_NUCL) {
150 char *name = bam1_qname(p->b);
151 c = (p->qpos + j + 1 >= p->b->core.l_qname)? ' ' : name[p->qpos + j];
153 c = bam_nt16_rev_table[bam1_seqi(bam1_seq(p->b), p->qpos + j)];
154 if (j == 0 && tv->is_dot && toupper(c) == toupper(rb)) c = bam1_strand(p->b)? ',' : '.';
157 c = bam_aux_getCSi(p->b, p->qpos + j);
158 if (tv->is_dot && '-' == bam_aux_getCEi(p->b, p->qpos + j)) c = bam1_strand(p->b)? ',' : '.';
162 if (row > TV_MIN_ALNROW && row < tv->mrow) {
165 if (((p->b->core.flag&BAM_FPAIRED) && !(p->b->core.flag&BAM_FPROPER_PAIR))
166 || (p->b->core.flag & BAM_FSECONDARY)) attr |= BAM_TVIEW_UNDERLINE;
167 if (tv->color_for == TV_COLOR_BASEQ) {
168 x = bam1_qual(p->b)[p->qpos]/10 + 1;
170 attr |= COLOR_PAIR(x);
171 } else if (tv->color_for == TV_COLOR_MAPQ) {
172 x = p->b->core.qual/10 + 1;
174 attr |= COLOR_PAIR(x);
175 } else if (tv->color_for == TV_COLOR_NUCL) {
176 x = bam_nt16_nt4_table[bam1_seqi(bam1_seq(p->b), p->qpos)] + 5;
177 attr |= COLOR_PAIR(x);
178 } else if(tv->color_for == TV_COLOR_COL) {
180 switch(bam_aux_getCSi(p->b, p->qpos)) {
181 case '0': x = 0; break;
182 case '1': x = 1; break;
183 case '2': x = 2; break;
184 case '3': x = 3; break;
185 case '4': x = 4; break;
186 default: x = bam_nt16_nt4_table[bam1_seqi(bam1_seq(p->b), p->qpos)]; break;
189 attr |= COLOR_PAIR(x);
190 } else if(tv->color_for == TV_COLOR_COLQ) {
191 x = bam_aux_getCQi(p->b, p->qpos);
192 if(0 == x) x = bam1_qual(p->b)[p->qpos];
195 attr |= COLOR_PAIR(x);
197 tv->my_attron(tv,attr);
198 tv->my_mvaddch(tv,row, tv->ccol, bam1_strand(p->b)? tolower(c) : toupper(c));
199 tv->my_attroff(tv,attr);
204 attr = COLOR_PAIR(8);
205 tv->my_attron(tv,attr);
206 tv->my_mvaddch(tv,1, tv->ccol++, c);
207 tv->my_attroff(tv,attr);
208 } else tv->my_mvaddch(tv,1, tv->ccol++, c);
217 int tv_fetch_func(const bam1_t *b, void *data)
219 tview_t *tv = (tview_t*)data;
222 const uint8_t *rg = bam_aux_get(b, "RG");
224 khiter_t k = kh_get(kh_rg, tv->rg_hash, (const char*)(rg + 1));
225 if ( k == kh_end(tv->rg_hash) ) return 0;
228 uint32_t *cigar = bam1_cigar(b); // this is cheating...
230 for (i = 0; i <b->core.n_cigar; ++i) {
231 if ((cigar[i]&0xf) == BAM_CREF_SKIP)
232 cigar[i] = cigar[i]>>4<<4 | BAM_CDEL;
235 bam_lplbuf_push(b, tv->lplbuf);
239 int tv_draw_aln(tview_t *tv, int tid, int pos)
244 tv->curr_tid = tid; tv->left_pos = pos;
245 tv->last_pos = tv->left_pos - 1;
247 // print ref and consensus
250 if (tv->ref) free(tv->ref);
251 assert(tv->curr_tid>=0);
253 str = (char*)calloc(strlen(tv->header->target_name[tv->curr_tid]) + 30, 1);
255 sprintf(str, "%s:%d-%d", tv->header->target_name[tv->curr_tid], tv->left_pos + 1, tv->left_pos + tv->mcol);
256 tv->ref = fai_fetch(tv->fai, str, &tv->l_ref);
260 bam_lplbuf_reset(tv->lplbuf);
261 bam_fetch(tv->fp, tv->idx, tv->curr_tid, tv->left_pos, tv->left_pos + tv->mcol, tv, tv_fetch_func);
262 bam_lplbuf_push(0, tv->lplbuf);
264 while (tv->ccol < tv->mcol) {
265 int pos = tv->last_pos + 1;
266 if (pos%10 == 0 && tv->mcol - tv->ccol >= 10) tv->my_mvprintw(tv,0, tv->ccol, "%-d", pos+1);
267 tv->my_mvaddch(tv,1, tv->ccol++, (tv->ref && pos < tv->l_ref)? tv->ref[pos - tv->left_pos] : 'N');