Debian Codegraph @

bcftools/version.c

raw symbols SCIP
1 /* version.c -- report version numbers for plugins.
2
3 Copyright (C) 2014-2024 Genome Research Ltd.
4
5 Author: Petr Danecek <[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 <stdarg.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <strings.h>
29 #include <errno.h>
30 #include <htslib/hts.h>
31 #include "bcftools.h"
32 #include "version.h"
33
34 void version(const char **bcftools_version, const char **htslib_version)
35 {
36 *bcftools_version = BCFTOOLS_VERSION;
37 *htslib_version = hts_version();
38 }
39
40 void error(const char *format, ...)
41 {
42 va_list ap;
43 va_start(ap, format);
44 vfprintf(stderr, format, ap);
45 va_end(ap);
46 exit(-1);
47 }
48
49 void error_errno(const char *format, ...)
50 {
51 va_list ap;
52 int e = errno;
53 va_start(ap, format);
54 vfprintf(stderr, format, ap);
55 va_end(ap);
56 if (e) {
57 fprintf(stderr, ": %s\n", strerror(e));
58 } else {
59 fprintf(stderr, "\n");
60 }
61 exit(-1);
62 }
63
64 const char *hts_bcf_wmode(int file_type)
65 {
66 if ( file_type == FT_BCF ) return "wbu"; // uncompressed BCF
67 if ( file_type & FT_BCF ) return "wb"; // compressed BCF
68 if ( file_type & FT_GZ ) return "wz"; // compressed VCF
69 return "w"; // uncompressed VCF
70 }
71
72 const char *hts_bcf_wmode2(int file_type, const char *fname)
73 {
74 if ( !fname ) return hts_bcf_wmode(file_type);
75 const char *end = fname ? strstr(fname, HTS_IDX_DELIM) : NULL;
76 if ( !end ) end = fname ? fname + strlen(fname) : fname;
77 int len = end - fname;
78 if ( len >= 4 && !strncasecmp(".bcf",fname+len-4,4) ) return hts_bcf_wmode(FT_BCF|FT_GZ);
79 if ( len >= 4 && !strncasecmp(".vcf",fname+len-4,4) ) return hts_bcf_wmode(FT_VCF);
80 if ( len >= 7 && !strncasecmp(".vcf.gz",fname+len-7,7) ) return hts_bcf_wmode(FT_VCF|FT_GZ);
81 if ( len >= 8 && !strncasecmp(".vcf.bgz",fname+len-8,8) ) return hts_bcf_wmode(FT_VCF|FT_GZ);
82 return hts_bcf_wmode(file_type);
83 }
84
85 void set_wmode(char dst[8], int file_type, const char *fname, int clevel)
86 {
87 const char *ret = NULL;
88 const char *end = fname ? strstr(fname, HTS_IDX_DELIM) : NULL;
89 if ( !end ) end = fname ? fname + strlen(fname) : fname;
90 int len = end - fname;
91 if ( len >= 4 && !strncasecmp(".bcf",fname+len-4,4) )
92 ret = hts_bcf_wmode(file_type & FT_BCF ? file_type : FT_BCF|FT_GZ);
93 else if ( len >= 4 && !strncasecmp(".vcf",fname+len-4,4) ) ret = hts_bcf_wmode(FT_VCF);
94 else if ( len >= 7 && !strncasecmp(".vcf.gz",fname+len-7,7) ) ret = hts_bcf_wmode(FT_VCF|FT_GZ);
95 else if ( len >= 8 && !strncasecmp(".vcf.bgz",fname+len-8,8) ) ret = hts_bcf_wmode(FT_VCF|FT_GZ);
96 else ret = hts_bcf_wmode(file_type);
97 if ( clevel>=0 && clevel<=9 )
98 {
99 if ( strchr(ret,'v') || strchr(ret,'u') ) error("Error: compression level (%d) cannot be set on uncompressed streams (%s)\n",clevel,fname);
100 len = strlen(ret);
101 if ( len>6 ) error("Fixme: %s\n", ret);
102 sprintf(dst, "%s%d", ret, clevel);
103 }
104 else
105 strcpy(dst, ret);
106 }
107
108 int parse_overlap_option(const char *arg)
109 {
110 if ( strcasecmp(arg, "pos") == 0 || strcmp(arg, "0") == 0 ) return 0;
111 else if ( strcasecmp(arg, "record") == 0 || strcmp(arg, "1") == 0 ) return 1;
112 else if ( strcasecmp(arg, "variant") == 0 || strcmp(arg, "2") == 0 ) return 2;
113 else return -1;
114 }
115
116 // Used to set args->write_index in CLI.
117 // It will be true if set correctly.
118 // Note due to HTS_FMT_CSI being zero we have to use an additional bit.
119 int write_index_parse(char *arg) {
120 int fmt = HTS_FMT_CSI;
121
122 if (arg) {
123 if (strcmp(arg, "csi") == 0 || strcmp(arg, "=csi") == 0)
124 fmt = HTS_FMT_CSI;
125 else if (strcmp(arg, "tbi") == 0 || strcmp(arg, "=tbi") == 0)
126 fmt = HTS_FMT_TBI;
127 else
128 return 0;
129 }
130
131 return 128 | fmt;
132 }
133
134 // See also samtools/sam_utils.c auto_index()
135 int init_index2(htsFile *fh, bcf_hdr_t *hdr, const char *fname,
136 char **idx_fname, int idx_fmt) {
137 // Nothing to do == success. This simplifies the main code simpler.
138 if (!idx_fmt)
139 return 0;
140
141 int min_shift;
142 char *idx_suffix;
143
144 if (idx_fmt && (idx_fmt&127) == HTS_FMT_TBI && fh->format.format == vcf) {
145 min_shift = 0; // TBI
146 idx_suffix = "tbi";
147 } else {
148 min_shift = 14; // CSI
149 idx_suffix = "csi";
150 }
151
152 if ( !fname || !*fname || !strcmp(fname, "-") ) return -1;
153
154 char *delim = strstr(fname, HTS_IDX_DELIM);
155 if (delim) {
156 delim += strlen(HTS_IDX_DELIM);
157 *idx_fname = strdup(delim);
158 if ( !*idx_fname ) return -1;
159
160 size_t l = strlen(*idx_fname);
161 if ( l >= 4 && strcmp(*idx_fname + l - 4, ".tbi")==0 ) min_shift = 0;
162 } else {
163 if ( !(*idx_fname = malloc(strlen(fname)+6)) ) return -1;
164 sprintf(*idx_fname, "%s.%s", fname, idx_suffix);
165 }
166
167 if ( bcf_idx_init(fh, hdr, min_shift, *idx_fname) < 0 ) return -1;
168
169 return 0;
170 }
171
172 int init_index(htsFile *fh, bcf_hdr_t *hdr, const char *fname, char **idx_fname)
173 {
174 return init_index2(fh,hdr, fname, idx_fname, HTS_FMT_CSI);
175 }