You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

141 line
4.9 KiB

  1. /* bcftools.h -- utility function declarations.
  2. Copyright (C) 2013-2021 Genome Research Ltd.
  3. Author: Petr Danecek <pd3@sanger.ac.uk>
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE. */
  19. #ifndef BCFTOOLS_H
  20. #define BCFTOOLS_H
  21. #include <stdarg.h>
  22. #include <htslib/hts_defs.h>
  23. #include <htslib/vcf.h>
  24. #include <math.h>
  25. #define FT_TAB_TEXT 0 // custom tab-delimited text file
  26. #define FT_GZ 1
  27. #define FT_VCF 2
  28. #define FT_VCF_GZ (FT_GZ|FT_VCF)
  29. #define FT_BCF (1<<2)
  30. #define FT_BCF_GZ (FT_GZ|FT_BCF)
  31. #define FT_STDIN (1<<3)
  32. char *bcftools_version(void);
  33. /// Report an error and exit -1
  34. void error(const char *format, ...) HTS_NORETURN HTS_FORMAT(HTS_PRINTF_FMT, 1, 2);
  35. /// Report an error and exit -1. If errno != 0, appends strerror(errno).
  36. // Note: unlike error() above, the message should not end with "\n" as a
  37. // newline will be added by the function.
  38. void error_errno(const char *format, ...) HTS_NORETURN HTS_FORMAT(HTS_PRINTF_FMT, 1, 2);
  39. void bcf_hdr_append_version(bcf_hdr_t *hdr, int argc, char **argv, const char *cmd);
  40. const char *hts_bcf_wmode(int file_type);
  41. const char *hts_bcf_wmode2(int file_type, char *fname);
  42. void set_wmode(char dst[8], int file_type, char *fname, int compression_level); // clevel: 0-9 with or zb type, -1 unset
  43. char *init_tmp_prefix(const char *prefix);
  44. void *smalloc(size_t size); // safe malloc
  45. static inline int iupac2bitmask(char iupac)
  46. {
  47. const int A = 1;
  48. const int C = 2;
  49. const int G = 4;
  50. const int T = 8;
  51. if ( iupac >= 97 ) iupac -= 32;
  52. if ( iupac == 'A' ) return A;
  53. if ( iupac == 'C' ) return C;
  54. if ( iupac == 'G' ) return G;
  55. if ( iupac == 'T' ) return T;
  56. if ( iupac == 'M' ) return A|C;
  57. if ( iupac == 'R' ) return A|G;
  58. if ( iupac == 'W' ) return A|T;
  59. if ( iupac == 'S' ) return C|G;
  60. if ( iupac == 'Y' ) return C|T;
  61. if ( iupac == 'K' ) return G|T;
  62. if ( iupac == 'V' ) return A|C|G;
  63. if ( iupac == 'H' ) return A|C|T;
  64. if ( iupac == 'D' ) return A|G|T;
  65. if ( iupac == 'B' ) return C|G|T;
  66. if ( iupac == 'N' ) return A|C|G|T;
  67. return -1;
  68. }
  69. static inline char bitmask2iupac(int bitmask)
  70. {
  71. const char iupac[16] = {'.','A','C','M','G','R','S','V','T','W','Y','H','K','D','B','N'};
  72. if ( bitmask <= 0 || bitmask > 15 ) return 0;
  73. return iupac[bitmask];
  74. }
  75. static inline int iupac_consistent(char iupac, char nt)
  76. {
  77. static const char iupac_mask[90] = {
  78. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  79. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  80. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,14,2,
  81. 13,0,0,4,11,0,0,12,0,3,15,0,0,0,5,6,8,0,7,9,0,10
  82. };
  83. if ( iupac > 89 ) return 0;
  84. if ( nt > 90 ) nt -= 32; // lowercase
  85. if ( nt=='A' ) nt = 1;
  86. else if ( nt=='C' ) nt = 2;
  87. else if ( nt=='G' ) nt = 4;
  88. else if ( nt=='T' ) nt = 8;
  89. return iupac_mask[(int)iupac] & nt ? 1 : 0;
  90. }
  91. static inline char nt_to_upper(char nt)
  92. {
  93. if ( nt < 97 ) return nt;
  94. return nt - 32;
  95. }
  96. static inline double phred_score(double prob)
  97. {
  98. if ( prob==0 ) return 99;
  99. prob = -4.3429*log(prob);
  100. return prob>99 ? 99 : prob;
  101. }
  102. static const uint64_t bcf_double_missing = 0x7ff0000000000001;
  103. static const uint64_t bcf_double_vector_end = 0x7ff0000000000002;
  104. static inline void bcf_double_set(double *ptr, uint64_t value)
  105. {
  106. union { uint64_t i; double d; } u;
  107. u.i = value;
  108. *ptr = u.d;
  109. }
  110. static inline int bcf_double_test(double d, uint64_t value)
  111. {
  112. union { uint64_t i; double d; } u;
  113. u.d = d;
  114. return u.i==value ? 1 : 0;
  115. }
  116. #define bcf_double_set_vector_end(x) bcf_double_set(&(x),bcf_double_vector_end)
  117. #define bcf_double_set_missing(x) bcf_double_set(&(x),bcf_double_missing)
  118. #define bcf_double_is_vector_end(x) bcf_double_test((x),bcf_double_vector_end)
  119. #define bcf_double_is_missing(x) bcf_double_test((x),bcf_double_missing)
  120. #define bcf_double_is_missing_or_vector_end(x) (bcf_double_test((x),bcf_double_missing) || bcf_double_test((x),bcf_double_vector_end))
  121. #endif