-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraceback.cpp
1152 lines (1075 loc) · 33.2 KB
/
traceback.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 "exODT.h"
using namespace std;
using namespace bpp;
pair<string,scalar_type> exODT_model::p_MLRec(approx_posterior *ale, bool lowmem)
{
ale_pointer=ale;
//cout << "start" << endl;
//iterate over directed patitions (i.e. clades) ordered by the number of leaves
//cout << "start loop" << endl;
//test
//long int tmp_g_id=-1;
//cout << ale->set2name(ale->id_sets[tmp_g_id]) <<endl;
//test
//directed partitions and thier sizes
vector <long int> g_ids;//del-loc
vector <long int> g_id_sizes;//del-loc
for (map <int, vector <long int > > :: iterator it = ale->size_ordered_bips.begin(); it != ale->size_ordered_bips.end(); it++)
for (vector <long int > :: iterator jt = (*it).second.begin(); jt != (*it).second.end(); jt++)
{
g_ids.push_back((*jt));
g_id_sizes.push_back((*it).first);
}
//root biprartitino needs to be handled seperatly
g_ids.push_back(-1);
g_id_sizes.push_back(ale->Gamma_size);
//
// gene<->species mapping
for (int i=0;i<(int)g_ids.size();i++)
{
long int g_id=g_ids[i];
for (int rank=0;rank<last_rank;rank++)
{
int n=time_slices[rank].size();
for (int t_i=0;t_i<(int)time_slice_times[rank].size();t_i++)
{
scalar_type t=time_slice_times[rank][t_i];
for (int branch_i=0;branch_i<n;branch_i++)
{
int e = time_slices[rank][branch_i];
q[g_id][t][e]=0;
}
q[g_id][t][alpha]=0;
}
}
if (g_id_sizes[i]==1)
{
string gene_name=ale->id_leaves[(* (ale->id_sets[g_id].begin()) )];
vector <string> tokens;
boost::split(tokens,gene_name,boost::is_any_of(string_parameter["gene_name_seperators"]),boost::token_compress_on);
string species_name;
if ((int)scalar_parameter["species_field"]==-1)
species_name=tokens[tokens.size()-1];
else
species_name=tokens[(int)scalar_parameter["species_field"]];
bool found=false;
for (map<int,std::string>::iterator tmpit=extant_species.begin();tmpit!=extant_species.end();tmpit++)
if ((*tmpit).second==species_name) found = true;
if (not found) cout << species_name << endl;
gid_sps[g_id]=species_name;
}
}
for (int i=0;i<(int)g_ids.size();i++)
{
// directed partition (dip) gamma's id
bool is_a_leaf=false;
long int g_id=g_ids[i];
if (g_id_sizes[i]==1)
is_a_leaf=true;
vector <long int> gp_ids;//del-loc
vector <long int> gpp_ids;//del-loc
vector <scalar_type> p_part;//del-loc
if (g_id!=-1)
for (map< set<long int>,scalar_type> :: iterator kt = ale->Dip_counts[g_id].begin(); kt != ale->Dip_counts[g_id].end(); kt++)
{
vector <long int> parts;
for (set<long int>::iterator sit=(*kt).first.begin();sit!=(*kt).first.end();sit++) parts.push_back((*sit));
long int gp_id=parts[0];
long int gpp_id=parts[1];
gp_ids.push_back(gp_id);
gpp_ids.push_back(gpp_id);
if (ale->Bip_counts[g_id]<=scalar_parameter["min_bip_count"])
p_part.push_back(0);
else
{
//XX
//cout << ale->p_dip(g_id,gp_id,gpp_id) << " " << g_id << " " << gp_id << " " << gpp_id << endl;
p_part.push_back(ale->p_dip(g_id,gp_id,gpp_id));
}
}
else
{
//root biprartition needs to be handled seperatly
map<set<long int>,int> bip_parts;
for (map <long int,scalar_type> :: iterator it = ale->Bip_counts.begin(); it != ale->Bip_counts.end(); it++)
{
long int gp_id=(*it).first;
set <int> gamma=ale->id_sets[gp_id];
set <int> not_gamma;
for (set<int>::iterator st=ale->Gamma.begin();st!=ale->Gamma.end();st++)
if (gamma.count(*st)==0)
not_gamma.insert(*st);
long int gpp_id = ale->set_ids[not_gamma];
set <long int> parts;
parts.insert(gp_id);
parts.insert(gpp_id);
bip_parts[parts]=1;
gamma.clear();
not_gamma.clear();
}
for (map<set<long int>,int> :: iterator kt = bip_parts.begin();kt!=bip_parts.end();kt++)
{
vector <long int> parts;
for (set<long int>::iterator sit=(*kt).first.begin();sit!=(*kt).first.end();sit++) parts.push_back((*sit));
long int gp_id=parts[0];
long int gpp_id=parts[1];
gp_ids.push_back(gp_id);
gpp_ids.push_back(gpp_id);
if (ale->Bip_counts[gp_id]<=scalar_parameter["min_bip_count"] and not ale->Gamma_size<4)
p_part.push_back(0);
else
p_part.push_back(ale->p_bip(gp_id));
}
bip_parts.clear();
}
int N_parts=gp_ids.size();
//iterate over all postions along S
for (int rank=0;rank<last_rank;rank++)
{
int n=time_slices[rank].size();
for (int t_i=0;t_i<(int)time_slice_times[rank].size();t_i++)
{
//######################################################################################################################
//#########################################INNNER LOOP##################################################################
//######################################################################################################################
scalar_type t=time_slice_times[rank][t_i];
scalar_type tpdt,tpdt_nl;
if ( t_i < scalar_parameter["D"]-1 )
tpdt=time_slice_times[rank][t_i+1];
else if (rank<last_rank-1)
tpdt=time_slice_times[rank+1][0];
else
//top of root stem
tpdt=t_begin[time_slices[rank][0]];
if (scalar_parameter["event_node"]==1)
tpdt_nl=t;
else
tpdt_nl=tpdt;
//root
scalar_type Delta_t=tpdt-t;
//scalar_type N=vector_parameter["N"][rank];
scalar_type Delta_bar=vector_parameter["Delta_bar"][rank];
//scalar_type Lambda_bar=vector_parameter["Lambda_bar"][rank];
//OMG
//scalar_type p_Delta_bar=1-exp(-Delta_bar/N*Delta_t);
scalar_type p_Delta_bar=Delta_bar*Delta_t;
scalar_type Ebar=Ee[-1][t];;
if(1)
{
for (int branch_i=0;branch_i<n;branch_i++)
{
int e = time_slices[rank][branch_i];
//boundaries for branch e
//boundary at present
if (t==0)
{
if (is_a_leaf && extant_species[e]==gid_sps[g_id])
q[g_id][t][e]=1;
else
q[g_id][t][e]=0;
}
//boundary between slice rank and rank-1
else if (t_i==0)
{
//terminating branch is last in time_slices and defines a represented speciation
if (branch_i==n-1 && rank>0)
{
int f=daughters[e][0];
int g=daughters[e][1];
scalar_type Eft=Ee[f][t];
scalar_type Egt=Ee[g][t];
//sum scalar_type q_sum=0;
//q[g_id][t][e]=0;
//max
scalar_type max_term=0;
step max_step;
//max
scalar_type SL_fLg=q[g_id][t][f]*Egt;
scalar_type SL_Lfg=q[g_id][t][g]*Eft;
//SL EVENT
// q_sum+=SL_fLg+SL_Lfg;
//q[g_id][t][e]=q[g_id][t][f]*Egt + q[g_id][t][g]*Eft;
//SL.
//max
if (max_term<SL_fLg)
{
max_term= SL_fLg;
max_step.e=f;
max_step.ep=-1;
max_step.epp=-1;
max_step.t=t;
max_step.rank=rank;
max_step.g_id=g_id;
max_step.gp_id=-1;
max_step.gpp_id=-1;
max_step.event="SL";
}
if (max_term<SL_Lfg)
{
max_term= SL_Lfg;
max_step.e=g;
max_step.ep=-1;
max_step.epp=-1;
max_step.t=t;
max_step.rank=rank;
max_step.g_id=g_id;
max_step.gp_id=-1;
max_step.gpp_id=-1;
max_step.event="SL";
}
//max
//non-leaf directed partition
if (not is_a_leaf)
for (int i=0;i<N_parts;i++)
{
long int gp_id=gp_ids.at(i);
long int gpp_id=gpp_ids.at(i);
scalar_type pp=p_part.at(i);
scalar_type S_pf_ppg=q[gp_id][t][f]*q[gpp_id][t][g]*pp;
scalar_type S_ppf_pg=q[gpp_id][t][f]*q[gp_id][t][g]*pp;
//S EVENT
//q[g_id][t][e]+=q[gp_id][t][f]*q[gpp_id][t][g] +q[gpp_id][t][f]*q[gp_id][t][g];
//sum q_sum+= S_pf_ppg + S_ppf_pg;
//S.
//max
if (max_term<S_pf_ppg)
{
max_term= S_pf_ppg;
max_step.e=-1;
max_step.ep=f;
max_step.epp=g;
max_step.t=t;
max_step.rank=rank;
max_step.g_id=-1;
max_step.gp_id=gp_id;
max_step.gpp_id=gpp_id;
max_step.event="S";
}
if (max_term<S_ppf_pg)
{
max_term=S_ppf_pg;
max_step.e=-1;
max_step.ep=g;
max_step.epp=f;
max_step.t=t;
max_step.rank=rank;
max_step.g_id=-1;
max_step.gp_id=gp_id;
max_step.gpp_id=gpp_id;
max_step.event="S";
}
//max
}
//sum q[g_id][t][e]=q_sum;
q[g_id][t][e]=max_term;
if (not lowmem) q_step[g_id][t][e]=max_step;
}
//branches that cross to next time slice
else
{
//trivial
;//q[g_id][t][e]=q[g_id][t][e];
}
}
//boundaries for branch e.
}
}
if(1)
{
//boundaries for branch alpha virtual branch
//boundary at present
if (t==0)
q[g_id][t][alpha]=0;
//boundary between slice rank and rank-1 slice is trivial
;//q[g_id][t][alpha]=q[g_id][t][alpha];
//boundaries for branch alpha virtual branch.
//events within slice rank at time t on alpha virtual branch
//scalar_type G_bar=exp(-n*(Delta_bar*(N-n)/N+Lambda_bar)*Delta_t);
scalar_type G_bar=Ge[-1][t];//exp(-(Delta_bar*(n-N)/N+Lambda_bar)*Delta_t);
q[g_id][tpdt][alpha]=0;
//sum scalar_type q_sum=0;
//sum scalar_type q_sum_nl=0;
//max
scalar_type max_term=0;
scalar_type max_term_nl=0;
step max_step;
step max_step_nl;
//max
for (int branch_i=0;branch_i<n;branch_i++)
{
int e = time_slices[rank][branch_i];
scalar_type tau_e=vector_parameter["tau"][e];
//scalar_type p_Ntau_e=1-exp(-N*tau_e*Delta_t);
scalar_type p_Ntau_e=1-exp(-tau_e*Delta_t);
//non-leaf directed partition
if (not is_a_leaf)
for (int i=0;i<N_parts;i++)
{
long int gp_id=gp_ids.at(i);
long int gpp_id=gpp_ids.at(i);
scalar_type pp=p_part.at(i);
scalar_type T_ep_app=p_Ntau_e*q[gp_id][t][e]*q[gpp_id][t][alpha]*pp;
scalar_type T_ap_epp=p_Ntau_e*q[gp_id][t][alpha]*q[gpp_id][t][e]*pp;
//Tb EVENT
//sum q_sum_nl+=T_ep_app+T_ap_epp;
//q[g_id][tpdt][alpha]+=p_Ntau_e*(q[gp_id][t][e]*q[gpp_id][t][alpha]+q[gp_id][t][alpha]*q[gpp_id][t][e]);
//Tb.
//max
if (max_term_nl<T_ep_app)
{
max_term_nl=T_ep_app;
max_step_nl.e=-1;
max_step_nl.ep=e;
max_step_nl.epp=alpha;
max_step_nl.t=t;
max_step_nl.rank=rank;
max_step_nl.g_id=-1;
max_step_nl.gp_id=gp_id;
max_step_nl.gpp_id=gpp_id;
max_step_nl.event="Tb";
}
if (max_term_nl<T_ap_epp)
{
max_term_nl=T_ap_epp;
max_step_nl.e=-1;
max_step_nl.ep=alpha;
max_step_nl.epp=e;
max_step_nl.t=t;
max_step_nl.rank=rank;
max_step_nl.g_id=-1;
max_step_nl.gp_id=gp_id;
max_step_nl.gpp_id=gpp_id;
max_step_nl.event="Tb";
}
//max
}
}
//non-leaf directed partition
if (not is_a_leaf)
for (int i=0;i<N_parts;i++)
{
long int gp_id=gp_ids.at(i);
long int gpp_id=gpp_ids[i];
scalar_type pp=p_part[i];
scalar_type Sb=p_Delta_bar*(2*q[gp_id][t][alpha]*q[gpp_id][t][alpha])*pp;
//S_bar EVENT
//sum q_sum_nl+=Sb;
//q[g_id][tpdt][alpha]+=p_Delta_bar*(2*q[gp_id][t][alpha]*q[gpp_id][t][alpha]);
//S_bar.
//max
if (max_term_nl<Sb)
{
max_term_nl=Sb;
max_step_nl.e=-1;
max_step_nl.ep=alpha;
max_step_nl.epp=alpha;
max_step_nl.t=t;
max_step_nl.rank=rank;
max_step_nl.g_id=-1;
max_step_nl.gp_id=gp_id;
max_step_nl.gpp_id=gpp_id;
max_step_nl.event="Sb";
}
//max
}
for (int branch_i=0;branch_i<n;branch_i++)
{
int e = time_slices[rank][branch_i];
scalar_type tau_e=vector_parameter["tau"][e];
//scalar_type p_Ntau_e=1-exp(-N*tau_e*Delta_t);
scalar_type p_Ntau_e=1-exp(-tau_e*Delta_t);
scalar_type TLb=p_Ntau_e*Ebar*q[g_id][t][e];
//TL_bar EVENT
//sum q_sum+=TLb;
//q[g_id][tpdt][alpha]+=p_Ntau_e*Ebar*q[g_id][t][e];
//TL_bar.
//max
if (max_term_nl<TLb)
{
max_term_nl=TLb;
max_step_nl.e=e;
max_step_nl.ep=-1;
max_step_nl.epp=-1;
max_step_nl.t=t;
max_step_nl.rank=rank;
max_step_nl.g_id=g_id;
max_step_nl.gp_id=-1;
max_step_nl.gpp_id=-1;
max_step_nl.event="TLb";
}
//max
}
//sum q[g_id][tpdt_nl][alpha]+=q_sum_nl;
if (q[g_id][tpdt_nl][alpha]<max_term_nl)
{
q[g_id][tpdt_nl][alpha]=max_term_nl;
if (not lowmem) q_step[g_id][tpdt_nl][alpha]=max_step_nl;
}
//0 EVENT
scalar_type empty=G_bar*q[g_id][t][alpha];
//sum q_sum+=empty;
//q[g_id][tpdt][alpha]+=G_bar*q[g_id][t][alpha];
//0.
//max
if (max_term<empty)
{
max_term=empty;
max_step.e=alpha;
max_step.ep=-1;
max_step.epp=-1;
max_step.t=t;
max_step.rank=rank;
max_step.g_id=g_id;
max_step.gp_id=-1;
max_step.gpp_id=-1;
max_step.event="0";
}
//max
//sum q[g_id][tpdt][alpha]+=q_sum;
if (q[g_id][tpdt][alpha]<max_term)
{
q[g_id][tpdt][alpha]=max_term;
if (not lowmem) q_step[g_id][tpdt][alpha]=max_step;
}
//events within slice rank at time t on alpha virtual branch.
}
if(1)
{
for (int branch_i=0;branch_i<n;branch_i++)
{
int e = time_slices[rank][branch_i];
scalar_type Get=Ge[e][t];
scalar_type Eet=Ee[e][t];
scalar_type delta_e=vector_parameter["delta"][e];
scalar_type p_delta_e=1-exp(-delta_e*Delta_t);
//events within slice rank at time t on branch e
q[g_id][tpdt][e]=0;
//sum scalar_type q_sum=0;
//sum scalar_type q_sum_nl=0;
//max
scalar_type max_term=0;
scalar_type max_term_nl=0;
step max_step;
step max_step_nl;
//max
//non-leaf directed partition
if (not is_a_leaf)
for (int i=0;i<N_parts;i++)
{
long int gp_id=gp_ids.at(i);
long int gpp_id=gpp_ids.at(i);
scalar_type pp=p_part.at(i);
scalar_type qpe=q[gp_id][t][e];
scalar_type qppe=q[gpp_id][t][e];
scalar_type Sb_pa_ppe= p_Delta_bar*q[gp_id][t][alpha]*qppe*pp;
scalar_type Sb_pe_ppa= p_Delta_bar*qpe*q[gpp_id][t][alpha]*pp;
//S_bar EVENT
//sum q_sum_nl+= Sb_pa_ppe + Sb_pe_ppa;
//q[g_id][tpdt][e]+=p_Delta_bar*(q[gp_id][t][alpha]*q[gpp_id][t][e]+q[gp_id][t][e]*q[gpp_id][t][alpha]);
//S_bar.
//max
if (max_term_nl<Sb_pa_ppe)
{
max_term_nl=Sb_pa_ppe;
max_step_nl.e=-1;
max_step_nl.ep=alpha;
max_step_nl.epp=e;
max_step_nl.t=t;
max_step_nl.rank=rank;
max_step_nl.g_id=-1;
max_step_nl.gp_id=gp_id;
max_step_nl.gpp_id=gpp_id;
max_step_nl.event="Sb";
}
if (max_term_nl<Sb_pe_ppa)
{
max_term_nl=Sb_pe_ppa;
max_step_nl.e=-1;
max_step_nl.ep=e;
max_step_nl.epp=alpha;
max_step_nl.t=t;
max_step_nl.rank=rank;
max_step_nl.g_id=-1;
max_step_nl.gp_id=gp_id;
max_step_nl.gpp_id=gpp_id;
max_step_nl.event="Sb";
}
//max
scalar_type D=p_delta_e*qpe*qppe*pp;
//D EVENT
//sum q_sum_nl+= D;
//q[g_id][tpdt][e]+=p_delta_e*q[gp_id][t][e]*q[gpp_id][t][e];
//D.
//max
if (max_term_nl<D)
{
max_term_nl=D;
max_step_nl.e=-1;
max_step_nl.ep=e;
max_step_nl.epp=e;
max_step_nl.t=t;
max_step_nl.rank=rank;
max_step_nl.g_id=-1;
max_step_nl.gp_id=gp_id;
max_step_nl.gpp_id=gpp_id;
max_step_nl.event="D";
}
//max
}
//sum q[g_id][tpdt_nl][e]+=q_sum_nl;
if (q[g_id][tpdt_nl][e]<max_term_nl)
{
q[g_id][tpdt_nl][e]=max_term_nl;
if (not lowmem) q_step[g_id][tpdt_nl][e]=max_step_nl;
}
scalar_type empty=Get*q[g_id][t][e];
//0 EVENT
//sum q_sum+=empty;
//q[g_id][tpdt][e]=Get*q[g_id][t][e];
//0.
//max
if (max_term<empty)
{
max_term=empty;
max_step.e=e;
max_step.ep=-1;
max_step.epp=-1;
max_step.t=t;
max_step.rank=rank;
max_step.g_id=g_id;
max_step.gp_id=-1;
max_step.gpp_id=-1;
max_step.event="0";
}
//max
scalar_type SLb=p_Delta_bar*Eet*q[g_id][t][alpha];
//SL_bar EVENT
//sum q_sum+=SLb;
//q[g_id][tpdt][e]+=p_Delta_bar*Eet*q[g_id][t][alpha];
//SL_bar.
//max
if (max_term<SLb)
{
max_term=SLb;
max_step.e=alpha;
max_step.ep=-1;
max_step.epp=-1;
max_step.t=t;
max_step.rank=rank;
max_step.g_id=g_id;
max_step.gp_id=-1;
max_step.gpp_id=-1;
max_step.event="SLb";
}
//max
//sum q[g_id][tpdt][e]+=q_sum;
if (q[g_id][tpdt][e]<max_term)
{
q[g_id][tpdt][e]=max_term;
if (not lowmem) q_step[g_id][tpdt][e]=max_step;
}
//events within slice rank at time t on branch e.
}
}
//######################################################################################################################
//#########################################INNNER LOOP##################################################################
//######################################################################################################################
}
}
gp_ids.clear();
gpp_ids.clear();
p_part.clear();
}
//cout << "end loop" << endl;
/*
for (int rank=0;rank<last_rank;rank++)
{
int n=time_slices[rank].size();
for (int t_i=0;t_i<(int)time_slice_times[rank].size();t_i++)
{
scalar_type t=time_slice_times[rank][t_i];
//cout << rank << " " << n << " " << t << " " << q[tmp_g_id][t][alpha]<< endl;
for (int branch_i=0;branch_i<n;branch_i++)
{
int branch = time_slices[rank][branch_i];
scalar_type tmp_q=q[tmp_g_id][t][branch];
Node * tmp_node = id_nodes[branch];
//cout << branch << " " << t_i << " " << rank <<" "<< tmp_node << endl;
stringstream out;
string name = (* (dynamic_cast<const BppString *>(tmp_node->getBranchProperty("ID")))).toSTL();
if (tmp_q>0)
out << log(tmp_q) ;
//out << Ge[branch][t];
tmp_node->setBranchProperty("ID",BppString(name+out.str().substr(0,4)+"|"));
//if (tmp_node->isLeaf())
//tmp_node->setName(tmp_node->getName()+out.str().substr(0,4)+"|");
}
}
}
cout << TreeTemplateTools::treeToParenthesis(*S,false,"ID") << endl;
for (map <Node *,int >::iterator it=node_ids.begin();it!=node_ids.end();it++ )
(*it).first->setBranchProperty("ID",BppString(""));
*/
//test
//del-locs
g_ids.clear();
g_id_sizes.clear();
if (not lowmem)
{
return traceback();
}
else
{
//cout << "LOWMEM" <<endl;
scalar_type max_term=0;
long int g_id=-1;
int max_e=-11;
scalar_type max_t=-11;
scalar_type max_rank=-11;
scalar_type root_norm=0;
for (int rank=0;rank<last_rank;rank++)
{
int n=time_slices[rank].size();
for (int t_i=0;t_i<(int)time_slice_times[rank].size();t_i++)
{
for (int branch_i=0;branch_i<n;branch_i++)
{
root_norm+=1;
}
root_norm+=1;
}
}
for (int rank=0;rank<last_rank;rank++)
{
int n=time_slices[rank].size();
for (int t_i=0;t_i<(int)time_slice_times[rank].size();t_i++)
{
scalar_type t=time_slice_times[rank][t_i];
for (int branch_i=0;branch_i<n;branch_i++)
{
int e = time_slices[rank][branch_i];
if (max_term<q[g_id][t][e])
{
max_term=q[g_id][t][e];
max_e=e;
max_t=t_i;
max_rank=rank;
}
}
if (max_term<q[g_id][t][alpha])
{
max_term=q[g_id][t][alpha];
max_e=alpha;
max_t=t_i;
max_rank=rank;
}
}
}
pair<string,scalar_type> return_pair;
MLRec_events.clear();
Ttokens.clear();
register_O(max_e);
return_pair.first=sample(false,-1,max_t,max_rank,max_e,0,"","",true)+";\n";
return_pair.second=max_term/root_norm;
return return_pair;
}
}
pair<string,scalar_type> exODT_model::traceback()
{
stringstream signal_stream;
scalar_type max_term=0;
long int g_id=-1;
int max_e=-11;
scalar_type max_t=-11;
scalar_type max_rank=-11;
scalar_type root_norm=0;
for (int rank=0;rank<last_rank;rank++)
{
int n=time_slices[rank].size();
for (int t_i=0;t_i<(int)time_slice_times[rank].size();t_i++)
{
for (int branch_i=0;branch_i<n;branch_i++)
{
root_norm+=1;
}
root_norm+=1;
}
}
for (int rank=0;rank<last_rank;rank++)
{
int n=time_slices[rank].size();
for (int t_i=0;t_i<(int)time_slice_times[rank].size();t_i++)
{
scalar_type t=time_slice_times[rank][t_i];
for (int branch_i=0;branch_i<n;branch_i++)
{
int e = time_slices[rank][branch_i];
signal_stream << g_id<<" "<<t<<" "<<e<<" "<<q[g_id][t][e]<<endl;
if (max_term<q[g_id][t][e])
{
max_term=q[g_id][t][e];
max_e=e;
max_t=t;
max_rank=rank;
}
}
signal_stream << g_id<<" "<<t<<" "<<alpha<<" "<<q[g_id][t][alpha]<<endl;
if (max_term<q[g_id][t][alpha])
{
max_term=q[g_id][t][alpha];
max_e=alpha;
max_t=t;
max_rank=rank;
}
}
}
signal_string=signal_stream.str();
pair<string,scalar_type> return_pair;
MLRec_events.clear();
Ttokens.clear();
register_O(max_e);
return_pair.first=traceback(g_id,max_t,max_rank,max_e,0,"")+";\n";
return_pair.second=max_term/root_norm;
for (std::map<long int, std::map< scalar_type, std::map<int, scalar_type> > >::iterator it=q.begin();it!=q.end();it++)
{
for ( std::map< scalar_type, std::map<int, scalar_type> >::iterator jt=(*it).second.begin();jt!=(*it).second.end();jt++)
(*jt).second.clear();
(*it).second.clear();
}
q.clear();
for (std::map<long int, std::map< scalar_type, std::map<int, step> > >::iterator it=q_step.begin();it!=q_step.end();it++)
{
for ( std::map< scalar_type, std::map<int, step> >::iterator jt=(*it).second.begin();jt!=(*it).second.end();jt++)
(*jt).second.clear();
(*it).second.clear();
}
q_step.clear();
return return_pair;
}
string exODT_model::traceback(long int g_id,scalar_type t,scalar_type rank,int e,scalar_type branch_length,string branch_events, string transfer_token)
{
/*
if (e==alpha)
cout << "b "<<-1;
else if (id_ranks[e]==0)
cout << "b "<<extant_species[e];
else
cout << "b "<< id_ranks[e];
cout << "from " << e << " " << t << " " << endl;
cout << "g_id "<< g_id <<" is " <<ale_pointer->set2name(ale_pointer->id_sets[g_id]) << endl;
*/
step max_step=q_step[g_id][t][e];
//cout << max_step.event<<endl;
//cout << t << "==0 and " << (int)( tmpset.size() ) << "==1 and " <<e <<" !=-1 "<<endl;
scalar_type new_branch_length=branch_length+t-max_step.t;
if (max_step.t!=max_step.t)
new_branch_length=branch_length;
if (t==0 and ale_pointer->id_sets[g_id].size()==1 and e!=-1)
{
register_leaf(e);
stringstream branch_string;
if (scalar_parameter["leaf_events"]==1) branch_string<<branch_events;
branch_string <<":"<<new_branch_length;
return ale_pointer->set2name(ale_pointer->id_sets[g_id])+branch_string.str();
}
/*
if (ale_pointer->Bip_counts[g_id]>0)
new_branch_length=ale_pointer->Bip_bls[g_id]/ale_pointer->Bip_counts[g_id];
else
new_branch_length=ale_pointer->Bip_bls[g_id]/ale_pointer->observations;
*/
if (max_step.event=="D" or max_step.event=="Tb" or max_step.event=="S" or max_step.event=="Sb")
{
//cout << max_step.event << " " << max_step.ep << "-" << max_step.epp <<" "<<max_step.t <<endl;
stringstream transfer_token_stream;
transfer_token_stream<<"";
stringstream branch_string;
if (max_step.event=="S")
{
register_S(e);
branch_string<< branch_events//<<max_step.event<<"@"
<<"."<<id_ranks[e]<<":"<<max(new_branch_length,(scalar_type)0.0);
}
else
{
if (max_step.event=="Tb")
{
//cout << "T";
int this_e;
if (max_step.ep==alpha)
this_e=max_step.epp;
else
this_e=max_step.ep;
stringstream named_branch;
if (this_e==alpha)
named_branch<<-1;
else if (id_ranks[this_e]==0)
named_branch<<extant_species[this_e];
else
named_branch<<id_ranks[this_e];
// Tto
register_Tto(this_e);
stringstream tmp;
tmp<<max_step.rank<<"|"<<t<<"|"<<named_branch.str();
//tmp<<max_step.t<<"|"<<this_e;
register_Ttoken(transfer_token+"|"+tmp.str());
// Tto
branch_string<< branch_events<<max_step.event<<"@"<<max_step.rank<<"|"<<named_branch.str()<<":"<<max(new_branch_length,(scalar_type)0.0);
}
else if (max_step.event=="Sb")
{
int this_e;
if (max_step.ep==alpha)
this_e=max_step.epp;
else
this_e=max_step.ep;
// Tfrom
register_Tfrom(this_e);
// Tfrom
stringstream named_branch;
if (this_e==alpha)
named_branch<<-1;
else if (id_ranks[this_e]==0)
named_branch<<extant_species[this_e];
else
named_branch<<id_ranks[this_e];
transfer_token_stream<< rank<<"|"<<t<<"|"<<named_branch.str();
//transfer_token_stream<< t<<"|"<<this_e;
branch_string<< branch_events<<"T@"<<rank<<"|"<<named_branch.str()<<":"<<max(new_branch_length,(scalar_type)0.0);
}
else
{
//cout << "D";
register_D(e);
stringstream named_branch;
if (e==alpha)
named_branch<<-1;
else if (id_ranks[e]==0)
named_branch<<extant_species[e];
else
named_branch<<id_ranks[e];
branch_string<< branch_events<<max_step.event<<"@"<<rank<<"|"<<named_branch.str()<<":"<<max(new_branch_length,(scalar_type)0.0);
}
}
if ( transfer_token_stream.str()=="")
return "("+
traceback(max_step.gp_id,max_step.t,max_step.rank,max_step.ep,0,"",transfer_token)
+","+
traceback(max_step.gpp_id,max_step.t,max_step.rank,max_step.epp,0,"",transfer_token)
+")"+branch_string.str();
else
if(max_step.ep==alpha)
return "("+
traceback(max_step.gp_id,max_step.t,max_step.rank,max_step.ep,0,"",transfer_token_stream.str())
+","+
traceback(max_step.gpp_id,max_step.t,max_step.rank,max_step.epp,0,"",transfer_token)
+")"+branch_string.str();
else
return "("+
traceback(max_step.gp_id,max_step.t,max_step.rank,max_step.ep,0,"",transfer_token)
+","+
traceback(max_step.gpp_id,max_step.t,max_step.rank,max_step.epp,0,"",transfer_token_stream.str())
+")"+branch_string.str();
}
else if ( max_step.event=="TLb" or max_step.event=="SL" or max_step.event=="SLb" or max_step.event=="0")
{
//cout << max_step.event << " " << max_step.e <<" "<<max_step.t <<endl;
stringstream branch_string;
stringstream transfer_token_stream;
transfer_token_stream <<"";
branch_string<< branch_events;
if (max_step.event!="0")
{
if (max_step.event=="SL")
{
register_S(e);
int f=daughters[e][0];
int g=daughters[e][1];
if (max_step.e==f)
register_L(g);
else
register_L(f);
branch_string<<"."//<<max_step.event<<"@"
<<id_ranks[e];
}
else
{
if (max_step.event=="TLb")
{
register_Tto(max_step.e);
stringstream tmp;
stringstream named_branch;
if (max_step.e==alpha)
named_branch<<-1;
else if (id_ranks[max_step.e]==0)
named_branch<<extant_species[max_step.e];
else
named_branch<<id_ranks[max_step.e];
tmp<<max_step.rank<<"|"<< t << "|"<< named_branch.str();
//tmp<<max_step.t<<"|"<<max_step.e;
register_Ttoken(transfer_token+"|"+tmp.str());
transfer_token="";
branch_string<<""//<<max_step.event
<<"@"<<max_step.rank<<"|"<<named_branch.str();
}
else if (max_step.event=="SLb")
{
//cout << "L";
register_L(e);
register_Tfrom(e);
stringstream named_branch;
if (e==alpha)
named_branch<<-1;
else if (id_ranks[e]==0)
named_branch<<extant_species[e];
else
named_branch<<id_ranks[e];
transfer_token_stream<< rank<<"|"<<t<<"|"<<named_branch.str();
//transfer_token_stream<< max_step.t<<"|"<<e;
branch_string<<".T"//<<max_step.event
<<"@"<<rank<<"|"<<named_branch.str();
}
}
}
if (transfer_token_stream.str()=="")
return traceback(max_step.g_id, max_step.t,max_step.rank, max_step.e, new_branch_length, branch_string.str(),transfer_token);
else