Gamgee
You miserable little maggot. I'll stove your head in!
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
merged_vcf_lut.h
Go to the documentation of this file.
1 #ifndef __gamgee_merged_vcf_lut__
2 #define __gamgee_merged_vcf_lut__
3 
4 #include <assert.h>
5 #include <vector>
6 
7 #include "htslib/vcf.h"
8 
9 #include "../missing.h"
10 #include "../variant/variant_header.h"
11 
12 //forward declaration for friend function of MergedVCFLUTBase
13 template<bool inputs_2_merged_LUT_is_input_ordered, bool merged_2_inputs_LUT_is_input_ordered>
14 void test_lut_base();
15 
16 namespace gamgee
17 {
18  //forward declaration of friend class of MergedVCFLUTBase
19  template<bool fields_forward_LUT_ordering, bool fields_reverse_LUT_ordering, bool samples_forward_LUT_ordering, bool samples_reverse_LUT_ordering>
21 
22  namespace utils
23  {
58  template<bool inputs_2_merged_LUT_is_input_ordered, bool merged_2_inputs_LUT_is_input_ordered>
60  {
61  template<bool fields_forward_LUT_ordering, bool fields_reverse_LUT_ordering, bool samples_forward_LUT_ordering, bool samples_reverse_LUT_ordering>
63  template<bool v1, bool v2>
64  friend void ::test_lut_base();
65  public:
69  inline void reset_luts()
70  {
71  for(auto& vec : m_inputs_2_merged_lut)
72  reset_vector(vec);
73  for(auto& vec : m_merged_2_inputs_lut)
74  reset_vector(vec);
75  }
76 
77  /*
78  * @brief Add a valid mapping between input VCF and merged VCF
79  * @note all parameters should be valid parameters, no bcf_int32_missing allowed, use reset_() functions to invalidate existing mapping
80  * @param inputGVCFIdx index of the input VCF file
81  * @param inputIdx index of the field in the input VCF file - field could be anything header field,sample,allele etc
82  * @param mergedIdx index of the field in the merged VCF file
83  */
84  inline void add_input_merged_idx_pair(unsigned inputGVCFIdx, int inputIdx, int mergedIdx)
85  {
86  set_merged_idx_for_input(inputGVCFIdx, inputIdx, mergedIdx);
87  set_input_idx_for_merged(inputGVCFIdx, inputIdx, mergedIdx);
88  }
89 
99  template <bool M = merged_2_inputs_LUT_is_input_ordered, typename std::enable_if<M>::type* = nullptr>
100  inline int get_input_idx_for_merged(unsigned inputGVCFIdx, int mergedIdx) const
101  { return get_lut_value(m_merged_2_inputs_lut, inputGVCFIdx, mergedIdx); }
102  template <bool M = merged_2_inputs_LUT_is_input_ordered, typename std::enable_if<!M>::type* = nullptr>
103  inline int get_input_idx_for_merged(unsigned inputGVCFIdx, int mergedIdx) const
104  { return get_lut_value(m_merged_2_inputs_lut, mergedIdx, inputGVCFIdx); }
105 
115  template <bool M = inputs_2_merged_LUT_is_input_ordered, typename std::enable_if<M>::type* = nullptr>
116  inline int get_merged_idx_for_input(unsigned inputGVCFIdx, int inputIdx) const
117  { return get_lut_value(m_inputs_2_merged_lut, inputGVCFIdx, inputIdx); }
118  template <bool M = inputs_2_merged_LUT_is_input_ordered, typename std::enable_if<!M>::type* = nullptr>
119  inline int get_merged_idx_for_input(unsigned inputGVCFIdx, int inputIdx) const
120  { return get_lut_value(m_inputs_2_merged_lut, inputIdx, inputGVCFIdx); }
121 
127  inline void reset_merged_idx_for_input(unsigned inputGVCFIdx, int inputIdx)
128  { set_merged_idx_for_input(inputGVCFIdx, inputIdx, gamgee::missing_values::int32); }
134  inline void reset_input_idx_for_merged(unsigned inputGVCFIdx, int mergedIdx)
135  { set_input_idx_for_merged(inputGVCFIdx, gamgee::missing_values::int32, mergedIdx); }
136 
137  protected:
138  //Only inherited classes should call constructor,destructor etc
139  MergedVCFLUTBase();
140  MergedVCFLUTBase(unsigned numInputGVCFs, unsigned numMergedFields);
141  ~MergedVCFLUTBase() = default;
145  void clear();
146 
149 
157  template <bool M = inputs_2_merged_LUT_is_input_ordered, typename std::enable_if<M>::type* = nullptr>
158  void resize_inputs_2_merged_lut_if_needed(unsigned numInputGVCFs, unsigned numMergedFields)
159  { resize_and_reset_lut(m_inputs_2_merged_lut, numInputGVCFs, numMergedFields, m_num_input_vcfs, m_num_merged_fields); }
160 
161  template <bool M = inputs_2_merged_LUT_is_input_ordered, typename std::enable_if<!M>::type* = nullptr>
162  void resize_inputs_2_merged_lut_if_needed(unsigned numInputGVCFs, unsigned numMergedFields)
163  { resize_and_reset_lut(m_inputs_2_merged_lut, numMergedFields, numInputGVCFs, m_num_merged_fields, m_num_input_vcfs); }
164 
165  template <bool M = merged_2_inputs_LUT_is_input_ordered, typename std::enable_if<M>::type* = nullptr>
166  void resize_merged_2_inputs_lut_if_needed(unsigned numInputGVCFs, unsigned numMergedFields)
167  { resize_and_reset_lut(m_merged_2_inputs_lut, numInputGVCFs, numMergedFields, m_num_input_vcfs, m_num_merged_fields); }
168 
169  template <bool M = merged_2_inputs_LUT_is_input_ordered, typename std::enable_if<!M>::type* = nullptr>
170  void resize_merged_2_inputs_lut_if_needed(unsigned numInputGVCFs, unsigned numMergedFields)
171  { resize_and_reset_lut(m_merged_2_inputs_lut, numMergedFields, numInputGVCFs, m_num_merged_fields, m_num_input_vcfs); }
172 
173  /*
174  * @brief wrapper around single LUT resize functions
175  */
176  void resize_luts_if_needed(unsigned numInputGVCFs, unsigned numMergedFields)
177  {
178  resize_merged_2_inputs_lut_if_needed(numInputGVCFs, numMergedFields);
179  resize_inputs_2_merged_lut_if_needed(numInputGVCFs, numMergedFields);
180  }
181  private:
182  //why not unordered_map? because I feel the need, the need for speed
183  std::vector<std::vector<int>> m_inputs_2_merged_lut;
184  std::vector<std::vector<int>> m_merged_2_inputs_lut;
191  void reset_vector(std::vector<int>& vec, unsigned from=0u);
196  void resize_and_reset_vector(std::vector<int>& vec, unsigned new_size);
201  void resize_and_reset_lut(std::vector<std::vector<int>>& lut, unsigned new_lut_size, unsigned new_size, unsigned& numRowsVar, unsigned& numColsVar);
202 
211  inline int get_lut_value(const std::vector<std::vector<int>>& lut, int rowIdx, int columnIdx) const
212  {
213  assert(rowIdx >= 0);
214  assert(rowIdx < static_cast<int>(lut.size()));
215  assert(columnIdx >= 0);
216  assert(columnIdx < static_cast<int>(lut[rowIdx].size()));
217  return lut[rowIdx][columnIdx];
218  }
219 
228  inline void set_lut_value(std::vector<std::vector<int>>& lut, int rowIdx, int columnIdx, int value)
229  {
230  assert(rowIdx >= 0);
231  assert(rowIdx < static_cast<int>(lut.size()));
232  assert(columnIdx >= 0);
233  assert(columnIdx < static_cast<int>(lut[rowIdx].size()));
234  lut[rowIdx][columnIdx] = value;
235  }
236 
244  template <bool M = inputs_2_merged_LUT_is_input_ordered, typename std::enable_if<M>::type* = nullptr>
245  inline void set_merged_idx_for_input(unsigned inputGVCFIdx, int inputIdx, int mergedIdx)
246  { set_lut_value(m_inputs_2_merged_lut, inputGVCFIdx, inputIdx, mergedIdx); }
247 
248  template <bool M = inputs_2_merged_LUT_is_input_ordered, typename std::enable_if<!M>::type* = nullptr>
249  inline void set_merged_idx_for_input(unsigned inputGVCFIdx, int inputIdx, int mergedIdx)
250  { set_lut_value(m_inputs_2_merged_lut, inputIdx, inputGVCFIdx, mergedIdx); }
251 
259  template <bool M = merged_2_inputs_LUT_is_input_ordered, typename std::enable_if<M>::type* = nullptr>
260  inline void set_input_idx_for_merged(unsigned inputGVCFIdx, int inputIdx, int mergedIdx)
261  { set_lut_value(m_merged_2_inputs_lut, inputGVCFIdx, mergedIdx, inputIdx); }
262 
263  template <bool M = merged_2_inputs_LUT_is_input_ordered, typename std::enable_if<!M>::type* = nullptr>
264  inline void set_input_idx_for_merged(unsigned inputGVCFIdx, int inputIdx, int mergedIdx)
265  { set_lut_value(m_merged_2_inputs_lut, mergedIdx, inputGVCFIdx, inputIdx); }
266 
267  };
268 
274  template<bool inputs_2_merged_LUT_is_input_ordered, bool merged_2_inputs_LUT_is_input_ordered>
276  : public MergedVCFLUTBase<inputs_2_merged_LUT_is_input_ordered, merged_2_inputs_LUT_is_input_ordered>
277  {
278  private:
279  static const auto m_DEFAULT_INIT_NUM_ALLELES=10u;
280  public:
281  MergedVCFAllelesIdxLUT(unsigned numInputGVCFs)
282  : MergedVCFLUTBase<inputs_2_merged_LUT_is_input_ordered, merged_2_inputs_LUT_is_input_ordered>(numInputGVCFs,
283  m_DEFAULT_INIT_NUM_ALLELES)
284  { m_max_num_alleles = m_DEFAULT_INIT_NUM_ALLELES; }
285  inline void resize_luts_if_needed(unsigned numMergedAlleles)
286  {
287  if(numMergedAlleles > m_max_num_alleles)
288  {
291  m_max_num_alleles = numMergedAlleles;
292  }
293  }
294  private:
295  unsigned m_max_num_alleles;
296  };
297 
298  /*NOTE: Needs explicit instantiation in .cpp file to use this type alias*/
300  }
301 }
302 
303 #endif
Base class to store look up information between fields of merged header and input headers...
Definition: merged_vcf_lut.h:59
void reset_input_idx_for_merged(unsigned inputGVCFIdx, int mergedIdx)
reset/invalidate the input field index for input VCF inputGVCFIdx for merged field mergedIdx ...
Definition: merged_vcf_lut.h:134
Definition: merged_vcf_lut.h:20
int get_input_idx_for_merged(unsigned inputGVCFIdx, int mergedIdx) const
Get field idx for input VCF inputGVCFIdx corresponding to field idx mergedIdx in the mergedVCF file...
Definition: merged_vcf_lut.h:100
MergedVCFAllelesIdxLUT(unsigned numInputGVCFs)
Definition: merged_vcf_lut.h:281
void test_lut_base()
void resize_inputs_2_merged_lut_if_needed(unsigned numInputGVCFs, unsigned numMergedFields)
resize LUT functions
Definition: merged_vcf_lut.h:158
void add_input_merged_idx_pair(unsigned inputGVCFIdx, int inputIdx, int mergedIdx)
Definition: merged_vcf_lut.h:84
void clear()
deallocates memory
Definition: merged_vcf_lut.cpp:27
MergedVCFLUTBase()
Definition: merged_vcf_lut.cpp:9
void resize_luts_if_needed(unsigned numInputGVCFs, unsigned numMergedFields)
Definition: merged_vcf_lut.h:176
constexpr auto int32
missing value for an int32
Definition: missing.h:18
void reset_merged_idx_for_input(unsigned inputGVCFIdx, int inputIdx)
reset/invalidate merged field index for field inputIdx of input VCF inputGVCFIdx
Definition: merged_vcf_lut.h:127
void reset_luts()
: clear all mappings
Definition: merged_vcf_lut.h:69
int get_merged_idx_for_input(unsigned inputGVCFIdx, int inputIdx) const
Get field idx for the merged VCF corresponding to field idx inputIdx in the input VCF of index inputG...
Definition: merged_vcf_lut.h:116
Definition: exceptions.h:9
unsigned m_num_merged_fields
Definition: merged_vcf_lut.h:148
void resize_merged_2_inputs_lut_if_needed(unsigned numInputGVCFs, unsigned numMergedFields)
Definition: merged_vcf_lut.h:166
unsigned m_num_input_vcfs
Definition: merged_vcf_lut.h:147
LUT class for storing mappings between allele vectors in the merged file and input VCF files Since th...
Definition: merged_vcf_lut.h:275
void resize_luts_if_needed(unsigned numMergedAlleles)
Definition: merged_vcf_lut.h:285