-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriorData.cpp
More file actions
1176 lines (929 loc) · 30.3 KB
/
PriorData.cpp
File metadata and controls
1176 lines (929 loc) · 30.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
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
//
// Created by Walfred (Wangfei) MA at the University of Southern California,
// Mark Chaisson Lab on 2/13/23.
//
// Licensed under the MIT License.
// If you use this code, please cite our work.
//
#include "PriorData.hpp"
using namespace std;
static inline uint int_deconpress(string int_conpress)
{
uint larger_kmer = 0;
for (char chr: int_conpress)
{
larger_kmer <<= 6;
larger_kmer += chr - '0';
}
return larger_kmer;
}
void inline strsplit(string& str, vector<uint16>& eles, char deli, string::size_type start = 0 )
{
size_t end = str.find(",");
size_t len = strlen(str.c_str());
while (end != std::string::npos && end < len)
{
eles.push_back(std::stoi(str.substr(start, end - start)));
start = end + 1;
end = str.find(",", start);
}
}
void inline strsplit(const string& str, vector<string>& eles, char deli)
{
string::size_type start = 0 , end = 0 ;
size_t len = strlen(str.c_str());
while (start < len)
{
end = str.find(deli, start);
if (end == std::string::npos) end = len ;
eles.push_back(str.substr(start, end - start));
start = end + 1;
}
}
inline void strsplit(const char* str, std::vector<std::string>& eles, char deli)
{
const char* start = str;
const char* end = str;
while (*end != '\0') {
if (*end == deli) {
eles.emplace_back(start, end - start); // create string from char* range
start = end + 1;
}
++end;
}
}
size_t PriorData::LoadIndex(const unordered_set<string>& geneset)
{
std::ifstream pathfile(datapath + ".index");
if(!pathfile)
{
std::cout<<"Error opening index file"<<std::endl;
return 0;
}
std::string line;
totalkmers = 0 ;
while (std::getline(pathfile, line))
{
string::size_type pos = line.find('\t');
string genename = line.substr(0, pos);
if (geneset.size() == 0 or geneset.find(genename) != geneset.end())
{
vector<string> eles;
strsplit(line, eles, '\t');
prefixes.push_back(eles[0]);
file_pos.push_back(make_pair(stol(eles[1]), stol(eles[2])));
kmervec_pos.push_back(make_pair(totalkmers , totalkmers + stol(eles[3])));
indexed_matrix_sizes.push_back(stol(eles[4]));
totalkmers += stol(eles[3]);
}
}
return file_pos.size();
}
size_t PriorData::LoadIndex()
{
std::ifstream pathfile(datapath + ".index");
if(!pathfile)
{
std::cout<<"Error opening index file"<<std::endl;
return 0;
}
std::string line;
totalkmers = 0 ;
while ( std::getline(pathfile, line) )
{
string::size_type pos = line.find('\t');
string genename = line.substr(0, pos);
vector<string> eles;
strsplit(line, eles, '\t');
prefixes.push_back(eles[0]);
file_pos.push_back(make_pair(stol(eles[1]), stol(eles[2])));
kmervec_pos.push_back(make_pair(totalkmers , totalkmers + stol(eles[3])));
indexed_matrix_sizes.push_back(stol(eles[4]));
totalkmers += stol(eles[3]);
}
return file_pos.size();
}
void PriorData::LoadHeader(PriorChunk &Chunk)
{
string StrLine;
StrLine.resize(MAX_LINE);
if (!file.nextLine(StrLine))
{
std::cerr << "ERROR: error in kmer matrix file "<<std::endl;
std::_Exit(EXIT_FAILURE);
return;
}
const size_t len = strlen(StrLine.c_str());
tuple<string,size_t,uint16> curr_info;
int startpos = 0;
char c;
for (; startpos < len ; ++startpos)
{
if (StrLine[startpos] == '\t') break;
}
Chunk.prefix = StrLine.substr(0, startpos);
auto &curr_genenum = Chunk.genenum;
auto &curr_kmernum = Chunk.kmervec_size;
curr_kmernum = 0;
for (startpos = startpos + 1; startpos < len ; ++startpos)
{
c = StrLine[startpos];
if (c == '\t' || c=='\0' || c =='\n') break;
curr_kmernum *= 10;
curr_kmernum += c - '0';
}
curr_genenum = 0;
for (startpos = startpos + 1; startpos < len ; ++startpos)
{
c = StrLine[startpos];
if (c == '\t') break;
curr_genenum *= 10;
curr_genenum += c - '0';
}
}
void PriorData::LoadSizes(PriorChunk &Chunk)
{
string StrLine;
StrLine.resize(MAX_LINE);
if (!file.nextLine(StrLine))
{
std::cerr << "ERROR: error in kmer matrix file "<<std::endl;
std::_Exit(EXIT_FAILURE);
return;
}
uint* &gene_kmercounts = Chunk.gene_kmercounts;
auto &curr_genenum = Chunk.genenum;
auto &allocsize = Chunk.gene_kmercounts_allocsize;
if (curr_genenum > allocsize || 2 * curr_genenum < allocsize)
{
gene_kmercounts = (uint *) realloc(gene_kmercounts, sizeof(uint) * curr_genenum );
allocsize = curr_genenum ;
}
const size_t len = strlen(StrLine.c_str());
int index =0;
int ele = 0;
char c;
for (int startpos = 1; startpos < len; ++startpos)
{
c = StrLine[startpos];
switch (c)
{
case ' ': case '\n':
gene_kmercounts[index++] = ele;
ele = 0;
break;
default:
ele *= 10;
ele += c - '0';
}
}
if (ele) gene_kmercounts[index++] = ele;
}
void PriorData::LoadAlleles(PriorChunk &Chunk)
{
const size_t& curr_genenum = Chunk.genenum;
vector<string>& genenames = Chunk.genenames;
vector<string>& pathnames = Chunk.pathnames;
if (curr_genenum > genenames.size() || 2 * curr_genenum < genenames.size())
{
genenames.resize(curr_genenum);
}
string StrLine;
StrLine.resize(MAX_LINE);
pathnames.clear();
pathnames.push_back("");
while (file.nextLine_start(StrLine, '+'))
{
string line = StrLine.substr(1, StrLine.find('\n') - 1);
size_t second_underscore = line.find('_', line.find('_') + 1);
if (second_underscore != std::string::npos) line = line.substr(second_underscore + 1);
size_t first_tab = line.find('\t');
size_t second_tab = line.find('\t', first_tab + 1);
if (first_tab != std::string::npos && second_tab != std::string::npos)
{
for (size_t i = first_tab + 1; i < second_tab; ++i)
{
if (line[i] == '-') line[i] = '_';
}
line[first_tab] = '_';
}
//pathnames.push_back(StrLine.substr(StrLine.find_last_of('\t') + 1, StrLine.find('\n') - StrLine.find_last_of('\t') - 1));
pathnames.push_back(line);
}
int phylo_treeindex = 0;
int index = 0;
for (int i =0 ; i< curr_genenum ; ++i)
{
if (!file.nextLine_start(StrLine, '>'))
{
std::cerr << "ERROR: error in kmer matrix file "<<std::endl;
std::_Exit(EXIT_FAILURE);
return;
}
for (; phylo_treeindex < Chunk.treealloc; ++phylo_treeindex)
{
if (Chunk.phylo_tree[phylo_treeindex].numchildren == 0) break;
}
Chunk.phylo_tree[phylo_treeindex++].numoffspring = (int) std::count_if( StrLine.begin(), StrLine.begin()+strlen(StrLine.c_str()), []( char c ){return c ==';';}) + 1;
genenames[index++] = StrLine.substr(1,MIN( StrLine.find('\n', 0)-1 , StrLine.find(';', 0) -1) );
}
}
void PriorData::LoadNorm(PriorChunk &Chunk)
{
/*
size_t& curr_genenum = Chunk.genenum;
string StrLine;
for ( int i = 0 ; i < curr_genenum ; ++i)
{
if (!file.nextLine_norm(StrLine))
{
std::cerr << "ERROR: error in kmer matrix file "<<std::endl;
std::_Exit(EXIT_FAILURE);
return;
}
}
return;
*/
size_t& curr_genenum = Chunk.genenum;
FLOAT_T*& prior_norm = Chunk.prior_norm;
size_t& prior_norm_allocsize = Chunk.prior_norm_allocsize;
if (curr_genenum *curr_genenum != prior_norm_allocsize )
{
assert(curr_genenum < MAX_UINT16);
try_allocate(prior_norm, curr_genenum *curr_genenum, curr_genenum *curr_genenum);
prior_norm_allocsize = curr_genenum * curr_genenum ;
}
float element = 0.0;
float decimal = 1.0;
bool ifdecimal = 0;
uint16 rowindex = 0, colindex = 0;
string StrLine;
for ( int i = 0 ; i < curr_genenum ; ++i)
{
if (!file.nextLine_norm(StrLine))
{
std::cerr << "ERROR: error in kmer matrix file "<<std::endl;
std::_Exit(EXIT_FAILURE);
return;
}
size_t len = strlen(StrLine.c_str());
int norm_c = 0;
char c;
for (int startpos = 1; startpos < len; ++startpos)
{
c = StrLine[startpos];
switch (c)
{
case '\t': case ' ': case '\n':
prior_norm[curr_genenum * rowindex + colindex ] = element;
prior_norm[curr_genenum * colindex + rowindex ] = element;
ifdecimal = 0;
element = 0.0;
decimal = 1.0;
norm_c++;
if (++colindex >= curr_genenum )
{
rowindex++;
colindex = rowindex;
}
break;
case '.':
ifdecimal = 1;
break;
default:
if (ifdecimal)
{
decimal *= 0.1;
element += (c - '0') * decimal;
}
else
{
element *= 10;
element += c - '0';
}
}
}
}
}
void PriorData::LoadGroups(PriorChunk &Chunk)
{
size_t& curr_genenum = Chunk.genenum;
string StrLine;
Chunk.genegroups.resize(curr_genenum, 0);
std::fill(Chunk.genegroups.begin(), Chunk.genegroups.end(), 0);
Chunk.groupkmernums.resize(curr_genenum, 0);
std::fill(Chunk.groupkmernums.begin(), Chunk.groupkmernums.end(), 0);
Chunk.numgroups = 0;
StrLine.resize(MAX_LINE);
for ( int i = 0 ; i < curr_genenum ; ++i)
{
if (!file.nextLine_genegroup(StrLine) )
{
break;
}
std::vector<std::string> fields;
strsplit(StrLine, fields, '\t');
auto num = std::stoi(fields[0].substr(1));
Chunk.groupkmernums[Chunk.numgroups] = num;
vector<uint16> eles;
strsplit(fields[1], eles, ',');
for (uint16 ele: eles)
{
Chunk.genegroups[ele] = Chunk.numgroups ;
}
Chunk.numgroups++;
}
}
void PriorData::LoadSmallGroups(PriorChunk &Chunk)
{
size_t& curr_genenum = Chunk.genenum;
string StrLine;
Chunk.smallgroups.resize(curr_genenum, 0);
std::fill(Chunk.smallgroups.begin(), Chunk.smallgroups.end(), 0);
Chunk.numsmallgroups = 0;
StrLine.resize(MAX_LINE);
for ( int i = 0 ; i < curr_genenum ; ++i)
{
if (!file.nextLine_smallgroup(StrLine) )
{
break;
}
Chunk.numsmallgroups ++ ;
vector<uint16> eles;
strsplit(StrLine, eles, ',', 1);
for (uint16 ele: eles)
{
Chunk.smallgroups[ele] = i ;
}
}
}
size_t PriorData::LoadRow(uint16* matrix, size_t rindex, string &StrLine, vector<uint> &pathsizes, vector<uint>& kmerhashs, size_t &kmerhashs_index)
{
if (!file.nextLine(StrLine))
{
std::cerr << "ERROR: error in kmer matrix file "<<std::endl;
std::_Exit(EXIT_FAILURE);
return NULL;
}
const size_t len = strlen(StrLine.c_str());
//size_t count = std::count_if( StrLine.begin(), StrLine.end(), []( char c ){return c ==',';}) + 3;
matrix[0] = StrLine[1];
uint rownum = FIXCOL;
uint16 element = 0;
char c;
size_t startpos = 3;
uint16 tag1=0, tag2 = 0;
for (; startpos < len ; ++startpos)
{
if (StrLine[startpos] == '|') break;
tag1 <<= 6;
tag1 += StrLine[startpos] - '0';
}
++startpos;
for (; startpos < len ; ++startpos)
{
if (StrLine[startpos] == '\t') break;
tag2 <<= 6;
tag2 += StrLine[startpos] - '0';
}
tag2 <<= 6;
++startpos;
matrix[5] = tag1 + tag2;
uint16 path=0;
for (; startpos < len ; ++startpos)
{
if (StrLine[startpos] == '\t') break;
path <<= 6;
path += StrLine[startpos] - '0';
}
++startpos;
matrix[2] = path;
if (pathsizes.size() <= path) pathsizes.resize(path + 1, 0);
uint loc=0;
for (; startpos < len ; ++startpos)
{
if (StrLine[startpos] == '\t') break;
loc <<= 6;
loc += StrLine[startpos] - '0';
}
++startpos;
pathsizes[path] = MAX(pathsizes[path], loc);
uint16 part2 = loc & 0xFFFF;
uint16 part1 = (loc >> 16) & 0xFFFF;
matrix[3] = part1;
matrix[4] = part2;
uint16 mean_repeat=0;
for (; startpos < len ; ++startpos)
{
if (StrLine[startpos] == '\t') break;
mean_repeat <<= 6;
mean_repeat += StrLine[startpos] - '0';
}
++startpos;
if (matrix[2] && (matrix[3] || matrix[4] >= 30 ))
{
mean_repeat = 255;
}
matrix[6] = mean_repeat;
uint16 totalcount = 0;
for (; startpos < len ; ++startpos)
{
if (StrLine[startpos] == '\t') break;
totalcount <<= 6;
totalcount += StrLine[startpos] - '0';
}
++startpos;
matrix[7] = totalcount;
uint16 posinum = 0;
for (; startpos < len ; ++startpos)
{
if (StrLine[startpos] == '\t') break;
posinum <<= 6;
posinum += StrLine[startpos] - '0';
}
++startpos;
matrix[8] = posinum;
uint16 totalnum = 0;
for (; startpos < len ; ++startpos)
{
if (StrLine[startpos] == '\t') break;
totalnum <<= 6;
totalnum += StrLine[startpos] - '0';
}
++startpos;
matrix[9] = totalnum;
ull larger_kmer = 0;
for (; startpos < len ; ++startpos)
{
if (StrLine[startpos] == '\t') break;
larger_kmer <<= 6;
larger_kmer += StrLine[startpos] - '0';
}
++startpos;
uint hash = kmerhashtable->findhash(larger_kmer);
kmerhashs[kmerhashs_index++] = hash;
bool ifdup = 0;
bool ifrange = 0;
uint16 firstele = 0, secondele = 0, lastele = 1;
for (; startpos < len; ++startpos)
{
c = StrLine[startpos];
switch (c)
{
case ',':
if (not ifdup && not ifrange)
{
matrix[rownum ++] = element;
}
else if (not ifdup)
{
secondele = element;
for (int x = firstele; x < firstele+secondele+1; ++x)
{
matrix[rownum ++] = x;
}
}
else if (not ifrange)
{
lastele = element;
for (int i = 0; i < lastele; ++i)
{
matrix[rownum ++] = secondele;
}
}
else
{
lastele = element;
for (int x = firstele; x < firstele+secondele+1; ++x)
{
for (int i = 0; i < lastele; ++i)
{
matrix[rownum ++] = x;
}
}
}
element = 0;
ifrange = 0;
ifdup = 0;
lastele = 1;
break;
case '~':
firstele = element;
secondele = 0;
ifrange = 1;
element = 0;
break;
case '*':
secondele = element;
element = 0;
ifdup = 1;
lastele = 0;
break;
default:
element <<= 6;
element += c - '0';
}
}
if (rownum >= MAX_UINT16) rownum = FIXCOL + matrix[9];
matrix[1] = (uint) rownum - FIXCOL;
return rownum;
}
void getPriorNorm(const uint numposi, const size_t colsize, FLOAT_T* prior_norm, uint16* temprow, uint16 matrixsize, FLOAT_T weight)
{
for (auto index = 0; index < numposi; ++index)
{
uint16 i = temprow[index];
prior_norm[matrixsize*i + i] += 0.5*weight ;
for (size_t jndex =index+1; jndex< numposi ; ++jndex)
{
uint16 j = temprow[jndex];
prior_norm[matrixsize*i + j] += weight;
}
}
for (auto index = numposi; index < colsize; ++index)
{
uint16 i = temprow[index];
prior_norm[matrixsize*i + i] += 0.5*weight ;
for (size_t jndex =index+1; jndex< colsize ; ++jndex)
{
uint16 j = temprow[jndex];
prior_norm[matrixsize*i + j] += weight;
}
}
for (auto index = 0; index < numposi; ++index)
{
uint16 i = temprow[index];
for (size_t jndex =numposi; jndex< colsize ; ++jndex)
{
uint16 j = temprow[jndex];
prior_norm[matrixsize* (MIN(i,j)) + (MAX(i,j)) ] -= weight;
}
}
}
void finishPriorNorm(FLOAT_T* norm_matrix, vector<FLOAT_T>& row_offsites, FLOAT_T matrix_offsite, const uint16 gnum)
{
for (int i = 0; i < gnum; ++i)
{
//norm_matrix[i*gnum+i] *= 2; //this is doubling diagonal mentioned above
//norm_matrix[i*gnum+i] += matrix_offsite;
for (int j = i + 1; j < gnum; ++j)
{
//norm_matrix[i*gnum+j] += row_offsites[i]; //offsite for ith sample
//norm_matrix[i*gnum+j] += row_offsites[j]; //offsite for jth sample
//norm_matrix[i*gnum+j] += matrix_offsite; //offsite for every cell
norm_matrix[j*gnum + i] = norm_matrix[i*gnum+j]; //square matrix is symmetric
}
}
}
void flatPriorNorm(const node* tree, const uint16 nodenum, FLOAT_T *norm_matrix, const uint16 matrix_size)
{
vector<uint16> repeat_keys(nodenum, 0 );
uint leaveindex = 0;
for (int i =0 ; i < nodenum; ++i)
{
auto& node = tree[i];
if (node.numchildren)
{
size_t index0 = node.children[0] - tree;
size_t index1 = node.children[1] - tree;
for (int j = 0; j < nodenum ; ++j)
{
norm_matrix[nodenum*index0+j] += norm_matrix[nodenum*i+j];
norm_matrix[nodenum*index1+j] += norm_matrix[nodenum*i+j];
}
}
else
{
repeat_keys[leaveindex++] = i;
}
}
for (int i =0 ; i < nodenum; ++i)
{
auto& node = tree[i];
if (node.numchildren)
{
size_t index0 = node.children[0] - tree;
size_t index1 = node.children[1] - tree;
for (int j = 0; j < nodenum ; ++j)
{
norm_matrix[index0+nodenum*j] += norm_matrix[i+nodenum*j];
norm_matrix[index1+nodenum*j] += norm_matrix[i+nodenum*j];
}
}
}
for (int i =0 ; i < leaveindex; ++i)
{
for (int j = 0 ; j < leaveindex ; ++j)
{
norm_matrix[matrix_size*i+j] = norm_matrix[nodenum*repeat_keys[i]+repeat_keys[j]];
}
}
}
vector<uint16> treedecode(node* tree, uint16 tree_size, uint16* matrix, uint16 matrix_posinum, uint16 matrix_totalnum)
{
vector<int> counts (tree_size,0);
for (int i = 0; i < matrix_posinum; ++i)
{
counts[matrix[i]] ++;
}
for (int i = matrix_posinum; i < matrix_totalnum; ++i)
{
counts[matrix[i]] --;
}
vector<uint16> outputs;
for (int i = 0; i < tree_size; ++i)
{
if (tree[i].numchildren)
{
auto child0index = tree[i].children[0] - tree;
auto child1index = tree[i].children[1] - tree;
counts[child0index] += counts[i];
counts[child1index] += counts[i];
}
else
{
for (int k = 0; k < counts[i]; ++k) outputs.push_back(tree[i].index);
}
}
return outputs;
}
void PriorData::LoadMatrix(PriorChunk &Chunk, size_t new_kmer_matrix_allocsize)
{
string StrLine;
StrLine.resize(MAX_LINE);
uint16*& kmer_matrix = Chunk.kmer_matrix;
const size_t kmernum = Chunk.kmervec_size;
size_t& kmer_matrix_size = Chunk.kmer_matrix_allocsize;
if (new_kmer_matrix_allocsize > kmer_matrix_size || 2*new_kmer_matrix_allocsize < kmer_matrix_size)
{
kmer_matrix = (uint16 *) realloc(kmer_matrix, sizeof(uint16) * new_kmer_matrix_allocsize + 10 );
kmer_matrix_size = new_kmer_matrix_allocsize;
}
Chunk.kmerhashs.resize(Chunk.kmervec_size+1, 0);
Chunk.pathsizes.resize(0);
const size_t curr_genenum = Chunk.nodenum;
FLOAT_T*& prior_norm = Chunk.prior_norm;
size_t& prior_norm_allocsize = Chunk.prior_norm_allocsize;
if (curr_genenum *curr_genenum > prior_norm_allocsize || 2*curr_genenum *curr_genenum < prior_norm_allocsize)
{
assert(curr_genenum < MAX_UINT16);
try_allocate(prior_norm, curr_genenum *curr_genenum, curr_genenum *curr_genenum);
prior_norm_allocsize = curr_genenum * curr_genenum ;
}
memset(prior_norm, 0, sizeof(FLOAT_T) * prior_norm_allocsize);
vector<FLOAT_T> vec_offsites(curr_genenum,0);
FLOAT_T offsite = 0;
vector<bool> ifingroup (Chunk.numgroups,0);
uint ifingroup_counter = 0;
uint16* matrix = kmer_matrix;
size_t rindex =0;
Chunk.kmervec_size = 0;
FLOAT_T weight = 0;
uint16* lastmatrix = matrix;
size_t lastrsize = 0;
char lastsign = '+';
uint16 lastposinum = 0;
uint16 lasttotalnum = 0;
uint16 lastcount = 0;
uint16 rsize;
for (rindex =0; rindex < kmernum; ++ rindex)
{
rsize = LoadRow(matrix , rindex, StrLine, Chunk.pathsizes, Chunk.kmerhashs, Chunk.kmervec_size);
if (StrLine[1] == '-' || StrLine[1] == '+')
{
if (lastcount == 1) weight *= 0.05;
getPriorNorm(lastposinum,lasttotalnum,prior_norm, lastmatrix, Chunk.nodenum, weight);
lastsign = StrLine[1];
lastmatrix = matrix + FIXCOL;
lastcount = matrix[7];
lastposinum = matrix[8];
lasttotalnum = matrix[9];
weight = 1;
}
else
{
weight += 1;
}
matrix = &matrix[rsize];
}
if (lastcount == 1) weight *= 0.05;
getPriorNorm(lastposinum,lasttotalnum,prior_norm, lastmatrix, Chunk.nodenum, weight);
finishPriorNorm(prior_norm, vec_offsites, offsite,Chunk.nodenum);
//flatPriorNorm(Chunk.phylo_tree, Chunk.nodenum, prior_norm, Chunk.genenum);
matrix[0] = '+';
matrix[1] = 0;
/*
size_t total2 = 0;
for (size_t rindex =0; rindex < kmernum; ++ rindex)
{
total2 += kmer_matrix[total2 + 1] + 2;
}
*/
}
void PriorData::LoadTree(PriorChunk &Chunk)
{
string StrLine;
StrLine.resize(MAX_LINE);
if (!file.nextLine(StrLine))
{
std::cerr << "ERROR: error in kmer matrix file "<<std::endl;
std::_Exit(EXIT_FAILURE);
return;
}
size_t len = strlen(StrLine.c_str());
if (len==0) return ;
static float pow10[7] = {1, 0.1, 0.01, 0.001, 0.0001, 0.00001, 0.000001};
size_t &count = Chunk.treealloc;
count = std::count_if( StrLine.begin(), StrLine.begin()+len, []( char c ){return c ==':';}) ;
size_t& phylo_tree_allocsize = Chunk.phylo_tree_allocsize;
node*& phylo_tree = Chunk.phylo_tree;
if (count > phylo_tree_allocsize || 2*count < phylo_tree_allocsize)
{
phylo_tree = (node*) realloc(phylo_tree, sizeof(node) * count);
phylo_tree_allocsize = count;
}
for (size_t index =0; index < count; ++index)
{
phylo_tree[index].clear();
}
node *current_node = &phylo_tree[0];
float current_num = 0.0;
uint16 current_index = 1;
float ifdeci = 0;
int notuselast = (StrLine[len-2] == ';') ;
float sign = 1;
bool ifsci_e = 0;
int sci_e = 0;
char c;
for (int pos=1; pos < len - notuselast; ++pos)
{
c = StrLine[pos];
switch(c)
{
case ' ': case '\n':
break;
case '(':
current_node = current_node->add(&phylo_tree[current_index++]);
break;
case ')': case ';':
if (sci_e>5) current_num = 0;
else if (sci_e > 0) current_num *= pow10[sci_e];
current_node->dist = sign * current_num ;
ifdeci = 0;
current_num = 0;
sign = 1;
sci_e = 0;
ifsci_e = 0;
current_node = current_node->parent;
break;
case ',':
if (sci_e>5) current_num = 0;
else if (sci_e > 0) current_num *= pow10[sci_e];
current_node->dist = sign * current_num ;
ifdeci = 0;
current_num = 0;
sign = 1;
sci_e = 0;
ifsci_e = 0;
current_node = current_node->parent->add(&phylo_tree[current_index++]);
break;
case ':':
ifdeci = 0;
ifsci_e = 0;
sci_e = 0;
break;
case '.':
ifdeci *= 0.1;