#!/usr/bin/perl -w
#smaman - 18/04/2014
use strict;
use File::Basename;

my $input_gtf          = $ARGV[0];
my $libtype            = $ARGV[1];
my $libmet             = $ARGV[2];
my $output_isoforms    = $ARGV[3];
my $output_genes       = $ARGV[4];
my $output_log         = $ARGV[5];
my $input_1_bam        = $ARGV[6];
my $input_2_bam        = $ARGV[7];
#Liste des fichiers bam entrants
my $input_other_bam    = @ARGV[8..$#ARGV];
             
my $cmd = '';
my $CUFFNORM = "/usr/local/bioinfo/src/cufflinks/cufflinks-2.2.0.Linux_x86_64/cuffnorm";
my $path = dirname($input_gtf);
my ($nb) = ($output_genes=~/galaxy_dataset_(\d+)\.\S+$/);
my $dirresults= $path."/".$nb."/";


#/usr/local/bioinfo/src/cufflinks/cufflinks-2.2.0.Linux_x86_64/cuffnorm tests/merged.gtf tests/accepted_hits_ERR022488_refchr22.bam tests/test.bam
#fICHIERS SORTANTS :
#-rw-r--r--  1 galaxy wbioinfo 1,6M 18 avril 16:00 isoforms.fpkm_table
#-rw-r--r--  1 galaxy wbioinfo 873K 18 avril 16:00 tss_groups.fpkm_table
#-rw-r--r--  1 galaxy wbioinfo   11 18 avril 16:00 cds.fpkm_table
#-rw-r--r--  1 galaxy wbioinfo 851K 18 avril 16:00 genes.fpkm_table
#-rw-r--r--  1 galaxy wbioinfo 1,6M 18 avril 16:00 isoforms.count_table
#-rw-r--r--  1 galaxy wbioinfo 873K 18 avril 16:00 tss_groups.count_table
#-rw-r--r--  1 galaxy wbioinfo   11 18 avril 16:00 cds.count_table
#-rw-r--r--  1 galaxy wbioinfo 851K 18 avril 16:00 genes.count_table
#-rw-r--r--  1 galaxy wbioinfo 6,3M 18 avril 16:00 isoforms.attr_table
#-rw-r--r--  1 galaxy wbioinfo 3,9M 18 avril 16:00 tss_groups.attr_table
#-rw-r--r--  1 galaxy wbioinfo   82 18 avril 16:00 cds.attr_table
#-rw-r--r--  1 galaxy wbioinfo 3,4M 18 avril 16:00 genes.attr_table
#-rw-r--r--  1 galaxy wbioinfo  136 18 avril 16:00 samples.table
#-rw-r--r--  1 galaxy wbioinfo  221 18 avril 16:00 run.info

$cmd = "($CUFFNORM --library-type $libtype --library-norm-method $libmet --output-dir $dirresults  $input_gtf $input_1_bam $input_2_bam @ARGV[8..$#ARGV]) >& ./cuffnorm.log 2>&1";
system $cmd;

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


# Recuperation des fichiers dans Galaxy
if (! -e "$dirresults/isoforms.count_table"){print STDERR "isoforms.count_table file not found. \n";}
else {`cp -a "$dirresults/isoforms.count_table" $output_isoforms`;}

if (! -e "$dirresults/genes.count_table"){print STDERR "genes.count_table not found. \n";}
else {`cp -a "$dirresults/genes.count_table" $output_genes`;}

if (! -e "$dirresults/run.info"){print STDERR "run info not found. \n";}
else {`cp -a "$dirresults/run.info" $output_log`;}


