my $blastcarpet = `mkdir blasts`;
my $exoneratecarpet = `mkdir exonerate`;
my $fastasubseqcarpet = `mkdir fastasubseq`;
my $exoncountcarpet = `mkdir exoncount`;
my $cDNAcarpet = `mkdir cDNA`;
my $scaffoldscarpet = `mkdir scaffolds`;
my $translatecarpet = `mkdir translate`;
my $tcoffee = `mkdir t-coffee`;


my $directory = 'proteines/';
opendir (DIR, $directory) or die $!;

while (my $file = readdir(DIR)) {
   my $result = `/cursos/BI/bin/ncbiblast/bin/blastall -p tblastn -i proteines/$file -d /cursos/BI/genomes/project_2014/Elephantulus_edwardii/genome.fa -o blasts/$file.blast -e 0.0001 -m 8`;
}
opendir(PROT, "proteines");
while (my $name = readdir(PROT)){
    opendir(TCOF, "t-coffee");
    my $ordre = `mkdir t-coffee/$name.t_coffee`;
}

opendir(BLASTDIR, "blasts") or die $!; # obro directori blasts
while (my $file = readdir(BLASTDIR)){
	my %ids;
	my $bestscore = 0;
	my $bestid;

  next if ($file =~ m/^\./); # no processis fitxers que comencin amb .

  open(BLAST, " < blasts/$file");
  while (<BLAST>) {
    chomp;
    my @fields = split ("\t", $_);
    my $identificador = $fields[1];
    my $score = $fields[11];

    my $start_pos;
    my $stop_pos;

    if ($fields[8] < $fields[9]) {
	$start_pos = $fields[8];
	$stop_pos = $fields[9];
    }
    else {
	$start_pos = $fields[9];
	$stop_pos = $fields[8];
    }

    # Identificador NO existeix
    if(!exists($ids{$identificador})) {
	$ids{$identificador}{"START"} = $start_pos;
	$ids{$identificador}{"STOP"} = $stop_pos;
    }
    else {
	if ($start_pos < $ids{$identificador}{"START"}){
	    $ids{$identificador}{"START"} = $start_pos;
	}
	if ($stop_pos > $ids{$identificador}{"STOP"}){
	    $ids{$identificador}{"STOP"} = $stop_pos;
	}
    }
    if ($score > $bestscore){
	$bestscore = $score;
	$bestid = $identificador;
    }
  }

print "$file\t$bestid\t$bestscore\t", $ids{$bestid}{"START"}, "\t", $ids{$bestid}{"STOP"}, "\n";
close(BLAST);

my $fastafetch = `/cursos/BI/soft/exonerate/i386/bin/fastafetch /cursos/BI/genomes/project_2014/Elephantulus_edwardii/genome.fa genome.index \"$bestid\" > scaffolds/$file.scaffold`;

my $start = $ids{$bestid}{"START"} - 50000;
my $llargada = $ids{$bestid}{"STOP"} - $ids{$bestid}{"START"} + 100000;
my $fastasubseq = `/cursos/BI/soft/exonerate/i386/bin/fastasubseq scaffolds/$file.scaffold $start $llargada > fastasubseq/$file.fastasubseq`;

my $protein_file = $file;
$protein_file =~ s/\.blast//g;

my $exonerate = `/cursos/BI/soft/exonerate/i386/bin/exonerate -m p2g --showtargetgff -q proteines/$protein_file -t fastasubseq/$file.fastasubseq > exonerate/$file.exonerate.gff`;
my $exoneratecount = `/cursos/BI/soft/exonerate/i386/bin/exonerate -m p2g --showtargetgff -q proteines/$protein_file -t fastasubseq/$file.fastasubseq | egrep -w exon > exoncount/$file.exo.exonerate.gff`;
my $fastaseqfromgff = `/cursos/BI/bin/fastaseqfromGFF.pl ./fastasubseq/$file.fastasubseq ./exoncount/$file.exo.exonerate.gff > ./cDNA/$file.pl.fa`;

my $fastatranslatefor1 = `/cursos/BI/soft/exonerate/i386/bin/fastatranslate -F 1 cDNA/$file.pl.fa > translate/$protein_file.for1.tr.fa`; 
my $fastatranslatefor2 = `/cursos/BI/soft/exonerate/i386/bin/fastatranslate -F 2 cDNA/$file.pl.fa > translate/$protein_file.for2.tr.fa`; 
my $fastatranslatefor3 = `/cursos/BI/soft/exonerate/i386/bin/fastatranslate -F 3 cDNA/$file.pl.fa > translate/$protein_file.for3.tr.fa`; 

my $fastarevcomp = `/cursos/BI/soft/exonerate/i386/bin/fastarevcomp cDNA/$file.pl.fa > cDNA/$file.pl.rev.fa`;

my $fastatranslaterev3 = `/cursos/BI/soft/exonerate/i386/bin/fastatranslate -F 3 cDNA/$file.pl.rev.fa > translate/$protein_file.rev3.tr.fa`; 
my $fastatranslaterev2 = `/cursos/BI/soft/exonerate/i386/bin/fastatranslate -F 2 cDNA/$file.pl.rev.fa > translate/$protein_file.rev2.tr.fa`; 
my $fastatranslaterev1 = `/cursos/BI/soft/exonerate/i386/bin/fastatranslate -F 1 cDNA/$file.pl.rev.fa > translate/$protein_file.rev1.tr.fa`; 



my $tcoffeefor1 = `/cursos/BI/bin/t_coffee proteines/$protein_file translate/$protein_file.for1.tr.fa > t-coffee/$protein_file.t_coffee/$protein_file.for1`;
my $tcoffeefor2 = `/cursos/BI/bin/t_coffee proteines/$protein_file translate/$protein_file.for2.tr.fa > t-coffee/$protein_file.t_coffee/$protein_file.for2`;
my $tcoffeefor3 = `/cursos/BI/bin/t_coffee proteines/$protein_file translate/$protein_file.for3.tr.fa > t-coffee/$protein_file.t_coffee/$protein_file.for3`;
my $tcoffeerev3 = `/cursos/BI/bin/t_coffee proteines/$protein_file translate/$protein_file.rev3.tr.fa > t-coffee/$protein_file.t_coffee/$protein_file.rev3`;
my $tcoffeerev2 = `/cursos/BI/bin/t_coffee proteines/$protein_file translate/$protein_file.rev2.tr.fa > t-coffee/$protein_file.t_coffee/$protein_file.rev2`;
my $tcoffeerev1 = `/cursos/BI/bin/t_coffee proteines/$protein_file translate/$protein_file.rev1.tr.fa > t-coffee/$protein_file.t_coffee/$protein_file.rev1`;
}
close(BLASTDIR);
