#!/usr/bin/perl -w 
# usage : perl sm_samtools_view_coord.pl 
#smaman - 24/04/2013

# 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; 
use Config::IniFiles;

my $input_bam          = $ARGV[0]; 
my $param              = $ARGV[1]; 
my $output_head_bam    = $ARGV[2];

my $cfg = Config::IniFiles->new( -file => "/path/to/PATH.ini" );
my $SAMTOOLS = $cfg->val( 'toolsPath', 'SAMTOOLS_PATH' ); 
my $cmd = ''; 
my $cmd1 = '';
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
my $ALL = $mday."-".($mon+1)."_".$hour."h".$min."mn".$sec."sec";



#Afficher les x premieres lignes de votre fichier BAM
$cmd1 = "$SAMTOOLS view -h $input_bam | head -n $param > ${ALL}_head.txt";
system $cmd1;


#Recuperation du fichier dans Galaxy
if (! -e "${ALL}_head.txt"){print STDERR "Pas de fichier head bam produit \n";}
else {$cmd = "(mv ${ALL}_head.txt $output_head_bam) >& ./cp_head-BAM.log 2>&1";
system $cmd;}

