Debian Codegraph @

src/BFC.cpp

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
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
35 #include <stdexcept>
36 #include <algorithm>
37
38 namespace SeqLib {
39
40 bool BFC::AddSequence(const char* seq, const char* qual, const char* name) {
41
42 // do the intial allocation
43 if (n_seqs == 0 && !m_seqs) {
44 m_seqs_size = 32;
45 m_seqs = (fseq1_t*)malloc(m_seqs_size * sizeof(fseq1_t));
46 }
47 // realloc if not enough space
48 else if (n_seqs >= m_seqs_size) {
49 m_seqs_size = 2 * m_seqs_size;
50 m_seqs = (fseq1_t*)realloc(m_seqs, m_seqs_size * sizeof(fseq1_t));
51 }
52
53 if (!m_seqs)
54 return false;
55
56 // make sure seq and qual are even valid (if qual provided)
57 if (strlen(qual) && seq && qual)
58 if (strlen(seq) != strlen(qual))
59 return false;
60 if (!strlen(seq))
61 return false;
62
63 fseq1_t *s;
64
65 s = &m_seqs[n_seqs];
66
67 s->seq = strdup(seq);
68 s->qual = 0;
69 if (strlen(qual)) {
70 s->qual = strdup(qual);
71 }
72
73 s->l_seq = strlen(seq);
74 n_seqs++;
75
76 m_names.push_back(strdup(name));
77
78 assert(m_names.size() == n_seqs);
79
80 return true;
81 }
82
83
84 bool BFC::ErrorCorrect() {
85 correct_reads();
86 return true;
87 }
88
89 bool BFC::Train() {
90 learn_correct();
91 return true;
92 }
93
94 bool BFC::GetSequence(std::string& s, std::string& q) {
95 if (m_idx >= n_seqs)
96 return false;
97 assert(m_idx < n_seqs);
98 assert(m_names.size() == n_seqs);
99 s = std::string(m_seqs[m_idx].seq);
100 q = std::string(m_names[m_idx]);
101 std::transform(s.begin(), s.end(), s.begin(), ::toupper);
102 ++m_idx;
103 return true;
104 }
105
106 bool BFC::CorrectSequence(std::string& str, const std::string& q) {
107
108 assert(n_seqs == 0);
109 assert(m_names.size() == 0);
110
111 m_seqs = (fseq1_t*)malloc(1 * sizeof(fseq1_t));
112 n_seqs = 1;
113
114 //uint64_t size = 0;
115 //for (std::vector<char*>::const_iterator r = v.begin(); r != v.end(); ++r) {
116 // for (auto& r : v) {
117 fseq1_t *s;
118 s = &m_seqs[0];
119 s->seq = strdup(str.c_str());
120 s->qual = q.empty() || q.length() != str.length() ? NULL : strdup(q.c_str());
121 s->l_seq = str.length();
122
123 // do the error correction of this one sequence
124 correct_reads();
125
126 // add a dummy name
127 m_names.push_back(strdup("1"));
128
129 // send to uppercase, and return
130 std::string cstr = std::string(m_seqs[0].seq);
131 std::transform(cstr.begin(), cstr.end(), cstr.begin(), ::toupper);
132 str = cstr;
133
134 clear();
135
136 return true;
137
138 }
139
140 void free_char(char*& c) {
141 if (c) {
142 free (c);
143 c = NULL;
144 }
145 }
146
147 void BFC::clear() {
148
149 assert(m_names.size() == n_seqs);
150 for (size_t i = 0; i < n_seqs; ++i) {
151 free_char(m_names[i]);
152 free_char(m_seqs[i].seq);
153 free_char(m_seqs[i].qual);
154 }
155
156 if (m_seqs)
157 free(m_seqs);
158 m_seqs = 0;
159 n_seqs = 0;
160
161 m_names.clear();
162 m_seqs_size = 0;
163 m_idx = 0;
164
165 }
166
167 void BFC::learn_correct() {
168
169 // options
170 fml_opt_init(&fml_opt);
171
172 // if kmer is 0, fix
173 if (kmer <= 0) {
174 fml_opt_adjust(&fml_opt, n_seqs, m_seqs);
175 kmer = fml_opt.ec_k;
176 }
177
178 // initialize BFC options
179 for (size_t i = 0; i < n_seqs; ++i)
180 tot_len += m_seqs[i].l_seq; // compute total length
181 bfc_opt.l_pre = tot_len - 8 < 20? tot_len - 8 : 20;
182
183 // setup the counting of kmers
184 memset(&es, 0, sizeof(ec_step_t));
185 //kmer is learned before this
186
187 bfc_opt.k = kmer;
188
189 //es.opt = &bfc_opt, es.n_seqs = n_seqs, es.seqs = m_seqs, es.flt_uniq = flt_uniq;
190
191 // hold count info. also called bfc_ch_s. Composed of
192 // int k
193 // int l_pre
194 // cnthash_t **h
195 // h is of size 1<<l_pre (2^l_pre). It is array of hash tables
196 // h[i] is initialized with kh_init(cnt) which makes a cnthash_t
197 // bfc_ch_t *ch; // set in BFC.h
198
199 // do the counting
200 ch = fml_count(n_seqs, m_seqs, bfc_opt.k, bfc_opt.q, bfc_opt.l_pre, bfc_opt.n_threads);
201
202 #ifdef DEBUG_BFC
203 // size of random hash value
204 khint_t k;
205 int* ksize = (int*)calloc(1<<ch->l_pre, sizeof(int));
206 for (int i = 0; i < (1<<ch->l_pre); ++i) {
207 for (k = kh_begin(ch->h[i]); k != kh_end(ch->h[i]); ++k)
208 ++ksize[i];
209 fprintf(stderr, "K: %d S: %d\n", i, ksize[i]);
210 }
211 #endif
212 }
213
214 void BFC::correct_reads() {
215
216 assert(kmer > 0);
217
218 es.ch = ch;
219 es.opt = &bfc_opt;
220 es.n_seqs = n_seqs;
221 es.seqs = m_seqs;
222 es.flt_uniq = flt_uniq;
223
224 // make the histogram?
225 // es.ch is unchanged (const)
226 int mode = bfc_ch_hist(es.ch, hist, hist_high);
227
228 for (int i = fml_opt.min_cnt; i < 256; ++i)
229 sum_k += hist[i], tot_k += i * hist[i];
230
231 #ifdef DEBUG_BFC
232 std::cerr << " sum_k " << sum_k << " tot_k " << tot_k << std::endl;
233 fprintf(stderr, "MODE: %d\n", mode);
234 for (int i = fml_opt.min_cnt; i < 256; ++i) {
235 fprintf(stderr, "hist[%d]: %d\n",i,hist[i]);
236 }
237 for (int i = fml_opt.min_cnt; i < 64; ++i) {
238 fprintf(stderr, "hist_high[%d]: %d\n",i,hist_high[i]);
239 }
240 #endif
241
242 kcov = (float)tot_k / sum_k;
243 bfc_opt.min_cov = (int)(BFC_EC_MIN_COV_COEF * kcov + .499);
244 bfc_opt.min_cov = bfc_opt.min_cov < fml_opt.max_cnt? bfc_opt.min_cov : fml_opt.max_cnt;
245 bfc_opt.min_cov = bfc_opt.min_cov > fml_opt.min_cnt? bfc_opt.min_cov : fml_opt.min_cnt;
246
247 #ifdef DEBUG_BFC
248 fprintf(stderr, "kcov: %f mincov: %d mode %d \n", kcov, bfc_opt.min_cov, mode);
249 #endif
250
251 // do the actual error correction
252 kmer_correct(&es, mode, ch);
253
254 return;
255
256
257 }
258
259 }