1 /* test/test_view.c -- simple view tool, purely for use in a test harness.
3 Copyright (C) 2012 Broad Institute.
4 Copyright (C) 2013-2014 Genome Research Ltd.
6 Author: Heng Li <lh3@sanger.ac.uk>
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 DEALINGS IN THE SOFTWARE. */
34 #include "cram/cram.h"
36 #include "htslib/sam.h"
38 int main(int argc
, char *argv
[])
42 int flag
= 0, c
, clevel
= -1, ignore_sam_err
= 0;
48 int r
= 0, exit_code
= 0;
49 hts_opt
*in_opts
= NULL
, *out_opts
= NULL
;
51 int extra_hdr_nuls
= 0;
53 int nthreads
= 0; // shared pool
55 while ((c
= getopt(argc
, argv
, "IbDCSl:t:i:o:N:BZ:@:")) >= 0) {
57 case 'S': flag
|= 1; break;
58 case 'b': flag
|= 2; break;
59 case 'D': flag
|= 4; break;
60 case 'C': flag
|= 8; break;
61 case 'B': benchmark
= 1; break;
62 case 'l': clevel
= atoi(optarg
); flag
|= 2; break;
63 case 't': fn_ref
= optarg
; break;
64 case 'I': ignore_sam_err
= 1; break;
65 case 'i': if (hts_opt_add(&in_opts
, optarg
)) return 1; break;
66 case 'o': if (hts_opt_add(&out_opts
, optarg
)) return 1; break;
67 case 'N': nreads
= atoi(optarg
); break;
68 case 'Z': extra_hdr_nuls
= atoi(optarg
); break;
69 case '@': nthreads
= atoi(optarg
); break;
73 fprintf(stderr
, "Usage: samview [-bSCSIB] [-N num_reads] [-l level] [-o option=value] [-Z hdr_nuls] <in.bam>|<in.sam>|<in.cram> [region]\n");
77 if (flag
&4) strcat(moder
, "c");
78 else if ((flag
&1) == 0) strcat(moder
, "b");
80 in
= sam_open(argv
[optind
], moder
);
82 fprintf(stderr
, "Error opening \"%s\"\n", argv
[optind
]);
87 fprintf(stderr
, "Couldn't read header for \"%s\"\n", argv
[optind
]);
90 h
->ignore_sam_err
= ignore_sam_err
;
92 char *new_text
= realloc(h
->text
, h
->l_text
+ extra_hdr_nuls
);
93 if (new_text
== NULL
) {
94 fprintf(stderr
, "Error reallocing header text\n");
98 memset(&h
->text
[h
->l_text
], 0, extra_hdr_nuls
);
99 h
->l_text
+= extra_hdr_nuls
;
105 if (clevel
>= 0 && clevel
<= 9) sprintf(modew
+ 1, "%d", clevel
);
106 if (flag
&8) strcat(modew
, "c");
107 else if (flag
&2) strcat(modew
, "b");
108 out
= hts_open("-", modew
);
110 fprintf(stderr
, "Error opening standard output\n");
118 // Parse input header and use for CRAM output
119 out
->fp
.cram
->header
= sam_hdr_parse_(h
->text
, h
->l_text
);
121 // Create CRAM references arrays
123 ret
= cram_set_option(out
->fp
.cram
, CRAM_OPT_REFERENCE
, fn_ref
);
125 // Attempt to fill out a cram->refs[] array from @SQ headers
126 ret
= cram_set_option(out
->fp
.cram
, CRAM_OPT_REFERENCE
, NULL
);
132 // Process any options; currently cram only.
133 if (hts_opt_apply(in
, in_opts
))
135 hts_opt_free(in_opts
);
137 if (hts_opt_apply(out
, out_opts
))
139 hts_opt_free(out_opts
);
141 // Create and share the thread pool
142 htsThreadPool p
= {NULL
, 0};
144 p
.pool
= hts_tpool_init(nthreads
);
146 fprintf(stderr
, "Error creating thread pool\n");
149 hts_set_opt(in
, HTS_OPT_THREAD_POOL
, &p
);
150 hts_set_opt(out
, HTS_OPT_THREAD_POOL
, &p
);
154 if (!benchmark
&& sam_hdr_write(out
, h
) < 0) {
155 fprintf(stderr
, "Error writing output header.\n");
158 if (optind
+ 1 < argc
&& !(flag
&1)) { // BAM input and has a region
161 if ((idx
= sam_index_load(in
, argv
[optind
])) == 0) {
162 fprintf(stderr
, "[E::%s] fail to load the BAM index\n", __func__
);
165 for (i
= optind
+ 1; i
< argc
; ++i
) {
167 if ((iter
= sam_itr_querys(idx
, h
, argv
[i
])) == 0) {
168 fprintf(stderr
, "[E::%s] fail to parse region '%s'\n", __func__
, argv
[i
]);
171 while ((r
= sam_itr_next(in
, iter
, b
)) >= 0) {
172 if (!benchmark
&& sam_write1(out
, h
, b
) < 0) {
173 fprintf(stderr
, "Error writing output.\n");
177 if (nreads
&& --nreads
== 0)
180 hts_itr_destroy(iter
);
182 hts_idx_destroy(idx
);
183 } else while ((r
= sam_read1(in
, h
, b
)) >= 0) {
184 if (!benchmark
&& sam_write1(out
, h
, b
) < 0) {
185 fprintf(stderr
, "Error writing output.\n");
189 if (nreads
&& --nreads
== 0)
194 fprintf(stderr
, "Error parsing input.\n");
200 fprintf(stderr
, "Error closing output.\n");
209 fprintf(stderr
, "Error closing input.\n");
214 hts_tpool_destroy(p
.pool
);