#!/usr/bin/perl -w
use strict;
#This program was developed by: Maria Quintana, Claudia Pallise, Socayna Jouide and Júlia Urgel.

my $s; #scaffold
my $p; #protein
my $g; #genome
my $start; #first nucleotide
my $final; #last nucleotide
my $length; #hit lenght
my $strand; #positive or negative strand


#################
# CREATE FOLDER #
#################

system ("mkdir -p treball");

#The program asks for the name of protein
print "Enter the name of the protein: ";
$p = <STDIN>;
chomp ($p);
system ("mkdir -p treball/$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 "First nucleotide of the alignment: ";
$start = <STDIN>;
chomp ($start);

print "Final nucleotide of the alignment: ";
$final = <STDIN>;
chomp ($final);

#The program runs the fastafetch command

system ("fastafetch /mnt/NFS_UPF/bioinfo/BI/genomes/2018/Helostoma_temminkii/genome.fa /mnt/NFS_UPF/bioinfo/BI/genomes/2018/Helostoma_temminkii/genome.index '$s' > $s.fa");

print "Fastafetch finished\n";

#The program asks for the length of the scaffold in order to calculate the length

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

#The program extends the sequence 50000 nucleotides on both ends

$start=$start-50000;

if ($start<0){
    $start=0;
}
print "$start\n";

$length=($final+50000)-$start;

if ($slength<($start+$length)){
    $length=$slength-$start-1;
}

print "$length\n";

#The program runs the fastasubseq command

system ("fastasubseq $s.fa $start $length > $p.genomic.fa"); 

print "Fastasubseq finished\n";

#The program runs the exonerate command

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

print "Exonerate finished\n";

#The program runs the fastaseqfromGFF command

system ("fastaseqfromGFF.pl $p.genomic.fa $p.pre.gff > $p.pre.nuc.fa");

print "FastaseqfromGFF finished\n";

#The program runs the fastatranslate command

system ("fastatranslate $p.pre.nuc.fa -F 1 > $p.pre.aa.fa");

# Subtitution of * for X

system ("sed s/*/X/g $p.pre.aa.fa > $p.preX.aa.fa");

print "Fastatranslate finished\n";

#The program runs the T-coffee

system ("t_coffee $p.fa $p.preX.aa.fa > $p.tcoffee.fa");

#The program asks for the strand of the scaffold where the gene is located and runs the Genewise command

print "+ or - strand: ";
$strand = <STDIN>;
chomp ($strand);

if ($strand eq "+") {

	print "Running genewise for a positive strand\n";

	system ("genewise -pep -pretty -cdna -gff $p.fa $p.genomic.fa > $p.genewise.info.txt");

	system ("genewise -pep $p.fa $p.genomic.fa > $p.genewise.raw.fa");

}

else {

	print "Running genewise for a negative strand\n";	
	
	system ("genewise -pep -pretty -trev -cdna -gff $p.fa $p.genomic.fa > $p.genewise.info.txt");

	system ("genewise -pep -trev $p.fa $p.genomic.fa > $p.genewise.raw.fa");

}

#The program runs the T-coffee with the file obtained with Genewise 

system ("grep -v -e 'Making' -e '//' $p.genewise.raw.fa > $p.genewise.fa");

system ("t_coffee $p.fa $p.genewise.fa > $p.tcoffee.genewise.fa");


#The program moves all the files that start with the protein names to the folder previously created

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


print "SEE YOU LATER, MATE\n";
