-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSPIn_main.cpp
More file actions
2484 lines (2143 loc) · 68.2 KB
/
SPIn_main.cpp
File metadata and controls
2484 lines (2143 loc) · 68.2 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
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <cmath>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
// Defining Variables ////////////////////////////////////////////////////////
typedef long long int LONG_INTEGER;
typedef int INTEGER;
//declaring the functions:
//void average(std::vector<std::vector<int> > &counts, std::vector<double> &results);
// Program options ///////////////////////////////////////////////////////////
enum ProgramOptions {
CREATE_FILE, LOAD_FILE, CALC_RULES_VALUES, SHOW_RESULTS, OPERATE, EXIT
};
// Global Variables //////////////////////////////////////////////////////////
#define MAX_CHAIN_LENGTH 100000
int MAX_NUM_SPECIES;
int SHIFT_CORRIMENT;
static int g_mask;
static int g_chainLength = -1;
static int g_numSpecies = -1;
static char* g_chainCharsSpeciesADN = 0; // Matrix dim: g_chainLength*g_numSpecies
int whichModel = 0;
double upp_bound_ident = 0;
static INTEGER g_valsSaved; // The chains that are not repeated...
static LONG_INTEGER *g_chainValsADN = 0; // Matrix dim: g_chainLength*NUM_ADN_OPTIONS
static INTEGER *g_numCountsData = 0; // Matrix dim: g_chainLength*NUM_ADN_OPTIONS
static std::string *g_nameSpecies = 0; // Vector dim: g_numSpecies
static double *g_average = 0; // Vector dim: g_chainLenght
double loglikSBD = 0.0;
// Info for the bases of the ADN /////////////////////////////////////////////
#define NUM_ADN_BASES 4
#define NUM_ADN_OPTIONS 4
enum ADNBases {
ADENINA, CITOSINA, TIMINA, GUANINA, NONE
};
static std::string changeADNBases[1][6] = { { "(at)(cg)", "(ac)(gt)",
"(ag)(ct)", "(ag)", "(ac)", "(at)" } };
//*********************************************************AIC/BIC for the SBD and ATR
double computeAICSBDATR( std::vector<LONG_INTEGER> &counts, std::vector<double> &mle, double K, double n, double &bic) {
double aic = 0.0;
double loglik = 0.0;
for (int i = 0; i < counts.size(); i++) {
if (mle[i]>0)
loglik += counts[i] * log(mle[i]);
}
K = K - 1;
aic = -2 * loglik + 2 * K + (2 * K * (K + 1)) / (n - K - 1);
bic = -2 * loglik + log(n) * K ;
return aic;
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% GENERAL function
void get_average(std::vector<std::vector<int> > &counts,
std::vector<double> &results, std::vector<double> &orbitSum) {
if (results.size() != 0) {
results.clear();
}
if (orbitSum.size() != 0) {
orbitSum.clear();
}
for (int i = 0; i < counts.size(); i++) {
double accum = 0.0;
for (int j = 0; j < counts[i].size(); j++) {
accum += counts[i][j];
}
orbitSum.push_back(accum);
accum /= counts[i].size();
results.push_back(accum);
}
}
double computeAIC(std::vector<std::vector<int> > &counts, double K, double n, double &bic) {
double aic = 0.0;
std::vector<double> averageVec; // vector of averages
std::vector<double> cumSumVec; // vector of cumm sums
get_average(counts, averageVec, cumSumVec);
double loglik = 0.0;
for (int i = 0; i < counts.size(); i++) {
loglik += cumSumVec[i] * log((double)averageVec[i]/(double)g_chainLength);
}
K = K - 1;
aic = -2 * loglik + 2 * K ;//+ (2 * K * (K + 1)) / (n - K - 1);
bic = -2 * loglik + log(n) * K ;
return aic;
}
int get_upp_bound( double numSpecies, int whichModel, int d)
{
if(whichModel == 0)
{
upp_bound_ident = 0.5*pow( 4.0 , numSpecies);
}
else if (whichModel == 1)
{upp_bound_ident = pow( 4.0 , numSpecies-1);
}
else if (whichModel == 2)
{upp_bound_ident = pow( 2.0 , 2*numSpecies-3) + pow( 2.0 , numSpecies-2);
}
else if (whichModel == 3)
{upp_bound_ident = ((1.0 / 3.0) * pow(2.0, 2 * numSpecies - 3) + pow(
2.0, numSpecies - 2) + 1.0 / 3.0);
}
int upp_bound=(int)((upp_bound_ident+1)/(d+1))-1;
return upp_bound;
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// Return the max number of species the system can work with
int MaxNumSpecies() {
int numBits = sizeof(LONG_INTEGER) * 8;
int val = -1;
if (NUM_ADN_BASES + 1 <= 0) {
std::cout << "Num of ADN Bases cannot be negative or zero."
<< std::endl;
} else if (NUM_ADN_BASES + 1 <= 2) {
val = numBits;
} else if (NUM_ADN_BASES + 1 <= 4) {
val = numBits / 2;
} else if (NUM_ADN_BASES + 1 <= 8) {
val = numBits / 3;
} else if (NUM_ADN_BASES + 1 <= 16) {
val = numBits / 4;
} else {
std::cout
<< "Num of ADN Bases superior to 16. Contact the program administrator."
<< std::endl;
}
return val;
}
// Get SHIFT corriment info for bit manipulation
int GetShiftCorriment() {
int numBits = sizeof(LONG_INTEGER) * 8;
int val = -1;
if (NUM_ADN_BASES + 1 <= 0) {
std::cout << "Num of DNA Bases cannot be negative or zero."
<< std::endl;
} else if (NUM_ADN_BASES + 1 <= 2) {
val = 1;
} else if (NUM_ADN_BASES + 1 <= 4) {
val = 2;
} else if (NUM_ADN_BASES + 1 <= 8) {
val = 3;
} else if (NUM_ADN_BASES + 1 <= 16) {
val = 4;
} else {
std::cout
<< "Num of DNA Bases superior to 16. Contact the program administrator."
<< std::endl;
}
return val;
}
void SetGlobalMask() {
if (NUM_ADN_BASES + 1 <= 0) {
std::cout << "Num of DNA Bases cannot be negative or zero." << std::endl;
} else if (NUM_ADN_BASES + 1 <= 2) {
g_mask = 1;
} else if (NUM_ADN_BASES + 1 <= 4) {
g_mask = 3;
} else if (NUM_ADN_BASES + 1 <= 8) {
g_mask = 7;
} else if (NUM_ADN_BASES + 1 <= 16) {
g_mask = 15;
} else {
//std::cout << "Num of DNA Bases superior to 16. Contact the program administrator." << std::endl;
}
}
inline char ADNBaseValToChar(int adnVal) {
if (adnVal == 0)
return 'a';
else if (adnVal == 1)
return 'c';
else if (adnVal == 2)
return 'g';
else if (adnVal == 3)
return 't';
// Error
//std::cout << "Error converting DNABase in ADNBaseValToChar." << std::endl;
return 0;
}
inline int ADNBaseCharToVal(int adnChar) {
if (adnChar == 'a')
return 0;
else if (adnChar == 'c')
return 1;
else if (adnChar == 'g')
return 2;
else if (adnChar == 't')
return 3;
// Error
// std::cout << "Error converting DNABase in ADNBaseCharToVal." << std::endl;
return -1;
}
LONG_INTEGER change_adn_bases_simple(LONG_INTEGER value, int col, int fil) {
std::string change = changeADNBases[fil][col];
LONG_INTEGER val1 = ADNBaseCharToVal(change[1]);
LONG_INTEGER val2 = ADNBaseCharToVal(change[2]);
LONG_INTEGER new_value = 0;
for (int i = 0; i < g_numSpecies; i++) {
LONG_INTEGER val = value >> i * SHIFT_CORRIMENT & g_mask;
if (val == val1) {
new_value |= (val2 << i * SHIFT_CORRIMENT);
} else if (val == val2) {
new_value |= (val1 << i * SHIFT_CORRIMENT);
} else {
new_value |= (val << i * SHIFT_CORRIMENT);
}
}
return new_value;
}
LONG_INTEGER change_adn_bases(LONG_INTEGER value, int col, int fil) {
std::string change = changeADNBases[fil][col];
LONG_INTEGER val1 = ADNBaseCharToVal(change[1]);
LONG_INTEGER val2 = ADNBaseCharToVal(change[2]);
LONG_INTEGER val3 = ADNBaseCharToVal(change[5]);
LONG_INTEGER val4 = ADNBaseCharToVal(change[6]);
LONG_INTEGER new_value = 0;
for (int i = 0; i < g_numSpecies; i++) {
LONG_INTEGER val = value >> i * SHIFT_CORRIMENT & g_mask;
if (val == val1) {
new_value |= (val2 << i * SHIFT_CORRIMENT);
} else if (val == val2) {
new_value |= (val1 << i * SHIFT_CORRIMENT);
} else if (val == val3) {
new_value |= (val4 << i * SHIFT_CORRIMENT);
} else if (val == val4) {
new_value |= (val3 << i * SHIFT_CORRIMENT);
}
}
return new_value;
}
std::string transform_adn_chain_val_to_string(LONG_INTEGER val) {
std::string adnChain;
for (int i = g_numSpecies - 1; i >= 0; i--) {
LONG_INTEGER value = val >> i * SHIFT_CORRIMENT & g_mask;
adnChain.push_back(ADNBaseValToChar(value));
}
return adnChain;
}
// Free System Memory
void FreeMemory() {
if (g_average) {
delete[] g_average;
g_average = 0;
}
if (g_chainCharsSpeciesADN) {
delete[] g_chainCharsSpeciesADN;
g_chainCharsSpeciesADN = 0;
}
if (g_chainValsADN) {
delete[] g_chainValsADN;
g_chainValsADN = 0;
}
if (g_numCountsData) {
delete[] g_numCountsData;
g_numCountsData = 0;
}
if (g_nameSpecies) {
delete[] g_nameSpecies;
g_nameSpecies = 0;
}
}
// Reserve Memory
void ReserveMemory(int numSpecies, int chainLength) {
g_chainCharsSpeciesADN = new char[numSpecies * chainLength];
g_chainValsADN = new LONG_INTEGER[chainLength * NUM_ADN_OPTIONS];
g_numCountsData = new INTEGER[chainLength * NUM_ADN_OPTIONS];
g_nameSpecies = new std::string[numSpecies];
}
// Crate a random file
void create_file(std::string &fileName, int numSpecies, int chainLength) {
std::ofstream myfile;
// Assert the demanding is possible
if (chainLength > MAX_CHAIN_LENGTH) {
std::cout << "DNA chain too long." << std::endl;
return;
}
if (numSpecies > MAX_NUM_SPECIES) {
std::cout << "Number of species cannot be superior to "
<< MAX_NUM_SPECIES << "." << std::endl;
return;
}
// Assert Name is correct
int pos;
if ((pos = fileName.find(".dat")) != -1)
fileName.replace(pos, 4, ".fa");
if (fileName.find(".fa") == -1)
fileName.append(".fa");
// Save random Information
myfile.open(fileName.c_str());
myfile << numSpecies << " " << chainLength << std::endl;
for (int i = 0; i < numSpecies; i++) {
myfile << "Taxon" << i << std::endl;
for (int j = 0; j < chainLength; j++) {
myfile << ADNBaseValToChar(rand() % NUM_ADN_BASES) << std::endl;
}
}
myfile.close();
}
// Load a random file
void load_file(std::string &fileName) {
std::ifstream myfile;
myfile.open(fileName.c_str(), ios::in);
INTEGER numSpecies;
INTEGER chainLength;
bool read = true;
INTEGER min_seq_length = 0;
std::string data;
if (!myfile) {
std::cout << "Unable to open file datafile.txt "<< std::endl;
exit(1); // call system to stop
}
else
{
FreeMemory();
// first pass to count the number of species and the length of the chainLength
// Load Info
numSpecies = 0;
INTEGER seqLengthRead = 0; // we have to get the minimum length of the seq
min_seq_length = 0;
while (read) {
myfile >> data;
if (data.size() > 0) {
if (data[0] != '>' && numSpecies == 0)
{
std::cout << "Invalid fasta format: first line should start with \">\" " << std::endl;
read = false;
exit(-1);
}
if (data[0] == '>') {
numSpecies++;
if (seqLengthRead < min_seq_length )
{
min_seq_length=seqLengthRead;
}
seqLengthRead=0;
// std::cout << "data " << data << " min "<< min_seq_length << "actual length " << seqLengthRead << std::endl;
} else if (myfile.eof()) {
read = false;
} else {
for (unsigned int i = 0; i < data.size(); i++) {
seqLengthRead++;
if (seqLengthRead == 1000)
int s = 0;
}
}
if ( numSpecies == 1)
{
min_seq_length=seqLengthRead;
}
}
}
//myfile.close();
}
chainLength = min_seq_length;
g_chainLength = min_seq_length;
std::cout << "Mulitple sequence alignment of length " << chainLength << "bp on "<< numSpecies << " taxa." << std::endl;
////////////////////////////////////////////////// end first pass
myfile.clear(); // forget we hit the end of file
myfile.seekg(0, ios::beg);
//myfile.seekg (0, ios_base::beg);
//rewind (myFile);
int trunc_num = 0;
if (!myfile) {
cerr << "Unable to open file datafile.txt " << std::endl;
exit(1); // call system to stop
}
else
{
// Reserve Memory
g_numSpecies = numSpecies;
g_chainLength = chainLength;
ReserveMemory(numSpecies, chainLength);
// Load Info
//std::string data;
read = true;
INTEGER specie = -1;
INTEGER valSavedForSpecie = 0;
while (read) {
myfile >> data;
if (data.size() > 0) {
// std::cout << "in " << data.size()<< " 1st " << data<< " line number " << line_num <<std::endl;
if (data[0] == '>') {
specie++;
trunc_num = 0;
valSavedForSpecie = 0;
g_nameSpecies[specie] = data;
std::cout << std::endl << "Reading species " << specie +1 << " " << g_nameSpecies[specie] << std::endl;
} else if (myfile.eof()) {
read = false;
} else {
for (unsigned int i = 0; i < data.size(); i++) {
if (valSavedForSpecie < g_chainLength) {
// std::cout << "len " <<g_chainLength << " we are at "<< valSavedForSpecie << std::endl;
g_chainCharsSpeciesADN[specie * g_chainLength
+ valSavedForSpecie] = tolower(data[i]);
valSavedForSpecie++;
if (valSavedForSpecie == 1000)
int s = 0;
} else {
trunc_num++;
if (trunc_num == 1)
{
std::cout << " sequence truncated for " << data[i] ;
}
else
{
std::cout << ", " << data[i] ;
}
}
}
}
}
}
memset(g_numCountsData, 0, NUM_ADN_OPTIONS * g_chainLength
* sizeof(INTEGER));
memset(g_chainValsADN, 0, NUM_ADN_OPTIONS * g_chainLength
* sizeof(LONG_INTEGER));
g_valsSaved = 0;
myfile.close();
}
}
// Save Information
void SaveValue(LONG_INTEGER value, INTEGER &valsSaved) {
bool valFound = false;
int valPosCol = -1;
int valPosFil = -1;
for (int i = 0; i < valsSaved && !valFound; i++) {
for (int j = 0; j < NUM_ADN_OPTIONS && !valFound; j++) {
if (g_chainValsADN[j * g_chainLength + i] == value) {
valPosFil = j;
valPosCol = i;
valFound = true;
}
}
}
if (valFound) {
g_numCountsData[valPosFil * g_chainLength + valPosCol] += 1;
#ifdef SHOW_ALG_INFO
std::cout << "-Val Found----------------------------" << std::endl;
std::cout << transform_adn_chain_val_to_string(g_chainValsADN[valPosFil*g_chainLength + valPosCol]) << std::endl;
#endif
} else {
#ifdef SHOW_ALG_INFO
std::cout << "---------------------------------------" << std::endl;
#endif
g_numCountsData[valsSaved] += 1;
g_chainValsADN[valsSaved] = value;
#ifdef SHOW_ALG_INFO
std::cout << transform_adn_chain_val_to_string(value) << std::endl;
#endif
for (int j = 1; j < NUM_ADN_OPTIONS; j++) {
g_chainValsADN[j * g_chainLength + valsSaved] = change_adn_bases(
value, j - 1, 0);
#ifdef SHOW_ALG_INFO
std::cout << transform_adn_chain_val_to_string(change_adn_bases(value, j-1, 0)) << std::endl;
#endif
}
valsSaved++;
}
}
// Calculate the system probabilities
void calc_rules_and_values() {
if (!g_chainCharsSpeciesADN) {
std::cout << "Error: No file Loaded." << std::endl;
return;
}
INTEGER valsSaved = 0;
for (int i = 0; i < g_chainLength; i++) {
LONG_INTEGER valChunk = 0;
for (int j = 0; j < g_numSpecies; j++) {
int val = ADNBaseCharToVal(g_chainCharsSpeciesADN[j * g_chainLength
+ i]);
valChunk = (valChunk << SHIFT_CORRIMENT) | val;
}
// Save the val
SaveValue(valChunk, valsSaved);
}
g_valsSaved = valsSaved;
}
void show_results() {
if (!g_chainCharsSpeciesADN) {
std::cout << "Error: No file Loaded." << std::endl;
return;
}
if (g_valsSaved == 0) {
std::cout << "No computation done." << std::endl;
}
int option = -1;
std::cout << " - (0) Show To Screen." << std::endl;
std::cout << " - (1) Save To File." << std::endl;
std::cin >> option;
if (option == 0) {
std::cout << "Total Options Found: " << g_valsSaved << std::endl;
double expected = pow(4.0, double(g_numSpecies));
std::cout << "Total Expected: " << expected << std::endl;
for (int i = 0; i < g_valsSaved; i++) {
std::cout
<< "------------------------------------------------------------------"
<< std::endl;
for (int j = 0; j < NUM_ADN_OPTIONS; j++) {
std::cout << "P(" << transform_adn_chain_val_to_string(
g_chainValsADN[j * g_chainLength + i]) << ") = "
<< g_numCountsData[j * g_chainLength + i] << std::endl;
}
}
} else if (option == 1) {
std::string fileName;
std::cout << "Name of the file to save: ";
std::cin >> fileName;
int pos;
if ((pos = fileName.find(".dat")) != -1)
fileName.replace(pos, 4, ".txt");
if (fileName.find(".txt") == -1)
fileName.append(".txt");
// Save
std::ofstream myfile;
myfile.open(fileName.c_str());
myfile << "Observed number of patterns: " << g_valsSaved << std::endl;
double expected = 1;
expected = pow( 4.0, double(g_numSpecies-1));
myfile << "Expected number of patterns: " << expected << std::endl;
for (int i = 0; i < g_valsSaved; i++) {
myfile
<< "------------------------------------------------------------------"
<< std::endl;
for (int j = 0; j < NUM_ADN_OPTIONS; j++) {
myfile << "P(" << transform_adn_chain_val_to_string(
g_chainValsADN[j * g_chainLength + i]) << ") = "
<< g_numCountsData[j * g_chainLength + i] << std::endl;
}
}
myfile.close();
}
}
void average() {
if (g_average != 0) {
delete[] g_average;
g_average = 0;
}
g_average = new double[g_valsSaved];
for (int i = 0; i < g_valsSaved; i++) {
double accum = 0.0;
for (int j = 0; j < NUM_ADN_OPTIONS; j++) {
accum += g_numCountsData[j * g_chainLength + i];
}
accum /= NUM_ADN_OPTIONS;
g_average[i] = accum;
}
for (int i = 0; i < g_valsSaved; i++) {
std::cout << "Average Orbital " << i << ": " << g_average[i]
<< std::endl;
}
}
//Models
class ModelSMM;
class ModelK81;
class ModelK80;
class ModelJC;
class ModelSBD;
class ModelATR;
class ModelGMM {
public:
ModelGMM() :
_calculated(false) {
if (!g_chainCharsSpeciesADN) {
std::cout << "No File Loaded.\n" << std::endl;
return;
}
_numSpecies = g_numSpecies;
_adnChainLength = g_chainLength;
CalcRulesAndValues();
}
void ShowResults() {
if (!g_chainCharsSpeciesADN) {
std::cout << "Error: No file Loaded." << std::endl;
return;
}
if (!_calculated) {
std::cout << "No computation done." << std::endl;
}
int option = 0;
// std::cout << " - (0) Show To Screen." << std::endl;
// std::cout << " - (1) Save To File." << std::endl;
//std::cin >> option;
if (option == 0) {
std::cout << "Observed number of orbits under the GMM: " << _orbitals.size()
<< std::endl;
double expected = pow( 4, double(g_numSpecies));
std::cout << "Expected number of orbits under the GMM: " << expected << std::endl;
for (int i = 0; i < _orbitals.size(); i++) {
std::cout
<< "------------------------------------------------------------------"
<< std::endl;
std::cout << "P(" << transform_adn_chain_val_to_string(
_orbitals[i]) << ") = " << _counts[i] << std::endl;
}
} else if (option == 1) {
std::string fileName;
std::cout << "Name of the file to save: ";
std::cin >> fileName;
int pos;
if ((pos = fileName.find(".dat")) != -1)
fileName.replace(pos, 4, ".txt");
if (fileName.find(".txt") == -1)
fileName.append(".txt");
// Save
std::ofstream myfile;
myfile.open(fileName.c_str());
myfile << "Observed number of orbits under the GMM: " << _orbitals.size() << std::endl;
double expected = pow(4.0 , _numSpecies);
myfile << "Expected number of orbits under the GMM: " << expected << std::endl;
for (int i = 0; i < _orbitals.size(); i++) {
myfile
<< "------------------------------------------------------------------"
<< std::endl;
myfile << "P(" << transform_adn_chain_val_to_string(
_orbitals[i]) << ") = " << _counts[i] << std::endl;
}
myfile.close();
}
}
bool IsCalculated() {
return _calculated;
}
INTEGER _numSpecies;
INTEGER _adnChainLength;
std::vector<LONG_INTEGER> _orbitals;
std::vector<INTEGER> _counts;
bool _calculated;
// Private methods
void CalcRulesAndValues() {
int _check = 0;
for (int i = 0; i < g_chainLength; i++) {
LONG_INTEGER valOrbital = 0;
for (int j = 0; j < g_numSpecies; j++) {
int val = ADNBaseCharToVal(g_chainCharsSpeciesADN[j * g_chainLength + i]); // returns -1 if the sumbol is not correct (one of the nt)
if(val==-1){
_check=1; // skip this site in the alignment, it contains signs that are not allowed
j=g_numSpecies; // no need to read further across the species, go to the next site
}
else{
valOrbital = (valOrbital << SHIFT_CORRIMENT) | val;
}
}
// Save the val (only if the signs were ok)
if(_check==0){
SaveOrbital(valOrbital);
}
else
{
_check=0;
};
}
_calculated = true;
if(_orbitals.size()==0)
{
std::cout << "Every site in the multiple DNA sequence alignment contains undefined signs. Only bases A, C, G, T are allowed.\n" << std::endl;
exit(0); }
}
void SaveOrbital(LONG_INTEGER &newOrbital) {
bool orbFound = false;
int orbPosCol = -1;
for (int i = 0; i < _orbitals.size() && !orbFound; i++) {
if (_orbitals[i] == newOrbital) {
orbPosCol = i;
orbFound = true;
}
}
if (orbFound) {
_counts[orbPosCol] += 1;
} else {
_counts.push_back(1);
_orbitals.push_back(newOrbital);
}
}
};
static ModelGMM *g_modelGMM = 0;
class ModelSMM {
public:
ModelSMM(ModelGMM *pmodelGMM) :
_calculated(false) {
if (!pmodelGMM || !pmodelGMM->IsCalculated()) {
std::cout << "Error: Model GMM not created or calculated.\n"
<< std::endl;
return;
}
_numSpecies = g_numSpecies;
_adnChainLength = g_chainLength;
CalcRulesAndValues(pmodelGMM);
}
double GetTheoreticalOrbits() {
double expected = pow(4.0 , _numSpecies) / 2.0;
return expected;
}
void ShowResults() {
if (!g_chainCharsSpeciesADN) {
std::cout << "Error: No file Loaded." << std::endl;
return;
}
if (!_calculated) {
std::cout << "No computation done." << std::endl;
}
int option = 1;
// std::cout << " - (0) Show To Screen." << std::endl;
// std::cout << " - (1) Save To File." << std::endl;
//std::cin >> option;
if (option == 0) {
std::cout << "Observed number of orbits under the SMM: " << _orbitals.size()
<< std::endl;
double expected = 1;
expected = GetTheoreticalOrbits();
std::cout << "Expected number of orbits under the SMM: " << expected << std::endl;
for (int i = 0; i < _orbitals.size(); i++) {
std::cout
<< "------------------------------------------------------------------"
<< std::endl;
for (int j = 0; j < _orbitals[i].size(); j++)
std::cout << "P(" << transform_adn_chain_val_to_string(
_orbitals[i][j]) << ") = " << _counts[i][j]
<< std::endl;
}
} else if (option == 1) {
std::string fileName;
std::cout << "Name of the file to save: ";
std::cin >> fileName;
int pos;
if ((pos = fileName.find(".dat")) != -1)
fileName.replace(pos, 4, ".txt");
if (fileName.find(".txt") == -1)
fileName.append(".txt");
// Save
std::ofstream myfile;
myfile.open(fileName.c_str());
myfile << "Observed number of orbits under the SMM: " << _orbitals.size() << std::endl;
double expected = 1;
expected = GetTheoreticalOrbits();
myfile << "expected number of orbits under the SMM: " << expected << std::endl;
for (int i = 0; i < _orbitals.size(); i++) {
myfile
<< "------------------------------------------------------------------"
<< std::endl;
for (int j = 0; j < _orbitals[i].size(); j++)
myfile << "P(" << transform_adn_chain_val_to_string(
_orbitals[i][j]) << ") = " << _counts[i][j]
<< std::endl;
}
myfile.close();
}
}
bool IsCalculated() {
return _calculated;
}
INTEGER _numSpecies;
INTEGER _adnChainLength;
std::vector<std::vector<LONG_INTEGER> > _orbitals;
std::vector<std::vector<INTEGER> > _counts;
bool _calculated;
// Private methods
void CalcRulesAndValues(ModelGMM *pmodelGMM) {
LONG_INTEGER valOrbital;
INTEGER *foundBefore = new INTEGER[pmodelGMM->_orbitals.size()];
memset(foundBefore, 0, sizeof(INTEGER) * pmodelGMM->_orbitals.size());
for (int i = 0; i < pmodelGMM->_orbitals.size(); i++) {
if (!foundBefore[i]) {
int pos = -1;
SearchTwinOrbital(pmodelGMM, i, pos);
if (pos != -1)
foundBefore[pos] = 1;
SaveOrbital(pmodelGMM, i, pos);
}
}
delete foundBefore;
_calculated = true;
}
void SearchTwinOrbital(ModelGMM *pmodelGMM, int posMain, int &posTwin) {
posTwin = -1;
LONG_INTEGER mainOrbVal = pmodelGMM->_orbitals[posMain];
LONG_INTEGER twinOrbVal = change_adn_bases(mainOrbVal, 0, 0);
bool bFound = false;
for (int i = posMain + 1; i < pmodelGMM->_orbitals.size() && !bFound; i++) {
if (pmodelGMM->_orbitals[i] == twinOrbVal) {
posTwin = i;
bFound = true;
}
}
}
void SaveOrbital(ModelGMM *pmodelGMM, int posMain, int posTwin) {
LONG_INTEGER mainOrbVal = pmodelGMM->_orbitals[posMain];
LONG_INTEGER twinOrbVal = change_adn_bases(mainOrbVal, 0, 0);
std::vector<LONG_INTEGER> newOrbital;
newOrbital.push_back(mainOrbVal);
newOrbital.push_back(twinOrbVal);
_orbitals.push_back(newOrbital);
std::vector<INTEGER> newCounts;
newCounts.push_back(pmodelGMM->_counts[posMain]);
if (posTwin != -1)
newCounts.push_back(pmodelGMM->_counts[posTwin]);
else
newCounts.push_back(0);
_counts.push_back(newCounts);
}
};
static ModelSMM *g_modelSMM = 0;
//*****************************************************
class ModelSBD {
public:
ModelSBD(ModelGMM *pmodelGMM) :
_calculated(false) {
if (!pmodelGMM || !pmodelGMM->IsCalculated()) {
std::cout << "Error: Model GMM not created or calculated.\n"
<< std::endl;
return;
}
memset(_eqSBD, 0, 3*sizeof(INTEGER*));
_numSpecies = g_numSpecies;
_adnChainLength = g_chainLength;
CalcRulesAndValues(pmodelGMM);
}
INTEGER *_eqSBD[3]; // for all 6 combinations of two leaves, A has 4 choices, C has 2 two choices, G has 1 choice
INTEGER _numClasses;
INTEGER _numSpecies;
INTEGER _adnChainLength;
double mleSBD;
std::vector<LONG_INTEGER> _orbitals;
std::vector<double> _counts;
std::vector<LONG_INTEGER> _patterns;
void CreatePairs()
{
FreePairs();
_numClasses = g_numSpecies-1;
for(int i = 0; i < 3; i++)
{
_eqSBD[i] = new INTEGER[_numClasses];
for(int k = 0; k < _numClasses; k++)
_eqSBD[i][k] = 0;
}
}
void FreePairs()
{
for(int i = 0; i < 3; i++)
{
if(_eqSBD[i])
{
delete []_eqSBD[i];
}
}
memset(_eqSBD, 0, 3*sizeof(INTEGER*));
}
~ModelSBD()
{
FreePairs();
}
double GetTheoreticalOrbits() { // dimension of the model
double expected = pow(4.0, g_numSpecies)-3*(g_numSpecies-1);
return expected;
}
void ShowResults() {
if (!g_chainCharsSpeciesADN) {