#!/usr/bin/perl -w 
# usage : perl sm_gtf_manip.pl 
#smaman - 26/04/2013

use strict; 
use File::Basename; 

my $input_gtf          = $ARGV[0]; 
my $param              = $ARGV[1];
my $output_report      = $ARGV[2]; 
 
my $path = dirname($input_gtf);
my $cmd='';
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
my $ALL = $mday."-".($mon+1)."_".$hour."h".$min."mn".$sec."sec";


#samtools view -h aln.bam | head 
if ($param != 0){
`cut -f $param $input_gtf > $path/tmp1`;
`sort -u $path/tmp1 > $path/tmp2`;
`wc -l $path/tmp2 > $path/tmp3`;
`rm $path/tmp1; rm $path/tmp2; mv $path/tmp3 $path/${ALL}_report.txt`;

#Info pour les biologistes
print STDOUT "Command line with param (= $param) : \n\n cut -f $param $input_gtf > $path/tmp1; sort -u $path/tmp1 > $path/tmp2 ; wc -l $path/tmp2 > $path/tmp3;
rm $path/tmp1; rm $path/tmp2; mv $path/tmp3 $path/${ALL}_report.txt; \n\n ";

}
else
{
`sort -u $input_gtf > $path/tmpa`;
`wc -l $path/tmpa > $path/tmpb`;
`rm $path/tmpa; mv $path/tmpb $path/${ALL}_report.txt`;

#Info pour les biologistes
print STDOUT "Command line with param (= $param) : \n\n sort -u $input_gtf > $path/tmpa; wc -l $path/tmpa > $path/tmpb ; rm $path/tmpa; mv $path/tmpb $path/${ALL}_report.txt; \n\n ";

}

#Recuperation du fichier dans Galaxy
if (! -e "$path/${ALL}_report.txt"){print STDERR "Pas de report produit \n";}
else {$cmd = "(mv $path/${ALL}_report.txt $output_report) >& ./cp_report_gtf.log 2>&1";
system $cmd;}


#pour tester :
#count uniq line or column (column 1 param : colonne)
#cut -f param | sort -u | wc -l (si param !=0)
#sort -u | wc -l
