#!/usr/bin/env perl

use 5.012;
use warnings;
use Pod::Usage;
use Getopt::Long;
use FindBin qw($RealBin);
use File::Basename;
use File::Spec;
use File::Spec::Functions;
use File::Copy;
use Term::ANSIColor qw(color);
my  $opt_verbose = undef;
  # 1. Working Directory
  # 2. Input Tab
  # 3. Method (0=No random subsampling; 1=Random subsampling;
  # 4. Level  (0=Minimum subsampling; 1=Fixed Value)
  # 5. Cutoff (if level=1 -> normalization level)
  # 6. Label  (# samples to label in plot)\n")
my $opt_labels = 5;
my $opt_cutoff = 1000;
my (
	$opt_outdir, $opt_input_table, $opt_fixed, $opt_random_subsampling,
	$opt_version, $opt_help, $opt_force, $opt_json
);
my @output_files = (
	'OTUs_Table-norm-rel-tax.tab',
	'OTUs_Table-norm-rel.tab',
	'OTUs_Table-norm-tax.tab',
	'OTUs_Table-norm.tab'
);

my $rhea_normalize_script = File::Spec->catfile($RealBin, 'rhea-normalize.R');

GetOptions(
	'o|output-directory=s'      => \$opt_outdir,
	'i|input-table=s'           => \$opt_input_table,
  'r|random-subsampling'      => \$opt_random_subsampling,
  'f|fixed-value'             => \$opt_fixed,
  'c|cutoff=i'                => \$opt_cutoff,
  'n|n-labels=i'              => \$opt_labels,
	'verbose'                   => \$opt_verbose,
	'version'                   => \$opt_version,
	'force'                     => \$opt_force,
	'help'                      => \$opt_help,
);

say STDERR color('magenta bold'), " DADAIST2 Normalize with Rhea", color('reset');

$opt_version && version();
$opt_help    && pod2usage({-exitval => 0, -verbose => 2});


# Check parameters
die "ERROR: Missing script $rhea_normalize_script.\n" unless (-e $rhea_normalize_script);
die "Missing parameters. Use --help for full manual.\n" if (!$opt_input_table or !$opt_outdir);
die "ERROR: Input file not found: $opt_input_table\n" if  (not -e "$opt_input_table" or -d "$opt_input_table");
if (! -d $opt_outdir) {
  mkdir "$opt_outdir" || die " FATAL ERROR: \n Unable to create $opt_outdir.\n";
}
die "ERROR: --n-labels INT (got $opt_labels)\n" if ($opt_labels !~/^\d+$/);
die "ERROR: --cutoff INT (got $opt_cutoff)\n" if ($opt_cutoff !~/^\d+$/);

# Set defaults, convert paths
$opt_input_table = File::Spec->rel2abs($opt_input_table);
$opt_outdir      = File::Spec->rel2abs($opt_outdir);
$opt_outdir .= '/' if ($opt_outdir !~/\/$/);
my $param_method = $opt_random_subsampling ? 1 : 0;
my $param_level  = $opt_fixed ? 1 : 0;

my $cmd = qq(Rscript --vanilla $rhea_normalize_script "$opt_outdir" "$opt_input_table" $param_method $param_level $opt_cutoff $opt_labels );

if ($opt_verbose) {
  say STDERR "$cmd";
}
my $captured = `$cmd 2>&1`;

if ($?) {
  die " ERROR: Execution of Rhea script failed.\n" .
				$captured . "\n";
} else {
	if ($opt_verbose) {
		say STDERR "Log: $captured\n";
	}
  for my $file (@output_files) {
    if (-e File::Spec->catfile($opt_outdir, $file) ) {
      say STDERR " * Generated: $file";
    }
  }
}

__END__

=head1 NAME

B<dadaist2-normalize> - Normalize OTU table using the B<Rhea> protocol.
The Rhea protocol (L<https://lagkouvardos.github.io/Rhea/>) is a complete
set of scripts to analyse microbiome files.

This wrapper is part of the I<AutoRhea> script bundled with I<Dadaist2>.
If used, please, cite the Rhea paper (see below).

=head1 AUTHORS

Andrea Telatin and Rebecca Ansorge

=head1 USAGE

  dadaist2-normalize [options] -i TABLE -o OUTDIR

=over 4

=item I<-i>, I<--input-table> FILE

Input file in in PhyloSeq object (R Object)

=item I<-o>, I<--output-outdir> DIR

Output directory

=item I<-r>, I<--random-subsampling>

Use random subsampling (default: off)

=item I<-f>, I<fixed-value>

Normalized using a fixed value (default: minimum)

=item I<-c>, I<--cutoff> INT

Normalization cutoff (if I<--fixed-value> is used)

=item I<-n>, I<--n-labels> INT

Highlight the INT  most undersampled samples

=back

=head1 CITATION

If you use B<Rhea> in your work please cite/attribute the original publication:

  Lagkouvardos I, Fischer S, Kumar N, Clavel T. (2017)
  Rhea: a transparent and modular R pipeline for microbial profiling based on 16S rRNA gene amplicons.
  PeerJ 5:e2836 https://doi.org/10.7717/peerj.2836

=head1 SOURCE CODE AND DOCUMENTATION

This wrapper is part of B<Dadaist2> freely available at
L<https://quadram-institute-bioscience.github.io/dadaist2>
released under the MIT licence. The website contains further DOCUMENTATION.
