
package DAFS;

use Exporter;
@ISA = (Exporter);
@EXPORT = qw(RunDAFSMode);

use Modules::DAFS_MakeList;
use Modules::DAFS_Run;
use Modules::DAFS_SimMatrix;

sub new{

    my $this = shift;
    my (
	$infile,
	$outfile, 
	$tmpdir,
	$alpha,
	$maxgap, 
	$CovThreshold,
	$LenThreshold,
	$bothendzero,
	$cut,
	$beta,
	$weight_ss,
	$weight_shape,
	$align_th,
	$Seq_Align_model,
	$rotherDAFSoptions,
	$align_mix_geometric,
	$weight,
	$sim_model,
	$cosgap_model,
	) = @_ ;
    
    my $DAFS = {
	"in" => $infile,
	"out" => $outfile,
	"tmp" => $tmpdir,
	"alpha" => $alpha,
        "maxgap" => $maxgap,
        "CovThreshold" => $CovThreshold,
	"LenThreshold" => $LenThreshold,
	"bothendzero" => $bothendzero,
	"cut" => $cut,
	"beta" => $beta,
	"weight_ss" =>$weight_ss,
	"weight_shape" => $weight_shape,
	"align_th" => $align_th,
	"Seq_Align_model" => $Seq_Align_model,
	"other" => $rotherDAFSoptions,
	"align_mix_geometric" => $align_mix_geometric,
	"weight" => $weight,
	"sim_model" => $sim_model,
	"cosgap_model" => $cosgap_model,
    };

    bless $DAFS, $this;
    return $DAFS;
}

sub RunDAFSMode
{
    my $this = shift;

    my @List = (); #$List[0]:geneName, $List[1]:sequence, $List[2]:Coverages Vector, ($List[3]:ExonInfo)
    my $DAFS_MakeList = 
	new DAFS_MakeList $this->{in}, $this->{CovThreshold}, 
	$this->{LenThreshold}, $this->{cut};
    $DAFS_MakeList->makeList(\@List);
    my $ListCount = $#List+1;
    print "parse Regions(or Genes) Count : $ListCount\n";

    my $options = $this->makeCommandOptions();
    #print "$options\n";
    my @preSimMatrix = (); 
    #preSimMatrix[~][~][0]:secondary structure Similarity by Ensembl Distance (0~0.5),
    #preSimMatrix[~][~][1]:average similarity score of Alignment by Sharaku and Seq Alignment (0~1),
    #preSimMatrix[~][~][2]:similarity score of Alignment by Sharaku (0~1),
    #preSimMatrix[~][~][3]:similarity score of Alignment by Seq Alignment (0~1),
    #preSimMatrix[~][~][4]:mapping shapes Similarity(0~1) 
    my @geneNamesIndex = ();

    my $DAFS_Run = new DAFS_Run $this->{tmp}, $this->{cosgap_model};
    $DAFS_Run->dafs_run(\@List, \@preSimMatrix, \@geneNamesIndex, $options);

    my @SimMatrix = ();
    my $DAFS_SimMatrix =
	new DAFS_SimMatrix $this->{out}, $this->{weight}, $this->{sim_model};  
    $DAFS_SimMatrix->makeSimMatrix(\@SimMatrix,\@preSimMatrix); 
    $DAFS_SimMatrix->output_SimMatrix(\@geneNamesIndex, \@SimMatrix);
}

sub makeCommandOptions
{
    my $this = shift;
    my $weight_Seq=1-$this->{weight_shape};

    my $options = " -a Sharaku --align-weight=$this->{weight_shape} -a $this->{Seq_Align_model} --align-weight=$weight_Seq --align-th=$this->{align_th}";
    if($this->{align_mix_geometric}){ $options = $options." --align-mix-geometric"; }
    if( $this->{maxgap} < 0 ){ $options .= " --alpha=$this->{alpha} --beta=$this->{beta} --weight=$this->{weight_ss}"; }
    else{ $options .= " --max-gap=$this->{maxgap} --beta=$this->{beta} --weight=$this->{weight_ss}"; }
    if(!$this->{bothendzero}){ $options .= " --both-end-zero"; }
    for(my $i=0; $i<=$#{$this->{other}}; $i++){ $options .= " $this->{other}[$i]"; }
    #print "run dafs options : $options\n\n";
    return $options;
}

1;
