#!/usr/bin/perl -w

# usage : sm_clones_meta1.pl
# sarah maman - 10 dec 2013 - Mis en place pour le traitement des clones metagenomiques

use strict;
use warnings;
use File::Basename;

#Inputs files
my $input_clean_fasta                 = $ARGV[0];
my $input_ref_fasta                   = $ARGV[1];
my $input_contig_res                  = $ARGV[2];

#Galaxy outputs files
my $output_C_AV_fasta                 = $ARGV[3];
my $output_projet_lib_C_VM_fasta      = $ARGV[4];
my $output_projet_lib_C_VN_fasta      = $ARGV[5];
my $output_projet_lib_contiglist_fasta= $ARGV[6];

#Parametres
my $nbContig                          = $ARGV[7];
my $projet                            = $ARGV[8];
my $lib                               = $ARGV[9];
my $poub                              = $ARGV[10];
my $minmatch                          = $ARGV[11];  
my $minscore                          = $ARGV[12]; 

my $cmd;
my $cmd2;
my $cmd3;
my $cmd4 = '';
my $cmd5 = '';
my $cmd6 = '';
my $cmd7 = '';
my ($nb) = ($output_C_AV_fasta=~/galaxy_dataset_(\d+)\.\S+$/);

#Creation d un repertoire de travail
#my $path= dirname($input_clean_fasta);
my $path= "/work/galaxy/database/files/workspace";
#Verification des droits d'ecriture sur ce repertoire de sortie
`cd $path; mkdir $nb/; chmod 777 $nb/`;
my $dirresults= $path."/".$nb;
print STDOUT "Votre repertoire de travail : $dirresults \n\n";

#Copier les fichiers entrants et l'ensemble des scripts du repertoire scripts/* dans le repertoire de travail
`cp -a $input_clean_fasta $dirresults/clean.fasta; cp -a $input_ref_fasta $dirresults/ref.fasta; cp -a $input_contig_res $dirresults/contig.res; cp /usr/local/bioinfo/src/galaxy/galaxy-dist/tools/sm_clones/scripts/* $dirresults/.;`; 

#Stand by avant de passer a l'étape suivante

#Lancer la premiere etape de traitement
$cmd = "(cd $dirresults/; cross_match clean.fasta ref.fasta -minmatch $minmatch -minscore $minscore -screen > contigs.fasta) >& ./clone1.log 2>&1;";
system $cmd;

#Info pour les biologistes
print STDOUT "First step : \n\n $cmd \n\n ";

#En sortie :
#1- contigs.fasta
#2- clean.fasta.screen (Pyrocleaner/RL1/Formation.RL1.decontaminated.clean.fasta.cap.contigs.screen)
#3- clean.fasta.log   ( Pyrocleaner/RL1/Formation.RL1.decontaminated.clean.fasta.cap.contigs.log)


#****************************************
#Etape 2
#****************************************

#lancer la seconde etape de traitement
$cmd2 = "(cd $dirresults/; perl CreateAVFiles.pl contig.res clean.fasta $nbContig) >& ./clone2.log 2>&1;";
system $cmd2;

#Info pour les biologistes
print STDOUT "Second step : \n\n $cmd2 \n\n ";

#EN ENTREE :
#arg0 : Pyrocleaner/RL1/Formation.RL1.ContigLengthG1000ProfG8.res (1) 	-> contig.res
#arg1 : Pyrocleaner/RL1/Formation.RL1.decontaminated.clean.fasta.cap.contigs 	  -> clean.fasta
#arg 2 : Nombre de contig -> 7 (par exemple !)
#EN SORTIE :
#Formation.RL1.C14AV.fasta (2) **************OUTPUT GALAXY********** : C7AV.fasta (C - nb contigs- AV.fasta)
#Formation.RL1.decontaminated.clean.fasta.cap.contigs.oneline  -> clean.fasta.oneline

my $CAV = "$dirresults/C"."$nbContig"."AV.fasta"; 
#Recuperation output Galaxy
if (! -e $CAV ){print STDERR "Echec lors de la generation du fichier de sortie $CAV \n";} else {$cmd4 = "(cp -a $CAV $output_C_AV_fasta) >& ./cpclone2.log 2>&1"; system $cmd4;}

#****************************************
#Etape 3
#****************************************

#lancer la seconde etape de traitement
$cmd3 = "(cd $dirresults/; perl CreateVMAndVNFiles.pl contig.res clean.fasta.screen $nbContig $projet $lib $poub) >& ./clone3.log 2>&1;";
system $cmd3;

#Info pour les biologistes
print STDOUT "Third step : \n\n $cmd3 \n\n ";


#EN ENTREE :
#Pyrocleaner/RL1/Formation.RL1.ContigLengthG1000ProfG8.res (1) 	 -> contig.res
#Pyrocleaner/RL1/Formation.RL1.decontaminated.clean.fasta.cap.contigs.screen 	************** Sortie du 1er script ********** -> clean.fasta.screen
#EN SORTIE :
#Formation.RL1.decontaminated.clean.fasta.cap.contigs.screen.oneline
#Formation.RL1.C14VM.fasta **************OUTPUT GALAXY**********
#Formation.RL1.C14VM.fasta.temp
#Formation.RL1.C14VM.fasta.temp.modified
#Formation.RL1.C14VN.nettoyageVecteur.log
#Formation.RL1.C14VN.fasta (2) **************OUTPUT GALAXY**********
#Formation.RL1.contiglist.fasta **************OUTPUT GALAXY**********

#On retrouve les fichiers dans le repertoire de travail :
#-rw-r--r-- 1 smaman BIOINFO  56K 10 déc.  15:11 clean.fasta.screen.oneline
#-rw-r--r-- 1 smaman BIOINFO  437 10 déc.  15:11 projet.lib.C7VM.fasta **************OUTPUT GALAXY**********
#drwxr-xr-x 2 smaman BIOINFO 1,1K 10 déc.  15:11 .
#-rw-r--r-- 1 smaman BIOINFO  438 10 déc.  15:11 projet.lib.C7VN.fasta **************OUTPUT GALAXY**********
#-rw-r--r-- 1 smaman BIOINFO   48 10 déc.  15:11 projet.lib.contiglist.fasta **************OUTPUT GALAXY**********


#Recuperation des output Galaxy
my $CVM = "$dirresults/projet.lib.C"."$nbContig"."VM.fasta"; 
if (! -e $CVM ){print STDERR "Echec lors de la generation du fichier de sortie $CVM \n";} else {$cmd5 = "(cp -a $CVM $output_projet_lib_C_VM_fasta) >& ./cpclone3.log 2>&1"; system $cmd5;}

my $CVN = "$dirresults/projet.lib.C"."$nbContig"."VN.fasta"; 
if (! -e $CVN ){print STDERR "Echec lors de la generation du fichier de sortie $CVN \n";} else {$cmd6 = "(cp -a $CVN $output_projet_lib_C_VN_fasta) >& ./cpclone4.log 2>&1"; system $cmd6;}

my $ContigList ="$dirresults/projet.lib.contiglist.fasta";
if (! -e $ContigList ){print STDERR "Echec lors de la generation du fichier de sortie $ContigList \n";} else {$cmd7 = "(cp -a $ContigList $output_projet_lib_contiglist_fasta) >& ./cpclone5.log 2>&1"; system $cmd7;}
