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