#!/bin/bash

# The bicycle command under docker
# the current working directory (pwd) will be seen insice docker as /data
alias bicycle="docker run -e JVM_ARGS=-Xmx16G --rm -v `pwd`/data-case-study:/data -u `id -u \`whoami\`` -it singgroup/bicycle bicycle"

# Local data directory
DATA_DIR=`pwd`/"data-case-study"

# Download data
mkdir -p $DATA_DIR

cd $DATA_DIR

if [ ! -d "raw_data" ]; then
  wget http://static.sing-group.org/software/bicycle/data/case_study/raw_data.zip && unzip raw_data.zip && rm raw_data.zip
fi

if [ ! -d "referenceGenome" ]; then
  wget http://static.sing-group.org/software/bicycle/data/case_study/reference_genome.zip && unzip reference_genome.zip && rm reference_genome.zip
fi

if [ ! -f "target_regions.bed" ]; then
  wget http://static.sing-group.org/software/bicycle/data/case_study/target_regions.bed
fi

cd ..

# Declare the Docker data directories
REFERENCE_GENOME="data/referenceGenome"
TARGET_REGIONS="data/target_regions.bed"
SAMPLES_DIR="data/raw_data"
PROJECT_DIR="data/bicycle-case-study-project"

# Create a new bicycle project
bicycle create-project -p $PROJECT_DIR -r $REFERENCE_GENOME -f $SAMPLES_DIR --paired-mate1-regexp _1.fastq

# Reads alignment
bicycle align -p $PROJECT_DIR -t 4 --bowtie2-quals phred33 --bowtie2-I 0 --bowtie2-X 500

# Analyze methilation
bicycle analyze-methylation -p $PROJECT_DIR -t 4 -n 1 --remove-ambiguous --only-with-one-alignment -b $TARGET_REGIONS

# Analyze differnetial metilation 
bicycle analyze-differential-methylation -p $PROJECT_DIR  -c SRR2052487,SRR2052488,SRR2052489,SRR2052490,SRR2052491 -t SRR2052492,SRR2052493,SRR2052494,SRR2052495,SRR2052496 -b $TARGET_REGIONS
