#!/usr/bin/perl -w

# usage : perl sm_samtools_flagstat.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 Config::IniFiles;

my $input_file = $ARGV[0];
my $output_file = $ARGV[1];
my $cfg = Config::IniFiles->new( -file => "/path/to/PATH.ini" );
my $SAMTOOLS = $cfg->val( 'toolsPath', 'SAMTOOLS_PATH' );
my $cmd = '';
my ($nboutput) = ($output_file=~/galaxy_dataset_(\d+)\.\S+$/);
my $GALAXYDATA = $cfg->val( 'workPath', 'FILEPATH_DEV' );

#commande : perl sm_samtools_flagstat.pl tests/test.bam
#ou perl sm_samtools_flagstat.pl tests/accepted_hits.bam
$cmd = "($SAMTOOLS flagstat $input_file > $GALAXYDATA/$nboutput.txt ) >& ./samtoolsflagstat.log 2>&1";
#Info pour les biologistes
print STDOUT "Samtools flagstat : \n\n $cmd \n\n ";
system $cmd;

my $out = "$GALAXYDATA/$nboutput.txt";
if (! -e "$out") {print STDERR "FLAGSTAT TXT FILE $out NOT FOUND\n";}
else{`cp -a $out $output_file`;}



