#! /usr/bin/awk -f # busco2multifasta.awk Bernardo for Steganinae phylogeny project 16feb2018 v. 10mar2018 # uses Busco list of single orthologs from two or more species to produece a multifasta ready for alignment #usage busco2multifasta.awk species="EhiansMLmf1 Git3 vir" diptera_odb9.gene_name # busco2multifasta.awk species="EhiansMLmf1 Git3 vir" file_extension=faa diptera_odb9.gene_name function usage(dummy){ print "usage:" print "busco2multifasta.awk species=\42Ehians Git3 nav\42 diptera_odb9.gene_name" print "busco2multifasta.awk species=\42Ehians Git3 nav\42 file_extension=faa diptera_odb9.gene_name" print "busco2multifasta.awk species=\42Ehians Git3 nav\42 out_dir="/home/tools/busco/dataset_10mar2018/busco_output/multifasta/" diptera_odb9.gene_name" print "Ehians Git3 nav are the names os the folders that contain the fasta of single-copy genes annotated by BUSCO, originally stored in the folder xxxx" print "this awk program produce multifasta files, one for each ortholog, containing the orthologs of the named species (Ehians Git3 nav) " print "default values: file_extension=fna " Print "prints also for each ortholog the number of sequences, the coefficient of variation of the sizes, and the individual sizes" flag_exit=1; exit } BEGIN{ if (ARGC==1){usage(dummy)} } (NR==1){ n_species=split(species,species_array) for (i=1; i<=n_species; i++){output_prefix=output_prefix species_array[i] "_"} # Ehians_Git3_nav_ if (file_extension == ""){file_extension="fna"} if (outdir !=""){out_dir=outdir} if (out_dir != ""){out_dir = out_dir "/"} #ensures that a "/" is present in the end log_output_file=out_dir output_prefix".ortholog_counts" } (NR>=1){ current_ortholog=$1 multifasta_ouput_file = out_dir output_prefix current_ortholog "." file_extension # Ehians_Git3_nav_EOG09150006.fna or Ehians_Git3_nav_EOG09150006.faa total_ortholog_count ++ shared_ortholog_count=0 ortholog_count_array[current_ortholog]=0 flag_first_saved_sequence=0 for (i=1; i<=n_species; i++){ current_species=species_array[i] ortholog_sizes_array[current_ortholog,current_species] = 0 current_file=current_species "/" current_ortholog "." file_extension #sed_command="sed \47/>/{s/>/>" current_species "_" current_ortholog " /}\47 " current_file # sed '/>/{s/>/>Ehians_EOG09150006 /}' Ehians/EOG09150006.fna sed_command="sed \47/>/{s/>.*/>" current_species "_" current_ortholog " /}\47 " current_file # sed '/>/{s/>.*/>Ehians_EOG09150006 /}' Ehians/EOG09150006.fna if (system("test -f " current_file)){ # 0 if the expression is true, 1 if the expression is false #print "file does not exist: " current_file } else{ if (flag_first_saved_sequence==0){system(sed_command " > " multifasta_ouput_file )} #avoids accumulating sequences when running multiple times the program. else {system(sed_command " >> " multifasta_ouput_file )} ortholog_count_array[current_ortholog] ++ species_array2[current_species] ++ command_count_char = "tr -d '\n\r' < " current_file " | wc -m" command_count_char | getline sequence_size ortholog_sizes_array[current_ortholog,current_species] = sequence_size close(command_count_char) flag_first_saved_sequence ++ } } } END{ if (flag_exit){exit} # for (ortho in ortholog_count_array){ # print ortho , ortholog_count_array[ortho] > output_prefix".ortholog_counts" #useful to select the genes thta have orthologs in more or all species # } print "species","found_genes" for (i=1; i <= n_species; i++){ current_species=species_array[i] print current_species , species_array2[current_species] } printf("%s\t%s\t%s\t" , "ortholog", "count" , "CV") > log_output_file for (i=1; i <= n_species; i++){printf("%s\t",species_array[i]) > log_output_file } print "" > log_output_file for (ortho in ortholog_count_array){ # printf("%s\t%i\t", ortho, ortholog_count_array[ortho] ) > log_output_file avg=0; CV=0; sum=0; sum_squares=0; var=0; n=0 for(i=1; i<=n_species; i++){ current_species=species_array[i] size=ortholog_sizes_array[ortho,current_species] sum = sum + size sum_squares = sum_squares + size^2 if (size > 0) {n ++} #excludes the missing data from the calculation of the CV (Coef_Variation) } printf("%s\t%i\t", ortho, n ) > log_output_file if (n>1) {avg=sum/n ; var=sum_squares/n - avg^2; CV=100*sqrt(var)/avg printf("%3.1f\t", CV ) > log_output_file } else {printf("%s\t", "." ) > log_output_file} for(i=1; i<=n_species; i++){ current_species=species_array[i] printf("%i\t", ortholog_sizes_array[ortho,current_species] ) > log_output_file } print "" > log_output_file } } # EOG09150006 # EOG09150009 # EOG0915000C # EOG0915000S # EOG0915000X # awk 'BEGIN {print getline < "file" < 0 ? "not exists" : "exists"}' # if (system("test -f " FILE)) { ... } # awk 'BEGIN{print (system("test -f " FILE))}' # awk 'BEGIN{print (system("test -f coloc.data"))}' # awk 'BEGIN { # cmd = "date -j -f %s" # cmd | getline mydate # close(cmd) # }'