forked from yechengxi/DBG2OLC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BasicDataStructure.h
1304 lines (1075 loc) · 22.7 KB
/
BasicDataStructure.h
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 __BASIC_DATA_STRUCTURE_H
#define __BASIC_DATA_STRUCTURE_H
#include <iostream>
#include <string>
#include <string.h>
#include <stdint.h>
#include <vector>
#include <map>
#include <list>
#include <algorithm>
#include <sstream>
#include <fstream>
#include "time.h"
#include "getRSS.h"
using namespace std;
void* myMalloc (size_t size) {
void *p = malloc(size);
if (NULL==p) {
cerr << "malloc error: Couldn't allocate memory for size="<<size<<". Exiting with error.\n";
cerr << "Current usage = "<<getCurrentRSS()<<"\n";
cerr << "Peak usage = "<<getPeakRSS()<<"\n";
exit(1);
}
return(p);
}
void* myCalloc (size_t num, size_t size) {
void *p = calloc(num, size);
if (NULL==p) {
cerr << "calloc error: Couldn't allocate memory for "<<num<<" elements of size "<<size<<" each. Exiting with error.\n";
cerr << "Current usage = "<<getCurrentRSS()<<"\n";
cerr << "Peak usage = "<<getPeakRSS()<<"\n";
exit(1);
}
return(p);
}
// These are the structures to save the k-mers.32 bases, 64 bases, 96 bases, 128 bases.
struct kmer_t
{
uint64_t kmer:62,cov:2;
};
struct kmer_t2
{
uint64_t kmer[2];
};
struct kmer_t3
{
uint64_t kmer[3];
};
struct kmer_t4
{
uint64_t kmer[4];
};
// edge structure, maximum length 25
struct edge_node
{
uint64_t edge:50,edge_cov:4,len:6,masked:1,removed:1;
struct edge_node *nxt_edge;
};
// edge structure, maximum length 64
struct edge_node2
{
uint64_t edge[2];
uint16_t edge_cov:8,len:8;
struct edge_node2 *nxt_edge;
};
// information for a k-mer node
struct kmer_info
{
//flags, recording if the node is used in a search, whether the node have branches on its sides...
//uint8_t used:1,split_left:1,split_right:1,removed:1,flip:1,marked:1,repeat:1,masked:1;
//pointers to edge links
//struct edge_node *left;
//struct edge_node *right;
//uint16_t cov1;
//for building scaffolds
int32_t contig_no:28,flip:1,cov1:2;
int32_t cod;
};
//split the above structure, round 1 of graph construction:
struct kmer_info_r1
{
uint8_t cov1;
};
//Bloom filter structure
struct BF_info
{
uint8_t * BF_HT;
uint64_t m;
int d;
bool Bloom;
};
// a complete k-mer node,
struct bucket
{
struct kmer_t kmer_t; //32 bp
struct kmer_info kmer_info;
bucket *nxt_bucket;
};
struct bucket2
{
struct kmer_t2 kmer_t2; //64 bp
struct kmer_info kmer_info;
bucket2 *nxt_bucket;
};
struct bucket3
{
struct kmer_t3 kmer_t3;
struct kmer_info kmer_info;
bucket3 *nxt_bucket;
};
struct bucket4
{
struct kmer_t4 kmer_t4;
struct kmer_info kmer_info;
bucket4 *nxt_bucket;
};
struct bucket0
{
uint64_t *kmer_t; //any kmer size
struct kmer_info kmer_info;
bucket0 *nxt_bucket;
};
// a structure recording the information of removed k-mers in the BFS bubble removal
struct bucket_rm
{
struct kmer_t kmer_t;
struct kmer_t merged_kmer;
bool flip;
bucket_rm *nxt_bucket;
};
struct bucket_rm2
{
struct kmer_t2 kmer_t2;
struct kmer_t2 merged_kmer;
bool flip;
bucket_rm2 *nxt_bucket;
};
struct bucket_rm3
{
struct kmer_t3 kmer_t3;
struct kmer_t3 merged_kmer;
bool flip;
bucket_rm3 *nxt_bucket;
};
struct bucket_rm4
{
struct kmer_t4 kmer_t4;
struct kmer_t4 merged_kmer;
bool flip;
bucket_rm4 *nxt_bucket;
};
struct bucket_rm0
{
uint64_t *kmer_t;
uint64_t *merged_kmer;
bool flip;
bucket_rm0 *nxt_bucket;
};
//bucket in round 1
struct bucket_r1
{
struct kmer_t kmer_t;
bucket_r1 *nxt_bucket;
};
struct bucket2_r1
{
struct kmer_t2 kmer_t2;
struct kmer_info_r1 kmer_info;
bucket2_r1 *nxt_bucket;
};
struct bucket3_r1
{
struct kmer_t3 kmer_t3;
struct kmer_info_r1 kmer_info;
bucket3_r1 *nxt_bucket;
};
struct bucket4_r1
{
struct kmer_t4 kmer_t4;
struct kmer_info_r1 kmer_info;
bucket4_r1 *nxt_bucket;
};
struct bucket0_r1
{
uint64_t * kmer_t;
struct kmer_info_r1 kmer_info;
bucket0_r1 *nxt_bucket;
};
//hashtable structure
struct hashtable
{
struct bucket **store_pos;
size_t ht_sz;
int round;
};
struct hashtable2
{
struct bucket2 **store_pos;
size_t ht_sz;
};
struct hashtable3
{
struct bucket3 **store_pos;
size_t ht_sz;
};
struct hashtable4
{
struct bucket4 **store_pos;
size_t ht_sz;
};
struct hashtable0
{
struct bucket0 **store_pos;
size_t ht_sz;
};
//read structure
struct read_t
{
char tag[1000];
bool error_nt[1000];
char c_seq[10000];//char representation
//char *c_seq;//char representation
//uint64_t read_bits[10000];//bit representation
uint64_t *read_bits;
//char read[1000];//char representation
int readLen;// read length
int read_idx;
};
struct ref_read_t
{
char tag[1000];
uint64_t *read_bits;//bit representation
size_t read_idx;
int alloc_sz;
int readLen;// read length
int contig_no;
};
struct key_table
{
bool in_use;
list<uint64_t *> pblocks;
int BytesPerKey;
int KeysPerBlock;
int current_block;
int current_index;
};
struct read_index
{
vector< map<uint64_t,bool> > repeat_maps;
uint64_t repeat_cnt;
int MaxMatch;
vector<int> read_len_vt;
};
struct reads_table
{
bool in_use;
list<uint64_t *> pblocks;
int BytesPerBlock;
int current_block;
int current_byte;
int current_read;
map<int,uint64_t *> read_ptr;
vector<int> read_len_vt;
};
struct KmerInContig
{
uint32_t contig_no:31,flip:1;
int pos;
};
struct ContigInRead
{
int ctg_no;
int cov;
int coord2;
};
struct LongReadContigIndex
{
map<int, KmerInContig> LR2CTG;
map<int, vector<int> > CTG2LR, CTG2LR_2;
map<int, map<int, int> > CTG2Coords;
map<int, map<int, int> > CTG2Offsets;
//map<int, ContigInRead > CTG2LR, CTG2LR_2;
map<int, ContigInRead > layout;
int KmerCovTh;
int nMatches;
int BlockSize;
bool FastMap;
// vector<int> matching_positions_LR;
//vector<KmerInContig> matching_positions_ctg;
};
struct Coord_CTG_Cov
{
int coord;//observed coords
int contig_no;
int cov;
int coord2;//contig coord in a read
};
struct raw_overlap_str
{
int score;
int index;
string raw_seq;
};
struct read_match_info
{
uint32_t read_idx;
uint32_t score;
};
struct reads_info
{
vector< vector<Coord_CTG_Cov> > LongReadIndexVec, RefIndexVec, LongReadIndexVecCorr,ShortReadIndexVec;
vector<string> tag_vec, input_files;
vector< vector<int> > Contig2LongRead,Contig2Ref,Contig2ShortRead;
vector<int> LenVec, RefLenVec;
vector < vector<int>::iterator> LengthRank;
vector<string> selected_long_reads_seq;
vector<map<int, int > > LeftOverlaps, RightOverlaps;//record all left and right overlaps with scores
vector<int> LeftOverlapsBest, RightOverlapsBest,degree_vec;//record non-tied single best overlap
vector<map<int, int> > LeftBestOverlapsTemp, RightBestOverlapsTemp;//record overlaps with scores
vector< vector<int> > LeftOverlapsBestWithTies, RightOverlapsBestWithTies;//record best overlaps with ties
map<int, vector<int> > contig2contig_cov;
map<int, vector<int> > Consensus_info;
map<int, vector<read_match_info> > Consensus_info_full;
int ConsensusBestN;
map<int, map<int, int> > LeftNodes, RightNodes, LeftNodes_ShortReads, RightNodes_ShortReads, LeftNodes_LongReads, RightNodes_LongReads;
map< vector<int>, vector<raw_overlap_str> > Selected_Overlaps;
bool RemoveChimera,MSA,RecoverFalseNegatives, Clean,CloseGaps;
int MinLen;
int MinOverlap;
int K_size;
int KmerCovTh;
double Redundancy;
int TopOverlaps;
int64_t TotalReads;
int PathCovTh;
int MaxReadLen,MaxCompressedReadLen;
int max_reads;
double HitRate, AdaptiveTh;
int ContigCovTh,ChimeraTh;
vector<bool> contained, used, used_vt_left, used_vt_right,both_stand_used,chimeric,gap_closing;
int n_deleted_edges;
int mode;
bool inward;
bool outward;
int insert_size;
size_t numReads;
vector<vector<int> > aligned_nodes, gap_idx_vec;
bool Debug;
//bool Debug;
};
//contig graph
struct adjacent_contig_info
{
int32_t dist_sum;
int8_t cov;
string bridge;
};
struct contigs_info
{
int total_contigs;
int K_size;
vector<int> contig_sz_vt,kmer_cnt_vt,comp_vt;
vector<int> contigs_hp_b,contigs_hp_e;
vector<string> contigs_str,contig_tag;
map<int, vector<int> > Length_ID;
map<int, vector<int> > Cov_Length;
string ContigsFileName;
string ContigGraphName;
//map<int, vector<int> > ctg_in_scf;
//vector<vector<int> > scaffolds;
//vector<vector<int> > gaps_in_scaffolds;
vector < vector<double>::iterator> SortedAstats;
vector<double> AstatsVec;
vector < vector<int>::iterator> LengthRank;
vector<int> cov_vt;
int ContigCovTh;
int KmerCovTh;
bool CallConsensus;
vector<bool> used;
//vector<struct c_info> c_info_vt;
//vector< map<int,struct scaffold_contig_info> > scaffold_adjacency_left,scaffold_adjacency_right;
vector< map<int,struct adjacent_contig_info> > contig_adjacency_left,contig_adjacency_right;
};
//path information in the BFS bubble removal
struct BFS_path_info
{
int cov;
int depth;
int len;
bool BothEndsUsed;
struct bucket* last_bkt;
struct edge_node* last_bkt_edge;
};
struct BFS_path_info2
{
int cov;
int depth;
int len;
struct bucket2* last_bkt;
struct edge_node* last_bkt_edge;
};
struct BFS_path_info3
{
int cov;
int depth;
int len;
struct bucket3* last_bkt;
struct edge_node* last_bkt_edge;
};
struct BFS_path_info4
{
int cov;
int depth;
int len;
struct bucket4* last_bkt;
struct edge_node* last_bkt_edge;
};
struct BFS_path_info0
{
int cov;
int depth;
int len;
struct bucket0* last_bkt;
struct edge_node* last_bkt_edge;
};
//outdated
struct BFS_path_info_ctg
{
int cov;
int depth;
int len;
int last_ctg;
};
//a stack in the BFS
struct stacked_bucket
{
bucket *bktptr;
bool RightSearch;
bool BothEndsUsed;
};
struct stacked_bucket2
{
bucket2 *bktptr;
bool RightSearch;
};
struct stacked_bucket3
{
bucket3 *bktptr;
bool RightSearch;
};
struct stacked_bucket4
{
bucket4 *bktptr;
bool RightSearch;
};
struct stacked_bucket0
{
bucket0 *bktptr;
bool RightSearch;
};
bool it_cmp(const vector<int>::iterator &a, const vector<int>::iterator &b)
{
return (*a) > (*b);
}
bool get_a_fasta_read(ifstream & fasta_in, string &tag, string &str, string & n_tag)
{
ifstream tmp_ifstream;
string temp;
if(!getline(fasta_in,temp))
{return 0;}
if(temp[temp.size()-1]=='\n'||temp[temp.size()-1]=='\r')
{temp.resize(temp.size()-1);}
str.clear();
if(temp[0]=='>')
{tag=temp;}
else
{
tag=n_tag;
str=temp;
}
while(getline(fasta_in,temp)&&temp.size()>0)
{
if(temp[temp.size()-1]=='\n'||temp[temp.size()-1]=='\r')
{temp.resize(temp.size()-1);}
if((temp.size()>0&&(temp[0]=='>'||temp[0]=='\n'||temp[0]=='\r')))
{
n_tag=temp;
return 1;
}
else
{
str+=temp;
}
}
return 1;
}
bool get_a_fastq_read(ifstream & fastq_in, string &tag, string &seq, string & quality)
{
ifstream tmp_ifstream;
string temp;
if(!getline(fastq_in,temp))
{return 0;}
seq.clear();
if(temp[0]=='@')
{tag=temp;}
else
{
return 0;
}
getline(fastq_in,seq);//seq
if(seq[seq.size()-1]=='\n'||seq[seq.size()-1]=='\r')
{seq.resize(seq.size()-1);}
getline(fastq_in,temp);//'+'
getline(fastq_in,quality);
if(quality[quality.size()-1]=='\n'||quality[quality.size()-1]=='\r')
{quality.resize(quality.size()-1);}
return 1;
}
bool get_a_contig_path(ifstream & cp_in, string &tag, vector<struct Coord_CTG_Cov> &TempIndex, int &Len, int KmerCovTh, string & n_tag)
{
string tmp1, tmp2, tmp3, str;
int coord, contig, cov, coord2;
TempIndex.clear();
TempIndex.reserve(500);
Coord_CTG_Cov Coord_CTG_Cov;
string temp;
if (!getline(cp_in, temp))
{
return 0;
}
if (temp.size() == 0)
{
return 0;
}
if (temp[temp.size() - 1] == '\n' || temp[temp.size() - 1] == '\r')
{
temp.resize(temp.size() - 1);
}
str.clear();
if (temp[0] == '>' || temp[0] == '@')
{
tag = temp;
}
else
{
tag = n_tag;
str = temp;
stringstream ss(str);
tmp1.clear();
ss >> coord >> tmp1 >> contig >> tmp2 >> cov >> tmp3 >> coord2;
if (tmp1.size() == 0 || tmp1[0] != ',')
{
Len = coord;
}
else
{
if (cov > KmerCovTh)
{
Coord_CTG_Cov.coord = coord;
Coord_CTG_Cov.contig_no = contig;
Coord_CTG_Cov.cov = cov;
Coord_CTG_Cov.coord2 = coord2;
TempIndex.push_back(Coord_CTG_Cov);
}
}
}
while (getline(cp_in, temp))
{
if (temp.size() == 0)
{
break;
}
if (temp[temp.size() - 1] == '\n' || temp[temp.size() - 1] == '\r')
{
temp.resize(temp.size() - 1);
}
if ((temp.size()>0 && (temp[0] == '@'||temp[0] == '>' || temp[0] == '\n' || temp[0] == '\r')))
{
n_tag = temp;
return 1;
}
else
{
stringstream ss(temp);
tmp1.clear();
ss >> coord >> tmp1 >> contig >> tmp2 >> cov >> tmp3 >> coord2;
if (tmp1.size() == 0 || tmp1[0] != ',')
{
Len = coord;
continue;
}
if (cov > KmerCovTh)
{
Coord_CTG_Cov.coord = coord;
Coord_CTG_Cov.contig_no = contig;
Coord_CTG_Cov.cov = cov;
Coord_CTG_Cov.coord2 = coord2;
TempIndex.push_back(Coord_CTG_Cov);
}
}
}
return 1;
}
bool basic_quality_check(string &seq_s)
{
bool good_read=1;
int seq_sz=seq_s.size();
for(int i=0;i<seq_sz;++i)
{
if(seq_s[i]!='A'&&seq_s[i]!='C'&&seq_s[i]!='G'&&seq_s[i]!='T'&&seq_s[i]!='N')
{
good_read=0;
seq_s.clear();
return good_read;
}
}
int nN=seq_sz-1,isN=-1;
for(int i=0;i<seq_sz;++i)
{
if(seq_s[i]=='-'||seq_s[i]=='N')
{
if(i<=seq_sz/2)
{
isN=i;
continue;
}
else
{
nN=i-1;
break;
}
}
}
int s=0;
if((nN-isN)<=seq_sz/2)
{
good_read=0;
}
if(good_read==0)
{
seq_s.clear();
return good_read;
}
if(isN>=0)
{
for(int i=isN+1;i<=nN;++i)
{
seq_s[s]=seq_s[i];
s++;
}
seq_s[s]='\0';
seq_s.resize(s);
}
return good_read;
}
//left shift and right shift of shift_sz bits of the whole bit array, arr_sz is the array length
static inline void L_shift_NB(uint64_t * bitsarr, int shift_sz,int arr_sz)
{
uint64_t temp_arr[100];
/*
for (int i=0;i<arr_sz;++i)
{
temp_arr[i]=0;
}
memset(temp_arr,0,sizeof(uint64_t)*arr_sz);
*/
int jmp=shift_sz/64;
int offset=shift_sz%64;
for (int i=0;i<arr_sz;++i)
{
if(i+jmp+1<arr_sz)
{
uint64_t tt=0;
if(offset==0)
{
tt=0;
}
else
{
tt=(bitsarr[i+jmp+1]>>(64-offset));
}
temp_arr[i]=((bitsarr[i+jmp]<<offset)|tt);
}
if(i+jmp+1==arr_sz)
{temp_arr[i]=bitsarr[i+jmp]<<offset;}
if(i+jmp+1>arr_sz)
{temp_arr[i]=0;}
}
memcpy(bitsarr,temp_arr,sizeof(uint64_t)*arr_sz);
/*
for (int i=0;i<arr_sz;++i)
{
bitsarr[i]=temp_arr[i];
}
*/
}
static inline void R_shift_NB(uint64_t * bitsarr, int shift_sz,int arr_sz)
{
uint64_t temp_arr[100];
/*
for (int i=0;i<arr_sz;++i)
{
temp_arr[i]=0;
}
memset(temp_arr,0,sizeof(uint64_t)*arr_sz);
*/
int jmp=shift_sz/64;
int offset=shift_sz%64;
for (int i=arr_sz-1;i>=0;--i)
{
if(i-jmp>0)
{
if(offset>0)
{temp_arr[i]=(bitsarr[i-jmp]>>offset)|(bitsarr[i-jmp-1]<<(64-offset));}
else
{temp_arr[i]=bitsarr[i-jmp];}
}
if (i-jmp==0)
{
if(offset>0)
{temp_arr[i]=(bitsarr[i-jmp]>>offset);}
else
{temp_arr[i]=bitsarr[i-jmp];}
}
if (i-jmp<0)
{temp_arr[i]=0;}
}
memcpy(bitsarr,temp_arr,sizeof(uint64_t)*arr_sz);
/*
for (int i=0;i<arr_sz;++i)
{
bitsarr[i]=temp_arr[i];
}
*/
}
// get reverse complement of a k-mer.
static inline uint64_t get_rev_comp_seq(uint64_t seq, int seq_size)
{
seq =~seq;
seq = ((seq & 0x3333333333333333 )<< 2) | ((seq & 0xCCCCCCCCCCCCCCCC )>> 2);
seq = ((seq & 0x0F0F0F0F0F0F0F0F )<< 4) | ((seq & 0xF0F0F0F0F0F0F0F0 )>> 4);
seq = ((seq & 0x00FF00FF00FF00FF )<< 8) | ((seq & 0xFF00FF00FF00FF00 )>> 8);
seq = ((seq & 0x0000FFFF0000FFFF )<<16) | ((seq & 0xFFFF0000FFFF0000 )>>16);
seq = ((seq & 0x00000000FFFFFFFF )<<32) | ((seq & 0xFFFFFFFF00000000 )>>32);
return seq >> (64 - (seq_size*2));
}
static inline uint64_t* get_rev_comp_seq_arr(uint64_t *seq_arr, int seq_size,int arr_sz)
{
int tot_bits=arr_sz*64;
for(int i=0;i<arr_sz;++i)
{
seq_arr[i]=~seq_arr[i];
seq_arr[i] = ((seq_arr[i] & 0x3333333333333333 )<< 2) | ((seq_arr[i] & 0xCCCCCCCCCCCCCCCC )>> 2);
seq_arr[i] = ((seq_arr[i] & 0x0F0F0F0F0F0F0F0F )<< 4) | ((seq_arr[i] & 0xF0F0F0F0F0F0F0F0 )>> 4);
seq_arr[i] = ((seq_arr[i] & 0x00FF00FF00FF00FF )<< 8) | ((seq_arr[i] & 0xFF00FF00FF00FF00 )>> 8);
seq_arr[i] = ((seq_arr[i] & 0x0000FFFF0000FFFF )<<16) | ((seq_arr[i] & 0xFFFF0000FFFF0000 )>>16);
seq_arr[i] = ((seq_arr[i] & 0x00000000FFFFFFFF )<<32) | ((seq_arr[i] & 0xFFFFFFFF00000000 )>>32);
}
int j=0,k=arr_sz-1;
for (;j<k;++j,--k)
{
uint64_t temp;
temp=seq_arr[j];
seq_arr[j]=seq_arr[k];
seq_arr[k]=temp;
}
R_shift_NB(seq_arr,tot_bits-(seq_size*2),arr_sz);
return seq_arr;
//return seq >> (64 - (seq_size*2));
}
// get sub bit array from a bit array.
inline void get_sub_arr(uint64_t * bitsarr_in,int bitsarr_len,int begin_pos,int sub_sz,uint64_t * bitsarr_out)
{
if(bitsarr_len<sub_sz)
{cout<<"Error! Input kmer too short."<<bitsarr_len <<" "<<sub_sz<<endl;return;}
int arr_sz_in=bitsarr_len/32+1;
int rem=bitsarr_len%32;
if(rem==0)
{arr_sz_in--;}
int arr_sz_out=sub_sz/32+1;
if(sub_sz%32==0)
{arr_sz_out--;}
uint64_t temp_arr[10];
memset(temp_arr,0,sizeof(temp_arr));
memset(bitsarr_out,0,sizeof(uint64_t)*arr_sz_out);
int rem2=(32-rem+begin_pos)%32;
int block_beg=(32-rem+begin_pos)/32;
if(rem==0)
{block_beg--;}
int rem3=(32-rem+begin_pos+sub_sz)%32;
int block_end=(32-rem+begin_pos+sub_sz)/32;
if(rem3!=0)
{rem3=32-rem3;}
else
{
block_end--;
}
if(rem==0)
{block_end--;}
int orig_sz=(block_end-block_beg+1);
memcpy(temp_arr,&bitsarr_in[block_beg],orig_sz*sizeof(uint64_t));
L_shift_NB(temp_arr,rem2*2,orig_sz);
R_shift_NB(temp_arr,(rem2+rem3)%32*2,arr_sz_out);
memcpy(bitsarr_out,temp_arr,arr_sz_out*sizeof(uint64_t));
}
uint64_t* str2bitsarr(const char * c_str,int len, uint64_t* b_str,int arr_sz )
{
for (int k=0;k<arr_sz;++k)
{
b_str[k]=0;
}
int arr_sz_needed=len/32+1;
int rem=len%32;
if(rem==0)
{arr_sz_needed--;}
int beg_arr_idx=arr_sz-arr_sz_needed;
if(rem==0&&arr_sz_needed>0)
{rem=32;}
for (int k=0;k<len;k++)
{
if(rem==0)
{beg_arr_idx++;rem=32;}
switch(c_str[k])
{
case ('A'):case ('a'):case ('0'):
b_str[beg_arr_idx]<<=2;//L_shift_NB(b_str,2,arr_sz);
rem--;
//b_str<<=2;
break;