#################################################################
# Dockerfile
#
# Software:         BioWardrobe DESeq/DESeq2 script
# Software Version: v0.0.24
# Description:      Dockerized version of DESeq/DESeq2 script from BioWardrobe
# Website:          https://github.com/Barski-lab/workflows
# Provides:         run_deseq.R
#                   run_deseq_lrt.R
#                   get_gene_n_tss.R 
#                   BiocManager       1.30.10
#                   BiocParallel      1.22.0
#                   DESeq             1.39.0
#                   DESeq2            1.28.1
#                   argparse          latest
#                   pheatmap          latest
# Base Image:       r-base:4.0.4
# Build Cmd:        docker build --rm -t biowardrobe2/scidap-deseq:v0.0.24 -f scidap-deseq-Dockerfile .
# Pull Cmd:         docker pull biowardrobe2/scidap-deseq:v0.0.24
# Run Cmd:          docker run --rm -ti biowardrobe2/scidap-deseq:v0.0.24 /bin/bash
#################################################################
#
# v0.0.24
#
# - Fix bug with pval in DESeq and pvalue in DESeq2. Now all it pvalue
#
# v0.0.23
#
# - Use RpkmCondition1 and RpkmCondition2 for RPKM columns in the output TSV file
#   We need hardcoded values for later filtering.
#
# v0.0.22
#
# - Column names for RPKM don't include spaces and brackets anymore
# - use r-base:4.0.4
# - Need to downgrade to Bioconductor 3.11, as DESeq is not available
#   in the newer versions
#
# v0.0.21
#
# - Add ggrepel for proper label positioning
#
# v0.0.20
#
# - Add --batchfile parameter to run_deseq.R to compensate batch effect
#
# v0.0.19
#
# - Update run_deseq.R (sets precision with --digits parameter)
#
# v0.0.18
#
# - Fix bug in get_gene_n_tss.R: works good for duplucate gene names
#
# v0.0.17
#
# - Update labels in cls, replaces n/a with na in gct files
#
# v0.0.16
#
# - Add max(rpkm) cutoff filtering
#
# v0.0.15
#
# - fix bug with " and ' in arguments. Replace all with ""
#
# v0.0.14
#
# - add PCA plot
#
# v0.0.13
#
# - Fix bug in phenotype.cls column order
# - Fix bug in logFC sign for DESeq2
#
# v0.0.12
#
# - Update run_deseq.R (v0.0.8)
#   * Export CLS file to be used by GSEA
#
# v0.0.11
#
# - Update run_deseq.R (v0.0.7)
#   * Use -ua and -ta to set aliases for input expression files. Should be unique
#   * Export GCT file to be used by GSEA
#
# v0.0.10
#
# - add run_deseq_lrt.R (v0.0.1)
#   * Run DeSeq2 for multi-factor analysis using LRT (likelihood ratio or chi-squared test)
#
# v0.0.9
#
# - update run_deseq.R (v0.0.6)
#   * proper way to set custom column names for outputs
#
# v0.0.8
#   switch to r-base:3.6.0
#   install
#     BiocManager   1.30.4
#     BiocParallel  1.18.0
#     DESeq         1.36.0
#     DESeq2        1.24.0
#     argparse      latest
#     pheatmap      latest
#
# v0.0.7
# - update run_deseq.R (v0.0.5)
#   * add -LOG10(pval) and -LOG10(padj) columns
#
# v0.0.6
# - update run_deseq.R (v0.0.4)
#   * generate heatmap to show the expression data of the 30 most highly expressed genes
#   * build plot to show the log2 fold changes attributable to a given variable over the mean of normalized counts for all the samples
#
# v0.0.5
# Update get_gene_n_tss.R to v0.0.2.
# If arguments --gene and --tss are not provided, generate default values
# based on the --isoforms input
#
# v0.0.4
# - fix bug when loading TSV files by run_deseq.R (v0.0.3) script
# - support loading both *.csv and *.tsv files
#   file type is identified from file's extension,
#   by default csv
#
# v0.0.3
# - add get_gene_n_tss.R script to group isoforms CSV file by
#   gene and common tss
#
# v0.0.2
# - the output file's rows order corresponds to the rows order of
#   the first CSV file in the untreated group
# - DESeq/DESeq2 always compares untreated_vs_treated groups
# - fixed sign for log2FoldChange column
#################################################################


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

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

WORKDIR /tmp

ENV VERSION_BIOCMANAGER 1.30.10
ENV URL_BIOCMANAGER "https://cran.r-project.org/src/contrib/BiocManager_${VERSION_BIOCMANAGER}.tar.gz"

COPY ./scripts/install_from_source.R /tmp/install_from_source.R
COPY ./scripts/run_deseq.R /usr/local/bin/run_deseq.R
COPY ./scripts/run_deseq_lrt.R /usr/local/bin/run_deseq_lrt.R
COPY ./scripts/get_gene_n_tss.R /usr/local/bin/get_gene_n_tss.R

### Installing python3, pip3 and argparse
RUN apt-get update && \
    apt-get install -y gcc-10-base libgcc-10-dev python3-dev python3-pip libxml2-dev libssl-dev libcurl4-openssl-dev && \
    pip3 install argparse && \
### Installing biocmanager, biocparallel, deseq, deseq2, argparse, pheatmap
    Rscript /tmp/install_from_source.R "${URL_BIOCMANAGER}" && \
    R -e "BiocManager::install(version = '3.11', update=TRUE, ask=FALSE)" && \
    R -e "BiocManager::install(c('BiocParallel', 'DESeq', 'DESeq2', 'limma'), update=TRUE, ask=FALSE)" && \
    R -e 'install.packages("argparse", repo = "https://cloud.r-project.org/")' && \
    R -e 'install.packages("data.table", repo = "https://cloud.r-project.org/")' && \
    R -e 'install.packages("pheatmap", repo = "https://cloud.r-project.org/")' && \
    R -e 'install.packages("ggrepel", repo = "https://cloud.r-project.org/")' && \
### Installing run_deseq.R, get_gene_n_tss.R and run_deseq_lrt.R scripts
    chmod +x /usr/local/bin/run_deseq.R && \
    chmod +x /usr/local/bin/run_deseq_lrt.R && \
    chmod +x /usr/local/bin/get_gene_n_tss.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