#!/usr/bin/perl -w

# usage : cluster file.fasta
use strict;
use File::Basename;

my $input_fasta  = $ARGV[0];
my $output = $ARGV[1];
my $cmd = '';
my $CLUSTER = "/usr/local/bioinfo/bin/cluster";
#my $path = dirname($input_fasta);
#my $path = "/work/galaxy-dev/workspace";
my $path = "/galaxydata/database/files/workspace/";
my ($nb) = ($output=~/galaxy_dataset_(\d+)\.\S+$/);
#creation du repertoire de sortie
`cd $path; mkdir $path/$nb; chmod -R 777 $path/$nb `;
my $dirresults= $path."/".$nb;

#Ligne de commande
$cmd = "cd $dirresults; ($CLUSTER $input_fasta) >& /galaxydata/logs/sm_cluster_$nb.log 2>&1";
system $cmd;

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

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

#if (! -e "$path/maxcluster"){print STDERR "maxcluster file not found. \n";}
#else {`cp -a "$path/maxcluster" $output`;}


