Debian Codegraph @

samtools/sam_utils.c.pysam.c

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