#!/usr/bin/perl -w

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

use strict;
use File::Basename;
use Getopt::Long;
use Config::IniFiles;

my $R1='';
my $R2='';
my $minoverlap='';
my $maxoverlap='';
my $maxmismatchdensity='';
my $phredoffset='';
my $readlen='';
my $fragmentlen='';
my $fragmentlenstddev='';
my $outextendedFragsfastq='';
my $outnotCombined_1fastq='';
my $outnotCombined_2fastq='';
my $outhist='';
my $outhistogram='';
my $cfg = Config::IniFiles->new( -file => "/path/to/PATH.ini" );


Getopt::Long::Configure( 'no_ignorecase', 'bundling' );
GetOptions (
    'R1=s' => \ $R1,
    'R2=s' => \ $R2,
    'minoverlap=i' => \ $minoverlap,
    'maxoverlap=i' => \ $maxoverlap,
    'maxmismatchdensity=s' => \ $maxmismatchdensity,
    'phredoffset=i' => \ $phredoffset,
    'readlen=i' => \ $readlen,
    'fragmentlen=i' => \ $fragmentlen,
    'fragmentlenstddev=i' => \$fragmentlenstddev,
    'out1=s' => \ $outextendedFragsfastq,
    'out2=s' => \ $outnotCombined_1fastq,
    'out3=s' => \ $outnotCombined_2fastq,
    'out4=s' => \ $outhist,
    'out5=s' => \ $outhistogram
) or die "Usage: Error in command line arguments\n";

my $FLASH = $cfg->val( 'toolsPath', 'FLASH_PATH' );
my $GALAXYDATA= $cfg->val( 'workPath', 'FILEPATH_DEV' );
my $cmd = '';
my ($nb) = ($outextendedFragsfastq=~/galaxy_dataset_(\d+)\.\S+$/);

#Repertoire de sortie cree par le script, verif des droits d'ecriture sur ce repertoire de sortie
`cd $GALAXYDATA; mkdir $nb/; chmod 777 $nb/; cd $nb/;`;
my $dirresults= "$GALAXYDATA".$nb;

#avec modification de l extension des inputs
`ln -s $R1 $dirresults/R1.fastq;`;
`ln -s $R2 $dirresults/R2.fastq;`;


#gestion des options
my $minoverlapOPTION='';
if ($minoverlap eq ""){$minoverlapOPTION="--min-overlap 10";}
else {$minoverlapOPTION="--min-overlap $minoverlap";}

my $maxoverlapOPTION='';
if ($maxoverlap eq ""){$maxoverlapOPTION="--max-overlap 70";}
else {$maxoverlapOPTION = "--max-overlap $maxoverlap";}


my $maxmismatchdensityOPTION='';
if ($maxmismatchdensity eq ""){$maxmismatchdensityOPTION='0.25';}
else {$maxmismatchdensityOPTION= $maxmismatchdensity ;}

my $phredoffsetOPTION='';
if ($phredoffset eq ""){$phredoffsetOPTION="--phred-offset 33";}
else {$phredoffsetOPTION="--phred-offset $phredoffset";}

my $readlenOPTION='';
if ($readlen eq ""){$readlenOPTION="--read-len 100";}
else {$readlenOPTION="--read-len $readlen";}

my $fragmentlenOPTION='';
if ($fragmentlen eq ""){$fragmentlenOPTION="--fragment-len 180";}
else {$fragmentlenOPTION="--fragment-len $fragmentlen";}

my $fragmentlenstddevOPTION='';
if ($fragmentlenstddev eq ""){$fragmentlenstddevOPTION="--fragment-len-stddev 10";}
else {$fragmentlenstddevOPTION="--fragment-len-stddev $fragmentlenstddev";}

                        

#Usage: flash [OPTIONS] MATES_1.FASTQ MATES_2.FASTQ
$cmd = "(cd $dirresults; $FLASH $minoverlapOPTION $maxoverlapOPTION  --max-mismatch-density $maxmismatchdensityOPTION $phredoffsetOPTION $readlenOPTION $fragmentlenOPTION $fragmentlenstddevOPTION -d $dirresults  $dirresults/R1.fastq $dirresults/R2.fastq) >& ./flash.log 2>&1";
system $cmd;  

#Info pour les biologistes
print STDOUT "Your command line is : \n\n $cmd \n\n ";

#flash r1.fastq r2.fastq


#Recuperation des fichiers par Galaxy
#-rw-r--r--   1 galaxy-dev wbioinfo    0  4 nov.  14:40 out.extendedFrags.fastq
#-rw-r--r--   1 galaxy-dev wbioinfo  266  4 nov.  14:40 out.notCombined_1.fastq
#-rw-r--r--   1 galaxy-dev wbioinfo  266  4 nov.  14:40 out.notCombined_2.fastq
#-rw-r--r--   1 galaxy-dev wbioinfo    0  4 nov.  14:40 out.hist
#-rw-r--r--   1 galaxy-dev wbioinfo    0  4 nov.  14:40 out.histogram

if (! -e "$dirresults/out.extendedFrags.fastq"){print STDERR "out.extendedFrags.fastq not found. \n";}else{`cp -a $dirresults/out.extendedFrags.fastq $outextendedFragsfastq`;}
if (! -e "$dirresults/out.notCombined_1.fastq"){print STDERR "out.notCombined_1.fastq not found. \n";}else{`cp -a $dirresults/out.notCombined_1.fastq $outnotCombined_1fastq`;}
if (! -e "$dirresults/out.notCombined_2.fastq"){print STDERR "out.notCombined_2.fastq not found. \n";}else{`cp -a $dirresults/out.notCombined_2.fastq $outnotCombined_2fastq`;}
if (! -e "$dirresults/out.hist"){print STDERR "out.hist not found. \n";}else{`cp -a $dirresults/out.hist $outhist`;}
if (! -e "$dirresults/out.histogram"){print STDERR "out.histogram not found. \n";}else{`cp -a $dirresults/out.histogram $outhistogram`;}
