1 #ifndef gamgee__indexed_sam_reader__guard
2 #define gamgee__indexed_sam_reader__guard
6 #include "../exceptions.h"
7 #include "../utils/hts_memory.h"
37 template<
class ITERATOR>
47 IndexedSamReader(
const std::string& filename,
const std::vector<std::string>& interval_list) :
51 m_interval_list {interval_list}
53 init_reader(filename);
79 if (m_interval_list.empty())
82 return ITERATOR{m_sam_file_ptr, m_sam_index_ptr, m_sam_header_ptr, m_interval_list};
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;
107 void init_reader(
const std::string& filename) {
108 auto* file_ptr =
sam_open(filename.c_str(),
"r");
109 if ( file_ptr ==
nullptr ) {
114 auto* index_ptr =
sam_index_load(m_sam_file_ptr.get(), filename.c_str());
115 if ( index_ptr ==
nullptr ) {
116 throw IndexLoadException{filename};
121 if ( header_ptr ==
nullptr ) {
122 throw HeaderReadException{filename};
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