#!/usr/bin/perl -w

# usage : perl sm_final_count.pl /bank/bowtie2db/genome.fa All/merged.gtf /work/sigenae/Project_MONOPOLY.222
# Sarah Maman
# Copyright (C) 2013 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_bank                                         = $ARGV[0];
my $input_gtf                                          = $ARGV[1];
my $path = dirname($input_gtf);
my $output_merged_rename_gtf                           = $ARGV[2];
my $output_correspondance_txt                          = $ARGV[3];
my $output_global_count_genes_transcripts_rename_csv   = $ARGV[4];
my $output_global_count_genes_rename_csv               = $ARGV[5];
my $output_transcrits_Cuffmerge_fasta                  = $ARGV[6];
#my $output_graphic_correlation_pdf                     = $ARGV[7];
#my $output_dendogramme_pdf                             = $ARGV[8];
#Liste des fichiers raw_transcripts.tsv issus de sigcufflinks
my $inputs_raw_transcripts_tsv = @ARGV[7..$#ARGV];

my $cmd = '';
my $COUNT = "/usr/local/bioinfo/src/galaxy-dev/galaxy-dist/tools/sm_RNAseq/final.pl";
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
my $ALL = $mday."-".($mon+1)."_".$hour."h".$min."mn".$sec."sec_FINALCOUNT";
my $dirresults= $path."/".$ALL;




#supprimer le rep testCOUNT/ avant de retester la commande. Les pdf generes par R ne seront pas trouver lors du test sur la vm.
#./final.pl /bank/bowtie2db/ensembl_sus_scrofa_genome tests/merged.gtf testCOUNT4  tests/raw_transcripts1.tsv  tests/raw_transcripts2.tsv  tests/raw_transcripts3.tsv


#perl sm_final_count.pl /bank/bowtie2db/ensembl_danio_rerio_genome tests/merge/Danio_rerio_chr22.Zv9.62.gtf 1 2 3 4 5 tests/merge/Galaxy25-[Sigcufflinks_on_Tophat_ERR022486_read1_bam_gives_rawtranscripts_tsv].txt  nom1 tests/merge/Galaxy29-[Sigcufflinks_on_Tophat_ERR022488_read1_bam_gives_rawtranscripts_tsv].txt nom2
#Liste des fichiers de test :
#Danio_rerio_chr22.Zv9.62.gtf
#Galaxy25-[Sigcufflinks_on_Tophat_ERR022486_read1_bam_gives_rawtranscripts_tsv].txt
#Galaxy29-[Sigcufflinks_on_Tophat_ERR022488_read1_bam_gives_rawtranscripts_tsv].txt


#PARAMETRES :
#-arg1<>  Path to genome bowtie2bd fasta file (/bank/bowtie2db/genome.fa)
#-arg2<>  Path to your merged.gtf file
#-arg3<>  Path to raw_transcripts tsv files (from sigcufflinks)


$cmd = "(perl $COUNT $input_bank $input_gtf $dirresults @ARGV[7..$#ARGV]) >& ./finalcount.log 2>&1";
system $cmd;

#Info pour les biologistes
print STDOUT "Final count : \n\n perl $COUNT $input_bank $input_gtf $dirresults @ARGV[7..$#ARGV]\n\n ";


# merged.rename.gtf
if (! -e "$dirresults/merged.rename.gtf"){print STDERR "merged rename gft file not found. \n";}
else {`cp -a "$dirresults/merged.rename.gtf" $output_merged_rename_gtf `;}

# correspondance.txt
#my $output_correspondance.txt
if (! -e "$dirresults/correspondance.txt"){print STDERR "correspondance txt file not found. \n";}
else {`cp -a "$dirresults/correspondance.txt" $output_correspondance_txt `;}

# global_count_genes_transcripts_rename.csv
#my $output_global_count_genes_transcripts_rename_csv
if (! -e "$dirresults/global_count_genes_transcripts_rename.csv"){print STDERR "global_count_genes_transcripts_rename_csv file not found. \n";}
else {`cp -a "$dirresults/global_count_genes_transcripts_rename.csv" $output_global_count_genes_transcripts_rename_csv`;}

# global_count_genes_rename.csv
#my $output_global_count_genes_rename_csv 
if (! -e "$dirresults/global_count_genes_rename.csv"){print STDERR "global_count_genes_rename_csv  file not found. \n";}
else {`cp -a "$dirresults/global_count_genes_rename.csv" $output_global_count_genes_rename_csv`;}

# transcrits_Cuffmerge.fasta
#my $output_transcrits_Cuffmerge_fasta                
if (! -e "$dirresults/transcrits_Cuffmerge.fasta"){print STDERR "transcrits_Cuffmerge_fasta file not found. \n";}
else {`cp -a "$dirresults/transcrits_Cuffmerge.fasta" $output_transcrits_Cuffmerge_fasta `;}

## graphic_correlation.pdf
#if (! -e "$dirresults/graphic_correlation.pdf"){print STDERR "graphic_correlation_pdf file not found. \n";}
#else {`cp -a "$dirresults/graphic_correlation.pdf" $output_graphic_correlation_pdf `;}
#
## dendogramme.pdf
#if (! -e "$dirresults/dendogramme.pdf"){print STDERR "dendogramme_pdf file not found. \n";}
#else {`cp -a "$dirresults/dendogramme.pdf" $output_dendogramme_pdf `;}
