Debian Codegraph @

samtools/bam_quickcheck.c

raw symbols SCIP
1 /* bam_quickcheck.c -- quickcheck subcommand.
2
3 Copyright (C) 2015-2017 Genome Research Ltd.
4
5 Author: Joshua C. Randall <[email protected]>
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 DEALINGS IN THE SOFTWARE. */
24
25 #include <config.h>
26
27 #include <htslib/hts.h>
28 #include <htslib/sam.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32
33 /* File status flags (zero means OK). It's possible for more than one to be
34 * set on a single file. The final exit status is the bitwise-or of the
35 * status of all the files. */
36 #define QC_FAIL_OPEN 2
37 #define QC_NOT_SEQUENCE 4
38 #define QC_BAD_HEADER 8
39 #define QC_NO_EOF_BLOCK 16
40 #define QC_FAIL_CLOSE 32
41
42 static void usage_quickcheck(FILE *write_to)
43 {
44 fprintf(write_to,
45 "Usage: samtools quickcheck [options] <input> [...]\n"
46 "Options:\n"
47 " -v verbose output (repeat for more verbosity)\n"
48 " -q suppress warning messages\n"
49 " -u unmapped input (do not require targets in header)\n"
50 "\n"
51 "Notes:\n"
52 "\n"
53 "1. By default quickcheck will emit a warning message if and only if a file\n"
54 " fails the checks, in which case the exit status is non-zero. Under normal\n"
55 " behaviour with valid data it will be silent and has a zero exit status.\n"
56 " The warning messages are purely for manual inspection and should not be \n"
57 " parsed by scripts.\n"
58 "\n"
59 "2. In order to use this command programmatically, you should check its exit\n"
60 " status. One way to use quickcheck might be as a check that all BAM files in\n"
61 " a directory are okay:\n"
62 "\n"
63 "\tsamtools quickcheck *.bam && echo 'all ok' \\\n"
64 "\t || echo 'fail!'\n"
65 "\n"
66 " The first level of verbosity lists only files that fail to stdout.\n"
67 " To obtain a parsable list of files that have failed, use this option:\n"
68 "\n"
69 "\tsamtools quickcheck -qv *.bam > bad_bams.fofn \\\n"
70 "\t && echo 'all ok' \\\n"
71 "\t || echo 'some files failed check, see bad_bams.fofn'\n"
72 );
73 }
74
75 #define QC_ERR(state, v, msg, arg1) \
76 file_state |= (state); \
77 if (!quiet || verbose >= (v)) fprintf(stderr, (msg), (arg1))
78
79 int main_quickcheck(int argc, char** argv)
80 {
81 int verbose = 0, quiet = 0, unmapped = 0;
82 hts_verbose = 0;
83
84 const char* optstring = "vqu";
85 int opt;
86 while ((opt = getopt(argc, argv, optstring)) != -1) {
87 switch (opt) {
88 case 'u':
89 unmapped = 1;
90 break;
91 case 'v':
92 verbose++;
93 break;
94 case 'q':
95 quiet = 1;
96 break;
97 default:
98 usage_quickcheck(stderr);
99 return 1;
100 }
101 }
102
103 argc -= optind;
104 argv += optind;
105
106 if (argc < 1) {
107 usage_quickcheck(stdout);
108 return 1;
109 }
110
111 if (verbose >= 2) {
112 fprintf(stderr, "verbosity set to %d\n", verbose);
113 }
114
115 if (verbose >= 4) {
116 hts_verbose = 3;
117 }
118
119 int ret = 0;
120 int i;
121
122 for (i = 0; i < argc; i++) {
123 char* fn = argv[i];
124 int file_state = 0;
125
126 if (verbose >= 3) fprintf(stderr, "checking %s\n", fn);
127
128 // attempt to open
129 htsFile *hts_fp = hts_open(fn, "r");
130 if (hts_fp == NULL) {
131 QC_ERR(QC_FAIL_OPEN, 2, "%s could not be opened for reading.\n", fn);
132 }
133 else {
134 if (verbose >= 3) fprintf(stderr, "opened %s\n", fn);
135 // make sure we have sequence data
136 const htsFormat *fmt = hts_get_format(hts_fp);
137 if (fmt->category != sequence_data ) {
138 QC_ERR(QC_NOT_SEQUENCE, 2, "%s was not identified as sequence data.\n", fn);
139 }
140 else {
141 if (verbose >= 3) fprintf(stderr, "%s is sequence data\n", fn);
142 // check header
143 sam_hdr_t *header = sam_hdr_read(hts_fp);
144 if (header == NULL) {
145 QC_ERR(QC_BAD_HEADER, 2, "%s caused an error whilst reading its header.\n", fn);
146 } else {
147 if (!unmapped && sam_hdr_nref(header) <= 0) {
148 QC_ERR(QC_BAD_HEADER, 2, "%s had no targets in header.\n", fn);
149 }
150 else {
151 if (verbose >= 3) fprintf(stderr, "%s has %d targets in header.\n", fn, sam_hdr_nref(header));
152 }
153 sam_hdr_destroy(header);
154 }
155 }
156 // check EOF on formats that support this
157 int ret;
158 if ((ret = hts_check_EOF(hts_fp)) < 0) {
159 QC_ERR(QC_NO_EOF_BLOCK, 2, "%s caused an error whilst checking for EOF block.\n", fn);
160 }
161 else {
162 switch (ret) {
163 case 0:
164 QC_ERR(QC_NO_EOF_BLOCK, 2, "%s was missing EOF block when one should be present.\n", fn);
165 break;
166 case 1:
167 if (verbose >= 3) fprintf(stderr, "%s has good EOF block.\n", fn);
168 break;
169 case 2:
170 if (verbose >= 3) fprintf(stderr, "%s cannot be checked for EOF block as it is not seekable.\n", fn);
171 break;
172 case 3:
173 if (verbose >= 3) fprintf(stderr, "%s cannot be checked for EOF block because its filetype does not contain one.\n", fn);
174 break;
175 }
176 }
177
178 if (hts_close(hts_fp) < 0) {
179 QC_ERR(QC_FAIL_CLOSE, 2, "%s did not close cleanly.\n", fn);
180 }
181 }
182
183 if (file_state > 0 && verbose >= 1) {
184 fprintf(stdout, "%s\n", fn);
185 }
186 ret |= file_state;
187 }
188
189 return ret;
190 }