#!/bin/bash

# Merge and threshold hotspots from pass 2.

tags=_TAGS_

thresh=_THRESH_
maxWin=_MAX_WIN_
pad=`echo $maxWin/2 | bc`
minSize=_MINSIZE_

echo "Merge and threshold final hotspots..."

name=`basename $tags`

dir=${name}-both-passes
cd $dir

in=*.hotspot.twopass.zscore.wig
if [ ! -e $in ]; then
    echo "$in does not exist; Check results. Skipping"
    cd ..
    exit
fi
test=`grep chrX $in | head -1`
if [ ${#test} == 0 ]; then 
    echo "$in does not contain data for chrX; Problem? Skipping."
    cd ..
    exit
fi

out=`echo $in | sed s/hotspot\.twopass\.zscore\.wig$/twopass.merge${maxWin}.wgt10.zgt${thresh}.wig/`
echo $out
echo "merging..."
awk -v pad=$pad -v thresh=$thresh -v minsize=$minSize '(NR > 1 && $4 >= thresh && ($3-$2)>=minsize) {left=$2 - pad; if(left < 1) left = 1; print $1"\t" left "\t" $3 + pad}' $in \
    | setops -m - \
    | awk -v pad=$pad -v minsize=$minSize '{left=$2+pad; right=$3-pad; if(left > right-minSize) left=1; print $1"\t"left"\t"right}' - \
    | sort-bed - \
    > new.bed

echo "track type=wiggle_0 visibility=dense name=${name}_2pass_merge_${maxWin}_zgt$thresh" > $out

echo "recovering z-scores for merged hotspots..."
awk '(NR>1){print $1"\t"$2"\t"$3"\tI\t"$4}' $in \
    | signalmap -max -% 0 new.bed - \
    | cut -f1 - \
    | paste new.bed - \
    >> $out

rm new.bed
cd ..
