| 1 |
#ifndef SEQLIB_BAM_HEADER_H__ |
| 2 |
#define SEQLIB_BAM_HEADER_H__ |
| 3 |
|
| 4 |
#include "htslib/htslib/hts.h" |
| 5 |
#include "htslib/htslib/sam.h" |
| 6 |
#include "htslib/htslib/bgzf.h" |
| 7 |
#include "htslib/htslib/kstring.h" |
| 8 |
|
| 9 |
#include "SeqLib/SeqLibUtils.h" |
| 10 |
#include <string> |
| 11 |
#include <vector> |
| 12 |
|
| 13 |
namespace SeqLib { |
| 14 |
|
| 15 |
/** Store a reference chromosome and its length |
| 16 |
* @note This parallels the data found in SQ tag of BAM header |
| 17 |
*/ |
| 18 |
struct HeaderSequence { |
| 19 |
|
| 20 |
/** Make a new header sequence |
| 21 |
* @param n Name of the chromosome |
| 22 |
* @param l Length of the chromosome |
| 23 |
*/ |
| 24 |
HeaderSequence(const std::string& n, uint32_t l) : Name(n), Length(l) {} |
| 25 |
|
| 26 |
std::string Name; ///< Name of the sequence (eg "1") |
| 27 |
uint32_t Length; ///< Length of the sequence (eg LN:191469) |
| 28 |
}; |
| 29 |
|
| 30 |
typedef std::vector<HeaderSequence> HeaderSequenceVector; |
| 31 |
|
| 32 |
/** Store a header to a BAM file |
| 33 |
* |
| 34 |
* Stores a BAM header, which also acts as a dictionary of |
| 35 |
* reference sequences, with names and lengths. |
| 36 |
*/ |
| 37 |
class BamHeader { |
| 38 |
|
| 39 |
public: |
| 40 |
|
| 41 |
/** Initializes a new empty BamHeader with no data |
| 42 |
* |
| 43 |
* @note No memory is allocated here |
| 44 |
*/ |
| 45 |
BamHeader() {}; |
| 46 |
|
| 47 |
/** Construct a new header from ref sequences and lengths |
| 48 |
* |
| 49 |
*/ |
| 50 |
BamHeader(const HeaderSequenceVector& hsv); |
| 51 |
|
| 52 |
/** Initialize a BamHeader from a string containing |
| 53 |
* a BAM header in human-readable form (e.g. PG ... ) |
| 54 |
* @param hdr Text of a BAM header, with newlines separating lines |
| 55 |
*/ |
| 56 |
BamHeader(const std::string& hdr); |
| 57 |
|
| 58 |
/** Create a new BamHeader from a raw HTSlib header. |
| 59 |
* |
| 60 |
* @note This will make a copy of the input header |
| 61 |
*/ |
| 62 |
BamHeader(const bam_hdr_t * hdr); |
| 63 |
|
| 64 |
/** Return the number of sequences store in this dictionary |
| 65 |
* Returns 0 if header is unitialized. |
| 66 |
*/ |
| 67 |
int NumSequences() const; |
| 68 |
|
| 69 |
/** Return the length of the sequence */ |
| 70 |
int GetSequenceLength(int id) const; |
| 71 |
|
| 72 |
/** Return the length of the sequence */ |
| 73 |
int GetSequenceLength(const std::string& id) const; |
| 74 |
|
| 75 |
/** Return if the header has been opened */ |
| 76 |
bool IsOpen() const { return h.get() != NULL; } |
| 77 |
|
| 78 |
/** Return the full text of the header */ |
| 79 |
std::string AsString() const; |
| 80 |
|
| 81 |
/** Convert a numeric sequence ID to a name |
| 82 |
* |
| 83 |
* @exception Throws an out_of_range if ID is >= then number of |
| 84 |
* targets in dictionary, or if header is uninitialized.. |
| 85 |
* @exception Throws an invalid_argument if ID is < 0; |
| 86 |
*/ |
| 87 |
std::string IDtoName(int id) const; |
| 88 |
|
| 89 |
/** Check if the header has been initialized |
| 90 |
*/ |
| 91 |
bool isEmpty() const { return h.get() == NULL; } |
| 92 |
|
| 93 |
/** Return the raw bam_hdr_t */ |
| 94 |
const bam_hdr_t* get() const { return h.get(); } |
| 95 |
|
| 96 |
/** Return the raw bam_hdr_t */ |
| 97 |
bam_hdr_t* get_() const { return h.get(); } |
| 98 |
|
| 99 |
/** Get the numeric ID associated with a sequence name. |
| 100 |
* @param name Name of the sequence |
| 101 |
* @return ID of named sequence, or -1 if not in dictionary |
| 102 |
*/ |
| 103 |
int Name2ID(const std::string& name) const; |
| 104 |
|
| 105 |
/** Return the reference sequences as vector of HeaderSequence objects */ |
| 106 |
HeaderSequenceVector GetHeaderSequenceVector() const; |
| 107 |
|
| 108 |
private: |
| 109 |
|
| 110 |
// adapted from sam.c - bam_nam2id |
| 111 |
int bam_name2id_2(const bam_hdr_t *h, const char *ref) const; |
| 112 |
|
| 113 |
SeqPointer<bam_hdr_t> h; |
| 114 |
|
| 115 |
// make the name 2 id map (to be used by Name2ID) |
| 116 |
// replaces part of bam_name2id that makes the hash table |
| 117 |
void ConstructName2IDTable(); |
| 118 |
|
| 119 |
// hash table for name to id |
| 120 |
SeqPointer<SeqHashMap<std::string, int> > n2i; |
| 121 |
|
| 122 |
// adapted from sam_hdr_read |
| 123 |
bam_hdr_t* sam_hdr_read2(const std::string& hdr) const; |
| 124 |
|
| 125 |
}; |
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
#endif |