#! /bin/bash

##############################################
# 
# Compute SPOT (Signal Portion Of Tags) metric
# 
##############################################

tags=_TAGS_
outdir=_OUTDIR_

# Duplicate tags OK?  (Set to T if yes - for DNaseI data, for instance; anything else - for ChIP, for instance - means no.)
dupok=_DUPOK_

thisscr="run_spot"
echo
echo $thisscr

proj=`basename $tags | sed s/\.bam$// | sed s/\.bed.starch$//`
tagb=$outdir/$proj.bed.starch
ntag=`cut -d" " -f2 $outdir/$proj-pass1/*.stdout`
hot=$outdir/$proj-both-passes/$proj.hotspot.twopass.zscore.wig
out=$outdir/$proj.spot.out
if [ $dupok == "T" ]; then
    tih=$(unstarch $tagb | bedops --header -e -1 - $hot | wc -l)
else
    tih=$(unstarch $tagb | bedops --header -e -1 - $hot | uniq | wc -l)
fi
spot=$(echo "scale=4; $tih/$ntag" | bc)
echo "tih = $tih"
echo "SPOT = $spot"
printf "%12s  %12s  %6s\n" "total tags" "hotspot tags" "SPOT" > $out
printf "%12d  %12d  %.4f\n" $ntag $tih $spot >> $out
