#!/bin/ksh

### GAD PIPELINE ###
## wrapper_create_html.sh
## Version : 1.0.0
## Description : a wrapper for qsubing create_html.py script
## Usage : qsub -pe smp <nb thread> -v VCF_SNP=</path/to/the/input/vcf/file>,VCF_CNV=</path/to/the/input/vcf/file>,REPORTFILE=<path/to/the/HLAscan/report>,BAMFILE=</path/to/the/bam>,TSVFILE=</path/to/the/tsv>,OUTPUTFILE=</path/to/the/output/vcf/file>,[LOGFILE=/path/to/the/log/file],[CONFIGFILE=/path/to/the/config/file] wrapper_create_html.sh
## Output : create_html.py output
## Requirements : Recquire script create_html.py and VcfTools

## Author : Simon.Verdez@u-bourgogne.fr
## Creation Date : 20180802
## last revision date : 20180802
## Known bugs : None

#$ -q batch
#$ -V


# Log file path option
if [ -z ${LOGFILE} ]
then
    LOGFILE=annotate_variants.$(date +"%F_%H-%M-%S").log
fi

# Config file path option
if [ -z ${CONFIGFILE} ]
then
    CONFIGFILE=analysis_config.tsv
fi

# Logging
exec 1>> $LOGFILE 2>&1
echo "$(date +"%F_%H-%M-%S"): START"

# Check if config file exist
if [ ! -f $VCF_SNP ]
then
    echo "Vcf SNP file does not exist"
    echo "$(date +"%F_%H-%M-%S"): END"
    touch create_html.failed
    exit 1
fi

if [ ! -f $VCF_CNV ]
then
    echo "Vcf CNV file does not exist"
    echo "$(date +"%F_%H-%M-%S"): END"
    touch create_html.failed
    exit 1
fi

if [ ! -f $REPORTFILE ]
then
    echo "Report HLAscan file does not exist"
    echo "$(date +"%F_%H-%M-%S"): END"
    touch create_html.failed
    exit 1
fi

if [ ! -f $BAMFILE ]
then
    echo "BAM file does not exist"
    echo "$(date +"%F_%H-%M-%S"): END"
    touch create_html.failed
    exit 1
fi

if [ ! -f $TSVFILE ]
then
    echo "TSV file does not exist"
    echo "$(date +"%F_%H-%M-%S"): END"
    touch create_html.failed
    exit 1
fi

# Check if output file is specified
if [ -z $OUTPUTFILE ]
then
    echo "Output file is not specified"
    echo "$(date +"%F_%H-%M-%S"): END"
    touch create_html.failed
    exit 1
fi

PYTHONBIN=`grep pythonbin $CONFIGFILE | cut -f2`
PIPELINEBASE=`grep pipelinebase $CONFIGFILE | cut -f2`

# Launch python script command and check exit code
echo "command : $PIPELINEBASE/create_html.py -v $VCF_SNP -c $VCF_CNV -h $REPORTFILE -b $BAMFILE -t $TSVFILE -o $OUTPUTFILE -e $LOGFILE"
$PYTHONBIN $PIPELINEBASE/create_html.py -v $VCF_SNP -c $VCF_CNV -h $REPORTFILE -b $BAMFILE -t $TSVFILE -o $OUTPUTFILE -e $LOGFILE
create_html_exitcode=$?
echo "create_html exit code : $annotate_variants_exitcode"
if [ $creaete_html_exitcode != 0 ]
then
    echo "$(date +"%F_%H-%M-%S"): END"
    touch create_html.failed
    exit 1
fi

echo "$(date +"%F_%H-%M-%S"): END"