-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadaptive_tree.c
3959 lines (3164 loc) · 113 KB
/
adaptive_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 "adaptive_tree.h"
#include "evolve.h"
#include "linked tree.h"
/* this is the program to run the algorithm devised by Kreitman and McDonald using trees */
void adaptive_tree(int **ratio)
{
int i = 0, j=0, count = 0, mutation = FALSE, x = 0, y = 0, last = FALSE, done = FALSE;
struct sequence *seq1 = start;
float ***subst_matrix; /* holds the likelihood of nucliotide substitutions at each fold site */
char nuc = '\0';
subst_matrix = malloc(3*sizeof(float**));
if(subst_matrix == NULL)
{
printf("Out of memory\n");
clean_exit();
}
for(y=0; y<3; y++)
{
subst_matrix[y] = malloc(5*sizeof(float*));
if(subst_matrix[y] == NULL)
{
printf("Out of memory\n");
clean_exit();
}
for(x=0; x<5; x++)
{
subst_matrix[y][x] = malloc(5*sizeof(float));
if(subst_matrix[y][x] == NULL)
{
printf("Out of memory\n");
clean_exit();
}
}
}
for(i=0; i<3; i++)
{
for(x=0; x<5; x++)
for(y=0; y<5; y++)
subst_matrix[i][x][y] = 0;
}
substitution_matrix(subst_matrix); /* calculate the substitution matrix for the given data */
/* printf("after making tree\n");
number = 0;
if(tree_top != NULL)
{
check_tree(tree_top, &number); *//* make sure the tree has been created correctly */
/* }
else printf("tree not defined\n");
printf("press return to continue - adaptive tree");
getchar();
*/
define_outgroup(); /* define the outgroup and give direction to the tree */
/* printf("after define outgroup");
number = 0;
if(tree_top != NULL)
{
check_tree(tree_top, &number); *//* make sure the tree has been created correctly */
/* }
else printf("tree not defined\n");
printf("press return to continue - adaptive tree");
getchar();
*/
done = prune_tree(tree_top);
while(done == TRUE) /* This checks to make sure that there are no obsolete nodes on the tree */
done = prune_tree(tree_top);
/* printf("Finished pruning tree (in Adaptive_tree) \n");
number = 0;
if(tree_top != NULL)
{
check_tree(tree_top, &number); *//* make sure the tree has been created correctly */
/* }
else printf("tree not defined\n");
printf("press return to continue - adaptive tree");
getchar();
*/
i = 0;
for(i=0; i<(start->length)/3; i++)
{
for(j=0; j<3; j++)
{
ancestral_codon(i, subst_matrix);
/* ancestral_nuc(i, j, subst_matrix); *//* calculate the ancestral nucliotide for this position on each node */
}
for(j=0; j<3; j++)
{
nuc = 's'; mutation = FALSE;
is_mutation(tree_top, &mutation, &nuc, i, j); /* we need to know if a mutation has occured in any sequence at this point */
if(mutation)
{
calculate_gaprun(tree_top, i); /* claculate gapruns at every position on the tree for i */
count = 0;
is_fixed(tree_top, i, j, ratio, &count, last); /* if a mutation has occured then calculate is_fixed otherwise don't */
last = TRUE;
}
else
{
last = FALSE;
if(toupper(nuc) != 'X')/* this is in case every sequence in the alignment has a gap here, while unlikely, without this it would think that a gaprun */
increment_gaprun(tree_top); /* increment every gaprun variable since there is a gap here at every sequence */
else
reset_gaprun(tree_top); /* see explaination with function */
}
}
}
if(seq1->bases[i] == 193) close_ances(tree_top, i);
/* number = 0;
if(tree_top != NULL)
{
check_tree(tree_top, &number); *//* make sure the tree has been created correctly */
/* printf("press return to continue - adaptive tree2 ---- after counting\n");
getchar();
}
else printf("tree not defined\n");
*/
if(ances_file != NULL)
{
fclose(ances_file);
}
if((ances_file = fopen("ancestor.out", "w")) == NULL) /* check to see if the file is there */
{
printf("\n\n\tCannot open the file, ancestor.out\n\t ancestor file not written\n");
}
else
{
count = 0;
output_ancestors(tree_top, &count);
fflush(ances_file);
fclose(ances_file);
ances_file = NULL;
printf("\n\n\n\n\n\n\n\nAncestors and current taxa written to file ancestor.out \n");
printf("Phylogenetic tree with significances written to result-tree.ph\n");
}
/* free up allocated memory */
if(subst_matrix != NULL)
{
for(i=0; i<3; i++)
{
for(j=0; j<5; j++)
free(subst_matrix[i][j]);
free(subst_matrix[i]);
}
free(subst_matrix);
}
}
/* for each i we check if there is a gap at any position, and increment the gaprun variable for that taxa or ancestor
if there is no gap at a particular position gaprun is assigned to 0. Doing this at this stage means we don't risk counting
the same position on the tree more than once, which may happen when the tree is traversed from within the program */
void calculate_gaprun(struct node *position, int i)
{
if(position->node1 != NULL) calculate_gaprun(position->node1, i);
if(position->node2 != NULL) calculate_gaprun(position->node2, i);
if(position->seq_num1 != NULL)
{
if((position->seq_num1)->bases[i] == 64)
(position->seq_num1)->gaprun++;
else
(position->seq_num1)->gaprun = 0;
}
if(position->seq_num2 != NULL)
{
if((position->seq_num2)->bases[i] == 64)
(position->seq_num2)->gaprun++;
else
(position->seq_num2)->gaprun = 0;
}
if(position->ances_seq[i] == 64)
position->gaprun++;
else
position->gaprun = 0;
}
/* This function is called when we come across a section of the sequences where there is no change. It resets all the gaprun variables to 0
so the program doesn't mistakenly think that the the last difference is part of this new one. */
void reset_gaprun(struct node *position)
{
if(position->node1 != NULL) reset_gaprun(position->node1);
if(position->node2 != NULL) reset_gaprun(position->node2);
if(position->seq_num1 != NULL)
(position->seq_num1)->gaprun = 0;
if(position->seq_num2 != NULL)
(position->seq_num2)->gaprun = 0;
position->gaprun = 0;
}
/* in the unlikely event that there is a gap at this position in every sequence, this increments gaprun at every position forthe tree */
void increment_gaprun(struct node *position)
{
if(position->node1 != NULL) reset_gaprun(position->node1);
if(position->node2 != NULL) reset_gaprun(position->node2);
if(position->seq_num1 != NULL)
(position->seq_num1)->gaprun++;
if(position->seq_num2 != NULL)
(position->seq_num2)->gaprun++;
position->gaprun++;
}
/* Is gap travels down through the tree checking if any of the mutations are a gap, if it is it reports this
and depending on whether the previous mutation was next to it AND contained gap this mutation is not counted in the
McD and K algorithm, since a row of gaps on a tree would generally be from a single insertion or deletion,
and not individual events as the program would otherwise assume */
int is_gap(int i, int j, struct node *position)
{
int ans = FALSE;
if(position->seq_num1 != NULL)
if((position->seq_num1)->bases[i] == 64)
ans = TRUE;
if(position->seq_num2 != NULL)
if((position->seq_num2)->bases[i] == 64)
ans = TRUE;
if(position->node1 != NULL && ans == FALSE) ans = is_gap(i,j, position->node1);
if(position->node2 != NULL && ans == FALSE) ans = is_gap(i,j, position->node2);
return(ans);
}
/* This function assigns the value 193 to the end of the ancestral sequence to signify that that is the end */
void close_ances(struct node *position, int i)
{
if(position->node1 != NULL) close_ances(position->node1, i);
if(position->node2 != NULL) close_ances(position->node2, i);
position->ances_seq[i] = 193;
}
/* this function outputs the ancestral sequences to a file along with the original sequences - all in fasta format */
void output_ancestors(struct node *position, int *count)
{
int i = 0, j = 0;
if(position->node1 != NULL && position->seq_num1 == NULL) output_ancestors(position->node1, count);
if(position->node2 != NULL && position->seq_num2 == NULL) output_ancestors(position->node2, count);
fprintf( ances_file, ">Node%d Ancestor sequence\n", *count);
for(i=0; i< (start->length)/3; i++)
{
for(j=0; j<3; j++)
{
fprintf(ances_file, "%c", codons[position->ances_seq[i]][j]);
}
if(fmod(i+1, 20) == 0 ) fprintf(ances_file, "\n");
}
fprintf(ances_file, "\n");
if(position->seq_num1 != NULL)
{
i = 0;
fprintf( ances_file, "> %s\n", (position->seq_num1)->name );
for(i=0; i< (start->length)/3; i++)
{
for(j=0; j<3; j++)
{
fprintf(ances_file, "%c", codons[(position->seq_num1)->bases[i]][j]);
}
if(fmod(i+1, 20) == 0) fprintf(ances_file, "\n");
}
fprintf(ances_file, "\n");
}
if(position->seq_num2 != NULL)
{
i = 0;
fprintf( ances_file, "> %s\n", (position->seq_num2)->name );
for(i=0; i< (start->length)/3; i++)
{
for(j=0; j<3; j++)
{
fprintf(ances_file, "%c", codons[(position->seq_num2)->bases[i]][j]);
}
if(fmod(i+1, 20) == 0) fprintf(ances_file, "\n");
}
fprintf(ances_file, "\n");
}
*count = *count +1;
}
/* This function travels down through the tree to see if there are any mutations at this position (if there is is_fixed
will be called to see if for each node the mutation is fixed or not ) */
void is_mutation(struct node *position,int *mutation, char *nuc, int i, int j)
{
if(position->seq_num1 != NULL)
{
if(*nuc == '\0') {*nuc = codons[(position->seq_num1)->bases[i]][j];
}
else
{
if(*nuc != codons[(position->seq_num1)->bases[i]][j])
{
*mutation = TRUE;
}
}
}
if(position->seq_num2 != NULL)
{
if(*nuc == '\0') { *nuc = codons[(position->seq_num2)->bases[i]][j];
}
else
{
if(*nuc != codons[(position->seq_num2)->bases[i]][j])
{
*mutation = TRUE;
}
}
}
if(*mutation == FALSE)
if(position->node1 != NULL)
is_mutation(position->node1, mutation, nuc, i, j);
if(*mutation == FALSE)
if(position->node2 != NULL)
is_mutation(position->node2, mutation, nuc, i, j);
}
/* this function is called with the following variables passed to it, i = codon number we are at, j the position of the codon we are at */
/* tree_description a pointer to the tree array created earlier, and diff, which is the number of the nucliotide (0 = U, 1 = C, 2 = A, 3 = G)*/
/*which occurs as the most common difference in that position. It uses the protocol defined by Mc Donald and Kreitman to determine whether a mutation is fixed or */
/* polymorphic, given the phylogenetic tree array. It increments the array ratio which is passed to it. */
void is_fixed(struct node *position, int i, int j, int **ratio, int *count, int last)
{
char nuc = 'a';
if(position->node1 != NULL) is_fixed(position->node1, i, j, ratio, count, last);
if(position->node2 != NULL) is_fixed(position->node2, i, j, ratio, count, last);
if(position->prev != NULL)
{
if(genetic_codes[code][position->ances_seq[i]] != 0) /* It won't count it if the ancestral codon is a stop */
{
snapshot(position, ratio, &nuc, count, i, j);
}
*count = *count + 1;
}
}
/* This function implements an idea that the measure of whether a population is under selection or not
is a function of the complete timeline from the snapshot in time represented by the internal branch, to
the moment the sequences in the clade were sampled. The question you have to ask is, whether an change occured
int the enviornment at this point in time from which point on the sequences in the clade have been trying to optimise
their survival rate in the enviornment? Adaption doesn't occur at one timepoint, but it is a progressive thing that
occurs after a change in the enviornment.
*/
void snapshot(struct node *position, int **ratio, char *nuc, int *count, int i, int j)
{
int fixed = TRUE, silent = FALSE, value = 0;
/* The ratio is divided as follows: there is a ratio for each node in the tree. that ratio is */
/* 0: repl/fix 1: repl/poly 2: synon/fix 3: synon/poly */
/* for each character the appropriate ratio is incremented according to which node it is at */
/* The variable Graphs is used to hold the results of the histograms at each node */
/* Check for fixations within the clade, based on a change in the ancestors */
if(codons[(position->prev)->ances_seq[i]][j] != codons[position->ances_seq[i]][j])
{
check_fixed_in(position, &fixed, &codons[position->ances_seq[i]][j], i, j);
value = (position->prev)->ances_seq[i] - transform_base(codons[(position->prev)->ances_seq[i]][j], j) + transform_base(codons[position->ances_seq[i]][j], j);
if(value > 64) value = 64; /* if there is a non standard charater tha whole codon is treated as a gap, and given the value of 64 to represent that */
if(genetic_codes[code][value] != genetic_codes[code][(position->prev)->ances_seq[i]])
silent = FALSE;
else
silent = TRUE;
if(position->gaprun < 2 && ((position->prev)->gaprun < 2)) /* if this ancestor of the previous has not got a run of gaps here */
{
if(fixed == TRUE)
{
if(silent == TRUE)
{
ratio[*count][2] = ratio[*count][2] + 1;
graphs[*count][2][i]++;
/* position->graph[2][i] = position->graph[2][i] + 1;
*/ }
else
{
ratio[*count][0] = ratio[*count][0] + 1;
graphs[*count][0][i]++;
/* position->graph[0][i] = position->graph[0][i] + 1;
*/ }
}
else
{
if(silent == TRUE)
{
ratio[*count][3] = ratio[*count][3] + 1;
graphs[*count][3][i]++;
/* position->graph[3][i] = position->graph[3][i] + 1;
*/ }
else
{
ratio[*count][1] = ratio[*count][1] + 1;
graphs[*count][1][i]++;
/* position->graph[1][i] = position->graph[1][i] + 1;
*/ }
}
}
}
/* count polymorphisms */
if(position->seq_num1 != NULL)
{
if(codons[position->ances_seq[i]][j] != codons[(position->seq_num1)->bases[i]][j])
{
if((position->seq_num1)->gaprun < 2 && position->gaprun < 2) /* if the ancestor or the taxa is not in a run of gaps */
{
value = position->ances_seq[i] - transform_base(codons[position->ances_seq[i]][j], j) + transform_base(codons[(position->seq_num1)->bases[i]][j], j);
if(value > 64) value = 64; /* if there is a non standard charater tha whole codon is treated as a gap, and given the value of 64 to rpreent that */
if(genetic_codes[code][value] != genetic_codes[code][position->ances_seq[i]])
{
ratio[*count][1] = ratio[*count][1] + 1;
graphs[*count][1][i]++;
/* position->graph[1][i] = position->graph[1][i] + 1;
*/ }
else
{
ratio[*count][3] = ratio[*count][3] + 1;
graphs[*count][3][i]++;
/* position->graph[3][i] = position->graph[3][i] + 1;
*/ }
}
}
}
if(position->seq_num2 != NULL)
{
if(codons[position->ances_seq[i]][j] != codons[(position->seq_num2)->bases[i]][j])
{
if((position->seq_num2)->gaprun < 2 && position->gaprun < 2) /* if the ancestor or the taxa is not part of a run of gaps */
{
value = position->ances_seq[i] - transform_base(codons[position->ances_seq[i]][j], j) + transform_base(codons[(position->seq_num2)->bases[i]][j], j);
if(value > 64) value = 64;
if(genetic_codes[code][value] != genetic_codes[code][position->ances_seq[i]])
{
ratio[*count][1] = ratio[*count][1] + 1;
graphs[*count][1][i]++;
/* position->graph[1][i] = position->graph[1][i] + 1;
*/ }
else
{
ratio[*count][3] = ratio[*count][3] + 1;
graphs[*count][3][i]++;
/* position->graph[3][i] = position->graph[3][i] + 1;
*/ }
}
}
}
if(position->node1 != NULL) snapshot(position->node1, ratio, nuc, count, i, j);
if(position->node2 != NULL) snapshot(position->node2, ratio, nuc, count, i, j);
}
/* This function will descend through the tree counting every change that occurs within the clade defined by the
internal node we first call the funtion with. during counting it also checks to see if the change wass synonymous
or replacement, This function is not called if there is a fixed mutation within the clade as this would mean that
there are no other changes within the tree. (20/9/00) */
void count_polymorphisms(struct node *position, int *repl, int *silent, int i, int j)
{
int value = 0;
if(position->node1 != NULL) count_polymorphisms(position->node1, repl, silent, i, j);
if(position->node2 != NULL) count_polymorphisms(position->node2, repl, silent, i ,j);
if(position->node1 != NULL)
{
if(codons[position->ances_seq[i]][j] != codons[(position->node1)->ances_seq[i]][j])
{
value = position->ances_seq[i] - transform_base(codons[position->ances_seq[i]][j], j) + transform_base(codons[(position->node1)->ances_seq[i]][j], j);
if(genetic_codes[code][value] != genetic_codes[code][position->ances_seq[i]])
*repl = *repl +1;
else
*silent = *silent +1;
}
}
if(position->node2 != NULL)
{
if(codons[position->ances_seq[i]][j] != codons[(position->node2)->ances_seq[i]][j])
{
value = position->ances_seq[i] - transform_base(codons[position->ances_seq[i]][j], j) + transform_base(codons[(position->node2)->ances_seq[i]][j], j);
if(genetic_codes[code][value] != genetic_codes[code][position->ances_seq[i]])
*repl = *repl +1;
else
*silent = *silent +1;
}
}
if(position->seq_num1 != NULL)
{
if(codons[position->ances_seq[i]][j] != codons[(position->seq_num1)->bases[i]][j])
{
value = position->ances_seq[i] - transform_base(codons[position->ances_seq[i]][j], j) + transform_base(codons[(position->seq_num1)->bases[i]][j], j);
if(genetic_codes[code][value] != genetic_codes[code][position->ances_seq[i]])
*repl = *repl +1;
else
*silent = *silent +1;
}
}
if(position->seq_num2 != NULL)
{
if(codons[position->ances_seq[i]][j] != codons[(position->seq_num2)->bases[i]][j])
{
value = position->ances_seq[i] - transform_base(codons[position->ances_seq[i]][j], j) + transform_base(codons[(position->seq_num2)->bases[i]][j], j);
if(genetic_codes[code][value] != genetic_codes[code][position->ances_seq[i]])
*repl = *repl +1;
else
*silent = *silent +1;
}
}
}
/* This function implements the new rules for determining whether a polymorphism is replacement or not,
This travels down the tree from the internal branch specified, firstly looking for differences in any ancestral sequences
it passes from the ancestor preceeding it. The algorithm increments the ratios (depending on synon or not) and then travels
down the clade, checking any other ancestors it comes across
and it also checks any taxa it comes across against the preceeding ancestor for differences and increments the ratios until
the end of the tree is found.
This method has an advantage in thata it will only count a mutation when it occurs in the tree, and not count for every occurance of the
difference that decendants may contain. It also ensures that every difference is counted.
*/
void find_polymorphisms(struct node *position, int **ratio, int **ratio1, int *count, int i, int j)
{
int value = 0;
if(codons[position->ances_seq[i]][j] != codons[(position->prev)->ances_seq[i]][j])
{
value = (position->prev)->ances_seq[i] - transform_base(codons[(position->prev)->ances_seq[i]][j], j) + transform_base(codons[position->ances_seq[i]][j], j);
if(genetic_codes[code][value] != genetic_codes[code][(position->prev)->ances_seq[i]])
{
ratio[*count][1] = ratio1[*count][1] + 1;
}
else
{
ratio[*count][3] = ratio1[*count][3] + 1;
}
}
if(position->seq_num1 != NULL)
if(codons[(position->seq_num1)->bases[i]][j] != codons[position->ances_seq[i]][j])
{
value = position->ances_seq[i] - transform_base(codons[position->ances_seq[i]][j], j) + transform_base(codons[(position->seq_num1)->bases[i]][j], j);
if(genetic_codes[code][value] != genetic_codes[code][position->ances_seq[i]])
{
ratio1[*count][1] = ratio[*count][1] + 1;
ratio[*count][1] = ratio1[*count][1] + 1;
}
else
{
ratio1[*count][3] = ratio[*count][3] + 1;
ratio[*count][3] = ratio1[*count][3] + 1;
}
}
if(position->seq_num2 != NULL)
if(codons[(position->seq_num2)->bases[i]][j] != codons[position->ances_seq[i]][j])
{
value = position->ances_seq[i] - transform_base(codons[position->ances_seq[i]][j], j) + transform_base(codons[(position->seq_num2)->bases[i]][j], j);
if(genetic_codes[code][value] != genetic_codes[code][position->ances_seq[i]])
{
ratio1[*count][1] = ratio[*count][1] + 1;
ratio[*count][1] = ratio1[*count][1] + 1;
}
else
{
ratio1[*count][3] = ratio[*count][3] + 1;
ratio[*count][3] = ratio1[*count][3] + 1;
}
}
if(position->node1 != NULL) find_polymorphisms(position->node1, ratio, ratio1, count, i, j);
if(position->node2 != NULL) find_polymorphisms(position->node2, ratio, ratio1, count, i, j);
}
/* This funcction implements a new rule for whether a mutation is fixed or not. The rule is as follows:
If the path from the internal branch we are at to the root (not counting the root as we can't resolve
an ambiguity at the root) doesn't contain an ancestor or *nucliotide* the same as the ancestor at the
internal branch we are at, then the mutation is fixed at that branch. - The mutation must be fixed in
that clade also- */
void check_fixed_ances_out(struct node *position, int *fixed, char *nuc, int i, int j)
{
if(codons[position->ances_seq[i]][j] == *nuc) *fixed = FALSE;
if(position->seq_num1 != NULL)
if(codons[(position->seq_num1)->bases[i]][j] == *nuc) *fixed = FALSE;
if(position->seq_num2 != NULL)
if(codons[(position->seq_num2)->bases[i]][j] == *nuc) *fixed = FALSE;
if(position->prev != NULL)
{
if(*fixed) check_fixed_ances_out(position->prev, fixed, nuc, i, j);
}
}
/* Called by is_fixed to travel down the tree from the node we are at to check if the nucliotide was fixed in that branch */
void check_fixed_in(struct node *position, int *fixed, char *nuc, int i, int j)
{
if(position->seq_num1 != NULL)
if(*nuc != codons[(position->seq_num1)->bases[i]][j]) *fixed = FALSE;
if(position->seq_num2 != NULL)
if(*nuc != codons[(position->seq_num2)->bases[i]][j]) *fixed = FALSE;
if(position->node1 != NULL && fixed) check_fixed_in(position->node1, fixed, nuc, i, j);
if(position->node2 != NULL && fixed) check_fixed_in(position->node2, fixed, nuc, i, j);
}
/* This checks to make sure that the mutation doesn't occur in any other position on the tree othe than that specified
by place (and children) */
void check_fixed_out(struct node *position, struct node *place, int *fixed, char *nuc, int i, int j )
{
if(position != place)
{
if(position->seq_num1 != NULL)
if(*nuc == codons[(position->seq_num1)->bases[i]][j]) *fixed = FALSE;
if(position->seq_num2 != NULL)
if(*nuc == codons[(position->seq_num2)->bases[i]][j]) *fixed = FALSE;
if(position->node1 != NULL && fixed) check_fixed_out(position->node1, place, fixed, nuc, i, j);
if(position->node2 != NULL && fixed) check_fixed_out(position->node2, place, fixed, nuc, i, j);
}
}
/* This function travels through the tree, printing out the names of the sequences to check that the tree is being built properly */
void check_tree(struct node *position, int *count)
{
printf("(");
if(position->node1 != NULL)
{
check_tree(position->node1, count);
printf(",");
}
if(position->seq_num1 != NULL)
{
printf("%s,", position->seq_num1->name);
}
if(position->node2 != NULL) check_tree(position->node2, count);
if(position->seq_num2 != NULL)
{
printf("%s", position->seq_num2->name);
}
printf(")");
*count = *count+1;
/* if(position->node1 != NULL) printf("node1 is assigned! %d\n", *count);
if(position->seq_num1 != NULL)
{
printf("seqnum1\n%s %d\n", (position->seq_num1)->name, *count);
printf("%s %d\n", (position->seq_num1)->nickname, *count);
printf("sequence no:%d %d\n", (position->seq_num1)->seq_num, *count);
printf("TAG:%d %d\n", (position->seq_num1)->tag, *count);
printf("Outgroup: %d %d\n", (position->seq_num1)->outgroup, *count);
printf("Length: %d %d\n", (position->seq_num1)->length, *count);
printf("Numofstopcodons%d %d\n", (position->seq_num1)->numofstpcodons, *count);
}
if(position->node2 != NULL) printf("node2 is assigned! %d\n", *count);
if(position->seq_num2 != NULL)
{
printf("seqnum2\n%s %d\n", (position->seq_num2)->name, *count);
printf("%s %d\n", (position->seq_num2)->nickname, *count);
printf("sequence no:%d %d\n", (position->seq_num2)->seq_num, *count);
printf("TAG:%d %d\n", (position->seq_num2)->tag, *count);
printf("Outgroup: %d %d\n", (position->seq_num2)->outgroup, *count);
printf("Length: %d %d\n", (position->seq_num2)->length, *count);
printf("Numofstopcodons%d %d\n", (position->seq_num2)->numofstpcodons, *count);
}
if(position->seq_num1 == NULL && position->seq_num2 == NULL) printf("-\n");
*/
}
/* function starts the checking process by calling assign_ances_up, and assign_ances_down which are both recursive and calls assign_codon_num, to calculate the ancestral sequence */
void ancestral_nuc(int i, int j, float *** subst_matrix)
{
struct node *position = NULL;
position = tree_top;
assign_ances_up(i, j, position);
assign_root_ances(i,j); /* this tries to solve ambiguities at the root */
assign_ances_down(position, subst_matrix);
assign_codon_num(i, j, position);
}
/* This function assigns the codon number to the ancestral sequence data in each node, depending on the position of the current nucliotide */
void assign_codon_num(int i, int j, struct node *position)
{
if(position->node1 != NULL) assign_codon_num(i, j, position->node1);
if(position->node2 != NULL) assign_codon_num(i, j, position->node2);
if( j == 0) position->ances_seq[i] = 0;
position->ances_seq[i] += transform_base(position->ancestor[0], j);
/* if(position == tree_top && i == 0) printf("ances tree_top j = %d - %d >> ances = %c\n", j, position->ances_seq[i], position->ancestor[0]);
if(position == tree_top && i == 0) printf("ances next j = %d - %d >> ances = %c\n", j, (position->node1)->ances_seq[i], (position->node1)->ancestor[0]);
*/ if(position->ances_seq[i] > 64) position->ances_seq[i] = 64;
}
/* This function travels up through the tree assigning the ancestral nucliotides */
void assign_ances_up(int i, int j, struct node *position)
{
char tmp[5] = {'\0','\0','\0','\0','\0'}, *pointer = NULL;
int l = 0, k = 0;
if(position->node1 != NULL) assign_ances_up(i, j, position->node1);
if(position->node2 != NULL) assign_ances_up(i, j, position->node2);
for(l=0; l<5; l++) position->ancestor[l] = '\0';
l = 0;
/*First assign those nucliotides which are at this node to the ancestors */
if(position->seq_num1 != NULL)
{
position->ancestor[0] = codons[(position->seq_num1)->bases[i]][j];
position->ancestor[1] = '\0';
}
if(position->seq_num2 != NULL && position->seq_num1 == NULL)
{
position->ancestor[0] = codons[(position->seq_num2)->bases[i]][j];
position->ancestor[1] = '\0';
}
if(position->seq_num2 != NULL && position->seq_num1 != NULL)
{
if(codons[(position->seq_num1)->bases[i]][j] == codons[(position->seq_num2)->bases[i]][j])
{ }
else
{
position->ancestor[1] = codons[(position->seq_num2)->bases[i]][j];
position->ancestor[2] = '\0';
}
}
/* Second, check any children to see what the ancestor was assigned for those */
if(position->node1 != NULL)
{
strcpy(tmp, (position->node1)->ancestor);
while((pointer = strpbrk(tmp, position->ancestor)) != NULL)
{
pointer[0] = 'q';
}
if((pointer = strchr(tmp, 'q')) == NULL)
{
strcat(position->ancestor, tmp);
}
else
{
l = 0;
for(k=0; k<5; k++)
if(tmp[k] == 'q')
{
position->ancestor[l] = (position->node1)->ancestor[k];
l++;
}
position->ancestor[l] = '\0';
}
}
if(position->node2 != NULL)
{
strcpy(tmp, (position->node2)->ancestor);
while((pointer = strpbrk(tmp, position->ancestor)) != NULL)
{
pointer[0] = 'q';
}
if((pointer = strchr(tmp, 'q')) == NULL)
{
strcat(position->ancestor, tmp);
}
else
{
l = 0;
for(k=0; k<5; k++)
if(tmp[k] == 'q')
{
position->ancestor[l] = (position->node2)->ancestor[k];
l++;
}
position->ancestor[l] = '\0';
}
}
}
/* This function simply solves abiguities at the root, before assign_ances_down is called, so that there can never be an ambiguity in the tree. */
/* It solves ambiguities by assigning the ancesral root to that of the outgroup */
/* this however still leaves the chance for ambiguous sites, with an arbitrary descision being made if there is ambiguity a the top of the */
/* clade that defines the outgroup: For these purposes, you can only garantee unambiguous descisions if you only define ONE out group */
/* However this is not a great way of solving this, the other way would be to define the outgroup in the middle of the outgroup, so that the */
/* tree would still be rooted correctly, and the separation between the outgroup and the rest would be taken away from the root, where the ambiguities */
/* may lie. */
void assign_root_ances(int i, int j)
{
int x = 0, found = FALSE;
while(tree_top->ancestor[x] != '\0') x++;
if(x > 1)
{
/* if the tree is rooted about a single sequence */
if(tree_top->seq_num1 != NULL)
{
if((tree_top->seq_num1)->outgroup == TRUE)
{
tree_top->ancestor[0] = codons[(tree_top->seq_num1)->bases[i]][j];
found = TRUE;
}
}
if(tree_top->seq_num2 != NULL)
{
if((tree_top->seq_num2)->outgroup == TRUE)
{
tree_top->ancestor[0] = codons[(tree_top->seq_num2)->bases[i]][j];
found = TRUE;