#!/usr/bin/perl -w

# usage : perl mi_RNA_qual.pl <FASTQ file> 
#sarah maman - 31 juillet 2012 - Mis en place pour le traitement des mi-RNA
# Copyright (C) 2012 INRA
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

#~ perl sm_miRNA_qual_test.pl --input test_4000.fastq --outfq  test --outlog test_log --primer "file" --primerfile test_primers.fasta

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

# SETTING GLOBAL VARIABLES ################################################################################
my $cfg = Config::IniFiles->new( -file => "/usr/local/bioinfo/src/galaxy-dev/galaxy-dist/PATH.ini" );
my $CUTADAPT = $cfg->val( 'toolsPath', 'CUTADAPT' );
my $file_path = $cfg->val( 'workPath', 'FILEPATH_DEV' );
###########################################################################################################

my $input;
my $output_fq;
my $output_log;
my $primer;
my $adaptateur; 
my $taillemini;
my $taillemaxi;
my $primerfile;
my $cmd = '';
my $search_region;
my $primer_occurence;    
    
Getopt::Long::Configure( 'no_ignorecase', 'bundling' );
GetOptions (
	'input=s' => \$input,
	'outfq=s' => \$output_fq,
	'outlog=s' => \$output_log,    
	'primer=s' => \$primer, 
	'search_region=s' => \$search_region,
	'primer_occurence=i' => \$primer_occurence,
	'primerfile=s' => \$primerfile,
	'adaptateur=s' => \$adaptateur,
	'taillemini=i' => \$taillemini,
	'taillemaxi=i' => \$taillemaxi
) or die "Usage: Error in command line arguments\n";


# UNIQ WORKING DIR
my $date=`date +%Y-%m-%d-%H%M%S`;
chomp($date);

`cd $file_path; mkdir $date/; chmod 777 $date/; cd $date/;`;
my $dirresults= $file_path.$date."/";

if ($primer eq "text"){
		print STDOUT "\ncommande:\n$CUTADAPT -a $adaptateur -f fastq -m $taillemini -M $taillemaxi -o ${dirresults}seq.cut.fq $input > ${dirresults}/run.cut.log\n\n";	
        $cmd = "=$adaptateur -f fastq -m $taillemini -M $taillemaxi -o ${dirresults}seq.cut.fq $input > ${dirresults}/run.cut.log";        
}
if ($primer eq "file"){
		print STDOUT "\ncommande:\n$CUTADAPT --anywhere=file:$primerfile -f fastq -m $taillemini -M $taillemaxi -o ${dirresults}seq.cut.fq $input\n\n";				
        $cmd = "=file:$primerfile -f fastq -m $taillemini -M $taillemaxi -o ${dirresults}seq.cut.fq $input > ${dirresults}/run.cut.log";
}

# Add the search region option
if($search_region eq "five_prime"){
	$cmd= "$CUTADAPT --times $primer_occurence --front".$cmd;
}
elsif($search_region eq "three_prime"){
	$cmd= "$CUTADAPT --times $primer_occurence --adapter".$cmd;
}
elsif($search_region eq "anywhere"){
	$cmd= "$CUTADAPT --times $primer_occurence --anywhere".$cmd;
}
system $cmd;

# generate the datasets in galaxy interface
if (! -e "${dirresults}seq.cut.fq"){
	print STDERR "Echec lors de la generation du cutadapt fq lors suppression des adaptateurs\n";
}
else{
	`(cp -a \"${dirresults}seq.cut.fq\" $output_fq) >& ./cutadapt1.log 2>&1`;
}
print STDOUT "${dirresults}/run.cut.log\n";

if (! -e "${dirresults}/run.cut.log"){
	print STDERR "Echec a la generation du fichier log de cutadapt\n";
}
else{
	`(cp -a \"${dirresults}/run.cut.log\" $output_log) >& ./cutadapt2.log 2>&1`;
}
