#!/usr/bin/perl -w
# smaman
# usage : python /usr/local/bioinfo/Scripts/bin/not_documented/merge_cols.py -f tata.txt,tot.txt -n eche1,eche2 -o matrice.txt
use strict;
use File::Basename;


my $output_txt               = $ARGV[0];
my $input_1_merged           = $ARGV[1];
my $input_1_merged_name      = $ARGV[2];
my $c                        = $ARGV[3];
my $input_2_merged           = $ARGV[4];
my $input_2_merged_name      = $ARGV[5];
my $inputs_merged            = @ARGV[6..$#ARGV];
my $cmd = '';
my $MERGECOLS = "/usr/local/bioinfo/Scripts/bin/merge_cols.py";
#my $path = dirname($input_1_merged);
my $path = "/galaxydata/database/files/workspace/";
my ($nb) = ($output_txt=~/galaxy_dataset_(\d+)\.\S+$/);
#creation du repertoire de sortie
`cd $path; mkdir $nb; chmod -R 777 $nb `;
my $dirresults= $path."/".$nb;

my @T1 = ();
my @T2 = ();

for (my $i = 1; $i < $#ARGV; $i += 3)
{
    push @T1, $ARGV[$i];
    push @T2, $ARGV[$i+1]; 
}
my $paths = sprintf "%s", join (",",@T1);
my $names = sprintf "%s", join (",",@T2);

#Ligne de commande
$cmd = "(python $MERGECOLS -f $paths -n $names -c $c -o $dirresults/matrice.txt) >& /galaxydata/logs/sm_mergedcols_$nb.log 2>&1";
system $cmd;

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

# Recuperation du fichier sortant
if (! -e "$dirresults/matrice.txt"){print STDERR "matrice.txt file not found. \n";}
else {`cp -a "$dirresults/matrice.txt" $output_txt `;}

