#!/bin/bash

mkdir -p ../Exonerate
mkdir -p ../Exonerate/exonerateexit 2> error.temp
mkdir -p ../Exonerate/exonerateexon 2> error.temp
mkdir -p ../Exonerate/querysambX 2> error.temp	
mkdir -p ../Exonerate/fastaseqfromGFF 2> error.temp
mkdir -p ../Exonerate/fastatranslate 2> error.temp

	for year_d in ../fastasubseq/fastasubseqexit/*; do {
		year=`basename $year_d`

	mkdir -p ../Exonerate/exonerateexit/$year
	mkdir -p ../Exonerate/exonerateexon/$year
	
	for genome_d in ../fastasubseq/fastasubseqexit/$year/*; do {
		genome=`basename $genome_d`

		mkdir -p ../Exonerate/exonerateexit/$year/$genome 2> error.temp
		mkdir -p ../Exonerate/exonerateexon/$year/$genome 2> error.temp
		

			for query_donanom in ../fastasubseq/fastasubseqexit/$year/$genome/*; do {
			
				query=`basename $query_donanom`
					
					mkdir -p ../Exonerate/exonerateexit/$year/$genome/$query 2> error.temp
					mkdir -p ../Exonerate/exonerateexon/$year/$genome/$query 2> error.temp
				
					for hit_carpetes_d in ../fastasubseq/fastasubseqexit/$year/$genome/$query/*; do {

						hitcarpetes=`basename $hit_carpetes_d`
											
					
						mkdir -p  ../Exonerate/exonerateexit/$year/$genome/$query/$hitcarpetes 2> error.temp
						mkdir -p ../Exonerate/exonerateexon/$year/$genome/$query/$hitcarpetes 2> error.temp

						for hit_bo in ../fastasubseq/fastasubseqexit/$year/$genome/$query/$hitcarpetes/*; do {
						
							hitbo=`basename $hit_bo`

							
perl substitucio.pl < ../Querys/$query.fa > ../Exonerate/querysambX/$query.fa

exonerate -m p2g --showtargetgff -q ../Exonerate/querysambX/$query.fa -t $hit_bo > ../Exonerate/exonerateexit/$year/$genome/$query/$hitcarpetes/alineament_$hitbo

exonerate -m p2g --showtargetgff -q ../Exonerate/querysambX/$query.fa -t $hit_bo | egrep -w exon > ../Exonerate/exonerateexon/$year/$genome/$query/$hitcarpetes/alinexon_${hitbo}.gff


Noencuentronah=`cat ../Exonerate/exonerateexon/$year/$genome/$query/$hitcarpetes/alinexon_${hitbo}.gff`

if [ "$Noencuentronah" != "" ]; then 
				
				mkdir -p ../Exonerate/fastaseqfromGFF/$year/$genome 2> error.temp
				mkdir -p ../Exonerate/fastatranslate/$year/$genome 2> error.temp
				mkdir -p ../Exonerate/fastaseqfromGFF/$year/$genome/$query 2> error.temp
				mkdir -p ../Exonerate/fastatranslate/$year/$genome/$query 2> error.temp
				mkdir -p ../Exonerate/fastaseqfromGFF/$year/$genome/$query/$hitcarpetes 2> error.temp
				mkdir -p ../Exonerate/fastatranslate/$year/$genome/$query/$hitcarpetes 2> error.temp
			
	perl fastaseqfromGFF.pl $hit_bo ../Exonerate/exonerateexon/$year/$genome/$query/$hitcarpetes/alinexon_${hitbo}.gff > ../Exonerate/fastaseqfromGFF/$year/$genome/$query/$hitcarpetes/cDNA_$hitbo

	fastatranslate ../Exonerate/fastaseqfromGFF/$year/$genome/$query/$hitcarpetes/cDNA_$hitbo > ../Exonerate/fastatranslate/$year/$genome/$query/$hitcarpetes/protein_$hitbo 

fi


					} done
	
			} done
		} done
	} done

} done
rm ./*.temp

