| 1 |
#ifndef SEQLIB_BFC_H |
| 2 |
#define SEQLIB_BFC_H |
| 3 |
|
| 4 |
extern "C" { |
| 5 |
#include "fermi-lite/bfc.h" |
| 6 |
#include "fermi-lite/fml.h" |
| 7 |
} |
| 8 |
|
| 9 |
#include "SeqLib/BamRecord.h" |
| 10 |
#include "SeqLib/UnalignedSequence.h" |
| 11 |
|
| 12 |
namespace SeqLib { |
| 13 |
|
| 14 |
/** Class to perform error-correction using BFC algorithm |
| 15 |
* |
| 16 |
* BFC is designed and implemented by Heng Li (https://github.com/lh3/bfc). |
| 17 |
* From Heng: It is a variant of the classical spectrum alignment algorithm introduced |
| 18 |
* by Pevzner et al (2001). It uses an exhaustive search to find a k-mer path |
| 19 |
* through a read that minimizeds a heuristic objective function jointly considering |
| 20 |
* penalities on correction, quality and k-mer support. |
| 21 |
*/ |
| 22 |
class BFC { |
| 23 |
|
| 24 |
public: |
| 25 |
/** Construct a new BFC engine */ |
| 26 |
BFC() { |
| 27 |
m_idx = 0; |
| 28 |
bfc_opt_init(&bfc_opt); |
| 29 |
ch = NULL; |
| 30 |
kmer = 0; |
| 31 |
flt_uniq = 0; |
| 32 |
n_seqs = 0; |
| 33 |
m_seqs = NULL; |
| 34 |
kcov = 0; |
| 35 |
tot_k = 0; |
| 36 |
sum_k = 0; |
| 37 |
tot_len = 0; |
| 38 |
m_seqs_size = 0; |
| 39 |
} |
| 40 |
|
| 41 |
~BFC() { |
| 42 |
clear(); |
| 43 |
if (ch) |
| 44 |
bfc_ch_destroy(ch); |
| 45 |
} |
| 46 |
|
| 47 |
/** Peform BFC error correction on the sequences stored in this object */ |
| 48 |
bool ErrorCorrect(); |
| 49 |
|
| 50 |
/** Train the error corrector using the reads stored in this object */ |
| 51 |
bool Train(); |
| 52 |
|
| 53 |
/** Add a sequence for either training or correction |
| 54 |
* @param seq A sequence to be copied into this object (A, T, C, G) |
| 55 |
*/ |
| 56 |
bool AddSequence(const char* seq, const char* qual, const char* name); |
| 57 |
|
| 58 |
/** Set the k-mer size for training |
| 59 |
* @note zero is auto |
| 60 |
*/ |
| 61 |
void SetKmer(int k) { kmer = k; } |
| 62 |
|
| 63 |
/** Correct a single new sequence not stored in object |
| 64 |
* @param str Sequence of string to correct (ACTG) |
| 65 |
* @param q Quality score of sequence to correct |
| 66 |
* @value Returns true if corrected */ |
| 67 |
bool CorrectSequence(std::string& str, const std::string& q); |
| 68 |
|
| 69 |
/** Clear the stored reads */ |
| 70 |
void clear(); |
| 71 |
|
| 72 |
/** Return the calculated kcov */ |
| 73 |
float GetKCov() const { return kcov; } |
| 74 |
|
| 75 |
/** Return the calculated kcov */ |
| 76 |
int GetKMer() const { return kmer; } |
| 77 |
|
| 78 |
/** Return the number of sequences controlled by this */ |
| 79 |
int NumSequences() const { return n_seqs; } |
| 80 |
|
| 81 |
/** Return the next sequence stored in object |
| 82 |
* @param s Empty string to be filled. |
| 83 |
* @param q Empty string name to be filled. |
| 84 |
* @value True if string was filled with sequence. False if no more sequences. |
| 85 |
*/ |
| 86 |
bool GetSequence(std::string& s, std::string& q); |
| 87 |
|
| 88 |
/** Reset the sequence iterator inside GetSequence |
| 89 |
*/ |
| 90 |
void ResetGetSequence() { m_idx = 0; }; |
| 91 |
|
| 92 |
private: |
| 93 |
|
| 94 |
size_t m_idx; |
| 95 |
|
| 96 |
// the amount of memory allocated |
| 97 |
size_t m_seqs_size; |
| 98 |
|
| 99 |
void learn_correct(); |
| 100 |
|
| 101 |
bfc_opt_t bfc_opt; |
| 102 |
|
| 103 |
// histogram of kmer occurences |
| 104 |
uint64_t hist[256]; |
| 105 |
|
| 106 |
// diff histogram of kmers?? |
| 107 |
uint64_t hist_high[64]; |
| 108 |
|
| 109 |
uint64_t tot_len; |
| 110 |
|
| 111 |
uint64_t sum_k; // total valid kmer count (kmers above min_count) ? |
| 112 |
|
| 113 |
// total number of kmers? |
| 114 |
uint64_t tot_k; |
| 115 |
|
| 116 |
// |
| 117 |
float kcov; |
| 118 |
|
| 119 |
// reads to correct in place |
| 120 |
fml_seq1_t * m_seqs; |
| 121 |
|
| 122 |
// number of sequeces |
| 123 |
size_t n_seqs; |
| 124 |
|
| 125 |
// fermi lite options |
| 126 |
fml_opt_t fml_opt; |
| 127 |
|
| 128 |
// vector of names |
| 129 |
std::vector<char*> m_names; |
| 130 |
|
| 131 |
// assign names, qualities and seq to m_seqs |
| 132 |
void allocate_sequences_from_reads(const BamRecordVector& brv); |
| 133 |
|
| 134 |
// assign names, qualities and seq to m_seqs |
| 135 |
void allocate_sequences_from_char(const std::vector<char*>& v); |
| 136 |
|
| 137 |
void allocate_sequences_from_strings(const std::vector<std::string>& v); |
| 138 |
|
| 139 |
// do the actual read correction |
| 140 |
void correct_reads(); |
| 141 |
|
| 142 |
// 0 turns off filter uniq |
| 143 |
int flt_uniq; // from fml_correct call |
| 144 |
|
| 145 |
int l_pre; |
| 146 |
|
| 147 |
// 0 is auto learn |
| 148 |
int kmer; |
| 149 |
|
| 150 |
// holds data after learning how to correct |
| 151 |
bfc_ch_t *ch; |
| 152 |
|
| 153 |
// holds data for actual error correction |
| 154 |
ec_step_t es; |
| 155 |
}; |
| 156 |
|
| 157 |
} |
| 158 |
|
| 159 |
#endif |