Gamgee
You miserable little maggot. I'll stove your head in!
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
variant_reader.h
Go to the documentation of this file.
1 #ifndef gamgee__variant_reader__guard
2 #define gamgee__variant_reader__guard
3 
4 #include "variant_header.h"
5 #include "variant_iterator.h"
6 
7 #include "../exceptions.h"
8 #include "../utils/hts_memory.h"
9 #include "../utils/variant_utils.h"
10 
11 #include "htslib/vcf.h"
12 
13 #include <string>
14 #include <fstream>
15 #include <algorithm>
16 #include <memory>
17 
18 
19 namespace gamgee {
20 
46 template<class ITERATOR>
48  public:
49 
56  explicit VariantReader(const std::string& filename) :
57  m_variant_file_ptr {},
58  m_variant_header_ptr {}
59  {
60  init_reader(filename);
61  }
62 
69  explicit VariantReader(const std::vector<std::string>& filenames) :
70  m_variant_file_ptr {},
71  m_variant_header_ptr {}
72  {
73  if (filenames.size() > 1)
74  throw SingleInputException{"filenames", filenames.size()};
75  if (!filenames.empty())
76  init_reader(filenames.front());
77  }
78 
88  VariantReader(const std::string& filename, const std::vector<std::string>& samples, const bool include = true) :
89  m_variant_file_ptr {},
90  m_variant_header_ptr {}
91  {
92  init_reader(filename);
93  subset_variant_samples(m_variant_header_ptr.get(), samples, include);
94  }
95 
105  VariantReader(const std::vector<std::string>& filenames, const std::vector<std::string>& samples, const bool include = true) :
106  m_variant_file_ptr {},
107  m_variant_header_ptr {}
108  {
109  if (filenames.size() > 1)
110  throw SingleInputException{"filenames", filenames.size()};
111  if (!filenames.empty()){
112  init_reader(filenames.front());
113  subset_variant_samples(m_variant_header_ptr.get(), samples, include);
114  }
115  }
116 
121  VariantReader(const VariantReader& other) = delete;
122  VariantReader& operator=(const VariantReader& other) = delete;
123 
128  VariantReader(VariantReader&& other) = default;
129  VariantReader& operator=(VariantReader&& other) = default;
130 
137  ITERATOR begin() const {
138  return ITERATOR{ m_variant_file_ptr, m_variant_header_ptr };
139  }
140 
146  ITERATOR end() const {
147  return ITERATOR{};
148  }
149 
153  inline VariantHeader header() const { return VariantHeader{m_variant_header_ptr}; }
154 
155  private:
156  std::shared_ptr<htsFile> m_variant_file_ptr;
157  std::shared_ptr<bcf_hdr_t> m_variant_header_ptr;
158 
164  void init_reader (const std::string& filename) {
165  // Need to check raw pointers for null before wrapping them in a shared_ptr to avoid a segfault
166  // during destruction if an exception is thrown
167 
168  auto* file_ptr = bcf_open(filename.empty() ? "-" : filename.c_str(), "r");
169  if ( file_ptr == nullptr ) {
170  throw FileOpenException{filename};
171  }
172  m_variant_file_ptr = utils::make_shared_hts_file(file_ptr);
173 
174  auto* header_ptr = bcf_hdr_read(file_ptr);
175  if ( header_ptr == nullptr ) {
176  throw HeaderReadException{filename};
177  }
178  m_variant_header_ptr = utils::make_shared_variant_header(header_ptr);
179  }
180 };
181 
183 
184 } // end of namespace
185 
186 #endif /* defined(gamgee__variant_reader__guard) */
VariantReader(const std::vector< std::string > &filenames)
reads through all records in a file (vcf or bcf) parsing them into Variant objects ...
Definition: variant_reader.h:69
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
VariantReader & operator=(const VariantReader &other)=delete
ITERATOR begin() const
creates a ITERATOR pointing at the start of the input stream (needed by for-each loop) ...
Definition: variant_reader.h:137
VariantHeader header() const
returns the variant header of the file being read
Definition: variant_reader.h:153
ITERATOR end() const
creates a ITERATOR with a nullified input stream (needed by for-each loop)
Definition: variant_reader.h:146
Exception for the case where there is an error opening a file for reading/writing.
Definition: exceptions.h:14
void subset_variant_samples(bcf_hdr_t *hdr_ptr, const std::vector< std::string > &samples, const bool include)
allows the caller to include only selected samples in a Variant Reader. To create a sites only file...
Definition: variant_utils.cpp:15
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
VariantReader(const std::string &filename, const std::vector< std::string > &samples, const bool include=true)
reads through all records in a file (vcf or bcf) parsing them into Variant objects but only including...
Definition: variant_reader.h:88
Utility class to read a VCF/BCF file with an appropriate Variant iterator from a stream (e...
Definition: variant_reader.h:47
Definition: exceptions.h:9
VariantReader(const std::string &filename)
reads through all records in a file (vcf or bcf) parsing them into Variant objects ...
Definition: variant_reader.h:56
Utility class to hold a variant header.
Definition: variant_header.h:52
VariantReader(const std::vector< std::string > &filenames, const std::vector< std::string > &samples, const bool include=true)
reads through all records in a file (vcf or bcf) parsing them into Variant objects but only including...
Definition: variant_reader.h:105
#define bcf_open(fn, mode)
Definition: vcf.h:274