Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsmithlab_utils.hpp
More file actions
Latest commit
476 lines (428 loc) · 10.3 KB
/
Copy pathsmithlab_utils.hpp
File metadata and controls
476 lines (428 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*
* Part of SMITHLAB software
*
* Copyright (C) 2008 Cold Spring Harbor Laboratory,
* University of Southern California and
* Andrew D. Smith
*
* Authors: Andrew D. Smith
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SMITHLAB_UTILS_HPP
#defineSMITHLAB_UTILS_HPP
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstddef>
#include<iterator>
#include<sstream>
#include<string>
#include<vector>
extern"C" {
charhave_smithlab_cpp();
}
namespacesmithlab {
template <classIn, classOut, classPred>
Out copy_if(In first, In last, Out res, Pred p) {
while (first != last) {
if (p(*first))
*res++ = *first;
++first;
}
return res;
}
} // namespace smithlab
typedefsize_t MASK_t;
namespacesmithlab {
// Code dealing with false discovery rate
doubleget_fdr_cutoff(constsize_t n_tests, std::vector<double> &pvals,
constdouble alpha);
voidcorrect_pvals(constsize_t n_tests, std::vector<double> &pvals);
// Assumes 4 nucleotide DNA alphabet
staticconstsize_t alphabet_size = 4;
std::vector<std::string> split(std::string, constchar *,
bool get_empty_fields = false);
std::vector<std::string> split_whitespace_quoted(std::string to_split);
voidsplit_whitespace(const std::string &s, std::vector<std::string> &v);
std::string strip(const std::string &s);
std::vector<std::string> squash(const std::vector<std::string> &v);
template <classT> std::string toa(T t) {
std::ostringstream s;
s << t;
return s.str();
}
doublelog_sum_log_vec(const std::vector<double> &vals, size_t lim);
template <classFwrdItr> doublelog_sum_log(FwrdItr first, FwrdItr last) {
constauto max_itr = std::max_element(first, last);
constdouble max_val = *max_itr;
double sum = 1.0;
for (auto itr = first; itr != last; ++itr)
sum += (itr == max_itr ? 0.0 : std::exp(*itr - max_val));
return max_val + std::log(sum);
}
inlinedoublelog_sum_log(constdouble p, constdouble q) {
if (p == 0) {
return q;
}
elseif (q == 0) {
return p;
}
constdouble larger = (p > q) ? p : q;
return larger + log(1.0 + exp((p > q ? q : p) - larger));
}
inlinedoublelog_sum_log(constdouble p, constdouble q, constdouble r) {
returnlog_sum_log(log_sum_log(p, q), r);
}
} // namespace smithlab
namespacesmithlab_bits {
staticconst MASK_t low_bit = 1ul;
staticconst MASK_t high_bit = static_cast<size_t>(0x8000000000000000ul);
staticconst MASK_t all_ones = static_cast<size_t>(-1);
staticconst MASK_t all_zeros = 0ul;
staticconstsize_t word_size = 64ul;
} // namespace smithlab_bits
inlinesize_tbase2int_upper_only(char c) {
switch (c) {
case'A':
return0;
case'C':
return1;
case'G':
return2;
case'T':
return3;
default:
return4;
}
}
inlinesize_tbase2int(char c) {
switch (c) {
case'A':
return0;
case'C':
return1;
case'G':
return2;
case'T':
return3;
case'a':
return0;
case'c':
return1;
case'g':
return2;
case't':
return3;
default:
return4;
}
}
inlinesize_tbase2int_bs_upper_only(char c) {
switch (c) {
case'A':
return0;
case'C':
return3;
case'G':
return2;
case'T':
return3;
default:
return4;
}
}
inlinesize_tbase2int_bs(char c) {
switch (c) {
case'A':
return0;
case'C':
return3;
case'G':
return2;
case'T':
return3;
case'a':
return0;
case'c':
return3;
case'g':
return2;
case't':
return3;
default:
return4;
}
}
inlinesize_tbase2int_bs_ag_upper_only(char c) {
switch (c) {
case'A':
return0;
case'C':
return1;
case'G':
return0;
case'T':
return3;
default:
return4;
}
}
inlinesize_tbase2int_bs_ag(char c) {
switch (c) {
case'A':
return0;
case'C':
return1;
case'G':
return0;
case'T':
return3;
case'a':
return0;
case'c':
return1;
case'g':
return0;
case't':
return3;
default:
return4;
}
}
inlinesize_tbase2int_bs_rc(char c) {
switch (c) {
case'A':
return0;
case'C':
return1;
case'G':
return0;
case'T':
return3;
case'a':
return0;
case'c':
return1;
case'g':
return0;
case't':
return3;
}
return4;
}
inlinesize_tbase2int_rc(char c) {
switch (c) {
case'A':
return3;
case'C':
return2;
case'G':
return1;
case'T':
return0;
case'a':
return3;
case'c':
return2;
case'g':
return1;
case't':
return0;
}
return4;
}
inlinecharint2base(size_t c) {
switch (c) {
case0:
return'A';
case1:
return'C';
case2:
return'G';
case3:
return'T';
}
return'N';
}
inlinecharint2base_rc(size_t c) {
switch (c) {
case3:
return'A';
case2:
return'C';
case1:
return'G';
case0:
return'T';
}
return'N';
}
inlineboolisvalid(char c) { return (base2int(c) != 4); }
inline std::string i2mer(size_t n, size_t index) {
std::string s(n, '');
do {
--n;
s[n] = int2base(index % smithlab::alphabet_size);
index /= smithlab::alphabet_size;
} while (n > 0);
return s;
}
inline std::string i2mer_rc(size_t n, size_t index) {
std::string s(n, '');
do {
--n;
s[n] = int2base_rc(index % smithlab::alphabet_size);
index /= smithlab::alphabet_size;
} while (n > 0);
return s;
}
inlinesize_tmer2i(const std::string::const_iterator a,
std::string::const_iterator b) {
size_t multiplier = 1, index = 0;
do {
--b;
index += base2int(*b) * multiplier;
multiplier *= smithlab::alphabet_size;
} while (b > a);
return index;
}
inlinesize_tmer2i_rc(std::string::const_iterator a,
const std::string::const_iterator b) {
size_t multiplier = 1, index = 0;
do {
index += base2int_rc(*a) * multiplier;
multiplier *= smithlab::alphabet_size;
} while (++a < b);
return index;
}
template <classT> std::string toa(T t) {
std::ostringstream s;
s << t;
return s.str();
}
////////////////////////////////////////////////////////////////////////
// Code for dealing with the DNA alphabet
charcomplement(int i);
inline std::string revcomp(const std::string &s) {
std::string r;
std::transform(s.begin(), s.end(), back_inserter(r), complement);
std::reverse(r.begin(), r.end());
return r;
}
inlinevoidrevcomp_inplace(std::string &s) {
std::transform(s.begin(), s.end(), s.begin(), complement);
std::reverse(s.begin(), s.end());
}
inlinevoidrevcomp_inplace(std::string::iterator first,
std::string::iterator last) {
std::transform(first, last, first, complement);
std::reverse(first, last);
}
inline std::string bits2string_masked(size_t mask, size_t bits) {
std::string s;
size_t selector = smithlab_bits::high_bit;
for (size_t i = 0; i < smithlab_bits::word_size; ++i) {
s += (selector & bits & mask) ? '1' : '0';
selector >>= 1;
}
return s;
}
inline std::string bits2string_for_positions(size_t positions, size_t bits) {
std::string s;
size_t selector = smithlab_bits::high_bit;
for (size_t i = 0; i < smithlab_bits::word_size; ++i) {
s += (selector & bits) ? '1' : '0';
selector >>= 1;
}
return s.substr(s.length() - positions);
}
inlinesize_tpercent(constsize_t a, constsize_t b) {
returnstatic_cast<size_t>((100.0 * a) / b);
}
inlineboolvalid_base(char c) {
char i = std::toupper(c);
return (i == 'A' || i == 'C' || i == 'G' || i == 'T');
}
inlinesize_tmer2index(constchar *s, size_t n) {
size_t multiplier = 1, index = 0;
do {
--n;
index += base2int(s[n]) * multiplier;
multiplier *= smithlab::alphabet_size;
} while (n > 0);
return index;
}
inlinesize_tkmer_counts(const std::vector<std::string> &seqs,
std::vector<size_t> &counts, size_t k) {
counts.clear();
size_t nwords = static_cast<size_t>(
pow(static_cast<float>(smithlab::alphabet_size), static_cast<int>(k)));
counts.resize(nwords, 0);
size_t total = 0;
for (size_t i = 0; i < seqs.size(); ++i) {
std::vector<char> seq(seqs[i].length() + 1, '\0');
auto seq_data = seq.data();
copy(cbegin(seqs[i]), cend(seqs[i]), seq_data);
for (size_t j = 0; j < seqs[i].length() - k + 1; ++j)
if (std::count_if(seq_data + j, seq_data + j + k, &valid_base) ==
static_cast<int>(k)) {
counts[mer2index(seq_data + j, k)]++;
++total;
}
}
return total;
}
/*
* How to use the ProgressBar:
*
* //====================================
* const uint64_t lim = v.size();
* ProgressBar progress(lim);
* if (VERBOSE)
* progress.report(cerr, 0);
* for (uint64_t i = 0; i < lim; ++i) {
* if (VERBOSE && progress.time_to_report(i))
* progress.report(cerr, i);
* v[i] += x;
* }
* if (VERBOSE)
* progress.report(cerr, lim);
*/
classProgressBar {
public:
ProgressBar(constsize_t x, const std::string message = "completion")
: total(x), prev(0), mid_tag(message) {
// the 3 below is for the default left_tag and right_tag printed
// width and the 5 is for the width of the percent (up to 100)
// plus two pipes ('|')
bar_width = max_bar_width - message.length() - 3 - 5;
bar = std::string(bar_width, '');
}
booltime_to_report(constsize_t i) const {
returnstd::round((100.0 * std::min(i, total)) / total) > prev;
}
voidreport(std::ostream &out, constsize_t i);
private:
size_t total{};
size_t prev{};
size_t bar_width{};
std::string left_tag = "\r[";
std::string mid_tag;
std::string bar;
std::string right_tag = "%]";
staticconstsize_t max_bar_width = 72;
};
externchar to_valid_five_letter[256];
#endif