
package DAFS_SimMatrix;

use Exporter;
@ISA = (Exporter);
@EXPORT = qw(makeSimMatrix output_SimMatrix);

sub new{

    my $this = shift;
    my ( 
	$outfile,
	$weight,
	$sim_model
	) = @_;

    my $DAFS_SimMatrix = {
	"out" => $outfile,
	"weight" => $weight,
	"sim_model" => $sim_model
    };

    bless $DAFS_SimMatrix, $this;
    return $DAFS_SimMatrix;
}

sub makeSimMatrix
{
    my ($this, $rSimMatrix, $rpreSimMatrix) = @_;
    for(my $i=0; $i<=$#{$rpreSimMatrix}; $i++)
    {
	for(my $j=0; $j<=$#{@$rpreSimMatrix[$i]}; $j++) #reference??
	{
	    if($i == $j){$$rSimMatrix[$i][$j]=0;}
	    else
	    {
		$$rSimMatrix[$i][$j] = $this->selectSimilarity($i,$j,$rpreSimMatrix);
	    }
	}
    }
}

sub selectSimilarity
{
    my ($this, $i, $j, $rpreSimMatrix) = @_;

    if($this->{sim_model} eq "alignprob_cos")
    {
	return (1 - $$rpreSimMatrix[$i][$j][1]) * (1 - $$rpreSimMatrix[$i][$j][4]);
    }
    elsif($this->{sim_model} eq "alignprob")
    {
	return (1 - $$rpreSimMatrix[$i][$j][1]);
    }
    elsif($this->{sim_model} eq "cos")
    {
	return (1 - $$rpreSimMatrix[$i][$j][4]);
    }
}

sub output_SimMatrix
{
    my ($this, $geneNamesIndex, $rSimMatrix) = @_;
    open(OUT,"> $this->{out}") || die "Can't $this->{out} open: $!";
 
    for(my $i=0; $i<=$#{$rSimMatrix}; $i++)
    {
	print OUT "$$geneNamesIndex[$i]";
	for(my $j=0; $j<=$#{@$rSimMatrix[$i]}; $j++)
	{
	    if($j <= $i){ print OUT "\t$$rSimMatrix[$i][$j]"; }
	    else{ print OUT "\t"; }
	}
	print OUT "\n";
    }
}

1;
