
#!/bin/bash

#Per exportar tos els programes necesaris (exonerate, fastaseqfromGFF.pl i t_coffee)
module load modulepath/goolf-1.7.20
module load BLAST+/2.2.30-goolf-1.7.20
module load Exonerate/2.2.0-goolf-1.7.20
module load T-Coffee/11.00.8cbe486-goolf-1.7.20
export PATH=/cursos/20428/BI/bin:$PATH
export PATH=/cursos/20428/BI/soft/genewise/x86_64/bin:$PATH
export WISECONFIGDIR=/cursos/20428/BI/soft/genewise/x86_64/wise2.2.0/wisecfg/

#Per definir els PATHS dels outputs i inputs
PATH_parus=/home/u104639/Escriptori/BI/Output  #Directori que contindrà tots els directoris i fitxers de soritda.  
PATH_files=/cursos/20428/BI/genomes/2016/Parus_major #Parus majus genome i genome.index  
PATH_proteins=/home/u104639/Escriptori/BI/Gallus #Gallus Selenoproteies obtingudes de SelenoDB
PATH_hits=/home/u104639/Escriptori/BI/Hits #Hits obtinguts amb el programa Blast i analitzats manualment


#Per canviar el directori al PATH dels hits i per definir la varible hits. 
cd $PATH_hits
hits="DIO1hit1 DIO1hit2 GPx3hit1 GPx3hit2 GPx3hit3 GPx3hit4 GPx3hit5 SELENOIhit1 SELENOIhit2 SELENOIhit3 SELENOIhit4 SELENOO1hit1 SELENOO1hit2 SELENOP2hit1 SELENOP2hit2 SELENOU1hit1 TXNRD3hit1 TXNRD3hit2 TXNRD3hit3 DIO2hit1 DIO2hit2 DIO2hit3 MSRB1hit1 SELENOKhit1 SELENOO3hit1 SELENOO3hit2 TXNRD1hit1 TXNRD1hit2 TXNRD1hit3 TXNRD1hit4 DIO3hit1 DIO3hit2 DIO3hit3 Sel15hit1 SELENONhit1 SELENOP1hit1 SELENOP1hit2 SELENOThit1 TXNRD2hit1 TXNRD2hit2 TXNRD2hit3 TXNRD2hit4 eEFsechit1 eEFsechit2 MsrAhit1 MsrAhit2 SBP2_1hit1 SBP2_1hit2 SECp43hit1 SECp43hit2 SECp43hit3 SECp43hit4 SELENOO_2hit1 SELENOO_2hit2 GPx7hit1 GPx7hit2 GPx7hit3 GPx7hit4 MSRB3hit1 MSRB3hit2 SBP2_2hit1 SBP2_2hit2 SecS_1hit1 SELENOO_4hit1 SELENOO_4hit2 GPx8hit1 GPx8hit2 GPx8hit3 GPx8hit4 GPx8hit5 PSTKhit1 SBP2_3hit1 SecS_2hit1 SEPHShit1 SEPHShit2"




#Per declarar un diccionari amb cada hit de cada proteïna
declare -A hits_proteins
hits_proteins=(["DIO1hit1"]="DIO1" ["DIO1hit2"]="DIO1" ["GPx3hit1"]="GPx3" ["GPx3hit2"]="GPx3" ["GPx3hit3"]="GPx3" ["GPx3hit4"]="GPx3" ["GPx3hit5"]="GPx3" ["SELENOIhit1"]="SELENOI" ["SELENOIhit2"]="SELENOI" ["SELENOIhit3"]="SELENOI" ["SELENOIhit4"]="SELENOI" ["SELENOO1hit1"]="SELENOO1" ["SELENOO1hit2"]="SELENOO1" ["SELENOP2hit1"]="SELENOP2" ["SELENOP2hit2"]="SELENOP2" ["SELENOU1hit1"]="SELENOU1" ["TXNRD3hit1"]="TXNRD3" ["TXNRD3hit2"]="TXNRD3" ["TXNRD3hit3"]="TXNRD3" ["DIO2hit1"]="DIO2" ["DIO2hit2"]="DIO2" ["DIO2hit3"]="DIO2" ["MSRB1hit1"]="MSRB1" ["SELENOKhit1"]="SELENOK" ["SELENOO3hit1"]="SELENOO3" ["SELENOO3hit2"]="SELENOO3" ["TXNRD1hit1"]="TXNRD1" ["TXNRD1hit2"]="TXNRD1" ["TXNRD1hit3"]="TXNRD1" ["TXNRD1hit4"]="TXNRD1" ["DIO3hit1"]="DIO3" ["DIO3hit2"]="DIO3" ["DIO3hit3"]="DIO3" ["Sel15hit1"]="Sel15" ["SELENONhit1"]="SELENON" ["SELENOP1hit1"]="SELENOP1" ["SELENOP1hit2"]="SELENOP1" ["SELENOThit1"]="SELENOT" ["TXNRD2hit1"]="TXNRD2" ["TXNRD2hit2"]="TXNRD2" ["TXNRD2hit3"]="TXNRD2" ["TXNRD2hit4"]="TXNRD2" ["eEFsechit1"]="eEFsec" ["eEFsechit2"]="eEFsec" ["MsrAhit1"]="MsrA" ["MsrAhit2"]="MsrA" ["SBP_1hit1"]="SBP_1" ["SBP_1hit2"]="SBP_1" ["Secp43hit1"]="Secp43" ["Secp43hit2"]="Secp43" ["Secp43hit3"]="Secp43" ["Secp43hit4"]="Secp43" ["SELENOO_2hit1"]="SELENOO_2" ["SELENOO_2hit2"]="SELENOO_2" ["GPx7hit1"]="GPx7" ["GPx7hit2"]="GPx7" ["GPx7hit3"]="GPx7" ["GPx7hit4"]="GPx7" ["GPx7hit5"]="GPx7" ["MSRB3hit1"]="MSRB3" ["MSRB3hit2"]="MSRB3" ["SBP2_2hit1"]="SBP2_2" ["SBP2_2hit2"]="SBP2_2" ["SecS_1hit1"]="SecS_1" ["SELENOKhit1"]="SELENOK" ["SELENOO_4hit1"]="SELENOO_4" ["SELENOO_4hit2"]="SELENOO_4" ["GPx8hit1"]="GPx8" ["GPx8hit2"]="GPx8" ["GPx8hit3"]="GPx8" ["GPx8hit4"]="GPx8" ["GPx8hit5"]="GPx8" ["PSTKhit1"]="PSTK" ["SBP2_3hit1"]="SBP2_3" ["SecS_2hit1"]="SecS_2" ["SEPHShit1"]="SEPHS" ["SEPHShit2"]="SEPHS")


	
#Per entrar al loop per cada hit
for hit in $hits

do
	#Per fer un directori per cada hit
	mkdir -p $PATH_parus/$hit/
   
	cd "$PATH_parus/$hit/" 


	#Per definir la sequencia ($seq variable) d’on provè el hit
	seq=$(cat $PATH_hits/$hit | cut -f1)

	#Per obtenir la seuqencia del hit executem fatsfetch
	touch $hit.seq.fetch
fastafetch $PATH_files/genome.fa $PATH_files/genome.index $seq > $hit.seq.fetch


	
	##Per definir les variables inici i llargada per el programa fastasubseq: 
	# Tallar les variables $start i $lenght del fitxer de hits 

	start=$(cat $PATH_hits/$hit | cut -f2) 
	length=$(cat $PATH_hits/$hit | cut -f3)	
	
	  
  #Per obtenir el motlle(genomic.fa) executem fastasubseq de cada hit
	fastasubseq  $hit.seq.fetch $start $length > $hit.genomic.fa


	#Per predir l’estructura del gen executem exonerate 
	exonerate -m p2g --showtargetgff -q $PATH_proteins/${hits_proteins[$hit]} -t $hit.genomic.fa > $hit.exonerate.gff	


	#Per extreure els exons de la predicció d’exonerate
	egrep -w exon $hit.exonerate.gff > $hit.exon.gff
	
	#Per canviar el format de GFF a Fasta	
	fastaseqfromGFF.pl $hit.genomic.fa $hit.exon.gff > $hit.pred.nuc.fa #predicció dels gens en format Fasta (nomes exons)


#	fastaseqfromGFF.pl $hit.genomic.fa $hit.exonerate.gff > $hit.SECIS.nuc.fa #predicció dels gens en format Fasta (introns i exons)

	#Per traduir el cDNA obtingut a proteïna utilitzem el Fastatranslate
	fastatranslate -F 1 $hit.pred.nuc.fa > $hit.pred.aa.fa 


	#Per comparar les dues sequencies d’Aa predites de Gallus gallus i Parus major utilitzem T-coffee
	t_coffee $hit.pred.aa.fa $PATH_proteins/${hits_proteins[$hit]} > $hit.tcoffee 
done



