#################################################################
# Software Version: v0.0.16
# Description:      Differential Binding Analysis
# Website:          http://www.bioconductor.org/packages/release/bioc/html/DiffBind.html
# Provides:         diffbind
# Base Image:       r-base:4.3.1
# Build Cmd:        docker build --platform linux/amd64 --no-cache --rm -t biowardrobe2/diffbind:v0.0.16 -f diffbind-Dockerfile .
# Pull Cmd:         docker pull --platform linux/amd64 biowardrobe2/diffbind:v0.0.16
# Run Cmd:          docker run --platform linux/amd64 --rm -ti biowardrobe2/diffbind:v0.0.16 /bin/bash
#################################################################
#
# v0.0.16
# - Fixed bug in run_diffbind_manual.R
#   caused by applying --minoverlap even when we load
#   the data
# - added separate Datasets correlation plots for all
#   and consensus peaks
# - Updated base image to from 4.2.2 to 4.3.1
# - removed unused parameters in run_diffbind.R
# - made run_diffbind.R work again (wasn't properly
#   updated to work with the latest DiffBind > 3)
#
# v0.0.15
# - Updated run_diffbind_manual.R to build better plots
# - Changed to the way how --groupby and --minoverlap parameters work
#
# v0.0.14
# - Updated from r-base:3.6.0 to r-base:4.2.2
# - Added run_diffbind_manual.R
# - Use that version of BiocManager that corresponds to the R version
# - Need to use --break-system-packages for pip3 install
#   https://discuss.python.org/t/error-externally-managed-environment/24237
#
# v0.0.13
# - add --blockfile to set multiple groups for multi-factor analysis
#
# v0.0.12
# - export all graphics to pdf too
# - allow to filter out intervals with low raw read counts
#
# v0.0.11
# - add occupancy based consensus peak selection
#
# v0.0.10
# - suppress scientific notation when exporting to TSV
#
# v0.0.9
# - export not filtered TSV results
#
# v0.0.8
# - supports blocking analyses for DESeq2 and EdgeR
#
# v0.0.7
# - add tryCatch to all optional outputs
#
# v0.0.6
# - filtering by P-value or FDR
#
# v0.0.5
# - add P-value cutoff for reported results
#
# v0.0.4
# - increased default padding for generated heatmaps
#
# v0.0.3
# - allows to control threads number
#
# v0.0.2
#   - exports
#     * peak overlap correlation heatmap
#     * counts correlation heatmap
#     * correlation heatmap based on all normalized data
#     * correlation heatmap based on DB sites only
#     * PCA plot using affinity data for only differentially bound sites
#     * MA plot
#     * volcano plot
#     * box plots of read distributions for significantly differentially bound (DB) sites
#   - allows to choose from deseq2 or edger
#
# v0.0.1
#   Use run_diffbind.R script
#      - use DiffBind with default parameters
#      - use only condition option in comparison
#      - export results as TSV
#
#################################################################


### Base Image
FROM r-base:4.3.1
LABEL maintainer="misha.kotliar@gmail.com"
ENV DEBIAN_FRONTEND noninteractive

################## BEGIN INSTALLATION ######################

WORKDIR /tmp

COPY ./scripts/run_diffbind.R /usr/local/bin/run_diffbind.R
COPY ./scripts/run_diffbind_manual.R /usr/local/bin/run_diffbind_manual.R

### Installing dependencies
RUN apt-get update && \
    apt-get install -y vim pandoc gcc-10-base libgcc-10-dev cmake python3-dev python3-pip libxml2-dev \
                       libssl-dev libcurl4-openssl-dev libfontconfig1-dev libharfbuzz-dev libfribidi-dev \
                       libpng-dev libtiff5-dev libjpeg-dev file && \
    pip3 install --break-system-packages argparse && \
### Installing R packages
    R -e "install.packages('BiocManager')" && \
    R -e "BiocManager::install(version = '3.17', update=TRUE, ask=FALSE)" && \
    R -e "BiocManager::install('DiffBind')" && \
    R -e "BiocManager::install('hopach')" && \
    R -e "BiocManager::install('cmapR')" && \
    R -e "BiocManager::install('EnhancedVolcano')" && \
    R -e "BiocManager::install('Glimma')" && \
    R -e "BiocManager::install('profileplyr')" && \
    R -e 'install.packages("argparse", repo = "https://cloud.r-project.org/")' && \
    R -e 'install.packages("ggpubr", repo = "https://cloud.r-project.org/")' && \
    R -e 'install.packages("tidyverse", repo = "https://cloud.r-project.org/")' && \
### Installing scripts
    chmod +x /usr/local/bin/run_diffbind.R && \
    chmod +x /usr/local/bin/run_diffbind_manual.R && \
### Cleaning
    apt-get clean && \
    apt-get purge && \
    rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/* && \
    strip /usr/local/bin/*; true