1 #include "bcftools.pysam.h"
2 
3 /*  version.c -- report version numbers for plugins.
4 
5     Copyright (C) 2014-2021 Genome Research Ltd.
6 
7     Author: Petr Danecek <pd3@sanger.ac.uk>
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 
version(const char ** bcftools_version,const char ** htslib_version)36 void version(const char **bcftools_version, const char **htslib_version)
37 {
38     *bcftools_version = BCFTOOLS_VERSION;
39     *htslib_version = hts_version();
40 }
41 
error(const char * format,...)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 
error_errno(const char * format,...)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 
hts_bcf_wmode(int file_type)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 
hts_bcf_wmode2(int file_type,char * fname)74 const char *hts_bcf_wmode2(int file_type, char *fname)
75 {
76     if ( !fname ) return hts_bcf_wmode(file_type);
77     int len = strlen(fname);
78     if ( len >= 4 && !strcasecmp(".bcf",fname+len-4) ) return hts_bcf_wmode(FT_BCF|FT_GZ);
79     if ( len >= 4 && !strcasecmp(".vcf",fname+len-4) ) return hts_bcf_wmode(FT_VCF);
80     if ( len >= 7 && !strcasecmp(".vcf.gz",fname+len-7) ) return hts_bcf_wmode(FT_VCF|FT_GZ);
81     if ( len >= 8 && !strcasecmp(".vcf.bgz",fname+len-8) ) return hts_bcf_wmode(FT_VCF|FT_GZ);
82     return hts_bcf_wmode(file_type);
83 }
84 
set_wmode(char dst[8],int file_type,char * fname,int clevel)85 void set_wmode(char dst[8], int file_type, char *fname, int clevel)
86 {
87     const char *ret = NULL;
88     int len = fname ? strlen(fname) : 0;
89     if ( len >= 4 && !strcasecmp(".bcf",fname+len-4) ) ret = hts_bcf_wmode(FT_BCF|FT_GZ);
90     else if ( len >= 4 && !strcasecmp(".vcf",fname+len-4) ) ret = hts_bcf_wmode(FT_VCF);
91     else if ( len >= 7 && !strcasecmp(".vcf.gz",fname+len-7) ) ret = hts_bcf_wmode(FT_VCF|FT_GZ);
92     else if ( len >= 8 && !strcasecmp(".vcf.bgz",fname+len-8) ) ret = hts_bcf_wmode(FT_VCF|FT_GZ);
93     else ret = hts_bcf_wmode(file_type);
94     if ( clevel>=0 && clevel<=9 )
95     {
96         if ( strchr(ret,'v') || strchr(ret,'u') ) error("Error: compression level (%d) cannot be set on uncompressed streams (%s)\n",clevel,fname);
97         len = strlen(ret);
98         if ( len>6 ) error("Fixme: %s\n", ret);
99         sprintf(dst, "%s%d", ret, clevel);
100     }
101     else
102         strcpy(dst, ret);
103 }
104 
105