Debian Codegraph @

samtools/sam_utils.c

raw symbols SCIP
1 /* sam_utils.c -- various utilities internal to samtools.
2
3 Copyright (C) 2014-2016, 2018, 2019, 2023 Genome Research Ltd.
4
5 Author: John Marshall <[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 #include <stdlib.h>
27
28 #include <stdio.h>
29 #include <stdarg.h>
30 #include <string.h>
31 #include <errno.h>
32
33 #include "sam_utils.h"
34
35 static htsFile *samtools_stdout = NULL;
36
37 void autoflush_if_stdout(htsFile *fp, const char *fname) {
38 if (fname == NULL || strcmp(fname, "-") == 0) samtools_stdout = fp;
39 }
40
41 void release_autoflush(htsFile *fp) {
42 if (samtools_stdout == fp) samtools_stdout = NULL;
43 }
44
45 static void HTS_FORMAT(HTS_PRINTF_FMT, 2, 0)
46 vprint_error_core(const char *subcommand, const char *format, va_list args, const char *extra)
47 {
48 fflush(stdout);
49 if (samtools_stdout) hts_flush(samtools_stdout);
50
51 if (subcommand && *subcommand) fprintf(stderr, "samtools %s: ", subcommand);
52 else fprintf(stderr, "samtools: ");
53 vfprintf(stderr, format, args);
54 if (extra) fprintf(stderr, ": %s\n", extra);
55 else fprintf(stderr, "\n");
56 fflush(stderr);
57 }
58
59 void print_error(const char *subcommand, const char *format, ...)
60 {
61 va_list args;
62 va_start(args, format);
63 vprint_error_core(subcommand, format, args, NULL);
64 va_end(args);
65 }
66
67 void print_error_errno(const char *subcommand, const char *format, ...)
68 {
69 int err = errno;
70 va_list args;
71 va_start(args, format);
72 vprint_error_core(subcommand, format, args, err? strerror(err) : NULL);
73 va_end(args);
74 }
75
76 void check_sam_close(const char *subcmd, samFile *fp, const char *fname, const char *null_fname, int *retp)
77 {
78 release_autoflush(fp);
79 int r = sam_close(fp);
80 if (r >= 0) return;
81
82 // TODO Need error infrastructure so we can print a message instead of r
83 if (fname) print_error(subcmd, "error closing \"%s\": %d", fname, r);
84 else print_error(subcmd, "error closing %s: %d", null_fname, r);
85
86 *retp = EXIT_FAILURE;
87 }
88
89 /* Pick an index suffix based on the output file descriptor type. */
90 static char *idx_suffix(htsFile *fp) {
91 switch (fp->format.format) {
92 case sam:
93 case bam:
94 // Tough cheese if you wanted bai!
95 // New feature => mandatory new index too, for simplicity of CLI.
96 return "csi";
97
98 case cram:
99 return "crai";
100
101 default:
102 return NULL;
103 }
104 }
105
106 /*
107 * Utility function to add an index to a file we've opened for write.
108 * NB: Call this after writing the header and before writing sequences.
109 *
110 * The returned index filename should be freed by the caller, but only
111 * after sam_idx_save has been called.
112 *
113 * Returns index filename on success,
114 * NULL on failure.
115 */
116 char *auto_index(htsFile *fp, const char *fn, bam_hdr_t *header) {
117 char *fn_idx;
118 int min_shift = 14; /* CSI */
119 if (!fn || !*fn || strcmp(fn, "-") == 0)
120 return NULL;
121
122 char *delim = strstr(fn, HTS_IDX_DELIM);
123 if (delim != NULL) {
124 delim += strlen(HTS_IDX_DELIM);
125
126 fn_idx = strdup(delim);
127 if (!fn_idx)
128 return NULL;
129
130 size_t l = strlen(fn_idx);
131 if (l >= 4 && strcmp(fn_idx + l - 4, ".bai") == 0)
132 min_shift = 0;
133 } else {
134 char *suffix = idx_suffix(fp);
135 if (!suffix)
136 return NULL;
137
138 fn_idx = malloc(strlen(fn)+6);
139 if (!fn_idx)
140 return NULL;
141
142 sprintf(fn_idx, "%s.%s", fn, suffix);
143 }
144
145 if (sam_idx_init(fp, header, min_shift, fn_idx) < 0) {
146 print_error_errno("auto_index", "failed to open index \"%s\" for writing", fn_idx);
147 free(fn_idx);
148 return NULL;
149 }
150
151 return fn_idx;
152 }
153
154
155 /// parse_aux_list - parses given string for aux tags which are ',' separated
156 /** @param h - pointer to a SET holding aux tags
157 * @param optarg - string having the ',' separated aux tags
158 * @param msgheader - string to be used during error output as a header
159 returns -1 on failure and 0 on success
160 moved from sam_view.c to here for common usage at different source files
161 */
162 int parse_aux_list(auxhash_t *h, char *optarg, const char *msgheader)
163 {
164 if (!*h)
165 *h = kh_init(aux_exists);
166
167 while (strlen(optarg) >= 2) {
168 int x = optarg[0]<<8 | optarg[1];
169 int ret = 0;
170 kh_put(aux_exists, *h, x, &ret);
171 if (ret < 0) {
172 kh_destroy(aux_exists, *h);
173 *h = NULL;
174 return -1;
175 }
176
177 optarg += 2;
178 if (*optarg == ',') // allow white-space too for easy `cat file`?
179 optarg++;
180 else if (*optarg != 0)
181 break;
182 }
183
184 if (strlen(optarg) != 0) {
185 fprintf(stderr, "%s: Error parsing option, "
186 "auxiliary tags should be exactly two characters long.\n", msgheader ? msgheader : "");
187 kh_destroy(aux_exists, *h);
188 *h = NULL;
189 return -1;
190 }
191
192 return 0;
193 }