-
Notifications
You must be signed in to change notification settings - Fork 1
/
BASE.sh
executable file
·1435 lines (844 loc) · 55.7 KB
/
BASE.sh
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
#!/bin/bash
for arg in "$@"; do
shift
case "$arg" in
"--requirements") set -- "$@" "-g" ;;
"--analyze") set -- "$@" "-x" ;;
"--extract") set -- "$@" "-j" ;;
"-mg") set -- "$@" "-a" ;;
"-ma") set -- "$@" "-b" ;;
"-l2") set -- "$@" "-q" ;;
"--input") set -- "$@" "-i" ;;
"--output") set -- "$@" "-o" ;;
"-st") set -- "$@" "-s" ;;
"-gt") set -- "$@" "-g" ;;
"--s_tree") set -- "$@" "-s" ;;
"--g_tree") set -- "$@" "-g" ;;
"--model_g") set -- "$@" "-a" ;;
"--model_a") set -- "$@" "-b" ;;
"--cores") set -- "$@" "-c" ;;
"--verbose") set -- "$@" "-v" ;;
"--replicates") set -- "$@" "-r" ;;
"--min_spp") set -- "$@" "-m" ;;
"--ubiquitous") set -- "$@" "-u" ;;
"--labels") set -- "$@" "-l" ;;
"--labels_2") set -- "$@" "-q" ;;
"--outgroups") set -- "$@" "-k" ;;
"--erase") set -- "$@" "-e" ;;
"--help") set -- "$@" "-h" ;;
*) set -- "$@" "$arg"
esac
done
while getopts ":xgji:o:s:ga:b:c:q:ek:l:vr:um:h" o; do
case "${o}" in
i) input_folder=${OPTARG}
;;
o) output_folder=${OPTARG}
;;
s) s_tree=${OPTARG}
;;
g) g_tree=1
;;
a) codeml_template_1=${OPTARG}
;;
b) codeml_template_2=${OPTARG}
;;
c) n_threads=${OPTARG}
;;
v) verbose=1
;;
r) rep=${OPTARG}
;;
m) min_otu=${OPTARG}
;;
u) missing_data=1
;;
g) g=1
;;
x) x=1
;;
j) j=1
;;
l) labels=${OPTARG}
;;
q) labels2=${OPTARG}
;;
k) outgroups=${OPTARG}
;;
e) e=1
;;
################################################################################################################################################################################################################### ANALYZE! HELP
h) if [ "$x" == 1 ]
then echo "
The analyze step of the workflow carries out branchlength optimization on the species-tree or gene-tree inference,then fits two models and compares them thourgh a LRT.
A likelihood summary table will be produced, which will show for each gene wether the general or the alternative model fits best to the data;
moreover the best-fit model output file will be returned. It's possible to label multiple branches and/or clades and branch-site, branch and site models are supported.
List of arguments:
-i --input path to the input folder containing coding sequence alignments in fasta format and with the .fa extension - headers must match with the spp. in the tree.
-st --s_tree tree including all species, in newick format and with the .nwk extension - tips must match whith the fasta headers in the sequence alignments.
-gt --g_tree the gene tree will be used for the analyses - please note: using the gene tree can cause some clades to be no more present in the phylogeny.
-mg --model_g codeml .ctl file of the generaly model, configured for the analysis (i.e. with the fields seqfile, oufile, treefile and omega left empty).
-ma --model_a codeml .ctl file of the alternative model, configured for the analysis (i.e. with the fields seqfile, oufile, treefile and omega left empty).
-c --cores maximum number of cores to be used by the analysis.
-o --output output folder.
-l --labels file with the branch / clade labels for model 2 analyses - each line must have all the relative species followed by the label ($ or #).
-l2 --labels_2 a second file with branch / clade labels, used to test two model 2 analyses VS each other.
-v --verbose verbose mode keeps the temporary folder wich contains all the intermediate files of the analyses.
-r --replicates number of replicates to be performed - if not specified it will be set to 1.
-u --ubiquitous analyze only ubiquitous genes.
-e --erase erase previous output folders with the same name as the specified output.
-h --help this help page.
"
################################################################################################################################################################################################################### ANNOTATE HELP
elif [ "$j" == 1 ] ;
then echo "
This extract step of the workflow:
a) annotates the internal nodes of each tree to match the output of codeml and lists all species associated to each branch.
b) extracts the metrics relative to each branch / clade of interest.
List of non-optional arguments:
-i --input the path to an input folder containing codeml outputs (.out extension is required). If no label is specified it will just annotate codeml outputs.
List of optional argument:
-l --labels the path to a file containing the branches/clades for which the metrics have to be extracted - on each line all the relative species followed by the name.
-m --min_spp min. number of spp. to be present for branches/clades to be considered - numbers and proportions allowed. If "x" is specified, only complete branches / clades are considered.
-v --verbose verbose mode prints also the tree branch (matching codeml output) and the species associated to the branch.
-h --help this help page.
"
################################################################################################################################################################################################################### MAIN HELP
else echo "
BASE - pronunced /'baze/ - is a workflow to test Branch And Site Evolution.
It has been made to:
1) analyze fits a general and an alternative model to each gene and compares them thourgh a LRT.
2) extract retrive metrics of branches and/or clades, allowing a treshold for missing species.
More information on each mode usage and options can be accessed by typing "--analize" or "--extract" followed by "-h".
BASE relies on the following softwares:
RAxML 8.1.21
PAML 4.3
R 3.2.2
getopt 2.23.2
EMBOSS 6.6.0
ape 5.4
phangorn 2.4.0
ete 3.1.2
Each software can be either placed in the PATH or installed with conda. The correct installation and versions of the requirements can be checked using the --requirements option.
For troubleshooting or any explanation on its functioning and usage write to forni.giobbe@gmail.com.
"
################################################################################################################################################################################################################### END OF HELP
fi
exit
################################################################################################################################################################################################################### ALERTS FOR MISSING OF MISSPECIFIED VARIABLES
;;
\?) echo "WARNING! -$OPTARG isn't a valid option"
exit
;;
:) echo "WARNING! missing -$OPTARG value"
exit
;;
esac
done
################################################################################################################################################################################################################### ALERTS FOR MISSING OF MISSPECIFIED VARIABLES
if [ "$g" == 1 ] ;
then
echo -e "List of requirements:"
raxmlHPC-PTHREADS -v > raxml.tmp.chk 2>&1 ; if grep -q "This is RAxML version" raxml.tmp.chk ; then echo -e "RAxML was found!"; else echo -e "RAxML not found"; fi
Rscript > rscript.tmp.chk 2>&1 ; if grep -q "Rscript" rscript.tmp.chk ; then echo -e "Rscript 3.2.2 was found!"; else echo -e "Rscript 3.2.2 not found"; fi
codeml . > codeml.tmp.chk 2>&1 ; if grep -q "Error: file name empty" codeml.tmp.chk ; then echo -e "codeml was found!"; rm rst* rub &>/dev/null; else echo -e "codeml not found"; fi
transeq -h > transeq.tmp.chk 2>&1 ; if grep -q "Translate nucleic acid sequences" transeq.tmp.chk ; then echo -e "transeq was found!"; else echo -e "transeq not found"; fi
rm *.tmp.chk
exit
fi
################################################################################################################################################################################################################### ALERTS FOR MISSING OF MISSPECIFIED VARIABLES
if [ "$x" == 1 ]; then
if [ -z "$input_folder" ] || [ -z "$output_folder" ] || [ -z "$codeml_template_1" ] || [ -z "$codeml_template_2" ] || [ -z "$n_threads" ];
then echo -e " \n WARNING! non-optional argument/s is missing!\n "; exit;
fi;
if [ -z "$s_tree" ] && [ -z "$g_tree" ]
then echo -e " \n WARNING! non-optional argument/s is missing!\n "; exit;
fi;
if [ ! -z "$s_tree" ] && [ ! -z "$g_tree" ]
then echo -e " \n WARNING! use either gene-tree or species-tree mode!\n "; exit;
fi;
if [[ -z "$rep" ]];
then rep=1;
fi;
if [[ -d "$output_folder" ]] || [[ -d $output_folder".tmp.full.out" ]] && [[ -z "$e" ]] ; then echo -e "\n WARNING! an output folder with the same name you specified is allready present \n"; exit; fi
if [[ -d "$output_folder" ]] && [[ ! -z "$e" ]] ; then rm -r $output_folder; fi
if [[ -d $output_folder".tmp.full.out" ]] && [[ ! -z "$e" ]] ; then rm -r $output_folder".tmp.full.out"; fi
folder_name=$(echo $output_folder | awk -F "/" '{print $NF}'); if [[ -z $folder_name ]]; then echo -e "\n WARNING! a folder name needs to be specified \n"; exit; fi
fi;
################################################################################################################################################################################ ANALYZE!
if [ "$x" == 1 ]
then
if [[ -z "$rep" ]] ;then rep=1 ;fi
######################################################################################### inputs specification
if [[ $(ls -l $input_folder/*.fa | wc -l) -eq 0 ]]; then echo -e " \n WARNING! no alignment file(s) (one liner fasta-formatted alignment files, with .fa extension) is present in the input folder. \n "; exit; fi
if [[ ! -z $s_tree ]]; then
if echo $s_tree | grep -v nwk || grep NEXUS $s_tree;
then echo -e " \n WARNING! the species tree needs to be in newick format and to have the .nwk extension. \n ";
exit;
fi;
fi;
for i in $input_folder/*.fa; do a=$(cat $i | head -2); if ( echo $a | grep -v ">") ; then echo -e " \n WARNING! something is wrong with the alnignment file(s) (they have to be one liner fasta-formatted files with the .fa extension). \n "; exit; fi; done
if [[ ! -z $s_tree ]]; then
if grep -q ":[0-9]" $s_tree;
then echo -e " \n WARNING! it seems your species tree contains branch length values, you should remove them before the analysis. \n ";
exit;
fi;
fi;
max_cores=$(( $(ls -l $input_folder/*.fa | wc -l) * $rep ))
if [[ $n_threads -gt $(( $(ls -l $input_folder/*.fa | wc -l) * $rep )) ]]; then echo -e " \n WARNING! too many cores have been specified. They were automatically set to $max_cores."; n_threads=$max_cores; fi
#echo $n_threads
initial_path=$(pwd)
mkdir $output_folder
mkdir $output_folder".tmp.full.out"
cp -r $input_folder/*.fa $output_folder".tmp.full.out"
if [[ ! -z $s_tree ]]; then cp $s_tree $output_folder".tmp.full.out"/species_tree; fi
cd $output_folder".tmp.full.out"
original_path=$(pwd)
for i in *.fa; do awk '/^>/ {printf("\n%s\n",$0);next; } { printf("%s",$0);} END {printf("\n");}' < $i > $i.ol; mv $i.ol $i; done
T_raxml=$((n_threads + 1))
echo -e "\n analysis started on $(date)"
######################################################################################### analysis specification
tot_genes=$( ls *.fa | wc -l | awk '{print $1}')
if [[ ! -z $s_tree ]];
then sed 's/,/\n/g' species_tree | sed 's/(//g' | sed 's/)//g' | sed 's/;//g' >> sp.lst;
else grep ">" *fa | awk -F ">" '{print $NF}' | sort -u >> sp.lst;
fi
for i in *.fa; do grep ">" $i | tr -d ">"; done | sort -u > aln_sp.lst
sp_mismatch_more_aln=$(cat aln_sp.lst | grep -wFvf sp.lst); if [[ -n $sp_mismatch_more_aln ]]; then echo -e " \n WARNING! some species which are included in the alignments are not present in the species tree. \n "; exit; fi
sp_mismatch_more_tre=$(cat sp.lst | grep -wFvf aln_sp.lst); if [[ -n $sp_mismatch_more_tre ]]; then echo -e " \n WARNING! some species which are included in the alignments are not present in the species tree. \n "; exit; fi
if [[ ! -z $outgroups ]]; then sp_mismatch_outgroups=$(cat ../$outgroups | grep -wFvf sp.lst);
if [[ -n $sp_mismatch_outgroups ]]; then echo -e " \n WARNING! some species specified in the outgroups file do not match with those in the tree and OGs. \n"; exit; fi; fi
if [[ ! -z $outgroups ]] && grep -q '^$' ../$outgroups; then echo -e " \n WARNING! remove empty lines in the outgroups file. \n"; exit; fi;
if [[ ! -z $labels ]]; then sp_mismatch_labels=$(cat ../$labels | grep -wFvf sp.lst);
if [[ -n $sp_mismatch_labels ]]; then echo -e " \n WARNING! some species specified in the labels file do not match with those in the tree and OGs. \n"; exit; fi; fi
if [[ ! -z $labels ]] && grep -q '^$' ../$labels; then echo -e " \n WARNING! remove empty lines in the labels file. \n"; exit; fi;
if [[ ! -z $labels2 ]]; then sp_mismatch_labels2=$(cat ../$labels2 | grep -wFvf sp.lst);
if [[ -n $sp_mismatch_labels2 ]]; then echo -e " \n WARNING! some species specified in the second labels file do not match with those in the tree and OGs. \n"; exit; fi; fi
if [[ ! -z $labels2 ]] && grep -q '^$' ../$labels2; then echo -e " \n WARNING! remove empty lines in the second labels file. \n"; exit; fi;
export g_m=$(grep model ../$codeml_template_1 | awk {'print $3'})
export a_m=$(grep model ../$codeml_template_2 | awk {'print $3'})
export ns_g=$(grep NSsites ../$codeml_template_1 | awk {'print $3'})
export ns_a=$(grep NSsites ../$codeml_template_2 | awk {'print $3'})
if [ $a_m == 2 ] && [ -z "$labels" ]; then printf "\n %s \n " " WARNING! branch labels are required to test model 2 vs 0 but not specified! \n"; exit; fi
if [ "$a_m" != 2 ] && [ ! -z "$labels" ]; then printf "\n %s \n " " WARNING! you specified branch labels for a model which do not require them! \n" " "; exit; fi
if [ "$a_m" != 2 ] && [ ! -z "$labels" ] && [ ! -z $g_tree ]; then
n_labelled_species=$(awk '{$NF=""}1' $labels | wc -w | awk '{print $1}');
n_labels=$(wc -l $labels | awk '{print $1}');
if [[ $n_labelled_species -ge $n_labels ]];
then printf "\n %s \n " " WARNING! Clade(s) may not be present in gene trees! " " "; exit;
fi
fi
if [ ! -z $s_tree ]; then echo -e "\n analyizing $rep replicate(s) of $tot_genes genes using the species-tree: branch models $g_m VS $a_m & site models $ns_g VS $ns_a \n"; fi
if [ ! -z $g_tree ]; then echo -e "\n analyizing $rep replicate(s) of $tot_genes genes using gene-trees: branch models $g_m VS $a_m & site models $ns_g VS $ns_a \n"; fi
######################################################################################### analysis specification
ls *.fa > tmp.lst
aln_tot=$(wc -l tmp.lst | awk '{print $1}')
gen_code=$(grep icode ../$codeml_template_1 | awk '{print $NF}' | tr -d " ")
if [[ $gen_code == 0 ]] ; then gencode=0;
elif [[ $gen_code == 1 ]] ; then gencode=2;
elif [[ $gen_code == 3 ]] ; then gencode=3;
elif [[ $gen_code == 4 ]] ; then gencode=5;
elif [[ $gen_code == 5 ]] ; then gencode=6;
elif [[ $gen_code == 6 ]] ; then gencode=9;
elif [[ $gen_code == 7 ]] ; then gencode=10;
elif [[ $gen_code == 8 ]] ; then gencode=12;
elif [[ $gen_code == 9 ]] ; then gencode=13;
elif [[ $gen_code == 10 ]] ; then gencode=15;
fi;
prog=0
for j in $(cat tmp.lst);
do
if ( pwd | grep -q $original_path ); then : ; else printf "\n %s \n " " WARNING! an error occured while creating the folder system. \n"; exit; fi
######################################################################################### folder creation
codeml_threads=$(jobs | wc -l); raxml_threads=$(( $n_threads - $codeml_threads ));
let prog=prog+1;
perc=$(( ((prog) * 100)/ aln_tot ))
printf "\r analyze:\t"$perc"%%"
k=$(head -$prog tmp.lst| tail -1)
export f=$(echo $j | sed "s/.fa//");
mkdir $f;
######################################################################################### branch length optimiztion
cd $f
if ( pwd | grep -q $original_path ); then : ; else printf "\n %s \n " " WARNING! an error occured while creating the folder system. \n"; exit; fi
transeq -sequence ../$j -outseq $j".aa" -table $gencode &> /dev/null ;
if grep -q "\*" $j".aa" ; then echo -e " the gene $j contains stop codon and has been excluded by the analyses - check if the gen code is correct" >> ../warnings_summary.txt; cd ..; mv $f $f"_stop_codons"; echo -e "$(echo $j | sed 's/.fa//')" >> failed_clusters.txt; continue; fi;
check=$(for i in $(grep ">" ../$j); do grep -A 1 "$i" ../$j | tail -1 | wc -c; done);
check=$(echo $check | sort -u | wc -l);
if [[ $check != 1 ]];
then echo -e " the gene $j is not aligned and has been excluded by the analyses" >> ../warnings_summary.txt; cd ..; mv $f $f"_misaligned"; echo -e "$(echo $j |sed 's/.fa//')" >> failed_clusters.txt; continue;
fi;
export n=$(tail -1 ../$j | wc -c);
echo "DNA, st=1-$n\3" >> $f.prt;
echo "DNA, nd=2-$n\3" >> $f.prt;
echo "DNA, rd=3-$n\3" >> $f.prt;
################################################################################################################################################################################################## md
for i in $(cat ../sp.lst);
do if grep -q $i ../$j;
then :;
else echo -n $i' ' >> $j"missing_otus.lst";
fi;
done;
if [[ -e $j"missing_otus.lst" ]] && [[ "$missing_data" = 1 ]];
then cd .. ; echo -e " the OG $j is non-ubiquitous and has been excluded by the analyses." >> warnings_summary.txt; continue;
# echo exclude
elif [[ ! -e $j"missing_otus.lst" ]] && [[ ! -z "$s_tree" ]]; then
raxmlHPC-PTHREADS -T $raxml_threads -m GTRGAMMA -s ../$j -f e -g ../species_tree -q $f.prt -n $f".tre" -p 420 &> $j'_RAxML.log'
raxmlHPC-PTHREADS -T $raxml_threads -m GTRGAMMA -s ../$j -q $f.prt -n $f"gene.tre" -p 420 &> $j'_RAxML_gene.log'
ete compare -t RAxML_result."$f"gene.tre -r ../species_tree --unrooted | awk -F "|" '{print $4}' | tail -1 > dst
# echo ubiquitous speciestree
elif [[ ! -e $j"missing_otus.lst" ]] && [[ ! -z "$g_tree" ]]; then
raxmlHPC-PTHREADS -T $raxml_threads -m GTRGAMMA -s ../$j -q $f.prt -n $f".tre" -p 420 &> $j'_RAxML.log'
# echo ubiquitous genetree
elif [[ -e $j"missing_otus.lst" ]] && [[ -z "$missing_data" ]] && [[ ! -z "$s_tree" ]]; then
printf 'library(ape) \n' >> prune.R
printf 'tre<-read.tree("../species_tree") \n' >> prune.R
printf 'tip<-scan(dir(pattern="_otus.lst$")[1], character()) \n' >> prune.R
printf 'tre <- drop.tip(tre, tip) \n' >> prune.R
printf 'write.tree(tre, file = "species_tree_cor") \n' >> prune.R
Rscript prune.R &> /dev/null;
raxmlHPC-PTHREADS -T $raxml_threads -m GTRGAMMA -s ../$j -f e -g species_tree_cor -q $f.prt -n $f".tre" -p 420 &> $j'_RAxML.log'
raxmlHPC-PTHREADS -T $raxml_threads -m GTRGAMMA -s ../$j -q $f.prt -n $f"gene.tre" -p 420 &> $j'_RAxML_gene.log'
ete compare -t RAxML_result."$f"gene.tre -r ../species_tree --unrooted | awk -F "|" '{print $4}' | tail -1 > dst
# echo non-ubiquitous speciestree
elif [[ -e $j"missing_otus.lst" ]] && [[ -z "$missing_data" ]] && [[ ! -z "$g_tree" ]]; then
# echo non-ubiquitous genetree
raxmlHPC-PTHREADS -T $raxml_threads -m GTRGAMMA -s ../$j -q $f.prt -n $f".tre" -p 420 &> $j'_RAxML.log'
else echo "problema genetree: $g_tree ; speciestree: $s_tree; ubiquitous: $missing_data"
fi
# echo -e "optimizing \t $j \t branchlength \t replicate \t $rep"
rm *reduced* &> /dev/null;
rm ../*reduced* &> /dev/null;
if grep -q "TOO FEW SPECIES" $j"_RAxML.log";
then cd .. ;
mv $f $f"_tree_failure";
echo -e "$(echo $j | sed 's/.fa//')" >> failed_clusters.txt;
echo -e " the gene $j contains less tha 3 OTUS so that branch lengths could not be calculated" >> warnings_summary.txt; continue;
fi
if grep -q "taxa that had equal names in the alignment" $j"_RAxML.log";
then cd .. ;
mv $f $f"_tree_failure";
echo -e "$(echo $j | sed 's/.fa//')" >> failed_clusters.txt;
echo -e " the gene $j contains OTUs with equl name" >> warnings_summary.txt; continue;
fi
#################################################################################################################################################################################################### add TAGS
if [ ! -z "$labels" ] && [ "$a_m" == 2 ]; then
cp RAxML_result."$f".tre r_input.tre
printf 'require(phangorn) \n' >> label.R
printf 'tr<-read.tree("./r_input.tre") \n' >> label.R
printf 'tips=(readLines(paste("./a_species.txt", sep = " "))) \n' >> label.R
printf 'labels<-scan("./a_tag.txt", character(), quote = " ") \n' >> label.R
printf 'label_nodes <- function(tr, tips, labels, filename = "tree.tre"){ \n' >> label.R
printf ' tr$node.label <- rep("", max(tr$edge)-Ntip(tr)) \n' >> label.R
printf ' for(i in 1:length(labels)){ \n' >> label.R
printf ' x <- sapply(strsplit(tips[i], " "), length) \n' >> label.R
printf ' if(x > 1){ \n' >> label.R
printf ' tr$node.label[mrca.phylo(tr, node = strsplit(tips[i], " ")[[1]], full = FALSE) - Ntip(tr)] <- labels[i] \n' >> label.R
printf ' } else { \n' >> label.R
printf ' tr$tip.label[tr$tip.label==tips[i]] <- paste(tips[i],labels[i]) \n' >> label.R
printf '} \n' >> label.R
printf '} \n' >> label.R
printf ' write.tree(tr, file = filename) \n' >> label.R
printf ' return(tr) \n' >> label.R
printf '} \n' >> label.R
printf 'label_nodes(tr, tips, labels, filename = "tree.tre") \n' >> label.R
for W in {1..9}; do
for lab in $labels $labels2; do
awk '{print $NF}' ../../$lab > a_tag.txt
awk '{$NF=""; print $0}' ../../$lab > a_species.txt
sed -i 's/ *$//' a_tag.txt
sed -i 's/ *$//' a_species.txt
if [[ -z "$missing_data" ]] && [[ -a $j"missing_otus.lst" ]];
then for i in $(cat $j"missing_otus.lst"); do
sed -i "s/$i//g" a_species.txt;
sed -i 's/ */ /g' a_species.txt;
sed -i 's/^ //g' a_species.txt;
sed -i 's/ $//g' a_species.txt;
done;
fi;
Rscript label.R &> /dev/null;
mv tree.tre "RAxML_result."$f"."$lab".tre"
done
if [ -f RAxML_result."$f"."$labels".tre ]; then awk -F ")" '{print $NF}' "RAxML_result."$f"."$labels".tre" > tree_end_1; fi
if [ ! -z $labels2 ] && [ -f RAxML_result."$f"."$labels2".tre ]; then awk -F ")" '{print $NF}' "RAxML_result."$f"."$labels2".tre" > tree_end_2; fi
#echo RAxML_result."$f"."$labels".tre RAxML_result."$f"."$labels2".tre
if grep -q "\\$" tree_end_1 || grep -q "#" tree_end_1 || grep -q "\\$" tree_end_2 &> /dev/null || grep -q "#" tree_end_2 &> /dev/null;
then
if [ ! -z "$outgroups" ] && [ -f $j"missing_otus.lst" ]; then
root=$(shuf -n 1 ../../$outgroups); if grep $root $j"missing_otus.lst" &> /dev/null; then continue; fi;
elif [ ! -z "$outgroups" ] && [ ! -f $j"missing_otus.lst" ]; then
root=$(shuf -n 1 ../../$outgroups)
elif [ -z "$outgroups" ] && [ -f $j"missing_otus.lst" ]; then
root=$(shuf -n 1 ../sp.lst); if grep $root $j"missing_otus.lst" &> /dev/null; then continue; fi;
elif [ -z "$outgroups" ] && [ ! -f $j"missing_otus.lst" ]; then
root=$(shuf -n 1 ../sp.lst);
fi;
printf 'library(ape) \n' >> root.R
printf 'tre<-read.tree("r_input.tre") \n' >> root.R
printf 'tip<-scan("root.tmp", character(), quote = " ") \n' >> root.R
printf 'tre <- root(tre, tip) \n' >> root.R
printf 'write.tree(tre, file = "r_input.tre") \n' >> root.R
echo $root > root.tmp
Rscript root.R &> /dev/null;
# echo -e "\n \n radice $root $j \n \n"
# cat r_input.tre
else break;
fi;
done
if [ -f RAxML_result."$f"."$labels".tre ]; then awk -F ")" '{print $NF}' "RAxML_result."$f"."$labels".tre" > tree_end_1; fi
if [ ! -z $labels2 ] && [ -f RAxML_result."$f"."$labels2".tre ]; then awk -F ")" '{print $NF}' "RAxML_result."$f"."$labels2".tre" > tree_end_2; fi
#echo $f $labels $labels2 "RAxML_result."$f"."$labels".tre" "RAxML_result."$f"."$labels2".tre"
if [ ! -z $labels2 ] && [ -f RAxML_result."$f"."$labels2".tre ]; then
if grep -q "\\$" tree_end_1 || grep -q "#" tree_end_1 || grep -q "\\$" tree_end_2 || grep -q "#" tree_end_2;
then echo -e " impossible to use tag $bad_tag in gene $j because of topology. Try to lanuch it again the analysis for the OGs which failed specifing outgroups with -k. \n" >> ../warnings_summary.txt;
fi;
fi;
if [ ! -f RAxML_result."$f"."$labels2".tre ]; then
if grep -q "\\$" tree_end_1 || grep -q "#" tree_end_1;
then echo -e " impossible to use tag $bad_tag in gene $j because of topology. Try to lanuch it again the analysis for the OGs which failed specifing outgroups with -k. \n" >> ../warnings_summary.txt
fi;
fi;
else : ;
fi ;
if [[ ! -z $labels ]]; then sed -i 's/_\#/\#/g' "RAxML_result."$f"."$labels".tre"; fi
if [[ ! -z $labels2 ]]; then sed -i 's/_\#/\#/g' "RAxML_result."$f"."$labels2".tre"; fi
if [[ ! -z $labels ]]; then sed -i 's/_\$/\$/g' "RAxML_result."$f"."$labels".tre"; fi
if [[ ! -z $labels2 ]]; then sed -i 's/_\$/\$/g' "RAxML_result."$f"."$labels2".tre"; fi
##############################################################################################################################################
for rep in $(seq 1 $rep); do
mkdir $f"_replicate_"$rep;
mkdir $f"_replicate_"$rep/$f"_model_alternative";
mkdir $f"_replicate_"$rep/$f"_model_general";
cd $f"_replicate_"$rep;
if ( pwd | grep -q $original_path ); then : ; else printf "\n %s \n " " WARNING! an error occured while creating the folder system. \n"; exit; fi
######################################################################################### tree building original loop & folder creation
# model general
cd $f"_model_general";
if ( pwd | grep -q $original_path ); then : ; else printf "\n %s \n " " WARNING! an error occured while creating the folder system. \n"; exit; fi
# if [ ! -z "$labels2" ]; then cp ../../"RAxML_result."$f"."$labels".tre" .; else cp ../../"RAxML_result."$f".tre" .; fi
if [ ! -z "$labels2" ];
then
cp ../../"RAxML_result."$f"."$labels".tre" .
mv "RAxML_result."$f"."$labels".tre" "RAxML_result."$f".general.tre"
else
cp ../../"RAxML_result."$f".tre" .
mv "RAxML_result."$f".tre" "RAxML_result."$f".general.tre"
fi
cp ../../../$j .
omega=$(printf '%s\n' $(echo "scale=2; $RANDOM/10000" | bc )); if echo $omega | grep -qe "^\."; then omega=$(echo 0$omega); fi;
sed "s/seqfile =/seqfile = $j/" ../../../../$codeml_template_1 | sed "s/treefile =/treefile = RAxML_result."$f".general.tre/" | sed "s/outfile =/outfile = "$f"_replicate_"$rep"_model_general.out/" | sed "s/omega =/omega = "$omega"/" > $f"_model_general.ctl" ;
codeml $f'_model_general.ctl' &> $j'model_general_codeml.log' &
while [ $(jobs | wc -l) -ge $n_threads ]; do sleep 1; done
# echo -e "performing \t $j \t model \t $general \t replicate \t $rep"
cd .. ;
# model alternative
cd $f"_model_alternative";
if ( pwd | grep -q $original_path ); then : ; else printf "\n %s \n " " WARNING! an error occured while creating the folder system. \n"; exit; fi
if [ ! -z "$labels" ] && [ ! -z "$labels2" ];
then
cp ../../"RAxML_result."$f".$labels2.tre" .
mv "RAxML_result."$f".$labels2.tre" "RAxML_result."$f".alternative.tre"
elif [ ! -z "$labels" ] && [ -z "$labels2" ];
then cp ../../"RAxML_result."$f".$labels.tre" .
mv "RAxML_result."$f".$labels.tre" "RAxML_result."$f".alternative.tre"
else
cp ../../"RAxML_result."$f".tre" .
mv "RAxML_result."$f".tre" "RAxML_result."$f".alternative.tre"
fi
cp ../../../$j .
sed "s/seqfile =/seqfile = $j/" ../../../../$codeml_template_2 | sed "s/treefile =/treefile = RAxML_result."$f".alternative.tre/" | sed "s/outfile =/outfile = "$f"_replicate_"$rep"_model_alternative.out/" | sed "s/omega =/omega = "$omega"/" > $f"_model_alternative.ctl" ;
codeml $f'_model_alternative.ctl' &> $j'_model_alternative_codeml.log' &
while [ $(jobs | wc -l) -ge $n_threads ]; do sleep 1; done
# echo -e "performing \t $j \t model \t $alternative \t replicate \t $rep"
cd ../..
done;
cd ..
done;
echo -e " "
######################################################################################### TAGS
wait
######################################################################################### likelihood ratio test
if [[ $(wc -l failed_clusters.txt | awk '{print $1}') == $tot_genes ]]; then echo -e "\n all clusters failed, check the warnings_summary.txt file in the .tmp.full.out folder. \n" ; exit; fi 2>/dev/null
echo -e "\n performing LRT \n"
for i in $(ls *.fa); do
export name=$(echo $i | sed -s 's/.fa//g');
unset lk_a_best rep_a_best lk_b_best rep_b_best
for r in $(seq 1 $rep); do
export occurencies=$(grep "Time used" $name/$name"_replicate_"$r/"$name"_model_*/*out 2>/dev/null | wc -l);
if [[ $occurencies == 2 ]];
then
export np_a=$(grep "lnL" $name/$name"_replicate_"$r/"$name"_model_general/*out | awk -F ":" '{print $3}' | sed 's/)/ /g' | tr -d " ");
export np_b=$(grep "lnL" $name/$name"_replicate_"$r/"$name"_model_alternative/*out | awk -F ":" '{print $3}' | sed 's/)/ /g' | tr -d " ");
export lk_a=$(grep "lnL" $name/$name"_replicate_"$r/"$name"_model_general/*out | awk '{print $5}'); echo $lk_a $r >> $name"_general.tmp"
export lk_b=$(grep "lnL" $name/$name"_replicate_"$r/"$name"_model_alternative/*out | awk '{print $5}'); echo $lk_b $r >> $name"_alternative.tmp"
elif [[ $occurencies -lt 2 ]] && [[ -e $name/"RAxML_result."$name".tre" ]];
then : ;
echo -e " the codeml analyisis relative to the replicate $r of the gene $name crashed" >> warnings_summary.txt;
mv $name/$name"_replicate_"$r $name/$name"_replicate_"$r"_codeml_failure"
fi;
done
lk_a_best=$(sort -g $name"_general.tmp" 2> /dev/null | head -1 | awk '{print $1}')
rep_a_best=$(sort -g $name"_general.tmp" 2> /dev/null | head -1 | awk '{print $2}')
lk_b_best=$(sort -g $name"_alternative.tmp" 2> /dev/null | head -1 | awk '{print $1}')
rep_b_best=$(sort -g $name"_alternative.tmp" 2> /dev/null | head -1 | awk '{print $2}')
# if [[ ! -z "$s_tree" ]]; then dst=$(cat $name/dst); fi
if [[ ! -z $lk_a_best ]] && [[ ! -z $lk_b_best ]];
then
if [[ ! -z "$s_tree" ]]; then
dst=$(cat $name/dst);
echo -e "$name\t$g_m\t$ns_g\t$np_a\t$lk_a_best\t$rep_a_best\t$a_m\t$ns_a\t$np_b\t$lk_b_best\t$rep_b_best\t$dst" >> likelihood_summary.txt;
else
echo -e "$name\t$g_m\t$ns_g\t$np_a\t$lk_a_best\t$rep_a_best\t$a_m\t$ns_a\t$np_b\t$lk_b_best\t$rep_b_best" >> likelihood_summary.txt;
fi
elif [[ -z $lk_a_best ]] || [[ -z $lk_b_best ]] && [[ -e $name/"RAxML_result."$name".tre" ]];
then
echo -e " all replicates relative to gene $name crashed" >> warnings_summary.txt;
echo -e "$name" >> failed_clusters.txt
elif [[ -z $lk_a_best ]] && [[ -z $lk_b_best ]] && [[ -e $name/$name"_replicate_1"/"RAxML_result."$name".tre" ]];
then : ;
fi;
done
if [[ ! -z "$s_tree" ]];
then
printf 'likelihood.summary <- read.table(file="./likelihood_summary.txt",sep="\t",header=FALSE,col.names=c("gene","branch_model_g","site_model_g","model_g_np","model_g_LnL","rep_g","branch_model_a","site_model_a","model_a_np","model_a_LnL","rep_a","nRF"),as.is=c(1:2,5)) \n' > LRT.R
else
printf 'likelihood.summary <- read.table(file="./likelihood_summary.txt",sep="\t",header=FALSE,col.names=c("gene","branch_model_g","site_model_g","model_g_np","model_g_LnL","rep_g","branch_model_a","site_model_a","model_a_np","model_a_LnL","rep_a"),as.is=c(1:2,5)) \n' > LRT.R
fi
printf 'LRT <- -2*(likelihood.summary$model_g_LnL-likelihood.summary$model_a_LnL) \n' >> LRT.R
printf 'degrees.of.freedom <- likelihood.summary$model_a_np-likelihood.summary$model_g_np \n' >> LRT.R
printf 'p.value <- 1-pchisq(LRT,df=degrees.of.freedom) \n' >> LRT.R
printf 'adj.p.value <- p.adjust(p.value, method = "hochberg", n = length(p.value)) \n' >> LRT.R
printf 'significance <- character() \n' >> LRT.R
printf 'selected.cluster <- character() \n' >> LRT.R
printf 'for (c in 1:dim(likelihood.summary)[1]) { \n' >> LRT.R
printf 'selected <- TRUE \n' >> LRT.R
printf 'if (adj.p.value[c] < 0.001) { \n' >> LRT.R
printf 'significance <- c(significance,"***") \n' >> LRT.R
printf '} \n' >> LRT.R
printf 'else if (adj.p.value[c] < 0.01) { \n' >> LRT.R
printf 'significance <- c(significance,"**") \n' >> LRT.R
printf '} \n' >> LRT.R
printf 'else if (adj.p.value[c] < 0.05) { \n' >> LRT.R
printf 'significance <- c(significance, "*") \n' >> LRT.R
printf '} else { \n' >> LRT.R
printf 'significance <- c(significance,"n/s") \n' >> LRT.R
printf 'selected <- FALSE \n' >> LRT.R
printf '} \n' >> LRT.R
printf 'if (selected == TRUE) selected.cluster <- c(selected.cluster,likelihood.summary$gene[c]) \n' >> LRT.R
printf '} \n' >> LRT.R
printf 'likelihood.summary$LRT <- round(LRT,digits=4) \n' >> LRT.R
printf 'likelihood.summary$df <- degrees.of.freedom \n' >> LRT.R
printf 'likelihood.summary$adj.p.value <- round(adj.p.value,digits=4) \n' >> LRT.R
printf 'likelihood.summary$significance <- significance \n' >> LRT.R
printf 'write.table(likelihood.summary,file="./likelihood_summary.txt",quote=FALSE,sep="\\t",eol="\\n",row.names=FALSE) \n' >> LRT.R
printf 'write.table(data.frame(selected.cluster=selected.cluster),file="./selected_clusters.txt",quote=FALSE,sep="\\t",eol="\\n",row.names=FALSE,col.names=FALSE) \n' >> LRT.R
Rscript LRT.R &> /dev/null;
rm LRT.R
########################################################################################
echo -e " formatting output \n"
#output_folder_rel=$(echo $output_folder | awk -F "/" '{print $NF}')
#echo $original_path
#echo "$original_path/$output_folder"
if [[ -f selected_clusters.txt ]]; then
for f in $(cat selected_clusters.txt); do
rep_b_best=$(sort -g $f"_alternative.tmp" 2> /dev/null | head -1 | awk '{print $2}')
cp $f/$f'_replicate_'$rep_b_best/$f'_model_alternative'/$f'_replicate_'$rep_b_best'_model_alternative.out' $initial_path/$output_folder &> /dev/null;
done
fi
######################################################################################## moves codeml .out which did pass the LRT
for i in $(cat tmp.lst | sed 's/.fa//'); do if grep $i selected_clusters.txt &> /dev/null; then : ; else echo $i >> unselected_clusters.txt; fi; done
if [[ -f unselected_clusters.txt ]]; then
for f in $(cat unselected_clusters.txt); do
rep_a_best=$(sort -g $f"_general.tmp" 2> /dev/null | head -1 | awk '{print $2}')
cp $f/$f'_replicate_'$rep_a_best/$f'_model_general'/$f'_replicate_'$rep_a_best'_model_general.out' $initial_path/$output_folder &> /dev/null;
done
fi
######################################################################################## moves codeml .out which did not pass the LRT
rm tmp.lst
sort -u -o warnings_summary.txt warnings_summary.txt &> /dev/null
cp likelihood_summary.txt $initial_path/$output_folder 2> /dev/null
cp warnings_summary.txt $initial_path/$output_folder 2> /dev/null
if [[ -a warnings_summary.txt ]]; then echo -e " the analysis has produced some warning(s): you will find the information relative to each failure in the file warnings_summary.txt\n"; fi
cd $initial_path/$output_folder
########################################################################################
cd ..
if [ "$verbose" != 1 ]
then rm -r $initial_path/$output_folder".tmp.full.out"
fi
################################################################################################################################################################################ ANNOTATE (PART 1)
elif [ "$j" == 1 ]
then
if ls $input_folder/*out > /dev/null ; then : ; else echo -e " \n WARNING! no codeml output(s) is present in the input folder - they need to have the .out extension \n "; exit; fi &> /dev/null
echo -e "\n analysis started on $(date) \n"
cp $labels $input_folder &> /dev/null
cd $input_folder
if [[ $(ls -l *out 2> /dev/null | wc -l) != $(ls -l *annotation 2> /dev/null | wc -l) ]]; then
# cp $tags_file $input_folder 2>/dev/null
ltot=$(ls -l *.out | wc -l)
prog=1
echo -e " annotating $ltot codeml outputs \n"
for i in *.out;
do
perc=$(( ((prog) * 100)/ ltot ))
printf "\r annotate:\t"$perc"%%"
let prog=prog+1
################################################################################################################################################################################ ANNOTATE (PART 2)
# echo "analyzing $i codeml output"
lines_number=$(awk '/dN & dS for each branch/,/tree length/' $i | wc -l);
awk '/dN & dS for each branch/,/tree length/' $i | head -$((lines_number -2)) | tail -$((lines_number -6)) | awk -F " " '{print $1}' | sed 's/\.\./_/g' | awk -F "_" '{print $2"_"$1}' | sort -n > branches.tmp
awk '/dN & dS for each branch/,/tree length/' $i | head -$((lines_number -2)) | tail -$((lines_number -6)) | awk -F " " '{print $1}' | sed 's/\.\./_/g' | awk -F "_" '{print $1"_"$2}' | sort -n > chesbran.tmp
awk -F "_" '{print $1}' branches.tmp > init.tmp
awk -F "_" '{print $2}' branches.tmp > hits.tmp
cat $i | grep -A 2 "tree length = " | tail -1 > codeml_tre.tmp
n=$(cat init.tmp | wc -l)
################################################################################################################################################################################ ANNOTATE (PART 3)
for k in {1..5};
do
for j in $(eval echo {1..$n});
do
init=$(sed -n $j"p" init.tmp)
hits=$(sed -n $j"p" hits.tmp)
# subs=$(cat codeml_tre.tmp | sed s"/[^0-9]$init:/here (\n/" | grep -zPo 'here (\(([^()]++|(?1))*\))' | tail -1);
subs=$(cat codeml_tre.tmp | tr -d '\0' | sed s"/[^0-9]$init:/$init (/" | tr -d '\0' | grep -zPo "$init (\(([^()]++|(?1))*\))" | tr -d '\0' | sed "s/$init (/$init\:/" | tr -d '\0') &> /dev/null;
# echo $init $hits $subs
if grep -w -q "$init" codeml_tre.tmp &> /dev/null;
then if grep -w -v "$hits" codeml_tre.tmp &> /dev/null;
then if [[ ! -z $subs ]];
then sed -i "s/[^0-9.]$subs/&$hits/" codeml_tre.tmp;
fi;