#!/usr/bin/perl -w
use strict;

my $p; #protein
my $s; #scaffold
my $i; #first nucleotide
my $l; #length of the hit
my $k; #the content of the file ending with .p
my $q; #the future content of the file in which the * are changed for X


#First of all the program creates a folder in which is going to save all the sequences of selenoproteins from Danio Rerio
system ("mkdir -p treball/run");

#The program asks the name of protein we are going to work with
print "Enter the name of the protein ";
$p = <STDIN>;
chomp ($p);
system ("mkdir -p treball/run/$p");

#The program asks to enter the scaffold
print "Enter the scaffold: ";
$s = <STDIN>;
chomp ($s);

#The program wants to know the first nucleotide of the scaffold
print "Enter the first nucleotide: "; 
$i = <STDIN>;
chomp ($i);

#The program extends the sequence 50000 nucleotides on both ends and puts a 0 in case it is a negative number
$i=$i-50000;
$l=100000;
if ($i < 0) {
$i = 0
};

#The program changes the "U" of the selenocistein for an "X"
system ("sed  '1 ! s/U/X/g' /export/home/u114891/treball/fastas/$p.fa  > $p.x.fa");

system ("fastafetch  /cursos/20428/BI/genomes/2017/Monopterus_albus/genome.fa  /cursos/20428/BI/genomes/2017/Monopterus_albus/genome.index '$s' > $s.fa");

print "Fastafetch done\n";  

system ("fastasubseq $s.fa $i $l > $p.extraction.$s.fa"); 

print "Fastasubseq done\n";

system ("exonerate  -m p2g --showtargetgff -q $p.x.fa -t $p.extraction.$s.fa > $p.$s.exonerate");
 
print "Exonerate done\n";

system ("egrep -w exon $p.$s.exonerate > $p.$s.exons.exonerate");

print "Egrep done\n";

system ("fastaseqfromGFF.pl $p.extraction.$s.fa $p.$s.exons.exonerate > $p.$s.nt");

print "FastaseqfromGFF done\n";

system ("fastatranslate -f $p.$s.nt -F 1 > $p.$s.p");

print "Fastatranslate done\n";

#Now the program changes the * for X in the file ending with .p and saves this new content in a new file ending with .p.x

system ("sed  '1 ! s/\*/X/g' $p.$s.p  > $p.$s.p.x");

system ("t_coffee $p.x.fa $p.$s.p.x > $p.tcoffee 2>/dev/null");

system ("more $p.tcoffee");

system ("mv $p* treball/run/$p");

print "THE END!\n";

