Gamgee
You miserable little maggot. I'll stove your head in!
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
hts_memory.h
Go to the documentation of this file.
1 #ifndef gamgee__hts_memory__guard
2 #define gamgee__hts_memory__guard
3 
4 #include "htslib/sam.h"
5 #include "htslib/vcf.h"
7 #include "htslib/kstring.h"
8 
9 #include <memory>
10 #include <vector>
11 #include <string>
12 
13 namespace gamgee {
14 namespace utils {
15 
20  void operator()(htsFile* p) const { hts_close(p); }
21 };
22 
27  void operator()(hts_idx_t* p) const { hts_idx_destroy(p); }
28 };
29 
34  void operator()(hts_itr_t* p) const { hts_itr_destroy(p); }
35 };
36 
43  void operator()(bam1_t* p) const { bam_destroy1(p); }
44 };
45 
50  void operator()(bam_hdr_t* p) const { bam_hdr_destroy(p); }
51 };
52 
57  void operator()(bcf_hdr_t* p) const { bcf_hdr_destroy(p); }
58 };
59 
64  void operator()(bcf1_t* p) const { bcf_destroy1(p); }
65 };
66 
71  void operator()(bcf_srs_t* p) const { bcf_sr_destroy(p); }
72 };
73 
74 std::shared_ptr<htsFile> make_shared_hts_file(htsFile* hts_file_ptr);
75 std::shared_ptr<hts_idx_t> make_shared_hts_index(hts_idx_t* hts_index_ptr);
76 std::shared_ptr<hts_itr_t> make_shared_hts_itr(hts_itr_t* hts_itr_ptr);
77 std::shared_ptr<bam1_t> make_shared_sam(bam1_t* sam_ptr);
78 std::shared_ptr<bam_hdr_t> make_shared_sam_header(bam_hdr_t* sam_header_ptr);
79 std::shared_ptr<bcf1_t> make_shared_variant(bcf1_t* bcf_ptr);
80 std::shared_ptr<bcf_hdr_t> make_shared_variant_header(bcf_hdr_t* bcf_hdr_ptr);
81 std::shared_ptr<bcf_srs_t> make_shared_synced_variant_reader(bcf_srs_t* synced_reader_ptr);
82 
83 std::unique_ptr<htsFile, HtsFileDeleter> make_unique_hts_file(htsFile* hts_file_ptr);
84 std::unique_ptr<hts_itr_t, HtsIteratorDeleter> make_unique_hts_itr(hts_itr_t* hts_itr_ptr);
85 
86 bam1_t* sam_deep_copy(bam1_t* original);
88 bcf1_t* variant_deep_copy(bcf1_t* original);
90 
91 bam1_t* sam_shallow_copy(bam1_t* original);
92 
99 std::string htslib_filter_name(bcf_hdr_t* header, bcf1_t* body, int index);
100 
101 uint8_t bcf_type_to_element_size(const int32_t htslib_type);
102 inline uint8_t int_encoded_type(const int32_t val) { return bcf_enc_inttype(val); }
103 uint8_t int_encoded_type(const int32_t min_val, const int32_t max_val);
104 inline uint8_t int_encoded_size(const int32_t val) { return bcf_type_to_element_size(bcf_enc_inttype(val)); }
105 inline uint32_t encoded_size(const int8_t field_type, const uint32_t field_length, bool add_type_descriptor = true) {
106  const auto type_descriptor_size = add_type_descriptor ? (field_length >= 15 ? int_encoded_size(field_length) + 2 : 1) : 0;
107  return field_length * bcf_type_to_element_size(field_type) + type_descriptor_size;
108 }
109 
110 kstring_t initialize_htslib_buffer(const uint32_t initial_capacity);
111 
112 }
113 }
114 
115 #endif // gamgee__hts_memory__guard
void hts_idx_destroy(hts_idx_t *idx)
Definition: hts.c:878
void bcf_hdr_destroy(bcf_hdr_t *h)
Definition: vcf.c:714
Definition: hts.h:109
void operator()(htsFile *p) const
Definition: hts_memory.h:20
void bcf_destroy1(bcf1_t *v)
Definition: vcf.c:841
bam1_t * sam_shallow_copy(bam1_t *original)
creates a shallow copy of an existing bam1_t: copies core fields but not the data buffer or fields re...
Definition: hts_memory.cpp:128
a functor object to delete a bam_hdr_t pointer
Definition: hts_memory.h:49
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
bam_hdr_t * sam_header_deep_copy(bam_hdr_t *original)
creates a deep copy of an existing bam_hdr_t
Definition: hts_memory.cpp:103
void hts_itr_destroy(hts_itr_t *iter)
Definition: hts.c:1346
void operator()(bam_hdr_t *p) const
Definition: hts_memory.h:50
void bam_destroy1(bam1_t *b)
Definition: sam.c:213
a functor object to delete a bcf_srs_t pointer
Definition: hts_memory.h:70
uint8_t int_encoded_size(const int32_t val)
Definition: hts_memory.h:104
a functor object to delete a bam1_t pointer
Definition: hts_memory.h:42
shared_ptr< bcf1_t > make_shared_variant(bcf1_t *bcf_ptr)
wraps a pre-allocated bcf1_t in a shared_ptr with correct deleter
Definition: hts_memory.cpp:55
void operator()(bcf_hdr_t *p) const
Definition: hts_memory.h:57
Definition: sam.h:170
bcf1_t * variant_deep_copy(bcf1_t *original)
creates a deep copy of an existing bcf1_t
Definition: hts_memory.cpp:111
void bam_hdr_destroy(bam_hdr_t *h)
Definition: sam.c:51
bcf_hdr_t * variant_header_deep_copy(bcf_hdr_t *original)
creates a deep copy of an existing bcf_hdr_t
Definition: hts_memory.cpp:119
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
uint32_t encoded_size(const int8_t field_type, const uint32_t field_length, bool add_type_descriptor=true)
Definition: hts_memory.h:105
a functor object to delete a bcf_hdr_t pointer
Definition: hts_memory.h:56
Definition: bgzf.h:69
int hts_close(htsFile *fp)
Definition: hts.c:367
Definition: vcf.h:194
Definition: hts.c:640
Definition: hts.h:321
void operator()(hts_itr_t *p) const
Definition: hts_memory.h:34
Definition: sam.h:46
void bcf_sr_destroy(bcf_srs_t *readers)
Definition: synced_bcf_reader.c:265
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
void operator()(hts_idx_t *p) const
Definition: hts_memory.h:27
std::unique_ptr< hts_itr_t, HtsIteratorDeleter > make_unique_hts_itr(hts_itr_t *hts_itr_ptr)
wraps a pre-allocated hts_itr_t in a unique_ptr with correct deleter
Definition: hts_memory.cpp:87
shared_ptr< bam1_t > make_shared_sam(bam1_t *sam_ptr)
wraps a pre-allocated bam1_t in a shared_ptr with correct deleter
Definition: hts_memory.cpp:39
a functor object to delete an htsFile pointer
Definition: hts_memory.h:19
Definition: exceptions.h:9
uint8_t bcf_type_to_element_size(const int32_t htslib_type)
Returns the number of bytes required to store each BCF_BT_* type.
Definition: hts_memory.cpp:154
uint8_t int_encoded_type(const int32_t min_val, const int32_t max_val)
Given a min and max value, determines whether int8, int16, or int32 BCF encoding is required...
Definition: hts_memory.cpp:161
std::shared_ptr< bcf_srs_t > make_shared_synced_variant_reader(bcf_srs_t *synced_reader_ptr)
wraps a pre-allocated bcf_srs_t in a shared_ptr with correct deleter
Definition: hts_memory.cpp:71
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
Definition: synced_bcf_reader.h:120
std::string htslib_filter_name(bcf_hdr_t *header, bcf1_t *body, int index)
helper function to translate an index into a string in the filter list
Definition: hts_memory.cpp:144
a functor object to delete an hts file index pointer
Definition: hts_memory.h:26
Definition: vcf.h:100
a functor object to delete an hts file iterator pointer
Definition: hts_memory.h:33
unique_ptr< htsFile, HtsFileDeleter > make_unique_hts_file(htsFile *hts_file_ptr)
wraps a pre-allocated htsFile in a unique_ptr with correct deleter
Definition: hts_memory.cpp:79
a functor object to delete a bcf1_t pointer
Definition: hts_memory.h:63
shared_ptr< hts_itr_t > make_shared_hts_itr(hts_itr_t *hts_itr_ptr)
wraps a pre-allocated hts_itr_t in a shared_ptr with correct deleter
Definition: hts_memory.cpp:31
void operator()(bcf1_t *p) const
Definition: hts_memory.h:64
kstring_t initialize_htslib_buffer(const uint32_t initial_capacity)
Returns a newly-allocated kstring_t buffer suitable for passing to htslib.
Definition: hts_memory.cpp:186
void operator()(bam1_t *p) const
Definition: hts_memory.h:43
void operator()(bcf_srs_t *p) const
Definition: hts_memory.h:71
bam1_t * sam_deep_copy(bam1_t *original)
creates a deep copy of an existing bam1_t
Definition: hts_memory.cpp:95