source: branches/profile/GDE/RAxML/searchAlgo.c

Last change on this file was 10409, checked in by aboeckma, 11 years ago

Updated raxml to current version

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 36.1 KB
Line 
1/*  RAxML-VI-HPC (version 2.2) a program for sequential and parallel estimation of phylogenetic trees
2 *  Copyright August 2006 by Alexandros Stamatakis
3 *
4 *  Partially derived from
5 *  fastDNAml, a program for estimation of phylogenetic trees from sequences by Gary J. Olsen
6 * 
7 *  and
8 *
9 *  Programs of the PHYLIP package by Joe Felsenstein.
10 *
11 *  This program is free software; you may redistribute it and/or modify its
12 *  under the terms of the GNU General Public License as published by the Free
13 *  Software Foundation; either version 2 of the License, or (at your option)
14 *  any later version.
15 *
16 *  This program is distributed in the hope that it will be useful, but
17 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 *  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19 *  for more details.
20 *
21 *
22 *  For any other enquiries send an Email to Alexandros Stamatakis
23 *  Alexandros.Stamatakis@epfl.ch
24 *
25 *  When publishing work that is based on the results from RAxML-VI-HPC please cite:
26 *
27 *  Alexandros Stamatakis:"RAxML-VI-HPC: maximum likelihood-based phylogenetic analyses with thousands of taxa and mixed models".
28 *  Bioinformatics 2006; doi: 10.1093/bioinformatics/btl446
29 */
30
31#ifndef WIN32
32#include <sys/times.h>
33#include <sys/types.h>
34#include <sys/time.h>
35#include <unistd.h>
36#endif
37
38#include <math.h>
39#include <time.h>
40#include <stdlib.h>
41#include <stdio.h>
42#include <ctype.h>
43#include <string.h>
44
45
46#include "axml.h"
47
48
49
50extern int Thorough;
51extern infoList iList;
52extern char seq_file[1024];
53extern char resultFileName[1024];
54extern char tree_file[1024];
55extern char workdir[1024];
56extern char run_id[128];
57extern FILE *INFILE;
58extern double masterTime;
59
60
61
62boolean initrav (tree *tr, nodeptr p)
63{ 
64  nodeptr  q;
65 
66  if (!isTip(p->number, tr->rdta->numsp)) 
67    {     
68      q = p->next;
69     
70      do 
71        {         
72          if (! initrav(tr, q->back))  return FALSE;               
73          q = q->next; 
74        } 
75      while (q != p); 
76     
77      newviewGeneric(tr, p);
78    }
79 
80  return TRUE;
81} 
82
83
84
85
86
87
88
89
90
91//#define _DEBUG_UPDATE
92
93
94boolean update(tree *tr, nodeptr p)
95{       
96  nodeptr  q; 
97  boolean smoothedPartitions[NUM_BRANCHES];
98  int i;
99  double   z[NUM_BRANCHES], z0[NUM_BRANCHES];
100  double _deltaz;
101
102#ifdef _DEBUG_UPDATE
103  double 
104    startLH;
105
106  evaluateGeneric(tr, p);
107
108  startLH = tr->likelihood;
109#endif
110
111  q = p->back;   
112
113  for(i = 0; i < tr->numBranches; i++)
114    z0[i] = q->z[i];   
115
116  if(tr->numBranches > 1)
117    makenewzGeneric(tr, p, q, z0, newzpercycle, z, TRUE); 
118  else
119    makenewzGeneric(tr, p, q, z0, newzpercycle, z, FALSE);
120 
121  for(i = 0; i < tr->numBranches; i++)   
122    smoothedPartitions[i]  = tr->partitionSmoothed[i];
123     
124  for(i = 0; i < tr->numBranches; i++)
125    {         
126      if(!tr->partitionConverged[i])
127        {         
128            _deltaz = deltaz;
129           
130          if(ABS(z[i] - z0[i]) > _deltaz) 
131            {         
132              smoothedPartitions[i] = FALSE;       
133            }   
134                 
135          p->z[i] = q->z[i] = z[i];     
136        }
137    }
138 
139#ifdef _DEBUG_UPDATE
140  evaluateGeneric(tr, p);
141
142  if(tr->likelihood <= startLH)
143    {
144      if(fabs(tr->likelihood - startLH) > 0.01)
145        {
146          printf("%f %f\n", startLH, tr->likelihood);
147          assert(0);     
148        }
149    }
150#endif
151
152  for(i = 0; i < tr->numBranches; i++)   
153    tr->partitionSmoothed[i]  = smoothedPartitions[i];
154 
155  return TRUE;
156}
157
158
159
160
161boolean smooth (tree *tr, nodeptr p)
162{
163  nodeptr  q;
164 
165  if (! update(tr, p))               return FALSE; /*  Adjust branch */
166  if (! isTip(p->number, tr->rdta->numsp)) 
167    {                                  /*  Adjust descendants */
168      q = p->next;
169      while (q != p) 
170        {
171          if (! smooth(tr, q->back))   return FALSE;
172          q = q->next;
173        }       
174     
175      if(tr->multiBranch)                 
176        newviewGenericMasked(tr, p);   
177      else
178        newviewGeneric(tr, p);     
179    }
180 
181  return TRUE;
182} 
183
184static boolean allSmoothed(tree *tr)
185{
186  int i;
187  boolean result = TRUE;
188 
189  for(i = 0; i < tr->numBranches; i++)
190    {
191      if(tr->partitionSmoothed[i] == FALSE)
192        result = FALSE;
193      else
194        tr->partitionConverged[i] = TRUE;
195    }
196
197  return result;
198}
199
200
201
202boolean smoothTree (tree *tr, int maxtimes)
203{
204  nodeptr  p, q;   
205  int i, count = 0;
206   
207  p = tr->start;
208  for(i = 0; i < tr->numBranches; i++)
209    tr->partitionConverged[i] = FALSE;
210
211  while (--maxtimes >= 0) 
212    {   
213      for(i = 0; i < tr->numBranches; i++)     
214        tr->partitionSmoothed[i] = TRUE;               
215
216      if (! smooth(tr, p->back))       return FALSE;
217      if (!isTip(p->number, tr->rdta->numsp)) 
218        {
219          q = p->next;
220          while (q != p) 
221            {
222              if (! smooth(tr, q->back))   return FALSE;
223              q = q->next;
224            }
225        }
226         
227      count++;
228
229      if (allSmoothed(tr)) 
230        break;     
231    }
232
233  for(i = 0; i < tr->numBranches; i++)
234    tr->partitionConverged[i] = FALSE;
235
236
237
238  return TRUE;
239} 
240
241
242
243boolean localSmooth (tree *tr, nodeptr p, int maxtimes)
244{ 
245  nodeptr  q;
246  int i;
247 
248  if (isTip(p->number, tr->rdta->numsp)) return FALSE;
249 
250   for(i = 0; i < tr->numBranches; i++) 
251     tr->partitionConverged[i] = FALSE; 
252
253  while (--maxtimes >= 0) 
254    {     
255      for(i = 0; i < tr->numBranches; i++)     
256        tr->partitionSmoothed[i] = TRUE;
257               
258      q = p;
259      do 
260        {
261          if (! update(tr, q)) return FALSE;
262          q = q->next;
263        } 
264      while (q != p);
265     
266      if (allSmoothed(tr)) 
267        break;
268    }
269
270  for(i = 0; i < tr->numBranches; i++)
271    {
272      tr->partitionSmoothed[i] = FALSE; 
273      tr->partitionConverged[i] = FALSE;
274    }
275
276  return TRUE;
277}
278
279
280
281
282
283static void resetInfoList(void)
284{
285  int i;
286
287  iList.valid = 0;
288
289  for(i = 0; i < iList.n; i++)   
290    {
291      iList.list[i].node = (nodeptr)NULL;
292      iList.list[i].likelihood = unlikely;
293    }   
294}
295
296void initInfoList(int n)
297{
298  int i;
299
300  iList.n = n;
301  iList.valid = 0;
302  iList.list = (bestInfo *)rax_malloc(sizeof(bestInfo) * n);
303
304  for(i = 0; i < n; i++)
305    {
306      iList.list[i].node = (nodeptr)NULL;
307      iList.list[i].likelihood = unlikely;
308    }
309}
310
311void freeInfoList(void)
312{ 
313  rax_free(iList.list);   
314}
315
316
317void insertInfoList(nodeptr node, double likelihood)
318{
319  int i;
320  int min = 0;
321  double min_l =  iList.list[0].likelihood;
322
323  for(i = 1; i < iList.n; i++)
324    {
325      if(iList.list[i].likelihood < min_l)
326        {
327          min = i;
328          min_l = iList.list[i].likelihood;
329        }
330    }
331
332  if(likelihood > min_l)
333    {
334      iList.list[min].likelihood = likelihood;
335      iList.list[min].node = node;
336      iList.valid += 1;
337    }
338
339  if(iList.valid > iList.n)
340    iList.valid = iList.n;
341}
342
343
344boolean smoothRegion (tree *tr, nodeptr p, int region)
345{ 
346  nodeptr  q;
347 
348  if (! update(tr, p))               return FALSE; /*  Adjust branch */
349
350  if(region > 0)
351    {
352      if (!isTip(p->number, tr->rdta->numsp)) 
353        {                                 
354          q = p->next;
355          while (q != p) 
356            {
357              if (! smoothRegion(tr, q->back, --region))   return FALSE;
358              q = q->next;
359            }   
360         
361            newviewGeneric(tr, p);
362        }
363    }
364 
365  return TRUE;
366}
367
368boolean regionalSmooth (tree *tr, nodeptr p, int maxtimes, int region)
369  {
370    nodeptr  q;
371    int i;
372
373    if (isTip(p->number, tr->rdta->numsp)) return FALSE;            /* Should be an error */
374
375    for(i = 0; i < tr->numBranches; i++)
376      tr->partitionConverged[i] = FALSE;
377
378    while (--maxtimes >= 0) 
379      { 
380        for(i = 0; i < tr->numBranches; i++)     
381          tr->partitionSmoothed[i] = TRUE;
382         
383        q = p;
384        do 
385          {
386            if (! smoothRegion(tr, q, region)) return FALSE;
387            q = q->next;
388          } 
389        while (q != p);
390       
391        if (allSmoothed(tr)) 
392          break;
393      }
394
395    for(i = 0; i < tr->numBranches; i++)
396      tr->partitionSmoothed[i] = FALSE;
397    for(i = 0; i < tr->numBranches; i++)
398      tr->partitionConverged[i] = FALSE;
399   
400    return TRUE;
401  } /* localSmooth */
402
403
404
405
406
407nodeptr  removeNodeBIG (tree *tr, nodeptr p, int numBranches)
408{ 
409  double   zqr[NUM_BRANCHES], result[NUM_BRANCHES];
410  nodeptr  q, r;
411  int i;
412       
413  q = p->next->back;
414  r = p->next->next->back;
415 
416  for(i = 0; i < numBranches; i++)
417    zqr[i] = q->z[i] * r->z[i];       
418   
419  makenewzGeneric(tr, q, r, zqr, iterations, result, FALSE);   
420
421  for(i = 0; i < numBranches; i++)       
422    tr->zqr[i] = result[i];
423
424  hookup(q, r, result, numBranches); 
425     
426  p->next->next->back = p->next->back = (node *) NULL;
427
428  return  q; 
429}
430
431nodeptr  removeNodeRestoreBIG (tree *tr, nodeptr p)
432{
433  nodeptr  q, r;
434       
435  q = p->next->back;
436  r = p->next->next->back; 
437
438  newviewGeneric(tr, q);
439  newviewGeneric(tr, r);
440 
441  hookup(q, r, tr->currentZQR, tr->numBranches);
442
443  p->next->next->back = p->next->back = (node *) NULL;
444     
445  return  q;
446}
447
448
449boolean insertBIG (tree *tr, nodeptr p, nodeptr q, int numBranches)
450{
451  nodeptr  r, s;
452  int i;
453 
454  r = q->back;
455  s = p->back;
456     
457  for(i = 0; i < numBranches; i++)
458    tr->lzi[i] = q->z[i];
459 
460  if(Thorough)
461    { 
462      double  zqr[NUM_BRANCHES], zqs[NUM_BRANCHES], zrs[NUM_BRANCHES], lzqr, lzqs, lzrs, lzsum, lzq, lzr, lzs, lzmax;     
463      double defaultArray[NUM_BRANCHES];       
464      double e1[NUM_BRANCHES], e2[NUM_BRANCHES], e3[NUM_BRANCHES];
465      double *qz;
466     
467      qz = q->z;
468     
469      for(i = 0; i < numBranches; i++)
470        defaultArray[i] = defaultz;
471     
472      makenewzGeneric(tr, q, r, qz, iterations, zqr, FALSE);           
473      makenewzGeneric(tr, q, s, defaultArray, iterations, zqs, FALSE);                 
474      makenewzGeneric(tr, r, s, defaultArray, iterations, zrs, FALSE);
475     
476     
477      for(i = 0; i < numBranches; i++)
478        {
479          lzqr = (zqr[i] > zmin) ? log(zqr[i]) : log(zmin); 
480          lzqs = (zqs[i] > zmin) ? log(zqs[i]) : log(zmin);
481          lzrs = (zrs[i] > zmin) ? log(zrs[i]) : log(zmin);
482          lzsum = 0.5 * (lzqr + lzqs + lzrs);
483         
484          lzq = lzsum - lzrs;
485          lzr = lzsum - lzqs;
486          lzs = lzsum - lzqr;
487          lzmax = log(zmax);
488         
489          if      (lzq > lzmax) {lzq = lzmax; lzr = lzqr; lzs = lzqs;} 
490          else if (lzr > lzmax) {lzr = lzmax; lzq = lzqr; lzs = lzrs;}
491          else if (lzs > lzmax) {lzs = lzmax; lzq = lzqs; lzr = lzrs;}         
492         
493          e1[i] = exp(lzq);
494          e2[i] = exp(lzr);
495          e3[i] = exp(lzs);
496        }
497      hookup(p->next,       q, e1, numBranches);
498      hookup(p->next->next, r, e2, numBranches);
499      hookup(p,             s, e3, numBranches);                 
500    }
501  else
502    {       
503      double  z[NUM_BRANCHES]; 
504     
505      for(i = 0; i < numBranches; i++)
506        {
507          z[i] = sqrt(q->z[i]);     
508         
509          if(z[i] < zmin) 
510            z[i] = zmin;
511          if(z[i] > zmax)
512            z[i] = zmax;
513        }
514     
515      hookup(p->next,       q, z, tr->numBranches);
516      hookup(p->next->next, r, z, tr->numBranches);                             
517    }
518 
519  newviewGeneric(tr, p);
520 
521  if(Thorough)
522    {     
523      localSmooth(tr, p, smoothings);   
524      for(i = 0; i < numBranches; i++)
525        {
526          tr->lzq[i] = p->next->z[i];
527          tr->lzr[i] = p->next->next->z[i];
528          tr->lzs[i] = p->z[i];           
529        }
530    }           
531 
532  return  TRUE;
533}
534
535boolean insertRestoreBIG (tree *tr, nodeptr p, nodeptr q)
536{
537  nodeptr  r, s;
538 
539  r = q->back;
540  s = p->back;
541
542  if(Thorough)
543    {                       
544      hookup(p->next,       q, tr->currentLZQ, tr->numBranches);
545      hookup(p->next->next, r, tr->currentLZR, tr->numBranches);
546      hookup(p,             s, tr->currentLZS, tr->numBranches);                 
547    }
548  else
549    {       
550      double  z[NUM_BRANCHES];
551      int i;
552     
553      for(i = 0; i < tr->numBranches; i++)
554        {
555          double zz;
556          zz = sqrt(q->z[i]);     
557          if(zz < zmin) 
558            zz = zmin;
559          if(zz > zmax)
560            zz = zmax;
561          z[i] = zz;
562        }
563
564      hookup(p->next,       q, z, tr->numBranches);
565      hookup(p->next->next, r, z, tr->numBranches);
566    }   
567   
568  newviewGeneric(tr, p);
569       
570  return  TRUE;
571}
572
573
574static void restoreTopologyOnly(tree *tr, bestlist *bt)
575{ 
576  nodeptr p = tr->removeNode;
577  nodeptr q = tr->insertNode;
578  double qz[NUM_BRANCHES], pz[NUM_BRANCHES], p1z[NUM_BRANCHES], p2z[NUM_BRANCHES];
579  nodeptr p1, p2, r, s;
580  double currentLH = tr->likelihood;
581  int i;
582     
583  p1 = p->next->back;
584  p2 = p->next->next->back;
585 
586  for(i = 0; i < tr->numBranches; i++)
587    {
588      p1z[i] = p1->z[i];
589      p2z[i] = p2->z[i];
590    }
591 
592  hookup(p1, p2, tr->currentZQR, tr->numBranches);
593 
594  p->next->next->back = p->next->back = (node *) NULL;             
595  for(i = 0; i < tr->numBranches; i++)
596    {
597      qz[i] = q->z[i];
598      pz[i] = p->z[i];           
599    }
600 
601  r = q->back;
602  s = p->back;
603 
604  if(Thorough)
605    {                       
606      hookup(p->next,       q, tr->currentLZQ, tr->numBranches);
607      hookup(p->next->next, r, tr->currentLZR, tr->numBranches);
608      hookup(p,             s, tr->currentLZS, tr->numBranches);                 
609    }
610  else
611    {   
612      double  z[NUM_BRANCHES]; 
613      for(i = 0; i < tr->numBranches; i++)
614        {
615          z[i] = sqrt(q->z[i]);     
616          if(z[i] < zmin)
617            z[i] = zmin;
618          if(z[i] > zmax)
619            z[i] = zmax;
620        }
621      hookup(p->next,       q, z, tr->numBranches);
622      hookup(p->next->next, r, z, tr->numBranches);
623    }     
624 
625  tr->likelihood = tr->bestOfNode;
626   
627  saveBestTree(bt, tr);
628 
629  tr->likelihood = currentLH;
630 
631  hookup(q, r, qz, tr->numBranches);
632 
633  p->next->next->back = p->next->back = (nodeptr) NULL;
634 
635  if(Thorough)   
636    hookup(p, s, pz, tr->numBranches);         
637     
638  hookup(p->next,       p1, p1z, tr->numBranches); 
639  hookup(p->next->next, p2, p2z, tr->numBranches);     
640}
641
642
643boolean testInsertBIG (tree *tr, nodeptr p, nodeptr q)
644{
645  double  qz[NUM_BRANCHES], pz[NUM_BRANCHES];
646  nodeptr  r;
647  boolean doIt = TRUE;
648  double startLH = tr->endLH;
649  int i;
650 
651  r = q->back; 
652  for(i = 0; i < tr->numBranches; i++)
653    {
654      qz[i] = q->z[i];
655      pz[i] = p->z[i];
656    }
657 
658  if(tr->grouped)
659    {
660      int rNumber, qNumber, pNumber;
661     
662      doIt = FALSE;
663     
664      rNumber = tr->constraintVector[r->number];
665      qNumber = tr->constraintVector[q->number];
666      pNumber = tr->constraintVector[p->number];
667     
668      if(pNumber == -9)
669        pNumber = checker(tr, p->back);
670      if(pNumber == -9)
671        doIt = TRUE;
672      else
673        {
674          if(qNumber == -9)
675            qNumber = checker(tr, q);
676         
677          if(rNumber == -9)
678            rNumber = checker(tr, r);
679         
680          if(pNumber == rNumber || pNumber == qNumber)
681            doIt = TRUE;         
682        }
683    }
684 
685  if(doIt)
686    {     
687      if (! insertBIG(tr, p, q, tr->numBranches))       return FALSE;         
688     
689      evaluateGeneric(tr, p->next->next);   
690       
691      if(tr->likelihood > tr->bestOfNode)
692        {
693          tr->bestOfNode = tr->likelihood;
694          tr->insertNode = q;
695          tr->removeNode = p;   
696          for(i = 0; i < tr->numBranches; i++)
697            {
698              tr->currentZQR[i] = tr->zqr[i];           
699              tr->currentLZR[i] = tr->lzr[i];
700              tr->currentLZQ[i] = tr->lzq[i];
701              tr->currentLZS[i] = tr->lzs[i];     
702            }
703        }
704     
705      if(tr->likelihood > tr->endLH)
706        {                         
707          tr->insertNode = q;
708          tr->removeNode = p;   
709          for(i = 0; i < tr->numBranches; i++)
710            tr->currentZQR[i] = tr->zqr[i];     
711          tr->endLH = tr->likelihood;                     
712        }       
713     
714      hookup(q, r, qz, tr->numBranches);
715     
716      p->next->next->back = p->next->back = (nodeptr) NULL;
717     
718      if(Thorough)
719        {
720          nodeptr s = p->back;
721          hookup(p, s, pz, tr->numBranches);     
722        } 
723     
724      if((tr->doCutoff) && (tr->likelihood < startLH))
725        {
726          tr->lhAVG += (startLH - tr->likelihood);
727          tr->lhDEC++;
728          if((startLH - tr->likelihood) >= tr->lhCutoff)
729            return FALSE;           
730          else
731            return TRUE;
732        }
733      else
734        return TRUE;
735    }
736  else
737    return TRUE; 
738}
739
740
741
742 
743void addTraverseBIG(tree *tr, nodeptr p, nodeptr q, int mintrav, int maxtrav)
744{ 
745  if (--mintrav <= 0) 
746    {             
747      if (! testInsertBIG(tr, p, q))  return;
748
749    }
750 
751  if ((!isTip(q->number, tr->rdta->numsp)) && (--maxtrav > 0)) 
752    {   
753      addTraverseBIG(tr, p, q->next->back, mintrav, maxtrav);
754      addTraverseBIG(tr, p, q->next->next->back, mintrav, maxtrav);   
755    }
756} 
757
758
759
760
761
762int rearrangeBIG(tree *tr, nodeptr p, int mintrav, int maxtrav)   
763{ 
764  double   p1z[NUM_BRANCHES], p2z[NUM_BRANCHES], q1z[NUM_BRANCHES], q2z[NUM_BRANCHES];
765  nodeptr  p1, p2, q, q1, q2;
766  int      mintrav2, i; 
767  boolean doP = TRUE, doQ = TRUE;
768 
769  if (maxtrav < 1 || mintrav > maxtrav)  return 0;
770  q = p->back;
771 
772  if(tr->constrained)
773    {
774      if(! tipHomogeneityChecker(tr, p->back, 0))
775        doP = FALSE;
776     
777      if(! tipHomogeneityChecker(tr, q->back, 0))
778        doQ = FALSE;
779     
780      if(doQ == FALSE && doP == FALSE)
781        return 0;
782    }
783 
784  if (!isTip(p->number, tr->rdta->numsp) && doP) 
785    {     
786      p1 = p->next->back;
787      p2 = p->next->next->back;
788     
789     
790      if(!isTip(p1->number, tr->rdta->numsp) || !isTip(p2->number, tr->rdta->numsp))
791        {
792          for(i = 0; i < tr->numBranches; i++)
793            {
794              p1z[i] = p1->z[i];
795              p2z[i] = p2->z[i];                   
796            }
797         
798          if (! removeNodeBIG(tr, p,  tr->numBranches)) return badRear;
799         
800          if (!isTip(p1->number, tr->rdta->numsp)) 
801            {
802              addTraverseBIG(tr, p, p1->next->back,
803                             mintrav, maxtrav);         
804              addTraverseBIG(tr, p, p1->next->next->back,
805                             mintrav, maxtrav);         
806            }
807         
808          if (!isTip(p2->number, tr->rdta->numsp)) 
809            {
810              addTraverseBIG(tr, p, p2->next->back,
811                             mintrav, maxtrav);
812              addTraverseBIG(tr, p, p2->next->next->back,
813                             mintrav, maxtrav);         
814            }
815                 
816          hookup(p->next,       p1, p1z, tr->numBranches); 
817          hookup(p->next->next, p2, p2z, tr->numBranches);                         
818          newviewGeneric(tr, p);                   
819        }
820    } 
821 
822  if (!isTip(q->number, tr->rdta->numsp) && maxtrav > 0 && doQ) 
823    {
824      q1 = q->next->back;
825      q2 = q->next->next->back;
826     
827      /*if (((!q1->tip) && (!q1->next->back->tip || !q1->next->next->back->tip)) ||
828        ((!q2->tip) && (!q2->next->back->tip || !q2->next->next->back->tip))) */
829      if (
830          (
831           ! isTip(q1->number, tr->rdta->numsp) && 
832           (! isTip(q1->next->back->number, tr->rdta->numsp) || ! isTip(q1->next->next->back->number, tr->rdta->numsp))
833           )
834          ||
835          (
836           ! isTip(q2->number, tr->rdta->numsp) && 
837           (! isTip(q2->next->back->number, tr->rdta->numsp) || ! isTip(q2->next->next->back->number, tr->rdta->numsp))
838           )
839          )
840        {
841         
842          for(i = 0; i < tr->numBranches; i++)
843            {
844              q1z[i] = q1->z[i];
845              q2z[i] = q2->z[i];
846            }
847         
848          if (! removeNodeBIG(tr, q, tr->numBranches)) return badRear;
849         
850          mintrav2 = mintrav > 2 ? mintrav : 2;
851         
852          if (/*! q1->tip*/ !isTip(q1->number, tr->rdta->numsp)) 
853            {
854              addTraverseBIG(tr, q, q1->next->back,
855                             mintrav2 , maxtrav);
856              addTraverseBIG(tr, q, q1->next->next->back,
857                             mintrav2 , maxtrav);         
858            }
859         
860          if (/*! q2->tip*/ ! isTip(q2->number, tr->rdta->numsp)) 
861            {
862              addTraverseBIG(tr, q, q2->next->back,
863                             mintrav2 , maxtrav);
864              addTraverseBIG(tr, q, q2->next->next->back,
865                             mintrav2 , maxtrav);         
866            }     
867         
868          hookup(q->next,       q1, q1z, tr->numBranches); 
869          hookup(q->next->next, q2, q2z, tr->numBranches);
870         
871          newviewGeneric(tr, q);           
872        }
873    } 
874 
875  return  1;
876} 
877
878
879
880
881
882double treeOptimizeRapid(tree *tr, int mintrav, int maxtrav, analdef *adef, bestlist *bt)
883{
884  int i, index,
885    *perm = (int*)NULL;   
886
887  nodeRectifier(tr);
888
889  if (maxtrav > tr->ntips - 3) 
890    maxtrav = tr->ntips - 3; 
891   
892  resetInfoList();
893 
894  resetBestTree(bt);
895 
896  tr->startLH = tr->endLH = tr->likelihood;
897 
898  if(tr->doCutoff)
899    {
900      if(tr->bigCutoff)
901        {         
902          if(tr->itCount == 0)   
903            tr->lhCutoff = 0.5 * (tr->likelihood / -1000.0);   
904          else                   
905            tr->lhCutoff = 0.5 * ((tr->lhAVG) / ((double)(tr->lhDEC)));           
906        }
907      else
908        {
909          if(tr->itCount == 0)   
910            tr->lhCutoff = tr->likelihood / -1000.0;   
911          else                   
912            tr->lhCutoff = (tr->lhAVG) / ((double)(tr->lhDEC));   
913        }   
914
915      tr->itCount = tr->itCount + 1;
916      tr->lhAVG = 0;
917      tr->lhDEC = 0;
918    }
919 
920  if(adef->permuteTreeoptimize)
921    {
922      int n = tr->mxtips + tr->mxtips - 2;   
923      perm = (int *)rax_malloc(sizeof(int) * (n + 1));
924      makePermutation(perm, n, adef);
925    }
926
927  for(i = 1; i <= tr->mxtips + tr->mxtips - 2; i++)
928    {           
929      tr->bestOfNode = unlikely;         
930
931      if(adef->permuteTreeoptimize)
932        index = perm[i];
933      else
934        index = i;
935     
936
937
938      if(rearrangeBIG(tr, tr->nodep[index], mintrav, maxtrav))
939        {   
940          if(Thorough)
941            {
942              if(tr->endLH > tr->startLH)                       
943                {                                   
944                  restoreTreeFast(tr);           
945                  tr->startLH = tr->endLH = tr->likelihood;     
946                  saveBestTree(bt, tr);
947                }
948              else
949                {                 
950                  if(tr->bestOfNode != unlikely)                             
951                    restoreTopologyOnly(tr, bt);                   
952                }         
953            }
954          else
955            {
956              insertInfoList(tr->nodep[index], tr->bestOfNode);     
957              if(tr->endLH > tr->startLH)                       
958                {                     
959                  restoreTreeFast(tr);               
960                  tr->startLH = tr->endLH = tr->likelihood;                                                               
961                }                 
962            }
963        }     
964    }     
965
966  if(!Thorough)
967    {           
968      Thorough = 1; 
969     
970      for(i = 0; i < iList.valid; i++)
971        { 
972         
973
974          tr->bestOfNode = unlikely;
975         
976          if(rearrangeBIG(tr, iList.list[i].node, mintrav, maxtrav))
977            {     
978              if(tr->endLH > tr->startLH)                       
979                {                   
980                  restoreTreeFast(tr);           
981                  tr->startLH = tr->endLH = tr->likelihood;     
982                  saveBestTree(bt, tr);
983                }
984              else
985                { 
986             
987                  if(tr->bestOfNode != unlikely)
988                    {       
989                      restoreTopologyOnly(tr, bt);
990                    }   
991                }     
992            }
993        }       
994         
995      Thorough = 0;
996    }
997
998  if(adef->permuteTreeoptimize)
999    rax_free(perm);
1000
1001  return tr->startLH;     
1002}
1003
1004
1005
1006
1007boolean testInsertRestoreBIG (tree *tr, nodeptr p, nodeptr q)
1008{   
1009  if(Thorough)
1010    {
1011      if (! insertBIG(tr, p, q, tr->numBranches))       return FALSE;   
1012     
1013      evaluateGeneric(tr, p->next->next);               
1014    }
1015  else
1016    {
1017      if (! insertRestoreBIG(tr, p, q))       return FALSE;
1018     
1019      {
1020        nodeptr x, y;
1021        x = p->next->next;
1022        y = p->back;
1023                       
1024        if(! isTip(x->number, tr->rdta->numsp) && isTip(y->number, tr->rdta->numsp))
1025          {
1026            while ((! x->x)) 
1027              {
1028                if (! (x->x))
1029                  newviewGeneric(tr, x);                     
1030              }
1031          }
1032       
1033        if(isTip(x->number, tr->rdta->numsp) && !isTip(y->number, tr->rdta->numsp))
1034          {
1035            while ((! y->x)) 
1036              {           
1037                if (! (y->x))
1038                  newviewGeneric(tr, y);
1039              }
1040          }
1041       
1042        if(!isTip(x->number, tr->rdta->numsp) && !isTip(y->number, tr->rdta->numsp))
1043          {
1044            while ((! x->x) || (! y->x)) 
1045              {
1046                if (! (x->x))
1047                  newviewGeneric(tr, x);
1048                if (! (y->x))
1049                  newviewGeneric(tr, y);
1050              }
1051          }                                     
1052       
1053      }
1054       
1055      tr->likelihood = tr->endLH;
1056    }
1057     
1058  return TRUE;
1059} 
1060
1061void restoreTreeFast(tree *tr)
1062{
1063  removeNodeRestoreBIG(tr, tr->removeNode);   
1064  testInsertRestoreBIG(tr, tr->removeNode, tr->insertNode);
1065}
1066
1067
1068int determineRearrangementSetting(tree *tr,  analdef *adef, bestlist *bestT, bestlist *bt)
1069{
1070  int i, mintrav, maxtrav, bestTrav, impr, index, MaxFast,
1071    *perm = (int*)NULL;
1072  double startLH; 
1073  boolean cutoff; 
1074
1075  MaxFast = 26;
1076
1077  startLH = tr->likelihood;
1078
1079  cutoff = tr->doCutoff;
1080  tr->doCutoff = FALSE;
1081 
1082   
1083  mintrav = 1;
1084  maxtrav = 5;
1085
1086  bestTrav = maxtrav = 5;
1087
1088  impr = 1;
1089
1090  resetBestTree(bt);
1091
1092  if(adef->permuteTreeoptimize)
1093    {
1094      int n = tr->mxtips + tr->mxtips - 2;   
1095      perm = (int *)rax_malloc(sizeof(int) * (n + 1));
1096      makePermutation(perm, n, adef);
1097    }
1098 
1099
1100  while(impr && maxtrav < MaxFast)
1101    {   
1102      recallBestTree(bestT, 1, tr);     
1103      nodeRectifier(tr);
1104     
1105     
1106      if (maxtrav > tr->ntips - 3) 
1107        maxtrav = tr->ntips - 3;   
1108 
1109      tr->startLH = tr->endLH = tr->likelihood;
1110         
1111      for(i = 1; i <= tr->mxtips + tr->mxtips - 2; i++)
1112        {               
1113
1114
1115
1116          if(adef->permuteTreeoptimize)
1117            index = perm[i];
1118          else
1119            index = i;           
1120
1121          tr->bestOfNode = unlikely;
1122          if(rearrangeBIG(tr, tr->nodep[index], mintrav, maxtrav))
1123            {       
1124              if(tr->endLH > tr->startLH)                       
1125                {                                     
1126                  restoreTreeFast(tr);                                               
1127                  tr->startLH = tr->endLH = tr->likelihood;                                                                           
1128                }                               
1129            }
1130        }
1131     
1132      treeEvaluate(tr, 0.25);
1133      saveBestTree(bt, tr);                                   
1134
1135      /*printf("DETERMINE_BEST: %d %f\n", maxtrav, tr->likelihood);*/
1136
1137      if(tr->likelihood > startLH)
1138        {       
1139          startLH = tr->likelihood;                       
1140          printLog(tr, adef, FALSE);     
1141          bestTrav = maxtrav;   
1142          impr = 1;
1143        }
1144      else
1145        {
1146          impr = 0;
1147        }
1148      maxtrav += 5;
1149     
1150      if(tr->doCutoff)
1151        {
1152          tr->lhCutoff = (tr->lhAVG) / ((double)(tr->lhDEC));       
1153 
1154          tr->itCount =  tr->itCount + 1;
1155          tr->lhAVG = 0;
1156          tr->lhDEC = 0;
1157        }
1158    }
1159
1160  recallBestTree(bt, 1, tr);   
1161  tr->doCutoff = cutoff;
1162
1163  if(adef->permuteTreeoptimize)
1164    rax_free(perm);
1165
1166 
1167  return bestTrav;     
1168}
1169
1170
1171
1172/*
1173  #define _TERRACES
1174 
1175  define _TERRACES to better explore trees on terraces
1176*/
1177
1178#ifdef _TERRACES
1179/* count the number of BS or ML tree search replicates */
1180
1181int bCount = 0;
1182#endif
1183
1184void computeBIGRAPID (tree *tr, analdef *adef, boolean estimateModel) 
1185{ 
1186  unsigned int
1187    vLength = 0;
1188  int
1189    i,
1190    impr, 
1191    bestTrav,
1192    rearrangementsMax = 0, 
1193    rearrangementsMin = 0,   
1194    thoroughIterations = 0,
1195    fastIterations = 0;
1196   
1197  double lh, previousLh, difference, epsilon;             
1198  bestlist *bestT, *bt; 
1199   
1200#ifdef _TERRACES
1201  /* store the 20 best trees found in a dedicated list */
1202
1203  bestlist
1204    *terrace;
1205 
1206  /* output file names */
1207
1208  char 
1209    terraceFileName[1024],
1210    buf[64];
1211#endif
1212
1213  hashtable *h = (hashtable*)NULL;
1214  unsigned int **bitVectors = (unsigned int**)NULL;
1215 
1216 
1217  if(tr->searchConvergenceCriterion)
1218    {         
1219      bitVectors = initBitVector(tr, &vLength);
1220      h = initHashTable(tr->mxtips * 4);   
1221    }
1222
1223  bestT = (bestlist *) rax_malloc(sizeof(bestlist));
1224  bestT->ninit = 0;
1225  initBestTree(bestT, 1, tr->mxtips);
1226     
1227  bt = (bestlist *) rax_malloc(sizeof(bestlist));     
1228  bt->ninit = 0;
1229  initBestTree(bt, 20, tr->mxtips); 
1230
1231#ifdef _TERRACES
1232  /* initialize the tree list and the output file name for the current tree search/replicate */
1233
1234
1235  terrace = (bestlist *) rax_malloc(sizeof(bestlist));     
1236  terrace->ninit = 0;
1237  initBestTree(terrace, 20, tr->mxtips); 
1238 
1239  sprintf(buf, "%d", bCount);
1240 
1241  strcpy(terraceFileName,         workdir);
1242  strcat(terraceFileName,         "RAxML_terrace.");
1243  strcat(terraceFileName,         run_id);
1244  strcat(terraceFileName,         ".BS.");
1245  strcat(terraceFileName,         buf);
1246 
1247  printf("%s\n", terraceFileName);
1248#endif
1249
1250  initInfoList(50);
1251 
1252  difference = 10.0;
1253  epsilon = 0.01;   
1254   
1255  Thorough = 0;     
1256 
1257  if(estimateModel)
1258    {
1259      if(adef->useBinaryModelFile)
1260        {
1261          readBinaryModel(tr);
1262          evaluateGenericInitrav(tr, tr->start);
1263          treeEvaluate(tr, 2);
1264        }
1265      else
1266        {
1267          evaluateGenericInitrav(tr, tr->start);
1268          modOpt(tr, adef, FALSE, 10.0);
1269        }
1270    }
1271  else
1272    treeEvaluate(tr, 2); 
1273
1274
1275  printLog(tr, adef, FALSE); 
1276
1277  saveBestTree(bestT, tr);
1278 
1279  if(!adef->initialSet)   
1280    bestTrav = adef->bestTrav = determineRearrangementSetting(tr, adef, bestT, bt);                   
1281  else
1282    bestTrav = adef->bestTrav = adef->initial;
1283
1284  if(estimateModel)
1285    {
1286      if(adef->useBinaryModelFile)     
1287        treeEvaluate(tr, 2);
1288      else
1289        {
1290          evaluateGenericInitrav(tr, tr->start);
1291          modOpt(tr, adef, FALSE, 5.0);
1292        }
1293    }
1294  else
1295    treeEvaluate(tr, 1);
1296 
1297  saveBestTree(bestT, tr); 
1298  impr = 1;
1299  if(tr->doCutoff)
1300    tr->itCount = 0;
1301
1302 
1303
1304  while(impr)
1305    {             
1306      recallBestTree(bestT, 1, tr); 
1307
1308      if(tr->searchConvergenceCriterion)
1309        {
1310          int bCounter = 0;           
1311         
1312          if(fastIterations > 1)
1313            cleanupHashTable(h, (fastIterations % 2));         
1314         
1315          bitVectorInitravSpecial(bitVectors, tr->nodep[1]->back, tr->mxtips, vLength, h, fastIterations % 2, BIPARTITIONS_RF, (branchInfo *)NULL,
1316                                  &bCounter, 1, FALSE, FALSE);     
1317         
1318          assert(bCounter == tr->mxtips - 3);             
1319         
1320          if(fastIterations > 0)
1321            {
1322              double rrf = convergenceCriterion(h, tr->mxtips);
1323             
1324              if(rrf <= 0.01) /* 1% cutoff */
1325                {
1326                  printBothOpen("ML fast search converged at fast SPR cycle %d with stopping criterion\n", fastIterations);
1327                  printBothOpen("Relative Robinson-Foulds (RF) distance between respective best trees after one succseful SPR cycle: %f%s\n", rrf, "%");
1328                  cleanupHashTable(h, 0);
1329                  cleanupHashTable(h, 1);
1330                  goto cleanup_fast;
1331                }
1332              else                 
1333                printBothOpen("ML search convergence criterion fast cycle %d->%d Relative Robinson-Foulds %f\n", fastIterations - 1, fastIterations, rrf);
1334            }
1335        }
1336
1337         
1338      fastIterations++; 
1339
1340
1341      treeEvaluate(tr, 1.0);
1342     
1343     
1344      saveBestTree(bestT, tr);           
1345      printLog(tr, adef, FALSE);         
1346      printResult(tr, adef, FALSE);   
1347      lh = previousLh = tr->likelihood;
1348   
1349     
1350      treeOptimizeRapid(tr, 1, bestTrav, adef, bt);   
1351     
1352      impr = 0;
1353         
1354      for(i = 1; i <= bt->nvalid; i++)
1355        {                                 
1356          recallBestTree(bt, i, tr);
1357         
1358          treeEvaluate(tr, 0.25);                                       
1359
1360          difference = ((tr->likelihood > previousLh)? 
1361                        tr->likelihood - previousLh: 
1362                        previousLh - tr->likelihood);       
1363          if(tr->likelihood > lh && difference > epsilon)
1364            {
1365              impr = 1;       
1366              lh = tr->likelihood;                   
1367              saveBestTree(bestT, tr);
1368            }             
1369        }       
1370    }
1371
1372 
1373
1374  if(tr->searchConvergenceCriterion)
1375    {
1376      cleanupHashTable(h, 0);
1377      cleanupHashTable(h, 1);
1378    }
1379
1380 cleanup_fast:
1381
1382  Thorough = 1;
1383  impr = 1;
1384 
1385  recallBestTree(bestT, 1, tr); 
1386  if(estimateModel)
1387    {
1388      if(adef->useBinaryModelFile)     
1389        treeEvaluate(tr, 2);
1390      else
1391        {
1392          evaluateGenericInitrav(tr, tr->start);
1393          modOpt(tr, adef, FALSE, 1.0);
1394        }
1395    }
1396  else
1397    treeEvaluate(tr, 1.0);
1398
1399  while(1)
1400    {   
1401      recallBestTree(bestT, 1, tr);   
1402      if(impr)
1403        {           
1404          printResult(tr, adef, FALSE);
1405          rearrangementsMin = 1;
1406          rearrangementsMax = adef->stepwidth; 
1407
1408         
1409
1410          if(tr->searchConvergenceCriterion)
1411            {
1412              int bCounter = 0;       
1413
1414              if(thoroughIterations > 1)
1415                cleanupHashTable(h, (thoroughIterations % 2));         
1416               
1417              bitVectorInitravSpecial(bitVectors, tr->nodep[1]->back, tr->mxtips, vLength, h, thoroughIterations % 2, BIPARTITIONS_RF, (branchInfo *)NULL,
1418                                      &bCounter, 1, FALSE, FALSE);         
1419             
1420              assert(bCounter == tr->mxtips - 3);                 
1421             
1422              if(thoroughIterations > 0)
1423                {
1424                  double rrf = convergenceCriterion(h, tr->mxtips);
1425                 
1426                  if(rrf <= 0.01) /* 1% cutoff */
1427                    {
1428                      printBothOpen("ML search converged at thorough SPR cycle %d with stopping criterion\n", thoroughIterations);
1429                      printBothOpen("Relative Robinson-Foulds (RF) distance between respective best trees after one succseful SPR cycle: %f%s\n", rrf, "%");
1430                      goto cleanup;
1431                    }
1432                  else             
1433                    printBothOpen("ML search convergence criterion thorough cycle %d->%d Relative Robinson-Foulds %f\n", thoroughIterations - 1, thoroughIterations, rrf);
1434                }
1435            }
1436
1437         
1438                 
1439          thoroughIterations++;   
1440        }                                               
1441      else
1442        {                         
1443          rearrangementsMax += adef->stepwidth;
1444          rearrangementsMin += adef->stepwidth;                               
1445          if(rearrangementsMax > adef->max_rearrange)                   
1446            goto cleanup;         
1447        }
1448      treeEvaluate(tr, 1.0);
1449     
1450      previousLh = lh = tr->likelihood;       
1451      saveBestTree(bestT, tr); 
1452     
1453      printLog(tr, adef, FALSE);
1454      treeOptimizeRapid(tr, rearrangementsMin, rearrangementsMax, adef, bt);
1455       
1456      impr = 0;                                             
1457
1458      for(i = 1; i <= bt->nvalid; i++)
1459        {               
1460          recallBestTree(bt, i, tr);                           
1461         
1462          treeEvaluate(tr, 0.25);               
1463       
1464#ifdef _TERRACES
1465          /* save all 20 best trees in the terrace tree list */
1466          saveBestTree(terrace, tr);
1467#endif
1468
1469          difference = ((tr->likelihood > previousLh)? 
1470                        tr->likelihood - previousLh: 
1471                        previousLh - tr->likelihood);       
1472          if(tr->likelihood > lh && difference > epsilon)
1473            {
1474              impr = 1;       
1475              lh = tr->likelihood;                   
1476              saveBestTree(bestT, tr);
1477            }             
1478        } 
1479
1480                     
1481    }
1482
1483 cleanup: 
1484
1485#ifdef _TERRACES
1486  {
1487    double
1488      bestLH = tr->likelihood;
1489    FILE 
1490      *f = myfopen(terraceFileName, "w");
1491   
1492    /* print out likelihood of best tree found */
1493
1494    printf("best tree: %f\n", tr->likelihood);
1495
1496    /* print out likelihoods of 20 best trees found during the tree search */
1497
1498    for(i = 1; i <= terrace->nvalid; i++)
1499      {
1500        recallBestTree(terrace, i, tr);
1501       
1502        /* if the likelihood scores are smaller than some epsilon 0.000001
1503           print the tree to file */
1504           
1505        if(ABS(bestLH - tr->likelihood) < 0.000001)
1506          {
1507            printf("%d %f\n", i, tr->likelihood);
1508            Tree2String(tr->tree_string, tr, tr->start->back, FALSE, TRUE, FALSE, FALSE, FALSE, adef, NO_BRANCHES, FALSE, FALSE, FALSE, FALSE);
1509           
1510            fprintf(f, "%s\n", tr->tree_string); 
1511          }
1512      }
1513
1514    fclose(f);
1515    /* increment tree search counter */
1516    bCount++;
1517  }
1518#endif
1519 
1520 
1521  if(tr->searchConvergenceCriterion)
1522    {
1523      freeBitVectors(bitVectors, 2 * tr->mxtips);
1524      rax_free(bitVectors);
1525      freeHashTable(h);
1526      rax_free(h);
1527    }
1528 
1529  freeBestTree(bestT);
1530  rax_free(bestT);
1531  freeBestTree(bt);
1532  rax_free(bt);
1533
1534#ifdef _TERRACES
1535  /* free terrace tree list */
1536
1537  freeBestTree(terrace);
1538  rax_free(terrace);
1539#endif
1540
1541  freeInfoList(); 
1542  printLog(tr, adef, FALSE);
1543  printResult(tr, adef, FALSE);
1544}
1545
1546
1547
1548
1549boolean treeEvaluate (tree *tr, double smoothFactor)       /* Evaluate a user tree */
1550{
1551  boolean result;
1552 
1553  if(tr->useBrLenScaler)
1554    assert(0);
1555 
1556  result = smoothTree(tr, (int)((double)smoothings * smoothFactor));
1557 
1558  assert(result); 
1559
1560  evaluateGeneric(tr, tr->start);         
1561
1562  return TRUE;
1563}
1564
1565static void setupBranches(tree *tr, nodeptr p,  branchInfo *bInf)
1566{
1567  int   
1568    countBranches = tr->branchCounter;
1569
1570  if(isTip(p->number, tr->mxtips))   
1571    {     
1572      p->bInf       = &bInf[countBranches];
1573      p->back->bInf = &bInf[countBranches];                           
1574
1575      bInf[countBranches].oP = p;
1576      bInf[countBranches].oQ = p->back;
1577           
1578      tr->branchCounter =  tr->branchCounter + 1;
1579      return;
1580    }
1581  else
1582    {
1583      nodeptr q;
1584      assert(p == p->next->next->next);
1585
1586      p->bInf       = &bInf[countBranches];
1587      p->back->bInf = &bInf[countBranches];
1588
1589      bInf[countBranches].oP = p;
1590      bInf[countBranches].oQ = p->back;     
1591
1592      tr->branchCounter =  tr->branchCounter + 1;     
1593
1594      q = p->next;
1595
1596      while(q != p)
1597        {
1598          setupBranches(tr, q->back, bInf);     
1599          q = q->next;
1600        }
1601     
1602      return;
1603    }
1604}
1605 
1606static void makePerm(int *perm, int n)
1607{
1608  int  i, j, k;
1609
1610   
1611  srand(12345);         
1612           
1613  for (i = 0; i < n; i++) 
1614    {   
1615      k        = randomInt(n - i);
1616
1617      assert(i + k < n);
1618
1619      j        = perm[i];
1620      perm[i]     = perm[i + k];
1621      perm[i + k] = j; 
1622    } 
1623
1624
1625}
1626
1627static void smoothTreeRandom (tree *tr, int maxtimes)
1628{
1629  int i, k, *perm;
1630
1631  tr->branchCounter = 0;
1632  tr->numberOfBranches = 2 * tr->mxtips - 3;
1633 
1634  tr->bInf = (branchInfo*)rax_malloc(tr->numberOfBranches * sizeof(branchInfo)); 
1635
1636  perm = (int*)rax_malloc(sizeof(int) * tr->numberOfBranches);
1637
1638  setupBranches(tr, tr->start->back, tr->bInf);
1639
1640
1641
1642  for(i = 0; i < tr->numBranches; i++)
1643    tr->partitionConverged[i] = FALSE;
1644
1645  /*printf("%d \n", maxtimes);*/
1646
1647  while (--maxtimes >= 0) 
1648    {   
1649      for(i = 0; i < tr->numBranches; i++)     
1650        tr->partitionSmoothed[i] = TRUE;               
1651
1652      /*printf("%d %d\n", maxtimes, tr->numberOfBranches);*/
1653
1654      for(k = 0; k < tr->numberOfBranches; k++)
1655        perm[k] = k;
1656
1657      makePerm(perm, tr->numberOfBranches);
1658
1659      for(k = 0; k < tr->numberOfBranches; k++)
1660        {
1661          /*printf("%d Node %d\n", k, tr->bInf[k].oP->number);*/
1662          update(tr, tr->bInf[perm[k]].oP);
1663          newviewGeneric(tr, tr->bInf[perm[k]].oP);     
1664        }
1665
1666      /*if (! smooth(tr, p->back))       return FALSE;
1667      if (!isTip(p->number, tr->rdta->numsp))
1668        {
1669          q = p->next;
1670          while (q != p)
1671            {
1672              if (! smooth(tr, q->back))   return FALSE;
1673              q = q->next;
1674            }
1675        }             
1676      */
1677
1678      if (allSmoothed(tr)) 
1679        break;     
1680    }
1681
1682  for(i = 0; i < tr->numBranches; i++)
1683    tr->partitionConverged[i] = FALSE;
1684
1685  rax_free(tr->bInf);
1686  rax_free(perm);
1687} 
1688
1689
1690void treeEvaluateRandom (tree *tr, double smoothFactor)       
1691{
1692 
1693  smoothTreeRandom(tr, (int)((double)smoothings * smoothFactor));
1694 
1695
1696  evaluateGeneric(tr, tr->start);       
1697}
1698
1699void treeEvaluateProgressive(tree *tr)
1700{ 
1701  int 
1702    i, k;
1703
1704  tr->branchCounter = 0;
1705  tr->numberOfBranches = 2 * tr->mxtips - 3;
1706 
1707  tr->bInf = (branchInfo*)rax_malloc(tr->numberOfBranches * sizeof(branchInfo)); 
1708
1709  setupBranches(tr, tr->start->back, tr->bInf);
1710
1711  assert(tr->branchCounter == tr->numberOfBranches);
1712 
1713  for(i = 0; i < tr->numBranches; i++)
1714    tr->partitionConverged[i] = FALSE;
1715 
1716  for(i = 0; i < 10; i++)
1717    {
1718      for(k = 0; k < tr->numberOfBranches; k++)
1719        {     
1720          update(tr, tr->bInf[k].oP);
1721          newviewGeneric(tr, tr->bInf[k].oP);     
1722        }
1723      evaluateGenericInitrav(tr, tr->start);
1724      printf("It %d %f \n", i, tr->likelihood);
1725    }
1726}
1727   
1728
1729 
1730
1731
1732
Note: See TracBrowser for help on using the repository browser.