Gamgee
You miserable little maggot. I'll stove your head in!
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
genotype_utils.h
Go to the documentation of this file.
1 #ifndef gamgee__genotype_utils__guard
2 #define gamgee__genotype_utils__guard
3 
4 #include "hts_memory.h"
5 #include "utils.h"
6 #include "variant_field_type.h"
7 
8 #include "../missing.h"
9 
10 #include "htslib/vcf.h"
11 
12 #include <memory>
13 
14 namespace gamgee {
15 
16 namespace utils {
17 
18 using namespace std;
19 
26  inline uint32_t allele_count(const bcf_fmt_t* const format_ptr) {
27  return format_ptr->n;
28  }
29 
39  bool allele_missing(const bcf_fmt_t* const format_ptr, const uint8_t* data_ptr, const uint32_t allele_index);
40 
41  template<class TYPE>
42  inline int32_t allele_key(const uint8_t* data_ptr, const uint32_t allele_index, const TYPE missing, const TYPE vector_end) {
43  // mostly copied from htslib
44  const auto p = reinterpret_cast<const TYPE*>(data_ptr);
45  if ( !(p[allele_index]>>1) || p[allele_index]==missing ) {
46  return missing_values::int32;
47  }
48  else if ( p[allele_index] == vector_end ) {
49  return bcf_int32_vector_end;
50  }
51  return (p[allele_index]>>1)-1;
52  }
53 
62  inline int32_t allele_key(
63  const bcf_fmt_t* const format_ptr, const uint8_t* data_ptr, const uint32_t allele_index) {
64  switch (format_ptr->type) {
65  case BCF_BT_INT8:
66  return allele_key<int8_t>(data_ptr, allele_index, bcf_int8_missing, bcf_int8_vector_end);
67  case BCF_BT_INT16:
68  return allele_key<int16_t>(data_ptr, allele_index, bcf_int16_missing, bcf_int16_vector_end);
69  case BCF_BT_INT32:
70  return allele_key<int32_t>(data_ptr, allele_index, bcf_int32_missing, bcf_int32_vector_end);
71  default:
72  throw invalid_argument("unknown GT field type: " + to_string(format_ptr->type));
73  }
74  }
75 
84  vector<int32_t> allele_keys(const std::shared_ptr<bcf1_t>& body, const bcf_fmt_t* const format_ptr, const uint8_t* data_ptr);
85 
94  vector<string> allele_strings(const std::shared_ptr<bcf1_t>& body,
95  const bcf_fmt_t* const format_ptr, const uint8_t* data_ptr); // returns the actual alleles (e.g. A/T or TG/C or T/T/C or T/T/T/T/T ... )
96 
104  string allele_key_to_string(const std::shared_ptr<bcf1_t>& body, const int32_t key_index);
105 }
106 
107 }
108 
109 #endif
#define BCF_BT_INT32
Definition: vcf.h:122
#define bcf_int8_vector_end
Definition: vcf.h:750
#define bcf_int32_missing
Definition: vcf.h:756
vector< int32_t > allele_keys(const std::shared_ptr< bcf1_t > &body, const bcf_fmt_t *const format_ptr, const uint8_t *data_ptr, const TYPE missing, const TYPE vector_end)
Definition: genotype_utils.cpp:59
#define BCF_BT_INT8
Definition: vcf.h:120
#define bcf_int16_vector_end
Definition: vcf.h:751
constexpr auto int32
missing value for an int32
Definition: missing.h:18
Definition: vcf.h:136
#define bcf_int32_vector_end
Definition: vcf.h:752
vector< string > allele_strings(const std::shared_ptr< bcf1_t > &body, const bcf_fmt_t *const format_ptr, const uint8_t *data_ptr, const TYPE missing, const TYPE vector_end)
Definition: genotype_utils.cpp:99
bool allele_missing(const uint8_t *data_ptr, const uint32_t allele_index, const TYPE missing)
Definition: genotype_utils.cpp:38
int n
Definition: vcf.h:138
Definition: exceptions.h:9
int type
Definition: vcf.h:138
uint32_t allele_count(const bcf_fmt_t *const format_ptr)
Counts the genotype alleles.
Definition: genotype_utils.h:26
#define BCF_BT_INT16
Definition: vcf.h:121
#define bcf_int16_missing
Definition: vcf.h:755
string allele_key_to_string(const std::shared_ptr< bcf1_t > &body, const int32_t allele_int)
Returns the genotype allele string from this line.
Definition: genotype_utils.cpp:125
#define bcf_int8_missing
Definition: vcf.h:754
int32_t allele_key(const uint8_t *data_ptr, const uint32_t allele_index, const TYPE missing, const TYPE vector_end)
Definition: genotype_utils.h:42
bool missing(const bool value)
Returns true if bool is false (missing).
Definition: missing.h:23