#!@WHICHPERL@
=head1 NAME

centrimo_webservice - Run centrimo in a restricted mode and create an index webpage.

=head1 SYNOPSIS

centrimo_webservice [options] <sequences file> <motif databases>

  Options:
    -score <score>    minimum score counted as hit
    -ethresh <evalue> minimum E-value to report
    -maxwin <window>  maximum window size to test
    -bfile <file>     background file
    -upmotifs <file>  uploaded motifs
    -help             brief help message

  Motif Databases
    The motif databases may be specified as a pattern using * as a wildcard.

=cut

use strict;
use warnings;

use Cwd qw(getcwd abs_path);
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/motif_databases';
my $workdir = getcwd;

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

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

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

# required parameters
my @dbmotifs;
my $sequences;
# option defaults
my $upmotif;
my $bfile;
my $score;
my $e_thresh;
my $max_win;
my $norc = 0; #FALSE
my $help = 0; #FALSE
# derived defaults
my @motifs = ();

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(
  'upmotifs=s' => \$upmotif,
  'bfile=s' => \$bfile,
  'score=f' => \$score,
  'ethresh=f' => \$e_thresh,
  'maxwin=i' => \$max_win,
  'norc' => \$norc,
  'help|?' => \$help
);
($sequences, @dbmotifs) = @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($sequences)) {
  push(@arg_errors, "No sequences provided.");
} else {
  $sequences = fileparse($sequences);
  if (not is_safe_name($sequences)) {
    push(@arg_errors, "Sequences \"$sequences\" does not fit allowed file name pattern.");
  } elsif (not -e $sequences) {
    push(@arg_errors, "Sequences \"$sequences\" does not exist.");
  } 
}
if (defined($upmotif)) {
  $upmotif = fileparse($upmotif);
  if (not is_safe_name($upmotif)) {
    push(@arg_errors, "Value \"$upmotif\" invalid for option upmotif (does not fit allowed file name pattern)");
  } elsif (not -e $upmotif) {
    push(@arg_errors, "Value \"$upmotif\" invalid for option upmotif (file does not exist)");
  } else {
    push(@motifs, $upmotif);
  }
} 

if (@dbmotifs) {
  # The expansion must be evaluated in the database directory
  chdir($dbdir);
  my @expanded_motifs = glob(join(' ', @dbmotifs));
  chdir($workdir);
  # check that the expanded files really are in the db dir
  for (my $i = 0; $i < scalar(@expanded_motifs); $i++) {
    $expanded_motifs[$i] = fileparse($expanded_motifs[$i]);
    if (-e catfile($dbdir, $expanded_motifs[$i])) {
      push(@motifs, catfile('db',$expanded_motifs[$i]));
    }
  }
}

unless (@motifs) {
  push(@arg_errors, "No motifs provided.");
}
if (defined($bfile)) {
  $bfile = fileparse($bfile);
  if (not is_safe_name($bfile)) {
    push(@arg_errors, "Value \"$bfile\" invalid for option bfile (does not fit allowed file name pattern)");
  } elsif (not -e $bfile) {
    push(@arg_errors, "Value \"$bfile\" invalid for option bfile (file does not exist)");
  }
}
if (defined($e_thresh)) {
  if ($e_thresh <= 0) {
    push(@arg_errors, "Value \"$e_thresh\" invalid for option ethresh (not a valid e-value)");
  }
}
if (defined($max_win)) {
  if ($max_win < 0) {
    push(@arg_errors, "Value \"$max_win\" invalid for option maxwin (must be >= 0)");
  }
}

$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 => 'centrimo.html', desc => 'CentriMo HTML output'},
    {file => 'centrimo.txt', desc => 'CentriMo text output'},
    {file => $sequences, desc => 'Input Sequences'},
    {file => $upmotif, desc => 'Uploaded Motifs'}
];

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 CentriMo
my @centrimo_args = ('--oc', '.', '--verbosity', 1);
push(@centrimo_args, '--norc') if ($norc);
push(@centrimo_args, '--bgfile', $bfile) if (defined($bfile));
push(@centrimo_args, '--score', $score) if (defined($score));
push(@centrimo_args, '--ethresh', $e_thresh) if (defined($e_thresh));
push(@centrimo_args, '--maxwin', $max_win) if (defined($max_win));
push(@centrimo_args, '-dfile', 'description') if (-e 'description');
push(@centrimo_args, $sequences, @motifs);
add_status_msg('Starting centrimo<br><code>' . stringify_args('centrimo', @centrimo_args) . '</code>', $msg_list);

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

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

my $status_msg;
if ($status_code != 0) {
  if ($status_code == -1) {
    $status_msg = "centrimo failed to run";
  } elsif ($status_code & 127) {
    $status_msg = "centrimo process died with signal " . 
        ($status_code & 127) . ", " . 
        (($status_code & 128) ? 'with' : 'without') . " coredump";
  } else {
    $status_msg = "centrimo exited with error code " . ($status_code >> 8);
  }
  print STDERR $status_msg;
  push(@{$file_list}, {file => $messages, desc => 'Error Messages'});
} else {
  $status_msg = 'centrimo 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;


