# # (gH)   -_-  lefse.pl  ;  TimeStamp (unix) : 09 Novembre 2016 vers 15:14

#################################################################################
#
# this script automates the production of png with lefse
#
#################################################################################

print "\n Lefse Analysis (gH) version 1.05\n\n" ;

use strict ;
use warnings ;
use POSIX qw/ceil/;

# check if the parameter baseName is present

if ($#ARGV==-1) {
  print " MANDATORY PARAMETER MISSING." ;
  print " Syntax: perl lefse.pl INPUT_FILE [PATH for *py files]\n\n" ;
  print " Examples: perl lefse.pl hmp.txt\n" ;
  print "           perl lefse.pl hmp.txt ~/Scripts/\n\n" ;
  print " Help: perl lefse.pl -h or perl lefse.pl --help\n\n" ;
  exit(-1) ;
} ; # end if

# detailed help

if (($ARGV[0] eq "-h") or ($ARGV[0] eq "--help")) {
  print " The script lefse.pl automates the production of png for LEfSE analysis.\n" ;
  print " Just give the name of the file to be processed and the path to the Python scripts for Lefse analysis.\n\n" ;
  print " If these scripts files are already in the same directory, just give the name of the file to be processed.\n\n" ;
  exit(-2) ;
} ; # end if

# check if the input file is present

my $dataFile = $ARGV[0] ;

if (!-e $dataFile) {
  print " file $dataFile not found.STOP.\n\n" ;
  exit(-3) ;
} ; # end if

# get the baseName and build the output names

my $idx      = rindex($dataFile,".") ;
my $baseName = "" ;
if ($idx==-1) {
   $baseName = $dataFile ;
} else {
   $baseName = substr($dataFile,0,$idx) ;
} ; # fin si

my $inFile   = $baseName.".in"  ;
my $resFile  = $baseName.".res" ;
my $pngFile  = $baseName.".png" ;
my $cladFile = $baseName."_clad.png" ;

# remove silently old output files

system("rm -f  $inFile $resFile $pngFile $cladFile") ;

# define $path if parameter PATH is defined

my $path = "" ;
if ($ARGV[1]) {
   $path = $ARGV[1] ;
   if (substr($path,length($path)-1,1) ne "/") {
     $path .= "/" ;
   } ; # fin si
} ; # fin si

# check if python files are OK

my @progFiles= (
  "format_input.py",
  "run_lefse.py",
  "plot_res.py",
  "plot_cladogram.py"
) ;

my $err = 0 ;
foreach my $filename (sort @progFiles) {
  my $fn = $path.$filename ;
  if (!-e $fn) {
      print " required file $filename not found.\n" ;
      $err++ ;
  } ; # fin si
} ; # fin pour chaque

if ($err>0) {
  print "\n" ;
  print" STOP.\n\n" ;
  exit(-4) ;
} ; # fin si

# step 1. format input

print(" 1. format input...") ;
my $cmd1 = " python ".$path."format_input.py $dataFile $inFile  -u 1 -c 2 -o 1000000 " ;
system("$cmd1 > log1.txt") ;
print "   done\n" ;
if (!-e $inFile) {
  print " Something went wrong. Please consult log1.txt" ;
  exit(-5) ;
} ; # fin si

# step 2. lefse results

print(" 2. lefse results...") ;
my $cmd2 = " python ".$path."run_lefse.py $inFile  $resFile" ;
system("$cmd2 > log2.txt") ;
print "  done\n" ;
if (!-e $resFile) {
  print " Something went wrong. Please consult log2.txt" ;
  exit(-6) ;
} ; # fin si

# step 3. plot results

print(" 3. plot results...") ;
my $cmd3 = " python ".$path."plot_res.py $resFile  $pngFile ";
system("$cmd3 > log3.txt") ;
print "   done\n" ;
if (!-e $pngFile) {
  print " Something went wrong. Please consult log3.txt" ;
  exit(-7) ;
} ; # fin si

# step 4. plot cladogram

print(" 4. plot cladogram...") ;
my $cmd4 = " python ".$path."plot_cladogram.py --format png $resFile $cladFile " ;
system("$cmd4 > log4.txt") ;
print " done\n" ;
if (!-e $cladFile) {
  print " Something went wrong. Please consult log4.txt" ;
  exit(-8) ;
} ; # fin si

# at last, tell which produced files are available

print "\n" ;
print " Computed files:   $inFile  and $resFile \n";
print " Available images: $pngFile and $cladFile \n";
print "\n" ;
