A library for working with phylogenetic and population genetic data.
v0.32.0
vcf_format_iterator.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2024 Lucas Czech
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  Contact:
19  Lucas Czech <lczech@carnegiescience.edu>
20  Department of Plant Biology, Carnegie Institution For Science
21  260 Panama Street, Stanford, CA 94305, USA
22 */
23 
31 #ifdef GENESIS_HTSLIB
32 
34 
35 extern "C" {
36  #include <htslib/hts.h>
37  #include <htslib/vcf.h>
38 }
39 
40 #include <cassert>
41 #include <cstdlib>
42 #include <cstring>
43 #include <stdexcept>
44 
45 namespace genesis {
46 namespace population {
47 
48 // =================================================================================================
49 // VCF/BCF Format Helper Constants
50 // =================================================================================================
51 
52 VcfFormatHelper::VcfFormatHelper()
53 {
54  // Vector End and Missing constants
55  static_assert(
56  VcfFormatHelper::bcf_int8_vector_end_ == bcf_int8_vector_end,
57  "Definitions of bcf_int8_vector_end in htslib and of VcfFormatHelper::bcf_int8_vector_end_ "
58  "in genesis differ. Please submit a bug report at https://github.com/lczech/genesis/issues"
59  );
60  static_assert(
61  VcfFormatHelper::bcf_int16_vector_end_ == bcf_int16_vector_end,
62  "Definitions of bcf_int16_vector_end in htslib and of VcfFormatHelper::bcf_int16_vector_end_ "
63  "in genesis differ. Please submit a bug report at https://github.com/lczech/genesis/issues"
64  );
65  static_assert(
66  VcfFormatHelper::bcf_int32_vector_end_ == bcf_int32_vector_end,
67  "Definitions of bcf_int32_vector_end in htslib and of VcfFormatHelper::bcf_int32_vector_end_ "
68  "in genesis differ. Please submit a bug report at https://github.com/lczech/genesis/issues"
69  );
70  static_assert(
71  VcfFormatHelper::bcf_int64_vector_end_ == bcf_int64_vector_end,
72  "Definitions of bcf_int64_vector_end in htslib and of VcfFormatHelper::bcf_int64_vector_end_ "
73  "in genesis differ. Please submit a bug report at https://github.com/lczech/genesis/issues"
74  );
75  static_assert(
76  VcfFormatHelper::bcf_str_vector_end_ == bcf_str_vector_end,
77  "Definitions of bcf_str_vector_end in htslib and of VcfFormatHelper::bcf_str_vector_end_ "
78  "in genesis differ. Please submit a bug report at https://github.com/lczech/genesis/issues"
79  );
80  static_assert(
81  VcfFormatHelper::bcf_int8_missing_ == bcf_int8_missing,
82  "Definitions of bcf_int8_missing in htslib and of VcfFormatHelper::bcf_int8_missing_ "
83  "in genesis differ. Please submit a bug report at https://github.com/lczech/genesis/issues"
84  );
85  static_assert(
86  VcfFormatHelper::bcf_int16_missing_ == bcf_int16_missing,
87  "Definitions of bcf_int16_missing in htslib and of VcfFormatHelper::bcf_int16_missing_ "
88  "in genesis differ. Please submit a bug report at https://github.com/lczech/genesis/issues"
89  );
90  static_assert(
91  VcfFormatHelper::bcf_int32_missing_ == bcf_int32_missing,
92  "Definitions of bcf_int32_missing in htslib and of VcfFormatHelper::bcf_int32_missing_ "
93  "in genesis differ. Please submit a bug report at https://github.com/lczech/genesis/issues"
94  );
95  static_assert(
96  VcfFormatHelper::bcf_int64_missing_ == bcf_int64_missing,
97  "Definitions of bcf_int64_missing in htslib and of VcfFormatHelper::bcf_int64_missing_ "
98  "in genesis differ. Please submit a bug report at https://github.com/lczech/genesis/issues"
99  );
100  static_assert(
101  VcfFormatHelper::bcf_str_missing_ == bcf_str_missing,
102  "Definitions of bcf_str_missing in htslib and of VcfFormatHelper::bcf_str_missing_ "
103  "in genesis differ. Please submit a bug report at https://github.com/lczech/genesis/issues"
104  );
105 }
106 
107 // =================================================================================================
108 // VCF/BCF Format Helper Static Helper Functions
109 // =================================================================================================
110 
111 int32_t VcfFormatHelper::bcf_hdr_nsamples_( ::bcf_hdr_t const* header )
112 {
113  return bcf_hdr_nsamples( header );
114 }
115 
116 std::string VcfFormatHelper::hdr_sample_name_( ::bcf_hdr_t const* header, size_t index )
117 {
118  assert( bcf_hdr_nsamples(header) == header->n[BCF_DT_SAMPLE] );
119  auto const smp_cnt = header->n[BCF_DT_SAMPLE];
120  if( static_cast<long>( index ) >= smp_cnt ) {
121  throw std::invalid_argument(
122  "Cannot get sample name for sample at index " + std::to_string(index) +
123  ", as the VCF/BCF file only uses " + std::to_string( smp_cnt ) + " samples."
124  );
125  }
126  return header->samples[index];
127 }
128 
129 int VcfFormatHelper::bcf_get_format_string_(
130  const bcf_hdr_t *hdr, bcf1_t *line, const char *tag, char ***dst, int *ndst
131 ) {
132  return bcf_get_format_string( hdr, line, tag, dst, ndst );
133 }
134 
135 int VcfFormatHelper::bcf_get_format_values_(
136  const bcf_hdr_t *hdr, bcf1_t *line, const char *tag, void **dst, int *ndst, int type
137 ) {
138  return bcf_get_format_values( hdr, line, tag, dst, ndst, type);
139 }
140 
141 int VcfFormatHelper::bcf_get_genotypes_( const bcf_hdr_t *hdr, bcf1_t *line, void **dst, int *ndst )
142 {
143  return bcf_get_genotypes( hdr, line, dst, ndst );
144 }
145 
146 int VcfFormatHelper::bcf_float_is_vector_end_(float f)
147 {
148  return bcf_float_is_vector_end(f);
149 }
150 
151 int VcfFormatHelper::bcf_float_is_missing_(float f)
152 {
153  return bcf_float_is_missing(f);
154 }
155 
156 } // namespace population
157 } // namespace genesis
158 
159 #endif // htslib guard
genesis::population::to_string
std::string to_string(GenomeLocus const &locus)
Definition: function/genome_locus.hpp:52
vcf_format_iterator.hpp
genesis
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Definition: placement/formats/edge_color.cpp:42