annotate ezBAMQC/src/htslib/htsfile.c @ 11:5bfcc6c131ed

Uploaded
author cshl-bsr
date Wed, 30 Mar 2016 12:14:21 -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 /* htsfile.c -- file identifier and minimal viewer.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
2
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
3 Copyright (C) 2014-2015 Genome Research Ltd.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
4
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
5 Author: John Marshall <jm18@sanger.ac.uk>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
6
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
7 Permission is hereby granted, free of charge, to any person obtaining a copy
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
8 of this software and associated documentation files (the "Software"), to deal
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
9 in the Software without restriction, including without limitation the rights
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
11 copies of the Software, and to permit persons to whom the Software is
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
12 furnished to do so, subject to the following conditions:
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
13
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
14 The above copyright notice and this permission notice shall be included in
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
15 all copies or substantial portions of the Software.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
16
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
23 DEALINGS IN THE SOFTWARE. */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
24
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
25 #include <errno.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
26 #include <stdio.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
27 #include <stdlib.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
28 #include <getopt.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
29 #include <unistd.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
30
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
31 #include "htslib/hfile.h"
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
32 #include "htslib/hts.h"
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
33 #include "htslib/sam.h"
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
34 #include "htslib/vcf.h"
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
35
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
36 enum { identify, view_headers, view_all } mode = identify;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
37 int show_headers = 1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
38
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
39 static htsFile *dup_stdout(const char *mode)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
40 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
41 int fd = dup(STDOUT_FILENO);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
42 hFILE *hfp = (fd >= 0)? hdopen(fd, mode) : NULL;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
43 return hfp? hts_hopen(hfp, "-", mode) : NULL;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
44 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
45
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
46 static int view_sam(hFILE *hfp, const char *filename)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
47 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
48 samFile *in = hts_hopen(hfp, filename, "r");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
49 if (in == NULL) return 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
50 samFile *out = dup_stdout("w");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
51 bam_hdr_t *hdr = sam_hdr_read(in);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
52
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
53 if (show_headers) sam_hdr_write(out, hdr);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
54 if (mode == view_all) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
55 bam1_t *b = bam_init1();
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
56 while (sam_read1(in, hdr, b) >= 0)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
57 sam_write1(out, hdr, b);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
58 bam_destroy1(b);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
59 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
60
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
61 bam_hdr_destroy(hdr);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
62 hts_close(out);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
63 hts_close(in);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
64 return 1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
65 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
66
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
67 static int view_vcf(hFILE *hfp, const char *filename)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
68 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
69 vcfFile *in = hts_hopen(hfp, filename, "r");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
70 if (in == NULL) return 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
71 vcfFile *out = dup_stdout("w");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
72 bcf_hdr_t *hdr = bcf_hdr_read(in);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
73
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
74 if (show_headers) bcf_hdr_write(out, hdr);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
75 if (mode == view_all) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
76 bcf1_t *rec = bcf_init();
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
77 while (bcf_read(in, hdr, rec) >= 0)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
78 bcf_write(out, hdr, rec);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
79 bcf_destroy(rec);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
80 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
81
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
82 bcf_hdr_destroy(hdr);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
83 hts_close(out);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
84 hts_close(in);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
85 return 1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
86 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
87
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
88 static void usage(FILE *fp, int status)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
89 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
90 fprintf(fp,
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
91 "Usage: htsfile [-chH] FILE...\n"
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
92 "Options:\n"
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
93 " -c, --view Write textual form of FILEs to standard output\n"
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
94 " -h, --header-only Display only headers in view mode, not records\n"
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
95 " -H, --no-header Suppress header display in view mode\n");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
96 exit(status);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
97 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
98
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
99 int main(int argc, char **argv)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
100 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
101 static const struct option options[] = {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
102 { "header-only", no_argument, NULL, 'h' },
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
103 { "no-header", no_argument, NULL, 'H' },
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
104 { "view", no_argument, NULL, 'c' },
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
105 { "help", no_argument, NULL, '?' },
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
106 { "version", no_argument, NULL, 1 },
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
107 { NULL, 0, NULL, 0 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
108 };
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
109
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
110 int status = EXIT_SUCCESS;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
111 int c, i;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
112 while ((c = getopt_long(argc, argv, "chH?", options, NULL)) >= 0)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
113 switch (c) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
114 case 'c': mode = view_all; break;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
115 case 'h': mode = view_headers; show_headers = 1; break;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
116 case 'H': show_headers = 0; break;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
117 case 1:
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
118 printf(
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
119 "htsfile (htslib) %s\n"
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
120 "Copyright (C) 2015 Genome Research Ltd.\n",
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
121 hts_version());
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
122 exit(EXIT_SUCCESS);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
123 break;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
124 case '?': usage(stdout, EXIT_SUCCESS); break;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
125 default: usage(stderr, EXIT_FAILURE); break;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
126 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
127
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
128 if (optind == argc) usage(stderr, EXIT_FAILURE);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
129
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
130 for (i = optind; i < argc; i++) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
131 htsFormat fmt;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
132 hFILE *fp = hopen(argv[i], "r");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
133 if (fp == NULL) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
134 fprintf(stderr, "htsfile: can't open \"%s\": %s\n", argv[i], strerror(errno));
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
135 status = EXIT_FAILURE;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
136 continue;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
137 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
138
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
139 if (hts_detect_format(fp, &fmt) < 0) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
140 fprintf(stderr, "htsfile: detecting \"%s\" format failed: %s\n", argv[i], strerror(errno));
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
141 hclose_abruptly(fp);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
142 status = EXIT_FAILURE;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
143 continue;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
144 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
145
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
146 if (mode == identify) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
147 char *description = hts_format_description(&fmt);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
148 printf("%s:\t%s\n", argv[i], description);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
149 free(description);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
150 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
151 else
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
152 switch (fmt.category) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
153 case sequence_data: if (view_sam(fp, argv[i])) fp = NULL; break;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
154 case variant_data: if (view_vcf(fp, argv[i])) fp = NULL; break;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
155 default:
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
156 fprintf(stderr, "htsfile: can't view %s: unknown format\n", argv[i]);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
157 status = EXIT_FAILURE;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
158 break;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
159 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
160
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
161 if (fp && hclose(fp) < 0) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
162 fprintf(stderr, "htsfile: closing %s failed\n", argv[i]);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
163 status = EXIT_FAILURE;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
164 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
165 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
166
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
167 return status;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
168 }