Debian Codegraph @

bcftools/version.c.pysam.c

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