#!/bin/bash
#
#SBATCH --output=/home/jvierstra/proj/footprinting.2019/processed/CD19+-DS17186/phylop.per-nt/logs/%A.%a.out
#SBATCH --mem=12G
#SBATCH --cpus-per-task=4

set -o pipefail

TMPDIR=`mktemp -d`

ARGS=(`cat /home/jvierstra/proj/footprinting.2019/processed/CD19+-DS17186/phylop.per-nt/slurm.phylop.params | head -n ${SLURM_ARRAY_TASK_ID} | tail -n 1`)

mkfifo ${TMPDIR}/phylop.bed
mkfifo ${TMPDIR}/fps.bed

tabix /home/jvierstra/data/genomes/hg38/phyloP100way/hg38.phyloP100way.bed.gz ${ARGS[0]} > ${TMPDIR}/phylop.bed &
tabix /home/jvierstra/proj/footprinting.2019/processed/CD19+-DS17186/interval.all.bedgraph.gz ${ARGS[0]} | cut -f1-3,7 > ${TMPDIR}/fps.bed &

bedmap --delim "\t" --echo --max ${TMPDIR}/fps.bed ${TMPDIR}/phylop.bed 	| awk '$4>0' | grep -v "NAN" | cut -f4- | sort -k1,1g > /home/jvierstra/proj/footprinting.2019/processed/CD19+-DS17186/phylop.per-nt/phylop.${ARGS[0]}

rm -rf ${TMPDIR}
