Gamgee
You miserable little maggot. I'll stove your head in!
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
indexed_variant_reader.h
Go to the documentation of this file.
1 #ifndef gamgee__indexed_variant_reader__guard
2 #define gamgee__indexed_variant_reader__guard
3 
5 
6 #include "../exceptions.h"
7 #include "../utils/hts_memory.h"
8 
9 #include "htslib/vcf.h"
10 
11 #include <memory>
12 #include <string>
13 #include <vector>
14 
15 namespace gamgee {
16 
30 template<class ITERATOR>
32  public:
33 
42  IndexedVariantReader(const std::string& filename, const std::vector<std::string>& interval_list) :
43  m_variant_file_ptr {},
44  m_variant_index_ptr {},
45  m_variant_header_ptr {},
46  m_interval_list { interval_list }
47  {
48  init_reader(filename);
49  }
50 
55  IndexedVariantReader(const IndexedVariantReader& other) = delete;
56  IndexedVariantReader& operator=(const IndexedVariantReader& other) = delete;
57 
62  IndexedVariantReader(IndexedVariantReader&& other) = default;
64 
65  ITERATOR begin() const {
66  return ITERATOR{ m_variant_file_ptr, m_variant_index_ptr, m_variant_header_ptr, m_interval_list };
67  }
68 
69  ITERATOR end() const {
70  return ITERATOR{};
71  }
72 
76  inline VariantHeader header() const { return VariantHeader{m_variant_header_ptr}; }
77 
78  private:
79  std::shared_ptr<vcfFile> m_variant_file_ptr;
80  std::shared_ptr<hts_idx_t> m_variant_index_ptr;
81  std::shared_ptr<bcf_hdr_t> m_variant_header_ptr;
82  std::vector<std::string> m_interval_list;
83 
84  void init_reader(const std::string& filename) {
85  // Need to check raw pointers for null before wrapping them in a shared_ptr to avoid a segfault
86  // during destruction if an exception is thrown
87 
88  auto* variant_file_ptr = bcf_open(filename.c_str(), "r");
89  if ( variant_file_ptr == nullptr ) {
90  throw FileOpenException{filename};
91  }
92  m_variant_file_ptr = utils::make_shared_hts_file(variant_file_ptr);
93 
94  auto* index_file_ptr = bcf_index_load(filename.c_str());
95  if ( index_file_ptr == nullptr ) {
96  throw IndexLoadException{filename};
97  }
98  m_variant_index_ptr = utils::make_shared_hts_index(index_file_ptr);
99 
100  auto* header_ptr = bcf_hdr_read(m_variant_file_ptr.get());
101  if ( header_ptr == nullptr ) {
102  throw HeaderReadException{filename};
103  }
104  m_variant_header_ptr = utils::make_shared_variant_header(header_ptr);
105  }
106 };
107 
108 }
109 
110 #endif /* defined(gamgee__indexed_variant_reader__guard) */
IndexedVariantReader(const std::string &filename, const std::vector< std::string > &interval_list)
reads through all records in a file matching one of the given intervals, parsing them into Variant ob...
Definition: indexed_variant_reader.h:42
bcf_hdr_t * bcf_hdr_read(htsFile *fp)
Definition: vcf.c:736
shared_ptr< bcf_hdr_t > make_shared_variant_header(bcf_hdr_t *bcf_hdr_ptr)
wraps a pre-allocated bcf_hdr_t in a shared_ptr with correct deleter
Definition: hts_memory.cpp:63
Exception for the case where there is an error opening a file for reading/writing.
Definition: exceptions.h:14
IndexedVariantReader & operator=(const IndexedVariantReader &other)=delete
shared_ptr< htsFile > make_shared_hts_file(htsFile *hts_file_ptr)
wraps a pre-allocated htsFile in a shared_ptr with correct deleter
Definition: hts_memory.cpp:15
ITERATOR begin() const
Definition: indexed_variant_reader.h:65
VariantHeader header() const
returns the variant header of the file being read
Definition: indexed_variant_reader.h:76
Definition: exceptions.h:9
shared_ptr< hts_idx_t > make_shared_hts_index(hts_idx_t *hts_index_ptr)
wraps a pre-allocated hts_idx_t in a shared_ptr with correct deleter
Definition: hts_memory.cpp:23
ITERATOR end() const
Definition: indexed_variant_reader.h:69
Utility class to read an indexed BCF file by intervals using an appropriate Variant iterator in a for...
Definition: indexed_variant_reader.h:31
Utility class to hold a variant header.
Definition: variant_header.h:52
#define bcf_index_load(fn)
Definition: vcf.h:724
#define bcf_open(fn, mode)
Definition: vcf.h:274