#!/bin/bash

# BAM_to_bigWig.sh
# By Kathryn Ramsey (ramsey.kathryn.m@gmail.com)
# Simon L. Dove Lab, Children's Hospital Boston, Harvard Medical School
# December 29, 2016
# Program generate bigwig files from bam files

# Program requirements:
#                  bedtools (version?)
#                  bedGraphToBigWig from UCSC
#                  Modification to direct program to appropriate directory; directory should contain new folder named "tracks"

# Usage: ./BAM_to_bigWig.sh ExperimentA ExperimentB


while [ ! -z "$1" ]

do
    PREFIX=$1  #Set the first argument to the experiment name
    DIRPATH=  #Define the working directory

    bedtools genomecov -bg -split -strand + -trackline -trackopts 'name='$PREFIX'_plus color=0,0,255' -ibam $DIRPATH/$PREFIX\_csort.plus.bam -g Fno_genomefile.txt > $DIRPATH/tracks/$PREFIX\_plus.bedGraph
    bedtools genomecov -bg -split -strand - -trackline -trackopts 'name='$PREFIX'_minus color=255,0,0' -ibam $DIRPATH/$PREFIX\_csort.minus.bam -g Fno_genomefile.txt > $DIRPATH/tracks/$PREFIX\_minus.bedGraph

    bedGraphToBigWig $DIRPATH/tracks/$PREFIX\_plus.bedGraph Fno_genomefile.txt $DIRPATH/tracks/$PREFIX\_plus.bw
    rm $DIRPATH/tracks/$PREFIX\_plus.bedGraph
    bedGraphToBigWig $DIRPATH/tracks/$PREFIX\_minus.bedGraph Fno_genomefile.txt $DIRPATH/tracks/$PREFIX\_minus.bw
    rm $DIRPATH/tracks/$PREFIX\_minus.bedGraph

    shift

done
