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

my $parte1selenoproteina;
my $selenoproteina;
my $hit;
my $tblast;
my $inputstart;
my $inputend;
my $especie;
my $tipus;
my $tcoffeesi;
my $secisi;
my $blastiansi;
my $exoneratepath;
my $tcoffeepath;
    my $blastpath;
my $numeroespecie;
my $secisimagepath;
my $secispath;
my @blastiandirs;
my $blastiandirs1;
my $blastiandirs2;
my $secisarchivo;
my $blastianpath;

#Input with selected scaffolds for discussion and telling for which scaffolds we obtained a good T-coffee alignment, SECIS and protein prediction:
open (HITSFUSIO, "<./hitsfusio3");
my $hitsfusiotxt = join '', <HITSFUSIO>;
close HITSFUSIO;

my @blastrows = split ('\n', $hitsfusiotxt);

foreach my $blastrow (@blastrows) {

    my @blastinput = split ('\t', $blastrow);

    $selenoproteina = $blastinput[0];
    $hit = $blastinput[1];
    $inputstart = $blastinput[3];
    $inputend = $blastinput[4];
    $especie = $blastinput[2];
    $tcoffeesi = $blastinput[5];
    $secisi = $blastinput[6];
    $blastiansi = $blastinput[7];

$exoneratepath = "./resultfiles/exonerate/$especie.$selenoproteina.$hit.$inputstart.$inputend.exonerate.gff";
$tcoffeepath = "./resultfiles/SELECCIONTCOFFEE/$especie.$selenoproteina.$hit.4.$inputstart.$inputend.tcoffee.html";

    my $secisdirs1;
    my $secisdirs2;

#Different file organization were used for different species:
if ($secisi eq 'si'){
    if ($especie eq 'human'){
opendir (my $secisdir,"./resultfiles/secis$especie/$selenoproteina");
my @secisdirs = readdir $secisdir;
closedir $secisdir;

foreach my $secisarchivo (@secisdirs){
    if ($secisarchivo =~ /png/){
	$secisdirs1 = $secisarchivo;
    }
    if ($secisarchivo =~ /txt/){
        $secisdirs2 = $secisarchivo;
    }
}
$secispath = "./resultfiles/secis$especie/$selenoproteina/$secisdirs2";
$secisimagepath = "./resultfiles/secis$especie/$selenoproteina/$secisdirs1";
}


if ($especie eq 'horse'){
      
opendir (my $secisdir,"./resultfiles/secis$especie/$selenoproteina");
my @secisdirs = readdir $secisdir;
closedir $secisdir;

foreach my $secisarchivo (@secisdirs){
    if ($secisarchivo =~ /png/){
        $secisdirs1 = $secisarchivo;
    }
    if ($secisarchivo =~ /txt/){
        $secisdirs2 = $secisarchivo;
    }
}

$secisimagepath = "./resultfiles/secis$especie/$selenoproteina/$secisdirs1";

if ($blastiansi eq 'si'){

opendir (my $blastiandir,"./resultfiles/blastianhorse/$selenoproteina");
my @blastiandirs = readdir $blastiandir;
closedir $blastiandir;

foreach my $blastianarchivo (@blastiandirs){
    if ($blastianarchivo =~ /png/){
	$blastiandirs1 = $blastianarchivo;
    }
    if ($blastianarchivo =~ /txt/){
	$blastiandirs2 = $blastianarchivo;
    }
}

$blastianpath = "./resultfiles/blastianhorse/$selenoproteina/$blastiandirs2";
}

$secispath = "./resultfiles/secis$especie/$selenoproteina/$secisdirs2";
}

}
    my $numeroespecie;
    if ($especie eq "human"){
	$numeroespecie = "1";
    }
    if ($especie eq "horse"){
	$numeroespecie = "2";
    }


    if ($especie eq "human"){
	$blastpath = "./resultfiles/Humanblast/$numeroespecie-$selenoproteina.fa.blast";
}
    if ($especie eq "horse"){
	$blastpath = "./resultfiles/Horseblast/$numeroespecie-$selenoproteina.fa.blast";
    }

    my $proteinpath = "./resultfiles/SP$especie/$numeroespecie-$selenoproteina.fa";

my $cientificoespecie;
if ($especie eq "human"){
    $cientificoespecie = "Homo sapiens";
}
if ($especie eq "horse"){
    $cientificoespecie = "Caballus equus";
}

#Printing the table html code:
print '        <tr class=tableint>
            <th scope ="row" class=tabletitle>&nbsp;'; print "$selenoproteina"; print '</th>
            <td style=" font-style: italic">&nbsp;'; print "$cientificoespecie"; print '</td>
	    <td>&nbsp;<a href='; print "$proteinpath.txt"; print ' target="_blank"><img src="./images/lupasi.png" height="25" width="25"></i></a></td>
 <td>&nbsp;'; print "$hit"; print '</td>
	    <td>&nbsp;<a href= '; print "$blastpath.txt"; print ' target="_blank"><img src="./images/lupasi.png" height="25" width="25"></i></a></td>
	    <td>&nbsp;<a href='; print "$exoneratepath.txt"; print ' target="_blank"><img src="./images/lupasi.png" height="25" width="25"></i></a></td>';

if ( $tcoffeesi eq 'si'){
print '             <td>&nbsp;<a href='; print "$tcoffeepath"; print ' target="_blank"><img src="./images/lupasi.png" height="25" width="25"></i></a></td>
';
}

if ( $tcoffeesi eq 'no'){
print '            <td>&nbsp;<img src="./images/lupano.png" height="25" width="25"></i></td>
';
}

    if ($especie eq 'human'){
	if ( $secisi eq 'si'){
	    print '            <td>&nbsp;<a href='; print "$secispath"; print ' target="_blank"><img src="./images/lupasi.png" height="25" width="25"></i></a></td>';
	    print '            <td>&nbsp;<a href='; print "$secisimagepath"; print ' target="_blank"><img src="./images/lupasi.png" height="25" width="25"></i></a></td>
        </tr>
';
	}
    }
    if ($especie eq 'horse'){
if ( $secisi eq 'si' && $blastiansi eq 'si'){
print '            <td>&nbsp;<a href='; print "$secispath"; print ' target="_blank"><img src="./images/lupasi.png" height="25" width="25"></i></a></td>
            <td>&nbsp;<a href='; print "$blastianpath"; print ' target="_blank"><img src="./images/lupasi.png" height="25" width="25"></i></a></td>
';
print '            <td>&nbsp;<a href='; print "$secisimagepath"; print ' target="_blank"><img src="./images/lupasi.png" height="25" width="25"></i></a></td>
        </tr>
';
}

if ( $secisi eq 'si' && $blastiansi eq 'no'){
    print '            <td>&nbsp;<a href='; print "$secispath"; print ' target="_blank"><img src="./images/lupasi.png" height="25" width="25"></i></a></td>
            <td>&nbsp;<img src="./images/lupano.png" height="25" width="25"></i></td>
';
print '            <td>&nbsp;<a href='; print "$secisimagepath"; print ' target="_blank"><img src="./images/lupasi.png" height="25" width="25"></i></a></td>
        </tr>
';
}
    }
if ( $secisi eq 'no'){
print '            <td>&nbsp;<img src="./images/lupano.png" height="25" width="25"></i></td>
';
print '            <td>&nbsp;<img src="./images/lupano.png" height="25" width="25"></i></td>
        </tr>
';
}
 }   


