FROM ubuntu:18.04

ARG DEBIAN_FRONTEND=noninteractive

ENV TERM linux

RUN echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections

RUN apt-get update && \
    apt-get -y --no-install-recommends install gawk && \
    update-alternatives --install /usr/bin/awk awk /usr/bin/gawk 9999 && \
    rm -rf /var/lib/apt/lists/*

RUN apt-get update && \
    apt-get purge openjdk-\* icedtea-\* icedtea6-\* && \
    apt-get remove openjdk-11-jre openjdk-11-jdk openjdk-11-jre-headless openjdk-11-jdk-headless && \
    apt-get purge openjdk-* && \
    apt-get install -y --no-install-recommends openjdk-8-jdk && \
    update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 9999 && \
    rm -rf /var/lib/apt/lists/*

# Install Packages
RUN apt-get update && \
    apt-get install -y --no-install-recommends apt-utils && \
    apt-get install -y --no-install-recommends \
    git  \
    vim \
    wget \
    sudo \
    openssh-server \
    openssh-client && \
    yes yes | ssh-keygen -f /root/.ssh/id_rsa -t rsa -N '' > /dev/null && \
    cat /root/.ssh/id_rsa.pub > /root/.ssh/authorized_keys && \
    git config --global core.compression 9 && \
# =============================================================================
# Dependencies for building COMPSs
# =============================================================================
# Build dependencies
    sudo apt-get install -y --no-install-recommends maven \
# Runtime dependencies
    openjdk-8-jdk graphviz xdg-utils \
# Bindings-common-dependencies
    libtool automake build-essential \
# C-binding dependencies
    libboost-all-dev libxml2-dev csh \
# Extrae dependencies
    libxml2 gfortran libpapi-dev papi-tools \
# Misc. dependencies
    openmpi-bin openmpi-doc libopenmpi-dev uuid-runtime curl bc \
# Python-binding dependencies
    python-dev python3-dev libpython2.7 python-pip python3-pip python-setuptools python3-setuptools && \
    pip2 install wheel && \
    pip3 install wheel && \
    pip2 install wheel numpy==1.15.4 dill guppy decorator mpi4py==1.3.1 && \
    pip3 install wheel numpy==1.15.4 dill decorator mpi4py==3.0.1 && \
# Python-redis dependencies
    pip2 install redis==2.10.6 redis-py-cluster && \
    pip3 install redis==2.10.6 redis-py-cluster && \
# pycompsslib dependencies
    pip2 install scipy==1.0.0 scikit-learn==0.19.1 pandas==0.23.1 && \
    pip3 install scipy==1.0.0 scikit-learn==0.19.1 pandas==0.23.1 && \
# Streaming dependencies
    apt-get install -y --no-install-recommends unzip && \
    wget https://services.gradle.org/distributions/gradle-5.4.1-bin.zip && \
    unzip -d /opt gradle-5.4.1-bin.zip && \
    rm gradle-5.4.1-bin.zip && \
    ln -s /opt/gradle-5.4.1 /opt/gradle && \
    echo "GRADLE_HOME=/opt/gradle" >> /etc/environment && \
# Testing dependencies
    pip3 install enum34 tabulate && \
# Jupyter dependencies
    pip2 install jupyter ipykernel && \
    python2 -m ipykernel install && \
    pip3 install jupyter ipykernel && \
    python3 -m ipykernel install && \
# Configure user environment
# =============================================================================
# System configuration
# =============================================================================
# Add environment variables
    echo "JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/" >> /etc/environment && \
    echo "MPI_HOME=/usr/lib/openmpi" >> /etc/environment && \
    #echo "LD_LIBRARY_PATH=/usr/lib/openmpi/lib" >> /etc/environment && \
    echo "LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu/openmpi/include/:${LD_LIBRARY_PATH}" >> /etc/environment && \
    mkdir /run/sshd && \
    rm -rf /var/lib/apt/lists/*

RUN rm -rf ./framework && \
    export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64/" && \
    export MPI_HOME="/usr/lib/openmpi" && \
    export LD_LIBRARY_PATH="/usr/lib/x86_64-linux-gnu/openmpi/include/:${LD_LIBRARY_PATH}" && \
    #export LD_LIBRARY_PATH="/usr/lib/openmpi/lib" && \
    #git clone --branch "guidance" https://github.com/bsc-wdc/compss.git framework && \
    git clone --branch "guidance" https://github.com/ramonamela/compss.git framework && \
    #git clone --branch "fifo_lifo_schedulers" http://ramela@compss.bsc.es/gitlab/compss/framework.git framework && \
    cd ./framework && \
    ./submodules_get.sh && \
    ./submodules_patch.sh && \
    echo "${JAVA_HOME}" && \
    sudo /framework/builders/buildlocal -A -M /opt/COMPSs && \
    rm -rf /root/.cache && \
    cd .. && \
    rm -r ./framework
  
WORKDIR /TOOLS

RUN export DEBIAN_FRONTEND=noninteractive && \
#    sudo add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran35/' && \
#    sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E084DAB9 && \
    sudo apt-get update && sudo apt-get install -y --no-install-recommends gnupg2 software-properties-common && \
    sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9 && \
    sudo add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran35/' && \
    sudo apt-get update && DEBIAN_FRONTEND=noninteractive sudo apt-get install -y --no-install-recommends apt-utils && \
    sudo sed -i 's/^mesg n$/tty -s \&\& mesg n/g' /root/.profile && \
    DEBIAN_FRONTEND=noninteractive sudo apt-get install -y --no-install-recommends r-base r-base-dev r-base-core libcurl4-openssl-dev \
    jags libpq-dev libmariadb-client-lgpl-dev && \
    rm -rf /var/lib/apt/lists/*

#RUN /usr/bin/Rscript /TOOLS/deps.R

#Copy binaries into the container
RUN mkdir /TOOLS
#COPY ./TOOLS/shapeit.v2.r727.linux.x64 /TOOLS/shapeit.v2.r727.linux.x64
COPY ./TOOLS/R_scripts /TOOLS/R_scripts
COPY ./TOOLS/deps.R /TOOLS/deps.R
RUN chmod 775 /TOOLS/R_scripts/*
#RUN chmod 775 /TOOLS/shapeit.v2.r727.linux.x64

WORKDIR /TOOLS

######### IN CASE WE WANT TO UPGRADE QCTOOL #########

#Install mercurial (for QCTool)
#RUN apt-get install -y mercurial

#Install qctoolNew
#RUN hg clone -r ba5eaa4 https://gavinband@bitbucket.org/gavinband/qctool qctool_2.0 && \
#    cd qctool_2.0 && \
#    ./waf-1.5.18 configure && \
#    ./waf-1.5.18 && \
#    ln -s /TOOLS/build/release/qctool_v2.0.1 /usr/bin/qctool2.0

######### END OF QCTOOL INSTALACTION ################

#Install qctool
RUN wget http://www.well.ox.ac.uk/~gav/resources/archive/qctool_v1.4-linux-x86_64.tgz && \
    tar zxvf qctool_v1.4-linux-x86_64.tgz && \
    rm qctool_v1.4-linux-x86_64.tgz && \
    chmod -R 755 /TOOLS/qctool_v1.4-linux-x86_64/ && \
    ln -s /TOOLS/qctool_v1.4-linux-x86_64/qctool /usr/bin/qctool1.4

#bcftools and samtools dependencies
RUN sudo apt-get update && \
    sudo apt-get install -y --no-install-recommends zlib1g-dev libbz2-dev liblzma-dev libncurses5-dev libncursesw5-dev && \
    rm -rf /var/lib/apt/lists/*

#Install bcftools
RUN wget https://github.com/samtools/bcftools/releases/download/1.8/bcftools-1.8.tar.bz2 -O bcftools.tar.bz2 && \
    tar -xjvf bcftools.tar.bz2 && \
    rm bcftools.tar.bz2 && \
    cd bcftools-1.8 && \
    make && \
    make prefix=/usr/local/ install && \
    ln -s /usr/local/bin/bcftools /usr/bin/bcftools

#Install samtools
RUN wget https://github.com/samtools/samtools/releases/download/1.5/samtools-1.5.tar.bz2 -O samtools.tar.bz2 && \
    tar -xjvf samtools.tar.bz2 && \
    rm samtools.tar.bz2 && \
    cd samtools-1.5 && \
    make && \
    make prefix=/usr/local/ install && \
    ln -s /usr/local/bin/samtools /usr/bin/samtools

#Install htslib
RUN wget https://github.com/samtools/htslib/releases/download/1.9/htslib-1.9.tar.bz2 -O htslib-1.9.tar.bz2 && \
    tar -xjvf htslib-1.9.tar.bz2 && \
    rm htslib-1.9.tar.bz2 && \
    cd htslib-1.9 && \
    make && \
    make prefix=//usr/local/ install && \
    ln -s /usr/local/bin/bgzip /usr/bin/bgzip && \
    ln -s /usr/local/bin/tabix /usr/bin/tabix

#Plink dependencies
RUN sudo add-apt-repository universe && \
    sudo apt-get update && \
    sudo apt-get install -y --no-install-recommends libatlas-base-dev libblas-dev liblapack-dev libatlas-base-dev && \
    rm -rf /var/lib/apt/lists/*

#Install plink
RUN git clone https://github.com/chrchang/plink-ng.git && \
    cd plink-ng && \
    rm -r 2.0 && \
    cd 1.9 && \
    ./plink_first_compile && \
    ln -s /TOOLS/plink-ng/1.9/plink /usr/bin/plink

#Install Eagle
#RUN wget https://data.broadinstitute.org/alkesgroup/Eagle/downloads/old/Eagle_v2.3.tar.gz && \
#    tar -zxvf Eagle_v2.3.tar.gz && \
#    rm Eagle_v2.3.tar.gz && \
#    rm -r Eagle_v2.3/example/ && \
#    ln -s /TOOLS/Eagle_v2.3/eagle /usr/bin/eagle
RUN wget https://data.broadinstitute.org/alkesgroup/Eagle/downloads/old/Eagle_v2.4.tar.gz && \
    tar -zxvf Eagle_v2.4.tar.gz && \
    rm -r Eagle_v2.4/example/ && \
    rm Eagle_v2.4.tar.gz && \
    ln -s /TOOLS/Eagle_v2.4/eagle /usr/bin/eagle

#Install Impute ### This step will stop working once they upgrade the program since only the last version is available
RUN wget https://mathgen.stats.ox.ac.uk/impute/impute_v2.3.2_x86_64_static.tgz && \
    tar -zxvf impute_v2.3.2_x86_64_static.tgz && \
    rm impute_v2.3.2_x86_64_static.tgz && \
    rm -r impute_v2.3.2_x86_64_static/Example/ && \
    ln -s /TOOLS/impute_v2.3.2_x86_64_static/impute2 /usr/bin/impute2

#Install snptest
RUN wget http://www.well.ox.ac.uk/~gav/resources/archive/snptest_v2.5_linux_x86_64_static.tgz && \
    tar -zxvf snptest_v2.5_linux_x86_64_static.tgz && \
    rm snptest_v2.5_linux_x86_64_static.tgz && \
    rm -r snptest_v2.5_linux_x86_64_static/example/ && \
    chmod -R 755 /TOOLS/snptest_v2.5_linux_x86_64_static/ && \
    ln -s /TOOLS/snptest_v2.5_linux_x86_64_static/snptest_v2.5 /usr/bin/snptest_v2.5

RUN wget https://mathgen.stats.ox.ac.uk/genetics_software/shapeit/shapeit.v2.r904.glibcv2.12.linux.tar.gz && \
    tar -zxvf shapeit.v2.r904.glibcv2.12.linux.tar.gz && \
    rm shapeit.v2.r904.glibcv2.12.linux.tar.gz  && \
    rm -r shapeit.v2.904.2.6.32-696.18.7.el6.x86_64/example/ && \
    chmod -R 775 /TOOLS/shapeit.v2.904.2.6.32-696.18.7.el6.x86_64/ && \
    ln -s /TOOLS/shapeit.v2.904.2.6.32-696.18.7.el6.x86_64/bin/shapeit /usr/bin/shapeit

#RUN wget http://www.well.ox.ac.uk/~gav/resources/snptest_v2.5.2_linux_x86_64_static.tgz && \
#    tar -zxvf snptest_v2.5.2_linux_x86_64_static.tgz && \
#    rm snptest_v2.5.2_linux_x86_64_static.tgz && \
#    rm -r snptest_v2.5.2_linux_x86_64_static/example/ && \
#    chmod -R 755 /TOOLS/snptest_v2.5.2_linux_x86_64_static/ && \
#    ln -s /TOOLS/snptest_v2.5.2_linux_x86_64_static/snptest_v2.5.2 /usr/bin/snptest_v2.5.2

#Install minimac3
#RUN git clone https://github.com/Santy-8128/Minimac3.git && \
#    cd Minimac3 && \
#    make -w && \
#    sudo ln -s /TOOLS/Minimac3/bin/Minimac3 /usr/bin/minimac3
RUN wget ftp://share.sph.umich.edu/minimac3/Minimac3Executable.tar.gz && \
    tar -zxvf Minimac3Executable.tar.gz && \
    rm Minimac3Executable.tar.gz && \
    chmod -R 755 /TOOLS/Minimac3Executable/bin && \
    ln -s /TOOLS/Minimac3Executable/bin/Minimac3-omp

#Minimac4 dependencies
RUN sudo apt-get update && \
    sudo apt-get install -y --no-install-recommends cmake python-pip python-dev && \
    pip install cget 

#Install minimac4
RUN git clone https://github.com/Santy-8128/Minimac4.git && \
    cd Minimac4 && \
    bash install.sh && \
    ln -s /TOOLS/Minimac4/release-build/minimac4 /usr/bin/minimac4
#    sudo ln -s /TOOLS/Minimac3/bin/Minimac3-omp /usr/bin/minimac3
#    sudo ln -s /TOOLS/Minimac3/bin/Minimac3 /usr/bin/minimac3

RUN apt-get update && \
#    update-alternatives --remove "java" "/usr/lib/jvm/java-11-openjdk-amd64/bin/java" && \
#    update-alternatives --remove "java" "/usr/lib/jvm/java-11-openjdk-amd64/bin/javac" && \
#    update-alternatives --remove "java" "/usr/lib/jvm/java-11-openjdk-amd64/bin/javaws" && \
#    rm -rf /usr/lib/jvm/java-1.11.0-openjdk-amd64 && \
#    apt-get purge openjdk-8-jre openjdk-8-jre-headless openjdk-7-jre gcj-4.7-base gcj-4.7-jre openjdk-6-jre-headless && \
#    apt-get remove openjdk* && \
#    apt-get remove --auto-remove openjdk* && \
#    apt-get purge openjdk* && \
#    apt-get purge --auto-remove openjdk* && \
    apt-get autoremove openjdk-11-jre openjdk-11-jdk

RUN /usr/bin/Rscript /TOOLS/deps.R

RUN ln -s /usr/lib/jvm/java-8-openjdk-amd64 /usr/lib/jvm/default-java

ENV LC_ALL "C"
#ENV PLINKBINARY "/TOOLS/plink_1.9/plink"
#ENV EAGLEBINARY "/TOOLS/Eagle_v2.3/eagle"
#ENV IMPUTE2BINARY "/TOOLS/impute_v2.3.2_x86_64_static/impute2"
#ENV QCTOOLBINARY "/TOOLS/qctool_v1.4-linux-x86_64/qctool
#ENV SNPTESTBINARY "/TOOLS/snptest_v2.5_linux_x86_64_static/snptest_v2.5"
#ENV MINIMACBINARY "/TOOLS/Minimac3/bin/Minimac3"
ENV RSCRIPTDIR "/TOOLS/R_scripts/"
#ENV SHAPEITBINARY "/TOOLS/shapeit.v2.r727.linux.x64"
#ENV MINIMACBINARY "/TOOLS/Minimac3/bin/Minimac3"

ENV SHAPEITBINARY "/usr/bin/shapeit"
ENV PLINKBINARY "/usr/bin/plink"
ENV QCTOOLBINARY "/usr/bin/qctool1.4"
ENV EAGLEBINARY "/usr/bin/eagle"
ENV IMPUTE2BINARY "/usr/bin/impute2"
ENV QCTOOLBINARY "/usr/bin/qctool1.4"
ENV SNPTESTBINARY "/usr/bin/snptest_v2.5"
#ENV SNPTESTBINARY "/usr/bin/snptest_v2.5.2"
ENV MINIMAC3BINARY "/usr/bin/minimac3"
ENV MINIMAC4BINARY "/usr/bin/minimac4"
ENV TABIXBINARY "/usr/bin/tabix"
ENV BGZIPBINARY "/usr/bin/bgzip"

ENV RSCRIPTBINDIR "/usr/bin/"

ENV BCFTOOLSBINARY "/usr/bin/bcftools"
#ENV QCTOOLSNEWBINARY "/gpfs/scratch/pr1ees00/pr1ees14/GCAT/SHAPEIT_IMPUTE/qctool/build/release/qctool_v2.0-rc9"
#ENV QCTOOLSNEWBINARY "/usr/bin/qctool2.0" ## THE INSTALLATION IS NOT PERFORMED BECAUSE THIS BINARY IS NOT USED IN THE CODE
ENV SAMTOOLSBINARY "/usr/bin/samtools"
