-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearchAlgo.c
3291 lines (2593 loc) · 89.9 KB
/
searchAlgo.c
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
/**
* PLL (version 1.0.0) a software library for phylogenetic inference
* Copyright (C) 2013 Tomas Flouri and Alexandros Stamatakis
*
* Derived from
* RAxML-HPC, a program for sequential and parallel estimation of phylogenetic
* trees by Alexandros Stamatakis
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* For any other enquiries send an Email to Tomas Flouri
* Tomas.Flouri@h-its.org
*
* When publishing work that uses PLL please cite PLL
*
* @file searchAlgo.c
* @brief Collection of routines for performing likelihood computation and branch optimization.
*
* Detailed description to appear soon.
*/
#include "mem_alloc.h"
#include "systypes.h"
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include "pll.h"
#include "pllInternal.h"
typedef struct bInf {
double likelihood;
nodeptr node;
} bestInfo;
typedef struct iL {
bestInfo *list;
int n;
int valid;
} infoList;
double treeOptimizeRapid(pllInstance *tr, partitionList *pr, int mintrav, int maxtrav, bestlist *bt, infoList *iList);
nniMove getBestNNIForBran(pllInstance* tr, partitionList *pr, nodeptr p, double curLH);
void evalNNIForSubtree(pllInstance* tr, partitionList *pr, nodeptr p, nniMove* nniList, int* cnt, int* cnt_nni, double curLH);
static int cmp_nni(const void* nni1, const void* nni2);
static void pllTraverseUpdate (pllInstance *tr, partitionList *pr, nodeptr p, nodeptr q, int mintrav, int maxtrav, pllRearrangeList * bestList);
static int pllStoreRearrangement (pllRearrangeList * bestList, pllRearrangeInfo * rearr);
static int pllTestInsertBIG (pllInstance * tr, partitionList * pr, nodeptr p, nodeptr q, pllRearrangeList * bestList);
static int pllTestSPR (pllInstance * tr, partitionList * pr, nodeptr p, int mintrav, int maxtrav, pllRearrangeList * bestList);
static void pllCreateSprInfoRollback (pllInstance * tr, pllRearrangeInfo * rearr, int numBranches);
static void pllCreateNniInfoRollback (pllInstance * tr, pllRearrangeInfo * rearr);
static void pllCreateRollbackInfo (pllInstance * tr, pllRearrangeInfo * rearr, int numBranches);
static void pllRollbackNNI (pllInstance * tr, partitionList * pr, pllRollbackInfo * ri);
static void pllRollbackSPR (partitionList * pr, pllRollbackInfo * ri);
extern partitionLengths pLengths[PLL_MAX_MODEL];
pllBoolean initrav (pllInstance *tr, partitionList *pr, nodeptr p)
{
nodeptr q;
if (!isTip(p->number, tr->mxtips))
{
q = p->next;
do
{
if (! initrav(tr, pr, q->back)) return PLL_FALSE;
q = q->next;
}
while (q != p);
pllUpdatePartials(tr, pr, p, PLL_FALSE);
}
return PLL_TRUE;
}
/** @brief Optimize the length of a specific branch
Optimize the length of the branch connecting \a p and \a p->back
for each partition (\a tr->numBranches) in library instance \a tr.
@param tr
The library instance
@param pr
Partition list
@param p
Endpoints of branch to be optimized
*/
void update(pllInstance *tr, partitionList *pr, nodeptr p)
{
nodeptr q;
int i;
double z[PLL_NUM_BRANCHES], z0[PLL_NUM_BRANCHES];
int numBranches = pr->perGeneBranchLengths ? pr->numberOfPartitions : 1;
#ifdef _DEBUG_UPDATE
double
startLH;
pllEvaluateLikelihood (tr, p);
startLH = tr->likelihood;
#endif
q = p->back;
for(i = 0; i < numBranches; i++)
z0[i] = q->z[i];
if(numBranches > 1)
makenewzGeneric(tr, pr, p, q, z0, PLL_NEWZPERCYCLE, z, PLL_TRUE);
else
makenewzGeneric(tr, pr, p, q, z0, PLL_NEWZPERCYCLE, z, PLL_FALSE);
for(i = 0; i < numBranches; i++)
{
if(!tr->partitionConverged[i])
{
if(PLL_ABS(z[i] - z0[i]) > PLL_DELTAZ)
{
tr->partitionSmoothed[i] = PLL_FALSE;
}
p->z[i] = q->z[i] = z[i];
}
}
#ifdef _DEBUG_UPDATE
pllEvaluateLikelihood (tr, p);
if(tr->likelihood <= startLH)
{
if(fabs(tr->likelihood - startLH) > 0.01)
{
printf("%f %f\n", startLH, tr->likelihood);
assert(0);
}
}
#endif
}
/** @brief Branch length optimization of subtree
Optimize the length of branch connected by \a p and \a p->back, and the
lengths of all branches in the subtrees rooted at \a p->next and \a p->next->next
@param tr
The library instance
@param pr
Partition list
@param p
Endpoint of branches to be optimized
*/
void smooth (pllInstance *tr, partitionList *pr, nodeptr p)
{
nodeptr q;
int numBranches = pr->perGeneBranchLengths?pr->numberOfPartitions:1;
update(tr, pr, p); /* Adjust branch */
if (! isTip(p->number, tr->mxtips))
{ /* Adjust descendants */
q = p->next;
while (q != p)
{
smooth(tr, pr, q->back);
q = q->next;
}
if(numBranches > 1 && !tr->useRecom)
pllUpdatePartials(tr, pr,p, PLL_TRUE);
else
pllUpdatePartials(tr, pr,p, PLL_FALSE);
}
}
/** @brief Check whether the branches in all partitions have been optimized
Check if all branches in all partitions have reached the threshold for
optimization. If at least one branch can be optimized further return \b PLL_FALSE.
@param tr
The library instance
@return
If at least one branch can be further optimized return \b PLL_FALSE,
otherwise \b PLL_TRUE.
*/
static pllBoolean allSmoothed(pllInstance *tr, int numBranches)
{
int i;
pllBoolean result = PLL_TRUE;
for(i = 0; i < numBranches; i++)
{
if(tr->partitionSmoothed[i] == PLL_FALSE)
result = PLL_FALSE;
else
tr->partitionConverged[i] = PLL_TRUE;
}
return result;
}
/** @brief Optimize all branch lenghts of a tree
Perform \a maxtimes rounds of branch length optimization by running smooth()
on all neighbour nodes of node \a tr->start.
@param tr
The library instance
@param maxtimes
Number of optimization rounds to perform
*/
/* do maxtimes rounds of branch length optimization */
void smoothTree (pllInstance *tr, partitionList *pr, int maxtimes)
{
nodeptr p, q;
int i, count = 0;
int numBranches = pr->perGeneBranchLengths?pr->numberOfPartitions:1;
p = tr->start;
for(i = 0; i < numBranches; i++)
tr->partitionConverged[i] = PLL_FALSE;
while (--maxtimes >= 0)
{
for(i = 0; i < numBranches; i++)
tr->partitionSmoothed[i] = PLL_TRUE;
smooth(tr, pr, p->back);
if (!isTip(p->number, tr->mxtips))
{
q = p->next;
while (q != p)
{
smooth(tr, pr, q->back);
q = q->next;
}
}
count++;
if (allSmoothed(tr, numBranches)) break;
}
for(i = 0; i < numBranches; i++)
tr->partitionConverged[i] = PLL_FALSE;
}
/** @brief Optimize the branch length of edges around a specific node
Optimize \a maxtimes the branch length of all (3) edges around a given node
\a p of the tree of library instance \a tr.
@param tr
The library instance
@param p
The node around which to optimize the edges
@param maxtimes
Number of optimization rounds to perform
*/
void localSmooth (pllInstance *tr, partitionList *pr, nodeptr p, int maxtimes)
{
nodeptr q;
int i;
int numBranches = pr->perGeneBranchLengths ? pr->numberOfPartitions : 1;
if (isTip(p->number, tr->mxtips)) return;
for(i = 0; i < PLL_NUM_BRANCHES; i++)
tr->partitionConverged[i] = PLL_FALSE;
while (--maxtimes >= 0)
{
for(i = 0; i < PLL_NUM_BRANCHES; i++)
tr->partitionSmoothed[i] = PLL_TRUE;
q = p;
do
{
update(tr, pr, q);
q = q->next;
}
while (q != p);
if (allSmoothed(tr, numBranches))
break;
}
for(i = 0; i < PLL_NUM_BRANCHES; i++)
{
tr->partitionSmoothed[i] = PLL_FALSE;
tr->partitionConverged[i] = PLL_FALSE;
}
}
/** @brief Reset an \a infoList
Resets an \a infoList by setting elements \a node and \a likelihood
of each element of the \a bestInfo list structure to \b NULL and
\a PLL_UNLIKELY, respectively.
@param iList
The given \a infoList.
*/
static void resetInfoList(infoList *iList)
{
int
i;
iList->valid = 0;
for(i = 0; i < iList->n; i++)
{
iList->list[i].node = (nodeptr)NULL;
iList->list[i].likelihood = PLL_UNLIKELY;
}
}
/** @brief Initialize an \a infoList
Initialize an \a infoList by creating a \a bestInfo list structure
of \a n elements and setting the attributes \a node and \a likelihood
of each element of the \a bestInfo list structure to \b NULL and
\a PLL_UNLIKELY, respectively.
@param iList
The given \a infoList.
@param n
Number of elements to be created in the \a bestInfo list.
*/
static void initInfoList(infoList *iList, int n)
{
int
i;
iList->n = n;
iList->valid = 0;
iList->list = (bestInfo *)rax_malloc(sizeof(bestInfo) * (size_t)n);
for(i = 0; i < n; i++)
{
iList->list[i].node = (nodeptr)NULL;
iList->list[i].likelihood = PLL_UNLIKELY;
}
}
/** @brief Deallocate the contents of an \a infoList
Deallocate the contents of a given \a infoList by freeing
the memory used by its \a bestInfo list structure.
@param iList
The \a infoList to be used.
*/
static void freeInfoList(infoList *iList)
{
rax_free(iList->list);
}
/** @brief Insert a record in an \a infoList
Insert the pair \a likelihood and \node into list \a iList
\b only if there already exists a pair in \a iList
whose \a likelihood attribute is smaller than the given \a
likelihood. The insertion is done by replacing the smallest
likelihood pair with the new pair.
@param node
The given node
@param likelihood
The given likelihood
@param iList
The given \a infoList where the record will possibly be appended.
*/
static void insertInfoList(nodeptr node, double likelihood, infoList *iList)
{
int
i,
min = 0;
double
min_l = iList->list[0].likelihood;
for(i = 1; i < iList->n; i++)
{
if(iList->list[i].likelihood < min_l)
{
min = i;
min_l = iList->list[i].likelihood;
}
}
if(likelihood > min_l)
{
iList->list[min].likelihood = likelihood;
iList->list[min].node = node;
if(iList->valid < iList->n)
iList->valid += 1;
}
}
/** @brief Optimize branch lengths of region
Optimize the branch lenghts of only a specific region. The branch optimization starts
at a node \a p and is carried out in all nodes with distance upto \a region edges from
\a p.
@param tr
The library instance.
@param p
Node to start branch optimization from.
@param region
The allowed node distance from \p for which to still perform branch optimization.
*/
void smoothRegion (pllInstance *tr, partitionList *pr, nodeptr p, int region)
{
nodeptr q;
update(tr, pr, p); /* Adjust branch */
if (region > 0)
{
if (!isTip(p->number, tr->mxtips))
{
q = p->next;
while (q != p)
{
smoothRegion(tr, pr, q->back, --region);
q = q->next;
}
pllUpdatePartials(tr, pr,p, PLL_FALSE);
}
}
}
/** @brief Wrapper function for optimizing the branch length of a region \a maxtimes times
Optimize the branch lengths of a specific region \a maxtimes times. The branch optimization
starts at a given node \a p and is carried out in all nodes with distance upto \a region
from \a p.
@param tr
The library instance.
@param p
Node to start branch optimization from.
@param maxtimes
Number of times to perform branch optimization.
@param region
The allwed node distance from \p for which to still perform branch optimization.
@todo
In the previous version (before the model-sep merge) the loops were controlled by tr->numBranches,
and now they are controlled by a constant PLL_NUM_BRANCHES. What is right?
*/
void regionalSmooth (pllInstance *tr, partitionList *pr, nodeptr p, int maxtimes, int region)
{
nodeptr q;
int i;
int numBranches = pr->perGeneBranchLengths?pr->numberOfPartitions:1;
if (isTip(p->number, tr->mxtips)) return; /* Should be an error */
for(i = 0; i < PLL_NUM_BRANCHES; i++)
tr->partitionConverged[i] = PLL_FALSE;
while (--maxtimes >= 0)
{
for(i = 0; i < PLL_NUM_BRANCHES; i++)
tr->partitionSmoothed[i] = PLL_TRUE;
q = p;
do
{
smoothRegion(tr, pr, q, region);
q = q->next;
}
while (q != p);
if (allSmoothed(tr, numBranches))
break;
}
for(i = 0; i < PLL_NUM_BRANCHES; i++) {
tr->partitionSmoothed[i] = PLL_FALSE;
tr->partitionConverged[i] = PLL_FALSE;
}
}
/** @brief Split the tree into two components and optimize new branch length
Split the tree into two components. The disconnection point is node \a p.
First, a branch length is computed for the newly created branch between nodes
\a p->next->back and \a p->next->next->back and then the two nodes are
connected (hookup). Disconnection is done by setting \a p->next->next->back
and \a p->next->back to \b NULL.
@param tr
The library instance
@param p
The node at which the tree should be decomposed into two components.
@param numBranches
Number of branches per partition
@return
Node from the disconnected component
@todo
Why do we return this node?
@image html removeBIG.png "The diagram shows in blue color the new edge that is created and in red the edges that are removed"
*/
nodeptr removeNodeBIG (pllInstance *tr, partitionList *pr, nodeptr p, int numBranches)
{
// double zqr[numBranches], result[numBranches];
double* zqr = (double*)rax_malloc(numBranches * sizeof(double));
double* result = (double*)rax_malloc(numBranches * sizeof(double));
nodeptr q, r;
int i;
q = p->next->back;
r = p->next->next->back;
for(i = 0; i < numBranches; i++)
zqr[i] = q->z[i] * r->z[i];
makenewzGeneric(tr, pr, q, r, zqr, PLL_ITERATIONS, result, PLL_FALSE);
for(i = 0; i < numBranches; i++)
tr->zqr[i] = result[i];
hookup(q, r, result, numBranches);
p->next->next->back = p->next->back = (node *) NULL;
rax_free(result);
rax_free(zqr);
return q;
}
/** @brief Split the tree into two components and recompute likelihood
Split the tree into two component. The disconnection point is node \a p.
Set the branch length of the new node between \a p->next->back and
\a p->next->next->back to \a tr->currentZQR and then decompose the tree
into two components by setting \a p->next->back and \a p->next->next->back
to \b NULL.
@param tr
The library instance
@param p
The node at which the tree should be decomposed into two components.
@return q
the node after \a p
@todo
Why do we return this node? Why do we set to tr->currentZQR and not compute
new optimized length? What is tr->currentZQR?
*/
nodeptr removeNodeRestoreBIG (pllInstance *tr, partitionList *pr, nodeptr p)
{
nodeptr q, r;
q = p->next->back;
r = p->next->next->back;
pllUpdatePartials(tr, pr,q, PLL_FALSE);
pllUpdatePartials(tr, pr,r, PLL_FALSE);
hookup(q, r, tr->currentZQR, pr->perGeneBranchLengths?pr->numberOfPartitions:1);
p->next->next->back = p->next->back = (node *) NULL;
return q;
}
/** @brief Connect two disconnected tree components
Connect two disconnected components by specifying an internal edge from one
component and a leaf from the other component. The internal edge \a e is the
edge between \a q and \a q->back. The leaf is specified by \a p.
Edge \a e is removed and two new edges are created. The first one is an edge
between \a p->next and \a q, and the second one is between \a p->next->next
and \a q->back. The new likelihood vector for node \a p is computed.
@note The function makes use of the \a thoroughInsertion flag
@todo
What is tr->lzi ? What is thorough insertion? Why do we optimize branch lengths
that will be removed? Add explanation
@image html pll.png "The diagram shows in blue colors the new edges that are created and in red the edge that is removed"
*/
pllBoolean insertBIG (pllInstance *tr, partitionList *pr, nodeptr p, nodeptr q)
{
nodeptr r, s;
int i;
int numBranches = pr->perGeneBranchLengths?pr->numberOfPartitions:1;
r = q->back;
s = p->back;
for(i = 0; i < numBranches; i++)
tr->lzi[i] = q->z[i];
if(tr->thoroughInsertion)
{
double* zqr = (double*)rax_malloc(numBranches * sizeof(double));
double* zqs = (double*)rax_malloc(numBranches * sizeof(double));
double* zrs = (double*)rax_malloc(numBranches * sizeof(double));
double lzqr, lzqs, lzrs, lzsum, lzq, lzr, lzs, lzmax;
double *defaultArray = (double*)rax_malloc(numBranches*sizeof(double));
double* e1 = (double*)rax_malloc(numBranches * sizeof(double));
double* e2 = (double*)rax_malloc(numBranches * sizeof(double));
double* e3 = (double*)rax_malloc(numBranches*sizeof(double));
double *qz;
qz = q->z;
for(i = 0; i < numBranches; i++)
defaultArray[i] = PLL_DEFAULTZ;
makenewzGeneric(tr, pr, q, r, qz, PLL_ITERATIONS, zqr, PLL_FALSE);
/* the branch lengths values will be estimated using q, r and s
* q-s are not connected, but both q and s have a valid LH vector , so we can call makenewzGeneric to get a value for
* lzsum, which is then use to generate reasonable starting values e1, e2, e3 for the new branches we create after the insertion
*/
makenewzGeneric(tr, pr, q, s, defaultArray, PLL_ITERATIONS, zqs, PLL_FALSE);
makenewzGeneric(tr, pr, r, s, defaultArray, PLL_ITERATIONS, zrs, PLL_FALSE);
for(i = 0; i < numBranches; i++)
{
lzqr = (zqr[i] > PLL_ZMIN) ? log(zqr[i]) : log(PLL_ZMIN);
lzqs = (zqs[i] > PLL_ZMIN) ? log(zqs[i]) : log(PLL_ZMIN);
lzrs = (zrs[i] > PLL_ZMIN) ? log(zrs[i]) : log(PLL_ZMIN);
lzsum = 0.5 * (lzqr + lzqs + lzrs);
lzq = lzsum - lzrs;
lzr = lzsum - lzqs;
lzs = lzsum - lzqr;
lzmax = log(PLL_ZMAX);
if (lzq > lzmax) {lzq = lzmax; lzr = lzqr; lzs = lzqs;}
else if (lzr > lzmax) {lzr = lzmax; lzq = lzqr; lzs = lzrs;}
else if (lzs > lzmax) {lzs = lzmax; lzq = lzqs; lzr = lzrs;}
e1[i] = exp(lzq);
e2[i] = exp(lzr);
e3[i] = exp(lzs);
}
hookup(p->next, q, e1, numBranches);
hookup(p->next->next, r, e2, numBranches);
hookup(p, s, e3, numBranches);
rax_free(e3);
rax_free(e2);
rax_free(e1);
rax_free(defaultArray);
rax_free(zrs);
rax_free(zqs);
rax_free(zqr);
}
else
{
double *z = (double*) rax_malloc(numBranches*sizeof(double));
for(i = 0; i < numBranches; i++)
{
z[i] = sqrt(q->z[i]);
if(z[i] < PLL_ZMIN)
z[i] = PLL_ZMIN;
if(z[i] > PLL_ZMAX)
z[i] = PLL_ZMAX;
}
hookup(p->next, q, z, numBranches);
hookup(p->next->next, r, z, numBranches);
rax_free(z);
}
pllUpdatePartials(tr, pr,p, PLL_FALSE);
if(tr->thoroughInsertion)
{
localSmooth(tr, pr, p, PLL_MAX_LOCAL_SMOOTHING_ITERATIONS);
for(i = 0; i < numBranches; i++)
{
tr->lzq[i] = p->next->z[i];
tr->lzr[i] = p->next->next->z[i];
tr->lzs[i] = p->z[i];
}
}
return PLL_TRUE;
}
/** @brief Connect two disconnected tree components without optimizing branch lengths
Connect two disconnected components by specifying an internal edge from one
component and a leaf from the other component. The internal edge \a e is the
edge between \a q and \a q->back. The leaf is specified by \a p.
Edge \a e is removed and two new edges are created. The first one is an edge
between \a p->next and \a q, and the second one is between \a p->next->next
and \a q->back. The new likelihood vector for node \a p is computed.
@note The function makes use of the \a thoroughInsertion flag
@todo
What is the difference between this and insertBIG?
*/
pllBoolean insertRestoreBIG (pllInstance *tr, partitionList *pr, nodeptr p, nodeptr q)
{
nodeptr r, s;
r = q->back;
s = p->back;
int numBranches = pr->perGeneBranchLengths?pr->numberOfPartitions:1;
if(tr->thoroughInsertion)
{
hookup(p->next, q, tr->currentLZQ, numBranches);
hookup(p->next->next, r, tr->currentLZR, numBranches);
hookup(p, s, tr->currentLZS, numBranches);
}
else
{
double z[PLL_NUM_BRANCHES];
int i;
for(i = 0; i < numBranches; i++)
{
double zz;
zz = sqrt(q->z[i]);
if(zz < PLL_ZMIN)
zz = PLL_ZMIN;
if(zz > PLL_ZMAX)
zz = PLL_ZMAX;
z[i] = zz;
}
hookup(p->next, q, z, numBranches);
hookup(p->next->next, r, z, numBranches);
}
pllUpdatePartials(tr, pr,p, PLL_FALSE);
return PLL_TRUE;
}
static void restoreTopologyOnly(pllInstance *tr, bestlist *bt, int numBranches)
{
nodeptr p = tr->removeNode;
nodeptr q = tr->insertNode;
double qz[PLL_NUM_BRANCHES], pz[PLL_NUM_BRANCHES], p1z[PLL_NUM_BRANCHES], p2z[PLL_NUM_BRANCHES];
nodeptr p1, p2, r, s;
double currentLH = tr->likelihood;
int i;
p1 = p->next->back;
p2 = p->next->next->back;
//memcpy(p1z, p1->z, numBranches*sizeof(double));
//memcpy(p2z, p2->z, numBranches*sizeof(double));
//memcpy(qz, q->z, numBranches*sizeof(double));
//memcpy(pz, p->z, numBranches*sizeof(double));
for(i = 0; i < numBranches; i++)
{
p1z[i] = p1->z[i];
p2z[i] = p2->z[i];
}
hookup(p1, p2, tr->currentZQR, numBranches);
p->next->next->back = p->next->back = (node *) NULL;
for(i = 0; i < numBranches; i++)
{
qz[i] = q->z[i];
pz[i] = p->z[i];
}
r = q->back;
s = p->back;
if(tr->thoroughInsertion)
{
hookup(p->next, q, tr->currentLZQ, numBranches);
hookup(p->next->next, r, tr->currentLZR, numBranches);
hookup(p, s, tr->currentLZS, numBranches);
}
else
{
double z[PLL_NUM_BRANCHES];
for(i = 0; i < numBranches; i++)
{
z[i] = sqrt(q->z[i]);
if(z[i] < PLL_ZMIN)
z[i] = PLL_ZMIN;
if(z[i] > PLL_ZMAX)
z[i] = PLL_ZMAX;
}
hookup(p->next, q, z, numBranches);
hookup(p->next->next, r, z, numBranches);
}
tr->likelihood = tr->bestOfNode;
saveBestTree(bt, tr, numBranches);
tr->likelihood = currentLH;
hookup(q, r, qz, numBranches);
p->next->next->back = p->next->back = (nodeptr) NULL;
if(tr->thoroughInsertion)
hookup(p, s, pz, numBranches);
hookup(p->next, p1, p1z, numBranches);
hookup(p->next->next, p2, p2z, numBranches);
}
/** @brief Test the
*/
pllBoolean testInsertBIG (pllInstance *tr, partitionList *pr, nodeptr p, nodeptr q)
{
int numBranches = pr->perGeneBranchLengths?pr->numberOfPartitions:1;
double qz[PLL_NUM_BRANCHES], pz[PLL_NUM_BRANCHES];
nodeptr r;
double startLH = tr->endLH;
int i;
r = q->back;
for(i = 0; i < numBranches; i++)
{
qz[i] = q->z[i];
pz[i] = p->z[i];
}
if (! insertBIG(tr, pr, p, q)) return PLL_FALSE;
pllEvaluateLikelihood (tr, pr, p->next->next, PLL_FALSE, PLL_FALSE);
if(tr->likelihood > tr->bestOfNode)
{
tr->bestOfNode = tr->likelihood;
tr->insertNode = q;
tr->removeNode = p;
for(i = 0; i < numBranches; i++)
{
tr->currentZQR[i] = tr->zqr[i];
tr->currentLZR[i] = tr->lzr[i];
tr->currentLZQ[i] = tr->lzq[i];
tr->currentLZS[i] = tr->lzs[i];
}
}
if(tr->likelihood > tr->endLH)
{
tr->insertNode = q;
tr->removeNode = p;
for(i = 0; i < numBranches; i++)
tr->currentZQR[i] = tr->zqr[i];
tr->endLH = tr->likelihood;
}
/* reset the topology so that it is the same as it was before calling insertBIG */
hookup(q, r, qz, numBranches);
p->next->next->back = p->next->back = (nodeptr) NULL;
if(tr->thoroughInsertion)
{
nodeptr s = p->back;
hookup(p, s, pz, numBranches);
}
if((tr->doCutoff) && (tr->likelihood < startLH))
{
tr->lhAVG += (startLH - tr->likelihood);
tr->lhDEC++;
if((startLH - tr->likelihood) >= tr->lhCutoff)
return PLL_FALSE;
else
return PLL_TRUE;
}
else
return PLL_TRUE;
}
/** @brief Recursively traverse tree and test insertion
Recursively traverses the tree structure starting from node \a q and
tests the insertion of the component specified by leaf \a p at the edge
between \a q and \a q->back.
@param tr
PLL instance
@param pr
List of partitions
@param p
Leaf node of one tree component
@param q
Endpoint node of the edge to test the insertion
@param mintrav
Minimum radius around \a q to test the insertion
@param maxtrav
Maximum radius around \a q to test the insertion\
*/
void addTraverseBIG(pllInstance *tr, partitionList *pr, nodeptr p, nodeptr q, int mintrav, int maxtrav)
{
if (--mintrav <= 0)
{
if (! testInsertBIG(tr, pr, p, q)) return;
}
if ((!isTip(q->number, tr->mxtips)) && (--maxtrav > 0))
{
addTraverseBIG(tr, pr, p, q->next->back, mintrav, maxtrav);
addTraverseBIG(tr, pr, p, q->next->next->back, mintrav, maxtrav);
}
}
/** @brief Compute the best SPR movement
Compute all SPR moves starting from \a p in the space defined by \a mintrav and
\a maxtrav and store the best in the \a tr structure.
@param tr
PLL instancve
@param pr
List of partitions