#!/usr/bin/perl -w

# usage : perl sm_gatk_1-3.pl <BAM file> <BAM file> 
#sarah maman - 06/05/2013

use strict;
use File::Basename;


my $input_bam         = $ARGV[0];
my $output_bam        = $ARGV[1];


#pour eviter que la commande ne fonctionne plus si le $PATH est perdu suite a un restart de galaxy
my $cmd = '';
my $DUPLICATES ='/usr/local/bioinfo/src/galaxy-dev/galaxy-dist/tool-data/shared/jars/MarkDuplicates.jar';
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
my $ALL = $mday."-".($mon+1)."_".$hour."h".$min."mn".$sec."sec";


####################################################################
# ETAPE 2 : Suppression des lectures dupliquées
#
# Input :   fichier BAM
# Output :  fichier BAM
#           fichier texte .metrics (fichier de log)
####################################################################
#java -jar -Xmx4G /usr/local/bioinfo/src/picard-tools/current/MarkDuplicates.jar I=SRR007327_sorted_rg.bam O=SRR007327_sorted_rg_rmdup.ba
#m REMOVE_DUPLICATES=true M=SRR.metrics
#java -jar -Xmx4G /usr/local/bioinfo/src/picard-tools/current/MarkDuplicates.jar I=ERR_merge_sorted_rg.bam O=ERR_merge_sorted_rg_rmdup.ba
#m REMOVE_DUPLICATES=true M=ERR.metrics

system ("(java -jar -Xmx4G $DUPLICATES I=$input_bam O=${input_bam}_rmdup.bam REMOVE_DUPLICATES=true M=$ALL.metrics) >& ./gatk_1-3.log 2>&1");


#Info pour les biologistes
print STDOUT "\nRemove duplicated reads \n\n java -jar -Xmx4G $DUPLICATES I=$input_bam O=${input_bam}_rg_rmdup.bam REMOVE_DUPLICATES=true M=$ALL.metrics \n\n";

#Recuperation des fichiers dans Galaxy
if (! -e "${input_bam}_rmdup.bam"){print STDERR "BAM FILE WITHOUT DUPLICATED READS NOT FOUND\n";}
else{`cp -a "${input_bam}_rmdup.bam" $output_bam ;`}

