#!/usr/bin/perl -w

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

use strict;
use File::Basename;

my $input_bam         = $ARGV[0];
my $RG                = $ARGV[1];
my $SM                = $ARGV[2];
my $PL                = $ARGV[3];
my $output_bam        = $ARGV[4];


#pour eviter que la commande ne fonctionne plus si le $PATH est perdu suite a un restart de galaxy
my $cmd = '';
my $addRG = '/usr/local/bioinfo/src/galaxy-dev/galaxy-dist/tool-data/shared/jars/AddOrReplaceReadGroups.jar';

#####################################################################
# ETAPE 1 : Ajout des champs @RG dans le fichier SRR007327
#
# Input :   fichier BAM
# Output :  fichier BAM
####################################################################
#java -jar -Xmx4G /usr/local/bioinfo/src/picard-tools/current/AddOrReplaceReadGroups.jar I=SRR007327_sorted.bam O=SRR007327_sorted_rg.bam
# ID=SRR RGLB=SRR PL=ILLUMINA PU=NONE SM=SRR
#java -jar -Xmx4G /usr/local/bioinfo/src/picard-tools/current/AddOrReplaceReadGroups.jar I=ERR_merge_sorted.bam O=ERR_merge_sorted_rg.bam
# ID=ERR RGLB=ERR PL=ILLUMINA PU=NONE SM=ERR

#system ("(java -jar -Xmx4G $addRG I=$input_bam O=${input_bam}_rg.bam ID=$RG RGLB=$RG PL=ILLUMINA PU=NONE SM=$RG) >& ./gatk1bis.log 2>&1");
system ("(java -jar -Xmx4G $addRG I=$input_bam O=${input_bam}_rg.bam ID=$RG RGLB=$RG PL=ILLUMINA PU=NONE SM=$SM) >& ./gatk_1-2.log 2>&1");

#Info pour les biologistes
print STDOUT "\nAdd Read Group (RG)  \n\n java -jar -Xmx4G $addRG I=$input_bam O=${input_bam}_rg.bam ID=$RG RGLB=$RG PL=$PL PU=NONE SM=$SM \n\n";

#Recuperation des fichiers dans Galaxy
if (! -e "${input_bam}_rg.bam"){print STDERR "BAM FILE WITH RG NOT FOUND\n";}
else{`mv "${input_bam}_rg.bam" $output_bam ;`}

