Debian Codegraph @

samtools/bam.c.pysam.c

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