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

my $p; #proteïna Mus Musculus
my $gen; #genoma Mus Spicilegus


print "Nom de la proteïna de Mus Musculus:";
$p = <STDIN>;
chomp ($p);

# U SUBSTITUTION
system("sed 's/U/X/g' $p.aa.fa > $p.fa");

print "U changed\n";

# TBLASTN

system ("tblastn -query $p.fa -db /mnt/NFS_UPF/bioinfo/BI/genomes/2018/Mus_spicilegus/genome.fa -outfmt 6 -evalue 0.01 -out $p.blast");
print "tblastn finished\n";

# FASTAFETCH

print "Introdueix el nom del scaffold:";
my $scaffold = <STDIN>;
chomp ($scaffold);

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

system ("fastafetch /mnt/NFS_UPF/bioinfo/BI/genomes/2018/Mus_spicilegus/genome.fa /mnt/NFS_UPF/bioinfo/BI/genomes/2018/Mus_spicilegus/genome.index $scaffold > $p.$scaffold.fastafetch.fa");
 
print "Fastafetch finished\n";

#FASTASUBSEQ

print "Search length of scaffold\n";
system ("grep $scaffold /mnt/NFS_UPF/bioinfo/BI/genomes/2018/Mus_spicilegus/genome.index\n");

print "Run FASTAsubseq\n";
print "Introduce start position:";
my $start = <STDIN>;
chomp ($start);

$start = $start - 50000;
if ($start < 0) { 	
    $start = 1; 	
}


print "Introduce final position:";
my $final = <STDIN>;
chomp ($final);

print "Introduce length of the scaffold:";
my $length =  <STDIN>;
chomp ($length);


if ($final > $length) { 	
    $final = $length;
}
else {
    $final = $final + 50000;
}



my $long = $final - $start;

system ("fastasubseq $p.$scaffold.fastafetch.fa $start $long > $p.$scaffold.fastasubseq.fa");

print "Fastasubseq done\n";


# EXONERATE 

system ("exonerate -m p2g --showtargetgff -q $p.fa -t $p.$scaffold.fastasubseq.fa | egrep -w exon > $p.$scaffold.exon.gff");

print "Exonerate done\n";



# FASTASEQFROMGFF

system ("fastaseqfromGFF.pl $p.$scaffold.fastasubseq.fa  $p.$scaffold.exon.gff > $p.$scaffold.nt.fa");

print "Fastaseqfromgff done\n";



# FASTATRANSLATE 

system ("fastatranslate -f $p.$scaffold.nt.fa -F 1 > $p.$scaffold.pep.fa");

print "Fastatranslate done\n";



# TCOFFEE  

system ("t_coffee $p.fa $p.$scaffold.pep.fa > $p.$scaffold.tcoffee.txt");

print "Tcoffee done\n";

    





