#generate a table (forR.BL.ok.tab) displaying CHIP-seq coverage per genomic window 
#for the different samples (and removes blacklisted regions and "chrUn|chrEBV|chrY|random|\s.\s")

rm -rf bg
mkdir bg ; mv *bam.bedgraph bg

for f in bg/*.bedgraph; do
	n1=`basename $f`
        nm=${n1/.bam.bedgraph/}
        echo $nm >$nm.RT
        cut -f5 $f >>$nm.RT
        echo -e "cs\tfrom\tto\tRT" >posrt.bed
        cut -f1-4 $f >> posrt.bed
done

paste posrt.bed *RT > forR.tab
rm *RT
rm posrt.bed

head -n1 forR.tab > header.tab

sed '1d' forR.tab | intersectBed -a stdin -b blacklist.bed -v > forR.BL.tab

grep -vP "chrUn|chrEBV|chrY|random|\s.\s" forR.BL.tab | cat header.tab - > forR.BL.ok.tab

rm header.tab
