| 1 | #ifndef SEQLIB_BWAWRAPPER_H |
| 2 | #define SEQLIB_BWAWRAPPER_H |
| 3 | |
| 4 | #include <string> |
| 5 | #include <vector> |
| 6 | #include <iostream> |
| 7 | #include <algorithm> |
| 8 | #include <memory> |
| 9 | |
| 10 | #include "SeqLib/BamRecord.h" |
| 11 | #include "SeqLib/BamHeader.h" |
| 12 | #include "SeqLib/UnalignedSequence.h" |
| 13 | |
| 14 | // all of the bwa and kseq stuff is in unaligned sequence |
| 15 | // best way I had to keep from clashes with klib macros |
| 16 | |
| 17 | #define MEM_F_SOFTCLIP 0x200 |
| 18 | |
| 19 | namespace SeqLib { |
| 20 | |
| 21 | /** Calls BWA-MEM on sequence queries and returns aligned reads, all in memory |
| 22 | * @note Calls core functions provided by Heng Li in BWA-MEM. https://github.com/lh3/bwa |
| 23 | */ |
| 24 | class BWAWrapper { |
| 25 | |
| 26 | public: |
| 27 | |
| 28 | /** Create an empty BWA MEM interface |
| 29 | * @note Will initalize a BWA-MEM memopt structure |
| 30 | * with the BWA-MEM defaults found in mem_opt_init. |
| 31 | * Memory allocation and deallocation is automatically |
| 32 | * handled in constructor / destructor. |
| 33 | */ |
| 34 | BWAWrapper() { |
| 35 | idx = 0; |
| 36 | copy_comment = false; |
| 37 | memopt = mem_opt_init(); |
| 38 | memopt->flag |= MEM_F_SOFTCLIP; |
| 39 | } |
| 40 | |
| 41 | /** Destroy the BWAWrapper (deallocate index and options) */ |
| 42 | ~BWAWrapper() { |
| 43 | if (idx) |
| 44 | bwa_idx_destroy(idx); |
| 45 | if (memopt) |
| 46 | free(memopt); |
| 47 | } |
| 48 | |
| 49 | /** Retrieve the sequence name from its numeric ID |
| 50 | * @param id Numeric ID of the reference sequence |
| 51 | * @exception throws an out_of_bounds if id not found |
| 52 | */ |
| 53 | std::string ChrIDToName(int id) const; |
| 54 | |
| 55 | /** Create a BamHeader from the loaded index files */ |
| 56 | BamHeader HeaderFromIndex() const; |
| 57 | |
| 58 | /** Perform a BWA-MEM alignment of a single sequnece, and store hits in BamReadVector |
| 59 | * @param seq Sequence to be aligned |
| 60 | * @param name Name of the sequence to be aligned |
| 61 | * @param vec Alignment hits are appended to vec |
| 62 | * @param hardclip Should the output BamRecord objects be hardclipped |
| 63 | * @param keep_sec_with_frac_of_primary_score Set a threshold for whether a secondary alignment should be output |
| 64 | * @param max_secondary Set a hard-limit on the number of secondary hits that will be reported |
| 65 | */ |
| 66 | void AlignSequence(const std::string& seq, const std::string& name, BamRecordVector& vec, bool hardclip, |
| 67 | double keep_sec_with_frac_of_primary_score, int max_secondary) const; |
| 68 | |
| 69 | /** Perform a BWA-MEM alignment of a single sequence, and store hits in BamReadVector |
| 70 | * @param us UnalignedSequence to be aligned |
| 71 | * @param vec Alignment hits are appended to vec |
| 72 | * @param hardclip Should the output BamRecord objects be hardclipped |
| 73 | * @param keep_sec_with_frac_of_primary_score Set a threshold for whether a secondary alignment should be output |
| 74 | * @param max_secondary Set a hard-limit on the number of secondary hits that will be reported |
| 75 | */ |
| 76 | void AlignSequence(const UnalignedSequence& us, BamRecordVector& vec, bool hardclip, |
| 77 | double keep_sec_with_frac_of_primary_score, int max_secondary) const; |
| 78 | |
| 79 | /** Construct a new bwa index for this object. |
| 80 | * @param v vector of references to input (e.g. v = {{"r1", "AT"}};) |
| 81 | * |
| 82 | * Throw an invalid_argument exception if any of the names or sequences |
| 83 | * of the input UnalignedSequenceVector is empty |
| 84 | */ |
| 85 | void ConstructIndex(const UnalignedSequenceVector& v); |
| 86 | |
| 87 | /** Retrieve a bwa index object from disk |
| 88 | * @param file path a to an index fasta (index with bwa index) |
| 89 | * @return True if successful |
| 90 | * @note Will delete the old index if already stored |
| 91 | */ |
| 92 | bool LoadIndex(const std::string& file); |
| 93 | |
| 94 | /** Dump the stored index to files |
| 95 | * @note This does not write the fasta itself |
| 96 | * @param index_name Write index files (*.sai, *.pac, *.ann, *.bwt, *.amb) |
| 97 | * @return True if able to write index |
| 98 | */ |
| 99 | bool WriteIndex(const std::string& index_name) const; |
| 100 | |
| 101 | /** Return the raw index in bwaidx_t form */ |
| 102 | bwaidx_t* GetIndex() const { return idx; } |
| 103 | |
| 104 | /** Return the number of reference sequences in current index |
| 105 | * @return Number of reference sequences, or 0 if uninitialized |
| 106 | */ |
| 107 | int NumSequences() const; |
| 108 | |
| 109 | /** Print some basic information about the loaded index */ |
| 110 | friend std::ostream& operator<<(std::ostream& out, const BWAWrapper& b); |
| 111 | |
| 112 | /** Set the gap open penalty |
| 113 | * @param gap_open Gap open penalty. Default 6. |
| 114 | * @exception Throws invalid_argument if gap_open < 0 |
| 115 | */ |
| 116 | void SetGapOpen(int gap_open); |
| 117 | |
| 118 | /** Set the gap open penalty |
| 119 | * @param gap_ext Gap extension penalty. Default 1 |
| 120 | * @exception Throws invalid_argument if gap_ext < 0 |
| 121 | */ |
| 122 | void SetGapExtension(int gap_ext); |
| 123 | |
| 124 | /** Set the mismatch penalty |
| 125 | * @param m Mismatch penalty (BWA-MEM b). Default 4 |
| 126 | * @exception Throws invalid_argument if m < 0 |
| 127 | */ |
| 128 | void SetMismatchPenalty(int m); |
| 129 | |
| 130 | /** Set the reseed trigger |
| 131 | * @param r See BWA-MEM -r. Default 1.5 |
| 132 | * @exception Throws invalid_argument if r < 0 |
| 133 | */ |
| 134 | void SetReseedTrigger(float r); |
| 135 | |
| 136 | /** Set the SW alignment bandwidth |
| 137 | * @param w See BWA-MEM -w. Default 100 |
| 138 | * @exception Throws invalid_argument if w < 0 |
| 139 | */ |
| 140 | void SetBandwidth(int w); |
| 141 | |
| 142 | /** Set the SW alignment Z dropoff |
| 143 | * @param z See BWA-MEM -d. Default 100 |
| 144 | * @exception Throws invalid_argument if z < 0 |
| 145 | */ |
| 146 | void SetZDropoff(int z); |
| 147 | |
| 148 | /** Set the 3-prime clipping penalty |
| 149 | * @param p See BWA-MEM -L. |
| 150 | * @exception Throws invalid_argument if p < 0 |
| 151 | */ |
| 152 | void Set3primeClippingPenalty(int p); |
| 153 | |
| 154 | /** Set the 5-prime clipping penalty |
| 155 | * @param p See BWA-MEM -L. |
| 156 | * @exception Throws invalid_argument if p < 0 |
| 157 | */ |
| 158 | void Set5primeClippingPenalty(int p); |
| 159 | |
| 160 | /** Set the match score. Scales -TdBOELU |
| 161 | * @note Since this scales penalty options, it should be |
| 162 | * probably be specified first, and then other options |
| 163 | * (eg gap penalty) can be Set explicitly afterwards. |
| 164 | * @param a See BWA-MEM -A |
| 165 | * @exception Throws invalid_argument if a < 0 |
| 166 | */ |
| 167 | void SetAScore(int a); |
| 168 | |
| 169 | /** Check if the index is empty */ |
| 170 | bool IsEmpty() const { return !idx; } |
| 171 | |
| 172 | /** Append FASTA/Q comment to SAM output */ |
| 173 | void AppendFqComment(){ |
| 174 | copy_comment = true; |
| 175 | } |
| 176 | |
| 177 | private: |
| 178 | |
| 179 | // Construct a bam_hdr_t from a header string |
| 180 | bam_hdr_t* sam_hdr_read2(const std::string& hdr) const; |
| 181 | |
| 182 | // Store the options in memory |
| 183 | mem_opt_t * memopt; |
| 184 | |
| 185 | // Store append FASTA/Q comment to SAM output |
| 186 | bool copy_comment; |
| 187 | |
| 188 | // hold the full index structure |
| 189 | bwaidx_t* idx; |
| 190 | |
| 191 | // Convert a bns to a header string |
| 192 | std::string bwa_print_sam_hdr2(const bntseq_t *bns, const char *hdr_line) const; |
| 193 | |
| 194 | // overwrite the bwa bwt_pac2pwt function |
| 195 | bwt_t *seqlib_bwt_pac2bwt(const uint8_t *pac, int bwt_seq_lenr); |
| 196 | |
| 197 | // add an anns (chr annotation structure) |
| 198 | bntann1_t* seqlib_add_to_anns(const std::string& name, const std::string& seq, bntann1_t * ann, size_t offset); |
| 199 | |
| 200 | // overwrite the bwa-mem add1 function, which takes a sequence and adds to pac |
| 201 | uint8_t* seqlib_add1(const kseq_t *seq, bntseq_t *bns, uint8_t *pac, int64_t *m_pac, int *m_seqs, int *m_holes, bntamb1_t **q); |
| 202 | |
| 203 | // make the pac structure (2-bit encoded packed sequence) |
| 204 | uint8_t* seqlib_make_pac(const UnalignedSequenceVector& v, bool for_only); |
| 205 | |
| 206 | // write pac part of the index |
| 207 | void seqlib_write_pac_to_file(const std::string& file) const; |
| 208 | |
| 209 | // write the bns file of the index |
| 210 | std::string print_bns(); |
| 211 | }; |
| 212 | |
| 213 | } |
| 214 | |
| 215 | |
| 216 | #endif |