-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked tree.c
1449 lines (1168 loc) · 38.8 KB
/
linked tree.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
/******************************************************************************/
// Crann: Detecting Adaptive evolution in Protein Coding DNA sequences.
//
// Copyright 2000 2001 2002 2003 Chris Creevey
//
//
// This file is part of Crann
//
// Crann 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 2 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, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// To Reference this program please use:
// For the Software:
// Creevey, C.J. and McInerney, J.O. (2003) CRANN: detecting adaptive evolution in protein-coding DNA sequences. Bioinformatics 19(13): 1726.
// For the Algorithm:
// Creevey, C.J. and McInerney, J.O. (2002) An algorithm for detecting directional and non-directional positive selection, neutrality and negative selection in protein coding DNA sequences. Gene 300(1-2):43-51
//
//
/*****************************************************************************/
#include "externals.h"
#include "linked tree.h"
#include "evolve.h"
#include "adaptive_tree.h"
#include "Li_Wu_19851993.h"
void linked_tree(float **tree_array)
{
int *done_col, row = 0, col = 0, taxa = 0, one = 0, i = 0, seq = 0, j =0, joining = FALSE;
struct node **build = NULL, *position = NULL;
struct sequence *list_pos = start;
/* start at the top of the tree_array and join together the nodes */
/* This keeps track of those taxa we have already joined */
done_col = malloc((num_of_seqs - untagged)*sizeof(int));
if(!done_col)
{
printf("\n Out of memory \n");
clean_exit();
}
for(i=0; i<num_of_seqs - untagged; i++)
done_col[i] = -1;
/* this keeps the diferent parts of the tree during the building process */
build = malloc((num_of_seqs - untagged - 2) * sizeof(struct node *));
if(!build)
{
printf("\n Out of memory \n");
clean_exit();
}
for(i=0; i<num_of_seqs - untagged - 2; i++)
build[i] = NULL;
for(row = 0; row< num_of_seqs - untagged; row++) /* While we are not at the end of the array */
{
joining = FALSE;
for(col = 0; col < num_of_seqs - untagged; col++)
{
if(tree_array[row][col] != 0)
joining = TRUE; /* The last two lines of the array may comtain nothing, so we don't want a node created in this case, so this checks if there is a joining at this row if there is not, then that row is skipped */
}
if(joining)
{
position = malloc(sizeof(node_type)); /* Each line of the tree arrray represents a node, (the joining of two neighbours), so for every line we create a new node */
if(!position)
{
printf("\n Out of memory \n");
clean_exit();
}
/* initialise all parts ofthe node */
position->seq_num1 = NULL;
position->seq_num2 = NULL;
position->node1 = NULL;
position->node2 = NULL;
position->prev = NULL;
position->ancestor[0] = '\0';
position->ances_seq = malloc(((start->length)/3 +1 )*sizeof(int));
if(!position->ances_seq)
{
printf("\n Out of memory \n");
clean_exit();
}
for(j=0; j<(start->length)/3 +1; j++) position->ances_seq[j] = 0;
position->gaprun = 0;
taxa = 1; /* keeps track of how many taxa we have joined (never more than 2) */
one = -1;
for(col = 0; col < num_of_seqs - untagged; col ++)
{
if(done_col[col] == -1 && tree_array[row][col] != 0) /* if we haven't joined this taxa yet and itis identified for joining at this node */
{
if(taxa == 1) /* signifies we have joined one taxa at this node (max of two) */
{
list_pos = start;
seq = 0;
while(!list_pos->tag && list_pos != NULL) list_pos = list_pos->next; /* as long as the sequence is tagged and we are not at the end of the list, go to the next sequence in the list */
while(list_pos != NULL && seq != col) /* While we haven't found the sequence identified by the position on the tree array */
{
list_pos = list_pos->next;
seq++;
while(!list_pos->tag && list_pos != NULL) list_pos = list_pos->next;
}
if(list_pos != NULL)
{
position->seq_num1 = list_pos; /* each node on the tree will point to the actual sequence it refences in the linked list */
taxa = 2;
done_col[col] = row; /* assign the number of the row to that position in the done_col array, so that we keep track of the last time we joined this taxa */
} /* This also means we know what clade this taxa belongs to, so we know to join those clades together */
}
else
{
if(taxa == 2) /* if we are joining the second taxa to this node */
{
list_pos = start;
seq = 0;
while(!list_pos->tag && list_pos != NULL) list_pos = list_pos->next;
while(list_pos != NULL && seq != col)
{
list_pos = list_pos->next;
seq++;
while(!list_pos->tag && list_pos != NULL) list_pos = list_pos->next;
}
if(list_pos != NULL)
{
position->seq_num2 = list_pos;
taxa = 3;
done_col[col] = row;
}
}
}
}
else /* if we identify this sequence as being part of this node (within the clade ) BUT previously joined */
{
if(done_col[col] != -1 && tree_array[row][col] != 0)
{
if(taxa == 1)
{
position->node1 = build[done_col[col]]; /* node1 of this node is assigned to the joining last made with this clade, we know that this joining includes the clade with this taxa in it, and something else */
build[done_col[col]]->prev = position;
build[done_col[col]] = NULL;
taxa = 2;
one = done_col[col];
done_col[col] = row;
}
else
{
if(taxa == 2)
{
if(done_col[col] == one) done_col[col] = row;
else
{
position->node2 = build[done_col[col]];
build[done_col[col]]->prev = position;
build[done_col[col]] = NULL;
taxa = 3;
done_col[col] = row;
}
}
else
{
if(taxa == 3)
{
done_col[col] = row;
}
}
}
}
}
}
build[row] = position;
position = NULL;
}
}
/* We are finished putting the clades together as described by the tree array, now we need to join those clades that are left seperate by the array */
taxa = 1;
for(i=0; i<num_of_seqs - untagged - 2; i++)
{
if(build[i] != NULL)
{
if(taxa == 1)
{
tree_top = build[i];
build[i] = NULL;
taxa = 2;
}
else
{
if(taxa != 1) /* if there is a single sequence at the end to be joined */
{
if(((build[i]->seq_num1 != NULL && build[i]->seq_num2 == NULL) || (build[i]->seq_num1 == NULL && build[i]->seq_num2 != NULL)) && (build[i]->node1 == NULL && build[i]->node2 == NULL))
{
if(build[i]->seq_num1 != NULL)
{
build[i]->node2 = tree_top; /* With only one taxa we can make this new node the tree top with the free noe pointing to the rest of the tree. */
build[i]->prev = NULL;
tree_top->prev = build[i];
tree_top = build[i];
build[i] = NULL;
}
else
{
build[i]->node1 = tree_top;
build[i]->prev = NULL;
tree_top->prev = build[i];
tree_top = build[i];
build[i] = NULL;
}
}
else
{
position = malloc(sizeof(node_type));
if(!position)
{
printf("Out of memory\n");
clean_exit();
}
position->seq_num1 = NULL;
position->seq_num2 = NULL;
position->node1 = build[i];
position->node2 = tree_top;
position->prev = NULL;
position->ancestor[0] = '\0';
position->ances_seq = malloc((((start->length)/3) +1) * sizeof(int));
if(!position->ances_seq)
{
printf("\n Out of memory \n");
clean_exit();
}
for(j=0; j<(((start->length)/3) +1); j++) position->ances_seq[j] = 0;
position->gaprun = 0;
build[i]->prev = position;
tree_top->prev = position;
build[i] = NULL;
tree_top = position;
position = NULL;
}
}
}
}
}
if(done_col != NULL)
{
free(done_col);
done_col = NULL;
}
if(build != NULL)
{
for(i=0; i<(num_of_seqs - untagged - 2); i++) build[i] = NULL;
free(build);
build = NULL;
}
}
/* I apoligise for this function in advance.... There is a problem in some platforms with the tree, in that for some reason the seq_num1 value
is not assigned to NULL when the the two nodes are assigned, The problem I cannot find, which makes me think its an internal problem
with the code being implemented on the particular environment. Since I cannot find anything to fix, I decided to come with an ad-hoc solution.
This function will travel through the tree checking to see if there is a case of the two nodes being assigned, AND one of the seqNUM being assigned.
it will then assign the pointer to NULL. By doing this I am assuming ALOT, namely that this is the only problem that can occur, since it could actually assign a
valid seqnum pointer to null, if for some reason one of the node pointers got assigned wrongly.
*/
int val_tree(struct node *position)
{
int result = FALSE, returned = FALSE;
if(position->node1 != NULL) result = val_tree(position->node1);
if(result == TRUE) returned = TRUE;
if(position->node2 != NULL) result = val_tree(position->node2);
if(result == TRUE) returned = TRUE;
if(position->node1 != NULL && position->node2 != NULL)
{
if(position->seq_num1 != NULL)
{
position->seq_num1 = NULL;
returned = TRUE;
}
if(position->seq_num2 != NULL)
{
position->seq_num2 = NULL;
returned = TRUE;
}
}
return(returned);
}
/* This recursive function travels through the tree depth first to search for the first occurance of an outgroup
which is present on a leaf */
struct node * find_outgroup (struct node *position)
{
struct node *now = NULL;
if((position->seq_num1) != NULL)
if((position->seq_num1)->outgroup == TRUE) now = position;
if((position->seq_num2) != NULL)
if((position->seq_num2)->outgroup == TRUE) now = position;
/* we want to find the first part of the outgroup that we come across */
if(position->node1 != NULL && now == NULL) now = find_outgroup(position->node1);
if(position->node2 != NULL && now == NULL) now = find_outgroup(position->node2);
return(now);
}
/* this goes down the tree from the position found and counts all the outgroups found
along with the total number of taxa found. */
void travel_down(struct node *position, int *found, int *total)
{
if(position->seq_num1 != NULL)
{
if(outgroup[(position->seq_num1)->seq_num]) found++;
total++;
}
if(position->seq_num2 != NULL)
{
if(outgroup[(position->seq_num2)->seq_num]) found++;
total++;
}
if(position->node1 != NULL)
travel_down(position->node1, found, total);
if(position->node2 != NULL)
travel_down(position->node2, found, total);
}
/* this will check if the outgroup is divided on either side of the tree_top which makes it very difficult to
check if the outgroup is valid. if this is divided the function will reroot the tree so that the
outgroup is all on one side of the tree */
int check_divided(struct node *position)
{
struct node *left = NULL, *right = NULL;
int answer = FALSE;
if(position->node1 != NULL)
left = find_outgroup(position->node1);
if(position->node2 != NULL)
right = find_outgroup(position->node2);
if(left != NULL && right != NULL)
{
if(left->node1 != NULL || left->node2 != NULL)
{
prepare_tree(left, 1);
answer = check_divided(tree_top);
}
if(answer == FALSE)
{
if(right->node1 != NULL || right->node2 != NULL)
{
prepare_tree(right, 1);
answer = check_divided(tree_top);
}
}
}
else answer = TRUE;
return(answer);
}
/* this function is called in the times the tree needs to be rerooted about a position */
/* It does nothing but blindly introduces a new node before the position specified, and
then reroots the tree about that new node */
void do_rerooting(struct node *position)
{
struct node *newnode = NULL;
int j = 0;
newnode = malloc(sizeof(node_type));
if(!newnode)
{
printf("ERROR\nOut of memory\n");
clean_exit();
}
newnode->seq_num1 = NULL;
newnode->seq_num2 = NULL;
newnode->node1 = NULL;
newnode->node2 = NULL;
newnode->prev = NULL;
newnode->ancestor[0] = '\0';
newnode->ances_seq = malloc(((start->length)/3) +1 * sizeof(int));
if(!newnode->ances_seq)
{
printf("ERROR\nOut of memory\n");
clean_exit();
}
else
for(j=0; j<(((start->length)/3) +1); j++) position->ances_seq[j] = 0;
/* put in the newnode before the present position */
newnode->node2 = position;
newnode->prev = position->prev;
position->prev = newnode;
if(newnode->prev != NULL)
{
if((newnode->prev)->node1 == position)
(newnode->prev)->node1 = newnode;
else (newnode->prev)->node2 = newnode;
}
else tree_top = newnode;
position = newnode;
/* reroot the tree about the present position */
if(newnode != tree_top)
{
reroot_tree(position);
if(position->node1 == NULL)
position->node1 = position->prev;
else
position->node2 = position->prev;
position->prev = NULL;
}
}
/* Because we have to introduce a new node if we try to reroot the tree on a node with two daughter branches
this function does this if necessary, and then uses this new node to reroot the tree using the function
reroot tree. It then cleans up the process by assigning the pointers for the top of the tree */
int prepare_tree(struct node *position, int count)
{
struct node *newnode = NULL, *tmp = NULL;
int j = 0, valid = TRUE;
/*********** allocate new node *****************/
newnode = malloc(sizeof(node_type));
newnode->seq_num1 = NULL;
newnode->seq_num2 = NULL;
newnode->node1 = NULL;
newnode->node2 = NULL;
newnode->prev = NULL;
newnode->ancestor[0] = '\0';
newnode->ances_seq = malloc((((start->length)/3) +1) * sizeof(int));
if(!newnode->ances_seq)
{
printf("ERROR\nOut of memory\n");
clean_exit();
}
else
for(j=0; j<(((start->length)/3 )+1); j++) position->ances_seq[j] = 0;
/********** start preparing ****************/
newnode->gaprun = 0;
if(count == 1) /* if there is only 1 taxa defined as the out group */
{
if(position->seq_num1 != NULL && position->seq_num2 != NULL) /* if the outgroup has been paired with a taxa */
{
if((position->seq_num1)->outgroup) /* if seq_num1 points to the outgroup */
{
newnode->seq_num1 = position->seq_num1;
position->seq_num1 = NULL;
position->node1 = newnode;
}
else
{
newnode->seq_num2 = position->seq_num2;
position->seq_num2 = NULL;
position->node2 = newnode;
}
newnode->prev = position;
position = newnode;
reroot_tree(position);
if(position->seq_num1 == NULL)
position->node1 = position->prev;
else
position->node2 = position->prev;
position->prev = NULL;
}
else /* if the outgroup is on its own */
{
newnode->node2 = position;
newnode->prev = position->prev;
position->prev = newnode;
if((newnode->prev)->node1 == position)
{
(newnode->prev)->node1 = newnode;
}
else
{
(newnode->prev)->node2 = newnode;
}
reroot_tree(newnode);
if(newnode->node1 == NULL)
newnode->node1 = newnode->prev;
else
newnode->node2 = newnode->prev;
newnode->prev = NULL;
if(position->node1 != NULL)
{
tree_top->node2 = position->node1;
(position->node1)->prev = tree_top;
position->node1 = tree_top;
tree_top->prev = position;
tree_top = position;
tree_top->prev = NULL;
}
else
{
tree_top->node2 = position->node2;
(position->node2)->prev = tree_top;
position->node2 = tree_top;
tree_top->prev = position;
tree_top = position;
tree_top->prev = NULL;
}
}
}
else /* if there are more than one taxa defined as the out groups */
{
/* first check to see if the taxa are valid. */
tmp = find_non_outgroup(position);
if(tmp == NULL)
{
if(count_taxa(position) == count)
{
/* put in the newnode before the present position */
newnode->node2 = position;
newnode->prev = position->prev;
position->prev = newnode;
if((newnode->prev)->node1 == position)
(newnode->prev)->node1 = newnode;
else (newnode->prev)->node2 = newnode;
/* reroot the tree about the present position */
reroot_tree(newnode);
if(newnode->node1 == NULL)
newnode->node1 = newnode->prev;
else
newnode->node2 = newnode->prev;
newnode->prev = NULL;
}
else
valid = FALSE;
}
else
valid = FALSE;
}
return(valid);
}
/* This simply counts how many taxa there are from the position on the tree that it is called from to the bottom */
int count_taxa(struct node *position)
{
int i = 0;
if(position->node1 != NULL) i += count_taxa(position->node1);
if(position->node2 != NULL) i += count_taxa(position->node2);
if(position->seq_num1 != NULL) i++;
if(position->seq_num2 != NULL) i++;
return(i);
}
/* This function will reeroot the tree to the node specified by the input
To complete the rerooting you need to add the following lines after whenver you call this function */
/* if(position->node1 == '\0')
position->node1 = position->prev;
else
position->node2 = position->prev;
position->prev = '\0';
without these lines the tree will not be fully rerooted */
void reroot_tree(struct node *position)
{
if((position->prev)->node1 == position)
(position->prev)->node1 = (position->prev)->prev;
if((position->prev)->node2 == position)
(position->prev)->node2 = (position->prev)->prev;
if((position->prev)->prev != NULL)
{
reroot_tree(position->prev);
}
(position->prev)->prev = position;
tree_top = position;
}
/* This function looks for occurances where we have rerooted a tree and in doing so we had to add another
node. Since this node is then obsolete if we reroot the tree again, this function deletes that node,
which would contain no information for the rerooted tree */
int prune_tree(struct node *position)
{
struct node *place = NULL;
int done = FALSE;
if(position->seq_num1 == NULL && position->seq_num2 == NULL)
{
if(position->node1 == NULL || position->node2 == NULL)
{
if(position->node1 == NULL && position->node2 == NULL) /* If the node is at the end of branch (leaf) */
{
if((position->prev)->node1 == position)
(position->prev)->node1 = NULL;
else (position->prev)->node2 = NULL;
place = position;
position = place->prev;
if(place->ances_seq != NULL) free(place->ances_seq);
place->ances_seq = NULL;
if(place != NULL) free(place);
place = NULL;
done = TRUE;
}
else /* if the node appears in the middle of a branch */
{
if(position->node1 != NULL)
{
place = position->node1;
(position->node1)->prev = position->prev;
}
else
{
place = position->node2;
(position->node2)->prev = position->prev;
}
if((position->prev)->node1 == position)
(position->prev)->node1 = place;
else (position->prev)->node2 = place;
place = position;
position = place->prev;
if(place->ances_seq != NULL) free(place->ances_seq);
place->ances_seq = NULL;
if(place != NULL) free(place);
place = NULL;
done = TRUE;
}
}
}
/* Ths next section checks to see if there is a single taxa on its own at the bottom of a branch,
This breaks the rules about branchs, so the taxa is included in the node previous to it */
if(position->node1 == NULL && position->node2 == NULL)
if(position->seq_num1 == NULL || position->seq_num2 == NULL)
{
if((position->prev)->node1 == position)
{
if(position->seq_num1 != NULL)
(position->prev)->seq_num1 = position->seq_num1;
else (position->prev)->seq_num1 = position->seq_num2;
(position->prev)->node1 = NULL;
}
else
{
if(position->seq_num1 != NULL)
(position->prev)->seq_num2 = position->seq_num1;
else (position->prev)->seq_num2 = position->seq_num2;
(position->prev)->node2 = NULL;
}
place = position;
position = place->prev;
if(place->ances_seq != NULL) free(place->ances_seq);
place->ances_seq = NULL;
if(place != NULL) free(place);
place = NULL;
done = TRUE;
}
if(position->node1 != NULL)
if(prune_tree(position->node1) == TRUE) done = TRUE;
if(position->node2 != NULL)
if(prune_tree(position->node2) == TRUE) done = TRUE;
return(done);
}
/* This checks the validity of the selected outgroup */
int check_outgroup(int count)
{
struct node *position = NULL;
int answer = FALSE, proceed = TRUE;
/* because of the nature of the storage of the tree, if the tree is originally rooted in the middle
of the outgroups, it will not be possible to verify the selection of the outgroups. The best way to
stop this from happening is to firstly make sure that the tree is rooted somewhere away from the out groups */
if((position = find_non_outgroup(tree_top)) != NULL)
{
if(position != tree_top)
prepare_tree(position, 1);
/* number = 0;
if(tree_top != '\0')
{
check_tree(tree_top, &number); *//* make sure the tree has been created correctly */
/* printf("\npress return to continue (rerooted about an ingroup)");
getchar();
}
else printf("tree not defined\n");
*/
while(prune_tree(tree_top)){}
/*
number = 0;
if(tree_top != '\0')
{
check_tree(tree_top, &number); *//* make sure the tree has been created correctly */
/* printf("\npress return to continue (rerooted about an ingroup)");
getchar();
}
else printf("tree not defined\n");
*/
if(count == 1) /* If there is only 1 outgroup, then we need to make sure that the tree is not already rooted about it before proceeding */
{
if(tree_top->seq_num1 != NULL && tree_top->seq_num2 == NULL)
{
if(tree_top->seq_num1->outgroup == TRUE)
proceed = FALSE;
answer = TRUE;
}
if(tree_top->seq_num2 != NULL && tree_top->seq_num1 == NULL)
{
if(tree_top->seq_num2->outgroup == TRUE)
proceed = FALSE;
answer = TRUE;
}
}
if(proceed)
{
if((position = find_outgroup(tree_top)) != NULL) /* Find a part of the out group */
{
if(prepare_tree(position, count) == TRUE)
answer = TRUE;
else answer = FALSE;
}
else
{
answer = FALSE;
printf("Cannot find an outgroup\n");
}
}
}
else
{
answer = FALSE;
printf("Cannot find an ingroup\n");
}
return(answer);
}
/* this function tries to find a non-outgroup taxa below where it is called from */
struct node * find_non_outgroup(struct node *position)
{
struct node *found = NULL;
if(position->seq_num1 != NULL)
{
if(!(position->seq_num1)->outgroup)
found = position;
}
if(found == NULL && position->seq_num2 != NULL)
{
if(!(position->seq_num2)->outgroup)
found = position;
}
if(found == NULL && position->node1 != NULL) found = find_non_outgroup(position->node1);
if(found == NULL && position->node2 != NULL) found = find_non_outgroup(position->node2);
return(found);
}
/* This function dismantles the binary tree */
void dismantle(struct node *position, int *count)
{
struct node *place = NULL;
if(position->node1 != NULL) dismantle(position->node1, count);
if(position->node2 != NULL) dismantle(position->node2, count);
if(position->ances_seq != NULL) free(position->ances_seq);
position->ances_seq = NULL;
place = position;
position = place->prev;
if(place != NULL) free(place);
place = NULL;
*count = *count + 1;
}
/*
void write_graph(struct node *position, int *count, int i, int title)
{
if(position->node1 != '\0') write_graph(position->node1, count, i, title);
if(position->node2 != '\0') write_graph(position->node2, count, i, title);
if(position != tree_top)
{
if(title ) {
fprintf(graphfile, "Node %d\t", *count);
fprintf(graphfile, "repl/fix\t");
fprintf(graphfile, "repl/poly\t");
fprintf(graphfile, "synon/fix\t");
fprintf(graphfile, "synon/poly\t");
}
else fprintf(graphfile, "\t%d", position->graph[0][i] );
*count = *count + 1;
}
}
*/
/* this writes a tree file from the tree in memory, This outputs the tree with the results from the Maynooth methods 1 & 2 */
void write_tree1(struct node *position, char *last, int *count, float **pvalue)
{
int i = 0;
if(*last == ')') fprintf(outtree, ",");
fprintf(outtree, "(");
if(position->seq_num1 != NULL)
{
fprintf(outtree, "%s,", (position->seq_num1)->name);
i++;
}
if(position->seq_num2 != NULL)
{
fprintf(outtree, "%s", (position->seq_num2)->name);
if(i == 0) fprintf(outtree, ",");
}
*last = ',';
if(position->node1 != NULL) write_tree1(position->node1, last, count, pvalue);
if(position->node2 != NULL) write_tree1(position->node2, last, count, pvalue);
fprintf(outtree, ")");
if(position->prev != NULL)
{
if(pvalue[*count][1] < 0.050 || (pvalue[*count][2] <= 0.05 && pvalue[*count][2] != 0)) fprintf(outtree, "%d ",*count);
}
*count = *count + 1;
*last = ')';
}
/* This writes a tree to the tree file, including the results from the pairwise analysis of the tree (incuding ancestors) */
void write_tree2(struct node *position, char *last, int *count)
{
int i = 0;
float li = 0;
if(*last == ')') fprintf(outtree, ",");
fprintf(outtree, "(");
if(position->seq_num1 != NULL)
{
fprintf(outtree, "%s,", (position->seq_num1)->name);
i++;
}
if(position->seq_num2 != NULL)
{
fprintf(outtree, "%s", (position->seq_num2)->name);
if(i == 0) fprintf(outtree, ",");
}
*last = ',';
if(position->node1 != NULL) write_tree2(position->node1, last, count);
if(position->node2 != NULL) write_tree2(position->node2, last, count);
fprintf(outtree, ")");
if(position->prev != NULL)
{
li = (position->li93_1[0]/position->li93_1[1]);
if(position->li93_1[1] != 0 && li > 1 ) fprintf(outtree, "1_%f", li);
li = (position->li93_2[0]/position->li93_2[1]);
if(position->li93_2[1] != 0 && li > 1 ) fprintf(outtree, "_2_%f", li);
}
*count = *count + 1;
*last = ')';
}
/* this writes a tree file from the tree in memory, Every internal branch is labeled so we know the number of all the branches */
void write_tree3(struct node *position, char *last, int *count, float **pvalue)
{
int i = 0;
if(*last == ')') fprintf(outtree, ",");
fprintf(outtree, "(");
if(position->seq_num1 != NULL)
{
fprintf(outtree, "%s,", (position->seq_num1)->name);
i++;
}
if(position->seq_num2 != NULL)
{
fprintf(outtree, "%s", (position->seq_num2)->name);
if(i == 0) fprintf(outtree, ",");
}