#!/usr/bin/perl  -w

if(scalar(@ARGV) < 2) {
    print "runAmemeMuscle.pl - Run the improbizer lots of times for a series of files
of interest. Assumes that they have the 'up' vs. 'down' naming
convention for the 3' and 5' intronic sequences respectively.
Uses the appropriate control as a background distribution.

usage:
   runAmeme.pl outputSummary.html file1Inc.fa file2Inc.fa ...
";
    exit(1);
}
$ameme = "/cluster/home/sugnet/bin/i386/ameme";
$upBackground = "fa/mm2.control.gold.up.fa";
$downBackground = "fa/mm2.control.gold.down.fa";

$file = shift(@ARGV);
open(HTML, ">$file");
print HTML "<html><body><table>\n";
$htmlDir = 'html/';
$tmpDir = 'tmp/';
$numControls = 100;
$controlDir = 'controls/';
print HTML "<tr><th>File</th><th>Score</th><th>motif</th><th>Controls</th></tr>\n";
while($goodFile = shift(@ARGV)) {
    print HTML "<tr><td>$goodFile</td>";
    chomp($goodFile);
    $ave = 0;
    $min = 1000000000;
    $max = -1000000000;
    print STDERR "Doing $goodFile\n";
    $prefix=`basename $goodFile`;
    chomp($prefix);
    if($goodFile =~ /(.*)up(.*)/) {
	$background = $upBackground;
    }
    elsif($goodFile =~ /(.*)down(.*)/) {
	$background = $downBackground;
    }
    else {
	die "Can't parse 'up' or 'down' out of $goodFile\n";
    }
    $command = "$ameme good=$goodFile numMotifs=4 background=m2 maxOcc=1 motifOutput=$htmlDir$prefix.motif html=$htmlDir$prefix.html gif=$htmlDir$prefix.gif constrainer=6 bad=$background startScanLimit=1000 2>&1 /dev/null";
    `$command`;
    @p = split /\s/, `head -n 1 $htmlDir$prefix.motif`;
    chomp($p[5]);
    print HTML "<td>$p[0]</td><td><a href='./$htmlDir$prefix.html'>$p[5]</a></td>";
    print STDERR "Doing controls";
    for($i=0; $i<$numControls; $i++) {
	print STDERR ".";
	`rm -f $controlDir$prefix.controls`;
	$command = "$ameme good=$goodFile numMotifs=4 background=m2 maxOcc=1 motifOutput=$tmpDir$prefix.tmp.motif html=$tmpDir$prefix.tmp.html gif=$tmpDir$prefix.tmp.gif constrainer=6 bad=$background controlRun=on 2>&1 /dev/null";
	`$command`;
	`head -n 1 $tmpDir$prefix.tmp.motif >> $controlDir$prefix.controls`;
	@p = split /\s/, `head -n 1 $tmpDir$prefix.tmp.motif`;
	if($p[0] >= $max) { $max = $p[0]; }
	if($p[0] <= $min) { $min = $p[0]; }
	$ave += $p[0];
    }
    print STDERR "\n";
    $ave = $ave / $numControls;
    print HTML "<td>$ave ($min-$max N=$numControls)</td></tr>\n";
}
print HTML "</table></body></html>\n";

