1 /*  sam_utils.c -- various utilities internal to samtools.
2 
3     Copyright (C) 2014-2016, 2018, 2019 Genome Research Ltd.
4 
5     Author: John Marshall <jm18@sanger.ac.uk>
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 "samtools.h"
34 
35 static htsFile *samtools_stdout = NULL;
36 
autoflush_if_stdout(htsFile * fp,const char * fname)37 void autoflush_if_stdout(htsFile *fp, const char *fname) {
38     if (fname == NULL || strcmp(fname, "-") == 0) samtools_stdout = fp;
39 }
40 
release_autoflush(htsFile * fp)41 void release_autoflush(htsFile *fp) {
42     if (samtools_stdout == fp) samtools_stdout = NULL;
43 }
44 
vprint_error_core(const char * subcommand,const char * format,va_list args,const char * extra)45 static void vprint_error_core(const char *subcommand, const char *format, va_list args, const char *extra)
46 {
47     fflush(stdout);
48     if (samtools_stdout) hts_flush(samtools_stdout);
49 
50     if (subcommand && *subcommand) fprintf(stderr, "samtools %s: ", subcommand);
51     else fprintf(stderr, "samtools: ");
52     vfprintf(stderr, format, args);
53     if (extra) fprintf(stderr, ": %s\n", extra);
54     else fprintf(stderr, "\n");
55     fflush(stderr);
56 }
57 
print_error(const char * subcommand,const char * format,...)58 void print_error(const char *subcommand, const char *format, ...)
59 {
60     va_list args;
61     va_start(args, format);
62     vprint_error_core(subcommand, format, args, NULL);
63     va_end(args);
64 }
65 
print_error_errno(const char * subcommand,const char * format,...)66 void print_error_errno(const char *subcommand, const char *format, ...)
67 {
68     int err = errno;
69     va_list args;
70     va_start(args, format);
71     vprint_error_core(subcommand, format, args, err? strerror(err) : NULL);
72     va_end(args);
73 }
74 
check_sam_close(const char * subcmd,samFile * fp,const char * fname,const char * null_fname,int * retp)75 void check_sam_close(const char *subcmd, samFile *fp, const char *fname, const char *null_fname, int *retp)
76 {
77     release_autoflush(fp);
78     int r = sam_close(fp);
79     if (r >= 0) return;
80 
81     // TODO Need error infrastructure so we can print a message instead of r
82     if (fname) print_error(subcmd, "error closing \"%s\": %d", fname, r);
83     else print_error(subcmd, "error closing %s: %d", null_fname, r);
84 
85     *retp = EXIT_FAILURE;
86 }
87 
88 /* Pick an index suffix based on the output file descriptor type. */
idx_suffix(htsFile * fp)89 static char *idx_suffix(htsFile *fp) {
90     switch (fp->format.format) {
91     case sam:
92     case bam:
93         // Tough cheese if you wanted bai!
94         // New feature => mandatory new index too, for simplicity of CLI.
95         return "csi";
96 
97     case cram:
98         return "crai";
99 
100     default:
101         return NULL;
102     }
103 }
104 
105 /*
106  * Utility function to add an index to a file we've opened for write.
107  * NB: Call this after writing the header and before writing sequences.
108  *
109  * The returned index filename should be freed by the caller, but only
110  * after sam_idx_save has been called.
111  *
112  * Returns index filename on success,
113  *         NULL on failure.
114  */
auto_index(htsFile * fp,const char * fn,bam_hdr_t * header)115 char *auto_index(htsFile *fp, const char *fn, bam_hdr_t *header) {
116     char *fn_idx;
117     int min_shift = 14; /* CSI */
118     if (!fn || !*fn || strcmp(fn, "-") == 0)
119         return NULL;
120 
121     char *delim = strstr(fn, HTS_IDX_DELIM);
122     if (delim != NULL) {
123         delim += strlen(HTS_IDX_DELIM);
124 
125         fn_idx = strdup(delim);
126         if (!fn_idx)
127             return NULL;
128 
129         size_t l = strlen(fn_idx);
130         if (l >= 4 && strcmp(fn_idx + l - 4, ".bai") == 0)
131             min_shift = 0;
132     } else {
133         char *suffix = idx_suffix(fp);
134         if (!suffix)
135             return NULL;
136 
137         fn_idx = malloc(strlen(fn)+6);
138         if (!fn_idx)
139             return NULL;
140 
141         sprintf(fn_idx, "%s.%s", fn, suffix);
142     }
143 
144     if (sam_idx_init(fp, header, min_shift, fn_idx) < 0) {
145         print_error_errno("auto_index", "failed to open index \"%s\" for writing", fn_idx);
146         free(fn_idx);
147         return NULL;
148     }
149 
150     return fn_idx;
151 }
152