Gamgee
You miserable little maggot. I'll stove your head in!
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
sam_reader.h
Go to the documentation of this file.
1 #ifndef gamgee__sam_reader__
2 #define gamgee__sam_reader__
3 
4 #include "sam_iterator.h"
5 #include "sam_pair_iterator.h"
6 #include "utils/hts_memory.h"
7 
8 #include "htslib/sam.h"
9 
10 #include <string>
11 #include <iostream>
12 #include <fstream>
13 #include <memory>
14 
15 
16 namespace gamgee {
17 
43 template<class ITERATOR>
44 class SamReader {
45  public:
46 
53  SamReader(const std::string& filename) :
54  m_sam_file_ptr {sam_open(filename.empty() ? "-" : filename.c_str(), "r")},
55  m_sam_header_ptr { utils::make_shared_sam_header(sam_hdr_read(m_sam_file_ptr)) }
56  {}
57 
58  SamReader(SamReader&& other) :
59  m_sam_file_ptr {std::move(other.m_sam_file_ptr)},
60  m_sam_header_ptr {std::move(other.m_sam_header_ptr)}
61  {}
62 
67  sam_close(m_sam_file_ptr);
68  }
69 
73  SamReader(const SamReader&) = delete;
74 
81  ITERATOR begin() {
82  return ITERATOR{m_sam_file_ptr, m_sam_header_ptr};
83  }
84 
90  ITERATOR end() {
91  return ITERATOR{};
92  }
93 
94  inline SamHeader header() { return SamHeader{m_sam_header_ptr}; }
95 
96  private:
97  samFile* m_sam_file_ptr;
98  const std::shared_ptr<bam_hdr_t> m_sam_header_ptr;
99 };
100 
103 
104 } // end of namespace
105 
106 #endif /* defined(__gamgee__sam_iterator__) */
Utility class to hold the header of a sam file.
Definition: sam_header.h:13
ITERATOR end()
creates a ITERATOR with a nullified input stream (needed by for-each loop)
Definition: sam_reader.h:90
~SamReader()
closes the file stream if there is one (in case we are reading a sam file)
Definition: sam_reader.h:66
Utility class to read a SAM/BAM/CRAM file with an appropriate Sam iterator from a stream (e...
Definition: sam_reader.h:44
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:22
SamHeader header()
Definition: sam_reader.h:94
SamReader(SamReader &&other)
Definition: sam_reader.h:58
ITERATOR begin()
creates a ITERATOR pointing at the start of the input stream (needed by for-each loop) ...
Definition: sam_reader.h:81
SamReader(const std::string &filename)
reads through all records in a file ( or sam) parsing them into Sam objects
Definition: sam_reader.h:53