Gamgee
You miserable little maggot. I'll stove your head in!
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sam_reader.h
Go to the documentation of this file.
1 #ifndef gamgee__sam_reader__guard
2 #define gamgee__sam_reader__guard
3 
4 #include "sam_iterator.h"
5 #include "sam_pair_iterator.h"
6 
7 #include "../exceptions.h"
8 #include "../utils/hts_memory.h"
9 
10 #include "htslib/sam.h"
11 
12 #include <string>
13 #include <iostream>
14 #include <fstream>
15 #include <memory>
16 #include <vector>
17 
18 namespace gamgee {
19 
45 template<class ITERATOR>
46 class SamReader {
47  public:
48 
55  SamReader(const std::string& filename) :
56  m_sam_file_ptr {},
57  m_sam_header_ptr {}
58  {
59  init_reader(filename);
60  }
61 
68  SamReader(const std::vector<std::string>& filenames) :
69  m_sam_file_ptr {},
70  m_sam_header_ptr {}
71  {
72  if (filenames.size() > 1)
73  throw SingleInputException{"filenames", filenames.size()};
74  if (!filenames.empty())
75  init_reader(filenames.front());
76  }
77 
81  SamReader(const SamReader& other) = delete;
82  SamReader& operator=(const SamIterator&) = delete;
83 
87  SamReader(SamReader&&) = default;
88  SamReader& operator=(SamReader&&) = default;
89 
96  ITERATOR begin() {
97  return ITERATOR{m_sam_file_ptr, m_sam_header_ptr};
98  }
99 
105  ITERATOR end() {
106  return ITERATOR{};
107  }
108 
109  inline SamHeader header() { return SamHeader{m_sam_header_ptr}; }
110 
111  private:
112  std::shared_ptr<htsFile> m_sam_file_ptr;
113  std::shared_ptr<bam_hdr_t> m_sam_header_ptr;
114 
120  void init_reader (const std::string& filename) {
121  auto* file_ptr = sam_open(filename.empty() ? "-" : filename.c_str(), "r");
122  if ( file_ptr == nullptr ) {
123  throw FileOpenException{filename};
124  }
125  m_sam_file_ptr = utils::make_shared_hts_file(file_ptr);
126 
127  auto* header_ptr = sam_hdr_read(file_ptr);
128  if ( header_ptr == nullptr ) {
129  throw HeaderReadException{filename};
130  }
131  m_sam_header_ptr = utils::make_shared_sam_header(header_ptr);
132  }
133 };
134 
137 
138 } // end of namespace
139 
140 #endif /* defined(gamgee__sam_reader__guard) */
Utility class to hold the header of a sam file.
Definition: sam_header.h:16
ITERATOR end()
creates a ITERATOR with a nullified input stream (needed by for-each loop)
Definition: sam_reader.h:105
bam_hdr_t * sam_hdr_read(samFile *fp)
Definition: sam.c:633
Exception for the case where there is an error opening a file for reading/writing.
Definition: exceptions.h:14
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
Utility class to read a SAM/BAM/CRAM file with an appropriate Sam iterator from a stream (e...
Definition: sam_reader.h:46
shared_ptr< bam_hdr_t > make_shared_sam_header(bam_hdr_t *sam_header_ptr)
wraps a pre-allocated bam_hdr_t in a shared_ptr with correct deleter
Definition: hts_memory.cpp:47
SamHeader header()
Definition: sam_reader.h:109
SamReader & operator=(const SamIterator &)=delete
Definition: exceptions.h:9
ITERATOR begin()
creates a ITERATOR pointing at the start of the input stream (needed by for-each loop) ...
Definition: sam_reader.h:96
SamReader(const std::string &filename)
reads through all records in a file ( or sam) parsing them into Sam objects
Definition: sam_reader.h:55
SamReader(const std::vector< std::string > &filenames)
reads through all records in a file ( or sam) parsing them into Sam objects
Definition: sam_reader.h:68
#define sam_open(fn, mode)
Definition: sam.h:317