modified: src1/input.c
[GalaxyCodeBases.git] / c_cpp / lib / htslib / test / test_view.c
blob7f173fd95e32503d783d0ff5c4ffafef9d9b9aab
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. */
26 #include <config.h>
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <getopt.h>
34 #include "cram/cram.h"
36 #include "htslib/sam.h"
38 int main(int argc, char *argv[])
40 samFile *in;
41 char *fn_ref = 0;
42 int flag = 0, c, clevel = -1, ignore_sam_err = 0;
43 char moder[8];
44 bam_hdr_t *h;
45 bam1_t *b;
46 htsFile *out;
47 char modew[800];
48 int r = 0, exit_code = 0;
49 hts_opt *in_opts = NULL, *out_opts = NULL;
50 int nreads = 0;
51 int extra_hdr_nuls = 0;
52 int benchmark = 0;
53 int nthreads = 0; // shared pool
55 while ((c = getopt(argc, argv, "IbDCSl:t:i:o:N:BZ:@:")) >= 0) {
56 switch (c) {
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;
72 if (argc == optind) {
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");
74 return 1;
76 strcpy(moder, "r");
77 if (flag&4) strcat(moder, "c");
78 else if ((flag&1) == 0) strcat(moder, "b");
80 in = sam_open(argv[optind], moder);
81 if (in == NULL) {
82 fprintf(stderr, "Error opening \"%s\"\n", argv[optind]);
83 return EXIT_FAILURE;
85 h = sam_hdr_read(in);
86 if (h == NULL) {
87 fprintf(stderr, "Couldn't read header for \"%s\"\n", argv[optind]);
88 return EXIT_FAILURE;
90 h->ignore_sam_err = ignore_sam_err;
91 if (extra_hdr_nuls) {
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");
95 return EXIT_FAILURE;
97 h->text = new_text;
98 memset(&h->text[h->l_text], 0, extra_hdr_nuls);
99 h->l_text += extra_hdr_nuls;
102 b = bam_init1();
104 strcpy(modew, "w");
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);
109 if (out == NULL) {
110 fprintf(stderr, "Error opening standard output\n");
111 return EXIT_FAILURE;
114 /* CRAM output */
115 if (flag & 8) {
116 int ret;
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
122 if (fn_ref)
123 ret = cram_set_option(out->fp.cram, CRAM_OPT_REFERENCE, fn_ref);
124 else
125 // Attempt to fill out a cram->refs[] array from @SQ headers
126 ret = cram_set_option(out->fp.cram, CRAM_OPT_REFERENCE, NULL);
128 if (ret != 0)
129 return EXIT_FAILURE;
132 // Process any options; currently cram only.
133 if (hts_opt_apply(in, in_opts))
134 return EXIT_FAILURE;
135 hts_opt_free(in_opts);
137 if (hts_opt_apply(out, out_opts))
138 return EXIT_FAILURE;
139 hts_opt_free(out_opts);
141 // Create and share the thread pool
142 htsThreadPool p = {NULL, 0};
143 if (nthreads > 0) {
144 p.pool = hts_tpool_init(nthreads);
145 if (!p.pool) {
146 fprintf(stderr, "Error creating thread pool\n");
147 exit_code = 1;
148 } else {
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");
156 exit_code = 1;
158 if (optind + 1 < argc && !(flag&1)) { // BAM input and has a region
159 int i;
160 hts_idx_t *idx;
161 if ((idx = sam_index_load(in, argv[optind])) == 0) {
162 fprintf(stderr, "[E::%s] fail to load the BAM index\n", __func__);
163 return 1;
165 for (i = optind + 1; i < argc; ++i) {
166 hts_itr_t *iter;
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]);
169 continue;
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");
174 exit_code = 1;
175 break;
177 if (nreads && --nreads == 0)
178 break;
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");
186 exit_code = 1;
187 break;
189 if (nreads && --nreads == 0)
190 break;
193 if (r < -1) {
194 fprintf(stderr, "Error parsing input.\n");
195 exit_code = 1;
198 r = sam_close(out);
199 if (r < 0) {
200 fprintf(stderr, "Error closing output.\n");
201 exit_code = 1;
204 bam_destroy1(b);
205 bam_hdr_destroy(h);
207 r = sam_close(in);
208 if (r < 0) {
209 fprintf(stderr, "Error closing input.\n");
210 exit_code = 1;
213 if (p.pool)
214 hts_tpool_destroy(p.pool);
216 return exit_code;