#!/bin/bash

USAGE="web_pipeline variants copynumber output_prefix "
if [ -z "$1" ]
  then
    echo "ERROR in SplitThreader: No variants file given"
    echo "Usage:"
    echo $USAGE
    exit
fi
if [ -z "$2" ]
  then
    echo "ERROR in SplitThreader: No copy number file given"
    echo "Usage:"
    echo $USAGE
    exit
fi
if [ -z "$3" ]
  then
    echo "ERROR in SplitThreader: No output prefix given"
    echo "Usage:"
    echo $USAGE
    exit
fi

# Author: Maria Nattestad
# Email: mnattest@cshl.edu

VARIANTS=${1?"$USAGE"}
COPYNUMBER=${2?"$USAGE"}
OUTPUT_PREFIX=${3?"$USAGE"}

source python_env/bin/activate

PATH=$PATH:/usr/local/bin/

# Log file contains all reporting that is shown to the user
LOG_FILE=${OUTPUT_PREFIX%/*}/progress.log
# Add description to first line of log file, which will be shown as the header
echo "${OUTPUT_PREFIX##*/}" > $LOG_FILE 

echo "1. Reading input files" >> $LOG_FILE
./bin/standardize_variants.py -input $VARIANTS -out $OUTPUT_PREFIX.variants.csv >> $LOG_FILE

if [ -e $OUTPUT_PREFIX.variants.csv ];
then 
    # if variants worked, then run copy number
    ./bin/standardize_copynumber.py -csv $COPYNUMBER -out $OUTPUT_PREFIX.copynumber.csv -genome_out $OUTPUT_PREFIX.genome.csv >> $LOG_FILE

    if [ -e $OUTPUT_PREFIX.copynumber.csv ];
    then
        echo "2. Segmenting copy number" >> $LOG_FILE
        Rscript ./bin/segment_copy_number.R $OUTPUT_PREFIX.copynumber.csv 
        if [ -e $OUTPUT_PREFIX.copynumber.segmented.csv ];
        then            
            ./bin/call_CNVs.py -csv $OUTPUT_PREFIX.copynumber.segmented.csv -out $OUTPUT_PREFIX.copynumber.segmented.consolidated.csv
        
            echo "done" >> $LOG_FILE
        else
            echo "Error: Copy number segmentation failed"
            echo "fail" >> $LOG_FILE
        fi
    else
        echo "Error: No standardized copy number file created" >> $LOG_FILE
        echo "fail" >> $LOG_FILE
    fi
else
    echo "Error: No standardized variant file created" >> $LOG_FILE
    echo "fail" >> $LOG_FILE
fi

