#!/usr/bin/env perl
############################################################
#
# $Id: supported-motif-databases,v 1.00 2015/04/18 jvanheld Exp $
#
############################################################

## use strict;

=pod

=head1 NAME

supported-motif-databases

=head1 VERSION

$program_version

=head1 DESCRIPTION

Return the list of motif databases supported on this instance of RSAT.

=head1 AUTHORS

Jacques.van-Helden\@univ-amu.fr

=head1 CATEGORY

=over

=item util

=back

=head1 USAGE

supported-motif-databases

=head1 OUTPUT FORMAT

A tab-delimited file with one row per supported database and one
column per attribute.

=head1 SEE ALSO

=head1 WISH LIST

=over

=item B<wish 1>

=item B<wish 2>

=back

=cut


BEGIN {
  if ($0 =~ /([^(\/)]+)$/) {
    push (@INC, "$`lib/");
  }
}
require "RSA.lib";



################################################################
## Main package
package main;
{

  ################################################################
  ## Initialise parameters
  our $start_time = &RSAT::util::StartScript();
  our $program_version = do { my @r = (q$Revision: 1.00 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
  #    $program_version = "0.00";

  our %infile = ();
  our %outfile = ();

  our $verbose = 0;
  our $in = STDIN;
  our $out = STDOUT;

  our @supported_out_fields = qw(name format file descr version url label);
  our %supported_out_field = ();
  foreach my $field (@supported_out_fields) {
    $supported_out_field{$field} = 1;
  }
  our $supported_out_fields = join ",", @supported_out_fields;

  ################################################################
  ## Read argument values
  &ReadArguments();
  if (scalar(@selected_out_fields) == 0) {
    @selected_out_fields = @supported_out_fields;
  }

  ################################################################
  ## Open output stream
  $out = &OpenOutputFile($outfile{output});

  ################################################################
  ## Print verbose
  &Verbose() if ($main::verbose >= 1);

  ################################################################
  ## Get info about supported motif databases
  my %matrix_db = &RSAT::server::supported_motif_databases();

  ################################################################
  ## Insert here output printing
  print $out "#", join("\t", @selected_out_fields), "\n";
  foreach my $db (sort keys %matrix_db) {
    my @out_fields = ();
    foreach my $field (@selected_out_fields) {
      push @out_fields, $matrix_db{$db}->{$field};
    }
    print $out join("\t", @out_fields), "\n";
  }

  ################################################################
  ## Report execution time and close output stream
  &close_and_quit();
}

################################################################
################### SUBROUTINE DEFINITION ######################
################################################################


################################################################
## Close output file and quit
sub close_and_quit {

  ## Report execution time
  my $exec_time = &RSAT::util::ReportExecutionTime($start_time); ## This has to be exectuted by all scripts
  print $main::out $exec_time if ($main::verbose >= 1); ## only report exec time if verbosity is specified

  ## Close output file
  if ($outfile{output}) {
    close $main::out;
    &RSAT::message::TimeWarn("Output file", $outfile{output}) if ($main::verbose >= 2);
  }

  ## CLOSE OTHER FILES HERE IF REQUIRED

  exit(0);
}


################################################################
## Display full help message 
sub PrintHelp {
  system "pod2text -c $0";
  exit()
}

################################################################
## Display short help message
sub PrintOptions {
  &PrintHelp();
}

################################################################
## Read arguments 
sub ReadArguments {
  my $arg;
  my @arguments = @ARGV; ## create a copy to shift, because we need ARGV to report command line in &Verbose()
  while (scalar(@arguments) >= 1) {
    $arg = shift (@arguments);


=pod

=head1 OPTIONS

=over 4

=item B<-v #>

Level of verbosity (detail in the warning messages during execution)

=cut
    if ($arg eq "-v") {
      if (&IsNatural($arguments[0])) {
	$main::verbose = shift(@arguments);
      } else {
	$main::verbose = 1;
      }


=pod

=item B<-h>

Display full help message

=cut
    } elsif ($arg eq "-h") {
      &PrintHelp();


=pod

=item B<-help>

Same as -h

=cut
    } elsif ($arg eq "-help") {
      &PrintOptions();


=pod

=item B<-i inputfile>

If no input file is specified, the standard input is used.  This
allows to use the command within a pipe.

=cut
    } elsif ($arg eq "-i") {
      $main::infile{input} = shift(@arguments);


=pod

=item	B<-o outputfile>

If no output file is specified, the standard output is used.  This
allows to use the command within a pipe.

=cut
    } elsif ($arg eq "-o") {
      $outfile{output} = shift(@arguments);


=pod

=item	B<-return field>

Field to return. Several fields can specified, separated by commas.
The option can also be used iteratively to specify multiple fields.

Supported output fields: name, format, file, descr, version, url.

Examples: 

supported-motif-databases -return name,version

=cut
    } elsif ($arg eq "-return") {
      my @fields = split(",", shift(@arguments));
      foreach my $field (@fields) {
	$field = lc($field);
	&RSAT::error::FatalError($field, "Invalid output field. Supported: ".$supported_out_fields) unless ($supported_out_field{$field});
	push @selected_out_fields, $field;
      }

    } else {
      &FatalError(join("\t", "Invalid option", $arg));

    }
  }

=pod

=back

=cut

}

################################################################
## Verbose message
sub Verbose {
  print $out "; supported-motif-databases ";
  &PrintArguments($out);
  printf $out "; %-22s\t%s\n", "Program version", $program_version;
  if (%main::infile) {
    print $out "; Input files\n";
    while (my ($key,$value) = each %main::infile) {
      printf $out ";\t%-13s\t%s\n", $key, $value;
    }
  }
  if (%main::outfile) {
    print $out "; Output files\n";
    while (my ($key,$value) = each %main::outfile) {
      printf $out ";\t%-13s\t%s\n", $key, $value;
    }
  }
}


__END__
