annotate PsiCLASS-1.0.2/samtools-0.1.19/bam.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 <stdio.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
2 #include <ctype.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
3 #include <errno.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
4 #include <assert.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
5 #include "bam.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
6 #include "bam_endian.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
7 #include "kstring.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
8 #include "sam_header.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
9
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
10 int bam_is_be = 0, bam_verbose = 2, bam_no_B = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
11 char *bam_flag2char_table = "pPuUrR12sfd\0\0\0\0\0";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
12
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
13 /**************************
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
14 * CIGAR related routines *
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
15 **************************/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
16
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
17 uint32_t bam_calend(const bam1_core_t *c, const uint32_t *cigar)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
18 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
19 int k, end = c->pos;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
20 for (k = 0; k < c->n_cigar; ++k) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
21 int op = bam_cigar_op(cigar[k]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
22 int len = bam_cigar_oplen(cigar[k]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
23 if (op == BAM_CBACK) { // move backward
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
24 int l, u, v;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
25 if (k == c->n_cigar - 1) break; // skip trailing 'B'
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
26 for (l = k - 1, u = v = 0; l >= 0; --l) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
27 int op1 = bam_cigar_op(cigar[l]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
28 int len1 = bam_cigar_oplen(cigar[l]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
29 if (bam_cigar_type(op1)&1) { // consume query
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
30 if (u + len1 >= len) { // stop
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
31 if (bam_cigar_type(op1)&2) v += len - u;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
32 break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
33 } else u += len1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
34 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
35 if (bam_cigar_type(op1)&2) v += len1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
36 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
37 end = l < 0? c->pos : end - v;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
38 } else if (bam_cigar_type(op)&2) end += bam_cigar_oplen(cigar[k]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
39 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
40 return end;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
41 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
42
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
43 int32_t bam_cigar2qlen(const bam1_core_t *c, const uint32_t *cigar)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
44 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
45 uint32_t k;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
46 int32_t l = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
47 for (k = 0; k < c->n_cigar; ++k)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
48 if (bam_cigar_type(bam_cigar_op(cigar[k]))&1)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
49 l += bam_cigar_oplen(cigar[k]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
50 return l;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
51 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
52
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
53 /********************
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
54 * BAM I/O routines *
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
55 ********************/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
56
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
57 bam_header_t *bam_header_init()
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
58 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
59 bam_is_be = bam_is_big_endian();
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
60 return (bam_header_t*)calloc(1, sizeof(bam_header_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
61 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
62
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
63 void bam_header_destroy(bam_header_t *header)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
64 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
65 int32_t i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
66 extern void bam_destroy_header_hash(bam_header_t *header);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
67 if (header == 0) return;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
68 if (header->target_name) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
69 for (i = 0; i < header->n_targets; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
70 free(header->target_name[i]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
71 free(header->target_name);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
72 free(header->target_len);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
73 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
74 free(header->text);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
75 if (header->dict) sam_header_free(header->dict);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
76 if (header->rg2lib) sam_tbl_destroy(header->rg2lib);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
77 bam_destroy_header_hash(header);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
78 free(header);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
79 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
80
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
81 bam_header_t *bam_header_read(bamFile fp)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
82 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
83 bam_header_t *header;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
84 char buf[4];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
85 int magic_len;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
86 int32_t i = 1, name_len;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
87 // check EOF
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
88 i = bgzf_check_EOF(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
89 if (i < 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
90 // If the file is a pipe, checking the EOF marker will *always* fail
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
91 // with ESPIPE. Suppress the error message in this case.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
92 if (errno != ESPIPE) perror("[bam_header_read] bgzf_check_EOF");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
93 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
94 else if (i == 0) fprintf(stderr, "[bam_header_read] EOF marker is absent. The input is probably truncated.\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
95 // read "BAM1"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
96 magic_len = bam_read(fp, buf, 4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
97 if (magic_len != 4 || strncmp(buf, "BAM\001", 4) != 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
98 fprintf(stderr, "[bam_header_read] invalid BAM binary header (this is not a BAM file).\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
99 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
100 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
101 header = bam_header_init();
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
102 // read plain text and the number of reference sequences
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
103 bam_read(fp, &header->l_text, 4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
104 if (bam_is_be) bam_swap_endian_4p(&header->l_text);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
105 header->text = (char*)calloc(header->l_text + 1, 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
106 bam_read(fp, header->text, header->l_text);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
107 bam_read(fp, &header->n_targets, 4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
108 if (bam_is_be) bam_swap_endian_4p(&header->n_targets);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
109 // read reference sequence names and lengths
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
110 header->target_name = (char**)calloc(header->n_targets, sizeof(char*));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
111 header->target_len = (uint32_t*)calloc(header->n_targets, 4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
112 for (i = 0; i != header->n_targets; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
113 bam_read(fp, &name_len, 4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
114 if (bam_is_be) bam_swap_endian_4p(&name_len);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
115 header->target_name[i] = (char*)calloc(name_len, 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
116 bam_read(fp, header->target_name[i], name_len);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
117 bam_read(fp, &header->target_len[i], 4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
118 if (bam_is_be) bam_swap_endian_4p(&header->target_len[i]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
119 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
120 return header;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
121 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
122
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
123 int bam_header_write(bamFile fp, const bam_header_t *header)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
124 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
125 char buf[4];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
126 int32_t i, name_len, x;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
127 // write "BAM1"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
128 strncpy(buf, "BAM\001", 4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
129 bam_write(fp, buf, 4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
130 // write plain text and the number of reference sequences
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
131 if (bam_is_be) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
132 x = bam_swap_endian_4(header->l_text);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
133 bam_write(fp, &x, 4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
134 if (header->l_text) bam_write(fp, header->text, header->l_text);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
135 x = bam_swap_endian_4(header->n_targets);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
136 bam_write(fp, &x, 4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
137 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
138 bam_write(fp, &header->l_text, 4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
139 if (header->l_text) bam_write(fp, header->text, header->l_text);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
140 bam_write(fp, &header->n_targets, 4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
141 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
142 // write sequence names and lengths
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
143 for (i = 0; i != header->n_targets; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
144 char *p = header->target_name[i];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
145 name_len = strlen(p) + 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
146 if (bam_is_be) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
147 x = bam_swap_endian_4(name_len);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
148 bam_write(fp, &x, 4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
149 } else bam_write(fp, &name_len, 4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
150 bam_write(fp, p, name_len);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
151 if (bam_is_be) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
152 x = bam_swap_endian_4(header->target_len[i]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
153 bam_write(fp, &x, 4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
154 } else bam_write(fp, &header->target_len[i], 4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
155 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
156 bgzf_flush(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
157 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
158 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
159
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
160 static void swap_endian_data(const bam1_core_t *c, int data_len, uint8_t *data)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
161 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
162 uint8_t *s;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
163 uint32_t i, *cigar = (uint32_t*)(data + c->l_qname);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
164 s = data + c->n_cigar*4 + c->l_qname + c->l_qseq + (c->l_qseq + 1)/2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
165 for (i = 0; i < c->n_cigar; ++i) bam_swap_endian_4p(&cigar[i]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
166 while (s < data + data_len) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
167 uint8_t type;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
168 s += 2; // skip key
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
169 type = toupper(*s); ++s; // skip type
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
170 if (type == 'C' || type == 'A') ++s;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
171 else if (type == 'S') { bam_swap_endian_2p(s); s += 2; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
172 else if (type == 'I' || type == 'F') { bam_swap_endian_4p(s); s += 4; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
173 else if (type == 'D') { bam_swap_endian_8p(s); s += 8; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
174 else if (type == 'Z' || type == 'H') { while (*s) ++s; ++s; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
175 else if (type == 'B') {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
176 int32_t n, Bsize = bam_aux_type2size(*s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
177 memcpy(&n, s + 1, 4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
178 if (1 == Bsize) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
179 } else if (2 == Bsize) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
180 for (i = 0; i < n; i += 2)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
181 bam_swap_endian_2p(s + 5 + i);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
182 } else if (4 == Bsize) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
183 for (i = 0; i < n; i += 4)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
184 bam_swap_endian_4p(s + 5 + i);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
185 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
186 bam_swap_endian_4p(s+1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
187 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
188 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
189 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
190
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
191 int bam_read1(bamFile fp, bam1_t *b)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
192 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
193 bam1_core_t *c = &b->core;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
194 int32_t block_len, ret, i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
195 uint32_t x[8];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
196
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
197 assert(BAM_CORE_SIZE == 32);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
198 if ((ret = bam_read(fp, &block_len, 4)) != 4) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
199 if (ret == 0) return -1; // normal end-of-file
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
200 else return -2; // truncated
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
201 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
202 if (bam_read(fp, x, BAM_CORE_SIZE) != BAM_CORE_SIZE) return -3;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
203 if (bam_is_be) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
204 bam_swap_endian_4p(&block_len);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
205 for (i = 0; i < 8; ++i) bam_swap_endian_4p(x + i);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
206 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
207 c->tid = x[0]; c->pos = x[1];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
208 c->bin = x[2]>>16; c->qual = x[2]>>8&0xff; c->l_qname = x[2]&0xff;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
209 c->flag = x[3]>>16; c->n_cigar = x[3]&0xffff;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
210 c->l_qseq = x[4];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
211 c->mtid = x[5]; c->mpos = x[6]; c->isize = x[7];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
212 b->data_len = block_len - BAM_CORE_SIZE;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
213 if (b->m_data < b->data_len) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
214 b->m_data = b->data_len;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
215 kroundup32(b->m_data);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
216 b->data = (uint8_t*)realloc(b->data, b->m_data);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
217 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
218 if (bam_read(fp, b->data, b->data_len) != b->data_len) return -4;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
219 b->l_aux = b->data_len - c->n_cigar * 4 - c->l_qname - c->l_qseq - (c->l_qseq+1)/2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
220 if (bam_is_be) swap_endian_data(c, b->data_len, b->data);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
221 if (bam_no_B) bam_remove_B(b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
222 return 4 + block_len;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
223 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
224
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
225 inline int bam_write1_core(bamFile fp, const bam1_core_t *c, int data_len, uint8_t *data)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
226 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
227 uint32_t x[8], block_len = data_len + BAM_CORE_SIZE, y;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
228 int i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
229 assert(BAM_CORE_SIZE == 32);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
230 x[0] = c->tid;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
231 x[1] = c->pos;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
232 x[2] = (uint32_t)c->bin<<16 | c->qual<<8 | c->l_qname;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
233 x[3] = (uint32_t)c->flag<<16 | c->n_cigar;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
234 x[4] = c->l_qseq;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
235 x[5] = c->mtid;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
236 x[6] = c->mpos;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
237 x[7] = c->isize;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
238 bgzf_flush_try(fp, 4 + block_len);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
239 if (bam_is_be) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
240 for (i = 0; i < 8; ++i) bam_swap_endian_4p(x + i);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
241 y = block_len;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
242 bam_write(fp, bam_swap_endian_4p(&y), 4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
243 swap_endian_data(c, data_len, data);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
244 } else bam_write(fp, &block_len, 4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
245 bam_write(fp, x, BAM_CORE_SIZE);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
246 bam_write(fp, data, data_len);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
247 if (bam_is_be) swap_endian_data(c, data_len, data);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
248 return 4 + block_len;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
249 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
250
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
251 int bam_write1(bamFile fp, const bam1_t *b)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
252 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
253 return bam_write1_core(fp, &b->core, b->data_len, b->data);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
254 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
255
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
256 char *bam_format1_core(const bam_header_t *header, const bam1_t *b, int of)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
257 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
258 uint8_t *s = bam1_seq(b), *t = bam1_qual(b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
259 int i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
260 const bam1_core_t *c = &b->core;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
261 kstring_t str;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
262 str.l = str.m = 0; str.s = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
263
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
264 kputsn(bam1_qname(b), c->l_qname-1, &str); kputc('\t', &str);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
265 if (of == BAM_OFDEC) { kputw(c->flag, &str); kputc('\t', &str); }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
266 else if (of == BAM_OFHEX) ksprintf(&str, "0x%x\t", c->flag);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
267 else { // BAM_OFSTR
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
268 for (i = 0; i < 16; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
269 if ((c->flag & 1<<i) && bam_flag2char_table[i])
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
270 kputc(bam_flag2char_table[i], &str);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
271 kputc('\t', &str);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
272 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
273 if (c->tid < 0) kputsn("*\t", 2, &str);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
274 else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
275 if (header) kputs(header->target_name[c->tid] , &str);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
276 else kputw(c->tid, &str);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
277 kputc('\t', &str);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
278 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
279 kputw(c->pos + 1, &str); kputc('\t', &str); kputw(c->qual, &str); kputc('\t', &str);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
280 if (c->n_cigar == 0) kputc('*', &str);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
281 else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
282 uint32_t *cigar = bam1_cigar(b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
283 for (i = 0; i < c->n_cigar; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
284 kputw(bam1_cigar(b)[i]>>BAM_CIGAR_SHIFT, &str);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
285 kputc(bam_cigar_opchr(cigar[i]), &str);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
286 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
287 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
288 kputc('\t', &str);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
289 if (c->mtid < 0) kputsn("*\t", 2, &str);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
290 else if (c->mtid == c->tid) kputsn("=\t", 2, &str);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
291 else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
292 if (header) kputs(header->target_name[c->mtid], &str);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
293 else kputw(c->mtid, &str);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
294 kputc('\t', &str);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
295 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
296 kputw(c->mpos + 1, &str); kputc('\t', &str); kputw(c->isize, &str); kputc('\t', &str);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
297 if (c->l_qseq) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
298 for (i = 0; i < c->l_qseq; ++i) kputc(bam_nt16_rev_table[bam1_seqi(s, i)], &str);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
299 kputc('\t', &str);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
300 if (t[0] == 0xff) kputc('*', &str);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
301 else for (i = 0; i < c->l_qseq; ++i) kputc(t[i] + 33, &str);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
302 } else kputsn("*\t*", 3, &str);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
303 s = bam1_aux(b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
304 while (s < b->data + b->data_len) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
305 uint8_t type, key[2];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
306 key[0] = s[0]; key[1] = s[1];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
307 s += 2; type = *s; ++s;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
308 kputc('\t', &str); kputsn((char*)key, 2, &str); kputc(':', &str);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
309 if (type == 'A') { kputsn("A:", 2, &str); kputc(*s, &str); ++s; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
310 else if (type == 'C') { kputsn("i:", 2, &str); kputw(*s, &str); ++s; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
311 else if (type == 'c') { kputsn("i:", 2, &str); kputw(*(int8_t*)s, &str); ++s; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
312 else if (type == 'S') { kputsn("i:", 2, &str); kputw(*(uint16_t*)s, &str); s += 2; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
313 else if (type == 's') { kputsn("i:", 2, &str); kputw(*(int16_t*)s, &str); s += 2; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
314 else if (type == 'I') { kputsn("i:", 2, &str); kputuw(*(uint32_t*)s, &str); s += 4; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
315 else if (type == 'i') { kputsn("i:", 2, &str); kputw(*(int32_t*)s, &str); s += 4; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
316 else if (type == 'f') { ksprintf(&str, "f:%g", *(float*)s); s += 4; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
317 else if (type == 'd') { ksprintf(&str, "d:%lg", *(double*)s); s += 8; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
318 else if (type == 'Z' || type == 'H') { kputc(type, &str); kputc(':', &str); while (*s) kputc(*s++, &str); ++s; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
319 else if (type == 'B') {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
320 uint8_t sub_type = *(s++);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
321 int32_t n;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
322 memcpy(&n, s, 4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
323 s += 4; // no point to the start of the array
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
324 kputc(type, &str); kputc(':', &str); kputc(sub_type, &str); // write the typing
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
325 for (i = 0; i < n; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
326 kputc(',', &str);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
327 if ('c' == sub_type || 'c' == sub_type) { kputw(*(int8_t*)s, &str); ++s; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
328 else if ('C' == sub_type) { kputw(*(uint8_t*)s, &str); ++s; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
329 else if ('s' == sub_type) { kputw(*(int16_t*)s, &str); s += 2; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
330 else if ('S' == sub_type) { kputw(*(uint16_t*)s, &str); s += 2; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
331 else if ('i' == sub_type) { kputw(*(int32_t*)s, &str); s += 4; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
332 else if ('I' == sub_type) { kputuw(*(uint32_t*)s, &str); s += 4; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
333 else if ('f' == sub_type) { ksprintf(&str, "%g", *(float*)s); s += 4; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
334 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
335 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
336 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
337 return str.s;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
338 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
339
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
340 char *bam_format1(const bam_header_t *header, const bam1_t *b)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
341 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
342 return bam_format1_core(header, b, BAM_OFDEC);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
343 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
344
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
345 void bam_view1(const bam_header_t *header, const bam1_t *b)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
346 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
347 char *s = bam_format1(header, b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
348 puts(s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
349 free(s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
350 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
351
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
352 int bam_validate1(const bam_header_t *header, const bam1_t *b)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
353 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
354 char *s;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
355
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
356 if (b->core.tid < -1 || b->core.mtid < -1) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
357 if (header && (b->core.tid >= header->n_targets || b->core.mtid >= header->n_targets)) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
358
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
359 if (b->data_len < b->core.l_qname) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
360 s = memchr(bam1_qname(b), '\0', b->core.l_qname);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
361 if (s != &bam1_qname(b)[b->core.l_qname-1]) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
362
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
363 // FIXME: Other fields could also be checked, especially the auxiliary data
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
364
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
365 return 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
366 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
367
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
368 // FIXME: we should also check the LB tag associated with each alignment
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
369 const char *bam_get_library(bam_header_t *h, const bam1_t *b)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
370 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
371 const uint8_t *rg;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
372 if (h->dict == 0) h->dict = sam_header_parse2(h->text);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
373 if (h->rg2lib == 0) h->rg2lib = sam_header2tbl(h->dict, "RG", "ID", "LB");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
374 rg = bam_aux_get(b, "RG");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
375 return (rg == 0)? 0 : sam_tbl_get(h->rg2lib, (const char*)(rg + 1));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
376 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
377
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
378 /************
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
379 * Remove B *
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
380 ************/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
381
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
382 int bam_remove_B(bam1_t *b)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
383 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
384 int i, j, end_j, k, l, no_qual;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
385 uint32_t *cigar, *new_cigar;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
386 uint8_t *seq, *qual, *p;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
387 // test if removal is necessary
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
388 if (b->core.flag & BAM_FUNMAP) return 0; // unmapped; do nothing
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
389 cigar = bam1_cigar(b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
390 for (k = 0; k < b->core.n_cigar; ++k)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
391 if (bam_cigar_op(cigar[k]) == BAM_CBACK) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
392 if (k == b->core.n_cigar) return 0; // no 'B'
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
393 if (bam_cigar_op(cigar[0]) == BAM_CBACK) goto rmB_err; // cannot be removed
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
394 // allocate memory for the new CIGAR
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
395 if (b->data_len + (b->core.n_cigar + 1) * 4 > b->m_data) { // not enough memory
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
396 b->m_data = b->data_len + b->core.n_cigar * 4;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
397 kroundup32(b->m_data);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
398 b->data = (uint8_t*)realloc(b->data, b->m_data);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
399 cigar = bam1_cigar(b); // after realloc, cigar may be changed
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
400 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
401 new_cigar = (uint32_t*)(b->data + (b->m_data - b->core.n_cigar * 4)); // from the end of b->data
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
402 // the core loop
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
403 seq = bam1_seq(b); qual = bam1_qual(b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
404 no_qual = (qual[0] == 0xff); // test whether base quality is available
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
405 i = j = 0; end_j = -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
406 for (k = l = 0; k < b->core.n_cigar; ++k) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
407 int op = bam_cigar_op(cigar[k]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
408 int len = bam_cigar_oplen(cigar[k]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
409 if (op == BAM_CBACK) { // the backward operation
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
410 int t, u;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
411 if (k == b->core.n_cigar - 1) break; // ignore 'B' at the end of CIGAR
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
412 if (len > j) goto rmB_err; // an excessively long backward
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
413 for (t = l - 1, u = 0; t >= 0; --t) { // look back
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
414 int op1 = bam_cigar_op(new_cigar[t]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
415 int len1 = bam_cigar_oplen(new_cigar[t]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
416 if (bam_cigar_type(op1)&1) { // consume the query
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
417 if (u + len1 >= len) { // stop
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
418 new_cigar[t] -= (len - u) << BAM_CIGAR_SHIFT;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
419 break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
420 } else u += len1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
421 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
422 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
423 if (bam_cigar_oplen(new_cigar[t]) == 0) --t; // squeeze out the zero-length operation
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
424 l = t + 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
425 end_j = j; j -= len;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
426 } else { // other CIGAR operations
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
427 new_cigar[l++] = cigar[k];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
428 if (bam_cigar_type(op)&1) { // consume the query
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
429 if (i != j) { // no need to copy if i == j
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
430 int u, c, c0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
431 for (u = 0; u < len; ++u) { // construct the consensus
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
432 c = bam1_seqi(seq, i+u);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
433 if (j + u < end_j) { // in an overlap
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
434 c0 = bam1_seqi(seq, j+u);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
435 if (c != c0) { // a mismatch; choose the better base
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
436 if (qual[j+u] < qual[i+u]) { // the base in the 2nd segment is better
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
437 bam1_seq_seti(seq, j+u, c);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
438 qual[j+u] = qual[i+u] - qual[j+u];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
439 } else qual[j+u] -= qual[i+u]; // the 1st is better; reduce base quality
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
440 } else qual[j+u] = qual[j+u] > qual[i+u]? qual[j+u] : qual[i+u];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
441 } else { // not in an overlap; copy over
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
442 bam1_seq_seti(seq, j+u, c);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
443 qual[j+u] = qual[i+u];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
444 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
445 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
446 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
447 i += len, j += len;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
448 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
449 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
450 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
451 if (no_qual) qual[0] = 0xff; // in very rare cases, this may be modified
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
452 // merge adjacent operations if possible
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
453 for (k = 1; k < l; ++k)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
454 if (bam_cigar_op(new_cigar[k]) == bam_cigar_op(new_cigar[k-1]))
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
455 new_cigar[k] += new_cigar[k-1] >> BAM_CIGAR_SHIFT << BAM_CIGAR_SHIFT, new_cigar[k-1] &= 0xf;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
456 // kill zero length operations
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
457 for (k = i = 0; k < l; ++k)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
458 if (new_cigar[k] >> BAM_CIGAR_SHIFT)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
459 new_cigar[i++] = new_cigar[k];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
460 l = i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
461 // update b
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
462 memcpy(cigar, new_cigar, l * 4); // set CIGAR
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
463 p = b->data + b->core.l_qname + l * 4;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
464 memmove(p, seq, (j+1)>>1); p += (j+1)>>1; // set SEQ
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
465 memmove(p, qual, j); p += j; // set QUAL
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
466 memmove(p, bam1_aux(b), b->l_aux); p += b->l_aux; // set optional fields
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
467 b->core.n_cigar = l, b->core.l_qseq = j; // update CIGAR length and query length
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
468 b->data_len = p - b->data; // update record length
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
469 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
470
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
471 rmB_err:
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
472 b->core.flag |= BAM_FUNMAP;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
473 return -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
474 }