| 1 |
#include "samtools.pysam.h" |
| 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 |
#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 |
|
| 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 |
|
| 92 |
static char *idx_suffix(htsFile *fp) { |
| 93 |
switch (fp->format.format) { |
| 94 |
case sam: |
| 95 |
case bam: |
| 96 |
|
| 97 |
|
| 98 |
return "csi"; |
| 99 |
|
| 100 |
case cram: |
| 101 |
return "crai"; |
| 102 |
|
| 103 |
default: |
| 104 |
return NULL; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
char *auto_index(htsFile *fp, const char *fn, bam_hdr_t *header) { |
| 119 |
char *fn_idx; |
| 120 |
int min_shift = 14; |
| 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 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 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 == ',') |
| 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 |
} |