#!@WHICHPERL@
=head1 NAME

mcast_webservice - Run mcast in a restricted mode and create an index webpage.

=head1 SYNOPSIS

mcast_webservice [options] <motifs> <sequence db>

      Options:
        -upseqs <file>        Uploaded sequences
        -bgweight <b>         Add b * background frequency to each count in query
        -motif_pvthresh <pv>  p-value threshold for motif hits
        -max_gap  <gap>       Maximum allowed distance between adjacent hits
        -output_evthresh <ev> Print matches with E-values less than E
        -help                 brief help message

=cut

use strict;
use warnings;

use Fcntl qw(SEEK_SET);
use File::Basename qw(fileparse);
use File::Spec::Functions qw(catfile tmpdir);
use File::Temp qw(tempfile);
use Getopt::Long;
use Pod::Usage;

use lib qw(@PERLLIBDIR@);

use ExecUtils qw(stringify_args invoke);
use MemeWebUtils qw(is_safe_name add_status_msg update_status loggable_date write_invocation_log);
use Globals;

# constants
my $tmpdir = '@TMP_DIR@';
# use the perl default if none is supplied or the replace fails
$tmpdir = &tmpdir() if ($tmpdir eq '' || $tmpdir =~ m/^\@TMP[_]DIR\@$/);
my $dbdir = '@MEMEDIR@/db/fasta_databases';

# variables for the service invocation log
my $log_args = stringify_args(@ARGV);
my $log_date = loggable_date();
my $log_file = 'mcast-log';

# error files
my $messages = "messages.txt";

#status page
my $file_list;
my @arg_errors = ();
my $msg_list = [];
my $program = 'MCAST';
my $page = 'index.html';
my $refresh = 10;

# required parameters
my $motifs;
my $dbseqs;
# option defaults
my $upseqs = undef;
my $bgweight = undef;
my $motif_pvthresh = undef;
my $max_gap = undef;
my $output_evthresh = undef;
my $help = 0; #FALSE
# derived defaults
my $seqs;
my $bfile = undef;

add_status_msg('Parsing arguments', $msg_list);

# redirect stderr so we can get the error message from GetOpts
my ($err_old, $err_tmp, $opts_ok, $opts_msg);
open($err_old, ">&STDERR") or die("Can't dup STDERR: $!");
$err_tmp = tempfile('GetOptions_XXXXXXXXXX', DIR => $tmpdir, UNLINK => 1); # make a temporary file
open(STDERR, '>&', $err_tmp) or die("Can't redirect STDERR: $!");
# parse options
$opts_ok = GetOptions(
  'upseqs=s' => \$upseqs,
  'bgweight=f' => \$bgweight,
  'motif_pvthresh=f' => \$motif_pvthresh,
  'max_gap=i' => \$max_gap,
  'output_evthresh=f' => \$output_evthresh,
  'help|?' => \$help
);
($motifs, $dbseqs) = @ARGV;
# display help
pod2usage(1) if $help;
# reset stderr and get the error message if any
open(STDERR, ">&", $err_old) or die("Can't reset STDERR: $!");
seek($err_tmp, 0, SEEK_SET);
while ($opts_msg = <$err_tmp>) {
  chomp($opts_msg);
  push(@arg_errors, $opts_msg);
}
close($err_tmp);

# remove any path from the files to ensure they are in this directory
unless (defined($motifs)) {
  push(@arg_errors, "No motifs provided.");
} else {
  $motifs = fileparse($motifs);
  if (not is_safe_name($motifs)) {
    push(@arg_errors, "Motifs \"$motifs\" does not fit allowed file name pattern.");
  } elsif (not -e $motifs) {
    push(@arg_errors, "Motifs \"$motifs\" does not exist.");
  } 
}
if (defined($upseqs)) {
  $upseqs = fileparse($upseqs);
  if (not is_safe_name($upseqs)) {
    push(@arg_errors, "Value \"$upseqs\" invalid for option upseqs (does not fit allowed file name pattern)");
  } elsif (not -e $upseqs) {
    push(@arg_errors, "Value \"$upseqs\" invalid for option upseqs (file does not exist)");
  } else {
    $seqs = $upseqs;
  }
} elsif (defined($dbseqs)) {
  $dbseqs = fileparse($dbseqs);
  if (-e catfile($dbdir, $dbseqs)) {
    $seqs = catfile('db', $dbseqs);
    # get the background file if it exists
    $bfile = $seqs . '.bfile' if (-e (catfile($dbdir, $dbseqs) . '.bfile'));
  } else {
    push(@arg_errors, "Database \"$dbseqs\" does not exist in the database directory \"$dbdir\"");
  }
} else {
  push(@arg_errors, "No sequences provided.");
}
if (defined($bgweight)) {
  if ($bgweight < 0) {
    push(@arg_errors, "Value \"$bgweight\" invalid for option bgweight (must be >= 0)");
  }
}
if (defined($motif_pvthresh)) {
  if ($motif_pvthresh <= 0 || $motif_pvthresh > 1) {
    push(@arg_errors, "Value \"$motif_pvthresh\" invalid for option motif_pvthresh (not a valid p-value)");
  }
}
if (defined($max_gap)) {
  if ($max_gap < 0) {
    push(@arg_errors, "Value \"$max_gap\" invalid for option max_gap (must be >= 0)");
  }
}
if (defined($output_evthresh)) {
  if ($output_evthresh <= 0) {
    push(@arg_errors, "Value \"$output_evthresh\" invalid for option output_evthresh (not a valid e-value)");
  }
}

$opts_ok = 0 if (scalar(@arg_errors) > 0);
foreach my $arg_error (@arg_errors) {
  print STDERR $arg_error, "\n";
  add_status_msg($arg_error, $msg_list);
}

# setup status page
$file_list = [
    {file => 'mcast.html', desc => 'MCAST HTML output'},
    {file => 'mcast.txt', desc => 'MCAST text output'},
    {file => $motifs, desc => 'Input Motifs'},
    {file => $upseqs, desc => 'Uploaded Database'}
];

if ($opts_ok) {
  add_status_msg('Arguments ok', $msg_list);
} else {
  add_status_msg("Error parsing arguments", $msg_list);
}

update_status($page, $program, ($opts_ok ? $refresh : 0), $file_list, 
    $msg_list, ($opts_ok ? "Starting" : "Error"));

# exit if there was an error reading the arguments
unless ($opts_ok) {
  write_invocation_log($log_file, $log_date, $log_args);
  pod2usage(2);
}

# Run MCAST
my @mcast_args = ('--oc', '.', '--verbosity', 1);
push(@mcast_args, '--bgfile', $bfile) if (defined($bfile));
push(@mcast_args, '--bgweight', $bgweight) if (defined($bgweight));
push(@mcast_args, '--output-ethresh', $output_evthresh) if (defined($output_evthresh));
push(@mcast_args, '--max-gap', $max_gap) if (defined($max_gap));
push(@mcast_args, '--motif-pthresh', $motif_pvthresh) if (defined($motif_pvthresh));
push(@mcast_args, $motifs, $seqs);
add_status_msg('Starting mcast<br><code>' . stringify_args('mcast', @mcast_args) . '</code>', $msg_list);

update_status($page, $program, $refresh, $file_list, $msg_list, "Starting");

my ($time, $status_code);
# create the symlink
symlink($dbdir, 'db') unless $upseqs;
# run the program
$status_code = invoke(
  PROG => 'mcast', 
  ARGS => \@mcast_args, 
  BIN => '@BINDIR@', 
  ALL_FILE => $messages, 
  TIME => \$time);
# remove the simlink
unlink('db') unless $upseqs;

my $status_msg;
if ($status_code != 0) {
  if ($status_code == -1) {
    $status_msg = "mcast failed to run";
  } elsif ($status_code & 127) {
    $status_msg = "mcast process died with signal " . 
        ($status_code & 127) . ", " . 
        (($status_code & 128) ? 'with' : 'without') . " coredump";
  } else {
    $status_msg = "mcast exited with error code " . ($status_code >> 8);
  }
  print STDERR $status_msg;
  push(@{$file_list}, {file => $messages, desc => 'Error Messages'});
} else {
  $status_msg = 'mcast ran successfully in ' . 
      (int($time * 100 + 0.5) / 100) . ' seconds';
  push(@{$file_list}, {file => $messages, desc => 'Warning Messages'});
}
add_status_msg($status_msg, $msg_list);

update_status($page, $program, 0, $file_list, $msg_list, 
    ($status_code ? "Error" : "Done"));


write_invocation_log($log_file, $log_date, $log_args);
exit(1) if $status_code;
1;

