#!/usr/bin/perl -w                                #
#                                                 #
# Script ProcessSample.pl                         #
#                                                 #
#                                                 #
# Input : species,sample                          # 
# Output: Battery of benchmarks with SeqCode      #
#                                                 #
#                                                 #
# by Enrique Blanco @ CRG (2021)                  #
###################################################

use strict;
use Getopt::Std;
use Term::ANSIColor;

#DEFINEs
my $TRUE = 1;
my $FALSE = 0;
my $BIN = "./bin/catalina";
my $RESULTS = "./catalina/results";
my $INFO = "./catalina/info";


## Step 0. Reading arguments
my %opt;
my $start;
my ($genome,$sample);
my $n_files;
my $pathsample;

(getopts('v',\%opt)) or print_error("parser: Problems reading options\n");

print_mess("ProcessSample.pl by Enrique Blanco @ CRG (2021)\n\n");
print_mess("Stage 0.  Reading options\n");

$n_files = $#ARGV+1;
($n_files==2) or print_error("USAGE: $n_files arguments were provided but two are at necessary!\nProcessSample.pl <species> <sample>");
($genome,$sample) = @ARGV;
$pathsample = "samples/".$genome."/".$sample.".bam";

# Save the starting time
$start = time();

print_mess("$n_files arguments: $genome $sample ($pathsample)");
print_ok();
##


## Step 1. Run distinct SeqCode commands over this sample
my $release;
my $command;


if ($genome eq "mouse")
{
    $release = "data/mm9";
}
else
{
    $release = "data/hg19";
}

print_mess("Stage 1.1  Generate custom track\n");
$command = "$BIN/buildChIPprofile -vx $RESULTS $release/ChromInfo.txt $pathsample $sample 2> $INFO/buildChIPprofile"."_".$sample.".txt";
print_mess("$command");
system($command);
print_ok();

print_mess("Stage 1.2  Generate the TSS metaplot\n");
$command = "$BIN/produceTSSplots -vx $RESULTS $release/ChromInfo.txt $release/refGene.txt $pathsample $release/list_genes.txt $sample 5000 2> $INFO/produceTSSplots"."_".$sample.".txt";
print_mess("$command");
system($command);
print_ok();

print_mess("Stage 1.3  Generate the GENE metaplot\n");
$command = "$BIN/produceGENEplots -vx $RESULTS $release/ChromInfo.txt $release/refGene.txt $pathsample $release/list_genes.txt $sample 5000 2> $INFO/produceGENEplots"."_".$sample.".txt";
print_mess("$command");
system($command);
print_ok();

print_mess("Stage 1.4  Generate the TSS heatmap\n");
$command = "$BIN/produceTSSmaps -vx $RESULTS $release/ChromInfo.txt $release/refGene.txt $pathsample $release/list_genes.txt $sample 5000 2> $INFO/produceTSSmaps"."_".$sample.".txt";
print_mess("$command");
system($command);
print_ok();

print_mess("Stage 1.5  Generate the GENE heatmap\n");
$command = "$BIN/produceGENEmaps -vx $RESULTS $release/ChromInfo.txt $release/refGene.txt $pathsample $release/list_genes.txt $sample 5000 2> $INFO/produceGENEmaps"."_".$sample.".txt";
print_mess("$command");
system($command);
print_ok();

print_mess("Stage 1.6  Calculate signal strength values\n");
$command = "$BIN/recoverChIPLevels -vx $RESULTS $release/ChromInfo.txt $pathsample $release/refGene.bed $sample 2> $INFO/recoverChIPLevels"."_".$sample.".txt";
print_mess("$command");
system($command);
print_ok();

print_mess("Stage 1.7  Seach higher peaks of signal\n");
$command = "$BIN/findPeaks -vx $RESULTS -t 5 $release/ChromInfo.txt $pathsample $sample 2> $INFO/findPeaks"."_".$sample.".txt";
print_mess("$command");
system($command);
print_ok();


## Step 2. Finishing successful program execution
my $stop;

# Save the ending time
$stop = time();

print_mess("Successful termination of the test\n");
print_mess("Total running time (hours):",int((($stop-$start)/3600)*1000)/1000," hours\n");
print_mess("Total running time (minutes):",int((($stop-$start)/60)*1000)/1000," mins\n");
print_mess("Total running time (seconds):",int(($stop-$start)*1000)/1000," secs");
print_ok();
exit(0);
##


############ Subroutines

sub print_mess
{
        my @mess = @_;

        print STDERR color("bold green"),"%%%% @mess" if (exists($opt{v}));
	print STDERR color("reset");
}

sub print_error
{
        my @mess = @_;

        print STDERR color("bold green"),"%%%% @mess\n";
	print STDERR color("reset");
	exit();
}

sub print_ok
{
    if (exists($opt{v}))
    {
	print STDERR color("bold green"), "\t\t[OK]\n\n";
	print STDERR color("reset");
    }
}

