#! /usr/bin/perl -w

$db1 = $ARGV[0];
$db2 = $ARGV[1];

my %db1_value;
my $i=0;
my $j=0;
my $k=0;
my $status = 1;
my $command="";
my $db_name="";

for($j=1;$j<=4;$j++)
{
for($k=1;$k<=5;$k++)
{
$i=($k-1)*4+$j+1;
$status=1;
$command = "cut -f1,".$i." $db1 > temp.tmp";
system("$command");
open(FIN,"temp.tmp") || die "can't open temp.tmp\n";
while(<FIN>)
{
	if($status == 1)
        {
                $status = 0;
		if(/^(.*?)\s+(.*?)$/)
		{
			$db_name=$2;
		}
                next;
        }
	chomp;
	if(/^(.*?)\s+(.*?)$/)
	{
		$db1_value{$1}=$2;
	}
}
close(FIN);

open(FOUT, "> temp_pair.dat") || die "can't open temp_pair.dat\n";
print FOUT "Name\tdb1\tdb2\n";
$status = 1;
$command = "cut -f1,".$i." $db2 > temp.tmp";
system("$command");
open(FIN,"temp.tmp") || die "can't open temp.tmp\n";
while(<FIN>)
{
	chomp;
	if($status == 1)
	{
		$status = 0;
		next;
	}
	if(/^(.*?)\s+(.*?)$/)
	{
		if(exists( $db1_value{$1} ) )
		{
			print FOUT $1."\t".$db1_value{$1}."\t".$2."\n";
		}
		else
		{
			print STDERR "Fatal error: can't find match for $1\n";
			exit(0);
		}
	}
}
close(FIN);
close(FOUT);
system("paired_t_test -i temp_pair.dat -o temp.dat");
system("echo $db_name >> sen_sel_pair_result.dat");
system("cat temp.dat >> sen_sel_pair_result.dat");
system("rm -f temp_pair.dat");
system("rm -f temp.dat");
}
}
