-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmkmh.hpp
1453 lines (1232 loc) · 45.8 KB
/
mkmh.hpp
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef MKMH_D
#define MKMH_D
#include <vector>
#include <queue>
#include <set>
#include <unordered_map>
#include <string>
#include <cstring>
#include <sstream>
#include <locale>
#include <cstdint>
#include <iostream>
#include <algorithm>
#include <assert.h>
#include <omp.h>
#include <assert.h>
#include <bitset>
#include "murmur3.hpp"
#include "HASHTCounter.hpp"
#define DBGG
namespace mkmh{
using namespace std;
using namespace mkmh;
typedef uint64_t hash_t;
const static uint64_t MOD_HASH_ONE = 654;
const static uint64_t MOD_HASH_TWO = 3459922;
const static uint64_t MOD_HASH_THREE = 42;
struct mkmh_kmer_list_t{
char** kmers;
int length;
int k;
mkmh_kmer_list_t(){
};
mkmh_kmer_list_t(int length, int k){
length = length;
k = k;
kmers = new char*[length];
};
~mkmh_kmer_list_t(){
for (int i = 0; i < length; ++i){
delete [] kmers[i];
}
delete [] kmers;
};
};
struct mkmh_minimizer {
uint64_t pos;
uint32_t length;
string seq;
bool operator<(const mkmh_minimizer& rhs) const {return seq < rhs.seq;};
};
struct mkmh_hash_vec {
hash_t* hashes;
uint32_t size;
uint64_t capacity;
mkmh_hash_vec(){
size = 0;
capacity = 1000;
hashes = new hash_t[capacity];
};
mkmh_hash_vec(uint32_t cap){
size = 0;
capacity = cap;
hashes = new hash_t[capacity];
};
mkmh_hash_vec(const mkmh_hash_vec& other){
size = other.size;
capacity = other.capacity;
hashes = new hash_t[capacity];
for (size_t i = 0; i < other.size; ++i){
hashes[i] = other.hashes[i];
}
};
~mkmh_hash_vec(){
if (this->capacity > 0){
delete [] this->hashes;
}
};
inline void set_capacity(int cap){
if (this->capacity > 0){
delete [] hashes;
}
size = 0;
capacity = cap;
hashes = new hash_t[capacity];
};
void resize(double factor = 1.2){
size_t newcap = int(factor * capacity);
hash_t* new_hashes = new hash_t[newcap];
capacity = newcap;
for (int i = 0; i < size; ++i){
new_hashes[i] = hashes[i];
}
delete [] hashes;
hashes = new_hashes;
};
void emplace( hash_t h){
if (this->size == this->capacity){
resize();
}
this->hashes[this->size++] = h;
};
// Trim excess array space IFF the number of elements is
// less than some percentage of capacity
void trim(double factor = 0.6){
if (this->size < int(this->capacity * factor)){
int newcap = size;
hash_t* new_hashes = new hash_t[newcap];
for (int i = 0; i < size; ++i){
new_hashes[i] = hashes[i];
}
delete [] hashes;
hashes = new_hashes;
capacity = newcap;
}
};
void sort(){
std::sort(this->hashes, this->hashes + this->size);
};
};
// Crazy hack char table to test for canonical bases
static const int valid_dna[127] = {
1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,0,1,0,1,1,1,
0,1,1,1,1,1,1,1,1,1,
1,1,1,0,1,1,1,1,1,1,
1,1,1,1,1,1,0,1,0,1,
1,1,0,1,1,1,1,1,1,1,
1,1,1,1,1,0,1,1,1,1,
1,1,1,1,1,1
};
static const int DNA_base_index[127] = {
4,
4,4,4,4,4,4,4,4,4,4,
4,4,4,4,4,4,4,4,4,4,
4,4,4,4,4,4,4,4,4,4,
4,4,4,4,4,4,4,4,4,4,
4,4,4,4,4,4,4,4,4,4,
4,4,4,4,4,4,4,4,4,4,
4,4,4,4,0,4,1,4,4,4,
2,4,4,4,4,4,4,4,4,4,
4,4,4,3,4,4,4,4,4,4,
4,4,4,4,4,4,0,4,1,4,
4,4,2,4,4,4,4,4,4,4,
4,4,4,4,4,3,4,4,4,4,
4,4,4,4,4,4
};
// Reverse complement lookup table
static char rev_arr [26] = {
84, 66, 71, 68, 69,
70, 67, 72, 73, 74,
75, 76, 77, 78, 79,
80, 81, 82, 83, 65,
85, 86, 87, 88, 89, 90
};
// Check a string (as a char*) for non-canonical DNA bases
inline bool canonical(const char* x, int len){
bool trip = false;
for (int i = 0; i < len; ++i){
trip |= valid_dna[x[i]];
}
return !trip;
};
inline bool canonical(string seq){
const char* x = seq.c_str();
int len = seq.length();
return canonical(x, len);
};
/* Reverse complement the string seq
* (assumes seq is DNA, and returns non-ACTG letters as-is*/
/* Reverse complement a C string
* NB: does not check safety of string lengths.
* NB: ret is modified to hold the reverse complement of seq.
* */
inline void reverse_complement(const char* seq, char* ret, int len){
//assert(seq != ret);
if (ret == NULL){
ret = new char[len+1];
}
for (int i = len - 1; i >=0; i--){
ret[ len - 1 - i ] = (char) rev_arr[ (int) seq[i] - 65];
}
ret[len] = '\0';
};
/* Reverse complement a string */
inline string reverse_complement(string& seq){
const char* s = seq.c_str();
int seqlen = seq.length();
char* ret = new char[seqlen];
reverse_complement(s, ret, seqlen);
string s_revc(ret);
delete [] ret;
return s_revc;
};
/* Capitalize all characters in a string */
/* Capitalize a C string */
inline void to_upper(char*& seq, size_t length){
for (int i = 0; i < length; i++){
char c = seq[i];
seq[i] = ( (c - 91) > 0 ? c - 32 : c);
}
};
inline void to_upper(const char* seq, int length, char* ret){
for (int i = 0; i < length; i++){
char c = seq[i];
ret[i] = ( (c - 91) > 0 ? c - 32 : c);
}
};
/* Capitalize a string */
inline string to_upper(string& seq){
for (int i = 0; i < seq.length(); i++){
char c = seq[i];
seq[i] = ((c - 91) > 0 ? c - 32 : c);
}
return seq;
};
/* Reverse a string */
inline string reverse(string seq){
string copy = string(seq);
std::reverse(copy.begin(), copy.end());
return copy;
};
/* Reverse a C string*/
inline void reverse(char* seq, const int& len){
char tmp;
for (int i = 0; i < len; ++i){
tmp = seq[len - i];
seq[ len - i ] = seq[i];
seq[i] = tmp;
}
};
/** Custom sort function wrapping the STL implementation, mostly to allow descending sort. */
inline void sort(hash_t*& hashes, int len, bool descending = false){
if (!descending){
std::sort(hashes, hashes + len);
}else{
std::sort(hashes, hashes + len, std::less<uint64_t>());
}
};
inline void sort(vector<hash_t>& hashes, bool descending = false){
if (!descending){
std::sort(hashes.begin(), hashes.end());
}
else{
std::sort(hashes.begin(), hashes.end(), std::less<uint64_t>());
}
};
inline void kmerize(char* seq, const int& seq_len, const int& k, char** kmers, int& kmer_num){
char** ret = new char*[seq_len - k];
kmer_num = seq_len - k;
for (int i = 0; i < kmer_num; ++i){
ret[i] = new char[ k + 1 ];
memcpy(ret[i], seq + i, k);
}
}
/* Returns the forward kmers of a sequence */
inline vector<string> kmerize(string seq, int k){
vector<string> ret(seq.length() - k, "");
#pragma omp parallel for
for (int i = 0; i < seq.length() - k; i++){
string s = seq.substr(i, k);
//#pragma omp atomic read
ret[i] = s;
//ret.push_back(s);
//ret.push_back(reverse(reverse_complement(s)));
}
return ret;
};
inline mkmh_kmer_list_t kmerize(char* seq, int seq_len, int k){
mkmh_kmer_list_t ret;
ret.kmers = new char*[seq_len - k];
ret.k = k;
ret.length = seq_len - k;
for (int i = 0; i < ret.length; ++i){
char* km = new char[k + 1];
memcpy(km, seq + i, k);
ret.kmers[i] = new char[k + 1];
ret.kmers[i] = km;
ret.kmers[i][k] = '\0';
}
return ret;
};
inline bool strcompare(const char* a, const int& alen, const char* b, const int& blen){
if (alen != blen){
return false;
}
for (int i = 0; i < alen; ++i){
if (a[i] != b[i]){
return false;
}
}
return true;
};
inline void count_kmer_occurrence(const char* seq, const int& seq_len, const char* kmer, const int& k, int& count){
count = 0;
int k_num = seq_len - k;
for (int i = 0; i < k_num; ++i){
int addit = strcompare((const char*) seq + i, k, kmer, k) ? 1 : 0;
count += addit;
}
};
inline void count_substring_occurrence(const char* seq, const int& seq_len, const char* kmer, const int& k, int& count){
count = 0;
int s_num = seq_len - k;
int i = 0;
while (i < s_num){
if (strcompare(seq + i, k, kmer, k)){
++count;
i = i + k;
}
else{
i += 1;
}
}
};
/* Print the kmers of a string, tab separated, to cout
* avoids allocating any new memory. */
inline void print_kmers(char* seq, const int& len, int k){
int kmerized_length = len - k;
stringstream st;
for (int i = 0; i < kmerized_length - 1; ++i){
int j = 0;
while (j < k){
st << seq[i + j];
++j;
}
st << "\t";
}
int j = 0;
while(j < k){
st << seq[kmerized_length - 1 + j];
++j;
}
st << endl;
cout << st.str();
st.str("");
};
/* Returns the forward and reverse-reverse complement kmers for all kmer sizes in k */
inline vector<string> multi_kmerize(string seq, vector<int> kSizes){
int i = 0;
vector<string> ret;
//ret.reserve(kSizes.size() * 1000);
for (auto k : kSizes){
vector<string> kmers = kmerize(seq, k);
ret.reserve(ret.size() + kmers.size());
ret.insert(ret.end(), kmers.begin(), kmers.end());
//for (i = 0; i + k < seq.length(); i++){
// ret.push_back(seq.substr(i, i+k));
// ret.push_back(reverse(reverse_complement(seq.substr(i, i+k))));
//}
}
return ret;
};
/* Returns a deduplicated set of string kmers */
inline vector<string> kmer_set(vector<string> kmers) {
set<string> uniqs = set<string> (kmers.begin(), kmers.end());
vector<string> ret = vector<string> (uniqs.begin(), uniqs.end());
return ret;
};
/* Returns a heap (priority queue) of the kmers of a read converted to ints. */
/* Returns a heap (priority queue) of the kmers of the read */
inline priority_queue<string> kmer_heap(string seq, vector<int> kmer){
vector<string> base;
//priority_queue<string> ret(base.begin(), base.end());
for (auto k : kmer){
vector<string> outmers(seq.length() - k, "");
for (int i = 0; i < seq.length() - k; i++){
string forward = seq.substr(i, k);
string revrev = reverse(reverse_complement(forward));
//ret.push( (revrev < forward ? revrev : forward) );
outmers[i] = (revrev < forward ? revrev : forward);
}
base.reserve(outmers.size() + base.size());
base.insert(base.end(), outmers.begin(), outmers.end());
}
priority_queue<string> ret(base.begin(), base.end());
return ret;
};
const static int first_bits[5] = {0,0,1,1,0};
const static int second_bits[5] = {1,0,0,1,0};
/* Converts a string kmer to an integer representation */
inline bool kmer_to_integer(const char* kmer, const int& length, hash_t*& h){
assert(length < 31);
*h = 0;
std::bitset<64> rb;
int bit = -1;
for (int i = 0; i < length; ++i){
int index = DNA_base_index[kmer[i]];
if (index == 4){
*h = 0;
return false;
}
++bit;
rb.set(63 - bit, first_bits[index]);
++bit;
rb.set(63 - bit, second_bits[index]);
}
*h = rb.to_ullong();
return true;
}
inline bool kmer_to_integer(const char* kmer, const int& length, hash_t& h){
assert(length < 31);
h = 0;
std::bitset<64> rb;
int bit = -1;
for (int i = 0; i < length; ++i){
int index = DNA_base_index[kmer[i]];
if (index == 4){
h = 0;
return false;
}
++bit;
rb.set(63 - bit, first_bits[index]);
++bit;
rb.set(63 - bit, second_bits[index]);
}
h = rb.to_ullong();
return true;
}
/** Primary calc_hashes function **/
/** Takes the string to be hashed, its length,
* a single kmer size, a pointer to hold the hashes,
* and an integer to hold the number of hashes.
*
* Possibly thread safe:
* seq, len and k are not modified
* new [] operator is known threadsafe
* User must handle hashes and numhashes properly in calling function.
**/
inline void calc_hashes(const char* seq, const int& len,
const int& k, hash_t*& hashes, int& numhashes){
char* reverse = new char[k+1];
uint32_t rhash[4];
uint32_t fhash[4];
//hash_t tmp_fwd;
//hash_t tmp_rev;
numhashes = len - k;
hashes = new hash_t[numhashes];
for (int i = 0; i < numhashes; ++i){
if (canonical(seq + i, k)){
reverse_complement(seq + i, reverse, k);
MurmurHash3_x64_128(seq + i, k, 42, fhash);
MurmurHash3_x64_128(reverse, k, 42, rhash);
//hash_t tmp_fwd = *((hash_t*) fhash);
//hash_t tmp_rev = *((hash_t*) rhash);
hash_t tmp_fwd = static_cast<uint64_t>(fhash[0]) << 32 | fhash[1];
hash_t tmp_rev = static_cast<uint64_t>(rhash[0]) << 32 | rhash[1];
hashes[i] = (tmp_fwd < tmp_rev ? tmp_fwd : tmp_rev);
}
else{
hashes[i] = 0;
}
}
delete [] reverse;
};
/**
* Thanks to https://stackoverflow.com/questions/39242932/how-to-encode-char-in-2-bits
* NB: limited to kmers < k = 32
*/
inline uint64_t kmer_to_integer(const char* kmer, const int& length){
uint64_t ret = 0;
uint64_t retrev = 0;
char* rev;
reverse_complement(kmer, rev, length);
for (int i = 0; i < length; ++i){
ret = (ret << 2) | ((kmer[i] >> 1) & 3);
retrev = (retrev << 2) | ((rev[i] >> 1) & 3);
}
return ret < retrev ? ret : retrev;
};
/* Returns a deduplicated set of kmers or hashes as a vector<T> */
template<typename T>
inline vector<T> v_set(vector<T> kmers){
set<T> s = set<T>(kmers.begin(), kmers.end());
vector<T> ret = vector<T>(s.begin(), s.end());
return ret;
}
/* Returns only the forward shingles size k of a sequence */
inline vector<string> shingle(string seq, int k){
int i = 0;
vector<string> ret;
for (i = 0; i < seq.length() - k; i++){
ret.push_back(seq.substr(i, k));
}
return ret;
}
/** Returns an mkmh_minimizer struct, equivalent to a tuple(kmer, position, kmer length), for every position in the genome **/
inline vector<mkmh_minimizer> kmer_tuples(string seq, int k){
vector<string> kmers = kmerize(seq, k);
vector<mkmh_minimizer> tups (kmers.size());
for (int i = 0; i < kmers.size(); i++){
mkmh_minimizer mm;
mm.seq = kmers[i];
mm.pos = i;
mm.length = k;
tups[i] = mm;
}
return tups;
}
/** Finds the (w, k) minimizers of a string **/
inline vector<mkmh_minimizer> minimizers(string seq, int k, int w){
vector<mkmh_minimizer> ret;
vector<mkmh_minimizer> kmert = kmer_tuples(seq, k);
int i = 0;
for (i = 0; i + w < kmert.size(); ++i){
// get and sort kmers in window (i, i + w)
vector<mkmh_minimizer> window_kmers(kmert.begin() + i, kmert.begin() + i + w);
std::sort(window_kmers.begin(), window_kmers.end());
// TODO filter minimizers if needed, e.g. to remove poly-As
// create an mkmh_minimizer struct
// tuck minimizer in ret
ret.push_back(*(window_kmers.begin()));
}
return v_set(ret);
};
/** Finds the (w,k) minimizers and reports all of them (including duplicates) **/
inline vector<mkmh_minimizer> unreduced_minimizers(string seq, int k, int w){
vector<mkmh_minimizer> ret;
vector<mkmh_minimizer> kmert = kmer_tuples(seq, k);
int i = 0;
for (i = 0; i + w < kmert.size(); ++i){
// get and sort kmers in window (i, i + w)
vector<mkmh_minimizer> window_kmers(kmert.begin() + i, kmert.begin() + i + w);
std::sort(window_kmers.begin(), window_kmers.end());
// TODO filter minimizers if needed, e.g. to remove poly-As
// create an mkmh_minimizer struct
// tuck miimizer in ret
ret.push_back(*(window_kmers.begin()));
}
return ret;
};
inline void top_64_bits(uint32_t*& hash_holder, hash_t& ret){
ret = static_cast<uint64_t>(hash_holder[0]) << 32 | hash_holder[1];
};
// Calculate a single hash of a sequence (usually a kmer).
// Takes in:
// seq: a char* (not affected)
// len: the length of seq (not affected)
// reverse: a char* of length(seq); modified to hold reverse_complement of (seq)
// forhash: a 128-bit int (e.g. uint32_t[4]) for holding forward hash.
// revhash: a 128-bit int (e.g. uint32_t[4]) for holding reverse hash.
// finhash: a 64-bit int (e.g. uint64_t) which holds the lesser of (forhash, revhash);
inline void calc_hash(const char* seq,
const int& len,
char*& reverse,
uint32_t*& forhash,
uint32_t*& revhash,
hash_t*& fin_hash){
if (canonical(seq, len)){
reverse_complement(seq, reverse, len);
MurmurHash3_x64_128(seq, len, 42, forhash);
MurmurHash3_x64_128(reverse, len, 42, revhash);
hash_t tmp_fwd = static_cast<uint64_t>(forhash[0]) << 32 | forhash[1];
hash_t tmp_rev = static_cast<uint64_t>(revhash[0]) << 32 | revhash[1];
*fin_hash = (tmp_fwd < tmp_rev ? tmp_fwd : tmp_rev);
}
else{
*forhash = 0;
*revhash = 0;
*fin_hash = 0;
}
};
/* Calculate the 64-bit hash for a string defined by seq and the length of seq */
inline hash_t calc_hash(const char* seq, int seqlen){
char* reverse = new char[seqlen];
uint32_t* fhash = new uint32_t [4];
uint32_t* rhash = new uint32_t [4];
hash_t* fin_hash = new hash_t [1];
calc_hash(seq, seqlen, reverse, fhash, rhash, fin_hash);
hash_t ret = *(fin_hash);
delete [] reverse;
delete [] fhash;
delete [] rhash;
delete [] fin_hash;
return ret;
}
/* Calculate the hash for a string seq */
inline hash_t calc_hash(string seq){
int k = seq.length();
const char* x = seq.c_str();
return calc_hash(x, k);
};
/** calc_hashes for multiple kmers sizes **/
/** returns an array, with hashes for each kmer size concatenated
* to those of the previous kmer size
**/
inline void calc_hashes(const char* seq, int seq_length,
vector<int> kmer_sizes,
hash_t*& hashes, int& numhashes){
numhashes = 0;
// This holds the number of hashes preceeding the
// kmer size currently being hashed.
vector<int> offsets;
for (auto k : kmer_sizes){
offsets.push_back(numhashes);
numhashes += seq_length - k;
}
hashes = new hash_t [numhashes];
for (int i = 0; i < kmer_sizes.size(); ++i){
int k = kmer_sizes[i];
int local_numhash;
hash_t* l_start;
calc_hashes(seq, seq_length, k, l_start, local_numhash);
memcpy(hashes + offsets[i], l_start, local_numhash * sizeof(hash_t));
delete [] l_start;
}
}
inline void calc_hashes(const char* seq, const int& len,
const int& k, hash_t*& hashes, int& numhashes, mkmh::HASHTCounter*& htc){
char* reverse = new char[k+1];
uint32_t rhash[4];
uint32_t fhash[4];
//hash_t tmp_fwd;
//hash_t tmp_rev;
numhashes = len - k;
hashes = new hash_t[numhashes];
for (int i = 0; i < numhashes; ++i){
if (canonical(seq + i, k)){
reverse_complement( (seq + i), reverse, k);
MurmurHash3_x64_128( (seq + i), k, 42, fhash);
MurmurHash3_x64_128(reverse, k, 42, rhash);
//hash_t tmp_fwd = *((hash_t*) fhash);
//hash_t tmp_rev = *((hash_t*) rhash);
hash_t tmp_fwd = static_cast<uint64_t>(fhash[0]) << 32 | fhash[1];
hash_t tmp_rev = static_cast<uint64_t>(rhash[0]) << 32 | rhash[1];
hashes[i] = (tmp_fwd < tmp_rev ? tmp_fwd : tmp_rev);
htc->increment(hashes[i]);
}
else{
hashes[i] = 0;
}
}
delete [] reverse;
}
inline void calc_hashes(const char* seq, int seq_length,
vector<int> kmer_sizes,
hash_t*& hashes, int& numhashes,
HASHTCounter*& htc){
numhashes = 0;
// This holds the number of hashes preceeding the
// kmer size currently being hashed.
vector<int> offsets;
for (auto k : kmer_sizes){
offsets.push_back(numhashes);
numhashes += seq_length - k;
}
hashes = new hash_t [numhashes];
for (int i = 0; i < kmer_sizes.size(); ++i){
int k = kmer_sizes[i];
int local_numhash;
//hash_t* l_start = hashes + offsets[i];
hash_t* l_start;
// HTC gets incremented within this function, so no need to do a bulk increment.
calc_hashes(seq, seq_length, k, l_start, local_numhash, htc);
memcpy(hashes + offsets[i], l_start, local_numhash * sizeof(hash_t));
delete [] l_start;
}
};
/* Calculate all the hashes of the kmers length k of seq */
inline vector<hash_t> calc_hashes(const char* seq, int seq_length, int k){
int numhashes = 0;
hash_t* hashes;
calc_hashes(seq, seq_length, k, hashes, numhashes);
vector<hash_t> ret(numhashes);
for (int i = 0; i < numhashes; i++){
ret[i] = *(hashes + i);
}
delete [] hashes;
return ret;
};
/* Calculate all the hashes of the kmers length k of seq */
inline vector<hash_t> calc_hashes(string seq, int k){
const char* x = seq.c_str();
int l = seq.length();
return calc_hashes(x, l, k);
}
inline vector<hash_t> calc_hashes(const char* seq, const int& len, const vector<int>& k_sizes){
vector<hash_t> ret;
for (auto k : k_sizes){
vector<hash_t> t = calc_hashes(seq, len, k);
ret.insert(ret.end(), t.begin(), t.end());
}
return ret;
};
/** Calculate the hashes of seq
* and fill in a HASHTCounter htc so that
* hashes can be kept or removed based on the number of times
* they occur in seq.
**/
// void calc_hashes(const char* seq, const int& len,
// const int& k, hash_t*& hashes, int& numhashes, HASHTCounter*& htc);
// void calc_hashes(const char* seq, const int& len,
// const int& k, hash_t*& hashes, int& numhashes, unordered_map<hash_t, int> counts);
/** Calculate the hashes for kmers of multiple lengths in <kmer>
*/
inline vector<hash_t> calc_hashes(string seq, const vector<int>& k_sizes){
const char* x = seq.c_str();
int l = seq.length();
return calc_hashes(x, l, k_sizes);
};
inline void hash_intersection_size(const hash_t* alpha, const int& alpha_size, const hash_t* beta, const int& beta_size, int& ret){
int a_ind = 0;
int b_ind = 0;
ret = 0;
while (a_ind < alpha_size && b_ind < beta_size){
if (alpha[a_ind] == beta[b_ind] && alpha[a_ind] != 0){
++ret;
++a_ind;
++b_ind;
}
else if (alpha[a_ind] > beta[b_ind]){
++b_ind;
}
else{
++a_ind;
}
}
};
inline void hash_set_intersection_size(const hash_t* alpha, const int& alpha_size, const hash_t* beta, const int& beta_size, int& ret){
int a_ind = 0;
int b_ind = 0;
ret = 0;
hash_t prev = 0;
while (a_ind < alpha_size && b_ind < beta_size){
if (alpha[a_ind] == beta[b_ind] && alpha[a_ind] != prev && alpha[a_ind] != 0){
++ret;
prev = alpha[a_ind];
++a_ind;
++b_ind;
}
else if (alpha[a_ind] > beta[b_ind]){
++b_ind;
}
else{
++a_ind;
}
}
};
/** Calculate a MinHash sketch for kmers length (2 * k) with skip bases in between the two k-length halves **/
inline vector<hash_t> allhash_64_linkmer(string seq, int k, int skip){
vector<hash_t> ret(seq.size() - (k*2));
int last_kmer_ind = seq.size() - (skip + (2 * k) );
#pragma omp parallel for
for (int i = 0; i <= last_kmer_ind; ++i){
char * linkmer = new char [k * 2];
for (int j = 0; j < k; ++j){
linkmer[j] = seq[i + j];
linkmer[k + j] = seq[i + skip + k + j];
}
hash_t c_hash = calc_hash(linkmer, 2*k);
ret[i] = c_hash;
delete [] linkmer;
}
return ret;
}
/* Returns the forward shingles of all k sizes of a sequence */
/* Shingles are just forward-only kmers */
inline vector<string> multi_shingle(string seq, vector<int> kSizes){
int i = 0;
vector<string> ret;
for (auto k : kSizes){
for (i = 0; i + k < seq.length(); i++){
ret.push_back(seq.substr(i, k));
}
}
return ret;
};
/** Mask (by converting to zero) hashes that don't satisfy min_occ <= frequency(h) <= max_occ **/
inline void mask_by_frequency(hash_t*& hashes, const int& num_hashes,
HASHTCounter* htc,
int min_occ = 0,
uint32_t max_occ = UINT32_MAX){
for (int i = 0; i < num_hashes; ++i){
int freq = 0;
htc->get(hashes[i], freq);
hashes[i] = (min_occ <= freq && freq <= max_occ) ? hashes[i] : 0;
}
};
inline vector<hash_t> minhashes(hash_t* hashes, int num_hashes, int sketch_size, bool useBottom){
vector<hash_t> x = vector<hash_t>(hashes, hashes + num_hashes);
std::sort(x.begin(), x.end());
int valid_ind = 0;
while (x[valid_ind] == 0){
valid_ind++;
}
/*for (auto xx : x){
if (xx != 0){
cerr << xx << endl;
}
}*/
int hashmax = valid_ind + sketch_size < num_hashes ? valid_ind + sketch_size : num_hashes - 1;
return std::vector<hash_t>(x.begin() + valid_ind, x.begin() + hashmax);
}
/** MinHash - given an array of hashes, modify the mins array to hold
* the lowest/highest N (excluding zeros) **/
inline void minhashes(hash_t*& hashes, int num_hashes,
int sketch_size,
hash_t*& ret,
int& retsize,
bool use_bottom=true){
int maxlen = min(num_hashes, sketch_size);
ret = new hash_t[maxlen];
retsize = 0;
mkmh::sort(hashes, num_hashes, !use_bottom);
int start = 0;
while (retsize < sketch_size && start < num_hashes){
if (hashes[start] != 0){
ret[retsize] = hashes[start];
++retsize;
}
++start;
}
};
inline void minhashes_frequency_filter(hash_t* hashes, int num_hashes,
int sketch_size,
hash_t*& ret,
int& retsize,
HASHTCounter* htc,
int min_occ = 0,
uint32_t max_occ = UINT32_MAX,
bool use_bottom=true){
ret = new hash_t[sketch_size];
mkmh::sort(hashes, num_hashes, !use_bottom);
int maxlen = min(num_hashes, sketch_size);
int start = 0;
while (retsize < sketch_size && start < num_hashes){
int freq = htc->get(hashes[start]);
if (hashes[start] != 0 && freq <= max_occ && freq >= min_occ){
ret[retsize] = hashes[start];
++retsize;
}
++start;
}
};
inline void minhashes_min_occurrence_filter(hash_t* hashes, int num_hashes,
int sketch_size,
hash_t*& ret,
int& retsize,
HASHTCounter* htc,
int min_occ = 0,
bool use_bottom=true){
ret = new hash_t[sketch_size];
mkmh::sort(hashes, num_hashes, !use_bottom);
int maxlen = min(num_hashes, sketch_size);
int start = 0;
while (retsize < sketch_size && start < num_hashes){
if (hashes[start] != 0 && htc->get(hashes[start]) >= min_occ){
ret[retsize] = hashes[start];
++retsize;
}
++start;
}
};
/** Base MinHash function - return the lowest n = min(num_hashes, sketch_size) hashes. **/