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

my $p; #contador proteines
my $q; #path de la query
my $g; #path del genoma
my $blast;
my $content;
my $contentfinal;
my $blastcut9;
my $blastcut10;
my $inici;
my $iniciprov;
my $iniciprov1;
my $iniciprov2;
my $numscaffold;
my $sizescaffold;
my $scaffold;
my $resta;
my $finalprov;
my $length;


print "Introduïr la proteïna a estudiar: ";
    $p= <STDIN>;
    chomp ($p);

    $q= "/home/u124776/Query/$p.fa";
    $g= "/mnt/NFS_UPF/soft/genomes/2019/Datnioides_undecimradiatus/genome.fa";
    my $gi= "/mnt/NFS_UPF/soft/genomes/2019/Datnioides_undecimradiatus/genome.index";
    $length = 200000;
    my $cadena = "/home/u124776/$p/tipuscadena.fa";

#amb system faré que l'ordinador em faci programes. 
    system ("sed s/@//g $q > temp; mv temp $q -f"); #canvia caracters com @ per res
    system ("sed s/\%//g $q > temp; mv temp $q -f");
    system ("sed s/\#//g $q > temp; mv temp $q -f");
    system ("sed s/U/X/g $q > temp; mv temp $q -f"); 
    
    system ("tblastn -query $q -db $g -outfmt 6 -evalue 0.0001 -out /home/u124776/$p/blastn$p");
    system ("cat /home/u124776/$p/blastn$p");


#############fastafetch##################

    print "escriu scaffold: \n";
    $scaffold= <STDIN>;
    chomp ($scaffold);
    system ("fastafetch $g $gi $scaffold  > /home/u124776/$p/scaffold$p");

  
    
#############FASTASUBSEQ#################

  #fer que no es passi al restar 
    print "Escriu el primer valor: \n";
    $iniciprov1 =<STDIN>;
    chomp ($iniciprov1);

    print "Escriu el segon valor: \n";
    $iniciprov2 =<STDIN>;
    chomp ($iniciprov2);

    
    if ($iniciprov1 < $iniciprov2){
	$iniciprov = $iniciprov1;
	print "la cadena és forward";
	my $str = "la cadena és forward" ;
         open(FH,'>', $cadena) or die $!;
	 print FH $str;
 	 close (FH);
    }
    else {$iniciprov = $iniciprov2;
	  print "la cadena és reverse \n";
	my $str2 = "la cadena és reverse";
	 open(FH,'>', $cadena) or die $!;
	 print FH $str2;
 	 close (FH);
}

    print "l'scaffold comença a: $iniciprov \n";

    
    $inici= $iniciprov - 100000; 
    if ($inici >= 0){
	$inici = $inici}
    else {$inici = 0};
    print "l'inici del scaffold és $inici \n";

    #length 
    system ("grep $scaffold /mnt/NFS_UPF/soft/genomes/2019/Datnioides_undecimradiatus/genome.lengths > /home/u124776/$p/lengths");
    print "la length de l'scaffold $scaffold es: ";
    system ("more /home/u124776/$p/lengths");

    print "introduiu la length scaffold: \n";
    $sizescaffold= <STDIN>;
    chomp ($sizescaffold);

    #per veure si entra he de fer una variable inici+200000 per veure si em passo
    
    
    if ($sizescaffold > ($inici + 200000)){
	$length = 200000;
	}
    else {$length = ($sizescaffold - $inici)} #hem de tenir en compte que si li dic al length = sizescaffold li hem de restar les posicions d'inici, ja que sino se sortira del scaffold
	
    
    
  
  system ("fastasubseq /home/u124776/$p/scaffold$p $inici  $length > /home/u124776/$p/subseq$p");

  
  
#############EXONERATE###################

  
  system ("exonerate -m p2g --exhaustive yes --showtargetgff -q $q -t /home/u124776/$p/subseq$p | egrep -w exon > /home/u124776/$p/prot$p.exonerate.gff");
  

  
############FASTASEQFROMGFF##############

  
  system ("fastaseqfromGFF.pl /home/u124776/$p/subseq$p  /home/u137265/$p/prot$p.exonerate.gff > /home/u124776/$p/prot$p.exonerate.fa");

  

############FASTATRANSLATE##############

    system ("fastatranslate -f /home/u124776/$p/prot$p.exonerate.fa -F 1 | sed 's/\*/X/g' > /home/u124776/$p/translate$p.fa");
    
#################TCOFEE#################

  system ("t_coffee /home/u124776/$p/translate$p.fa $q");
  
    



$blast = "blast\n";
system ("more blast")
