annotate ezBAMQC/src/htslib/test/test_view.c @ 17:161866cee2a2

wrapper rename
author youngkim
date Wed, 30 Mar 2016 13:28:47 -0400
parents dfa3745e5fd8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
1 /* test/test_view.c -- simple view tool, purely for use in a test harness.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
2
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
3 Copyright (C) 2012 Broad Institute.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
4 Copyright (C) 2013-2014 Genome Research Ltd.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
5
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
6 Author: Heng Li <lh3@sanger.ac.uk>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
7
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
8 Permission is hereby granted, free of charge, to any person obtaining a copy
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
9 of this software and associated documentation files (the "Software"), to deal
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
10 in the Software without restriction, including without limitation the rights
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
12 copies of the Software, and to permit persons to whom the Software is
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
13 furnished to do so, subject to the following conditions:
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
14
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
15 The above copyright notice and this permission notice shall be included in
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
16 all copies or substantial portions of the Software.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
17
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
21 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
23 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
24 DEALINGS IN THE SOFTWARE. */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
25
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
26 #include <stdio.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
27 #include <unistd.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
28 #include <stdlib.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
29 #include <string.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
30
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
31 #include "cram/cram.h"
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
32
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
33 #include "htslib/sam.h"
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
34
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
35 typedef struct hts_opt {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
36 enum cram_option opt;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
37 union {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
38 int i;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
39 char *s;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
40 } val;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
41 struct hts_opt *next;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
42 } hts_opt;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
43
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
44 /*
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
45 * Parses arg and appends it to the option list.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
46 * Returns 0 on success;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
47 * -1 on failure.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
48 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
49 int add_option(hts_opt **opts, char *arg) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
50 hts_opt *o, *t;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
51 char *cp;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
52
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
53 if (!(cp = strchr(arg, '=')))
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
54 cp = "1"; // assume boolean
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
55 else
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
56 *cp++ = 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
57
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
58 if (!(o = malloc(sizeof(*o))))
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
59 return -1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
60
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
61 if (strcmp(arg, "DECODE_MD") == 0)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
62 o->opt = CRAM_OPT_DECODE_MD, o->val.i = atoi(cp);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
63 else if (strcmp(arg, "VERBOSITY") == 0)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
64 o->opt = CRAM_OPT_VERBOSITY, o->val.i = atoi(cp);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
65 else if (strcmp(arg, "SEQS_PER_SLICE") == 0)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
66 o->opt = CRAM_OPT_SEQS_PER_SLICE, o->val.i = atoi(cp);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
67 else if (strcmp(arg, "SLICES_PER_CONTAINER") == 0)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
68 o->opt = CRAM_OPT_SLICES_PER_CONTAINER, o->val.i = atoi(cp);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
69 else if (strcmp(arg, "EMBED_REF") == 0)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
70 o->opt = CRAM_OPT_EMBED_REF, o->val.i = atoi(cp);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
71 else if (strcmp(arg, "NO_REF") == 0)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
72 o->opt = CRAM_OPT_NO_REF, o->val.i = atoi(cp);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
73 else if (strcmp(arg, "IGNORE_MD5") == 0)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
74 o->opt = CRAM_OPT_IGNORE_MD5, o->val.i = atoi(cp);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
75 else if (strcmp(arg, "USE_BZIP2") == 0)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
76 o->opt = CRAM_OPT_USE_BZIP2, o->val.i = atoi(cp);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
77 else if (strcmp(arg, "USE_RANS") == 0)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
78 o->opt = CRAM_OPT_USE_RANS, o->val.i = atoi(cp);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
79 else if (strcmp(arg, "USE_LZMA") == 0)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
80 o->opt = CRAM_OPT_USE_LZMA, o->val.i = atoi(cp);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
81 else if (strcmp(arg, "REFERENCE") == 0)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
82 o->opt = CRAM_OPT_REFERENCE, o->val.s = cp;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
83 else if (strcmp(arg, "VERSION") == 0)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
84 o->opt = CRAM_OPT_VERSION, o->val.s =cp;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
85 else if (strcmp(arg, "MULTI_SEQ_PER_SLICE") == 0)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
86 o->opt = CRAM_OPT_MULTI_SEQ_PER_SLICE, o->val.i = atoi(cp);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
87 else if (strcmp(arg, "NTHREADS") == 0)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
88 o->opt = CRAM_OPT_NTHREADS, o->val.i = atoi(cp);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
89 else if (strcmp(arg, "REQUIRED_FIELDS") == 0)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
90 o->opt = CRAM_OPT_REQUIRED_FIELDS, o->val.i = strtol(cp, NULL, 0);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
91 else {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
92 fprintf(stderr, "Unknown option '%s'\n", arg);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
93 free(o);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
94 return -1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
95 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
96
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
97 o->next = NULL;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
98
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
99 if (*opts) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
100 t = *opts;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
101 while (t->next)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
102 t = t->next;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
103 t->next = o;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
104 } else {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
105 *opts = o;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
106 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
107
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
108 return 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
109 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
110
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
111 int main(int argc, char *argv[])
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
112 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
113 samFile *in;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
114 char *fn_ref = 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
115 int flag = 0, c, clevel = -1, ignore_sam_err = 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
116 char moder[8];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
117 bam_hdr_t *h;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
118 bam1_t *b;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
119 htsFile *out;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
120 char modew[8];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
121 int r = 0, exit_code = 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
122 hts_opt *in_opts = NULL, *out_opts = NULL, *last = NULL;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
123
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
124 while ((c = getopt(argc, argv, "IbDCSl:t:i:o:")) >= 0) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
125 switch (c) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
126 case 'S': flag |= 1; break;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
127 case 'b': flag |= 2; break;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
128 case 'D': flag |= 4; break;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
129 case 'C': flag |= 8; break;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
130 case 'l': clevel = atoi(optarg); flag |= 2; break;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
131 case 't': fn_ref = optarg; break;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
132 case 'I': ignore_sam_err = 1; break;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
133 case 'i': if (add_option(&in_opts, optarg)) return 1; break;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
134 case 'o': if (add_option(&out_opts, optarg)) return 1; break;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
135 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
136 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
137 if (argc == optind) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
138 fprintf(stderr, "Usage: samview [-bSCSI] [-l level] [-o option=value] <in.bam>|<in.sam>|<in.cram> [region]\n");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
139 return 1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
140 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
141 strcpy(moder, "r");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
142 if (flag&4) strcat(moder, "c");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
143 else if ((flag&1) == 0) strcat(moder, "b");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
144
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
145 in = sam_open(argv[optind], moder);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
146 if (in == NULL) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
147 fprintf(stderr, "Error opening \"%s\"\n", argv[optind]);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
148 return EXIT_FAILURE;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
149 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
150 h = sam_hdr_read(in);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
151 h->ignore_sam_err = ignore_sam_err;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
152 b = bam_init1();
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
153
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
154 strcpy(modew, "w");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
155 if (clevel >= 0 && clevel <= 9) sprintf(modew + 1, "%d", clevel);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
156 if (flag&8) strcat(modew, "c");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
157 else if (flag&2) strcat(modew, "b");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
158 out = hts_open("-", modew);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
159 if (out == NULL) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
160 fprintf(stderr, "Error opening standard output\n");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
161 return EXIT_FAILURE;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
162 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
163
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
164 /* CRAM output */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
165 if (flag & 8) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
166 // Parse input header and use for CRAM output
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
167 out->fp.cram->header = sam_hdr_parse_(h->text, h->l_text);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
168
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
169 // Create CRAM references arrays
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
170 if (fn_ref)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
171 cram_set_option(out->fp.cram, CRAM_OPT_REFERENCE, fn_ref);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
172 else
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
173 // Attempt to fill out a cram->refs[] array from @SQ headers
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
174 cram_set_option(out->fp.cram, CRAM_OPT_REFERENCE, NULL);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
175 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
176
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
177 // Process any options; currently cram only.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
178 for (; in_opts; in_opts = (last=in_opts)->next, free(last)) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
179 hts_set_opt(in, in_opts->opt, in_opts->val);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
180 if (in_opts->opt == CRAM_OPT_REFERENCE)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
181 hts_set_opt(out, in_opts->opt, in_opts->val);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
182 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
183 for (; out_opts; out_opts = (last=out_opts)->next, free(last))
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
184 hts_set_opt(out, out_opts->opt, out_opts->val);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
185
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
186 sam_hdr_write(out, h);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
187 if (optind + 1 < argc && !(flag&1)) { // BAM input and has a region
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
188 int i;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
189 hts_idx_t *idx;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
190 if ((idx = bam_index_load(argv[optind])) == 0) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
191 fprintf(stderr, "[E::%s] fail to load the BAM index\n", __func__);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
192 return 1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
193 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
194 for (i = optind + 1; i < argc; ++i) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
195 hts_itr_t *iter;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
196 if ((iter = bam_itr_querys(idx, h, argv[i])) == 0) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
197 fprintf(stderr, "[E::%s] fail to parse region '%s'\n", __func__, argv[i]);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
198 continue;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
199 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
200 while ((r = bam_itr_next(in, iter, b)) >= 0) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
201 if (sam_write1(out, h, b) < 0) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
202 fprintf(stderr, "Error writing output.\n");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
203 exit_code = 1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
204 break;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
205 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
206 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
207 hts_itr_destroy(iter);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
208 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
209 hts_idx_destroy(idx);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
210 } else while ((r = sam_read1(in, h, b)) >= 0) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
211 if (sam_write1(out, h, b) < 0) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
212 fprintf(stderr, "Error writing output.\n");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
213 exit_code = 1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
214 break;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
215 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
216 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
217
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
218 if (r < -1) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
219 fprintf(stderr, "Error parsing input.\n");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
220 exit_code = 1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
221 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
222
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
223 r = sam_close(out);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
224 if (r < 0) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
225 fprintf(stderr, "Error closing output.\n");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
226 exit_code = 1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
227 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
228
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
229 bam_destroy1(b);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
230 bam_hdr_destroy(h);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
231
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
232 r = sam_close(in);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
233 if (r < 0) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
234 fprintf(stderr, "Error closing input.\n");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
235 exit_code = 1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
236 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
237
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
238 return exit_code;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
239 }