Gamgee
You miserable little maggot. I'll stove your head in!
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
indexed_sam_reader.h
Go to the documentation of this file.
1 #ifndef gamgee__indexed_sam_reader__guard
2 #define gamgee__indexed_sam_reader__guard
3 
4 #include "indexed_sam_iterator.h"
5 
6 #include "../exceptions.h"
7 #include "../utils/hts_memory.h"
8 
9 #include "htslib/sam.h"
10 
11 #include <string>
12 #include <iostream>
13 #include <fstream>
14 #include <memory>
15 #include <vector>
16 
17 namespace gamgee {
18 
37 template<class ITERATOR>
39  public:
40 
47  IndexedSamReader(const std::string& filename, const std::vector<std::string>& interval_list) :
48  m_sam_file_ptr {},
49  m_sam_index_ptr {},
50  m_sam_header_ptr {},
51  m_interval_list {interval_list}
52  {
53  init_reader(filename);
54  }
55 
59  IndexedSamReader(IndexedSamReader&& other) = default;
60  IndexedSamReader& operator=(IndexedSamReader&& other) = default;
61 
65  IndexedSamReader(const IndexedSamReader& other) = delete;
66 
71 
78  ITERATOR begin() {
79  if (m_interval_list.empty())
80  return ITERATOR{};
81  else
82  return ITERATOR{m_sam_file_ptr, m_sam_index_ptr, m_sam_header_ptr, m_interval_list};
83  }
84 
90  ITERATOR end() {
91  return ITERATOR{};
92  }
93 
99  inline SamHeader header() { return SamHeader{m_sam_header_ptr}; }
100 
101  private:
102  std::shared_ptr<htsFile> m_sam_file_ptr;
103  std::shared_ptr<hts_idx_t> m_sam_index_ptr;
104  std::shared_ptr<bam_hdr_t> m_sam_header_ptr;
105  std::vector<std::string> m_interval_list;
106 
107  void init_reader(const std::string& filename) {
108  auto* file_ptr = sam_open(filename.c_str(), "r");
109  if ( file_ptr == nullptr ) {
110  throw FileOpenException{filename};
111  }
112  m_sam_file_ptr = utils::make_shared_hts_file(file_ptr);
113 
114  auto* index_ptr = sam_index_load(m_sam_file_ptr.get(), filename.c_str());
115  if ( index_ptr == nullptr ) {
116  throw IndexLoadException{filename};
117  }
118  m_sam_index_ptr = utils::make_shared_hts_index(index_ptr);
119 
120  auto* header_ptr = sam_hdr_read(m_sam_file_ptr.get());
121  if ( header_ptr == nullptr ) {
122  throw HeaderReadException{filename};
123  }
124  m_sam_header_ptr = utils::make_shared_sam_header(header_ptr);
125  }
126 };
127 
129 
130 }
131 
132 #endif
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: indexed_sam_reader.h:90
IndexedSamReader(const std::string &filename, const std::vector< std::string > &interval_list)
reads through all records in a file parsing them into Sam objects
Definition: indexed_sam_reader.h:47
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
IndexedSamReader & operator=(IndexedSamReader &&other)=default
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
hts_idx_t * sam_index_load(htsFile *fp, const char *fn)
Definition: sam.c:501
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
Definition: exceptions.h:9
ITERATOR begin()
creates a ITERATOR pointing at the start of the input stream (needed by for-each loop) ...
Definition: indexed_sam_reader.h:78
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
SamHeader header()
returns the header
Definition: indexed_sam_reader.h:99
#define sam_open(fn, mode)
Definition: sam.h:317
Utility class to read a BAM/CRAM file with an appropriate Sam iterator from an indexed file in a for-...
Definition: indexed_sam_reader.h:38