-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevolve.c
1564 lines (1272 loc) · 37.5 KB
/
evolve.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
//
//
/*****************************************************************************/
/***************************************************************************************************************/
/* This program reads in a fasta formatted file which contains multiple sequences along with names etc.... */
/* The sequences are read into a linked list of structures which record the name, length, number and tag of */
/* the sequence. Each structure conatins a dynamically allocated array, which stores the data representing the */
/* sequence itself. It runs through the input file reading in nucliotide triplets and allocates them a number */
/* which represents the codon (see the Global array transform_values). */
/* The program then asks whether the user specified output file is to be in codon number format, or in amino */
/* acid format. If amino acid is selected, then the program asks in which genetic code the input file is */
/* written, there are a choice of 13 (see the global array genetic_codes). The amino acids are then outputted */
/* to the file along with the name, sequence length etc.. for each sequence in the input file. */
/* */
/* Programmed by Chris Creevey 13-1-2000 Bioinformatics Lab, NUI Maynooth Ireland. */
/***************************************************************************************************************/
#include "definitions.h"
#include "evolve.h"
#include "adaptive_tree.h"
#include "linked tree.h"
#include "Li_Wu_19851993.h"
int main(void)
{
start = NULL;
last = NULL; /* initialise top and bottom pointers of the sequence in memory */
li_wu_start = NULL;
li_wu_end = NULL; /* initialise top and bottom pointers of the li_wu results */
printf("\n\n\n\n\n\n\n\n");
main_menu();
/*
open_input_file();
open_output_file();
McDonald_Kreitman();
*/
if(file != NULL) fclose(file);
if(outfile != NULL) fclose(outfile);
if(dist != NULL) fclose(dist);
if(outtree != NULL) fclose(outtree);
outtree = NULL;
printf("\n\nFinished!!!!\n\n");
clean_exit();
return(0);
}
void main_menu(void)
{
int exit = FALSE, done = FALSE;
int choice =0;
do
{
choice = 0;
printf("\n\t\t***************************************************");
printf("\n\t\t* Main menu *");
printf("\n\t\t* Crann 1.2 *");
printf("\n\t\t* *");
printf("\n\t\t* Copyright (c) 2016 Chris Creevey*");
printf("\n\t\t***************************************************\n\n");
printf("\t1 = Read new input file < Current input file = ");
if(file == NULL) printf("None>\n");
else printf("%s>\n", filename);
printf("\t2 = Open new output file < Current output file = ");
if(outfile == NULL) printf("None>\n");
else printf("%s>\n", outfilename);
printf("\t3 = Output sequences from memory < Sequences in memory = %d>\n", num_of_seqs);
printf("\t4 = Perform Phylogenetic Relative Rate Ratio Test (Creevey & McInerney 2002)\n");
printf("\t5 = Calculate all dN/dS pairwise distances\n");
printf("\t6 = Calculate all dN/dS pairwise distances as a moving window along alignment\n");
printf("\t7 = Options\n");
printf("\t8 = About Crann\n");
printf("\t9 = Quit program\n");
printf("\n\n\n");
if(start == NULL && done == FALSE)
choice = getint("\n\tPlease select an action (default = 1) ", 1, 9, 1);
if(start != NULL && outfile == NULL && done == FALSE)
choice = getint("\n\tPlease select an action (default = 2) ", 1, 9, 2);
if(start != NULL && outfile != NULL && done != TRUE)
choice = getint("\n\tPlease select an action (default = 4) ", 1, 9, 4);
if(done == TRUE)
choice = getint("\n\tPlease select an action (default = 9) ", 1, 9, 9);
switch(choice)
{
case 1:
open_input_file();
printf("\n\n\n");
break;
case 2:
open_output_file();
printf("\n\n\n\n\n\n\n\n\n");
break;
case 3:
show();
printf("\n\n\n");
break;
case 4:
if(check_files())
{
printf("starting :Li_Wu\n");
/* Li_Wu(); */
printf("Finished :Li_Wu\n");
/* if(check_distances() == TRUE)
{
printf("\nSome sequences were too distantly related from each other to accuratley\ncalculate the Li distances.");
printf(" In these cases the Li value was set to\ntwice the largest calculated value in the matrix\n");
} */
printf("starting :McD\n");
McDonald_Kreitman();
printf("Finished :McD\n");
done = TRUE;
}
break;
case 5:
if(check_files())
{
Li_Wu();
printf("Finished :Li_Wu\n");
allocate_distances(2);
printf("Finished :allocate_distances\n");
done = TRUE;
}
break;
case 6:
if(check_files()) Li_Wu_movwin();
done = TRUE;
break;
case 7:
if(check_files()) general_options();
break;
case 8:
Splash();
break;
case 9:
exit = TRUE;
break;
default:
choice = 0;
}
}while(!exit|| choice == 0);
}
void general_options(void)
{
int choice = 0;
int count = 0, ans = 0;
do{
printf("\n\n\n\n\n\n\n\n\t\t***************************************************");
printf("\n\t\t* General Options Menu *");
printf("\n\t\t* Crann 1.1 *");
printf("\n\t\t* by Chris Creevey*");
printf("\n\t\t***************************************************\n\n");
printf("\t1 = Genetic Code is %s\n", name_code(code) );
printf("\t2 = Deletion of Non-Standard Characters = ");
if(gen_opt[0] == 0) printf("Pairwise\n"); else printf("Complete\n");
printf("\t3 = Deletion of Stop Codons = ");
if(gen_opt[1] == 0) printf("Pairwise\n"); else printf("Complete\n");
printf("\t4 = Analyse all sequences in memory? = ");
if(gen_opt[2] == 0) printf("No, # omitted = %d\n", untagged); else printf("Yes\n");
printf("\t5 = Analyse whole sequence length? = ");
if(gen_opt[3] == 0) printf("No, start = %d, end = %d\n", startw, endw); else printf("Yes\n");
printf("\t6 = Which Li method to use? = ");
if(gen_opt[4] == 0) printf("1993\n"); else printf("1985\n");
printf("\t7 = Input Phylogentic tree? = ");
if(gen_opt[5] == 0) printf("No\n"); else printf("Yes, File = %s\n", nestname);
if(gen_opt[5] == 0)
{
printf("\t8 = Build a neighbour joining tree with which distances? = ");
switch(gen_opt[6])
{
case 0:
printf("Dn\n");
break;
case 1:
printf("Ds\n");
break;
default:
printf("Dn/Ds\n");
break;
}
}
printf("\n\t0 = Return to main menu (default)\n");
/* Use for Mac version */
/* printf("\n\nEnter a number to change that option, or press return to continue\n\n\n\n\n\n");
ans = getch(); */
/* Use for PC version */
ans = getint("\n\nEnter a number to change that option, or press return to continue\n", 0, 9, 0 );
switch(toupper(ans))
{
case 1 :
choose_code();
if(gen_opt[1] == 1) delete_stpcodons();
checkdata();
break;
case 2:
if(found() == TRUE)
{
gen_opt[0] = 1;
delete_nonstd(64);
}
else
{
gen_opt[0] = 0;
undelete_nonstd(64);
}
break;
case 3:
if(found() == TRUE)
{
gen_opt[1] = 1;
delete_stpcodons();
}
else
{
gen_opt[1] = 0;
undelete_stpcodons();
}
break;
case 4:
which_sequences();
if(untagged == 0) gen_opt[2] = 1; else gen_opt[2] = 0;
break;
case 5:
printf("\n\n\n\n\n\n\n\n\n\n\n\n");
window_size(&startw, &endw);
if(startw == 0 && endw == (start->length/3 -1)) gen_opt[3] = 1; else gen_opt[3] = 0;
break;
case 6:
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\tWhich method would you like to use? ");
printf("\n\t1 = 1985 method ");
printf("\n\t2 = 1993 method ");
choice = getint("\n\n\tPlease choose either 1 or 2", 1,2,2);
switch(choice)
{
case 1:
gen_opt[4] = 1;
break;
default:
gen_opt[4] = 0;
break;
}
break;
case 7:
if(tree_top != NULL) dismantle(tree_top, &count);
if(tree_choice() == 1) gen_opt[5] = 1; else gen_opt[5] = 0;
break;
case 8:
/* choose whether to use Ks or Ka values */
choice = 0;
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\tWhich distances would you like to use in the algorithm\n");
printf("\t1 = Dn values\n");
printf("\t2 = Ds values\n");
printf("\t3 = Rate of evolution (Dn/Ds)\n");
choice = getint("\n\tPlease choose either 1, 2 or 3\n", 1,3,1);
switch(toupper(choice))
{
case 1:
gen_opt[6] = 0;
break;
case 2:
gen_opt[6] = 1;
break;
default:
gen_opt[6] = 2;
break;
}
break;
default:
ans = -1;
break;
}
}while(ans != -1);
printf("\n\n\n\n\n\n\n\n");
}
void delete_nonstd(int num)
{
struct sequence *position = start;
int i = 0;
while(position != NULL)
{
i = 0;
while(position->bases[i] != 193)
{
if(position->bases[i] == num) deletion[i] = TRUE;
i++;
}
position = position->next;
}
}
void undelete_nonstd(int num)
{
struct sequence *position = start;
int i = 0;
while(position != NULL)
{
i = 0;
while(position->bases[i] != 193)
{
if(position->bases[i] == num) deletion[i] = FALSE;
i++;
}
position = position->next;
}
}
void delete_stpcodons(void)
{
struct sequence *position = start;
int i = 0;
for(i=0; i<(start->length/3) -1; i++) deletion[i] = FALSE; /* reset the array */
if(gen_opt[0] == 1) delete_nonstd(64); /* if we want the non std chars deleted */
while(position != NULL)
{
i = 0;
while(position->bases[i] != 193)
{
if(genetic_codes[code][position->bases[i]] == 0)
{
deletion[i] = TRUE;
}
i++;
}
position = position->next;
}
}
void undelete_stpcodons(void)
{
int i =0;
for(i=0; i<(start->length/3) -1; i++) deletion[i] = FALSE; /* reset the array */
if(gen_opt[0] == 1) delete_nonstd(64); /* if we want the non std chars deleted */
}
void Splash(void)
{
char ans[80];
printf("\n\n");
printf("/**********************************************************************/\n");
printf("/* Crann 1.2 */\n");
printf("/* */\n");
printf("/* Written by: Chris Creevey */\n");
printf("/* Initial release April 2003; Current release October 2016 */\n");
printf("/* Address: */\n");
printf("/* Ecological and Evolutionary Genomics Laboratory, */\n");
printf("/* IBERS, */\n");
printf("/* Aberystwyth University, */\n");
printf("/* Aberystwyth, */\n");
printf("/* SY23 2DA, */\n");
printf("/* United Kingdom. */\n");
printf("/* */\n");
printf("/* Email: chris.creevey@gmail.com */\n");
printf("/* Web: http://www.creeveylab.org */\n");
printf("/* */\n");
printf("/* */\n");
printf("/* Any and all suggestions and/or comments are welcome. */\n");
printf("/* */\n");
printf("/* Copyright: Chris Creevey 2003-2016 */\n");
printf("/**********************************************************************/\n");
printf(" Press return to continue: ");
xgets(ans);
printf("\n\n\n\n\n\n\n\n");
}
int check_files(void)
{
int pass = TRUE;
if(start == NULL)
{
printf("\n\nNo sequences have been read into memory, \nPlease select to read in a new input file from the main menu\n");
pass = FALSE;
}
else
{
if(outfile == NULL)
{
printf("\n\nNo output file has been specified, \nPlease select to open a new output file from the main menu\n");
pass = FALSE;
}
}
return(pass);
}
void open_input_file(void)
{
char choice;
int fastaformat, exit = FALSE, i =0;
if(file != NULL)
{
fclose(file); /* If there has been an input file opened already, close it before opening the new one */
file = NULL;
}
clear_memory();
for(i=0; i<100; i++) nonstandchars[i] = '\0'; /* initialise nonstandchars array before reading new file */
do{
do{
fastaformat = TRUE;
/*printf("\n\n\tName of the fasta formatted file "); */
getstr("\n\n\tName of the fasta formatted file", string1);
/*scanf("%s%c", string1, &overflow); */
filename[0] = '\0'; strcpy(filename, string1);
if((file = fopen(filename, "r")) == NULL) /* check to see if the file is there */
{
printf("\n\n\tCannot open the sequence file, named %s\n\n", filename);
choice = getletter("\n\n\tPress 1 to return to the Main Menu\n\tOR press 2 to try again");
if(toupper(choice) == '1') exit = TRUE;
}
}while(file == NULL && !exit);
if(!exit)
{
printf("opened %s.....\n", filename);
fastaformat = read_file(fastaformat);
if(!fastaformat)
{
choice = getletter("\n\n\tPress 1 to return to the Main Menu\n\tOr press 2 to try a different file");
if(toupper(choice) == '1') exit = TRUE;
}
}
}while(!fastaformat && !exit );
if(!exit)
{
checkdata();
/* codon_usage();
AAmakeup();
count_bases();
*/
}
}
void open_output_file(void)
{
char choice;
int exit = FALSE;
if(outfile != NULL)
{
fclose(outfile); /* If there has been an output file opened already, close it before opening the new one */
outfile = NULL;
}
do{
/*printf("\n\n\tName of the output file: ");
scanf("%s", string1); */
getstr("\n\n\tName of the output file", string1 );
outfilename[0] = '\0'; strcpy(outfilename, string1);
if((outfile = fopen(outfilename, "w")) == NULL) /* check to see if the file can be opened/created */
{
printf("\n\n\tCannot open the output file, named %s\n\n", outfilename);
choice = getletter("\n\n\tPress 1 to return to the Main Menu\n\nOR press 2 to try again: ");
if(toupper(choice == '1')) exit = TRUE;
}
}while(outfile == NULL && !exit);
}
void show(void)
{
int choice1 = 0;
if(check_files())
{
choice1 = getint("\n\n\n Please select the output file format:\n\t1 = Codon numbers\n\t2 = Amino acid format\n\t3 = Dna sequences in tab delimited format (in columns)\n\t4 = Return to the main menu\n\n\t please select 1, 2, 3 or 4: ", 1,4,4);
switch(choice1)
{
case 1:
show_codons();
break;
case 2:
show_Amino_Acids();
break;
case 3:
show_tab_dna();
break;
default:
break;
}
}
}
void show_tab_dna(void)
{
struct sequence *seq = start;
int i = 0, j = 0;
while(seq != NULL)
{
fprintf(outfile, "%s\t", seq->name);
seq = seq->next;
}
fprintf(outfile, "\n");
seq = start;
while(seq->bases[i] != 193)
{
while(seq != NULL)
{
for(j=0; j<3; j++)
{
fprintf(outfile, "%c", codons[genetic_codes[code][seq->bases[i]]][j]);
}
fprintf(outfile, "\t");
seq = seq->next;
}
fprintf(outfile, "\n");
i++;
seq = start;
}
}
int choose_code(void)
{
int i;
printf("\n\n\n\n\n\n\n\n\n\n\n\n");
printf("\n\nPlease specify which genetic code the input file contains:\n\n");
printf("\t\t 1 = Universal (default)\n");
printf("\t\t 2 = Vertebrate standard mitochondrial\n");
printf("\t\t 3 = Yeast mitochondrial\n");
printf("\t\t 4 = Mycoplasma/Spiroplasma/Mold/Protozoan/Coelenterate\n");
printf("\t\t 5 = Invertebrate mitochondrial\n");
printf("\t\t 6 = Ciliate\n");
printf("\t\t 7 = Echinoderm mitochondrial\n");
printf("\t\t 8 = Euplotid\n");
printf("\t\t 9 = Bacterial (same as universal)\n");
printf("\t\t 10= Alternative Yeast Nuclear\n");
printf("\t\t 11= Ascidian mitochondrial\n");
printf("\t\t 12= Flatworm mitochondrial\n");
printf("\t\t 13= Blepharisma Nuclear\n");
i = getint("\n\n\t please select one of 1 to 13 from above: ", 1,13,1);
code = i -1;
return(i);
}
int read_file(int fastaformat)
{
struct sequence *new = NULL;
int i=0, j=0;
char c;
while(!feof(file) && ((c= getc(file)) == ' ' || c == '\t' || c == '\n')); /* skip past invisible characters */
if(c == '>')
{
fastaformat = TRUE;
j = 0;
do{
new = malloc(sizeof(list_entry));
if(new == NULL)
{
printf("\n\t Out of memory\n");
clean_exit();
}
c = read_sequence(j, new);
j++;
new=NULL;
}while(!feof(file) && (c == '>'));
}
else
{
/* if the first character in the file is not a '>' then we assume the format is wrong */
printf("\n\n\tThis does not seem to be a fasta formatted file\n\n");
fastaformat = FALSE;
}
num_of_seqs = j;
/* link the sequences created to a new array */
allseqslist=malloc(num_of_seqs*sizeof(struct sequence *));
for(i=0; i<num_of_seqs; i++) allseqslist[i] = NULL;
new=start;
while(new != NULL) {
allseqslist[new->seq_num] = new;
new=new->next;
}
return(fastaformat);
}
char read_sequence(int seq_num, struct sequence *new)
{
char c = '\0';
int i = 0, j = 0, place = 0, value = 0, memory_allocations = 0;
/* assign the sequence number */
new->seq_num = seq_num;
/* initialise the tag (equal to TRUE) */
new->tag = TRUE;
/* All sequences are assumed to not be in the outgroup in the begining */
new->outgroup = FALSE;
/* read in the name of the sequence */
j = 0;
c = getc(file);
while(c == ' ' && !feof(file)) c = getc(file);
do{
new->name[j] = c;
if(j < maxnamlen-1) j++;
}while(!feof(file) && (c = getc(file)) != '\n' && c != '\r');
new->name[j] = '\0'; /* append a '\0' terminator */
/* create the nickname */
for(i=0; i<19; i++)
{
if(new->name[i] != '\0' && new->name[i] != ' ')
{
new->nickname[i] = new->name[i];
}
else
{
new->nickname[i] = '\0';
i=19;
}
}
new->nickname[i] = '\0';
/* read in the sequence */
i = 0;
j = 0;
new->bases = malloc((STD_CODON_NUM * (sizeof(int)))); /* Allocate the memory to store the sequence in codons */
memory_allocations = 1;
c = getc(file);
do{
place = 0;
value = 0;
do{
if(c != '\n' && c != '\r' && !feof(file) && c != ' ') /* if not an end of line or end of file */
{
value = value + transform_base(c, place); /* call transform_base to calculate the codon number */
place++;
j++;
}
}while(!feof(file) && ((c= getc(file)) != '>') && (place < 3));
if(value > 64) value = 64; /* if there is a gap, then the codon value is assigned to 64 */
if(i >= ((STD_CODON_NUM * memory_allocations) - 1)) /* if the sequence has exceeded the length of STD_CODON_NUM * the number of memory allocations */
{
memory_allocations++;
new->bases = realloc(new->bases, ((STD_CODON_NUM * (sizeof(int))) * memory_allocations)); /* reallocate the memory space to the size required to fit the STD_CODON_NUM * new memory allocations */
if(new->bases == NULL)
{
printf("\n\t Out of memory\n");
clean_exit();
}
}
if(c != '>' && place == 3) /* the check to make sure c is not a space is incase there are any spaces at the end of the file */
{
new->bases[i] = value;
i++;
}
}while((c != '>') && !feof(file));
new->bases[i] = 193; /* 193 is the terminator value for the sequence */
/* assign the length of the sequence */
new->length = j;
/* initialise the numofstpcodons value */
new->numofstpcodons = 0;
/* initialise the gaprun value (Used in McD&K algorithm) */
new->gaprun = 0;
/* initialise the stopcodon array */
new->stopcodons = (malloc(1 * (sizeof (int))));
if(new->stopcodons == NULL)
{
printf("\n\t Out of memory\n");
clean_exit();
}
/* now assign the pointers to the previous and next sequence */
if (last == NULL || start == NULL) /* If this is a new list */
{
last = new;
start = new;
new->next = NULL;
new->previous = NULL;
}
else
{
last->next = new;
new->next = NULL;
new->previous = last;
last = new;
}
return(c);
}
char getletter(char *instr)
{
char outchar = '\0';
printf("%s: ", instr);
outchar = getc(stdin); /* Use for Mac version */
/* scanf("%1c%c", &outchar, &overflow); */ /* Use for PC version */
return(outchar);
}
void getstr(char *instr, char *outstr)
{
printf("%s: ", instr);
xgets(outstr);
}
void clear_memory(void)
{
int i;
struct sequence *new = NULL;
struct synon *place = NULL;
if(start != NULL)
{
while(start != NULL)
{
new = start->next;
free(start->bases);
free(start);
start = new;
}
start = NULL;
last = NULL;
}
if(li_wu_start != NULL)
{
while(li_wu_start != NULL)
{
place = li_wu_start->next;
free(li_wu_start->Ks);
free(li_wu_start->varKs);
free(li_wu_start->Ka);
free(li_wu_start->varKa);
free(li_wu_start);
li_wu_start = place;
}
li_wu_start = NULL;
li_wu_end = NULL;
}
if(deletion != NULL)
{
free(deletion);
deletion = NULL;
}
num_of_seqs = 0;
if(outgroup != NULL)
{
free(outgroup);
outgroup = NULL;
}
if(distances != NULL)
{
for(i=0; i<num_of_seqs-untagged; i++)
free(distances[i]);
free(distances);
}
distances = NULL;
if(file != NULL)
{
fclose(file);
file = NULL;
}
}
void clear_results(void)
{
struct synon *place = NULL;
if(li_wu_start != NULL)
{
while(li_wu_start != NULL)
{
place = li_wu_start->next;
free(li_wu_start->Ks);
free(li_wu_start->varKs);
free(li_wu_start->Ka);
free(li_wu_start->varKa);
free(li_wu_start);
li_wu_start = place;
}
li_wu_start = NULL;
li_wu_end = NULL;
}
}
/* This file counts the codon usage data for the dataset entered and writes it to the file codonUsage.out */
void codon_usage(void)
{
struct sequence *position = start;
int i;
float usage[64], total = 0;
for(i=0; i<64; i++) usage[i] = 0;
while(position != NULL)
{
i = 0;
while(position->bases[i] != 193)
{
if(position->bases[i] < 64)
{
usage[position->bases[i]]++;
total++;
}
i++;
}
position = position->next;
}
if(usagefile != NULL)
{
fclose(usagefile); /* If there has been an output file opened already, close it before opening the new one */
usagefile = NULL;
}
if((usagefile = fopen("codonUsage.out", "w")) == NULL)
{
printf("Could not open cononUsage.out\n codon usage data not written\n");
}
else
{
for(i=0; i<64; i++)
{
fprintf(usagefile, "%f ", usage[i]/total);
if(fmod(i+1,4) == 0 && i != 0) fprintf(usagefile, "\n");
}
printf("Codon usage data written to codonUsage.out");
}
}
void show_codons(void)
{
int i;
struct sequence *info = start;