Debian Codegraph @

src/BFC.cpp

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 A significant portion of this code is derived from Heng Li's BFC
3 repository: https://github.com/lh3/bfc
4
5 BFC is copyrighted by Heng Li with the following license:
6
7 The MIT License
8
9 Copyright (c) 2015 Broad Institute
10
11 Permission is hereby granted, free of charge, to any person obtaining
12 a copy of this software and associated documentation files (the
13 "Software"), to deal in the Software without restriction, including
14 without limitation the rights to use, copy, modify, merge, publish,
15 distribute, sublicense, and/or sell copies of the Software, and to
16 permit persons to whom the Software is furnished to do so, subject to
17 the following conditions:
18
19 The above copyright notice and this permission notice shall be
20 included in all copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
26 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
28 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30
31 */
32
33 #include "SeqLib/BFC.h"
•34 #include <fml/bfc.h>
35
36 #include <stdexcept>
37 #include <algorithm>
38
39 namespace SeqLib {
40
41 bool BFC::AddSequence(const char* seq, const char* qual, const char* name) {
42
43 // do the intial allocation
44 if (n_seqs == 0 && !m_seqs) {
45 m_seqs_size = 32;
•46 m_seqs = (fml_seq1_t*)malloc(m_seqs_size * sizeof(fml_seq1_t));
47 }
48 // realloc if not enough space
49 else if (n_seqs >= m_seqs_size) {
50 m_seqs_size = 2 * m_seqs_size;
•51 m_seqs = (fml_seq1_t*)realloc(m_seqs, m_seqs_size * sizeof(fml_seq1_t));
52 }
53
54 if (!m_seqs)
55 return false;
56
57 // make sure seq and qual are even valid (if qual provided)
58 if (strlen(qual) && seq && qual)
59 if (strlen(seq) != strlen(qual))
60 return false;
61 if (!strlen(seq))
62 return false;
63
•64 fml_seq1_t *s;
65
66 s = &m_seqs[n_seqs];
67
68 s->seq = strdup(seq);
69 s->qual = 0;
70 if (strlen(qual)) {
71 s->qual = strdup(qual);
72 }
73
74 s->l_seq = strlen(seq);
75 n_seqs++;
76
77 m_names.push_back(strdup(name));
78
79 assert(m_names.size() == n_seqs);
80
81 return true;
82 }
83
84
85 bool BFC::ErrorCorrect() {
86 correct_reads();
87 return true;
88 }
89
90 bool BFC::Train() {
91 learn_correct();
92 return true;
93 }
94
95 bool BFC::GetSequence(std::string& s, std::string& q) {
96 if (m_idx >= n_seqs)
97 return false;
98 assert(m_idx < n_seqs);
99 assert(m_names.size() == n_seqs);
100 s = std::string(m_seqs[m_idx].seq);
101 q = std::string(m_names[m_idx]);
102 std::transform(s.begin(), s.end(), s.begin(), ::toupper);
103 ++m_idx;
104 return true;
105 }
106
107 bool BFC::CorrectSequence(std::string& str, const std::string& q) {
108
109 assert(n_seqs == 0);
110 assert(m_names.size() == 0);
111
•112 m_seqs = (fml_seq1_t*)malloc(1 * sizeof(fml_seq1_t));
113 n_seqs = 1;
114
115 //uint64_t size = 0;
116 //for (std::vector<char*>::const_iterator r = v.begin(); r != v.end(); ++r) {
117 // for (auto& r : v) {
•118 fml_seq1_t *s;
119 s = &m_seqs[0];
120 s->seq = strdup(str.c_str());
121 s->qual = q.empty() || q.length() != str.length() ? NULL : strdup(q.c_str());
122 s->l_seq = str.length();
123
124 // do the error correction of this one sequence
125 correct_reads();
126
127 // add a dummy name
128 m_names.push_back(strdup("1"));
129
130 // send to uppercase, and return
131 std::string cstr = std::string(m_seqs[0].seq);
132 std::transform(cstr.begin(), cstr.end(), cstr.begin(), ::toupper);
133 str = cstr;
134
135 clear();
136
137 return true;
138
139 }
140
141 void free_char(char*& c) {
142 if (c) {
143 free (c);
144 c = NULL;
145 }
146 }
147
148 void BFC::clear() {
149
150 assert(m_names.size() == n_seqs);
151 for (size_t i = 0; i < n_seqs; ++i) {
152 free_char(m_names[i]);
153 free_char(m_seqs[i].seq);
154 free_char(m_seqs[i].qual);
155 }
156
157 if (m_seqs)
158 free(m_seqs);
159 m_seqs = 0;
160 n_seqs = 0;
161
162 m_names.clear();
163 m_seqs_size = 0;
164 m_idx = 0;
165
166 }
167
168 void BFC::learn_correct() {
169
170 // options
171 fml_opt_init(&fml_opt);
172
173 // if kmer is 0, fix
174 if (kmer <= 0) {
175 fml_opt_adjust(&fml_opt, n_seqs, m_seqs);
176 kmer = fml_opt.ec_k;
177 }
178
179 // initialize BFC options
180 for (size_t i = 0; i < n_seqs; ++i)
181 tot_len += m_seqs[i].l_seq; // compute total length
182 bfc_opt.l_pre = tot_len - 8 < 20? tot_len - 8 : 20;
183
184 // setup the counting of kmers
185 memset(&es, 0, sizeof(ec_step_t));
186 //kmer is learned before this
187
188 bfc_opt.k = kmer;
189
190 //es.opt = &bfc_opt, es.n_seqs = n_seqs, es.seqs = m_seqs, es.flt_uniq = flt_uniq;
191
192 // hold count info. also called bfc_ch_s. Composed of
193 // int k
194 // int l_pre
195 // cnthash_t **h
196 // h is of size 1<<l_pre (2^l_pre). It is array of hash tables
197 // h[i] is initialized with kh_init(cnt) which makes a cnthash_t
198 // bfc_ch_t *ch; // set in BFC.h
199
200 // do the counting
201 ch = fml_count(n_seqs, m_seqs, bfc_opt.k, bfc_opt.q, bfc_opt.l_pre, bfc_opt.n_threads);
202
203 #ifdef DEBUG_BFC
204 // size of random hash value
205 khint_t k;
206 int* ksize = (int*)calloc(1<<ch->l_pre, sizeof(int));
207 for (int i = 0; i < (1<<ch->l_pre); ++i) {
208 for (k = kh_begin(ch->h[i]); k != kh_end(ch->h[i]); ++k)
209 ++ksize[i];
210 fprintf(stderr, "K: %d S: %d\n", i, ksize[i]);
211 }
212 #endif
213 }
214
215 void BFC::correct_reads() {
216
217 assert(kmer > 0);
218
219 es.ch = ch;
220 es.opt = &bfc_opt;
221 es.n_seqs = n_seqs;
222 es.seqs = m_seqs;
223 es.flt_uniq = flt_uniq;
224
225 // make the histogram?
226 // es.ch is unchanged (const)
227 int mode = bfc_ch_hist(es.ch, hist, hist_high);
228
229 for (int i = fml_opt.min_cnt; i < 256; ++i)
230 sum_k += hist[i], tot_k += i * hist[i];
231
232 #ifdef DEBUG_BFC
233 std::cerr << " sum_k " << sum_k << " tot_k " << tot_k << std::endl;
234 fprintf(stderr, "MODE: %d\n", mode);
235 for (int i = fml_opt.min_cnt; i < 256; ++i) {
236 fprintf(stderr, "hist[%d]: %d\n",i,hist[i]);
237 }
238 for (int i = fml_opt.min_cnt; i < 64; ++i) {
239 fprintf(stderr, "hist_high[%d]: %d\n",i,hist_high[i]);
240 }
241 #endif
242
243 kcov = (float)tot_k / sum_k;
244 bfc_opt.min_cov = (int)(BFC_EC_MIN_COV_COEF * kcov + .499);
245 bfc_opt.min_cov = bfc_opt.min_cov < fml_opt.max_cnt? bfc_opt.min_cov : fml_opt.max_cnt;
246 bfc_opt.min_cov = bfc_opt.min_cov > fml_opt.min_cnt? bfc_opt.min_cov : fml_opt.min_cnt;
247
248 #ifdef DEBUG_BFC
249 fprintf(stderr, "kcov: %f mincov: %d mode %d \n", kcov, bfc_opt.min_cov, mode);
250 #endif
251
252 // do the actual error correction
253 kmer_correct(&es, mode, ch);
254
255 return;
256
257
258 }
259
260 }