Debian Codegraph @

samtools/bam_quickcheck.c.pysam.c

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