Debian Codegraph @

samtools/cram_size.c.pysam.c

raw symbols SCIP
1 #include "samtools.pysam.h"
2
3 /* cram_size.c -- produces summary of the size of each cram data-series
4
5 Copyright (C) 2023 Genome Research Ltd.
6
7 Author: James Bonfield <[email protected]>
8
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE. */
26
27 // TODO: add range query. Eg the ability to look at size for "*" only
28 // (unmapped), or in a specific region such as a centromere.
29
30 #include <config.h>
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <string.h>
36 #include <strings.h>
37
38 #include "htslib/bgzf.h"
39 #include "htslib/sam.h"
40 #include "htslib/cram.h"
41 #include "htslib/kstring.h"
42 #include "htslib/khash.h"
43 #include "samtools.h"
44 #include "sam_opts.h"
45 #include "htslib/hfile.h"
46
47 /*----------------------------------------------------------------------
48 * Compression method handling
49 */
50
51 // A numeric version of the cram_method_details struct.
52 // We expand the myriad of struct field combinations into a single
53 // enumerated type so we can index and accumulate statistics for
54 // purposes of reporting.
55 //
56 // These expanded numeric values have no definition within CRAM itself
57 // and never occur within the file format.
58 enum comp_expanded {
59 //----
60 // Copies from htslib cram_block_method enum
61 COMP_RAW = CRAM_COMP_RAW,
62 COMP_GZIP = CRAM_COMP_GZIP,
63 COMP_BZIP2 = CRAM_COMP_BZIP2,
64 COMP_LZMA = CRAM_COMP_LZMA,
65 COMP_RANS8 = CRAM_COMP_RANS4x8,
66 COMP_RANS16 = CRAM_COMP_RANSNx16,
67 COMP_ARITH = CRAM_COMP_ARITH,
68 COMP_FQZ = CRAM_COMP_FQZ,
69 COMP_TOK3 = CRAM_COMP_TOK3,
70
71 //----
72 // Localised variants.
73
74 // Gzip
75 COMP_GZIP_1,
76 COMP_GZIP_9,
77
78 // Bzip2
79 COMP_BZIP2_1,
80 COMP_BZIP2_2,
81 COMP_BZIP2_3,
82 COMP_BZIP2_4,
83 COMP_BZIP2_5,
84 COMP_BZIP2_6,
85 COMP_BZIP2_7,
86 COMP_BZIP2_8,
87 COMP_BZIP2_9,
88
89 // rans 4x8
90 COMP_RANS4x8_O0,
91 COMP_RANS4x8_O1,
92
93 // rans Nx16. Note order here is to enable selection via bit-fields
94 // bit 0: O0/O1
95 // bit 1: RLE
96 // bit 2: PACK
97 // bit 3: 32x16
98 COMP_RANS4x16_O0,
99 COMP_RANS4x16_O1,
100 COMP_RANS4x16_O0R, // +RLE
101 COMP_RANS4x16_O1R,
102 COMP_RANS4x16_O0P, // +PACK
103 COMP_RANS4x16_O1P,
104 COMP_RANS4x16_O0PR, // +PACK+RLE
105 COMP_RANS4x16_O1PR,
106 COMP_RANS32x16_O0, // SIMD variants
107 COMP_RANS32x16_O1,
108 COMP_RANS32x16_O0R, // +RLE
109 COMP_RANS32x16_O1R,
110 COMP_RANS32x16_O0P, // +PACK
111 COMP_RANS32x16_O1P,
112 COMP_RANS32x16_O0PR, // +PACK+RLE
113 COMP_RANS32x16_O1PR,
114 COMP_RANSNx16_STRIPE,
115 COMP_RANSNx16_CAT,
116
117 // Arith
118 COMP_ARITH_O0,
119 COMP_ARITH_O1,
120 COMP_ARITH_O0R, // +RLE
121 COMP_ARITH_O1R,
122 COMP_ARITH_O0P, // +PACK
123 COMP_ARITH_O1P,
124 COMP_ARITH_O0PR, // +PACK+RLE
125 COMP_ARITH_O1PR,
126 COMP_ARITH_STRIPE,
127 COMP_ARITH_CAT, // no entropy encoder
128 COMP_ARITH_EXT, // external entropy encode
129
130 // Nake tokeniser
131 COMP_TOK3_RANS,
132 COMP_TOK3_ARITH,
133
134 // To mark maximum size
135 COMP_MAX,
136 };
137
138 static enum comp_expanded comp_method2expanded(cram_method_details *cm) {
139 switch (cm->method) {
140 case CRAM_COMP_GZIP:
141 switch (cm->level) {
142 case 1: return COMP_GZIP_1;
143 case 9: return COMP_GZIP_9;
144 default: return COMP_GZIP;
145 }
146 break;
147
148 case CRAM_COMP_BZIP2:
149 if (cm->level >= 1 && cm->level <= 9)
150 return COMP_BZIP2_1 + cm->level-1;
151 else
152 return COMP_BZIP2;
153 break;
154
155 case CRAM_COMP_RANS4x8:
156 return cm->order ? COMP_RANS4x8_O1 : COMP_RANS4x8_O0;
157
158 case CRAM_COMP_RANSNx16: {
159 // 8 4x16, 8 32x16 and 2 stripe/cat
160 if (cm->stripe) return COMP_RANSNx16_STRIPE;
161 if (cm->cat) return COMP_RANSNx16_CAT;
162 int c = COMP_RANS4x16_O0;
163 c += 1*cm->order;
164 c += 2*cm->rle;
165 c += 4*cm->pack;
166 c += 8*(cm->Nway==32);
167 return c;
168 }
169
170 case CRAM_COMP_ARITH: {
171 // 8 4x16, 8 32x16 and 2 stripe/cat
172 if (cm->stripe) return COMP_ARITH_STRIPE;
173 if (cm->cat) return COMP_ARITH_CAT;
174 if (cm->ext) return COMP_ARITH_EXT;
175 int c = COMP_ARITH_O0;
176 c += 1*cm->order;
177 c += 2*cm->rle;
178 c += 4*cm->pack;
179 return c;
180 }
181
182 case CRAM_COMP_TOK3:
183 return cm->level < 10
184 ? COMP_TOK3_RANS
185 : COMP_TOK3_ARITH;
186
187 default:
188 // Any unspecialised method
189 return (enum comp_expanded)cm->method;
190 }
191 }
192
193 // Short form of cram_block_method_int type
194 static char comp_method2char[COMP_MAX] =
195 ".gblr0afn" // standard CRAM methods
196 "_G" // gzip
197 "bbbbbbbbB" // bzip2
198 "rR" // rans4x8
199 "010101014545454582" // ransNx16
200 "aAaAaAaAaaa" // arith
201 "nN"; // tok3
202
203 // Long form of cram_block_method_int type
204 static char *comp_method2str[COMP_MAX] = {
205 // Standard CRAM methods
206 "raw", "gzip", "bzip2", "lzma", "r4x8", "rNx16",
207 "arith", "fqzcomp", "tok3",
208
209 // custom gzip
210 "gzip-min", "gzip-max",
211
212 // custom bzip2
213 "bzip2-1", "bzip2-2", "bzip2-3", "bzip2-4", "bzip2-5",
214 "bzip2-6", "bzip2-7", "bzip2-8", "bzip2-9",
215
216 // rANS 4x8
217 "r4x8-o0", "r4x8-o1",
218
219 // rANS 4x16
220 "r4x16-o0", "r4x16-o1",
221
222 "r4x16-o0R", "r4x16-o1R",
223 "r4x16-o0P", "r4x16-o1P",
224 "r4x16-o0PR", "r4x16-o1PR",
225 "r32x16-o0", "r32x16-o1",
226 "r32x16-o0R", "r32x16-o1R",
227 "r32x16-o0P", "r32x16-o1P",
228 "r32x16-o0PR","r32x16-o1PR",
229 "rNx16-xo0", "rNx16-cat",
230
231 // Arith
232 "arith-o0", "arith-o1",
233 "arith-o0R", "arith-o1R",
234 "arith-o0P", "arith-o1P",
235 "arith-o0PR", "arith-o1PR",
236 "arith-stripe", "arith-cat", "arith-ext",
237
238 // Name tokeniser
239 "tok3-rans", "tok3-arith",
240 };
241
242 /*----------------------------------------------------------------------
243 * Manipulation and sorting of Block Content-ID arrays and hashes
244 */
245
246 typedef struct {
247 int64_t csize[COMP_MAX];
248 int64_t usize[COMP_MAX];
249 } cusize_t;
250
251 static int64_t total_csize(cusize_t *cu) {
252 int i;
253 int64_t tot = 0;
254 for (i = 0; i < COMP_MAX; i++)
255 tot += cu->csize[i];
256 return tot;
257 }
258
259 static int64_t total_usize(cusize_t *cu) {
260 int i;
261 int64_t tot = 0;
262 for (i = 0; i < COMP_MAX; i++)
263 tot += cu->usize[i];
264 return tot;
265 }
266
267 // cusize_t array and sorting by compressed size
268 static cusize_t *sort_cusize_global; // avoids a messy extra data type
269 static int sort_cusize_compar(const void *i1, const void *i2) {
270 int64_t n = sort_cusize_global->csize[*(const int *)i2] -
271 sort_cusize_global->csize[*(const int *)i1];
272 return n > 0 ? 1 : (n < 0 ? -1 : *(const int *)i1 - *(const int *)i2);
273 }
274
275 // Sort a cusize array by size of used method.
276 // Returns cu->csize[comp] indices in descending size, as static mem
277 static int *sort_cusize(cusize_t *cu) {
278 static int idx[COMP_MAX];
279 int i;
280 for (i = 0; i < COMP_MAX; i++)
281 idx[i] = i;
282 sort_cusize_global = cu;
283 qsort(idx, COMP_MAX, sizeof(*idx), sort_cusize_compar);
284
285 return idx;
286 }
287
288 // Hash table of cusize_t and sorting by key (content-id)
289 KHASH_MAP_INIT_INT(cu, cusize_t)
290
291 /* Sort by hash key. Global due to rubbish qsort API, but it's simple. */
292 static khash_t(cu) *global_cu_hash = NULL;
293 static int cu_compar(const void *i1, const void *i2) {
294 return kh_key(global_cu_hash, *(const int *)i1) -
295 kh_key(global_cu_hash, *(const int *)i2);
296 }
297
298 /*----------------------------------------------------------------------
299 * Main cram_size reporting and aggregation
300 */
301 static off_t report_size(FILE *outfp, int verbose, int ref_seq_blk,
302 khash_t(cu) *cu_size, cram_cid2ds_t *cid2ds) {
303 if (!cu_size || !cid2ds)
304 return -1;
305
306 khiter_t k;
307 off_t tot_size = 0;
308
309 fprintf(outfp, "# Content_ID Uncomp.size Comp.size Ratio Method%.*s Data_series\n", verbose ? 4 : 0, " ");
310 int *sorted_blocks = malloc(kh_end(cu_size)*sizeof(int));
311 if (!sorted_blocks)
312 return -1;
313 int nblocks = 0;
314 for (k = kh_begin(cu_size); k != kh_end(cu_size); k++) {
315 if (!kh_exist(cu_size, k))
316 continue;
317 sorted_blocks[nblocks++] = k;
318 }
319 global_cu_hash = cu_size;
320 qsort(sorted_blocks, nblocks, sizeof(int), cu_compar);
321
322 int i;
323 for (i = 0; i < nblocks; i++) {
324 k = sorted_blocks[i];
325
326 if (verbose) {
327 // FULL output
328 int *comp_idx = sort_cusize(&kh_value(cu_size, k));
329 int first_line = 1, c, j;
330 for (c = 0; c < COMP_MAX; c++) {
331 int comp = comp_idx[c];
332 if (!kh_value(cu_size, k).csize[comp] && c)
333 break;
334
335 if (!first_line)
336 fprintf(outfp, "\n");
337 first_line = 0;
338
339 if ((int)kh_key(cu_size, k) < 0)
340 fprintf(outfp, "BLOCK %8s", "CORE");
341 else
342 fprintf(outfp, "BLOCK %8d", kh_key(cu_size, k));
343
344 fprintf(outfp, " %12"PRId64" %12"PRId64,
345 kh_value(cu_size, k).usize[comp],
346 kh_value(cu_size, k).csize[comp]);
347 double f = (100.0*(kh_value(cu_size, k).csize[comp]+.0001)) /
348 (kh_value(cu_size, k).usize[comp]+.0001);
349 if (f > 999)
350 fprintf(outfp, " >999%% %-11s", comp_method2str[comp]);
351 else
352 fprintf(outfp, " %6.2f%% %-11s",f, comp_method2str[comp]);
353
354 int n, *dsa = cram_cid2ds_query(cid2ds, kh_key(cu_size, k), &n);
355 for (j = 0; j < n; j++) {
356 int d = dsa[j];
357 if (d > 65535)
358 fprintf(outfp, " %c%c%c", d>>16, (d>>8)&0xff, d&0xff);
359 else
360 fprintf(outfp, " %c%c", (d>>8)&0xff, d&0xff);
361 }
362 }
363 } else {
364 // aggregate by compression type.
365 int64_t csize = total_csize(&kh_value(cu_size, k));
366 int64_t usize = total_usize(&kh_value(cu_size, k));
367 int *comp_idx = sort_cusize(&kh_value(cu_size, k));
368
369 char cstr[COMP_MAX+1] = {0};
370 int cidx = 0, c;
371 for (c = 0; c < COMP_MAX; c++) {
372 if (!kh_value(cu_size, k).csize[comp_idx[c]])
373 break;
374 cstr[cidx++] = comp_method2char[comp_idx[c]];
375 }
376 if (!*cstr) *cstr = '.';
377
378 if ((int)kh_key(cu_size, k) < 0)
379 fprintf(outfp, "BLOCK %8s", "CORE");
380 else
381 fprintf(outfp, "BLOCK %8d", kh_key(cu_size, k));
382 fprintf(outfp, " %12"PRId64" %12"PRId64, usize, csize);
383 double f = 100*(csize+.0001)/(usize+.0001);
384 if (f > 999)
385 fprintf(outfp, " >999%% %-7s", cstr);
386 else
387 fprintf(outfp, " %6.2f%% %-7s", f, cstr);
388
389 int n, j, *dsa = cram_cid2ds_query(cid2ds, kh_key(cu_size, k), &n);
390 for (j = 0; j < n; j++) {
391 int d = dsa[j];
392 if (d > 65535)
393 fprintf(outfp, " %c%c%c", d>>16, (d>>8)&0xff, d&0xff);
394 else
395 fprintf(outfp, " %c%c", (d>>8)&0xff, d&0xff);
396 }
397 }
398
399 if ((int)kh_key(cu_size, k) >= 0 &&
400 (int)kh_key(cu_size, k) == ref_seq_blk) {
401 fprintf(outfp, " embedded_ref");
402 }
403 fprintf(outfp, "\n");
404
405 tot_size += total_csize(&kh_value(cu_size, k));
406 }
407
408 free(sorted_blocks);
409
410 return tot_size;
411 }
412
413 /* Main processing loop */
414 static int cram_size(hFILE *hf_in, samFile *in, sam_hdr_t *h, FILE *outfp,
415 int verbose, int encodings) {
416 cram_fd *in_c;
417 cram_container *c = NULL;
418 cram_block *blk = NULL;
419 cram_block_slice_hdr *shdr = NULL;
420 khiter_t k;
421 int ret;
422 cram_cid2ds_t *cid2ds = NULL;
423 khash_t(cu) *cu_size = kh_init(cu);
424 int ref_seq_blk_used = -1;
425 int64_t nseqs = 0, nbases = 0, ncont = 0, nslice = 0;
426
427 if (!in->is_cram) {
428 print_error("cram_size", "Input is not a CRAM file");
429 goto err;
430 }
431 in_c = in->fp.cram; // low level htslib abuse?
432 while ((c = cram_read_container(in_c))) {
433 if (cram_container_is_empty(in_c)) {
434 cram_block *blk;
435 // Container compression header
436 if (!(blk = cram_read_block(in_c)))
437 goto err;
438 cram_free_block(blk);
439 cram_free_container(c);
440 c = NULL; blk = NULL;
441 continue;
442 }
443
444 nseqs += cram_container_get_num_records(c);
445 nbases += cram_container_get_num_bases(c);
446
447 // Container compression header
448 int32_t num_slices;
449 if (!(blk = cram_read_block(in_c)))
450 goto err;
451
452 // Decode compression header...
453 cram_block_compression_hdr *chdr;
454 chdr = cram_decode_compression_header(in_c, blk);
455
456 if (encodings) {
457 kstring_t ks = KS_INITIALIZE;
458 if (cram_describe_encodings(chdr, &ks) < 0)
459 goto err;
460
461 fprintf(outfp, "Container encodings\n%s\n", ks_str(&ks));
462
463 ks_free(&ks);
464 }
465
466 cid2ds = cram_update_cid2ds_map(chdr, cid2ds);
467
468 cram_free_block(blk);
469 blk = NULL;
470
471 cram_free_compression_header(chdr);
472
473 // Container num_blocks can be invalid, due to a bug.
474 // Instead we iterate in slice context instead.
475 (void)cram_container_get_landmarks(c, &num_slices);
476 ncont++;
477 nslice += num_slices;
478
479 int i, j;
480 for (i = 0; i < num_slices; i++) {
481 // Slice header
482 if (!(blk = cram_read_block(in_c)))
483 goto err;
484 if (!(shdr = cram_decode_slice_header(in_c, blk)))
485 goto err;
486 cram_free_block(blk);
487 blk = NULL;
488
489 int ref_seq_blk = cram_slice_hdr_get_embed_ref_id(shdr);
490 int num_blocks = cram_slice_hdr_get_num_blocks(shdr);
491
492 // Embedded reference. Check it's consistent (if used this is
493 // an almost guaranteed certainty, so we take the easy route).
494 if (ref_seq_blk >= 0) {
495 if (ref_seq_blk_used == -1)
496 ref_seq_blk_used = ref_seq_blk;
497 else if (ref_seq_blk_used != ref_seq_blk)
498 fprintf(samtools_stderr, "Embedded reference is not consistently using the same Content-Id.\n"
499 "Reported figures for reference will be invalid.\n");
500 }
501
502 // Slice data blocks
503 for (j = 0; j < num_blocks; j++) {
504 // read and discard, unless it's the ref-ID block
505 if (!(blk = cram_read_block(in_c)))
506 goto err;
507
508 int32_t csize = cram_block_get_comp_size(blk);
509 int32_t usize = cram_block_get_uncomp_size(blk);
510 int cid = cram_block_get_content_id(blk);
511 enum cram_block_method method = cram_block_get_method(blk);
512
513 // Expand comp to the internal sub-formats, eg
514 // rANS order-0/1, PACK+RLE, etc.
515 cram_method_details *cm;
516 cm = cram_expand_method(cram_block_get_data(blk),
517 cram_block_get_comp_size(blk),
518 method);
519 if (!cm)
520 goto err;
521 enum comp_expanded comp
522 = comp_method2expanded(cm);
523 free(cm);
524
525 k = kh_put(cu, cu_size, cid, &ret);
526 if (ret < 0)
527 goto err;
528 if (ret == 0) {
529 kh_value(cu_size, k).csize[comp] += csize;
530 kh_value(cu_size, k).usize[comp] += usize;
531 } else {
532 memset(&kh_value(cu_size, k), 0, sizeof(cusize_t));
533 kh_value(cu_size, k).csize[comp] = csize;
534 kh_value(cu_size, k).usize[comp] = usize;
535 }
536
537 cram_free_block(blk);
538 blk = NULL;
539 }
540 cram_free_slice_header(shdr);
541 shdr = NULL;
542 }
543
544 cram_free_container(c);
545 c = NULL;
546 }
547
548 off_t tot_size = report_size(outfp, verbose, ref_seq_blk_used,
549 cu_size, cid2ds);
550 if (tot_size < 0)
551 goto err;
552
553 kh_destroy(cu, cu_size);
554 cram_cid2ds_free(cid2ds);
555
556 off_t end = htell(hf_in);
557
558 fprintf(outfp, "\n");
559 fprintf(outfp, "Number of containers %18"PRId64"\n", ncont);
560 fprintf(outfp, "Number of slices %18"PRId64"\n", nslice);
561 fprintf(outfp, "Number of sequences %18"PRId64"\n", nseqs);
562 fprintf(outfp, "Number of bases %18"PRId64"\n", nbases);
563 fprintf(outfp, "Total file size %18"PRId64"\n", (int64_t) end);
564 fprintf(outfp, "Format overhead size %18"PRId64"\n", (int64_t) (end - tot_size));
565
566 return 0;
567
568 err:
569 // Report anyway so we can get stats on partial files, but be
570 // sure to error too.
571 report_size(outfp, verbose, ref_seq_blk_used, cu_size, cid2ds);
572
573 print_error("cram_size", "Failed in decoding CRAM file");
574 if (blk)
575 cram_free_block(blk);
576 if (shdr)
577 cram_free_slice_header(shdr);
578 if (c)
579 cram_free_container(c);
580 if (cid2ds)
581 cram_cid2ds_free(cid2ds);
582
583 return -1;
584 }
585
586 /* main() for cram_size */
587 int main_cram_size(int argc, char *argv[]) {
588 int c, usage = 0, verbose = 0, encodings = 0;
589 sam_hdr_t *h = 0;
590 hFILE *hf_in = NULL;
591 samFile *in = NULL;
592 sam_global_args ga;
593 FILE *outfp = samtools_stdout;
594
595 static const struct option lopts[] = {
596 {"output", required_argument, NULL, 'o'},
597 {"verbose", no_argument, NULL, 'v'},
598 {"encodings", no_argument, NULL, 'e'},
599 SAM_OPT_GLOBAL_OPTIONS('-', 0, '-', '-', '-', '-'),
600 { NULL, 0, NULL, 0 }
601 };
602
603 sam_global_args_init(&ga);
604
605 while ((c = getopt_long(argc, argv, "vo:e", lopts, NULL)) >= 0) {
606 switch (c) {
607 case 'o':
608 if (!(outfp = fopen(optarg, "w"))) {
609 perror(optarg);
610 goto err;
611 }
612 break;
613
614 case 'v':
615 verbose++;
616 break;
617
618 case 'e':
619 encodings++;
620 break;
621
622 default:
623 if (parse_sam_global_opt(c, optarg, lopts, &ga) == 0) break;
624 /* else fall-through */
625 case '?': usage=1; break;
626 }
627 }
628
629 if ((optind == argc && isatty(0)) || usage) {
630 fprintf(samtools_stdout, "Usage: samtools cram_size [-ve] [-o out.size] [in.cram]\n");
631 return 0;
632 }
633
634 char *fn = optind < argc ? argv[optind] : "-";
635
636 // We want access to in->fp.cram->fp, but this is an opaque struct so we
637 // can't get that. However we opened with hopen and then reopen as
638 // CRAM with hts_hopen, which will swallow the initial hFILE and take
639 // owenership of it. Hence we now know in->fp.cram->fp.
640 if (!(hf_in = hopen(fn, "r"))) {
641 print_error_errno("cram_size", "failed to open file '%s'", fn);
642 return 1;
643 }
644 if (!(in = hts_hopen(hf_in, fn, "r"))) {
645 print_error_errno("cram_size", "failed to open file '%s'", fn);
646 goto err;
647 }
648
649 if (!(h = sam_hdr_read(in)))
650 goto err;
651
652 int ret = cram_size(hf_in, in, h, outfp, verbose, encodings);
653 sam_hdr_destroy(h);
654 sam_close(in);
655 if (outfp != samtools_stdout)
656 fclose(outfp);
657
658 return ret ? 1 : 0;
659
660 err:
661 if (in)
662 sam_close(in);
663 if (h)
664 sam_hdr_destroy(h);
665
666 return 1;
667 }