#!/usr/bin/env perl
#ABSTRACT: Check phyloseq object

use 5.012;
use warnings;
use Pod::Usage;
my $VERSION = "1.0.5";

use Getopt::Long;
use Data::Dumper;
use FindBin qw($RealBin);
use File::Basename;
use File::Spec;
use File::Spec::Functions;
use File::Copy;
use Term::ANSIColor qw(color);
use FASTX::Reader;
use FASTX::ScriptHelper;
use JSON::PP;
use File::Temp;
my  $opt_verbose = undef;
my (
	$opt_inputdir, $opt_outfile,
	$opt_verbose, $opt_version, $opt_help, $opt_force, $opt_json
); 

my %required_files = (
	'table.csv' => 'feature table',
	'taxonomy.csv' => 'taxonomy table',
	'metadata.csv' => 'metadata file',
);
my %desired_files = (
	'rep-seqs.tree' => 'tree of representative sequences',
	'table.csv' => 'feature table',
	'taxonomy.csv' => 'taxonomy table',
	'metadata.csv' => 'metadata file',
);

my $rScript = File::Spec->catfile($RealBin, 'D2-importPhyloseq.R');
 
GetOptions(
	'i|input=s'      => \$opt_inputdir,
	'o|output=s'     => \$opt_outfile,
	'verbose'        => \$opt_verbose,
	'version'        => \$opt_version,
	'force'          => \$opt_force,
	'help'           => \$opt_help,
);

say STDERR color('magenta bold'), " DADAIST2 Import to PhyloSeq", color('reset');

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


my $S = FASTX::ScriptHelper->new({
	verbose => $opt_verbose,
	logfile => File::Temp->new(TEMPLATE => "dadaist2-phyloSeq-XXXXXXX")
}); 

die "Missing parameters. Use --help for full manual.\n" if (!$opt_inputdir); 

my $MicrobiomeAnalystDir = File::Spec->catdir($opt_inputdir, "MicrobiomeAnalyst");
die "ERROR: Input directory not found: $opt_inputdir\n" unless (-d $opt_inputdir);
die "ERROR: Input directory not valid (MicrobiomeAnalyst missing): $opt_inputdir/MicrobiomeAnalyst\n" 
 unless (-d $MicrobiomeAnalystDir);


for my $file (sort keys %desired_files) {
	if (! -e File::Spec->catfile($MicrobiomeAnalystDir,$file) ) {
		if (defined $required_files{$file}) {
			die "Missing required file <$file> in $opt_inputdir/MicrobiomeAnalyst (", $required_files{$file}, ")\n";
		} else {
			say STDERR " WARNING: Missing file <$file> in $opt_inputdir/MicrobiomeAnalyst\n";
		}
	}
}
die "ERROR: Missing script $rScript.\n" unless (-e $rScript);

  
my $output = $S->run(qq(Rscript --vanilla --no-save $rScript "$opt_inputdir" 2>&1), {candie => 1});

if ( $output->{exit} == 0 ) {
	if ($opt_verbose) {
		say $output->{stdout};
	}
	my $PSobject = File::Spec->catfile($opt_inputdir, 'phyloseq.rds');
	if (! -e "$PSobject") {
		die " ERROR: Phyloseq object not found at: ", $PSobject, "\n";
	}
	my %parsed_log = ();
	if (defined $opt_outfile) {
		if (! -d dirname($opt_outfile)) {
			mkdir dirname($opt_outfile) || die " ERROR: Unable to make dir: ",dirname($opt_outfile), "\n";
		}
		say STDERR " * saving to: ", $opt_outfile;
		move(
			$PSobject,
			$opt_outfile
		) || die "ERROR: Unable to move ", $PSobject, " to ", $opt_outfile, "\n";
	}
 
} else {
	say STDERR $output->{stdout};
	say STDERR $output->{stderr};
	die "PhyloSeq creation failed.";
}
__END__

=head1 NAME

B<dadaist2-phyloseqMake> - Generate PhyloSeq object from the command line

=head1 AUTHOR

Andrea Telatin and Rebecca Ansorge

=head1 PARAMETERS

=over 4

=item I<-i>, I<--input> DIR

Directory containing the C<MicrobiomeAnalyst> folder generated by Dadaist2.

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

Output filename. If omitted, a 'phyloseq.rds' file will be placed in the input directory.

=item I<--verbose>

Print verbose output

=back

=head1 SOURCE CODE AND DOCUMENTATION

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