#! /bin/bash

## Generate tiled count of uniquely mappable bases, every 10kb.
## Resulting file will be used for hotspot scoring (passes 1 and 2,
## and re-scoring combined passes).

# Check if these results have already been computed?
check=_CHECK_

umap=_MAPPABLE_FILE_
chrfile=_CHROM_FILE_
chroms=$(cut -f1 $chrfile)
outdir=_OUTDIR_

umap10kb=$outdir/$(basename $umap).counts.10kb

bins=10000

if [ $check == "T" ] && [ -e $umap10kb ]; then
    echo "$thisscr: $umap10kb already exists; skipping" 1>&2
    exit 0
fi

for chr in $chroms
do
    coords=$(grep -e $chr$ $chrfile)

    echo $coords \
	| awk -v b=$bins \
	'BEGIN {OFS="\t"} ; { \
         for ( d = $2; d < $3; ) { \
           e = d + b; \
           if ( e > $3 ) { e = $3 } \
           print $1, d, e; \
           d = e; \
         } \
       }' - \
	| bedmap --delim "\t" --prec 0 --bp-ovr 1 --echo --bases - $umap \
        | cut --output-delimiter=" " -f1,2,4 - \
	> $chr.10kb.summed
done

## When that is done, merge by doing the following
rm -f $umap10kb
for chr in $chroms
do
    cat $chr.10kb.summed >> $umap10kb
    rm $chr.10kb.summed
done
