#!/usr/bin/perl -w

# usage : perl sm_mothur_preprocess_1.pl <input FASTA> <input config txt>
# 02/08/2013 - Wrapper dans le cadre du projet 16S (pour Maria B.)

use strict;
use File::Basename;
    
my $input_fasta                  = $ARGV[0];
my $input_config                 = $ARGV[1];
my $NB_BARCODE                   = $ARGV[2];
my $NB_PRIMER                    = $ARGV[3];
my $BMM                          = $ARGV[4];
my $PMM                          = $ARGV[5];
my $MIN_LEN                      = $ARGV[6];
my $output_fasta                 = $ARGV[7];
my $output_all_trim_count_table  = $ARGV[8];
my $output_html                  = $ARGV[9];


#my $path= dirname($input_fasta);
my $path= "/galaxydata/database/files/workspace/";
my ($nb) = ($output_html=~/galaxy_dataset_(\d+)\.\S+$/);

#my $MOTHUR1 = '/save/galaxy-dev/scripts/sm_mothur/1_preprocess_454_mothur.1.2.sh';
my $MOTHUR1 = '/usr/local/bioinfo/src/galaxy-dev/galaxy-dist/tools/sm_mothur/bin/1_preprocess_454_mothur.1.2.sh';
my $cmd = '';
#Repertoire de sortie cree par le script, verif des droits d'ecriture sur ce repertoire de sortie
`cd $path; mkdir $nb/; chmod -R 777 $nb/`;
my $dirresults= $path."/".$nb;

#print STDOUT "nb : $nb \n path : $path  \n dirresults : $dirresults \n";

#modification de l extension des inputs
`cp $input_fasta $dirresults/input.fasta; cp $input_config $dirresults/config.txt`;

#TESTS commande : perl sm_mothur_preprocess_1.pl /usr/local/bioinfo/src/galaxy/galaxy-dist/tools/sm_mothur/tests2/batch1_16S_extract.fasta /usr/local/bioinfo/src/galaxy/galaxy-dist/tools/sm_mothur/tests2/454_Thailande_2013_16S_correct.txt 0 0 4 1 200 outgz outfasta outgroups outhtmlgalaxy_dataset_123 outnames
#mkdir out/; chmod 777 out/; cd out/; sh /home/sigenae/bin/Galaxy/r454_diversity_Galaxy/1_preprocess_mothur1.1.sh /usr/local/bioinfo/src/galaxy/galaxy-dist/tools/sm_mothur/tests2/batch1_16S_extract.fasta  /usr/local/bioinfo/src/galaxy/galaxy-dist/tools/sm_mothur/out/ /usr/local/bioinfo/src/galaxy/galaxy-dist/tools/sm_mothur/tests2/454_Thailande_2013_16S_correct.txt 0 0 4 1 200

#sh 1_preprocess_454_mothur1.2.sh path_INPUT_FASTA path_to_OUTDIR path_to_SAMPLE_CONF NB_BARCODE NB_PRIMER BMM PMM MIN_LEN
$cmd = "(cd $dirresults; sh $MOTHUR1 $dirresults/input.fasta $dirresults $dirresults/config.txt $NB_BARCODE $NB_PRIMER $BMM $PMM $MIN_LEN) >& ./mothur1.log 2>&1";
system $cmd;



#Info pour les biologistes
print STDOUT "Step 1 - Pre-process Mothur : \n\n $cmd \n\n ";


#Recuperation des fichiers par Galaxy
if (! -e "$dirresults/all.trim.unique.count_table"){print STDERR "all.trim.count_table file not found. \n";}else{`cp -a $dirresults/all.trim.unique.count_table $output_all_trim_count_table`;}
if (! -e "$dirresults/all.trim.unique.fasta"){print STDERR "all.trim.unique.fasta file not found. \n";}else{`cp -a $dirresults/all.trim.unique.fasta $output_fasta`;}
if (! -e "$dirresults/bilan.html"){print STDERR "bilan.html file not found. \n";}else{`cp -a $dirresults/bilan.html $output_html`;}
