Debian Codegraph @

samtools/bam.c

raw symbols SCIP
1 /* bam.c -- miscellaneous BAM functions.
2
3 Copyright (C) 2008-2013, 2015, 2019-2020, 2022 Genome Research Ltd.
4 Portions copyright (C) 2009-2012 Broad Institute.
5
6 Author: Heng Li <[email protected]>
7
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
14
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 DEALINGS IN THE SOFTWARE. */
25
26 #include <config.h>
27
28 #include <stdio.h>
29 #include <ctype.h>
30 #include <errno.h>
31 #include "bam.h"
32 #include "htslib/kstring.h"
33
34 // FIXME: we should also check the LB tag associated with each alignment
35 const char *bam_get_library(sam_hdr_t *h, const bam1_t *b)
36 {
37 const char *rg;
38 kstring_t lib = { 0, 0, NULL };
39 rg = (char *)bam_aux_get(b, "RG");
40
41 if (!rg)
42 return NULL;
43 else
44 rg++;
45
46 if (sam_hdr_find_tag_id(h, "RG", "ID", rg, "LB", &lib) < 0)
47 return NULL;
48
49 static char LB_text[1024];
50 int len = lib.l < sizeof(LB_text) - 1 ? lib.l : sizeof(LB_text) - 1;
51
52 memcpy(LB_text, lib.s, len);
53 LB_text[len] = 0;
54
55 free(lib.s);
56
57 return LB_text;
58 }
59
60 /************
61 * Remove B *
62 ************/
63
64 #define bam1_seq_seti(s, i, c) ( (s)[(i)>>1] = ((s)[(i)>>1] & 0xf<<(((i)&1)<<2)) | (c)<<((~(i)&1)<<2) )
65
66 int bam_remove_B(bam1_t *b)
67 {
68 int i, j, end_j, k, l, no_qual;
69 uint32_t *cigar, *new_cigar;
70 uint8_t *seq, *qual, *p;
71 // test if removal is necessary
72 if (b->core.flag & BAM_FUNMAP) return 0; // unmapped; do nothing
73 cigar = bam_get_cigar(b);
74 for (k = 0; k < b->core.n_cigar; ++k)
75 if (bam_cigar_op(cigar[k]) == BAM_CBACK) break;
76 if (k == b->core.n_cigar) return 0; // no 'B'
77 if (bam_cigar_op(cigar[0]) == BAM_CBACK) goto rmB_err; // cannot be removed
78 // allocate memory for the new CIGAR
79 if (b->l_data + (b->core.n_cigar + 1) * 4 > b->m_data) { // not enough memory
80 b->m_data = b->l_data + b->core.n_cigar * 4;
81 kroundup32(b->m_data);
82 b->data = (uint8_t*)realloc(b->data, b->m_data);
83 cigar = bam_get_cigar(b); // after realloc, cigar may be changed
84 }
85 new_cigar = (uint32_t*)(b->data + (b->m_data - b->core.n_cigar * 4)); // from the end of b->data
86 // the core loop
87 seq = bam_get_seq(b); qual = bam_get_qual(b);
88 no_qual = (qual[0] == 0xff); // test whether base quality is available
89 i = j = 0; end_j = -1;
90 for (k = l = 0; k < b->core.n_cigar; ++k) {
91 int op = bam_cigar_op(cigar[k]);
92 int len = bam_cigar_oplen(cigar[k]);
93 if (op == BAM_CBACK) { // the backward operation
94 int t, u;
95 if (k == b->core.n_cigar - 1) break; // ignore 'B' at the end of CIGAR
96 if (len > j) goto rmB_err; // an excessively long backward
97 for (t = l - 1, u = 0; t >= 0; --t) { // look back
98 int op1 = bam_cigar_op(new_cigar[t]);
99 int len1 = bam_cigar_oplen(new_cigar[t]);
100 if (bam_cigar_type(op1)&1) { // consume the query
101 if (u + len1 >= len) { // stop
102 new_cigar[t] -= (len - u) << BAM_CIGAR_SHIFT;
103 break;
104 } else u += len1;
105 }
106 }
107 if (bam_cigar_oplen(new_cigar[t]) == 0) --t; // squeeze out the zero-length operation
108 l = t + 1;
109 end_j = j; j -= len;
110 } else { // other CIGAR operations
111 new_cigar[l++] = cigar[k];
112 if (bam_cigar_type(op)&1) { // consume the query
113 if (i != j) { // no need to copy if i == j
114 int u, c, c0;
115 for (u = 0; u < len; ++u) { // construct the consensus
116 c = bam_seqi(seq, i+u);
117 if (j + u < end_j) { // in an overlap
118 c0 = bam_seqi(seq, j+u);
119 if (c != c0) { // a mismatch; choose the better base
120 if (qual[j+u] < qual[i+u]) { // the base in the 2nd segment is better
121 bam1_seq_seti(seq, j+u, c);
122 qual[j+u] = qual[i+u] - qual[j+u];
123 } else qual[j+u] -= qual[i+u]; // the 1st is better; reduce base quality
124 } else qual[j+u] = qual[j+u] > qual[i+u]? qual[j+u] : qual[i+u];
125 } else { // not in an overlap; copy over
126 bam1_seq_seti(seq, j+u, c);
127 qual[j+u] = qual[i+u];
128 }
129 }
130 }
131 i += len, j += len;
132 }
133 }
134 }
135 if (no_qual) qual[0] = 0xff; // in very rare cases, this may be modified
136 // merge adjacent operations if possible
137 for (k = 1; k < l; ++k)
138 if (bam_cigar_op(new_cigar[k]) == bam_cigar_op(new_cigar[k-1]))
139 new_cigar[k] += new_cigar[k-1] >> BAM_CIGAR_SHIFT << BAM_CIGAR_SHIFT, new_cigar[k-1] &= 0xf;
140 // kill zero length operations
141 for (k = i = 0; k < l; ++k)
142 if (new_cigar[k] >> BAM_CIGAR_SHIFT)
143 new_cigar[i++] = new_cigar[k];
144 l = i;
145 // update b
146 memcpy(cigar, new_cigar, l * 4); // set CIGAR
147 p = b->data + b->core.l_qname + l * 4;
148 memmove(p, seq, (j+1)>>1); p += (j+1)>>1; // set SEQ
149 memmove(p, qual, j); p += j; // set QUAL
150 memmove(p, bam_get_aux(b), bam_get_l_aux(b)); p += bam_get_l_aux(b); // set optional fields
151 b->core.n_cigar = l, b->core.l_qseq = j; // update CIGAR length and query length
152 b->l_data = p - b->data; // update record length
153 return 0;
154
155 rmB_err:
156 b->core.flag |= BAM_FUNMAP;
157 return -1;
158 }
159
160 /* Calculate the current read's start based on the stored cigar string. */
161 hts_pos_t unclipped_start(bam1_t *b) {
162 uint32_t *cigar = bam_get_cigar(b);
163 int64_t clipped = 0;
164 uint32_t i;
165
166 for (i = 0; i < b->core.n_cigar; i++) {
167 char c = bam_cigar_opchr(cigar[i]);
168
169 if (c == 'S' || c == 'H') { // clips
170 clipped += bam_cigar_oplen(cigar[i]);
171 } else {
172 break;
173 }
174 }
175
176 return b->core.pos - clipped + 1;
177 }
178
179 /* Calculate the mate's unclipped start based on position and cigar string from MC tag. */
180 hts_pos_t unclipped_other_start(hts_pos_t op, char *cigar) {
181 char *c = cigar;
182 int64_t clipped = 0;
183
184 while (*c && *c != '*') {
185 long num = 0;
186
187 if (isdigit((int)*c)) {
188 num = strtol(c, &c, 10);
189 } else {
190 num = 1;
191 }
192
193 if (*c == 'S' || *c == 'H') { // clips
194 clipped += num;
195 } else {
196 break;
197 }
198
199 c++;
200 }
201
202 return op - clipped + 1;
203 }
204
205 /* Calculate the current read's end based on the stored cigar string. */
206 hts_pos_t unclipped_end(bam1_t *b) {
207 uint32_t *cigar = bam_get_cigar(b);
208 hts_pos_t end_pos, clipped = 0;
209 int32_t i;
210
211 end_pos = bam_endpos(b);
212
213 // now get the clipped end bases (if any)
214 // if we get to the beginning of the cigar string
215 // without hitting a non-clip then the results are meaningless
216 for (i = b->core.n_cigar - 1; i >= 0; i--) {
217 char c = bam_cigar_opchr(cigar[i]);
218
219 if (c == 'S' || c == 'H') { // clips
220 clipped += bam_cigar_oplen(cigar[i]);
221 } else {
222 break;
223 }
224 }
225
226 return end_pos + clipped;
227 }
228
229
230 /* Calculate the mate's unclipped end based on start position and cigar string from MC tag.*/
231 hts_pos_t unclipped_other_end(int64_t op, char *cigar) {
232 char *c = cigar;
233 int64_t refpos = 0;
234 int skip = 1;
235
236 while (*c && *c != '*') {
237 long num = 0;
238
239 if (isdigit((int)*c)) {
240 num = strtol(c, &c, 10);
241 } else {
242 num = 1;
243 }
244
245 switch (*c) {
246 case 'M':
247 case 'D':
248 case 'N':
249 case '=':
250 case 'X':
251 refpos += num;
252 skip = 0; // ignore initial clips
253 break;
254
255 case 'S':
256 case 'H':
257 if (!skip) {
258 refpos += num;
259 }
260 break;
261 }
262
263 c++;
264 }
265
266 return op + refpos;
267 }