#!/usr/bin/perl -w

use strict;

use IPC::System::Simple qw(capture);

my $i;
my $l;
my @array;
my $identity;
my $a;
my @splitrow;
my $longitudlinea;
my $posiciolinea;
my $mutacions;
my $longitudseq;
my $splitrow;

@array = ("sel15", "eEFsec", "SELENOE","GPx1a","GPx1b","GPx2","GPx3a","GPx3b","GPx4a", "GPx4b", "GPx7", "GPx8", "DIO1", "DIO2","DIO3a", "DIO3b", "MsrA", "PSTK", "SBP2", "SecS", "SEPHS2", "SELENOH", "SELENOI", "SELENOJ", "SELENOK", "SELENOL", "SELENOM", "SELENON", "SELENOO1", "SELENOO2", "SELENOP1", "SELENOP2", "MSRB1a", "MSRB1b", "MSRB2", "MSRB3", "SELENOS", "SELENOT1", "SELENOT1b", "SELENOT2", "SELENOU1a", "SELENOU2", "SELENOU3", "SELENOW2", "TXNRD2", "TXNRD3", "SECp43");

$l = scalar @array;
$i = 0;


while ($i<$l) {
  print "\n";
  print "\n";
  print "COMENCEM AMB LA PROTEINA $array[$i]\n";

  my $filename = "output.tcoffee/$array[$i]xh.result.fa";
  open(my $fh, '<:encoding(UTF-8)', $filename) or
  die "Can't open '$filename': $!";

  $identity = 0;
    while (my $row = <$fh>) {
	chomp $row;

	@splitrow = split(//,$row);
	$longitudlinea = scalar(@splitrow);

	$posiciolinea = 0;
	while ($posiciolinea < $longitudlinea) {
	    if ($splitrow[$posiciolinea] eq "*") {
		$identity = $identity + 1;
	    }
	    $posiciolinea = $posiciolinea + 1;
	}
    }
    
    $longitudseq = `cut -d "=" -f 5 output.tcoffee/$array[$i]xh.result.fa | head -n 1`;

    $mutacions = 100 - (($identity / $longitudseq) * 100);
    print "La longitud de la sequencia predita es $longitudseq";
    print "El nombre d'identitat entre les sequencies es $identity.\n";
    print "El percentatge d'aminoàcids modificats és $mutacions.\n";

$i = $i + 1;
}
