#!/usr/bin/perl -w

# usage : perl sm_sigcufflinks.pl <BAM file> <output TXT file>
# Sarah Maman
# 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/>.
#


use strict;
use File::Basename;

my $input_bam             = $ARGV[0];
my $input_gtf             = $ARGV[1];
my $optiong               = $ARGV[2];
my $output_isoforms       = $ARGV[3];
my $output_genes          = $ARGV[4];
my $output_transcripts    = $ARGV[5];
my $output_rawtranscripts = $ARGV[6];
my $SIGCUFFLINKS = '/usr/local/bioinfo/bin/sigcufflinks';
my $cmd = '';
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
my $ALL = $mday."-".($mon+1)."_".$hour."h".$min."mn".$sec."sec_SIGCUFFLINKS";
my ($nb) = ($output_genes=~/galaxy_dataset_(\d+)\.\S+$/);
#creation du repertoire resultat 
#my $chemin=dirname($input_bam);
my $chemin="/galaxydata/database/files/workspace/";
`cd $chemin; mkdir $nb/; chmod -R 777 $nb/;`;
my $path =$chemin."/".$nb;



my $g ='';

if ($optiong eq "guide"){$g ="-G ";}
if ($optiong eq "ref"){$g ="-g ";}

#commande : perl sm_sigcufflinks.pl tets/accepted_hits.bam tests/Danio_rerio_chr22.Zv9.62.gtf
#sigcufflinks --output-dir=/work/.../sigcufflinks_ERR022486 -G /.../Danio_rerio_chr22.Zv9.62.gtf /..../tophat_ERR022486/accepted_hits_ERR022486.bam
$cmd = "($SIGCUFFLINKS --output-dir=$path $g $input_gtf  $input_bam -p 4) >& /galaxydata/logs/sigcufflinks_$nb.log 2>&1";
system $cmd;
#Info pour les biologistes
print STDOUT "Sigcufflinks : \n\n $cmd \n\n ";


#--output-dir fichier a recuperer :
#isoforms.fpkm_tracking
#gene.fpkm_tracking
#transcripts.gtf
#raw_transcripts.tsv

#ls -ltrah  /galaxydata/database/files/workspace//15113/
#total 14M
#-rw-r--r--   1 nazzouzi   UMR6290     0 12 déc.  13:07 skipped.gtf
#drwxrwxrwx   2 nazzouzi   UMR6290   212 12 déc.  13:07 .
#-rw-r--r--   1 nazzouzi   UMR6290   230 12 déc.  13:07 sigcufflink.report
#-rw-r--r--   1 nazzouzi   UMR6290  151K 16 déc.  04:28 raw_transcripts.tsv
#-rw-r--r--   1 nazzouzi   UMR6290  311K 16 déc.  04:28 genes.fpkm_tracking
#-rw-r--r--   1 nazzouzi   UMR6290  523K 16 déc.  04:28 isoforms.fpkm_tracking
#-rw-r--r--   1 nazzouzi   UMR6290  9,7M 16 déc.  04:28 transcripts.gtf
#drwxrwxrwt 757 galaxy-dev wbioinfo  40K 16 déc.  11:34 ..


my $isoforms       = $path."/"."isoforms.fpkm_tracking";
my $genes          = $path."/"."genes.fpkm_tracking"; 
my $transcripts    = $path."/"."transcripts.gtf";
my $rawtranscripts = $path."/"."raw_transcripts.tsv";

print STDOUT "Outputs files :\n
$isoforms       \n
$genes          \n
$transcripts    \n
$rawtranscripts \n
";

if (! -e "$isoforms"){print STDERR "isofomes file not found\n";}
else{`cp -a $isoforms $output_isoforms`;}
if (! -e "$genes"){print STDERR "genes file not found\n";}
else{`cp -a $genes $output_genes`;}
if (! -e "$transcripts"){print STDERR "transcripts file not found\n";}
else{`cp -a $transcripts $output_transcripts`;}
if (! -e "$rawtranscripts"){print STDERR "rawtranscripts file not found\n";}
else{`cp -a $rawtranscripts $output_rawtranscripts`;}
