Gamgee
You miserable little maggot. I'll stove your head in!
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
individual_field.h
Go to the documentation of this file.
1 #ifndef gamgee__individual_field__guard
2 #define gamgee__individual_field__guard
3 
4 #include <iostream>
5 
7 
8 #include "../utils/hts_memory.h"
9 #include "../utils/utils.h"
10 
11 #include "htslib/vcf.h"
12 
13 #include <memory>
14 #include <sstream>
15 #include <stdexcept>
16 
17 namespace gamgee {
18 
64 template<class TYPE>
66  public:
67 
73  IndividualField() : m_body {nullptr}, m_format_ptr {nullptr} {}
74 
81  explicit IndividualField(const std::shared_ptr<bcf1_t>& body, bcf_fmt_t* format_ptr) :
82  m_body {body}, m_format_ptr {format_ptr}
83  {}
84 
88  IndividualField(const IndividualField& other) = delete;
89 
94  IndividualField(IndividualField&& other) = default;
98  IndividualField& operator=(const IndividualField& other) = delete;
99 
104  IndividualField& operator=(IndividualField&& other) = default;
105 
111  bool operator==(const IndividualField& other) const {
112  if (this == &other)
113  return true;
114  if (size() != other.size())
115  return false;
116  for (auto i=0u; i != size(); ++i) {
117  if (operator[](i) != other[i])
118  return false;
119  }
120  return true;
121  }
122 
128  bool operator!=(const IndividualField& other) const {
129  return !(*this == other);
130  }
131 
139  TYPE operator[](const uint32_t sample) const {
140  if (empty())
141  throw std::out_of_range("Tried to index an individual field that is missing with operator[]");
142  utils::check_max_boundary(sample, m_body->n_sample);
143  return TYPE{m_body, m_format_ptr, m_format_ptr->p + (sample * m_format_ptr->size)};
144  }
145 
150  return IndividualFieldIterator<TYPE>{m_body, m_format_ptr};
151  }
152 
157  return IndividualFieldIterator<TYPE>{m_body, m_format_ptr, true};
158  }
159 
160  uint32_t size() const { return empty() ? 0 : m_body->n_sample; }
161  uint32_t n_samples() const { return size(); }
162  uint32_t empty() const { return m_body == nullptr; }
163  uint32_t missing() const { return empty();}
164  TYPE front() const { return operator[](0); }
165  TYPE back() const { return operator[](m_body->n_sample - 1); }
166 
167  private:
168  std::shared_ptr<bcf1_t> m_body;
169  bcf_fmt_t* m_format_ptr;
170 };
171 
172 
173 }
174 
175 #endif // gamgee__individual_field__guard
bool operator==(const IndividualField &other) const
compares two IndividualField objects in the following order: memory address, size and values...
Definition: individual_field.h:111
uint32_t missing() const
checks if the object is empty.
Definition: individual_field.h:163
IndividualField & operator=(const IndividualField &other)=delete
copying of the IndividualField object is not allowed. Use move constructor instead.
TYPE operator[](const uint32_t sample) const
random access to the value of a given sample for reading or writing
Definition: individual_field.h:139
TYPE front() const
convenience function to access the first element
Definition: individual_field.h:164
uint8_t * p
Definition: vcf.h:139
void check_max_boundary(const uint32_t index, const uint32_t size, const std::string &prefix_msg)
checks that an index is greater than or equal to size
Definition: utils.h:63
bool operator!=(const IndividualField &other) const
compares two IndividualField objects in the following order: memory address, size and values...
Definition: individual_field.h:128
uint32_t n_samples() const
just an alias to size() to simplify interfaces
Definition: individual_field.h:161
uint32_t empty() const
checks if the object is empty.
Definition: individual_field.h:162
IndividualFieldIterator< TYPE > end() const
an iterator to the end of the object
Definition: individual_field.h:156
IndividualField()
default constructor of an empty IndividualField
Definition: individual_field.h:73
TYPE back() const
convenience function to access the last element
Definition: individual_field.h:165
Definition: vcf.h:136
A class template to hold the values of a specific Variant's format field for all samples.
Definition: individual_field.h:65
IndividualField(const std::shared_ptr< bcf1_t > &body, bcf_fmt_t *format_ptr)
creates a new format field object pointing to the shared byte array inside the Variant object ...
Definition: individual_field.h:81
Definition: exceptions.h:9
IndividualFieldIterator< TYPE > begin() const
an iterator to the beginning of the object
Definition: individual_field.h:149
iterator for VariantField objects.
Definition: individual_field_iterator.h:28
uint32_t size() const
the number of values in this IndividualField
Definition: individual_field.h:160