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

my $s; #scaffold
my $i; #first nucleotide
my $f; #last nucleotide
my $l; #hit lenght
my $p; #protein
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
my $r; #the answer to the question about the t-coffee result 

#First of all the program asks for the protein we are working with

print “Excuse me, which protein were you working with? ";
$p = <STDIN>;
chomp ($p);

print “Enter the scaffold: ";
$s = <STDIN>;
chomp ($s);

print “First nucleotide (the smallest): ";
$i = <STDIN>;
chomp ($i);

print “Final nucleotide (the largest): ";
$f = <STDIN>;
chomp ($f);

#The program extends the sequence 20000 nucleotides on both ends

$i=$i-20000;
$l=($f+20000)-$i;

#The program makes a directory named after the protein and the scaffold, in which every file will be saved

system ("mkdir $p.$s");

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

print “Fastafetch done!:)\n”;

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

print “Fastasubseq done!!\””;

system ("exonerate --exhaustive yes -m p2g --showtargetgff -q $p.anole.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.extraccio.$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 .pX

open INPUT, "$p.$s.p";
undef $/;
$k = <INPUT>;
close INPUT;
$/ ="\n";

$k =~ s/\*/X/g;

open(my $fh, '>', ‘previous’);
print $fh $k;
close $fh;

system ("mv previous $p.$s.pX"); 

print “Running T-Coffee…\n”;

system ("t_coffee $p.anole.fa $p.$s.pX > $p.$s.tcoffe");

#The program asks if we want to see in the screen the result of the T-Coffee

print "Do you want to see the result of the T-Coffee? ";
$r = <STDIN>;
chomp ($r);

if ($r eq “yes”) {
    system ("more $p.$s.tcoffe");
}

else {
    print "Okey!\n”;
}

#Finally all files are moved to the new directory

system ("mv $s.fa $p.$s");
system ("mv $p.extraction.$s.fa $p.$s");
system ("mv $p.$s.exonerate $p.$s");
system ("mv $p.$s.exons.exonerate $p.$s");
system ("mv $p.$s.nt $p.$s");
system ("mv $p.$s.p $p.$s");
system ("mv $p.anole.fa $p.$s");
system ("mv $p.blast $p.$s");
system ("mv $p.$s.pX $p.$s"); 
system ("mv $p.$s.tcoffe $p.$s");
system ("mv $p.anole.aln $p.$s");
system ("mv $p.anole.html $p.$s"); 

print “THE END!\n”;
