/
computeMeasure.cpp
executable file
·2382 lines (1920 loc) · 84.9 KB
/
computeMeasure.cpp
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
#include <getopt.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <ctype.h>
#include <iostream>
#include <fstream>
#include <cstring>
//#include<cmath>
#include <vector>
#include <sstream>
#include <string>
#include <stdlib.h>
#include <iomanip>
//strtol(s.c_str(),0,10);
using namespace std;
# include <stdio.h>
# include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include<algorithm>
#include <unordered_map>
//using namespace std::tr1;
//#include<ext/hash_map>
//using __gnu_cxx::hash_map;
using namespace std;
struct SCIENTIFIC_NUMBER
{
int factor;
double value;
};
int ZI = 4;
//int k = 3;
//int order = 1;
struct SPECIESINFO
{
std::string name;
std::string dir;
int order;
};
struct KMERINFO
{
//receptacle of kmer counts
unordered_map<unsigned long, unsigned long> HashTable;
unordered_map<unsigned long, unsigned long> HashTableK_1;
// kmer length = k - 2
unordered_map<unsigned long, unsigned long> HashTableK_2;
unordered_map<unsigned long, unsigned long> HashTableOrder;
// kmer length = order + 1
unordered_map<unsigned long, unsigned long> HashTableOrder_1;
//receptacle of Pw(probability of a kmer word)
unordered_map<unsigned long, SCIENTIFIC_NUMBER > HashPw;
unsigned long totalKmer;
unsigned long totalK_1;
unsigned long totalK_2;
unsigned long totalOrder;
unsigned long totalOrder_1;
vector<SCIENTIFIC_NUMBER> *probIIDPointer;
vector< vector<SCIENTIFIC_NUMBER> > *transMatrixPointer;
vector<SCIENTIFIC_NUMBER> *iniProbPointer;
};
//KMERINFO.p = new vector<vector> transMatrixNew(col, row)
//transMatrixNew = transmatrix
//*KMERINFO.p[colNum][rowNum]
//vector< vector<SCIENTIFIC_NUMBER> > transMatrix(transRowSize, vector<SCIENTIFIC_NUMBER>(transColSize));
//vector<SCIENTIFIC_NUMBER> iniProb;
// Scientific Number calculation funtion:
// TransToReal : trans a SCIENTIFIC NUMBER to a read number
double TransToReal(SCIENTIFIC_NUMBER dSci)
{
double dReal=0;
dReal = dSci.value * pow(10,dSci.factor);
return dReal;
}
// TransToScientific : trans a read number to a SCIENTIFIC NUMBER
SCIENTIFIC_NUMBER TransToScientific(double dReal)
{
SCIENTIFIC_NUMBER sciTemp;
int count;
if( dReal==0.0 )
{
sciTemp.factor=0;
sciTemp.value=0.0;
}
else if(dReal>10.0 || dReal<-10.0)
{
count=0;
while(dReal>10.0 || dReal<-10.0)
{
dReal /=10.0;
count++;
}
sciTemp.value=dReal;
sciTemp.factor=count;
}
else if( dReal<1.0 && dReal>-1.0)
{
count=0;
while( dReal<1.0 && dReal>-1.0 )
{
dReal *=10.0;
count--;
}
sciTemp.value=dReal;
sciTemp.factor=count;
}
else
{
sciTemp.value=dReal;
sciTemp.factor=0;
}
return sciTemp;
}
SCIENTIFIC_NUMBER SciNegative(SCIENTIFIC_NUMBER Sci)
{
SCIENTIFIC_NUMBER sciNeg;
sciNeg.value = - Sci.value; sciNeg.factor = Sci.factor;
return sciNeg;
}
SCIENTIFIC_NUMBER SciInverse(SCIENTIFIC_NUMBER Sci)
{
SCIENTIFIC_NUMBER sciInv;
sciInv.value = 1/Sci.value; sciInv.factor = -Sci.factor;
return sciInv;
}
// SciMultiple : Multiplication of two SCIENTIFIC NUMBERS
SCIENTIFIC_NUMBER SciMultiple(SCIENTIFIC_NUMBER left,SCIENTIFIC_NUMBER right)
{
// cout << "SciMultiple " << endl;
double dTemp;
SCIENTIFIC_NUMBER sciTemp;
int count;
if( left.value==0.0 || right.value==0.0 )
{
// cout << "Both 0 " << endl;
sciTemp.value=0.0;
sciTemp.factor=0;
return sciTemp;
}
// now both left and right element are nonzero
dTemp=left.value * right.value;
// cout << "left.value " << left.value << endl;
// cout << "right.value " << right.value << endl;
// cout << "dTemp " << dTemp << endl;
if( dTemp>10.0 || dTemp<-10.0 )
{
// cout << "10 < dTemp or dTemp < -10 " << endl;
count=0;
while(dTemp>10.0 || dTemp<-10.0 )
{
dTemp /=10.0;
count++;
}
sciTemp.value=dTemp;
sciTemp.factor=left.factor+right.factor+count;
}
else if( dTemp<1.0 && dTemp>-1.0)
{
// cout << "dTemp < 1 or dTemp > -1 " << dTemp << endl;
count=0;
while( dTemp<1.0 && dTemp>-1.0 )
{
dTemp *=10.0;
count--;
}
// cout << "count " << count << endl;
sciTemp.value=dTemp;
sciTemp.factor=left.factor+right.factor+count;
// cout << "sciTemp " << sciTemp.value << " " << sciTemp.factor << endl;
}
else
{
// cout << "dTemp normal " << dTemp << endl;
sciTemp.value=dTemp;
sciTemp.factor=left.factor+right.factor;
}
return sciTemp;
}
// SciMultiple : Multiplication between a SCIENTIFIC NUMBERS and a real number
SCIENTIFIC_NUMBER SciMultiple(SCIENTIFIC_NUMBER left,double right)
{
SCIENTIFIC_NUMBER sciTemp;
sciTemp=TransToScientific(right);
sciTemp=SciMultiple(left,sciTemp);
return sciTemp;
}
// SciAddition : addition of two SCIENTIFIC NUMBERS
SCIENTIFIC_NUMBER SciAddition(SCIENTIFIC_NUMBER left,SCIENTIFIC_NUMBER right)
{
double dTemp;
SCIENTIFIC_NUMBER sciTemp;
int i,count;
if( left.value==0.0 || right.value==0.0 )
{
// cout << "this step" << endl;
if( left.value==0.0 )
{
return right;
}else{
return left;
}
}
// now the two element are both non zero
if( left.factor>=right.factor)
{
// left element is larger than right element
dTemp=right.value;
for(i=0;i<(left.factor-right.factor);i++)
dTemp /=10.0;
dTemp +=left.value;
if( dTemp==0.0 )
{
sciTemp.factor=0;
sciTemp.value=0.0;
return sciTemp;
}
// now dTemp is not zero
if( dTemp>10.0 || dTemp <-10.0 )
{
count=0;
while(dTemp>10.0 || dTemp<-10.0 )
{
dTemp /=10.0;
count++;
}
sciTemp.value=dTemp;
sciTemp.factor=left.factor+count;
}
else if( dTemp<1.0 && dTemp>-1.0 )
{
count=0;
while(dTemp<1.0 && dTemp>-1.0)
{
dTemp *=10.0;
count--;
}
sciTemp.value=dTemp;
sciTemp.factor=left.factor+count;
}
else
{
sciTemp.value=dTemp;
sciTemp.factor=left.factor;
}
return sciTemp;
}
else
{
// right element is larger than left element
dTemp=left.value;
for(i=0;i<(right.factor-left.factor);i++)
dTemp /=10.0;
dTemp +=right.value;
if( dTemp==0.0 )
{
sciTemp.factor=0;
sciTemp.value=0.0;
return sciTemp;
}
// now dTemp is not zero
if( dTemp>10.0 || dTemp <-10.0 )
{
count=0;
while( dTemp>10.0 || dTemp <-10.0 )
{
dTemp /=10.0;
count++;
}
sciTemp.value=dTemp;
sciTemp.factor=right.factor+count;
}
else if( dTemp<1.0 && dTemp>-1.0 )
{
count=0;
while(dTemp<1.0 && dTemp>-1.0)
{
dTemp *=10.0;
count--;
}
sciTemp.value=dTemp;
sciTemp.factor=right.factor+count;
}
else
{
sciTemp.value=dTemp;
sciTemp.factor=right.factor;
}
return sciTemp;
}
}
// SciAddition : addition between a SCIENTIFIC NUMBERS and a real number
SCIENTIFIC_NUMBER SciAddition(SCIENTIFIC_NUMBER left,double right)
{
SCIENTIFIC_NUMBER sciTemp;
sciTemp=TransToScientific(right);
sciTemp=SciAddition(left,sciTemp);
return sciTemp;
}
// SciPow : give the power of a scientific number
SCIENTIFIC_NUMBER SciPow(SCIENTIFIC_NUMBER left,double right)
{
SCIENTIFIC_NUMBER sciTemp;
double dTemp;
int iTemp;
/*
if(left.value==0.0 )
{
printf("the base of the power is nagative\n");
exit(1);
}
*/
if(left.value==0.0 )
{
sciTemp.factor=0;
sciTemp.value=0.0;
return sciTemp;
}
dTemp=(log10(fabs(left.value))+left.factor)*right;
if( dTemp>0.0 )
iTemp=int(ceil(dTemp)); //ceil(a)是求不小于a的最小整数。floor(a)表示求不大于a的最大整数
else
iTemp=int(floor(dTemp));
sciTemp.factor=iTemp;
sciTemp.value=pow(10.0,dTemp-iTemp);
return sciTemp;
}
// SciPow : give the power of a scientific number
SCIENTIFIC_NUMBER SciLn(SCIENTIFIC_NUMBER sci)
{
SCIENTIFIC_NUMBER sciTemp = TransToScientific( log( sci.value ) + sci.factor * log(10) ) ;
return sciTemp;
}
void printFour(vector<int> four)
{
cout << "print ";
for(int it=0; it<four.size(); it++)
{
cout << four[it] << "," ;
}
cout << endl;
}
vector<int> ten2four(unsigned long ten, int k)
{
vector<int> four (k,0);
unsigned long tmp = ten;
int currentPos = k-1;
for(int currentPos = k-1; currentPos >=0; --currentPos)
{
four[currentPos]=tmp%ZI;
tmp/=ZI;
}
//while(tmp>=(ZI-1)) {four[currentPos]=tmp%ZI;tmp/=ZI;currentPos--; }
//four[currentPos] = tmp;
return four;
}
int four2ten(vector<int> four, int k)
{
unsigned long ten = 0;
for(int currentPos=(k-1); currentPos >= 0; --currentPos)
{
int tmp = four[currentPos] * pow(ZI,(k-1 - currentPos));
ten = ten + tmp;
//cout << currentPos << " " << ten << endl;
}
return ten;
}
void loadKmerCountHash(string currentKmerFilePathName, KMERINFO* speciesKmerInfo, string kmerUsage)
{
ifstream fin(currentKmerFilePathName.c_str());
string currentKmerLine;
while(getline(fin, currentKmerLine, '\n'))
{
unsigned long currentKmerID; unsigned long currentKmerCount;
std::istringstream ss(currentKmerLine);
std::string token;
int colCount = 0;
while(std::getline(ss, token, ',')) {
colCount++;
if(colCount == 1){
currentKmerID = atoi(token.c_str());
}else{
currentKmerCount = atoi(token.c_str());
}//std::cout << token << '\n';
}
//cout << "kmerID:" << currentKmerID << " kmerCount:" << currentKmerCount << endl;
if(kmerUsage == "kmerCount"){
speciesKmerInfo->HashTable[currentKmerID] = currentKmerCount;
speciesKmerInfo->totalKmer += currentKmerCount;
}else if(kmerUsage == "kmerCount_1"){
speciesKmerInfo->HashTableK_1[currentKmerID] = currentKmerCount;
speciesKmerInfo->totalK_1 += currentKmerCount;
}else if(kmerUsage == "kmerCount_2"){
speciesKmerInfo->HashTableK_2[currentKmerID] = currentKmerCount;
speciesKmerInfo->totalK_2 += currentKmerCount;
}else if(kmerUsage == "kmerOrder"){
speciesKmerInfo->HashTableOrder[currentKmerID] = currentKmerCount;
speciesKmerInfo->totalOrder += currentKmerCount;
}else if(kmerUsage == "kmerOrder+1"){
speciesKmerInfo->HashTableOrder_1[currentKmerID] = currentKmerCount;
speciesKmerInfo->totalOrder_1 += currentKmerCount;
}else{cout << "ERROR: wrong kmerUsage" << endl;}
}
return;
}
void loadSpeciesInfo(string speciesInfoFilePathName, vector<SPECIESINFO>& speciesInfoList)
{
ifstream fin(speciesInfoFilePathName.c_str());
string currentSpeciesLine;
while(getline(fin, currentSpeciesLine, '\n'))
{
string currentName; string currentDir; int currentOrder;
std::istringstream ss(currentSpeciesLine);
std::string token;
int colCount = 0;
while(std::getline(ss, token, ' ')) {
colCount++;
if(colCount == 1){
currentName = token;
}else if(colCount == 2){
currentDir = token;
if (currentDir[currentDir.length()-1] != '/')
{
currentDir = currentDir + "/";
}
}else{
currentOrder = atoi(token.c_str());
}//std::cout << token << '\n';
}
SPECIESINFO currentSpeciesInfo;
currentSpeciesInfo.name = currentName;
currentSpeciesInfo.dir = currentDir;
currentSpeciesInfo.order = currentOrder;
//cout << "speciesInfo.name:" << currentSpeciesInfo.name << ", dir:" << currentSpeciesInfo.dir << ", oroder:" << currentSpeciesInfo.order << endl;
speciesInfoList.push_back(currentSpeciesInfo);
}
return;
}
int loadTaxaInfo (string taxaFile, vector<string>& hostNCBIName, vector<string>& hostName, vector<string>& hostSuperkingdom, vector<string>& hostPhylum, vector<string>& hostClass, vector<string>& hostOrder, vector<string>& hostFamily, vector<string>& hostGenus, vector<string>& hostSpecies)
{
ifstream taxaFileIn(taxaFile.c_str());
string currentTaxaLine;
string token;
int lineNum = 0;
int taxaColCount = 0;
int errorTaxa = 0;
while( getline(taxaFileIn, currentTaxaLine) )
{
//cout << currentTaxaLine << endl;
lineNum++;
if( lineNum == 1 )
{
// first line is the header
std::istringstream ss(currentTaxaLine);
taxaColCount = 0;
while(std::getline(ss, token, '\t')) {
taxaColCount++;
//cout << taxaColCount << endl;
//cout << token << endl;
if(taxaColCount == 1){
if(token != "hostNCBIName"){
errorTaxa = 1;
}
}else if(taxaColCount == 2){
if(token != "hostName"){
errorTaxa = 1;
}
}else if(taxaColCount == 3){
if(token != "hostSuperkingdom"){
errorTaxa = 1;
}
}else if(taxaColCount == 4){
if(token != "hostPhylum"){
errorTaxa = 1;
}
}else if(taxaColCount == 5){
if(token != "hostClass"){
errorTaxa = 1;
}
}else if(taxaColCount == 6){
if(token != "hostOrder"){
errorTaxa = 1;
}
}else if(taxaColCount == 7){
if(token != "hostFamily"){
errorTaxa = 1;
}
}else if(taxaColCount == 8){
if(token != "hostGenus"){
errorTaxa = 1;
}
}else if(taxaColCount == 9){
if(token != "hostSpecies"){
errorTaxa = 1;
}
}
}
if(errorTaxa == 1)
{
cerr << "ERROR: the format of taxaFile is not correct!" << endl;
return 0;
}
}else{
std::istringstream ss(currentTaxaLine);
taxaColCount = 0;
while(std::getline(ss, token, '\t')) {
taxaColCount++;
//cout << taxaColCount << endl;
//cout << token << endl;
if(taxaColCount == 1){
hostNCBIName.push_back(token);
//cout << "check token " << hostNCBIName[0] << endl;
}else if(taxaColCount == 2){
hostName.push_back(token);
//cout << "check token " << token << endl;
}else if(taxaColCount == 3){
hostSuperkingdom.push_back(token);
//cout << "check token " << token << endl;
}else if(taxaColCount == 4){
hostPhylum.push_back(token);
}else if(taxaColCount == 5){
hostClass.push_back(token);
}else if(taxaColCount == 6){
hostOrder.push_back(token);
}else if(taxaColCount == 7){
hostFamily.push_back(token);
}else if(taxaColCount == 8){
hostGenus.push_back(token);
//cout << "check token " << token << endl;
}else if(taxaColCount == 9){
hostSpecies.push_back(token);
//cout << "check token " << token << endl;
}
}
}
}
return lineNum-1;
}
vector<int> reverseFour(vector<int> Four)
{
vector<int> reverseFour(Four.size(), 4);
for(int revPos = 0; revPos < Four.size(); revPos++)
{
reverseFour[revPos] = 3 - Four[Four.size()- 1 - revPos];
}
return reverseFour;
}
void pwIID(int ZI, int k, KMERINFO* speciesKmerInfo, vector<SCIENTIFIC_NUMBER>& probIID )
{
// compute pw for each kmer word
//unordered_map<unsigned long,SCIENTIFIC_NUMBER > probIID;
//cout << HashTableOrder_1[speciesID][0]/double(totalOrder_1[speciesID]) << endl;
for(int index = 0; index < ZI; index++)
{
//cout << HashTableOrder_1[speciesID][index] << endl;
//probIID[index] = TransToScientific(speciesKmerInfo->HashTableOrder_1[index]/double(speciesKmerInfo->totalOrder_1));
probIID.push_back( TransToScientific(speciesKmerInfo->HashTableOrder_1[index]/double(speciesKmerInfo->totalOrder_1)) );
}
for(unsigned long ten = 0; ten < pow(ZI, k); ten++ )
{
SCIENTIFIC_NUMBER pw;
pw.value = 1; pw.factor = 0;
//vector<int> four (k, 0);
vector<int> currentKmerFour = ten2four(ten, k);
for(int pos = 0; pos < k; pos ++)
{
pw = SciMultiple(pw, probIID[currentKmerFour[pos]]);
}
speciesKmerInfo->HashPw[ten] = pw;
}
}
void pwMC(int ZI, int k, int order, KMERINFO* speciesKmerInfo, vector<SCIENTIFIC_NUMBER>& iniProb, vector< vector<SCIENTIFIC_NUMBER> > &transMatrix)
{
// compute the transition probability matrix
int transColSize = ZI;
int transRowSize = pow(ZI, order);
//vector< vector<SCIENTIFIC_NUMBER> > transMatrix(transRowSize, vector<SCIENTIFIC_NUMBER>(transColSize));
//vector<SCIENTIFIC_NUMBER> iniProb;
for(int currentRow = 0; currentRow < transRowSize; currentRow++)
{
vector<int> currentRowFour = ten2four(currentRow, order);
unsigned long countBelow = speciesKmerInfo->HashTableOrder[currentRow];
double probBelow = countBelow/double(speciesKmerInfo->totalOrder);
SCIENTIFIC_NUMBER probBelowSci = TransToScientific(probBelow);
iniProb.push_back(probBelowSci);
// %% it could be 0 ! %%
// 20140910: the denominator cannot be 0
SCIENTIFIC_NUMBER inv_probBelowSci;
inv_probBelowSci.value = 0; inv_probBelowSci.factor = 0;
if(probBelowSci.value != 0)
{
inv_probBelowSci = SciInverse(probBelowSci);
}
SCIENTIFIC_NUMBER rowSum;
rowSum.value = 0; rowSum.factor = 0;
for(int currentCol = 0; currentCol < transColSize; currentCol++)
{
//cout << "row:" << currentRow << ", col:" << currentCol << endl;
vector<int> currentRowColFour(currentRowFour);
currentRowColFour.push_back(currentCol);
for(int it=0; it< currentRowColFour.size(); it++)
{
//cout << currentRowColFour[it];
}
//cout << endl;
int currentRowColTen = four2ten(currentRowColFour, (order+1));
unsigned long countAbove = speciesKmerInfo->HashTableOrder_1[currentRowColTen];
double probAbove = countAbove/double(speciesKmerInfo->totalOrder_1);
SCIENTIFIC_NUMBER probAboveSci = TransToScientific(probAbove);
if( probBelow != 0 )
{
transMatrix[currentRow][currentCol] = SciMultiple(probAboveSci,inv_probBelowSci);
}else{
transMatrix[currentRow][currentCol] = TransToScientific(0);
}
rowSum = SciAddition(rowSum, transMatrix[currentRow][currentCol]);
//cout << currentRow << ", " << currentCol << ", " << countAbove << ", " << countBelow << ", " << totalOrder_1[speciesID] << ", " << totalOrder[speciesID] << ", " << countAbove/double(countBelow) << ", " << probAbove << ", " << probBelow << ", " << TransToReal(transMatrix[currentRow][currentCol]) << endl;
//cout << totalKmer[speciesID] << ", " << totalOrder[speciesID] << ", "<< totalOrder_1[speciesID] << endl;
}
// normalize trans matrix
for(int currentCol = 0; currentCol < transColSize; currentCol++)
{
transMatrix[currentRow][currentCol] = SciMultiple(transMatrix[currentRow][currentCol], SciInverse(rowSum));
//cout << TransToReal(transMatrix[currentRow][currentCol]) << endl;
}
}
//vector<SCIENTIFIC_NUMBER> *iniProbPointer = &iniProb;
//cout << "iniProb[0]" << TransToReal(iniProb[0]) << endl;
//cout << TransToReal((*iniProbPointer)[0]) << endl;
//speciesKmerInfo->iniProbPointer = &iniProb;
//cout << TransToReal((*(speciesKmerInfo->iniProbPointer))[0]) << endl;
//speciesKmerInfo->transMatrixPointer = &transMatrix;
// compute pw for each kmer word
for(unsigned long currentKmerTen = 0; currentKmerTen < pow(ZI, k); currentKmerTen++ )
{
SCIENTIFIC_NUMBER pw;
//vector<int> four (k, 0);
vector<int> currentKmerFour = ten2four(currentKmerTen, k);
//cout << endl << endl;
//printFour(currentKmerFour);
vector<int> iniWordFour;
for(int iniWordPos = 0; iniWordPos < order; iniWordPos ++)
{
iniWordFour.push_back(currentKmerFour[iniWordPos]);
}
unsigned long iniWordTen = four2ten(iniWordFour, iniWordFour.size());
pw = iniProb[iniWordTen];
//cout << "wordten: " << currentKmerTen << endl << "wordfour: ";
//cout << "iniProb " << TransToReal(pw);
for(int currentTransToPos = order; currentTransToPos < k; currentTransToPos++)
{
vector<int> currentTransFromWordFour;
for(int transWordPos = currentTransToPos - order; transWordPos < currentTransToPos; transWordPos ++)
{
//cout << currentTransToPos << ", " << transWordPos << endl;
//cout << currentKmerFour[transWordPos] << ",";
currentTransFromWordFour.push_back(currentKmerFour[transWordPos]);
}
//printFour(currentTransFromWordFour);
unsigned long currentTransFromWordTen = four2ten(currentTransFromWordFour, currentTransFromWordFour.size());
pw = SciMultiple(transMatrix[currentTransFromWordTen][currentKmerFour[currentTransToPos]], pw);
//cout << " trans from ";
//printFour(currentTransFromWordFour);
//cout << "to " << currentKmerFour[currentTransToPos] << " with prob " << TransToReal(transMatrix[currentTransFromWordTen][currentKmerFour[currentTransToPos]]) << endl;
//cout << pw.value << ", " << pw.factor << endl;
}
speciesKmerInfo->HashPw[currentKmerTen] = pw;
//printFour(currentKmerFour);
//cout << " pw:" << TransToReal(pw) << endl;
}
}
vector<double> D2C2computeNGS(int ZI, int k, KMERINFO* speciesKmerInfoA, KMERINFO* speciesKmerInfoB)
{
// compute D2 statistics
SCIENTIFIC_NUMBER D2, D2star, D2shepp;
D2.value = 0; D2.factor = 0;
D2star.value = 0; D2star.factor = 0;
D2shepp.value = 0; D2shepp.factor = 0;
SCIENTIFIC_NUMBER C2_below[2];
C2_below[0].value = 0; C2_below[0].factor = 0;
C2_below[1].value = 0; C2_below[1].factor = 0;
SCIENTIFIC_NUMBER C2star_below[2];
C2star_below[0].value = 0; C2star_below[0].factor = 0;
C2star_below[1].value = 0; C2star_below[1].factor = 0;
SCIENTIFIC_NUMBER C2shepp_below[2];
C2shepp_below[0].value = 0; C2shepp_below[0].factor = 0;
C2shepp_below[1].value = 0; C2shepp_below[1].factor = 0;
//cout << TransToReal(D2) << endl;
for(unsigned long currentKmerTen = 0; currentKmerTen < pow(ZI, k); currentKmerTen++)
{
vector<int> currentKmerFour = ten2four(currentKmerTen, k);
vector<int> currentKmerRevFour = reverseFour(currentKmerFour);
unsigned long currentKmerRevTen = four2ten(currentKmerRevFour, currentKmerFour.size());
//cout << currentKmerRevTen << endl;
//printFour(currentKmerFour);
//printFour(currentKmerFour);
//printFour(currentKmerRevFour);
SCIENTIFIC_NUMBER p_w[2], EX_w[2], X_w[2], X_w_tilde[2], X_w_tilde_var[2];
for(int speciesID=0; speciesID<2; speciesID++)
{
KMERINFO* speciesKmerInfo;
if (speciesID == 0)
speciesKmerInfo = speciesKmerInfoA;
else
speciesKmerInfo = speciesKmerInfoB;
p_w[speciesID] = SciAddition(speciesKmerInfo->HashPw[currentKmerTen], speciesKmerInfo->HashPw[currentKmerRevTen]);
EX_w[speciesID] = SciMultiple(p_w[speciesID], speciesKmerInfo->totalKmer);
X_w[speciesID] = TransToScientific(speciesKmerInfo->HashTable[currentKmerTen] + speciesKmerInfo->HashTable[currentKmerRevTen]);
//cout << "Species" << speciesID << " Ten:" << currentKmerTen << " Four: " ;
//printFour(currentKmerFour);
//cout << "forward:" << HashTable[speciesID][currentKmerTen] << " reverse:" << HashTable[speciesID][currentKmerRevTen] << TransToReal(X_w[speciesID]) << endl;
X_w_tilde[speciesID] = SciAddition(X_w[speciesID], SciNegative(EX_w[speciesID]));
//cout << TransToReal(p_w[speciesID]) << ", " << TransToReal(EX_w[speciesID]) << ", " << TransToReal(X_w[speciesID]) << endl;
// prepare for C2 compute
C2_below[speciesID] = SciAddition(C2_below[speciesID], SciPow(X_w[speciesID], 2));
// prepare for C2star compute
// 20140910: the denominator cannot be 0
if(EX_w[speciesID].value != 0)
{
X_w_tilde_var[speciesID] = SciMultiple(X_w_tilde[speciesID], SciInverse(SciPow(EX_w[speciesID],0.5)));
C2star_below[speciesID] = SciAddition(C2star_below[speciesID], SciPow(X_w_tilde_var[speciesID], 2));
}
//cout << TransToReal(X_w[speciesID]) << endl;
}
//cout << "D2:" << D2.value << ", " << D2.factor << endl;
//if(D2.value == 0.0){cout << "D2.value = 0.0" << endl;}
D2 = SciAddition(SciMultiple(X_w[0], X_w[1]), D2);
//cout << "X_w[0]:" << TransToReal(X_w[1]) << " X_w[1]:" << TransToReal(X_w[1]) << " D2:" << TransToReal(D2) << endl;
SCIENTIFIC_NUMBER D2star_above = SciMultiple(X_w_tilde[0], X_w_tilde[1]);
SCIENTIFIC_NUMBER D2star_below = SciPow(SciMultiple(EX_w[0], EX_w[1]), 0.5);
if(D2star_below.value != 0)
{
D2star = SciAddition(D2star, SciMultiple(D2star_above, SciInverse(D2star_below)));
}
SCIENTIFIC_NUMBER D2shepp_below = SciPow(SciAddition(SciPow(X_w_tilde[0],2), SciPow(X_w_tilde[1],2)),0.5);
// 20140910: the denominator cannot be 0
if(D2shepp_below.value != 0)
{
D2shepp = SciAddition(D2shepp, SciMultiple(D2star_above, SciInverse(D2shepp_below)));
// prepare for c2shepp compute
C2shepp_below[0] = SciAddition(C2shepp_below[0], SciMultiple(SciPow(X_w_tilde[0],2), SciInverse(D2shepp_below)));
C2shepp_below[1] = SciAddition(C2shepp_below[1], SciMultiple(SciPow(X_w_tilde[1],2), SciInverse(D2shepp_below)));
}
}
SCIENTIFIC_NUMBER C2 = SciMultiple(D2, SciInverse(SciMultiple(SciPow(C2_below[0],0.5),SciPow(C2_below[1],0.5))));
SCIENTIFIC_NUMBER C2star = SciMultiple(D2star, SciInverse( SciMultiple(SciPow(C2star_below[0], 0.5), SciPow(C2star_below[1], 0.5)) ));
SCIENTIFIC_NUMBER C2shepp = SciMultiple(D2shepp, SciInverse( SciMultiple(SciPow(C2shepp_below[0],0.5),SciPow(C2shepp_below[1],0.5) )) );
//cout << "d2shepp_below " << TransToReal(SciMultiple(SciPow(C2shepp_below[0],0.5),SciPow(C2shepp_below[1],0.5) )) << endl;
vector<double> C2values;
C2values.push_back(0.5*(1-TransToReal(C2)));
C2values.push_back(0.5*(1-TransToReal(C2star)));
C2values.push_back(0.5*(1-TransToReal(C2shepp)));
//cout << D2C2values[0] << endl;
return C2values;
}
vector<double> EuMaChCombineDistNGS(int ZI, int k, KMERINFO* speciesKmerInfoA, KMERINFO* speciesKmerInfoB)
{
//cout << TransToReal(D2) << endl;
SCIENTIFIC_NUMBER EuDist ; EuDist.value=0; EuDist.factor=0;
SCIENTIFIC_NUMBER MaDist ; MaDist.value=0; MaDist.factor=0;
SCIENTIFIC_NUMBER ChDist ; ChDist.value=0; ChDist.factor=0;
SCIENTIFIC_NUMBER EuUDist ; EuUDist.value=0; EuUDist.factor=0;
SCIENTIFIC_NUMBER MaUDist ; MaUDist.value=0; MaUDist.factor=0;
SCIENTIFIC_NUMBER ChUDist ; ChUDist.value=0; ChUDist.factor=0;
int count=0;
SCIENTIFIC_NUMBER EuFDist ; EuFDist.value=0; EuFDist.factor=0;
for(unsigned long currentKmerTen = 0; currentKmerTen < pow(ZI, k); currentKmerTen++)
{
vector<int> currentKmerFour = ten2four(currentKmerTen, k);
vector<int> currentKmerRevFour = reverseFour(currentKmerFour);
unsigned long currentKmerRevTen = four2ten(currentKmerRevFour, currentKmerFour.size());
// frequency
SCIENTIFIC_NUMBER X1 = TransToScientific( ( speciesKmerInfoA->HashTable[currentKmerTen] + speciesKmerInfoA->HashTable[currentKmerRevTen] ) / double ( 2 * speciesKmerInfoA->totalKmer ) );
SCIENTIFIC_NUMBER X2 = TransToScientific( ( speciesKmerInfoB->HashTable[currentKmerTen] + speciesKmerInfoB->HashTable[currentKmerRevTen] ) / double ( 2 * speciesKmerInfoB->totalKmer ) );
SCIENTIFIC_NUMBER diff = SciAddition(X1, SciNegative(X2)) ;
diff.value = fabs(diff.value);
EuDist = SciAddition( EuDist, SciPow( diff , 2 ) );
MaDist = SciAddition( MaDist, diff ) ;
if( TransToReal(diff) > TransToReal(ChDist) )
{
ChDist = diff;
}
// EuF
SCIENTIFIC_NUMBER pw1 = SciAddition( speciesKmerInfoA->HashPw[currentKmerTen], speciesKmerInfoA->HashPw[currentKmerRevTen]);
SCIENTIFIC_NUMBER Fw1;
Fw1.value = 0; Fw1.factor = 0;
if (pw1.value != 0)
{
Fw1 = SciMultiple( X1, SciInverse( SciMultiple(pw1, 0.5)) );
}
SCIENTIFIC_NUMBER pw2 = SciAddition( speciesKmerInfoB->HashPw[currentKmerTen], speciesKmerInfoB->HashPw[currentKmerRevTen] );
SCIENTIFIC_NUMBER Fw2;
Fw2.value = 0; Fw2.factor = 0;
if (pw2.value != 0)
{
Fw2 = SciMultiple( X2, SciInverse( SciMultiple(pw2, 0.5) ));
}
SCIENTIFIC_NUMBER diffF = SciAddition(Fw1, SciNegative(Fw2)) ;
EuFDist = SciAddition( EuFDist, SciPow( diffF , 2 ) );
}
//cout << "count: " << count << endl;
vector<double> EuMaChCombineDistvalues;
EuMaChCombineDistvalues.push_back(TransToReal(SciPow(EuDist, 0.5)));
EuMaChCombineDistvalues.push_back(TransToReal(MaDist));
EuMaChCombineDistvalues.push_back(TransToReal(ChDist));