Line | Count | Source |
1 | | /* |
2 | | Copyright (c) 2018-2020, 2023, 2025-2026 Genome Research Ltd. |
3 | | Authors: James Bonfield <jkb@sanger.ac.uk>, Valeriu Ohan <vo2@sanger.ac.uk> |
4 | | |
5 | | Redistribution and use in source and binary forms, with or without |
6 | | modification, are permitted provided that the following conditions are met: |
7 | | |
8 | | 1. Redistributions of source code must retain the above copyright notice, |
9 | | this list of conditions and the following disclaimer. |
10 | | |
11 | | 2. Redistributions in binary form must reproduce the above copyright notice, |
12 | | this list of conditions and the following disclaimer in the documentation |
13 | | and/or other materials provided with the distribution. |
14 | | |
15 | | 3. Neither the names Genome Research Ltd and Wellcome Trust Sanger |
16 | | Institute nor the names of its contributors may be used to endorse or promote |
17 | | products derived from this software without specific prior written permission. |
18 | | |
19 | | THIS SOFTWARE IS PROVIDED BY GENOME RESEARCH LTD AND CONTRIBUTORS "AS IS" AND |
20 | | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
21 | | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
22 | | DISCLAIMED. IN NO EVENT SHALL GENOME RESEARCH LTD OR CONTRIBUTORS BE LIABLE |
23 | | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
24 | | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
25 | | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
26 | | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
27 | | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 | | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | | */ |
30 | | |
31 | | #define HTS_BUILDING_LIBRARY // Enables HTSLIB_EXPORT, see htslib/hts_defs.h |
32 | | #include <config.h> |
33 | | |
34 | | #include <string.h> |
35 | | #include <assert.h> |
36 | | #include <errno.h> |
37 | | #include "textutils_internal.h" |
38 | | #include "header.h" |
39 | | #include "htslib/bgzf.h" |
40 | | #include "htslib/hfile.h" |
41 | | #include "htslib/hts_alloc.h" |
42 | | #include "htslib/kseq.h" |
43 | | |
44 | | // Hash table for removing multiple lines from the header |
45 | | KHASH_SET_INIT_STR(rm) |
46 | | // Used for long refs in SAM files |
47 | | KHASH_DECLARE(s2i, kh_cstr_t, int64_t) |
48 | | |
49 | | typedef khash_t(rm) rmhash_t; |
50 | | |
51 | | static int sam_hdr_link_pg(sam_hdr_t *bh); |
52 | | |
53 | | static int sam_hrecs_vupdate(sam_hrecs_t *hrecs, sam_hrec_type_t *type, va_list ap); |
54 | | static int sam_hrecs_update(sam_hrecs_t *hrecs, sam_hrec_type_t *type, ...); |
55 | | |
56 | | |
57 | 704 | #define MAX_ERROR_QUOTE 320 // Prevent over-long error messages |
58 | 625 | static void sam_hrecs_error(const char *msg, const char *line, size_t len, size_t lno) { |
59 | 625 | int j; |
60 | | |
61 | 625 | if (len > MAX_ERROR_QUOTE) |
62 | 79 | len = MAX_ERROR_QUOTE; |
63 | 33.9k | for (j = 0; j < len && line[j] != '\n'; j++) |
64 | 33.3k | ; |
65 | 625 | hts_log_error("%s at line %zd: \"%.*s\"", msg, lno, j, line); |
66 | 625 | } |
67 | | |
68 | | /* ==== Static methods ==== */ |
69 | | |
70 | 55.5k | static int sam_hrecs_init_type_order(sam_hrecs_t *hrecs, char *type_list) { |
71 | 55.5k | if (!hrecs) |
72 | 0 | return -1; |
73 | | |
74 | 55.5k | if (!type_list) { |
75 | 55.5k | hrecs->type_count = 5; |
76 | 55.5k | hrecs->type_order = calloc(hrecs->type_count, 3); |
77 | 55.5k | if (!hrecs->type_order) |
78 | 0 | return -1; |
79 | 55.5k | memcpy(hrecs->type_order[0], "HD", 2); |
80 | 55.5k | memcpy(hrecs->type_order[1], "SQ", 2); |
81 | 55.5k | memcpy(hrecs->type_order[2], "RG", 2); |
82 | 55.5k | memcpy(hrecs->type_order[3], "PG", 2); |
83 | 55.5k | memcpy(hrecs->type_order[4], "CO", 2); |
84 | 55.5k | } |
85 | | |
86 | 55.5k | return 0; |
87 | 55.5k | } |
88 | | |
89 | 23.2k | static int sam_hrecs_add_ref_altnames(sam_hrecs_t *hrecs, int nref, const char *list) { |
90 | 23.2k | const char *token; |
91 | 23.2k | ks_tokaux_t aux; |
92 | | |
93 | 23.2k | if (!list) |
94 | 22.8k | return 0; |
95 | | |
96 | 2.06M | for (token = kstrtok(list, ",", &aux); token; token = kstrtok(NULL, NULL, &aux)) { |
97 | 2.06M | if (aux.p == token) |
98 | 2.01M | continue; |
99 | | |
100 | 49.7k | char *name = string_ndup(hrecs->str_pool, token, aux.p - token); |
101 | 49.7k | if (!name) |
102 | 0 | return -1; |
103 | 49.7k | int r; |
104 | 49.7k | khint_t k = kh_put(m_s2i, hrecs->ref_hash, name, &r); |
105 | 49.7k | if (r < 0) return -1; |
106 | | |
107 | 49.7k | if (r > 0) |
108 | 5.19k | kh_val(hrecs->ref_hash, k) = nref; |
109 | 44.5k | else if (kh_val(hrecs->ref_hash, k) != nref) |
110 | 1.08k | hts_log_warning("Duplicate entry AN:\"%s\" in sam header", name); |
111 | 49.7k | } |
112 | | |
113 | 404 | return 0; |
114 | 404 | } |
115 | | |
116 | 0 | static int sam_hrecs_remove_ref_altnames(sam_hrecs_t *hrecs, int expected, const char *list) { |
117 | 0 | const char *token, *sn; |
118 | 0 | ks_tokaux_t aux; |
119 | 0 | kstring_t str = KS_INITIALIZE; |
120 | |
|
121 | 0 | if (expected < 0 || expected >= hrecs->nref) |
122 | 0 | return 0; |
123 | 0 | sn = hrecs->ref[expected].name; |
124 | |
|
125 | 0 | for (token = kstrtok(list, ",", &aux); token; token = kstrtok(NULL, NULL, &aux)) { |
126 | 0 | if (kputsn(token, aux.p - token, ks_clear(&str)) < 0) { |
127 | 0 | free(str.s); |
128 | 0 | return -1; |
129 | 0 | } |
130 | 0 | khint_t k = kh_get(m_s2i, hrecs->ref_hash, str.s); |
131 | 0 | if (k != kh_end(hrecs->ref_hash) |
132 | 0 | && kh_val(hrecs->ref_hash, k) == expected |
133 | 0 | && strcmp(sn, str.s) != 0) |
134 | 0 | kh_del(m_s2i, hrecs->ref_hash, k); |
135 | 0 | } |
136 | | |
137 | 0 | free(str.s); |
138 | 0 | return 0; |
139 | 0 | } |
140 | | |
141 | | /* Updates the hash tables in the sam_hrecs_t structure. |
142 | | * |
143 | | * Returns 0 on success; |
144 | | * -1 on failure |
145 | | */ |
146 | | static int sam_hrecs_update_hashes(sam_hrecs_t *hrecs, |
147 | | khint32_t type, |
148 | 225k | sam_hrec_type_t *h_type) { |
149 | | /* Add to reference hash? */ |
150 | 225k | if (type == TYPEKEY("SQ")) { |
151 | 23.9k | sam_hrec_tag_t *tag = h_type->tag; |
152 | 23.9k | int nref = hrecs->nref; |
153 | 23.9k | const char *name = NULL; |
154 | 23.9k | const char *altnames = NULL; |
155 | 23.9k | hts_pos_t len = -1; |
156 | 23.9k | int r, invLN = 0; |
157 | 23.9k | khint_t k; |
158 | | |
159 | 302k | while (tag) { |
160 | 278k | if (tag->str[0] == 'S' && tag->str[1] == 'N') { |
161 | 90.1k | assert(tag->len >= 3); |
162 | 90.1k | name = tag->str+3; |
163 | 188k | } else if (tag->str[0] == 'L' && tag->str[1] == 'N') { |
164 | 89.9k | assert(tag->len >= 3); |
165 | 89.9k | hts_pos_t tmp = len; |
166 | 89.9k | len = strtoll(tag->str+3, NULL, 10); |
167 | 89.9k | if (tmp != -1 && tmp != len) { //duplicate and different LN |
168 | 814 | invLN = 1; |
169 | 814 | } |
170 | 98.7k | } else if (tag->str[0] == 'A' && tag->str[1] == 'N') { |
171 | 65.5k | assert(tag->len >= 3); |
172 | 65.5k | altnames = tag->str+3; |
173 | 65.5k | } |
174 | 278k | tag = tag->next; |
175 | 278k | } |
176 | | |
177 | 23.9k | if (!name) { |
178 | 167 | hts_log_error("Header includes @SQ line with no SN: tag"); |
179 | 167 | return -1; // SN should be present, according to spec. |
180 | 167 | } |
181 | | |
182 | 23.7k | if (len <= 0) { |
183 | 390 | hts_log_error("Header includes @SQ line \"%s\" with invalid or no LN: tag", |
184 | 390 | name); |
185 | 390 | return -1; // LN should be present, according to spec. |
186 | 390 | } |
187 | | |
188 | 23.4k | if (invLN) { |
189 | 126 | hts_log_error("Header includes @SQ line \"%s\" with multiple LN:" |
190 | 126 | " tag with different values.", name); |
191 | 126 | return -1; // LN should not be duplicated or be same |
192 | 126 | } |
193 | | |
194 | | // Seen already? |
195 | 23.2k | k = kh_get(m_s2i, hrecs->ref_hash, name); |
196 | 23.2k | if (k < kh_end(hrecs->ref_hash)) { |
197 | 7.79k | nref = kh_val(hrecs->ref_hash, k); |
198 | 7.79k | int ref_changed_flag = 0; |
199 | | |
200 | | // Check for hash entry added by sam_hrecs_refs_from_targets_array() |
201 | 7.79k | if (hrecs->ref[nref].ty == NULL) { |
202 | | // Attach header line to existing stub entry. |
203 | 7.74k | hrecs->ref[nref].ty = h_type; |
204 | | // Check lengths match; correct if not. |
205 | 7.74k | if (len != hrecs->ref[nref].len) { |
206 | 4 | char tmp[32]; |
207 | 4 | snprintf(tmp, sizeof(tmp), "%" PRIhts_pos, |
208 | 4 | hrecs->ref[nref].len); |
209 | 4 | if (sam_hrecs_update(hrecs, h_type, "LN", tmp, NULL) < 0) |
210 | 0 | return -1; |
211 | 4 | ref_changed_flag = 1; |
212 | 4 | } |
213 | 7.74k | if (sam_hrecs_add_ref_altnames(hrecs, nref, altnames) < 0) |
214 | 0 | return -1; |
215 | | |
216 | 7.74k | if (ref_changed_flag && (hrecs->refs_changed < 0 || hrecs->refs_changed > nref)) |
217 | 4 | hrecs->refs_changed = nref; |
218 | 7.74k | return 0; |
219 | 7.74k | } |
220 | | |
221 | | // Check to see if an existing entry is being updated |
222 | 49 | if (hrecs->ref[nref].ty == h_type) { |
223 | 0 | if (hrecs->ref[nref].len != len) { |
224 | 0 | hrecs->ref[nref].len = len; |
225 | 0 | ref_changed_flag = 1; |
226 | 0 | } |
227 | 0 | if (!hrecs->ref[nref].name || strcmp(hrecs->ref[nref].name, name)) { |
228 | 0 | hrecs->ref[nref].name = name; |
229 | 0 | ref_changed_flag = 1; |
230 | 0 | } |
231 | 0 | if (sam_hrecs_add_ref_altnames(hrecs, nref, altnames) < 0) |
232 | 0 | return -1; |
233 | | |
234 | 0 | if (ref_changed_flag && (hrecs->refs_changed < 0 || hrecs->refs_changed > nref)) |
235 | 0 | hrecs->refs_changed = nref; |
236 | 0 | return 0; |
237 | 0 | } |
238 | | |
239 | | // If here, the name is a duplicate. |
240 | | // Check to see if it matches the SN: tag from the earlier record. |
241 | 49 | if (strcmp(hrecs->ref[nref].name, name) == 0) { |
242 | 29 | hts_log_error("Duplicate entry \"%s\" in sam header", |
243 | 29 | name); |
244 | 29 | return -1; |
245 | 29 | } |
246 | | |
247 | | // Clash with an already-seen altname |
248 | | // As SN: should be preferred to AN: add this as a new |
249 | | // record and update the hash entry to point to it. |
250 | 20 | hts_log_warning("Ref name SN:\"%s\" is a duplicate of an existing AN key", name); |
251 | 20 | nref = hrecs->nref; |
252 | 20 | } |
253 | | |
254 | 15.5k | if (nref == hrecs->ref_sz) { |
255 | 45 | size_t new_sz = hrecs->ref_sz >= 4 ? hrecs->ref_sz + (hrecs->ref_sz / 4) : 32; |
256 | 45 | sam_hrec_sq_t *new_ref = hts_realloc_p(hrecs->ref, sizeof(*hrecs->ref), new_sz); |
257 | 45 | if (!new_ref) |
258 | 0 | return -1; |
259 | 45 | hrecs->ref = new_ref; |
260 | 45 | hrecs->ref_sz = new_sz; |
261 | 45 | } |
262 | | |
263 | 15.5k | hrecs->ref[nref].name = name; |
264 | 15.5k | hrecs->ref[nref].len = len; |
265 | 15.5k | hrecs->ref[nref].ty = h_type; |
266 | | |
267 | 15.5k | k = kh_put(m_s2i, hrecs->ref_hash, hrecs->ref[nref].name, &r); |
268 | 15.5k | if (-1 == r) return -1; |
269 | 15.5k | kh_val(hrecs->ref_hash, k) = nref; |
270 | | |
271 | 15.5k | if (sam_hrecs_add_ref_altnames(hrecs, nref, altnames) < 0) |
272 | 0 | return -1; |
273 | | |
274 | 15.5k | if (hrecs->refs_changed < 0 || hrecs->refs_changed > hrecs->nref) |
275 | 11.7k | hrecs->refs_changed = hrecs->nref; |
276 | 15.5k | hrecs->nref++; |
277 | 15.5k | } |
278 | | |
279 | | /* Add to read-group hash? */ |
280 | 217k | if (type == TYPEKEY("RG")) { |
281 | 166k | sam_hrec_tag_t *tag = sam_hrecs_find_key(h_type, "ID", NULL); |
282 | 166k | int nrg = hrecs->nrg, r; |
283 | 166k | khint_t k; |
284 | | |
285 | 166k | if (!tag) { |
286 | 122 | hts_log_error("Header includes @RG line with no ID: tag"); |
287 | 122 | return -1; // ID should be present, according to spec. |
288 | 122 | } |
289 | 166k | assert(tag->str && tag->len >= 3); |
290 | | |
291 | | // Seen already? |
292 | 166k | k = kh_get(m_s2i, hrecs->rg_hash, tag->str + 3); |
293 | 166k | if (k < kh_end(hrecs->rg_hash)) { |
294 | 160k | nrg = kh_val(hrecs->rg_hash, k); |
295 | 160k | assert(hrecs->rg[nrg].ty != NULL); |
296 | 160k | if (hrecs->rg[nrg].ty != h_type) { |
297 | 160k | hts_log_warning("Duplicate entry \"%s\" in sam header", |
298 | 160k | tag->str + 3); |
299 | 160k | } else { |
300 | 0 | hrecs->rg[nrg].name = tag->str + 3; |
301 | 0 | hrecs->rg[nrg].name_len = tag->len - 3; |
302 | 0 | } |
303 | 160k | return 0; |
304 | 160k | } |
305 | | |
306 | 6.09k | if (nrg == hrecs->rg_sz) { |
307 | 3.15k | size_t new_sz = hrecs->rg_sz >= 4 ? hrecs->rg_sz + hrecs->rg_sz / 4 : 4; |
308 | 3.15k | sam_hrec_rg_t *new_rg = hts_realloc_p(hrecs->rg, sizeof(*hrecs->rg), new_sz); |
309 | 3.15k | if (!new_rg) |
310 | 0 | return -1; |
311 | 3.15k | hrecs->rg = new_rg; |
312 | 3.15k | hrecs->rg_sz = new_sz; |
313 | 3.15k | } |
314 | | |
315 | 6.09k | hrecs->rg[nrg].name = tag->str + 3; |
316 | 6.09k | hrecs->rg[nrg].name_len = tag->len - 3; |
317 | 6.09k | hrecs->rg[nrg].ty = h_type; |
318 | 6.09k | hrecs->rg[nrg].id = nrg; |
319 | | |
320 | 6.09k | k = kh_put(m_s2i, hrecs->rg_hash, hrecs->rg[nrg].name, &r); |
321 | 6.09k | if (-1 == r) return -1; |
322 | 6.09k | kh_val(hrecs->rg_hash, k) = nrg; |
323 | | |
324 | 6.09k | hrecs->nrg++; |
325 | 6.09k | } |
326 | | |
327 | | /* Add to program hash? */ |
328 | 56.7k | if (type == TYPEKEY("PG")) { |
329 | 24.3k | sam_hrec_tag_t *tag; |
330 | 24.3k | sam_hrec_pg_t *new_pg; |
331 | 24.3k | int npg = hrecs->npg; |
332 | | |
333 | 24.3k | if (npg == hrecs->pg_sz) { |
334 | 4.79k | size_t new_sz = hrecs->pg_sz >= 4 ? hrecs->pg_sz + hrecs->pg_sz / 4 : 4; |
335 | 4.79k | new_pg = hts_realloc_p(hrecs->pg, sizeof(*hrecs->pg), new_sz); |
336 | 4.79k | if (!new_pg) |
337 | 0 | return -1; |
338 | 4.79k | hrecs->pg = new_pg; |
339 | 4.79k | hrecs->pg_sz = new_sz; |
340 | 4.79k | } |
341 | | |
342 | 24.3k | tag = h_type->tag; |
343 | 24.3k | hrecs->pg[npg].name = NULL; |
344 | 24.3k | hrecs->pg[npg].name_len = 0; |
345 | 24.3k | hrecs->pg[npg].ty = h_type; |
346 | 24.3k | hrecs->pg[npg].id = npg; |
347 | 24.3k | hrecs->pg[npg].prev_id = -1; |
348 | | |
349 | 72.1k | while (tag) { |
350 | 47.8k | if (tag->str[0] == 'I' && tag->str[1] == 'D') { |
351 | | /* Avoid duplicate ID tags coming from other applications */ |
352 | 32.6k | if (!hrecs->pg[npg].name) { |
353 | 24.1k | assert(tag->len >= 3); |
354 | 24.1k | hrecs->pg[npg].name = tag->str + 3; |
355 | 24.1k | hrecs->pg[npg].name_len = tag->len - 3; |
356 | 24.1k | } else { |
357 | 8.42k | hts_log_warning("PG line with multiple ID tags. The first encountered was preferred - ID:%s", hrecs->pg[npg].name); |
358 | 8.42k | } |
359 | 32.6k | } else if (tag->str[0] == 'P' && tag->str[1] == 'P') { |
360 | | // Resolve later if needed |
361 | 6.48k | khint_t k; |
362 | 6.48k | k = kh_get(m_s2i, hrecs->pg_hash, tag->str+3); |
363 | | |
364 | 6.48k | if (k != kh_end(hrecs->pg_hash)) { |
365 | 4.88k | int p_id = kh_val(hrecs->pg_hash, k); |
366 | 4.88k | hrecs->pg[npg].prev_id = hrecs->pg[p_id].id; |
367 | | |
368 | | /* Unmark previous entry as a PG termination */ |
369 | 4.88k | if (hrecs->npg_end > 0 && |
370 | 4.67k | hrecs->pg_end[hrecs->npg_end-1] == p_id) { |
371 | 495 | hrecs->npg_end--; |
372 | 4.39k | } else { |
373 | 4.39k | int i; |
374 | 517k | for (i = 0; i < hrecs->npg_end; i++) { |
375 | 512k | if (hrecs->pg_end[i] == p_id) { |
376 | 1.62k | memmove(&hrecs->pg_end[i], &hrecs->pg_end[i+1], |
377 | 1.62k | (hrecs->npg_end-i-1)*sizeof(*hrecs->pg_end)); |
378 | 1.62k | hrecs->npg_end--; |
379 | 1.62k | } |
380 | 512k | } |
381 | 4.39k | } |
382 | 4.88k | } else { |
383 | 1.59k | hrecs->pg[npg].prev_id = -1; |
384 | 1.59k | } |
385 | 6.48k | } |
386 | 47.8k | tag = tag->next; |
387 | 47.8k | } |
388 | | |
389 | 24.3k | if (hrecs->pg[npg].name) { |
390 | 24.1k | khint_t k; |
391 | 24.1k | int r; |
392 | 24.1k | k = kh_put(m_s2i, hrecs->pg_hash, hrecs->pg[npg].name, &r); |
393 | 24.1k | if (-1 == r) return -1; |
394 | 24.1k | kh_val(hrecs->pg_hash, k) = npg; |
395 | 24.1k | } else { |
396 | 154 | return -1; // ID should be present, according to spec. |
397 | 154 | } |
398 | | |
399 | | /* Add to npg_end[] array. Remove later if we find a PP line */ |
400 | 24.1k | if (hrecs->npg_end >= hrecs->npg_end_alloc) { |
401 | 2.13k | int *new_pg_end; |
402 | 2.13k | int new_alloc = hrecs->npg_end_alloc ? hrecs->npg_end_alloc*2 : 4; |
403 | | |
404 | 2.13k | new_pg_end = hts_realloc_p(hrecs->pg_end, sizeof(int), new_alloc); |
405 | 2.13k | if (!new_pg_end) |
406 | 0 | return -1; |
407 | 2.13k | hrecs->npg_end_alloc = new_alloc; |
408 | 2.13k | hrecs->pg_end = new_pg_end; |
409 | 2.13k | } |
410 | 24.1k | hrecs->pg_end[hrecs->npg_end++] = npg; |
411 | | |
412 | 24.1k | hrecs->npg++; |
413 | 24.1k | } |
414 | | |
415 | 56.5k | return 0; |
416 | 56.7k | } |
417 | | |
418 | 0 | static int sam_hrecs_remove_hash_entry(sam_hrecs_t *hrecs, khint32_t type, sam_hrec_type_t *h_type) { |
419 | 0 | if (!hrecs || !h_type) |
420 | 0 | return -1; |
421 | | |
422 | 0 | sam_hrec_tag_t *tag; |
423 | 0 | const char *key = NULL; |
424 | 0 | khint_t k; |
425 | | |
426 | | /* Remove name and any alternative names from reference hash */ |
427 | 0 | if (type == TYPEKEY("SQ")) { |
428 | 0 | const char *altnames = NULL; |
429 | |
|
430 | 0 | tag = h_type->tag; |
431 | |
|
432 | 0 | while (tag) { |
433 | 0 | if (tag->str[0] == 'S' && tag->str[1] == 'N') { |
434 | 0 | assert(tag->len >= 3); |
435 | 0 | key = tag->str + 3; |
436 | 0 | } else if (tag->str[0] == 'A' && tag->str[1] == 'N') { |
437 | 0 | assert(tag->len >= 3); |
438 | 0 | altnames = tag->str + 3; |
439 | 0 | } |
440 | 0 | tag = tag->next; |
441 | 0 | } |
442 | |
|
443 | 0 | if (key) { |
444 | 0 | k = kh_get(m_s2i, hrecs->ref_hash, key); |
445 | 0 | if (k != kh_end(hrecs->ref_hash)) { |
446 | 0 | int idx = kh_val(hrecs->ref_hash, k); |
447 | 0 | if (altnames) { |
448 | 0 | if (sam_hrecs_remove_ref_altnames(hrecs, idx, altnames) < 0) |
449 | 0 | return -1; |
450 | 0 | } |
451 | 0 | if (idx + 1 < hrecs->nref) |
452 | 0 | memmove(&hrecs->ref[idx], &hrecs->ref[idx+1], |
453 | 0 | sizeof(sam_hrec_sq_t)*(hrecs->nref - idx - 1)); |
454 | 0 | kh_del(m_s2i, hrecs->ref_hash, k); |
455 | 0 | hrecs->nref--; |
456 | 0 | if (hrecs->refs_changed < 0 || hrecs->refs_changed > idx) |
457 | 0 | hrecs->refs_changed = idx; |
458 | 0 | for (k = 0; k < kh_end(hrecs->ref_hash); k++) { |
459 | 0 | if (kh_exist(hrecs->ref_hash, k) |
460 | 0 | && kh_value(hrecs->ref_hash, k) > idx) { |
461 | 0 | kh_value(hrecs->ref_hash, k)--; |
462 | 0 | } |
463 | 0 | } |
464 | 0 | } |
465 | 0 | } |
466 | 0 | } |
467 | | |
468 | | /* Remove from read-group hash */ |
469 | 0 | if (type == TYPEKEY("RG")) { |
470 | 0 | tag = h_type->tag; |
471 | |
|
472 | 0 | while (tag) { |
473 | 0 | if (tag->str[0] == 'I' && tag->str[1] == 'D') { |
474 | 0 | assert(tag->len >= 3); |
475 | 0 | key = tag->str + 3; |
476 | 0 | k = kh_get(m_s2i, hrecs->rg_hash, key); |
477 | 0 | if (k != kh_end(hrecs->rg_hash)) { |
478 | 0 | int idx = kh_val(hrecs->rg_hash, k); |
479 | 0 | if (idx + 1 < hrecs->nrg) |
480 | 0 | memmove(&hrecs->rg[idx], &hrecs->rg[idx+1], sizeof(sam_hrec_rg_t)*(hrecs->nrg - idx - 1)); |
481 | 0 | kh_del(m_s2i, hrecs->rg_hash, k); |
482 | 0 | hrecs->nrg--; |
483 | 0 | for (k = 0; k < kh_end(hrecs->rg_hash); k++) { |
484 | 0 | if (kh_exist(hrecs->rg_hash, k) |
485 | 0 | && kh_value(hrecs->rg_hash, k) > idx) { |
486 | 0 | kh_value(hrecs->rg_hash, k)--; |
487 | 0 | } |
488 | 0 | } |
489 | 0 | } |
490 | 0 | break; |
491 | 0 | } |
492 | 0 | tag = tag->next; |
493 | 0 | } |
494 | 0 | } |
495 | | |
496 | 0 | return 0; |
497 | 0 | } |
498 | | |
499 | | /** Add a header record to the global line ordering |
500 | | * |
501 | | * If @p after is not NULL, the new record will be inserted after this one, |
502 | | * otherwise it will go at the end. |
503 | | * |
504 | | * An exception is an HD record, which will always be put first unless |
505 | | * one is already present. |
506 | | */ |
507 | | static void sam_hrecs_global_list_add(sam_hrecs_t *hrecs, |
508 | | sam_hrec_type_t *h_type, |
509 | 1.89M | sam_hrec_type_t *after) { |
510 | 1.89M | const khint32_t hd_type = TYPEKEY("HD"); |
511 | 1.89M | int update_first_line = 0; |
512 | | |
513 | | // First line seen |
514 | 1.89M | if (!hrecs->first_line) { |
515 | 26.2k | hrecs->first_line = h_type->global_next = h_type->global_prev = h_type; |
516 | 26.2k | return; |
517 | 26.2k | } |
518 | | |
519 | | // @HD goes at the top (unless there's one already) |
520 | 1.86M | if (h_type->type == hd_type && hrecs->first_line->type != hd_type) { |
521 | 37 | after = hrecs->first_line->global_prev; |
522 | 37 | update_first_line = 1; |
523 | 37 | } |
524 | | |
525 | | // If no instructions given, put it at the end |
526 | 1.86M | if (!after) |
527 | 1.86M | after = hrecs->first_line->global_prev; |
528 | | |
529 | 1.86M | h_type->global_prev = after; |
530 | 1.86M | h_type->global_next = after->global_next; |
531 | 1.86M | h_type->global_prev->global_next = h_type; |
532 | 1.86M | h_type->global_next->global_prev = h_type; |
533 | | |
534 | 1.86M | if (update_first_line) |
535 | 37 | hrecs->first_line = h_type; |
536 | 1.86M | } |
537 | | |
538 | | /*! Add header record with a va_list interface. |
539 | | * |
540 | | * Adds a single record to a SAM header. |
541 | | * |
542 | | * This takes a header record type, a va_list argument and one or more |
543 | | * key,value pairs, ending with the NULL key. |
544 | | * |
545 | | * Eg. sam_hrecs_vadd(h, "SQ", args, "ID", "foo", "LN", "100", NULL). |
546 | | * |
547 | | * The purpose of the additional va_list parameter is to permit other |
548 | | * varargs functions to call this while including their own additional |
549 | | * parameters; an example is in sam_hdr_add_pg(). |
550 | | * |
551 | | * Note: this function invokes va_arg at least once, making the value |
552 | | * of ap indeterminate after the return. The caller should call |
553 | | * va_start/va_end before/after calling this function or use va_copy. |
554 | | * |
555 | | * @return |
556 | | * Returns >= 0 on success; |
557 | | * -1 on failure |
558 | | */ |
559 | 1.38k | static int sam_hrecs_vadd(sam_hrecs_t *hrecs, const char *type, va_list ap, ...) { |
560 | 1.38k | va_list args; |
561 | 1.38k | sam_hrec_type_t *h_type; |
562 | 1.38k | sam_hrec_tag_t *h_tag, *last=NULL; |
563 | 1.38k | int new; |
564 | 1.38k | khint32_t type_i = TYPEKEY(type), k; |
565 | | |
566 | 1.38k | if (!strncmp(type, "HD", 2) && (h_type = sam_hrecs_find_type_id(hrecs, "HD", NULL, NULL))) |
567 | 0 | return sam_hrecs_vupdate(hrecs, h_type, ap); |
568 | | |
569 | 1.38k | if (!(h_type = pool_alloc(hrecs->type_pool))) |
570 | 0 | return -1; |
571 | 1.38k | k = kh_put(sam_hrecs_t, hrecs->h, type_i, &new); |
572 | 1.38k | if (new < 0) |
573 | 0 | return -1; |
574 | | |
575 | 1.38k | h_type->type = type_i; |
576 | | |
577 | | // Form the ring, either with self or other lines of this type |
578 | 1.38k | if (!new) { |
579 | 229 | sam_hrec_type_t *t = kh_val(hrecs->h, k), *p; |
580 | 229 | p = t->prev; |
581 | | |
582 | 229 | assert(p->next == t); |
583 | 229 | p->next = h_type; |
584 | 229 | h_type->prev = p; |
585 | | |
586 | 229 | t->prev = h_type; |
587 | 229 | h_type->next = t; |
588 | 1.15k | } else { |
589 | 1.15k | kh_val(hrecs->h, k) = h_type; |
590 | 1.15k | h_type->prev = h_type->next = h_type; |
591 | 1.15k | } |
592 | 1.38k | h_type->tag = NULL; |
593 | | |
594 | | // Add to global line ordering after any existing line of the same type, |
595 | | // or at the end if no line of this type exists yet. |
596 | 1.38k | sam_hrecs_global_list_add(hrecs, h_type, !new ? h_type->prev : NULL); |
597 | | |
598 | | // Check linked-list invariants |
599 | 1.38k | assert(h_type->prev->next == h_type); |
600 | 1.38k | assert(h_type->next->prev == h_type); |
601 | 1.38k | assert(h_type->global_prev->global_next == h_type); |
602 | 1.38k | assert(h_type->global_next->global_prev == h_type); |
603 | | |
604 | | // Any ... varargs |
605 | 1.38k | va_start(args, ap); |
606 | 1.38k | for (;;) { |
607 | 1.38k | char *key, *val = NULL, *str; |
608 | | |
609 | 1.38k | if (!(key = (char *)va_arg(args, char *))) |
610 | 1.38k | break; |
611 | 0 | if (strncmp(type, "CO", 2) && !(val = (char *)va_arg(args, char *))) |
612 | 0 | break; |
613 | 0 | if (!val || *val == '\0') |
614 | 0 | continue; |
615 | | |
616 | 0 | if (!(h_tag = pool_alloc(hrecs->tag_pool))) |
617 | 0 | return -1; |
618 | | |
619 | 0 | if (strncmp(type, "CO", 2)) { |
620 | 0 | h_tag->len = 3 + strlen(val); |
621 | 0 | str = string_alloc(hrecs->str_pool, hts_add_sat2(h_tag->len, 1)); |
622 | 0 | if (!str || snprintf(str, h_tag->len+1, "%2.2s:%s", key, val) < 0) |
623 | 0 | return -1; |
624 | 0 | h_tag->str = str; |
625 | 0 | } else { |
626 | 0 | h_tag->len = strlen(key); |
627 | 0 | h_tag->str = string_ndup(hrecs->str_pool, key, h_tag->len); |
628 | 0 | if (!h_tag->str) |
629 | 0 | return -1; |
630 | 0 | } |
631 | | |
632 | 0 | h_tag->next = NULL; |
633 | 0 | if (last) |
634 | 0 | last->next = h_tag; |
635 | 0 | else |
636 | 0 | h_type->tag = h_tag; |
637 | |
|
638 | 0 | last = h_tag; |
639 | 0 | } |
640 | 1.38k | va_end(args); |
641 | | |
642 | | // Plus the specified va_list params |
643 | 4.15k | for (;;) { |
644 | 4.15k | char *key, *val = NULL, *str; |
645 | | |
646 | 4.15k | if (!(key = (char *)va_arg(ap, char *))) |
647 | 1.38k | break; |
648 | 2.76k | if (strncmp(type, "CO", 2) && !(val = (char *)va_arg(ap, char *))) |
649 | 0 | break; |
650 | | |
651 | 2.76k | if (!(h_tag = pool_alloc(hrecs->tag_pool))) |
652 | 0 | return -1; |
653 | | |
654 | 2.76k | if (strncmp(type, "CO", 2)) { |
655 | 2.76k | h_tag->len = 3 + strlen(val); |
656 | 2.76k | str = string_alloc(hrecs->str_pool, hts_add_sat2(h_tag->len, 1)); |
657 | 2.76k | if (!str || snprintf(str, h_tag->len+1, "%2.2s:%s", key, val) < 0) |
658 | 0 | return -1; |
659 | 2.76k | h_tag->str = str; |
660 | 2.76k | } else { |
661 | 0 | h_tag->len = strlen(key); |
662 | 0 | h_tag->str = string_ndup(hrecs->str_pool, key, h_tag->len); |
663 | 0 | if (!h_tag->str) |
664 | 0 | return -1; |
665 | 0 | } |
666 | | |
667 | 2.76k | h_tag->next = NULL; |
668 | 2.76k | if (last) |
669 | 1.38k | last->next = h_tag; |
670 | 1.38k | else |
671 | 1.38k | h_type->tag = h_tag; |
672 | | |
673 | 2.76k | last = h_tag; |
674 | 2.76k | } |
675 | | |
676 | 1.38k | if (-1 == sam_hrecs_update_hashes(hrecs, TYPEKEY(type), h_type)) |
677 | 60 | return -1; |
678 | | |
679 | 1.32k | if (!strncmp(type, "PG", 2)) |
680 | 0 | hrecs->pgs_changed = 1; |
681 | | |
682 | 1.32k | hrecs->dirty = 1; |
683 | | |
684 | 1.32k | return 0; |
685 | 1.38k | } |
686 | | |
687 | | // As sam_hrecs_vadd(), but without the extra va_list parameter |
688 | 1.38k | static int sam_hrecs_add(sam_hrecs_t *hrecs, const char *type, ...) { |
689 | 1.38k | va_list args; |
690 | 1.38k | int res; |
691 | 1.38k | va_start(args, type); |
692 | 1.38k | res = sam_hrecs_vadd(hrecs, type, args, NULL); |
693 | 1.38k | va_end(args); |
694 | 1.38k | return res; |
695 | 1.38k | } |
696 | | |
697 | | /* |
698 | | * Function for deallocating a list of tags |
699 | | */ |
700 | | |
701 | 403 | static void sam_hrecs_free_tags(sam_hrecs_t *hrecs, sam_hrec_tag_t *tag) { |
702 | 403 | if (!hrecs || !tag) |
703 | 242 | return; |
704 | | |
705 | 1.31M | while (tag) { |
706 | 1.31M | sam_hrec_tag_t *next = tag->next; |
707 | 1.31M | pool_free(hrecs->tag_pool, tag); |
708 | 1.31M | tag = next; |
709 | 1.31M | } |
710 | 161 | } |
711 | | |
712 | 0 | static int sam_hrecs_remove_line(sam_hrecs_t *hrecs, const char *type_name, sam_hrec_type_t *type_found, int remove_hash) { |
713 | 0 | if (!hrecs || !type_name || !type_found) |
714 | 0 | return -1; |
715 | | |
716 | 0 | khint32_t itype = TYPEKEY(type_name); |
717 | 0 | khint_t k = kh_get(sam_hrecs_t, hrecs->h, itype); |
718 | 0 | if (k == kh_end(hrecs->h)) |
719 | 0 | return -1; |
720 | | |
721 | | // Remove from global list (remembering it could be the only line) |
722 | 0 | if (hrecs->first_line == type_found) { |
723 | 0 | hrecs->first_line = (type_found->global_next != type_found |
724 | 0 | ? type_found->global_next : NULL); |
725 | 0 | } |
726 | 0 | type_found->global_next->global_prev = type_found->global_prev; |
727 | 0 | type_found->global_prev->global_next = type_found->global_next; |
728 | | |
729 | | /* single element in the list */ |
730 | 0 | if (type_found->prev == type_found || type_found->next == type_found) { |
731 | 0 | kh_del(sam_hrecs_t, hrecs->h, k); |
732 | 0 | } else { |
733 | 0 | type_found->prev->next = type_found->next; |
734 | 0 | type_found->next->prev = type_found->prev; |
735 | 0 | if (kh_val(hrecs->h, k) == type_found) { //first element |
736 | 0 | kh_val(hrecs->h, k) = type_found->next; |
737 | 0 | } |
738 | 0 | } |
739 | |
|
740 | 0 | if (remove_hash && (!strncmp(type_name, "SQ", 2) || !strncmp(type_name, "RG", 2))) |
741 | 0 | sam_hrecs_remove_hash_entry(hrecs, itype, type_found); |
742 | |
|
743 | 0 | sam_hrecs_free_tags(hrecs, type_found->tag); |
744 | 0 | pool_free(hrecs->type_pool, type_found); |
745 | |
|
746 | 0 | hrecs->dirty = 1; |
747 | |
|
748 | 0 | return 0; |
749 | 0 | } |
750 | | |
751 | | // Paste together a line from the parsed data structures |
752 | 327k | static int build_header_line(const sam_hrec_type_t *ty, kstring_t *ks) { |
753 | 327k | sam_hrec_tag_t *tag; |
754 | 327k | int r = 0; |
755 | 327k | char c[2]= { ty->type >> 8, ty->type & 0xff }; |
756 | | |
757 | 327k | r |= (kputc_('@', ks) == EOF); |
758 | 327k | r |= (kputsn(c, 2, ks) == EOF); |
759 | 844k | for (tag = ty->tag; tag; tag = tag->next) { |
760 | 516k | r |= (kputc_('\t', ks) == EOF); |
761 | 516k | r |= (kputsn(tag->str, tag->len, ks) == EOF); |
762 | 516k | } |
763 | | |
764 | 327k | return r; |
765 | 327k | } |
766 | | |
767 | 37.2k | static int sam_hrecs_rebuild_lines(const sam_hrecs_t *hrecs, kstring_t *ks) { |
768 | 37.2k | const sam_hrec_type_t *t1, *t2; |
769 | | |
770 | 37.2k | if (!hrecs->first_line) |
771 | 0 | return kputsn("", 0, ks) >= 0 ? 0 : -1; |
772 | | |
773 | 37.2k | t1 = t2 = hrecs->first_line; |
774 | 327k | do { |
775 | 327k | if (build_header_line(t1, ks) != 0) |
776 | 0 | return -1; |
777 | 327k | if (kputc('\n', ks) < 0) |
778 | 0 | return -1; |
779 | | |
780 | 327k | t1 = t1->global_next; |
781 | 327k | } while (t1 != t2); |
782 | | |
783 | 37.2k | return 0; |
784 | 37.2k | } |
785 | | |
786 | | // Print warnings about known log messages that may have been inadvertently |
787 | | // merged into SAM files. |
788 | | |
789 | 15 | static void known_stderr(const char *tool, const char *advice) { |
790 | 15 | hts_log_warning("SAM file corrupted by embedded %s error/log message", tool); |
791 | 15 | hts_log_warning("%s", advice); |
792 | 15 | } |
793 | | |
794 | | // Look for known log messages that may have been inadvertently merged into SAM |
795 | | // files, and print advice on how to avoid the problem if one is spotted. |
796 | | |
797 | 463 | static void warn_if_known_stderr(const char *line, size_t len) { |
798 | 463 | int ilen = len < INT_MAX ? len : INT_MAX; |
799 | 463 | if (kmemmem(line, ilen, "M::bwa_idx_load_from_disk", 25, NULL) != NULL) |
800 | 4 | known_stderr("bwa", "Use `bwa mem -o file.sam ...` or `bwa sampe -f file.sam ...` instead of `bwa ... > file.sam`"); |
801 | 459 | else if (kmemmem(line, ilen, "M::mem_pestat", 13, NULL) != NULL) |
802 | 7 | known_stderr("bwa", "Use `bwa mem -o file.sam ...` instead of `bwa mem ... > file.sam`"); |
803 | 452 | else if (kmemmem(line, ilen, "loaded/built the index", 22, NULL) != NULL) |
804 | 4 | known_stderr("minimap2", "Use `minimap2 -o file.sam ...` instead of `minimap2 ... > file.sam`"); |
805 | 463 | } |
806 | | |
807 | | // Parse a CO line |
808 | | // Returns 0 on failure; line length (>= 1) on success |
809 | | static size_t parse_comment_line(const char *hdr, size_t len, size_t lno, |
810 | 11.3k | sam_hrecs_t *hrecs, sam_hrec_type_t *h_type) { |
811 | 11.3k | size_t j; |
812 | 11.3k | sam_hrec_tag_t *h_tag; |
813 | | |
814 | 11.3k | if (len == 0 || hdr[0] != '\t') { |
815 | 48 | sam_hrecs_error("Missing tab", hdr, len, lno); |
816 | 48 | warn_if_known_stderr(hdr, len); |
817 | 48 | return 0; |
818 | 48 | } |
819 | | |
820 | 172k | for (j = 1; j < len && hdr[j] != '\0' && hdr[j] != '\n'; j++) |
821 | 161k | ; |
822 | | |
823 | 11.2k | if (!(h_type->tag = h_tag = pool_alloc(hrecs->tag_pool))) |
824 | 0 | return -1; |
825 | 11.2k | h_tag->str = string_ndup(hrecs->str_pool, hdr + 1, j - 1); |
826 | 11.2k | h_tag->len = j - 1; |
827 | 11.2k | h_tag->next = NULL; |
828 | 11.2k | if (!h_tag->str) |
829 | 0 | return 0; |
830 | 11.2k | return j; |
831 | 11.2k | } |
832 | | |
833 | | // Parse header lines other than CO |
834 | | // Returns 0 on failure; line length (>= 1) on success |
835 | | static size_t parse_noncomment_line(const char *hdr, size_t len, size_t lno, |
836 | | sam_hrecs_t *hrecs, sam_hrec_type_t *h_type, |
837 | 1.88M | khint_t *sq_count, khint_t *an_count) { |
838 | 1.88M | size_t i = 0; |
839 | 1.88M | sam_hrec_tag_t *h_tag, *last = NULL; |
840 | 1.88M | int is_sq = h_type->type == TYPEKEY("SQ"); |
841 | | |
842 | | // Count reference name |
843 | 1.88M | if (is_sq) |
844 | 23.8k | (*sq_count)++; |
845 | | |
846 | 4.05M | do { |
847 | 4.05M | size_t j; |
848 | | |
849 | 4.05M | if (i == len || hdr[i] != '\t') { |
850 | 57 | sam_hrecs_error("Missing tab", hdr, len, lno); |
851 | 57 | warn_if_known_stderr(hdr, len); |
852 | 57 | return 0; |
853 | 57 | } |
854 | | |
855 | 4.05M | for (j = ++i; |
856 | 1.12G | j < len && hdr[j] != '\0' && hdr[j] != '\n' && hdr[j] != '\t'; |
857 | 1.11G | j++) |
858 | 1.11G | ; |
859 | | |
860 | 4.05M | if (j - i < 3 || hdr[i + 2] != ':') { |
861 | 298 | sam_hrecs_error("Malformed key:value pair", hdr, len, lno); |
862 | 298 | warn_if_known_stderr(hdr, len); |
863 | 298 | return 0; |
864 | 298 | } |
865 | | |
866 | | // Count alternate names |
867 | 4.05M | if (is_sq && hdr[i] == 'A' && hdr[i + 1] == 'N') { |
868 | 511k | size_t k; |
869 | 511k | (*an_count)++; |
870 | 17.1M | for (k = i + 2; k < j; k++) |
871 | 16.6M | (*an_count) += hdr[k] == ','; |
872 | 511k | } |
873 | | |
874 | 4.05M | if (!(h_tag = pool_alloc(hrecs->tag_pool))) |
875 | 0 | return 0; |
876 | 4.05M | h_tag->str = string_ndup(hrecs->str_pool, &hdr[i], j-i); |
877 | 4.05M | h_tag->len = j-i; |
878 | 4.05M | h_tag->next = NULL; |
879 | 4.05M | if (!h_tag->str) |
880 | 0 | return 0; |
881 | | |
882 | 4.05M | if (last) |
883 | 2.17M | last->next = h_tag; |
884 | 1.88M | else |
885 | 1.88M | h_type->tag = h_tag; |
886 | | |
887 | 4.05M | last = h_tag; |
888 | 4.05M | i = j; |
889 | 4.05M | } while (i < len && hdr[i] != '\0' && hdr[i] != '\n'); |
890 | | |
891 | 1.88M | return i; |
892 | 1.88M | } |
893 | | |
894 | | /** Parse a single header line. |
895 | | * @param hrecs Destination sam_hrecs_t struct |
896 | | * @param hdr Data to parse |
897 | | * @param len Size of @p hdr |
898 | | * @param lno Line number, for error / warning messages |
899 | | * @param[out] line_len_out Returned length of parsed line |
900 | | * @param[in,out] sq_count Count of SQ lines seen so far |
901 | | * @param[in,out] an_count Count of alternate names seen so far |
902 | | * |
903 | | * Used by sam_hrecs_parse_lines() and sam_hdr_build_from_sam_file(), |
904 | | * so has to be able to deal with lines that are not NUL terminated. |
905 | | * A single line pointed to by @ph hdr will be parsed into @p hrecs. |
906 | | * @p len gives the size of @p hdr, which may include more than one line |
907 | | * if called from sam_hrecs_parse_lines(). The length of the parsed line |
908 | | * is returned in @p line_len_out. |
909 | | * @p sq_count and @p an_count are pointers to the number of SQ lines |
910 | | * (i.e. references), and the number of alternate names. These are used |
911 | | * by the caller to set the sizes of the sam_hrecs_t::ref array and |
912 | | * sam_hrecs_t::ref_hash table once all the lines have been parsed. |
913 | | */ |
914 | | |
915 | | static sam_hrec_type_t * sam_hrecs_parse_single_line(sam_hrecs_t *hrecs, |
916 | | const char *hdr, |
917 | | size_t len, size_t lno, |
918 | | size_t *line_len_out, |
919 | | khint_t *sq_count, |
920 | 1.89M | khint_t *an_count) { |
921 | 1.89M | int new; |
922 | 1.89M | khint32_t type; |
923 | 1.89M | khint_t k; |
924 | 1.89M | sam_hrec_type_t *h_type; |
925 | 1.89M | size_t line_len = 0; |
926 | | |
927 | 1.89M | if (hdr[0] != '@') { |
928 | 40 | sam_hrecs_error("Header line does not start with '@'", |
929 | 40 | hdr, len, lno); |
930 | 40 | return NULL; |
931 | 40 | } |
932 | | |
933 | 1.89M | if (!isalpha_c(hdr[1]) || !isalpha_c(hdr[2])) { |
934 | 81 | sam_hrecs_error("Header line does not have a two character key", |
935 | 81 | hdr, len, lno); |
936 | 81 | return NULL; |
937 | 81 | } |
938 | 1.89M | type = TYPEKEY(&hdr[1]); |
939 | | |
940 | 1.89M | if (len < 3 || hdr[3] == '\n') |
941 | 61 | return NULL; |
942 | | |
943 | | // Add the header line type |
944 | 1.89M | if (!(h_type = pool_alloc(hrecs->type_pool))) |
945 | 0 | return NULL; |
946 | | |
947 | 1.89M | h_type->tag = NULL; |
948 | | |
949 | 1.89M | k = kh_put(sam_hrecs_t, hrecs->h, type, &new); |
950 | 1.89M | if (new < 0) |
951 | 0 | goto error; |
952 | | |
953 | 1.89M | h_type->type = type; |
954 | | |
955 | | // Parse the tags on this line |
956 | 1.89M | if (type == TYPEKEY("CO")) { |
957 | | |
958 | 11.3k | line_len = parse_comment_line(hdr + 3, len - 3, lno, |
959 | 11.3k | hrecs, h_type); |
960 | 11.3k | if (line_len == 0) // Failed |
961 | 48 | goto error; |
962 | | |
963 | 1.88M | } else { |
964 | | |
965 | 1.88M | line_len = parse_noncomment_line(hdr + 3, len - 3, lno, |
966 | 1.88M | hrecs, h_type, |
967 | 1.88M | sq_count, an_count); |
968 | 1.88M | if (line_len == 0) // Failed |
969 | 355 | goto error; |
970 | 1.88M | } |
971 | | |
972 | | // Add to end of global list |
973 | 1.89M | sam_hrecs_global_list_add(hrecs, h_type, NULL); |
974 | | |
975 | | // Form the ring, either with self or other lines of this type |
976 | 1.89M | if (!new) { |
977 | 1.86M | sam_hrec_type_t *t = kh_val(hrecs->h, k), *p; |
978 | 1.86M | p = t->prev; |
979 | | |
980 | 1.86M | assert(p->next == t); |
981 | 1.86M | p->next = h_type; |
982 | 1.86M | h_type->prev = p; |
983 | | |
984 | 1.86M | t->prev = h_type; |
985 | 1.86M | h_type->next = t; |
986 | 1.86M | } else { |
987 | 28.0k | kh_val(hrecs->h, k) = h_type; |
988 | 28.0k | h_type->prev = h_type->next = h_type; |
989 | 28.0k | } |
990 | | |
991 | 1.89M | if (line_len_out) |
992 | 59.1k | *line_len_out = line_len + 3; |
993 | 1.89M | return h_type; |
994 | | |
995 | 403 | error: |
996 | | // Tidy up as best we can |
997 | 403 | sam_hrecs_free_tags(hrecs, h_type->tag); |
998 | 403 | pool_free(hrecs->type_pool, h_type); |
999 | 403 | return NULL; |
1000 | 1.89M | } |
1001 | | |
1002 | | // Parse complete header into a sam_hrecs_t structure |
1003 | | |
1004 | 7.50k | static int sam_hrecs_parse_lines(sam_hrecs_t *hrecs, const char *hdr, size_t len) { |
1005 | 7.50k | size_t i, lno; |
1006 | | |
1007 | 7.50k | if (!hrecs || len > SSIZE_MAX) |
1008 | 0 | return -1; |
1009 | | |
1010 | 7.50k | if (!len) |
1011 | 0 | len = strlen(hdr); |
1012 | | |
1013 | 7.50k | if (len < 3) { |
1014 | 315 | if (len == 0 || *hdr == '\0') return 0; |
1015 | 35 | sam_hrecs_error("Header line too short", hdr, len, 1); |
1016 | 35 | return -1; |
1017 | 315 | } |
1018 | | |
1019 | 7.19k | sam_hrec_type_t *first_h_type = NULL; |
1020 | 7.19k | sam_hrec_type_t *last_h_type = NULL; |
1021 | 7.19k | khint_t sq_count = 0, an_count = 0; |
1022 | 7.19k | size_t line_len = 0; |
1023 | | |
1024 | 66.3k | for (i = 0, lno = 1; i < len - 3 && hdr[i] != '\0'; lno++) { |
1025 | 59.4k | sam_hrec_type_t *h_type; |
1026 | 59.4k | h_type = sam_hrecs_parse_single_line(hrecs, &hdr[i], len - i, |
1027 | 59.4k | lno, &line_len, |
1028 | 59.4k | &sq_count, &an_count); |
1029 | 59.4k | if (!h_type) |
1030 | 297 | return -1; |
1031 | | |
1032 | 59.1k | i += line_len + 1; |
1033 | | |
1034 | | // Keep track of first / last new record so they can be passed to |
1035 | | // sam_hrecs_update_hashes() below. Ignore HD as it may be stored |
1036 | | // out of order (it always goes at the start) and |
1037 | | // sam_hrecs_update_hashes() does nothing with HD. |
1038 | 59.1k | if (h_type->type != TYPEKEY("HD")) { |
1039 | 58.5k | if (!first_h_type) |
1040 | 6.81k | first_h_type = h_type; |
1041 | 58.5k | last_h_type = h_type; |
1042 | 58.5k | } |
1043 | 59.1k | } |
1044 | | |
1045 | | // Reserve space in hash tables and refs array |
1046 | 6.89k | if (sq_count) { |
1047 | | // It's possible that some entries are already in the hash table, |
1048 | | // e.g. via sam_hrecs_refs_from_targets_array(), so only reserve |
1049 | | // space beyond what's already there. |
1050 | 4.64k | khint_t ref_name_count = sq_count + an_count; |
1051 | 4.64k | khint_t sz = kh_size(hrecs->ref_hash); |
1052 | 4.64k | if (ref_name_count > sz |
1053 | 400 | && kh_grow_to_fit(m_s2i, hrecs->ref_hash, ref_name_count - sz) < 0) |
1054 | 0 | return -1; |
1055 | | |
1056 | 4.64k | if (hrecs->ref_sz < sq_count) { |
1057 | 287 | sam_hrec_sq_t *new_ref = hts_realloc_p(hrecs->ref, |
1058 | 287 | sizeof(*hrecs->ref), sq_count); |
1059 | 287 | if (!new_ref) |
1060 | 0 | return -1; |
1061 | 287 | hrecs->ref = new_ref; |
1062 | 287 | hrecs->ref_sz = sq_count; |
1063 | 287 | } |
1064 | 4.64k | } |
1065 | | |
1066 | | // Update RG/SQ/PG hashes |
1067 | 6.89k | if (first_h_type) { |
1068 | 6.72k | sam_hrec_type_t *h_type = first_h_type->global_prev; |
1069 | 58.3k | do { |
1070 | 58.3k | h_type = h_type->global_next; |
1071 | 58.3k | if (-1 == sam_hrecs_update_hashes(hrecs, h_type->type, h_type)) |
1072 | 76 | return -1; |
1073 | 58.3k | } while (h_type != last_h_type); |
1074 | 6.72k | } |
1075 | 6.81k | return 0; |
1076 | 6.89k | } |
1077 | | |
1078 | | /*! Update sam_hdr_t target_name and target_len arrays |
1079 | | * |
1080 | | * @return 0 on success; -1 on failure |
1081 | | */ |
1082 | | int sam_hdr_update_target_arrays(sam_hdr_t *bh, const sam_hrecs_t *hrecs, |
1083 | 68.7k | int refs_changed) { |
1084 | 68.7k | if (!bh || !hrecs) |
1085 | 0 | return -1; |
1086 | | |
1087 | 68.7k | if (refs_changed < 0) |
1088 | 0 | return 0; |
1089 | | |
1090 | | // Grow arrays if necessary |
1091 | 68.7k | if (bh->n_targets < hrecs->nref) { |
1092 | 28.7k | char **new_names = hts_realloc_p(bh->target_name, sizeof(*new_names), |
1093 | 28.7k | hrecs->nref); |
1094 | 28.7k | if (!new_names) |
1095 | 0 | return -1; |
1096 | 28.7k | bh->target_name = new_names; |
1097 | 28.7k | uint32_t *new_lens = hts_realloc_p(bh->target_len, sizeof(*new_lens), |
1098 | 28.7k | hrecs->nref); |
1099 | 28.7k | if (!new_lens) |
1100 | 0 | return -1; |
1101 | 28.7k | bh->target_len = new_lens; |
1102 | 28.7k | } |
1103 | | |
1104 | | // Update names and lengths where changed |
1105 | | // hrecs->refs_changed is the first ref that has been updated, so ones |
1106 | | // before that can be skipped. |
1107 | 68.7k | int i; |
1108 | 68.7k | khint_t k; |
1109 | 68.7k | khash_t(s2i) *long_refs = (khash_t(s2i) *) bh->sdict; |
1110 | 106k | for (i = refs_changed; i < hrecs->nref; i++) { |
1111 | 37.9k | if (i >= bh->n_targets |
1112 | 37.9k | || strcmp(bh->target_name[i], hrecs->ref[i].name) != 0) { |
1113 | 37.9k | if (i < bh->n_targets) { |
1114 | | // Remove from the hash table too as target_name[i] |
1115 | | // is used as a hash table key. |
1116 | 0 | if (long_refs) { |
1117 | 0 | k = kh_get(s2i, long_refs, bh->target_name[i]); |
1118 | 0 | if (k < kh_end(long_refs)) |
1119 | 0 | kh_del(s2i, long_refs, k); |
1120 | 0 | } |
1121 | 0 | free(bh->target_name[i]); |
1122 | 0 | } |
1123 | 37.9k | bh->target_name[i] = strdup(hrecs->ref[i].name); |
1124 | 37.9k | if (!bh->target_name[i]) |
1125 | 0 | return -1; |
1126 | 37.9k | } |
1127 | 37.9k | if (hrecs->ref[i].len < UINT32_MAX) { |
1128 | 27.7k | bh->target_len[i] = hrecs->ref[i].len; |
1129 | | |
1130 | 27.7k | if (!long_refs) |
1131 | 27.1k | continue; |
1132 | | |
1133 | | // Check if we have an old length, if so remove it. |
1134 | 568 | k = kh_get(s2i, long_refs, bh->target_name[i]); |
1135 | 568 | if (k < kh_end(long_refs)) |
1136 | 0 | kh_del(s2i, long_refs, k); |
1137 | 10.2k | } else { |
1138 | 10.2k | bh->target_len[i] = UINT32_MAX; |
1139 | 10.2k | if (bh->hrecs != hrecs) { |
1140 | | // Called from sam_hdr_dup; need to add sdict entries |
1141 | 6.11k | if (!long_refs) { |
1142 | 2.24k | if (!(bh->sdict = long_refs = kh_init(s2i))) |
1143 | 0 | return -1; |
1144 | 2.24k | } |
1145 | | |
1146 | | // Add / update length |
1147 | 6.11k | int absent; |
1148 | 6.11k | k = kh_put(s2i, long_refs, bh->target_name[i], &absent); |
1149 | 6.11k | if (absent < 0) |
1150 | 0 | return -1; |
1151 | 6.11k | kh_val(long_refs, k) = hrecs->ref[i].len; |
1152 | 6.11k | } |
1153 | 10.2k | } |
1154 | 37.9k | } |
1155 | | |
1156 | | // Free up any names that have been removed |
1157 | 68.7k | for (; i < bh->n_targets; i++) { |
1158 | 0 | if (long_refs) { |
1159 | 0 | k = kh_get(s2i, long_refs, bh->target_name[i]); |
1160 | 0 | if (k < kh_end(long_refs)) |
1161 | 0 | kh_del(s2i, long_refs, k); |
1162 | 0 | } |
1163 | 0 | free(bh->target_name[i]); |
1164 | 0 | } |
1165 | | |
1166 | 68.7k | bh->n_targets = hrecs->nref; |
1167 | 68.7k | return 0; |
1168 | 68.7k | } |
1169 | | |
1170 | 291 | static int rebuild_target_arrays(sam_hdr_t *bh) { |
1171 | 291 | if (!bh || !bh->hrecs) |
1172 | 0 | return -1; |
1173 | | |
1174 | 291 | sam_hrecs_t *hrecs = bh->hrecs; |
1175 | 291 | if (hrecs->refs_changed < 0) |
1176 | 0 | return 0; |
1177 | | |
1178 | 291 | if (sam_hdr_update_target_arrays(bh, hrecs, hrecs->refs_changed) != 0) |
1179 | 0 | return -1; |
1180 | | |
1181 | 291 | hrecs->refs_changed = -1; |
1182 | 291 | return 0; |
1183 | 291 | } |
1184 | | |
1185 | | /// Populate hrecs refs array from header target_name, target_len arrays |
1186 | | /** |
1187 | | * @return 0 on success; -1 on failure |
1188 | | * |
1189 | | * Pre-fills the refs hash from the target arrays. For BAM files this |
1190 | | * will ensure that they are in the correct order as the target arrays |
1191 | | * are the canonical source for converting target ids to names and lengths. |
1192 | | * |
1193 | | * The added entries do not link to a header line. sam_hrecs_update_hashes() |
1194 | | * will add the links later for lines found in the text header. |
1195 | | * |
1196 | | * This should be called before the text header is parsed. |
1197 | | */ |
1198 | | static int sam_hrecs_refs_from_targets_array(sam_hrecs_t *hrecs, |
1199 | 5.74k | const sam_hdr_t *bh) { |
1200 | 5.74k | int32_t tid = 0; |
1201 | | |
1202 | 5.74k | if (!hrecs || !bh) |
1203 | 0 | return -1; |
1204 | | |
1205 | | // This should always be called before parsing the text header |
1206 | | // so the ref array should start off empty, and we don't have to try |
1207 | | // to reconcile any existing data. |
1208 | 5.74k | if (hrecs->nref > 0) { |
1209 | 0 | hts_log_error("Called with non-empty ref array"); |
1210 | 0 | return -1; |
1211 | 0 | } |
1212 | | |
1213 | 5.74k | if (hrecs->ref_sz < bh->n_targets) { |
1214 | 5.74k | sam_hrec_sq_t *new_ref = hts_realloc_p(hrecs->ref, sizeof(*new_ref), |
1215 | 5.74k | bh->n_targets); |
1216 | 5.74k | if (!new_ref) |
1217 | 0 | return -1; |
1218 | | |
1219 | 5.74k | hrecs->ref = new_ref; |
1220 | 5.74k | hrecs->ref_sz = bh->n_targets; |
1221 | 5.74k | } |
1222 | | |
1223 | 5.74k | if (kh_grow_to_fit(m_s2i, hrecs->ref_hash, bh->n_targets) != 0) |
1224 | 0 | goto fail; |
1225 | | |
1226 | 13.8k | for (tid = 0; tid < bh->n_targets; tid++) { |
1227 | 8.22k | khint_t k; |
1228 | 8.22k | int r; |
1229 | 8.22k | hrecs->ref[tid].name = string_dup(hrecs->str_pool, bh->target_name[tid]); |
1230 | 8.22k | if (!hrecs->ref[tid].name) goto fail; |
1231 | 8.22k | if (bh->target_len[tid] < UINT32_MAX || !bh->sdict) { |
1232 | 5.98k | hrecs->ref[tid].len = bh->target_len[tid]; |
1233 | 5.98k | } else { |
1234 | 2.23k | khash_t(s2i) *long_refs = (khash_t(s2i) *) bh->sdict; |
1235 | 2.23k | k = kh_get(s2i, long_refs, hrecs->ref[tid].name); |
1236 | 2.23k | if (k < kh_end(long_refs)) { |
1237 | 2.23k | hrecs->ref[tid].len = kh_val(long_refs, k); |
1238 | 2.23k | } else { |
1239 | 0 | hrecs->ref[tid].len = UINT32_MAX; |
1240 | 0 | } |
1241 | 2.23k | } |
1242 | 8.22k | hrecs->ref[tid].ty = NULL; |
1243 | 8.22k | k = kh_put(m_s2i, hrecs->ref_hash, hrecs->ref[tid].name, &r); |
1244 | 8.22k | if (r < 0) goto fail; |
1245 | 8.22k | if (r == 0) { |
1246 | 100 | hts_log_error("Duplicate entry \"%s\" in target list", |
1247 | 100 | hrecs->ref[tid].name); |
1248 | 100 | return -1; |
1249 | 8.12k | } else { |
1250 | 8.12k | kh_val(hrecs->ref_hash, k) = tid; |
1251 | 8.12k | } |
1252 | 8.22k | } |
1253 | 5.64k | hrecs->nref = bh->n_targets; |
1254 | 5.64k | return 0; |
1255 | | |
1256 | 0 | fail: { |
1257 | 0 | int32_t i; |
1258 | 0 | hts_log_error("%s", strerror(errno)); |
1259 | 0 | for (i = 0; i < tid; i++) { |
1260 | 0 | khint_t k; |
1261 | 0 | if (!hrecs->ref[i].name) continue; |
1262 | 0 | k = kh_get(m_s2i, hrecs->ref_hash, hrecs->ref[tid].name); |
1263 | 0 | if (k < kh_end(hrecs->ref_hash)) kh_del(m_s2i, hrecs->ref_hash, k); |
1264 | 0 | } |
1265 | 0 | hrecs->nref = 0; |
1266 | 0 | return -1; |
1267 | 5.74k | } |
1268 | 5.74k | } |
1269 | | |
1270 | | /* |
1271 | | * Add SQ header records for any references in the hrecs->ref array that |
1272 | | * were added by sam_hrecs_refs_from_targets_array() but have not |
1273 | | * been linked to an @SQ line by sam_hrecs_update_hashes() yet. |
1274 | | * |
1275 | | * This may be needed either because: |
1276 | | * |
1277 | | * - A bam file was read that had entries in its refs list with no |
1278 | | * corresponding @SQ line. |
1279 | | * |
1280 | | * - A program constructed a sam_hdr_t which has target_name and target_len |
1281 | | * array entries with no corresponding @SQ line in text. |
1282 | | */ |
1283 | 36.6k | static int add_stub_ref_sq_lines(sam_hrecs_t *hrecs) { |
1284 | 36.6k | int tid; |
1285 | 36.6k | char len[32]; |
1286 | | |
1287 | 44.4k | for (tid = 0; tid < hrecs->nref; tid++) { |
1288 | 7.85k | if (hrecs->ref[tid].ty == NULL) { |
1289 | 1.38k | snprintf(len, sizeof(len), "%"PRIhts_pos, hrecs->ref[tid].len); |
1290 | 1.38k | if (sam_hrecs_add(hrecs, "SQ", |
1291 | 1.38k | "SN", hrecs->ref[tid].name, |
1292 | 1.38k | "LN", len, NULL) != 0) |
1293 | 60 | return -1; |
1294 | | |
1295 | | // Check that the stub has actually been filled |
1296 | 1.32k | if(hrecs->ref[tid].ty == NULL) { |
1297 | 0 | hts_log_error("Reference stub with tid=%d, name=\"%s\", len=%"PRIhts_pos" could not be filled", |
1298 | 0 | tid, hrecs->ref[tid].name, hrecs->ref[tid].len); |
1299 | 0 | return -1; |
1300 | 0 | } |
1301 | 1.32k | } |
1302 | 7.85k | } |
1303 | 36.5k | return 0; |
1304 | 36.6k | } |
1305 | | |
1306 | 37.0k | int sam_hdr_fill_hrecs(sam_hdr_t *bh) { |
1307 | 37.0k | sam_hrecs_t *hrecs = sam_hrecs_new(); |
1308 | | |
1309 | 37.0k | if (!hrecs) |
1310 | 0 | return -1; |
1311 | | |
1312 | 37.0k | if (bh->target_name && bh->target_len && bh->n_targets > 0) { |
1313 | 5.74k | if (sam_hrecs_refs_from_targets_array(hrecs, bh) != 0) { |
1314 | 100 | sam_hrecs_free(hrecs); |
1315 | 100 | return -1; |
1316 | 100 | } |
1317 | 5.74k | } |
1318 | | |
1319 | | // Parse existing header text |
1320 | 36.9k | if (bh->text && bh->l_text > 0) { |
1321 | 6.69k | if (sam_hrecs_parse_lines(hrecs, bh->text, bh->l_text) != 0) { |
1322 | 302 | sam_hrecs_free(hrecs); |
1323 | 302 | return -1; |
1324 | 302 | } |
1325 | 6.69k | } |
1326 | | |
1327 | 36.6k | if (add_stub_ref_sq_lines(hrecs) < 0) { |
1328 | 60 | sam_hrecs_free(hrecs); |
1329 | 60 | return -1; |
1330 | 60 | } |
1331 | | |
1332 | 36.5k | bh->hrecs = hrecs; |
1333 | | |
1334 | 36.5k | if (hrecs->refs_changed >= 0 && rebuild_target_arrays(bh) != 0) |
1335 | 0 | return -1; |
1336 | | |
1337 | 36.5k | return 0; |
1338 | 36.5k | } |
1339 | | |
1340 | | // Check header type is valid according to the SAM specification |
1341 | | |
1342 | 1.83M | static int valid_sam_header_type(const char *s) { |
1343 | 1.83M | if (s[0] != '@') return 0; |
1344 | 1.83M | switch (s[1]) { |
1345 | 567 | case 'H': |
1346 | 567 | return s[2] == 'D' && s[3] == '\t'; |
1347 | 15.6k | case 'S': |
1348 | 15.6k | return s[2] == 'Q' && s[3] == '\t'; |
1349 | 1.78M | case 'R': |
1350 | 1.80M | case 'P': |
1351 | 1.80M | return s[2] == 'G' && s[3] == '\t'; |
1352 | 8.74k | case 'C': |
1353 | 8.74k | return s[2] == 'O'; |
1354 | 1.83M | } |
1355 | 6 | return 0; |
1356 | 1.83M | } |
1357 | | |
1358 | | /** Populate sam_hdr_t from SAM file content |
1359 | | * |
1360 | | * @param hdr empty header struct to be filled |
1361 | | * @param fp File to read from |
1362 | | * @return 0 on success |
1363 | | * -1 on failure |
1364 | | * |
1365 | | * This function is used to build the header structure when reading SAM files. |
1366 | | * The lines read from the file are used to create sam_hrecs_t structures. |
1367 | | * The function also populates sam_hdr_t::text, sam_hdr_t::l_text, |
1368 | | * sam_hdr_t::target_name and sam_hdr_t::target_len. |
1369 | | */ |
1370 | 18.5k | int sam_hdr_build_from_sam_file(sam_hdr_t *hdr, htsFile* fp) { |
1371 | 18.5k | kstring_t str = { 0, 0, NULL }; |
1372 | 18.5k | int ret; |
1373 | 18.5k | int next_c = '@'; |
1374 | 18.5k | sam_hrec_type_t *first_h_type = NULL; |
1375 | 18.5k | sam_hrec_type_t *last_h_type = NULL; |
1376 | 18.5k | sam_hrecs_t *hrecs = sam_hrecs_new(); |
1377 | 18.5k | size_t lno = 0; |
1378 | 18.5k | khint_t sq_count = 0, an_count = 0; |
1379 | | |
1380 | 18.5k | if (!hrecs) |
1381 | 0 | return -1; |
1382 | | |
1383 | 18.5k | hdr->hrecs = hrecs; |
1384 | | |
1385 | 1.85M | while (next_c == '@' && (ret = hts_getline(fp, KS_SEP_LINE, &fp->line)) >= 0) { |
1386 | 1.83M | sam_hrec_type_t *h_type; |
1387 | | |
1388 | 1.83M | lno++; |
1389 | 1.83M | if (!fp->line.s || fp->line.s[0] != '@') |
1390 | 51 | break; |
1391 | | |
1392 | 1.83M | if (fp->line.l < 3 |
1393 | 1.83M | || !isalpha_c(fp->line.s[1]) || !isalpha_c(fp->line.s[2])) { |
1394 | 66 | sam_hrecs_error("Header line does not have a two character key", |
1395 | 66 | fp->line.s, fp->line.l, lno); |
1396 | 66 | goto error; |
1397 | 66 | } |
1398 | | |
1399 | 1.83M | if (fp->line.l == 3 && strncmp(fp->line.s, "@CO", 3) == 0) { |
1400 | | // HTSlib's original SAM reader let you get away with an untabbed |
1401 | | // @CO line |
1402 | 825 | if (kputc('\t', &fp->line) < 0) |
1403 | 0 | goto error; |
1404 | 825 | } |
1405 | | |
1406 | 1.83M | if (!valid_sam_header_type(fp->line.s)) { |
1407 | 60 | hts_log_error("Invalid header line: must start with @HD/@SQ/@RG/@PG/@CO"); |
1408 | 60 | warn_if_known_stderr(fp->line.s, fp->line.l); |
1409 | 60 | goto error; |
1410 | 60 | } |
1411 | | |
1412 | 1.83M | h_type = sam_hrecs_parse_single_line(hrecs, fp->line.s, fp->line.l, |
1413 | 1.83M | lno, NULL, &sq_count, &an_count); |
1414 | 1.83M | if (!h_type) |
1415 | 288 | goto error; |
1416 | | |
1417 | 1.83M | if (h_type->type != TYPEKEY("HD")) { |
1418 | 1.83M | if (!first_h_type) |
1419 | 18.1k | first_h_type = h_type; |
1420 | 1.83M | last_h_type = h_type; |
1421 | 1.83M | } |
1422 | | |
1423 | 1.83M | if (kputsn(fp->line.s, fp->line.l, &str) < 0) |
1424 | 0 | goto error; |
1425 | | |
1426 | 1.83M | if (kputc('\n', &str) < 0) |
1427 | 0 | goto error; |
1428 | | |
1429 | 1.83M | if (fp->is_bgzf) { |
1430 | 1.77M | next_c = bgzf_peek(fp->fp.bgzf); |
1431 | 1.77M | } else { |
1432 | 55.8k | unsigned char nc; |
1433 | 55.8k | ssize_t pret = hpeek(fp->fp.hfile, &nc, 1); |
1434 | 55.8k | next_c = pret > 0 ? nc : pret - 1; |
1435 | 55.8k | } |
1436 | 1.83M | if (next_c < -1) |
1437 | 3 | goto error; |
1438 | 1.83M | } |
1439 | 18.1k | if (next_c != '@') |
1440 | 18.0k | fp->line.l = 0; |
1441 | | |
1442 | 18.1k | if (ret < -1) |
1443 | 42 | goto error; |
1444 | | |
1445 | 18.0k | if (sq_count == 0 && fp->fn_aux) { |
1446 | | // No @SQ lines found. Try to get them from the fasta index. |
1447 | 0 | kstring_t line = { 0, 0, NULL }; |
1448 | | |
1449 | | /* The reference index (.fai) is actually needed here */ |
1450 | 0 | char *fai_fn = fp->fn_aux; |
1451 | 0 | char *fn_delim = strstr(fp->fn_aux, HTS_IDX_DELIM); |
1452 | 0 | if (fn_delim) |
1453 | 0 | fai_fn = fn_delim + strlen(HTS_IDX_DELIM); |
1454 | |
|
1455 | 0 | hFILE* f = hopen(fai_fn, "r"); |
1456 | 0 | int e = 0; |
1457 | 0 | if (f == NULL) |
1458 | 0 | goto error; |
1459 | | |
1460 | 0 | while (line.l = 0, khgetline(&line, f) >= 0) { |
1461 | 0 | char* tab = strchr(line.s, '\t'); |
1462 | 0 | size_t l_start = str.l; |
1463 | 0 | sam_hrec_type_t *h_type; |
1464 | 0 | hts_pos_t ln; |
1465 | |
|
1466 | 0 | if (tab == NULL) |
1467 | 0 | continue; |
1468 | | |
1469 | 0 | ln = strtoll(tab, NULL, 10); |
1470 | |
|
1471 | 0 | e |= kputs("@SQ\tSN:", &str) < 0; |
1472 | 0 | e |= kputsn(line.s, tab - line.s, &str) < 0; |
1473 | 0 | e |= kputs("\tLN:", &str) < 0; |
1474 | 0 | e |= kputll(ln, &str) < 0; |
1475 | 0 | e |= kputc('\n', &str) < 0; |
1476 | 0 | if (e) |
1477 | 0 | break; |
1478 | | |
1479 | 0 | h_type = sam_hrecs_parse_single_line(hrecs, &str.s[l_start], |
1480 | 0 | str.l - l_start, |
1481 | 0 | lno, NULL, |
1482 | 0 | &sq_count, &an_count); |
1483 | 0 | if (!h_type) |
1484 | 0 | goto error; |
1485 | | |
1486 | 0 | if (!first_h_type) |
1487 | 0 | first_h_type = h_type; |
1488 | 0 | last_h_type = h_type; |
1489 | 0 | } |
1490 | | |
1491 | 0 | ks_free(&line); |
1492 | 0 | if (hclose(f) != 0) { |
1493 | 0 | hts_log_error("Error on closing %s", fai_fn); |
1494 | 0 | e = 1; |
1495 | 0 | } |
1496 | 0 | if (e) |
1497 | 0 | goto error; |
1498 | 0 | } |
1499 | | |
1500 | | // Reserve space in hash tables |
1501 | 18.0k | if (sq_count) { |
1502 | | // Hash table should be empty at the moment |
1503 | 12.0k | if (kh_grow_to_fit(m_s2i, hrecs->ref_hash, sq_count + an_count) < 0) |
1504 | 0 | goto error; |
1505 | | |
1506 | 12.0k | if (hrecs->ref_sz < sq_count) { |
1507 | 12.0k | sam_hrec_sq_t *new_ref = hts_realloc_p(hrecs->ref, |
1508 | 12.0k | sizeof(*hrecs->ref), sq_count); |
1509 | 12.0k | if (!new_ref) |
1510 | 0 | goto error; |
1511 | 12.0k | hrecs->ref = new_ref; |
1512 | 12.0k | hrecs->ref_sz = sq_count; |
1513 | 12.0k | } |
1514 | 12.0k | } |
1515 | | |
1516 | | // Update RG/SQ/PG hashes |
1517 | 18.0k | if (first_h_type) { |
1518 | 17.9k | sam_hrec_type_t *h_type = first_h_type->global_prev; |
1519 | 165k | do { |
1520 | 165k | h_type = h_type->global_next; |
1521 | 165k | if (-1 == sam_hrecs_update_hashes(hrecs, h_type->type, h_type)) |
1522 | 852 | goto error; |
1523 | 165k | } while (h_type != last_h_type); |
1524 | 17.9k | } |
1525 | | |
1526 | 17.2k | if (str.l == 0) { |
1527 | 51 | if (kputsn("", 0, &str) < 0) |
1528 | 0 | goto error; |
1529 | 51 | } |
1530 | 17.2k | hdr->l_text = str.l; |
1531 | 17.2k | hdr->text = ks_release(&str); |
1532 | | |
1533 | 17.2k | if (sam_hdr_update_target_arrays(hdr, hrecs, 0) < 0) |
1534 | 0 | goto error; |
1535 | | |
1536 | 17.2k | return 0; |
1537 | | |
1538 | 1.31k | error: |
1539 | 1.31k | ks_free(&str); |
1540 | 1.31k | return -1; |
1541 | 17.2k | } |
1542 | | |
1543 | | /** Remove outdated header text |
1544 | | |
1545 | | @param bh BAM header |
1546 | | |
1547 | | This is called when API functions have changed the header so that the |
1548 | | text version is no longer valid. |
1549 | | */ |
1550 | 708 | static void redact_header_text(sam_hdr_t *bh) { |
1551 | 708 | assert(bh->hrecs && bh->hrecs->dirty); |
1552 | 708 | bh->l_text = 0; |
1553 | 708 | free(bh->text); |
1554 | 708 | bh->text = NULL; |
1555 | 708 | } |
1556 | | |
1557 | | /** Find nth header record of a given type |
1558 | | |
1559 | | @param type Header type (SQ, RG etc.) |
1560 | | @param idx 0-based index |
1561 | | |
1562 | | @return sam_hrec_type_t pointer to the record on success |
1563 | | NULL if no record exists with the given type and index |
1564 | | */ |
1565 | | |
1566 | | static sam_hrec_type_t *sam_hrecs_find_type_pos(sam_hrecs_t *hrecs, |
1567 | 0 | const char *type, int idx) { |
1568 | 0 | sam_hrec_type_t *first, *itr; |
1569 | |
|
1570 | 0 | if (idx < 0) |
1571 | 0 | return NULL; |
1572 | | |
1573 | 0 | if (type[0] == 'S' && type[1] == 'Q') |
1574 | 0 | return idx < hrecs->nref ? hrecs->ref[idx].ty : NULL; |
1575 | | |
1576 | 0 | if (type[0] == 'R' && type[1] == 'G') |
1577 | 0 | return idx < hrecs->nrg ? hrecs->rg[idx].ty : NULL; |
1578 | | |
1579 | 0 | if (type[0] == 'P' && type[1] == 'G') |
1580 | 0 | return idx < hrecs->npg ? hrecs->pg[idx].ty : NULL; |
1581 | | |
1582 | 0 | first = itr = sam_hrecs_find_type_id(hrecs, type, NULL, NULL); |
1583 | 0 | if (!first) |
1584 | 0 | return NULL; |
1585 | | |
1586 | 0 | while (idx > 0) { |
1587 | 0 | itr = itr->next; |
1588 | 0 | if (itr == first) |
1589 | 0 | break; |
1590 | 0 | --idx; |
1591 | 0 | } |
1592 | |
|
1593 | 0 | return idx == 0 ? itr : NULL; |
1594 | 0 | } |
1595 | | |
1596 | | /* ==== Public methods ==== */ |
1597 | | |
1598 | 10.9k | size_t sam_hdr_length(sam_hdr_t *bh) { |
1599 | 10.9k | if (!bh || -1 == sam_hdr_rebuild(bh)) |
1600 | 0 | return SIZE_MAX; |
1601 | | |
1602 | 10.9k | return bh->l_text; |
1603 | 10.9k | } |
1604 | | |
1605 | 6.12k | const char *sam_hdr_str(sam_hdr_t *bh) { |
1606 | 6.12k | if (!bh || -1 == sam_hdr_rebuild(bh)) |
1607 | 0 | return NULL; |
1608 | | |
1609 | 6.12k | return bh->text; |
1610 | 6.12k | } |
1611 | | |
1612 | 0 | int sam_hdr_nref(const sam_hdr_t *bh) { |
1613 | 0 | if (!bh) |
1614 | 0 | return -1; |
1615 | | |
1616 | 0 | return bh->hrecs ? bh->hrecs->nref : bh->n_targets; |
1617 | 0 | } |
1618 | | |
1619 | | /* |
1620 | | * Reconstructs the text representation from the header hash table. |
1621 | | * Returns 0 on success |
1622 | | * -1 on failure |
1623 | | */ |
1624 | 17.0k | int sam_hdr_rebuild(sam_hdr_t *bh) { |
1625 | 17.0k | sam_hrecs_t *hrecs; |
1626 | 17.0k | if (!bh) |
1627 | 0 | return -1; |
1628 | | |
1629 | 17.0k | if (!(hrecs = bh->hrecs)) |
1630 | 0 | return bh->text ? 0 : -1; |
1631 | | |
1632 | 17.0k | if (hrecs->refs_changed >= 0) { |
1633 | 0 | if (rebuild_target_arrays(bh) < 0) { |
1634 | 0 | hts_log_error("Header target array rebuild has failed"); |
1635 | 0 | return -1; |
1636 | 0 | } |
1637 | 0 | } |
1638 | | |
1639 | | /* If header text wasn't changed or header is empty, don't rebuild it. */ |
1640 | 17.0k | if (!hrecs->dirty) |
1641 | 16.9k | return 0; |
1642 | | |
1643 | 44 | if (hrecs->pgs_changed && sam_hdr_link_pg(bh) < 0) { |
1644 | 0 | hts_log_error("Linking @PG lines has failed"); |
1645 | 0 | return -1; |
1646 | 0 | } |
1647 | | |
1648 | 44 | kstring_t ks = KS_INITIALIZE; |
1649 | 44 | if (sam_hrecs_rebuild_text(hrecs, &ks) != 0) { |
1650 | 0 | ks_free(&ks); |
1651 | 0 | hts_log_error("Header text rebuild has failed"); |
1652 | 0 | return -1; |
1653 | 0 | } |
1654 | | |
1655 | 44 | hrecs->dirty = 0; |
1656 | | |
1657 | | /* Sync */ |
1658 | 44 | free(bh->text); |
1659 | 44 | bh->l_text = ks_len(&ks); |
1660 | 44 | bh->text = ks_release(&ks); |
1661 | | |
1662 | 44 | return 0; |
1663 | 44 | } |
1664 | | |
1665 | | /* |
1666 | | * Appends a formatted line to an existing SAM header. |
1667 | | * Line is a full SAM header record, eg "@SQ\tSN:foo\tLN:100", with |
1668 | | * optional new-line. If it contains more than 1 line then multiple lines |
1669 | | * will be added in order. |
1670 | | * |
1671 | | * Input text is of maximum length len or as terminated earlier by a NUL. |
1672 | | * len may be 0 if unknown, in which case lines must be NUL-terminated. |
1673 | | * |
1674 | | * Returns 0 on success |
1675 | | * -1 on failure |
1676 | | */ |
1677 | 10.0k | int sam_hdr_add_lines(sam_hdr_t *bh, const char *lines, size_t len) { |
1678 | 10.0k | sam_hrecs_t *hrecs; |
1679 | | |
1680 | 10.0k | if (!bh || !lines) |
1681 | 0 | return -1; |
1682 | | |
1683 | 10.0k | if (len == 0 && *lines == '\0') |
1684 | 9.21k | return 0; |
1685 | | |
1686 | 814 | if (!(hrecs = bh->hrecs)) { |
1687 | 814 | if (sam_hdr_fill_hrecs(bh) != 0) |
1688 | 0 | return -1; |
1689 | 814 | hrecs = bh->hrecs; |
1690 | 814 | } |
1691 | | |
1692 | 814 | if (sam_hrecs_parse_lines(hrecs, lines, len) != 0) |
1693 | 106 | return -1; |
1694 | | |
1695 | 708 | if (hrecs->refs_changed >= 0 && rebuild_target_arrays(bh) != 0) |
1696 | 0 | return -1; |
1697 | | |
1698 | 708 | hrecs->dirty = 1; |
1699 | 708 | redact_header_text(bh); |
1700 | | |
1701 | 708 | return 0; |
1702 | 708 | } |
1703 | | |
1704 | | /* |
1705 | | * Adds a single line to a SAM header. |
1706 | | * Specify type and one or more key,value pairs, ending with the NULL key. |
1707 | | * Eg. sam_hdr_add_line(h, "SQ", "ID", "foo", "LN", "100", NULL). |
1708 | | * |
1709 | | * Returns 0 on success |
1710 | | * -1 on failure |
1711 | | */ |
1712 | 0 | int sam_hdr_add_line(sam_hdr_t *bh, const char *type, ...) { |
1713 | 0 | va_list args; |
1714 | 0 | sam_hrecs_t *hrecs; |
1715 | |
|
1716 | 0 | if (!bh || !type) |
1717 | 0 | return -1; |
1718 | | |
1719 | 0 | if (!(hrecs = bh->hrecs)) { |
1720 | 0 | if (sam_hdr_fill_hrecs(bh) != 0) |
1721 | 0 | return -1; |
1722 | 0 | hrecs = bh->hrecs; |
1723 | 0 | } |
1724 | | |
1725 | 0 | va_start(args, type); |
1726 | 0 | int ret = sam_hrecs_vadd(hrecs, type, args, NULL); |
1727 | 0 | va_end(args); |
1728 | |
|
1729 | 0 | if (ret == 0) { |
1730 | 0 | if (hrecs->refs_changed >= 0 && rebuild_target_arrays(bh) != 0) |
1731 | 0 | return -1; |
1732 | | |
1733 | 0 | if (hrecs->dirty) |
1734 | 0 | redact_header_text(bh); |
1735 | 0 | } |
1736 | | |
1737 | 0 | return ret; |
1738 | 0 | } |
1739 | | |
1740 | | /* |
1741 | | * Returns a complete line of formatted text for a specific head type/ID |
1742 | | * combination. If ID_key is NULL then it returns the first line of the specified |
1743 | | * type. |
1744 | | */ |
1745 | | int sam_hdr_find_line_id(sam_hdr_t *bh, const char *type, |
1746 | 0 | const char *ID_key, const char *ID_val, kstring_t *ks) { |
1747 | 0 | sam_hrecs_t *hrecs; |
1748 | 0 | if (!bh || !type) |
1749 | 0 | return -2; |
1750 | | |
1751 | 0 | if (!(hrecs = bh->hrecs)) { |
1752 | 0 | if (sam_hdr_fill_hrecs(bh) != 0) |
1753 | 0 | return -2; |
1754 | 0 | hrecs = bh->hrecs; |
1755 | 0 | } |
1756 | | |
1757 | 0 | sam_hrec_type_t *ty = sam_hrecs_find_type_id(hrecs, type, ID_key, ID_val); |
1758 | 0 | if (!ty) |
1759 | 0 | return -1; |
1760 | | |
1761 | 0 | ks->l = 0; |
1762 | 0 | if (build_header_line(ty, ks) < 0) { |
1763 | 0 | return -2; |
1764 | 0 | } |
1765 | | |
1766 | 0 | return 0; |
1767 | 0 | } |
1768 | | |
1769 | | int sam_hdr_find_line_pos(sam_hdr_t *bh, const char *type, |
1770 | 0 | int pos, kstring_t *ks) { |
1771 | 0 | sam_hrecs_t *hrecs; |
1772 | 0 | if (!bh || !type) |
1773 | 0 | return -2; |
1774 | | |
1775 | 0 | if (!(hrecs = bh->hrecs)) { |
1776 | 0 | if (sam_hdr_fill_hrecs(bh) != 0) |
1777 | 0 | return -2; |
1778 | 0 | hrecs = bh->hrecs; |
1779 | 0 | } |
1780 | | |
1781 | 0 | sam_hrec_type_t *ty = sam_hrecs_find_type_pos(hrecs, type, pos); |
1782 | 0 | if (!ty) |
1783 | 0 | return -1; |
1784 | | |
1785 | 0 | ks->l = 0; |
1786 | 0 | if (build_header_line(ty, ks) < 0) { |
1787 | 0 | return -2; |
1788 | 0 | } |
1789 | | |
1790 | 0 | return 0; |
1791 | 0 | } |
1792 | | |
1793 | | /* |
1794 | | * Remove a line from the header by specifying a tag:value that uniquely |
1795 | | * identifies a line, i.e. the @SQ line containing "SN:ref1". |
1796 | | * @SQ line is uniquely identified by SN tag. |
1797 | | * @RG line is uniquely identified by ID tag. |
1798 | | * @PG line is uniquely identified by ID tag. |
1799 | | * |
1800 | | * Returns 0 on success and -1 on error |
1801 | | */ |
1802 | | |
1803 | 0 | int sam_hdr_remove_line_id(sam_hdr_t *bh, const char *type, const char *ID_key, const char *ID_value) { |
1804 | 0 | sam_hrecs_t *hrecs; |
1805 | 0 | if (!bh || !type) |
1806 | 0 | return -1; |
1807 | | |
1808 | 0 | if (!(hrecs = bh->hrecs)) { |
1809 | 0 | if (sam_hdr_fill_hrecs(bh) != 0) |
1810 | 0 | return -1; |
1811 | 0 | hrecs = bh->hrecs; |
1812 | 0 | } |
1813 | | |
1814 | 0 | if (!strncmp(type, "PG", 2)) { |
1815 | 0 | hts_log_warning("Removing PG lines is not supported!"); |
1816 | 0 | return -1; |
1817 | 0 | } |
1818 | | |
1819 | 0 | sam_hrec_type_t *type_found = sam_hrecs_find_type_id(hrecs, type, ID_key, ID_value); |
1820 | 0 | if (!type_found) |
1821 | 0 | return 0; |
1822 | | |
1823 | 0 | int ret = sam_hrecs_remove_line(hrecs, type, type_found, 1); |
1824 | 0 | if (ret == 0) { |
1825 | 0 | if (hrecs->refs_changed >= 0 && rebuild_target_arrays(bh) != 0) |
1826 | 0 | return -1; |
1827 | | |
1828 | 0 | if (hrecs->dirty) |
1829 | 0 | redact_header_text(bh); |
1830 | 0 | } |
1831 | | |
1832 | 0 | return ret; |
1833 | 0 | } |
1834 | | |
1835 | | /* |
1836 | | * Remove a line from the header by specifying the position in the type |
1837 | | * group, i.e. 3rd @SQ line. |
1838 | | * |
1839 | | * Returns 0 on success and -1 on error |
1840 | | */ |
1841 | | |
1842 | 0 | int sam_hdr_remove_line_pos(sam_hdr_t *bh, const char *type, int position) { |
1843 | 0 | sam_hrecs_t *hrecs; |
1844 | 0 | if (!bh || !type || position < 0) |
1845 | 0 | return -1; |
1846 | | |
1847 | 0 | if (!(hrecs = bh->hrecs)) { |
1848 | 0 | if (sam_hdr_fill_hrecs(bh) != 0) |
1849 | 0 | return -1; |
1850 | 0 | hrecs = bh->hrecs; |
1851 | 0 | } |
1852 | | |
1853 | 0 | if (!strncmp(type, "PG", 2)) { |
1854 | 0 | hts_log_warning("Removing PG lines is not supported!"); |
1855 | 0 | return -1; |
1856 | 0 | } |
1857 | | |
1858 | 0 | sam_hrec_type_t *type_found = sam_hrecs_find_type_pos(hrecs, type, |
1859 | 0 | position); |
1860 | 0 | if (!type_found) |
1861 | 0 | return -1; |
1862 | | |
1863 | 0 | int ret = sam_hrecs_remove_line(hrecs, type, type_found, 1); |
1864 | 0 | if (ret == 0) { |
1865 | 0 | if (hrecs->refs_changed >= 0 && rebuild_target_arrays(bh) != 0) |
1866 | 0 | return -1; |
1867 | | |
1868 | 0 | if (hrecs->dirty) |
1869 | 0 | redact_header_text(bh); |
1870 | 0 | } |
1871 | | |
1872 | 0 | return ret; |
1873 | 0 | } |
1874 | | |
1875 | | /* |
1876 | | * Check if sam_hdr_update_line() is being used to change the name of |
1877 | | * a record, and if the new name is going to clash with an existing one. |
1878 | | * |
1879 | | * If ap includes repeated keys, we go with the last one as sam_hrecs_vupdate() |
1880 | | * will go through them all and leave the final one in place. |
1881 | | * |
1882 | | * Returns 0 if the name does not change |
1883 | | * 1 if the name changes but does not clash |
1884 | | * -1 if the name changes and the new one is already in use |
1885 | | */ |
1886 | | static int check_for_name_update(sam_hrecs_t *hrecs, sam_hrec_type_t *rec, |
1887 | | va_list ap, const char **old_name, |
1888 | | const char **new_name, |
1889 | | char id_tag_out[3], |
1890 | 0 | khash_t(m_s2i) **hash_out) { |
1891 | 0 | char *key, *val; |
1892 | 0 | const char *id_tag; |
1893 | 0 | sam_hrec_tag_t *tag, *prev; |
1894 | 0 | khash_t(m_s2i) *hash; |
1895 | 0 | khint_t k; |
1896 | 0 | int ret = 0; |
1897 | |
|
1898 | 0 | if (rec->type == TYPEKEY("SQ")) { |
1899 | 0 | id_tag = "SN"; hash = hrecs->ref_hash; |
1900 | 0 | } else if (rec->type == TYPEKEY("RG")) { |
1901 | 0 | id_tag = "ID"; hash = hrecs->rg_hash; |
1902 | 0 | } else if (rec->type == TYPEKEY("PG")) { |
1903 | 0 | id_tag = "ID"; hash = hrecs->pg_hash; |
1904 | 0 | } else { |
1905 | 0 | return 0; |
1906 | 0 | } |
1907 | | |
1908 | 0 | memcpy(id_tag_out, id_tag, 3); |
1909 | 0 | *hash_out = hash; |
1910 | |
|
1911 | 0 | tag = sam_hrecs_find_key(rec, id_tag, &prev); |
1912 | 0 | if (!tag) |
1913 | 0 | return 0; |
1914 | 0 | assert(tag->len >= 3); |
1915 | 0 | *old_name = tag->str + 3; |
1916 | |
|
1917 | 0 | while ((key = va_arg(ap, char *)) != NULL) { |
1918 | 0 | val = va_arg(ap, char *); |
1919 | 0 | if (!val) val = ""; |
1920 | 0 | if (strcmp(key, id_tag) != 0) continue; |
1921 | 0 | if (strcmp(val, tag->str + 3) == 0) { ret = 0; continue; } |
1922 | 0 | k = kh_get(m_s2i, hash, val); |
1923 | 0 | ret = k < kh_end(hash) ? -1 : 1; |
1924 | 0 | *new_name = val; |
1925 | 0 | } |
1926 | 0 | return ret; |
1927 | 0 | } |
1928 | | |
1929 | | int sam_hdr_update_line(sam_hdr_t *bh, const char *type, |
1930 | 0 | const char *ID_key, const char *ID_value, ...) { |
1931 | 0 | sam_hrecs_t *hrecs; |
1932 | 0 | if (!bh) |
1933 | 0 | return -1; |
1934 | | |
1935 | 0 | if (!(hrecs = bh->hrecs)) { |
1936 | 0 | if (sam_hdr_fill_hrecs(bh) != 0) |
1937 | 0 | return -1; |
1938 | 0 | hrecs = bh->hrecs; |
1939 | 0 | } |
1940 | | |
1941 | 0 | int ret, rename; |
1942 | 0 | sam_hrec_type_t *ty = sam_hrecs_find_type_id(hrecs, type, ID_key, ID_value); |
1943 | 0 | if (!ty) |
1944 | 0 | return -1; |
1945 | | |
1946 | 0 | va_list args; |
1947 | 0 | const char *old_name = "?", *new_name = "?"; |
1948 | 0 | char id_tag[3]; |
1949 | 0 | khash_t(m_s2i) *hash = NULL; |
1950 | 0 | va_start(args, ID_value); |
1951 | 0 | rename = check_for_name_update(hrecs, ty, args, |
1952 | 0 | &old_name, &new_name, id_tag, &hash); |
1953 | 0 | va_end(args); |
1954 | 0 | if (rename < 0) { |
1955 | 0 | hts_log_error("Cannot rename @%s \"%s\" to \"%s\" : already exists", |
1956 | 0 | type, old_name, new_name); |
1957 | 0 | return -1; |
1958 | 0 | } |
1959 | 0 | if (rename > 0 && TYPEKEY(type) == TYPEKEY("PG")) { |
1960 | | // This is just too complicated |
1961 | 0 | hts_log_error("Renaming @PG records is not supported"); |
1962 | 0 | return -1; |
1963 | 0 | } |
1964 | 0 | va_start(args, ID_value); |
1965 | 0 | ret = sam_hrecs_vupdate(hrecs, ty, args); |
1966 | 0 | va_end(args); |
1967 | |
|
1968 | 0 | if (ret) |
1969 | 0 | return ret; |
1970 | | |
1971 | | // TODO Account for @SQ-AN altnames |
1972 | | |
1973 | 0 | if (rename) { |
1974 | | // Adjust the hash table to point to the new name |
1975 | | // sam_hrecs_update_hashes() should sort out everything else |
1976 | 0 | khint_t k = kh_get(m_s2i, hash, old_name); |
1977 | 0 | sam_hrec_tag_t *new_tag = sam_hrecs_find_key(ty, id_tag, NULL); |
1978 | 0 | int r, pos; |
1979 | 0 | assert(k < kh_end(hash)); // Or we wouldn't have found it earlier |
1980 | 0 | assert(new_tag && new_tag->str); // id_tag should exist |
1981 | 0 | assert(new_tag->len > 3); |
1982 | 0 | pos = kh_val(hash, k); |
1983 | 0 | kh_del(m_s2i, hash, k); |
1984 | 0 | k = kh_put(m_s2i, hash, new_tag->str + 3, &r); |
1985 | 0 | if (r < 1) { |
1986 | 0 | hts_log_error("Failed to rename item in hash table"); |
1987 | 0 | return -1; |
1988 | 0 | } |
1989 | 0 | kh_val(hash, k) = pos; |
1990 | 0 | } |
1991 | | |
1992 | 0 | ret = sam_hrecs_update_hashes(hrecs, TYPEKEY(type), ty); |
1993 | |
|
1994 | 0 | if (!ret && hrecs->refs_changed >= 0) |
1995 | 0 | ret = rebuild_target_arrays(bh); |
1996 | |
|
1997 | 0 | if (!ret && hrecs->dirty) |
1998 | 0 | redact_header_text(bh); |
1999 | |
|
2000 | 0 | return ret; |
2001 | 0 | } |
2002 | | |
2003 | 0 | static int rebuild_hash(sam_hrecs_t *hrecs, const char *type) { |
2004 | 0 | sam_hrec_type_t *head, *step; |
2005 | 0 | khiter_t k; |
2006 | |
|
2007 | 0 | if (strncmp(type, "SQ", 2) == 0) { |
2008 | 0 | hrecs->nref = 0; |
2009 | 0 | kh_clear(m_s2i, hrecs->ref_hash); |
2010 | 0 | } else if (strncmp(type, "RG", 2) == 0) { |
2011 | 0 | hrecs->nrg = 0; |
2012 | 0 | kh_clear(m_s2i, hrecs->rg_hash); |
2013 | 0 | } |
2014 | |
|
2015 | 0 | k = kh_get(sam_hrecs_t, hrecs->h, TYPEKEY(type)); |
2016 | |
|
2017 | 0 | if (k != kh_end(hrecs->h)) { // something to rebuild |
2018 | 0 | head = kh_val(hrecs->h, k); |
2019 | 0 | step = head; |
2020 | |
|
2021 | 0 | do { |
2022 | 0 | if (sam_hrecs_update_hashes(hrecs, TYPEKEY(type), step) == -1) { |
2023 | 0 | hts_log_error("Unable to rebuild hashes"); |
2024 | 0 | return -1; |
2025 | 0 | } |
2026 | | |
2027 | 0 | step = step->next; |
2028 | 0 | } while (step != head); |
2029 | 0 | } |
2030 | | |
2031 | 0 | return 0; |
2032 | 0 | } |
2033 | | |
2034 | 0 | int sam_hdr_remove_except(sam_hdr_t *bh, const char *type, const char *ID_key, const char *ID_value) { |
2035 | 0 | sam_hrecs_t *hrecs; |
2036 | 0 | if (!bh || !type) |
2037 | 0 | return -1; |
2038 | | |
2039 | 0 | if (!(hrecs = bh->hrecs)) { |
2040 | 0 | if (sam_hdr_fill_hrecs(bh) != 0) |
2041 | 0 | return -1; |
2042 | 0 | hrecs = bh->hrecs; |
2043 | 0 | } |
2044 | | |
2045 | 0 | sam_hrec_type_t *step; |
2046 | 0 | int ret = 1, remove_all = (ID_key == NULL); |
2047 | |
|
2048 | 0 | if (!strncmp(type, "PG", 2) || !strncmp(type, "CO", 2)) { |
2049 | 0 | hts_log_warning("Removing PG or CO lines is not supported!"); |
2050 | 0 | return -1; |
2051 | 0 | } |
2052 | | |
2053 | 0 | sam_hrec_type_t *type_found = sam_hrecs_find_type_id(hrecs, type, ID_key, ID_value); |
2054 | 0 | if (!type_found) { // remove all line of this type |
2055 | 0 | khint_t k = kh_get(sam_hrecs_t, hrecs->h, TYPEKEY(type)); |
2056 | 0 | if (k == kh_end(hrecs->h)) |
2057 | 0 | return 0; |
2058 | 0 | type_found = kh_val(hrecs->h, k); |
2059 | 0 | if (!type_found) |
2060 | 0 | return 0; |
2061 | 0 | remove_all = 1; |
2062 | 0 | } |
2063 | | |
2064 | 0 | step = type_found->next; |
2065 | 0 | while (step != type_found) { |
2066 | 0 | sam_hrec_type_t *to_remove = step; |
2067 | 0 | step = step->next; |
2068 | 0 | ret &= sam_hrecs_remove_line(hrecs, type, to_remove, 0); |
2069 | 0 | } |
2070 | |
|
2071 | 0 | if (remove_all) |
2072 | 0 | ret &= sam_hrecs_remove_line(hrecs, type, type_found, 0); |
2073 | | |
2074 | | /* if RG or SQ, delete then rebuild the hashes (as it is faster |
2075 | | to rebuild than delete one by one). |
2076 | | */ |
2077 | |
|
2078 | 0 | if ((strncmp(type, "SQ", 2) == 0) || (strncmp(type, "RG", 2) == 0)) { |
2079 | 0 | if (rebuild_hash(hrecs, type)) { |
2080 | 0 | return -1; |
2081 | 0 | } |
2082 | 0 | } |
2083 | | |
2084 | 0 | if (!ret && hrecs->dirty) |
2085 | 0 | redact_header_text(bh); |
2086 | |
|
2087 | 0 | if (strncmp(type, "SQ", 2) == 0) { |
2088 | 0 | hrecs->refs_changed = 0; // force |
2089 | 0 | if (rebuild_target_arrays(bh) != 0) |
2090 | 0 | return -1; |
2091 | 0 | } |
2092 | | |
2093 | 0 | return 0; |
2094 | 0 | } |
2095 | | |
2096 | 0 | int sam_hdr_remove_lines(sam_hdr_t *bh, const char *type, const char *id, void *vrh) { |
2097 | 0 | sam_hrecs_t *hrecs; |
2098 | 0 | rmhash_t *rh = (rmhash_t *)vrh; |
2099 | |
|
2100 | 0 | if (!bh || !type) |
2101 | 0 | return -1; |
2102 | 0 | if (!rh) // remove all lines |
2103 | 0 | return sam_hdr_remove_except(bh, type, NULL, NULL); |
2104 | 0 | if (!id) |
2105 | 0 | return -1; |
2106 | | |
2107 | 0 | if (!(hrecs = bh->hrecs)) { |
2108 | 0 | if (sam_hdr_fill_hrecs(bh) != 0) |
2109 | 0 | return -1; |
2110 | 0 | hrecs = bh->hrecs; |
2111 | 0 | } |
2112 | | |
2113 | 0 | khint_t k = kh_get(sam_hrecs_t, hrecs->h, TYPEKEY(type)); |
2114 | 0 | if (k == kh_end(hrecs->h)) // nothing to remove from |
2115 | 0 | return 0; |
2116 | | |
2117 | 0 | sam_hrec_type_t *head = kh_val(hrecs->h, k); |
2118 | 0 | if (!head) { |
2119 | 0 | hts_log_error("Header inconsistency"); |
2120 | 0 | return -1; |
2121 | 0 | } |
2122 | | |
2123 | 0 | int ret = 0; |
2124 | 0 | sam_hrec_type_t *step = head->next; |
2125 | 0 | while (step != head) { |
2126 | 0 | sam_hrec_tag_t *tag = sam_hrecs_find_key(step, id, NULL); |
2127 | 0 | if (tag && tag->str && tag->len >= 3) { |
2128 | 0 | k = kh_get(rm, rh, tag->str+3); |
2129 | 0 | if (k == kh_end(rh)) { // value is not in the hash table, so remove |
2130 | 0 | sam_hrec_type_t *to_remove = step; |
2131 | 0 | step = step->next; |
2132 | 0 | ret |= sam_hrecs_remove_line(hrecs, type, to_remove, 0); |
2133 | 0 | } else { |
2134 | 0 | step = step->next; |
2135 | 0 | } |
2136 | 0 | } else { // tag is not on the line, so skip to next line |
2137 | 0 | step = step->next; |
2138 | 0 | } |
2139 | 0 | } |
2140 | | |
2141 | | // process the first line |
2142 | 0 | sam_hrec_tag_t * tag = sam_hrecs_find_key(head, id, NULL); |
2143 | 0 | if (tag && tag->str && tag->len >= 3) { |
2144 | 0 | k = kh_get(rm, rh, tag->str+3); |
2145 | 0 | if (k == kh_end(rh)) { // value is not in the hash table, so remove |
2146 | 0 | sam_hrec_type_t *to_remove = head; |
2147 | 0 | head = head->next; |
2148 | 0 | ret |= sam_hrecs_remove_line(hrecs, type, to_remove, 0); |
2149 | 0 | } |
2150 | 0 | } |
2151 | | |
2152 | | /* if RG or SQ, delete then rebuild the hashes (as it is faster |
2153 | | to rebuild than delete one by one). |
2154 | | */ |
2155 | |
|
2156 | 0 | if ((strncmp(type, "SQ", 2) == 0) || (strncmp(type, "RG", 2) == 0)) { |
2157 | 0 | if (rebuild_hash(hrecs, type)) { |
2158 | 0 | return -1; |
2159 | 0 | } |
2160 | 0 | } |
2161 | | |
2162 | 0 | if (!ret && hrecs->dirty) |
2163 | 0 | redact_header_text(bh); |
2164 | |
|
2165 | 0 | if (strncmp(type, "SQ", 2) == 0) { |
2166 | 0 | hrecs->refs_changed = 0; // force |
2167 | 0 | if (rebuild_target_arrays(bh) != 0) |
2168 | 0 | return -1; |
2169 | 0 | } |
2170 | | |
2171 | 0 | return ret; |
2172 | 0 | } |
2173 | | |
2174 | 33.1k | int sam_hdr_count_lines(sam_hdr_t *bh, const char *type) { |
2175 | 33.1k | int count; |
2176 | 33.1k | sam_hrec_type_t *first_ty, *itr_ty; |
2177 | | |
2178 | 33.1k | if (!bh || !type) |
2179 | 0 | return -1; |
2180 | | |
2181 | 33.1k | if (!bh->hrecs) { |
2182 | 15.9k | if (sam_hdr_fill_hrecs(bh) != 0) |
2183 | 306 | return -1; |
2184 | 15.9k | } |
2185 | | |
2186 | | // Deal with types that have counts |
2187 | 32.8k | switch (type[0]) { |
2188 | 32.8k | case 'S': |
2189 | 32.8k | if (type[1] == 'Q') |
2190 | 32.8k | return bh->hrecs->nref; |
2191 | 0 | break; |
2192 | 0 | case 'R': |
2193 | 0 | if (type[1] == 'G') |
2194 | 0 | return bh->hrecs->nrg; |
2195 | 0 | break; |
2196 | 0 | case 'P': |
2197 | 0 | if (type[1] == 'G') |
2198 | 0 | return bh->hrecs->npg; |
2199 | 0 | break; |
2200 | 0 | default: |
2201 | 0 | break; |
2202 | 32.8k | } |
2203 | | |
2204 | 0 | first_ty = sam_hrecs_find_type_id(bh->hrecs, type, NULL, NULL); |
2205 | 0 | if (!first_ty) |
2206 | 0 | return 0; |
2207 | | |
2208 | 0 | count = 1; |
2209 | 0 | for (itr_ty = first_ty->next; |
2210 | 0 | itr_ty && itr_ty != first_ty; itr_ty = itr_ty->next) { |
2211 | 0 | count++; |
2212 | 0 | } |
2213 | |
|
2214 | 0 | return count; |
2215 | 0 | } |
2216 | | |
2217 | | int sam_hdr_line_index(sam_hdr_t *bh, |
2218 | | const char *type, |
2219 | 0 | const char *key) { |
2220 | 0 | sam_hrecs_t *hrecs; |
2221 | 0 | if (!bh || !type || !key) |
2222 | 0 | return -2; |
2223 | | |
2224 | 0 | if (!(hrecs = bh->hrecs)) { |
2225 | 0 | if (sam_hdr_fill_hrecs(bh) != 0) |
2226 | 0 | return -2; |
2227 | 0 | hrecs = bh->hrecs; |
2228 | 0 | } |
2229 | | |
2230 | 0 | khint_t k; |
2231 | 0 | int idx = -1; |
2232 | 0 | switch (type[0]) { |
2233 | 0 | case 'S': |
2234 | 0 | if (type[1] == 'Q') { |
2235 | 0 | k = kh_get(m_s2i, hrecs->ref_hash, key); |
2236 | 0 | if (k != kh_end(hrecs->ref_hash)) |
2237 | 0 | idx = kh_val(hrecs->ref_hash, k); |
2238 | 0 | } else { |
2239 | 0 | hts_log_warning("Type '%s' not supported. Only @SQ, @RG and @PG lines are indexed", type); |
2240 | 0 | } |
2241 | 0 | break; |
2242 | 0 | case 'R': |
2243 | 0 | if (type[1] == 'G') { |
2244 | 0 | k = kh_get(m_s2i, hrecs->rg_hash, key); |
2245 | 0 | if (k != kh_end(hrecs->rg_hash)) |
2246 | 0 | idx = kh_val(hrecs->rg_hash, k); |
2247 | 0 | } else { |
2248 | 0 | hts_log_warning("Type '%s' not supported. Only @SQ, @RG and @PG lines are indexed", type); |
2249 | 0 | } |
2250 | 0 | break; |
2251 | 0 | case 'P': |
2252 | 0 | if (type[1] == 'G') { |
2253 | 0 | k = kh_get(m_s2i, hrecs->pg_hash, key); |
2254 | 0 | if (k != kh_end(hrecs->pg_hash)) |
2255 | 0 | idx = kh_val(hrecs->pg_hash, k); |
2256 | 0 | } else { |
2257 | 0 | hts_log_warning("Type '%s' not supported. Only @SQ, @RG and @PG lines are indexed", type); |
2258 | 0 | } |
2259 | 0 | break; |
2260 | 0 | default: |
2261 | 0 | hts_log_warning("Type '%s' not supported. Only @SQ, @RG and @PG lines are indexed", type); |
2262 | 0 | } |
2263 | | |
2264 | 0 | return idx; |
2265 | 0 | } |
2266 | | |
2267 | | const char *sam_hdr_line_name(sam_hdr_t *bh, |
2268 | | const char *type, |
2269 | 0 | int pos) { |
2270 | 0 | sam_hrecs_t *hrecs; |
2271 | 0 | if (!bh || !type || pos < 0) |
2272 | 0 | return NULL; |
2273 | | |
2274 | 0 | if (!(hrecs = bh->hrecs)) { |
2275 | 0 | if (sam_hdr_fill_hrecs(bh) != 0) |
2276 | 0 | return NULL; |
2277 | 0 | hrecs = bh->hrecs; |
2278 | 0 | } |
2279 | | |
2280 | 0 | switch (type[0]) { |
2281 | 0 | case 'S': |
2282 | 0 | if (type[1] == 'Q') { |
2283 | 0 | if (pos < hrecs->nref) |
2284 | 0 | return hrecs->ref[pos].name; |
2285 | 0 | } else { |
2286 | 0 | hts_log_warning("Type '%s' not supported. Only @SQ, @RG and @PG lines are indexed", type); |
2287 | 0 | } |
2288 | 0 | break; |
2289 | 0 | case 'R': |
2290 | 0 | if (type[1] == 'G') { |
2291 | 0 | if (pos < hrecs->nrg) |
2292 | 0 | return hrecs->rg[pos].name; |
2293 | 0 | } else { |
2294 | 0 | hts_log_warning("Type '%s' not supported. Only @SQ, @RG and @PG lines are indexed", type); |
2295 | 0 | } |
2296 | 0 | break; |
2297 | 0 | case 'P': |
2298 | 0 | if (type[1] == 'G') { |
2299 | 0 | if (pos < hrecs->npg) |
2300 | 0 | return hrecs->pg[pos].name; |
2301 | 0 | } else { |
2302 | 0 | hts_log_warning("Type '%s' not supported. Only @SQ, @RG and @PG lines are indexed", type); |
2303 | 0 | } |
2304 | 0 | break; |
2305 | 0 | default: |
2306 | 0 | hts_log_warning("Type '%s' not supported. Only @SQ, @RG and @PG lines are indexed", type); |
2307 | 0 | } |
2308 | | |
2309 | 0 | return NULL; |
2310 | 0 | } |
2311 | | |
2312 | | /* ==== Key:val level methods ==== */ |
2313 | | |
2314 | | int sam_hdr_find_tag_id(sam_hdr_t *bh, |
2315 | | const char *type, |
2316 | | const char *ID_key, |
2317 | | const char *ID_value, |
2318 | | const char *key, |
2319 | 0 | kstring_t *ks) { |
2320 | 0 | sam_hrecs_t *hrecs; |
2321 | 0 | if (!bh || !type || !key) |
2322 | 0 | return -2; |
2323 | | |
2324 | 0 | if (!(hrecs = bh->hrecs)) { |
2325 | 0 | if (sam_hdr_fill_hrecs(bh) != 0) |
2326 | 0 | return -2; |
2327 | 0 | hrecs = bh->hrecs; |
2328 | 0 | } |
2329 | | |
2330 | 0 | sam_hrec_type_t *ty = sam_hrecs_find_type_id(hrecs, type, ID_key, ID_value); |
2331 | 0 | if (!ty) |
2332 | 0 | return -1; |
2333 | | |
2334 | 0 | sam_hrec_tag_t *tag = sam_hrecs_find_key(ty, key, NULL); |
2335 | 0 | if (!tag || !tag->str || tag->len < 4) |
2336 | 0 | return -1; |
2337 | | |
2338 | 0 | ks->l = 0; |
2339 | 0 | if (kputsn(tag->str+3, tag->len-3, ks) == EOF) { |
2340 | 0 | return -2; |
2341 | 0 | } |
2342 | | |
2343 | 0 | return 0; |
2344 | 0 | } |
2345 | | |
2346 | | int sam_hdr_find_tag_pos(sam_hdr_t *bh, |
2347 | | const char *type, |
2348 | | int pos, |
2349 | | const char *key, |
2350 | 0 | kstring_t *ks) { |
2351 | 0 | sam_hrecs_t *hrecs; |
2352 | 0 | if (!bh || !type || !key) |
2353 | 0 | return -2; |
2354 | | |
2355 | 0 | if (!(hrecs = bh->hrecs)) { |
2356 | 0 | if (sam_hdr_fill_hrecs(bh) != 0) |
2357 | 0 | return -2; |
2358 | 0 | hrecs = bh->hrecs; |
2359 | 0 | } |
2360 | | |
2361 | 0 | sam_hrec_type_t *ty = sam_hrecs_find_type_pos(hrecs, type, pos); |
2362 | 0 | if (!ty) |
2363 | 0 | return -1; |
2364 | | |
2365 | 0 | sam_hrec_tag_t *tag = sam_hrecs_find_key(ty, key, NULL); |
2366 | 0 | if (!tag || !tag->str || tag->len < 4) |
2367 | 0 | return -1; |
2368 | | |
2369 | 0 | ks->l = 0; |
2370 | 0 | if (kputsn(tag->str+3, tag->len-3, ks) == EOF) { |
2371 | 0 | return -2; |
2372 | 0 | } |
2373 | | |
2374 | 0 | return 0; |
2375 | 0 | } |
2376 | | |
2377 | | int sam_hdr_remove_tag_id(sam_hdr_t *bh, |
2378 | | const char *type, |
2379 | | const char *ID_key, |
2380 | | const char *ID_value, |
2381 | 0 | const char *key) { |
2382 | 0 | sam_hrecs_t *hrecs; |
2383 | 0 | if (!bh || !type || !key) |
2384 | 0 | return -1; |
2385 | | |
2386 | | // Disallow @SQ SN:, @RG ID: and @PG ID: |
2387 | 0 | if (strcmp(key, strcmp(type, "SQ")==0 ? "SN" : "ID") == 0) |
2388 | 0 | return -1; |
2389 | | |
2390 | 0 | if (!(hrecs = bh->hrecs)) { |
2391 | 0 | if (sam_hdr_fill_hrecs(bh) != 0) |
2392 | 0 | return -1; |
2393 | 0 | hrecs = bh->hrecs; |
2394 | 0 | } |
2395 | | |
2396 | 0 | sam_hrec_type_t *ty = sam_hrecs_find_type_id(hrecs, type, ID_key, ID_value); |
2397 | 0 | if (!ty) |
2398 | 0 | return -1; |
2399 | | |
2400 | 0 | int ret = sam_hrecs_remove_key(hrecs, ty, key); |
2401 | 0 | if (!ret && hrecs->dirty) |
2402 | 0 | redact_header_text(bh); |
2403 | |
|
2404 | 0 | return ret; |
2405 | 0 | } |
2406 | | |
2407 | | /* |
2408 | | * Reconstructs a kstring from the header hash table. |
2409 | | * Returns 0 on success |
2410 | | * -1 on failure |
2411 | | */ |
2412 | 73.2k | int sam_hrecs_rebuild_text(const sam_hrecs_t *hrecs, kstring_t *ks) { |
2413 | 73.2k | ks->l = 0; |
2414 | | |
2415 | 73.2k | if (!hrecs->h || !hrecs->h->size) { |
2416 | 35.9k | return kputsn("", 0, ks) >= 0 ? 0 : -1; |
2417 | 35.9k | } |
2418 | 37.2k | if (sam_hrecs_rebuild_lines(hrecs, ks) != 0) |
2419 | 0 | return -1; |
2420 | | |
2421 | 37.2k | return 0; |
2422 | 37.2k | } |
2423 | | |
2424 | | /* |
2425 | | * Looks up a reference sequence by name and returns the numerical ID. |
2426 | | * Returns -1 if unknown reference; -2 if header could not be parsed. |
2427 | | */ |
2428 | 797k | int sam_hdr_name2tid(sam_hdr_t *bh, const char *ref) { |
2429 | 797k | sam_hrecs_t *hrecs; |
2430 | 797k | khint_t k; |
2431 | | |
2432 | 797k | if (!bh) |
2433 | 0 | return -1; |
2434 | | |
2435 | 797k | if (!(hrecs = bh->hrecs)) { |
2436 | 0 | if (sam_hdr_fill_hrecs(bh) != 0) |
2437 | 0 | return -2; |
2438 | 0 | hrecs = bh->hrecs; |
2439 | 0 | } |
2440 | | |
2441 | 797k | if (!hrecs->ref_hash) |
2442 | 0 | return -1; |
2443 | | |
2444 | 797k | k = kh_get(m_s2i, hrecs->ref_hash, ref); |
2445 | 797k | return k == kh_end(hrecs->ref_hash) ? -1 : kh_val(hrecs->ref_hash, k); |
2446 | 797k | } |
2447 | | |
2448 | 0 | const char *sam_hdr_tid2name(const sam_hdr_t *h, int tid) { |
2449 | 0 | sam_hrecs_t *hrecs; |
2450 | |
|
2451 | 0 | if (!h || tid < 0) |
2452 | 0 | return NULL; |
2453 | | |
2454 | 0 | if ((hrecs = h->hrecs) != NULL && tid < hrecs->nref) { |
2455 | 0 | return hrecs->ref[tid].name; |
2456 | 0 | } else { |
2457 | 0 | if (tid < h->n_targets) |
2458 | 0 | return h->target_name[tid]; |
2459 | 0 | } |
2460 | | |
2461 | 0 | return NULL; |
2462 | 0 | } |
2463 | | |
2464 | 0 | hts_pos_t sam_hdr_tid2len(const sam_hdr_t *h, int tid) { |
2465 | 0 | sam_hrecs_t *hrecs; |
2466 | |
|
2467 | 0 | if (!h || tid < 0) |
2468 | 0 | return 0; |
2469 | | |
2470 | 0 | if ((hrecs = h->hrecs) != NULL && tid < hrecs->nref) { |
2471 | 0 | return hrecs->ref[tid].len; |
2472 | 0 | } else { |
2473 | 0 | if (tid < h->n_targets) { |
2474 | 0 | if (h->target_len[tid] < UINT32_MAX || !h->sdict) { |
2475 | 0 | return h->target_len[tid]; |
2476 | 0 | } else { |
2477 | 0 | khash_t(s2i) *long_refs = (khash_t(s2i) *) h->sdict; |
2478 | 0 | khint_t k = kh_get(s2i, long_refs, h->target_name[tid]); |
2479 | 0 | if (k < kh_end(long_refs)) { |
2480 | 0 | return kh_val(long_refs, k); |
2481 | 0 | } else { |
2482 | 0 | return UINT32_MAX; |
2483 | 0 | } |
2484 | 0 | } |
2485 | 0 | } |
2486 | 0 | } |
2487 | | |
2488 | 0 | return 0; |
2489 | 0 | } |
2490 | | |
2491 | | /* |
2492 | | * Fixes any PP links in @PG headers. |
2493 | | * If the entries are in order then this doesn't need doing, but in case |
2494 | | * our header is out of order this goes through the hrecs->pg[] array |
2495 | | * setting the prev_id field. |
2496 | | * |
2497 | | * Note we can have multiple complete chains. This code should identify the |
2498 | | * tails of these chains as these are the entries we have to link to in |
2499 | | * subsequent PP records. |
2500 | | * |
2501 | | * Returns 0 on success |
2502 | | * -1 on failure (indicating broken PG/PP records) |
2503 | | */ |
2504 | 0 | static int sam_hdr_link_pg(sam_hdr_t *bh) { |
2505 | 0 | sam_hrecs_t *hrecs; |
2506 | 0 | int i, j, ret = 0, *new_pg_end; |
2507 | |
|
2508 | 0 | if (!bh) |
2509 | 0 | return -1; |
2510 | | |
2511 | 0 | if (!(hrecs = bh->hrecs)) { |
2512 | 0 | if (sam_hdr_fill_hrecs(bh) != 0) |
2513 | 0 | return -1; |
2514 | 0 | hrecs = bh->hrecs; |
2515 | 0 | } |
2516 | | |
2517 | 0 | if (!hrecs->pgs_changed || !hrecs->npg) |
2518 | 0 | return 0; |
2519 | | |
2520 | 0 | hrecs->npg_end_alloc = hrecs->npg; |
2521 | 0 | new_pg_end = hts_realloc_p(hrecs->pg_end, sizeof(*new_pg_end), hrecs->npg); |
2522 | 0 | if (!new_pg_end) |
2523 | 0 | return -1; |
2524 | 0 | hrecs->pg_end = new_pg_end; |
2525 | 0 | int *chain_size = calloc(hrecs->npg, sizeof(int)); |
2526 | 0 | if (!chain_size) |
2527 | 0 | return -1; |
2528 | | |
2529 | 0 | for (i = 0; i < hrecs->npg; i++) |
2530 | 0 | hrecs->pg_end[i] = i; |
2531 | |
|
2532 | 0 | for (i = 0; i < hrecs->npg; i++) { |
2533 | 0 | khint_t k; |
2534 | 0 | sam_hrec_tag_t *tag; |
2535 | |
|
2536 | 0 | assert(hrecs->pg[i].ty != NULL); |
2537 | 0 | for (tag = hrecs->pg[i].ty->tag; tag; tag = tag->next) { |
2538 | 0 | if (tag->str[0] == 'P' && tag->str[1] == 'P') |
2539 | 0 | break; |
2540 | 0 | } |
2541 | 0 | if (!tag) { |
2542 | | /* Chain start points */ |
2543 | 0 | continue; |
2544 | 0 | } |
2545 | | |
2546 | 0 | k = kh_get(m_s2i, hrecs->pg_hash, tag->str+3); |
2547 | |
|
2548 | 0 | if (k == kh_end(hrecs->pg_hash)) { |
2549 | 0 | hts_log_warning("PG line with ID:%s has a PP link to missing program '%s'", |
2550 | 0 | hrecs->pg[i].name, tag->str+3); |
2551 | 0 | continue; |
2552 | 0 | } |
2553 | | |
2554 | 0 | int pp_idx = kh_val(hrecs->pg_hash, k); |
2555 | 0 | if (pp_idx == i) { |
2556 | 0 | hts_log_warning("PG line with ID:%s has a PP link to itself", |
2557 | 0 | hrecs->pg[i].name); |
2558 | 0 | continue; |
2559 | 0 | } |
2560 | | |
2561 | 0 | hrecs->pg[i].prev_id = hrecs->pg[pp_idx].id; |
2562 | 0 | hrecs->pg_end[pp_idx] = -1; |
2563 | 0 | chain_size[i] = chain_size[pp_idx]+1; |
2564 | 0 | } |
2565 | | |
2566 | 0 | int last_end = -1; |
2567 | 0 | for (i = j = 0; i < hrecs->npg; i++) { |
2568 | 0 | if (hrecs->pg_end[i] != -1) { |
2569 | 0 | last_end = hrecs->pg_end[i]; |
2570 | 0 | if (chain_size[i] > 0) |
2571 | 0 | hrecs->pg_end[j++] = hrecs->pg_end[i]; |
2572 | 0 | } |
2573 | 0 | } |
2574 | | /* Only leafs? Choose the last one! */ |
2575 | 0 | if (!j && hrecs->npg_end > 0 && last_end >= 0) { |
2576 | 0 | hrecs->pg_end[0] = last_end; |
2577 | 0 | j = 1; |
2578 | 0 | } |
2579 | |
|
2580 | 0 | hrecs->npg_end = j; |
2581 | 0 | hrecs->pgs_changed = 0; |
2582 | | |
2583 | | /* mark as dirty or empty for rebuild */ |
2584 | 0 | hrecs->dirty = 1; |
2585 | 0 | redact_header_text(bh); |
2586 | 0 | free(chain_size); |
2587 | |
|
2588 | 0 | return ret; |
2589 | 0 | } |
2590 | | |
2591 | | /* |
2592 | | * Returns a unique ID from a base name. |
2593 | | * |
2594 | | * The value returned is valid until the next call to |
2595 | | * this function. |
2596 | | */ |
2597 | 0 | const char *sam_hdr_pg_id(sam_hdr_t *bh, const char *name) { |
2598 | 0 | sam_hrecs_t *hrecs; |
2599 | 0 | size_t name_len; |
2600 | 0 | const size_t name_extra = 17; |
2601 | 0 | if (!bh || !name) |
2602 | 0 | return NULL; |
2603 | | |
2604 | 0 | if (!(hrecs = bh->hrecs)) { |
2605 | 0 | if (sam_hdr_fill_hrecs(bh) != 0) |
2606 | 0 | return NULL; |
2607 | 0 | hrecs = bh->hrecs; |
2608 | 0 | } |
2609 | | |
2610 | 0 | khint_t k = kh_get(m_s2i, hrecs->pg_hash, name); |
2611 | 0 | if (k == kh_end(hrecs->pg_hash)) |
2612 | 0 | return name; |
2613 | | |
2614 | 0 | name_len = strlen(name); |
2615 | 0 | if (name_len > 1000) name_len = 1000; |
2616 | 0 | if (hrecs->ID_buf_sz < name_len + name_extra) { |
2617 | 0 | char *new_ID_buf = realloc(hrecs->ID_buf, |
2618 | 0 | hts_add_sat2(name_len, name_extra)); |
2619 | 0 | if (new_ID_buf == NULL) |
2620 | 0 | return NULL; |
2621 | 0 | hrecs->ID_buf = new_ID_buf; |
2622 | 0 | hrecs->ID_buf_sz = name_len + name_extra; |
2623 | 0 | } |
2624 | | |
2625 | 0 | do { |
2626 | 0 | snprintf(hrecs->ID_buf, hrecs->ID_buf_sz, "%.1000s.%d", name, hrecs->ID_cnt++); |
2627 | 0 | k = kh_get(m_s2i, hrecs->pg_hash, hrecs->ID_buf); |
2628 | 0 | } while (k != kh_end(hrecs->pg_hash)); |
2629 | |
|
2630 | 0 | return hrecs->ID_buf; |
2631 | 0 | } |
2632 | | |
2633 | | /* |
2634 | | * Add an @PG line. |
2635 | | * |
2636 | | * If we wish complete control over this use sam_hdr_add_line() directly. This |
2637 | | * function uses that, but attempts to do a lot of tedious house work for |
2638 | | * you too. |
2639 | | * |
2640 | | * - It will generate a suitable ID if the supplied one clashes. |
2641 | | * - It will generate multiple @PG records if we have multiple PG chains. |
2642 | | * |
2643 | | * Call it as per sam_hdr_add_line() with a series of key,value pairs ending |
2644 | | * in NULL. |
2645 | | * |
2646 | | * Returns 0 on success |
2647 | | * -1 on failure |
2648 | | */ |
2649 | 0 | int sam_hdr_add_pg(sam_hdr_t *bh, const char *name, ...) { |
2650 | 0 | sam_hrecs_t *hrecs; |
2651 | 0 | const char *specified_id = NULL, *specified_pn = NULL, *specified_pp = NULL; |
2652 | 0 | const char *key, *val; |
2653 | 0 | if (!bh) |
2654 | 0 | return -1; |
2655 | | |
2656 | 0 | if (!(hrecs = bh->hrecs)) { |
2657 | 0 | if (sam_hdr_fill_hrecs(bh) != 0) |
2658 | 0 | return -1; |
2659 | 0 | hrecs = bh->hrecs; |
2660 | 0 | } |
2661 | | |
2662 | 0 | bh->hrecs->pgs_changed = 1; |
2663 | 0 | if (sam_hdr_link_pg(bh) < 0) { |
2664 | 0 | hts_log_error("Error linking @PG lines"); |
2665 | 0 | return -1; |
2666 | 0 | } |
2667 | | |
2668 | 0 | va_list args; |
2669 | | // Check for ID / PN / PP tags in varargs list |
2670 | 0 | va_start(args, name); |
2671 | 0 | while ((key = va_arg(args, const char *)) != NULL) { |
2672 | 0 | val = va_arg(args, const char *); |
2673 | 0 | if (!val) break; |
2674 | 0 | if (strcmp(key, "PN") == 0 && *val != '\0') |
2675 | 0 | specified_pn = val; |
2676 | 0 | else if (strcmp(key, "PP") == 0 && *val != '\0') |
2677 | 0 | specified_pp = val; |
2678 | 0 | else if (strcmp(key, "ID") == 0 && *val != '\0') |
2679 | 0 | specified_id = val; |
2680 | 0 | } |
2681 | 0 | va_end(args); |
2682 | |
|
2683 | 0 | if (specified_id && hrecs->pg_hash) { |
2684 | 0 | khint_t k = kh_get(m_s2i, hrecs->pg_hash, specified_id); |
2685 | 0 | if (k != kh_end(hrecs->pg_hash)) { |
2686 | 0 | hts_log_error("Header @PG ID:%s already present", specified_id); |
2687 | 0 | return -1; |
2688 | 0 | } |
2689 | 0 | } |
2690 | | |
2691 | 0 | if (specified_pp && hrecs->pg_hash) { |
2692 | 0 | khint_t k = kh_get(m_s2i, hrecs->pg_hash, specified_pp); |
2693 | 0 | if (k == kh_end(hrecs->pg_hash)) { |
2694 | 0 | hts_log_error("Header @PG ID:%s referred to by PP tag not present", |
2695 | 0 | specified_pp); |
2696 | 0 | return -1; |
2697 | 0 | } |
2698 | 0 | } |
2699 | | |
2700 | 0 | if (!specified_pp && hrecs->npg_end) { |
2701 | | /* Copy ends array to avoid us looping while modifying it */ |
2702 | 0 | int *end = hts_malloc_p(sizeof(int), hrecs->npg_end); |
2703 | 0 | int i, nends = hrecs->npg_end; |
2704 | |
|
2705 | 0 | if (!end) |
2706 | 0 | return -1; |
2707 | | |
2708 | 0 | memcpy(end, hrecs->pg_end, nends * sizeof(*end)); |
2709 | |
|
2710 | 0 | for (i = 0; i < nends; i++) { |
2711 | 0 | const char *id = !specified_id ? sam_hdr_pg_id(bh, name) : ""; |
2712 | 0 | if (!id) { |
2713 | 0 | free(end); |
2714 | 0 | return -1; |
2715 | 0 | } |
2716 | 0 | assert(end[i] >= 0 && end[i] < hrecs->npg); |
2717 | 0 | va_start(args, name); |
2718 | 0 | if (-1 == sam_hrecs_vadd(hrecs, "PG", args, |
2719 | 0 | "ID", id, |
2720 | 0 | "PN", !specified_pn ? name : "", |
2721 | 0 | "PP", hrecs->pg[end[i]].name, |
2722 | 0 | NULL)) { |
2723 | 0 | free(end); |
2724 | 0 | return -1; |
2725 | 0 | } |
2726 | 0 | va_end(args); |
2727 | 0 | } |
2728 | | |
2729 | 0 | free(end); |
2730 | 0 | } else { |
2731 | 0 | const char *id = !specified_id ? sam_hdr_pg_id(bh, name) : ""; |
2732 | 0 | if (!id) |
2733 | 0 | return -1; |
2734 | 0 | va_start(args, name); |
2735 | 0 | if (-1 == sam_hrecs_vadd(hrecs, "PG", args, |
2736 | 0 | "ID", id, |
2737 | 0 | "PN", !specified_pn ? name : "", |
2738 | 0 | NULL)) |
2739 | 0 | return -1; |
2740 | 0 | va_end(args); |
2741 | 0 | } |
2742 | | |
2743 | 0 | hrecs->dirty = 1; |
2744 | 0 | redact_header_text(bh); |
2745 | |
|
2746 | 0 | return 0; |
2747 | 0 | } |
2748 | | |
2749 | | /*! Increments a reference count on bh. |
2750 | | * |
2751 | | * This permits multiple files to share the same header, all calling |
2752 | | * sam_hdr_destroy when done, without causing errors for other open files. |
2753 | | */ |
2754 | 10.5k | void sam_hdr_incr_ref(sam_hdr_t *bh) { |
2755 | 10.5k | if (!bh) |
2756 | 0 | return; |
2757 | 10.5k | bh->ref_count++; |
2758 | 10.5k | } |
2759 | | |
2760 | | /* ==== Internal methods ==== */ |
2761 | | |
2762 | | /* |
2763 | | * Creates an empty SAM header. Allocates space for the SAM header |
2764 | | * structures (hash tables) ready to be populated. |
2765 | | * |
2766 | | * Returns a sam_hrecs_t struct on success (free with sam_hrecs_free()) |
2767 | | * NULL on failure |
2768 | | */ |
2769 | 55.5k | sam_hrecs_t *sam_hrecs_new(void) { |
2770 | 55.5k | sam_hrecs_t *hrecs = calloc(1, sizeof(*hrecs)); |
2771 | | |
2772 | 55.5k | if (!hrecs) |
2773 | 0 | return NULL; |
2774 | | |
2775 | 55.5k | hrecs->h = kh_init(sam_hrecs_t); |
2776 | 55.5k | if (!hrecs->h) |
2777 | 0 | goto err; |
2778 | | |
2779 | 55.5k | hrecs->ID_cnt = 1; |
2780 | | |
2781 | 55.5k | hrecs->nref = 0; |
2782 | 55.5k | hrecs->ref_sz = 0; |
2783 | 55.5k | hrecs->ref = NULL; |
2784 | 55.5k | if (!(hrecs->ref_hash = kh_init(m_s2i))) |
2785 | 0 | goto err; |
2786 | 55.5k | hrecs->refs_changed = -1; |
2787 | | |
2788 | 55.5k | hrecs->nrg = 0; |
2789 | 55.5k | hrecs->rg_sz = 0; |
2790 | 55.5k | hrecs->rg = NULL; |
2791 | 55.5k | if (!(hrecs->rg_hash = kh_init(m_s2i))) |
2792 | 0 | goto err; |
2793 | | |
2794 | 55.5k | hrecs->npg = 0; |
2795 | 55.5k | hrecs->pg_sz = 0; |
2796 | 55.5k | hrecs->pg = NULL; |
2797 | 55.5k | hrecs->npg_end = hrecs->npg_end_alloc = 0; |
2798 | 55.5k | hrecs->pg_end = NULL; |
2799 | 55.5k | if (!(hrecs->pg_hash = kh_init(m_s2i))) |
2800 | 0 | goto err; |
2801 | | |
2802 | 55.5k | if (!(hrecs->tag_pool = pool_create(sizeof(sam_hrec_tag_t)))) |
2803 | 0 | goto err; |
2804 | | |
2805 | 55.5k | if (!(hrecs->type_pool = pool_create(sizeof(sam_hrec_type_t)))) |
2806 | 0 | goto err; |
2807 | | |
2808 | 55.5k | if (!(hrecs->str_pool = string_pool_create(65536))) |
2809 | 0 | goto err; |
2810 | | |
2811 | 55.5k | if (sam_hrecs_init_type_order(hrecs, NULL)) |
2812 | 0 | goto err; |
2813 | | |
2814 | 55.5k | return hrecs; |
2815 | | |
2816 | 0 | err: |
2817 | 0 | if (hrecs->h) |
2818 | 0 | kh_destroy(sam_hrecs_t, hrecs->h); |
2819 | |
|
2820 | 0 | if (hrecs->tag_pool) |
2821 | 0 | pool_destroy(hrecs->tag_pool); |
2822 | |
|
2823 | 0 | if (hrecs->type_pool) |
2824 | 0 | pool_destroy(hrecs->type_pool); |
2825 | |
|
2826 | 0 | if (hrecs->str_pool) |
2827 | 0 | string_pool_destroy(hrecs->str_pool); |
2828 | |
|
2829 | 0 | free(hrecs); |
2830 | |
|
2831 | 0 | return NULL; |
2832 | 55.5k | } |
2833 | | #if 0 |
2834 | | /* |
2835 | | * Produces a duplicate copy of source and returns it. |
2836 | | * Returns NULL on failure |
2837 | | */ |
2838 | | sam_hrecs_t *sam_hrecs_dup(sam_hrecs_t *source) { |
2839 | | return NULL; |
2840 | | } |
2841 | | #endif |
2842 | | /*! Deallocates all storage used by a sam_hrecs_t struct. |
2843 | | * |
2844 | | * This also decrements the header reference count. If after decrementing |
2845 | | * it is still non-zero then the header is assumed to be in use by another |
2846 | | * caller and the free is not done. |
2847 | | * |
2848 | | */ |
2849 | 55.5k | void sam_hrecs_free(sam_hrecs_t *hrecs) { |
2850 | 55.5k | if (!hrecs) |
2851 | 0 | return; |
2852 | | |
2853 | 55.5k | if (hrecs->h) |
2854 | 55.5k | kh_destroy(sam_hrecs_t, hrecs->h); |
2855 | | |
2856 | 55.5k | if (hrecs->ref_hash) |
2857 | 55.5k | kh_destroy(m_s2i, hrecs->ref_hash); |
2858 | | |
2859 | 55.5k | if (hrecs->ref) |
2860 | 18.0k | free(hrecs->ref); |
2861 | | |
2862 | 55.5k | if (hrecs->rg_hash) |
2863 | 55.5k | kh_destroy(m_s2i, hrecs->rg_hash); |
2864 | | |
2865 | 55.5k | if (hrecs->rg) |
2866 | 1.36k | free(hrecs->rg); |
2867 | | |
2868 | 55.5k | if (hrecs->pg_hash) |
2869 | 55.5k | kh_destroy(m_s2i, hrecs->pg_hash); |
2870 | | |
2871 | 55.5k | if (hrecs->pg) |
2872 | 852 | free(hrecs->pg); |
2873 | | |
2874 | 55.5k | if (hrecs->pg_end) |
2875 | 818 | free(hrecs->pg_end); |
2876 | | |
2877 | 55.5k | if (hrecs->type_pool) |
2878 | 55.5k | pool_destroy(hrecs->type_pool); |
2879 | | |
2880 | 55.5k | if (hrecs->tag_pool) |
2881 | 55.5k | pool_destroy(hrecs->tag_pool); |
2882 | | |
2883 | 55.5k | if (hrecs->str_pool) |
2884 | 55.5k | string_pool_destroy(hrecs->str_pool); |
2885 | | |
2886 | 55.5k | if (hrecs->type_order) |
2887 | 55.5k | free(hrecs->type_order); |
2888 | | |
2889 | 55.5k | if (hrecs->ID_buf) |
2890 | 0 | free(hrecs->ID_buf); |
2891 | | |
2892 | 55.5k | free(hrecs); |
2893 | 55.5k | } |
2894 | | |
2895 | | /* |
2896 | | * Internal method already used by the CRAM code |
2897 | | * Returns the first header item matching 'type'. If ID is non-NULL it checks |
2898 | | * for the tag ID: and compares against the specified ID. |
2899 | | * |
2900 | | * Returns NULL if no type/ID is found |
2901 | | */ |
2902 | | sam_hrec_type_t *sam_hrecs_find_type_id(sam_hrecs_t *hrecs, const char *type, |
2903 | 15.8k | const char *ID_key, const char *ID_value) { |
2904 | 15.8k | if (!hrecs || !type) |
2905 | 0 | return NULL; |
2906 | 15.8k | sam_hrec_type_t *t1, *t2; |
2907 | 15.8k | khint_t k; |
2908 | | |
2909 | | /* Special case for types we have prebuilt hashes on */ |
2910 | 15.8k | if (ID_key) { |
2911 | 15.8k | if (!ID_value) |
2912 | 0 | return NULL; |
2913 | | |
2914 | 15.8k | if (type[0] == 'S' && type[1] == 'Q' && |
2915 | 15.8k | ID_key[0] == 'S' && ID_key[1] == 'N') { |
2916 | 15.8k | k = kh_get(m_s2i, hrecs->ref_hash, ID_value); |
2917 | 15.8k | return k != kh_end(hrecs->ref_hash) |
2918 | 15.8k | ? hrecs->ref[kh_val(hrecs->ref_hash, k)].ty |
2919 | 15.8k | : NULL; |
2920 | 15.8k | } |
2921 | | |
2922 | 0 | if (type[0] == 'R' && type[1] == 'G' && |
2923 | 0 | ID_key[0] == 'I' && ID_key[1] == 'D') { |
2924 | 0 | k = kh_get(m_s2i, hrecs->rg_hash, ID_value); |
2925 | 0 | return k != kh_end(hrecs->rg_hash) |
2926 | 0 | ? hrecs->rg[kh_val(hrecs->rg_hash, k)].ty |
2927 | 0 | : NULL; |
2928 | 0 | } |
2929 | | |
2930 | 0 | if (type[0] == 'P' && type[1] == 'G' && |
2931 | 0 | ID_key[0] == 'I' && ID_key[1] == 'D') { |
2932 | 0 | k = kh_get(m_s2i, hrecs->pg_hash, ID_value); |
2933 | 0 | return k != kh_end(hrecs->pg_hash) |
2934 | 0 | ? hrecs->pg[kh_val(hrecs->pg_hash, k)].ty |
2935 | 0 | : NULL; |
2936 | 0 | } |
2937 | 0 | } |
2938 | | |
2939 | 0 | k = kh_get(sam_hrecs_t, hrecs->h, TYPEKEY(type)); |
2940 | 0 | if (k == kh_end(hrecs->h)) |
2941 | 0 | return NULL; |
2942 | | |
2943 | 0 | if (!ID_key) |
2944 | 0 | return kh_val(hrecs->h, k); |
2945 | | |
2946 | 0 | t1 = t2 = kh_val(hrecs->h, k); |
2947 | 0 | do { |
2948 | 0 | sam_hrec_tag_t *tag; |
2949 | 0 | for (tag = t1->tag; tag; tag = tag->next) { |
2950 | 0 | if (tag->str[0] == ID_key[0] && tag->str[1] == ID_key[1]) { |
2951 | 0 | const char *cp1 = tag->str+3; |
2952 | 0 | const char *cp2 = ID_value; |
2953 | 0 | while (*cp1 && *cp1 == *cp2) |
2954 | 0 | cp1++, cp2++; |
2955 | 0 | if (*cp2 || *cp1) |
2956 | 0 | continue; |
2957 | 0 | return t1; |
2958 | 0 | } |
2959 | 0 | } |
2960 | 0 | t1 = t1->next; |
2961 | 0 | } while (t1 != t2); |
2962 | | |
2963 | 0 | return NULL; |
2964 | 0 | } |
2965 | | |
2966 | | /* |
2967 | | * Adds or updates tag key,value pairs in a header line. |
2968 | | * Eg for adding M5 tags to @SQ lines or updating sort order for the |
2969 | | * @HD line. |
2970 | | * |
2971 | | * va_list contains multiple key,value pairs ending in NULL. |
2972 | | * |
2973 | | * Returns 0 on success |
2974 | | * -1 on failure |
2975 | | */ |
2976 | 4 | int sam_hrecs_vupdate(sam_hrecs_t *hrecs, sam_hrec_type_t *type, va_list ap) { |
2977 | 4 | if (!hrecs) |
2978 | 0 | return -1; |
2979 | | |
2980 | 8 | for (;;) { |
2981 | 8 | char *k, *v, *str; |
2982 | 8 | sam_hrec_tag_t *tag, *prev = NULL; |
2983 | | |
2984 | 8 | if (!(k = (char *)va_arg(ap, char *))) |
2985 | 4 | break; |
2986 | 4 | if (!(v = va_arg(ap, char *))) |
2987 | 0 | v = ""; |
2988 | | |
2989 | 4 | tag = sam_hrecs_find_key(type, k, &prev); |
2990 | 4 | if (!tag) { |
2991 | 0 | if (!(tag = pool_alloc(hrecs->tag_pool))) |
2992 | 0 | return -1; |
2993 | 0 | if (prev) |
2994 | 0 | prev->next = tag; |
2995 | 0 | else |
2996 | 0 | type->tag = tag; |
2997 | |
|
2998 | 0 | tag->next = NULL; |
2999 | 0 | } |
3000 | | |
3001 | 4 | tag->len = 3 + strlen(v); |
3002 | 4 | str = string_alloc(hrecs->str_pool, hts_add_sat2(tag->len, 1)); |
3003 | 4 | if (!str) |
3004 | 0 | return -1; |
3005 | | |
3006 | 4 | if (snprintf(str, tag->len+1, "%2.2s:%s", k, v) < 0) |
3007 | 0 | return -1; |
3008 | | |
3009 | 4 | tag->str = str; |
3010 | 4 | } |
3011 | | |
3012 | 4 | hrecs->dirty = 1; //mark text as dirty and force a rebuild |
3013 | | |
3014 | 4 | return 0; |
3015 | 4 | } |
3016 | | |
3017 | | /* |
3018 | | * Adds or updates tag key,value pairs in a header line. |
3019 | | * Eg for adding M5 tags to @SQ lines or updating sort order for the |
3020 | | * @HD line. |
3021 | | * |
3022 | | * Specify multiple key,value pairs ending in NULL. |
3023 | | * |
3024 | | * Returns 0 on success |
3025 | | * -1 on failure |
3026 | | */ |
3027 | 4 | static int sam_hrecs_update(sam_hrecs_t *hrecs, sam_hrec_type_t *type, ...) { |
3028 | 4 | va_list args; |
3029 | 4 | int res; |
3030 | 4 | va_start(args, type); |
3031 | 4 | res = sam_hrecs_vupdate(hrecs, type, args); |
3032 | 4 | va_end(args); |
3033 | 4 | return res; |
3034 | 4 | } |
3035 | | |
3036 | | /* |
3037 | | * Looks for a specific key in a single sam header line identified by *type. |
3038 | | * If prev is non-NULL it also fills this out with the previous tag, to |
3039 | | * permit use in key removal. *prev is set to NULL when the tag is the first |
3040 | | * key in the list. When a tag isn't found, prev (if non NULL) will be the last |
3041 | | * tag in the existing list. |
3042 | | * |
3043 | | * Returns the tag pointer on success |
3044 | | * NULL on failure |
3045 | | */ |
3046 | | sam_hrec_tag_t *sam_hrecs_find_key(sam_hrec_type_t *type, |
3047 | | const char *key, |
3048 | 194k | sam_hrec_tag_t **prev) { |
3049 | 194k | sam_hrec_tag_t *tag, *p = NULL; |
3050 | 194k | if (!type) |
3051 | 0 | return NULL; |
3052 | | |
3053 | 266k | for (tag = type->tag; tag; p = tag, tag = tag->next) { |
3054 | 248k | if (tag->str[0] == key[0] && tag->str[1] == key[1]) { |
3055 | 175k | if (prev) |
3056 | 4 | *prev = p; |
3057 | 175k | return tag; |
3058 | 175k | } |
3059 | 248k | } |
3060 | | |
3061 | 18.3k | if (prev) |
3062 | 0 | *prev = p; |
3063 | | |
3064 | 18.3k | return NULL; |
3065 | 194k | } |
3066 | | |
3067 | | int sam_hrecs_remove_key(sam_hrecs_t *hrecs, |
3068 | | sam_hrec_type_t *type, |
3069 | 0 | const char *key) { |
3070 | 0 | sam_hrec_tag_t *tag, *prev; |
3071 | 0 | if (!hrecs) |
3072 | 0 | return -1; |
3073 | 0 | tag = sam_hrecs_find_key(type, key, &prev); |
3074 | 0 | if (!tag) |
3075 | 0 | return 0; // Not there anyway |
3076 | | |
3077 | 0 | if (type->type == TYPEKEY("SQ") && tag->str[0] == 'A' && tag->str[1] == 'N') { |
3078 | 0 | assert(tag->len >= 3); |
3079 | 0 | sam_hrec_tag_t *sn_tag = sam_hrecs_find_key(type, "SN", NULL); |
3080 | 0 | if (sn_tag) { |
3081 | 0 | assert(sn_tag->len >= 3); |
3082 | 0 | khint_t k = kh_get(m_s2i, hrecs->ref_hash, sn_tag->str + 3); |
3083 | 0 | if (k != kh_end(hrecs->ref_hash)) { |
3084 | 0 | if (sam_hrecs_remove_ref_altnames(hrecs, kh_val(hrecs->ref_hash, k), tag->str + 3) < 0) |
3085 | 0 | return -1; |
3086 | 0 | } |
3087 | 0 | } |
3088 | 0 | } |
3089 | | |
3090 | 0 | if (!prev) { //first tag |
3091 | 0 | type->tag = tag->next; |
3092 | 0 | } else { |
3093 | 0 | prev->next = tag->next; |
3094 | 0 | } |
3095 | 0 | pool_free(hrecs->tag_pool, tag); |
3096 | 0 | hrecs->dirty = 1; //mark text as dirty and force a rebuild |
3097 | |
|
3098 | 0 | return 1; |
3099 | 0 | } |
3100 | | |
3101 | | /* |
3102 | | * Looks up a read-group by name and returns a pointer to the start of the |
3103 | | * associated tag list. |
3104 | | * |
3105 | | * Returns NULL on failure |
3106 | | */ |
3107 | 1.04k | sam_hrec_rg_t *sam_hrecs_find_rg(sam_hrecs_t *hrecs, const char *rg) { |
3108 | 1.04k | khint_t k = kh_get(m_s2i, hrecs->rg_hash, rg); |
3109 | 1.04k | return k == kh_end(hrecs->rg_hash) |
3110 | 1.04k | ? NULL |
3111 | 1.04k | : &hrecs->rg[kh_val(hrecs->rg_hash, k)]; |
3112 | 1.04k | } |
3113 | | |
3114 | | #if DEBUG_HEADER |
3115 | | void sam_hrecs_dump(sam_hrecs_t *hrecs) { |
3116 | | khint_t k; |
3117 | | int i; |
3118 | | |
3119 | | printf("===DUMP===\n"); |
3120 | | for (k = kh_begin(hrecs->h); k != kh_end(hrecs->h); k++) { |
3121 | | sam_hrec_type_t *t1, *t2; |
3122 | | char c[2]; |
3123 | | int idx = 0; |
3124 | | |
3125 | | if (!kh_exist(hrecs->h, k)) |
3126 | | continue; |
3127 | | |
3128 | | t1 = t2 = kh_val(hrecs->h, k); |
3129 | | c[0] = kh_key(hrecs->h, k)>>8; |
3130 | | c[1] = kh_key(hrecs->h, k)&0xff; |
3131 | | printf("Type %.2s\n", c); |
3132 | | |
3133 | | do { |
3134 | | sam_hrec_tag_t *tag; |
3135 | | printf(">>>%d ", idx++); |
3136 | | for (tag = t1->tag; tag; tag=tag->next) { |
3137 | | if (strncmp(c, "CO", 2)) |
3138 | | printf("\"%.2s\":\"%.*s\"\t", tag->str, tag->len-3, tag->str+3); |
3139 | | else |
3140 | | printf("%s", tag->str); |
3141 | | } |
3142 | | putchar('\n'); |
3143 | | t1 = t1->next; |
3144 | | } while (t1 != t2); |
3145 | | } |
3146 | | |
3147 | | /* Dump out PG chains */ |
3148 | | printf("\n@PG chains:\n"); |
3149 | | for (i = 0; i < hrecs->npg_end; i++) { |
3150 | | int j; |
3151 | | printf(" %d:", i); |
3152 | | for (j = hrecs->pg_end[i]; j != -1; j = hrecs->pg[j].prev_id) { |
3153 | | printf("%s%d(%.*s)", |
3154 | | j == hrecs->pg_end[i] ? " " : "->", |
3155 | | j, hrecs->pg[j].name_len, hrecs->pg[j].name); |
3156 | | } |
3157 | | printf("\n"); |
3158 | | } |
3159 | | |
3160 | | puts("===END DUMP==="); |
3161 | | } |
3162 | | #endif |
3163 | | |
3164 | | /* |
3165 | | * Returns the sort order: |
3166 | | */ |
3167 | 0 | enum sam_sort_order sam_hrecs_sort_order(sam_hrecs_t *hrecs) { |
3168 | 0 | khint_t k; |
3169 | 0 | enum sam_sort_order so; |
3170 | |
|
3171 | 0 | so = ORDER_UNKNOWN; |
3172 | 0 | k = kh_get(sam_hrecs_t, hrecs->h, TYPEKEY("HD")); |
3173 | 0 | if (k != kh_end(hrecs->h)) { |
3174 | 0 | sam_hrec_type_t *ty = kh_val(hrecs->h, k); |
3175 | 0 | sam_hrec_tag_t *tag; |
3176 | 0 | for (tag = ty->tag; tag; tag = tag->next) { |
3177 | 0 | if (tag->str[0] == 'S' && tag->str[1] == 'O') { |
3178 | 0 | if (strcmp(tag->str+3, "unsorted") == 0) |
3179 | 0 | so = ORDER_UNSORTED; |
3180 | 0 | else if (strcmp(tag->str+3, "queryname") == 0) |
3181 | 0 | so = ORDER_NAME; |
3182 | 0 | else if (strcmp(tag->str+3, "coordinate") == 0) |
3183 | 0 | so = ORDER_COORD; |
3184 | 0 | else if (strcmp(tag->str+3, "unknown") != 0) |
3185 | 0 | hts_log_error("Unknown sort order field: %s", tag->str+3); |
3186 | 0 | } |
3187 | 0 | } |
3188 | 0 | } |
3189 | |
|
3190 | 0 | return so; |
3191 | 0 | } |
3192 | | |
3193 | 0 | enum sam_group_order sam_hrecs_group_order(sam_hrecs_t *hrecs) { |
3194 | 0 | khint_t k; |
3195 | 0 | enum sam_group_order go; |
3196 | |
|
3197 | 0 | go = ORDER_NONE; |
3198 | 0 | k = kh_get(sam_hrecs_t, hrecs->h, TYPEKEY("HD")); |
3199 | 0 | if (k != kh_end(hrecs->h)) { |
3200 | 0 | sam_hrec_type_t *ty = kh_val(hrecs->h, k); |
3201 | 0 | sam_hrec_tag_t *tag; |
3202 | 0 | for (tag = ty->tag; tag; tag = tag->next) { |
3203 | 0 | if (tag->str[0] == 'G' && tag->str[1] == 'O') { |
3204 | 0 | if (strcmp(tag->str+3, "query") == 0) |
3205 | 0 | go = ORDER_QUERY; |
3206 | 0 | else if (strcmp(tag->str+3, "reference") == 0) |
3207 | 0 | go = ORDER_REFERENCE; |
3208 | 0 | } |
3209 | 0 | } |
3210 | 0 | } |
3211 | |
|
3212 | 0 | return go; |
3213 | 0 | } |
3214 | | |
3215 | | |
3216 | | /* |
3217 | | * |
3218 | | * Removes all the instances of a tag. |
3219 | | * The tag_id should be a tag key you wish to remove from all records of |
3220 | | * the specified type. Note however this cannot be the primary key ("SN" / |
3221 | | * "ID") as this is used as the key to internal hash tables. |
3222 | | * |
3223 | | * Returns number removed (can be 0) or -1 on a fail. |
3224 | | */ |
3225 | 0 | int sam_hdr_remove_tag_all(sam_hdr_t *bh, const char *type, const char *tag_id) { |
3226 | 0 | sam_hrecs_t *hrecs; |
3227 | 0 | sam_hrec_type_t *t1, *t2; |
3228 | 0 | khint_t k; |
3229 | 0 | int ret = 0; |
3230 | 0 | int deleted = 0; |
3231 | |
|
3232 | 0 | if (!bh || !type || !tag_id) |
3233 | 0 | return -1; |
3234 | | |
3235 | | // Disallow @SQ SN:, @RG ID: and @PG ID: |
3236 | 0 | if (strcmp(tag_id, strcmp(type, "SQ")==0 ? "SN" : "ID") == 0) |
3237 | 0 | return -1; |
3238 | | |
3239 | 0 | if (!(hrecs = bh->hrecs)) { |
3240 | 0 | if (sam_hdr_fill_hrecs(bh) != 0) |
3241 | 0 | return -1; |
3242 | 0 | hrecs = bh->hrecs; |
3243 | 0 | } |
3244 | | |
3245 | 0 | k = kh_get(sam_hrecs_t, hrecs->h, TYPEKEY(type)); |
3246 | |
|
3247 | 0 | if (k == kh_end(hrecs->h)) |
3248 | 0 | return 0; |
3249 | | |
3250 | 0 | t1 = t2 = kh_val(hrecs->h, k); |
3251 | |
|
3252 | 0 | do { |
3253 | 0 | if ((ret = sam_hrecs_remove_key(hrecs, t1, tag_id)) == 1) { |
3254 | 0 | deleted++; |
3255 | 0 | } else if (ret == -1) { |
3256 | 0 | return -1; |
3257 | 0 | } |
3258 | | |
3259 | 0 | t1 = t1->next; |
3260 | 0 | } while (t1 != t2); |
3261 | | |
3262 | 0 | return deleted; |
3263 | 0 | } |