#!/usr/bin/perl -w

#Aquest codi esta fer per el Martí Boltá, la Gemma Algaba, la Laura Ciarán i la Júlia Coll
use strict;

my $dir;
my $nomquery;
my $query;
my $querymod;
my $row;
my $scaffold;
my $start;
my $length;
my $ambasterisc;
my $ambx;
my $fila;
my $inici;
my $final;
my $llargada;
my $columna9;
my $columna10;
my $llegirinici;
my $llegirfinal;
my $longitud;
my $llegirlongitud;
my $line;
my $possiblesscaffolds;

use Cwd qw(cwd);
$dir = cwd;

print "Escriu la selenoproteina que vols comparar:";

$nomquery = <STDIN>;
chomp ($nomquery);

open ($query,$nomquery); 
open ($querymod, '>', 'querydefinitiva.fa'); 

while ($row = <$query>){
    chomp $row;
    if ($row =~ m/^>/){
	print $querymod "$row\n"; 
    }else{
	$row =~ s/U/X/g;
	print $querymod "$row\n"; 
    }
 
}

close $query;
close $querymod;

system ("tblastn -query querydefinitiva.fa -db $dir/Callopanchax_toddi/genome.fa -outfmt 6 -evalue 0.01 -out query.blast");

print "Els resultats del blast són aquests:\n";

system ("cat query.blast");

print "Els scaffolds on pots trobar amb més probabilitat la proteïna són:\n";

system ("cut -f2 query.blast | sort | uniq");

system ("cut -f2 query.blast | sort | uniq > possiblesscaffolds.fa");

open ($possiblesscaffolds,'possiblesscaffolds.fa');
chomp $possiblesscaffolds;

while ($line = <$possiblesscaffolds>){
    chomp $line;
    $scaffold = $line;
    system ("fastafetch $dir/Callopanchax_toddi/genome.fa $dir/Callopanchax_toddi/genome.index $scaffold > 'scaffold.fa'");

    system ("grep $scaffold query.blast > elmillorscaffold.blast");

    system ("cut -f 9 elmillorscaffold.blast > columnes.blast | cut -f 10 elmillorscaffold.blast st >> columnes.blast");

    system ("sort -n columnes.blast > columnesordenades.blast");

    system ("head -n 1 columnesordenades.blast > inici.blast");
    system ("tail -1 columnesordenades.blast > final.blast");

    system ("fastalength scaffold.fa > llargadascaffold.fa");
    system ("cut -d ' ' -f1 llargadascaffold.fa > longitud.fa");
    open ($longitud,'longitud.fa');
    chomp ($longitud);
    $llegirlongitud = <$longitud>;
    close $longitud;

    open ($inici, 'inici.blast');
    open ($final, 'final.blast');

    $llegirinici = <$inici>;
    chomp $inici;
    $llegirfinal = <$final>;
    chomp $final;

    $llegirinici = $llegirinici - 50000;
    if ($llegirinici < 0){
	$llegirinici = 0;
    }
    $llegirfinal = $llegirfinal + 50000;
    if ($llegirfinal > $llegirlongitud){
	$llegirfinal = $llegirlongitud;
    }
    close $inici;
    close $final;

    $llargada = $llegirfinal - $llegirinici;
    
    system ("fastasubseq scaffold.fa $llegirinici $llargada > 'tros.fa'");

    system ("exonerate -m p2g --showtargetgff -q querydefinitiva.fa -t tros.fa | egrep -w exon > 'comparacio.exonerate.gff'");

    system ("fastaseqfromGFF.pl tros.fa comparacio.exonerate.gff > sequenciacomparacio.fa");

    system ("fastatranslate -f sequenciacomparacio.fa -F 1 > sequenciacomparacio.aa.fa");

    open ($ambasterisc,'sequenciacomparacio.aa.fa'); 
    open ($ambx, '>', 'seqx.aa.fa'); 

    while ($fila = <$ambasterisc>){
	chomp $fila;
	if ($fila =~ m/^>/){
	    print $ambx "$fila\n"; 
	}else{
	    $fila =~ s/\*/X/g;
	    print $ambx "$fila\n"; 
	}
 
    }

    close $ambasterisc;
    close $ambx;

    system ("t_coffee querydefinitiva.fa seqx.aa.fa > tcoffee.fa");

    system ("mkdir -p treball/$nomquery");
    system ("mkdir -p treball/$nomquery/$scaffold");
    system ("mkdir -p treball/$nomquery/$scaffold/Intermedis");
    system ("mkdir -p treball/$nomquery/$scaffold/Importants");
    system ("mv scaffold.fa treball/$nomquery/$scaffold/Intermedis");
    system ("mv columnes.blast treball/$nomquery/$scaffold/Intermedis");
    system ("mv columnesordenades.blast treball/$nomquery/$scaffold/Intermedis");
    system ("mv inici.blast treball/$nomquery/$scaffold/Intermedis");
    system ("mv final.blast treball/$nomquery/$scaffold/Intermedis");
    system ("mv tros.fa treball/$nomquery/$scaffold/Intermedis");
    system ("mv comparacio.exonerate.gff treball/$nomquery/$scaffold/Intermedis");
    system ("mv sequenciacomparacio.fa treball/$nomquery/$scaffold/Intermedis");
    system ("mv sequenciacomparacio.aa.fa treball/$nomquery/$scaffold/Intermedis");
    system ("mv seqx.aa.fa treball/$nomquery/$scaffold/Importants");
    system ("mv tcoffee.fa treball/$nomquery/$scaffold/Importants");
    system ("mv llargadascaffold.fa treball/$nomquery/$scaffold/Intermedis");
    system ("mv longitud.fa treball/$nomquery/$scaffold/Intermedis");
    system ("cp query.blast treball/$nomquery/$scaffold/Importants");
}


close $possiblesscaffolds;
system ("mkdir -p treball/$nomquery/Comuns");
system ("mv query.blast treball/$nomquery/Comuns");
system ("mv elmillorscaffold.blast treball/$nomquery/Comuns");
system ("mv possiblesscaffolds.fa treball/$nomquery/Comuns");
system ("mv querydefinitiva.fa treball/$nomquery/Comuns");
system ("mv querydefinitiva.aln treball/$nomquery/Comuns");
system ("mv querydefinitiva.html treball/$nomquery/Comuns");
