#! /bin/bash

#############################################################
# Modified runhotspot for better integration
#############################################################

if [ $# -lt 4 ];then
    echo `basename $0` "config for hotspot"
    echo "Need 1 parameters now: <pipeDir> <tokenFile> <output> <omit_region_file>"
    exit 1
fi

scriptTokBin="script-tokenizer.py" ## write to bin
pipeDir=$1
tokenFile=$2
output=$3
spot=$4
omit=$5

## Do SPOT only (set _FDRS_ to "N" in runall.tokens.txt)
# scripts="$pipeDir/run_make_lib
#     $pipeDir/run_10kb_counts
#     $pipeDir/run_pass1_hotspot
#     $pipeDir/run_pass1_merge_and_thresh_hotspots
#     $pipeDir/run_pass2_hotspot
#     $pipeDir/run_rescore_hotspot_passes
#     $pipeDir/run_spot"

## Do everything, including badspots and final cleanup
scripts="$pipeDir/run_badspot
    $pipeDir/run_make_lib
    $pipeDir/run_wavelet_peak_finding
    $pipeDir/run_10kb_counts
    $pipeDir/run_generate_random_lib
    $pipeDir/run_pass1_hotspot
    $pipeDir/run_pass1_merge_and_thresh_hotspots
    $pipeDir/run_pass2_hotspot
    $pipeDir/run_rescore_hotspot_passes
    $pipeDir/run_spot
    $pipeDir/run_thresh_hot.R
    $pipeDir/run_both-passes_merge_and_thresh_hotspots
    $pipeDir/run_add_peaks_per_hotspot
    $pipeDir/run_final"

cd $output
$scriptTokBin \
    --clobber \
    --output-dir=`pwd` \
    $tokenFile \
    $scripts

if [ ${#omit} != 0 ];
then
    bash ./run_badspot.tok ## need omit satellite regions, not available for mm9 yet
fi

bash ./run_make_lib.tok
bash ./run_wavelet_peak_finding.tok
bash ./run_10kb_counts.tok
bash ./run_generate_random_lib.tok
bash ./run_pass1_hotspot.tok
bash ./run_pass1_merge_and_thresh_hotspots.tok
bash ./run_pass2_hotspot.tok
bash ./run_rescore_hotspot_passes.tok

if [ $spot == "5M" ]
then
    bash ./run_spot.tok
fi

Rscript ./run_thresh_hot.R.tok
bash ./run_both-passes_merge_and_thresh_hotspots.tok
bash ./run_add_peaks_per_hotspot.tok
bash ./run_final.tok

cd ..
