FROM nvidia/cuda:11.4.3-cudnn8-runtime-ubuntu18.04

LABEL authors="Athanasios Baltzis, Jose Espinosa-Carrasco, Leila Mansouri" \
    title="nfcore/proteinfold_alphafold2_split" \
    Version="1.1.0" \
    description="Docker image containing all software requirements to run the RUN_ALPHAFOLD2_PRED module using the nf-core/proteinfold pipeline"

# Use bash to support string substitution.
SHELL ["/bin/bash", "-o", "pipefail", "-c"]

# Add env variables
ENV LD_LIBRARY_PATH="/conda/lib:/usr/local/cuda-11.4/lib64:$LD_LIBRARY_PATH"
ENV PATH="/conda/bin:$PATH"

RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys A4B469963BF863CC
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y \
    build-essential \
    cmake \
    cuda-command-line-tools-11-1 \
    git \
    hmmer \
    kalign \
    tzdata \
    wget \
    && rm -rf /var/lib/apt/lists/* \
    && apt-get autoremove -y \
    && apt-get clean

# Clone AlphaFold2
RUN git clone https://github.com/cbcrg/alphafold.git /app/alphafold && \
    cd /app/alphafold && \
    git checkout 1b3170e9409472ec8ad044f9935c92bedd7b4674 && \
    cd -

# Compile HHsuite from source
RUN git clone --branch v3.3.0 https://github.com/soedinglab/hh-suite.git /tmp/hh-suite \
    && mkdir /tmp/hh-suite/build \
    &&  cd /tmp/hh-suite/build \
    &&  cmake -DCMAKE_INSTALL_PREFIX=/opt/hhsuite -DHAVE_AVX2=1 .. \
    && make -j 4 && make install \
    && ln -s /opt/hhsuite/bin/* /usr/bin \
    && cd - && rm -rf /tmp/hh-suite

# Install Miniconda package manager
RUN wget -q -P /tmp \
    https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh \
    && bash /tmp/Miniconda3-latest-Linux-x86_64.sh -b -p /conda \
    && rm /tmp/Miniconda3-latest-Linux-x86_64.sh

# Install conda packages
RUN /conda/bin/conda install -y -c conda-forge \
    openmm=7.7.0 \
    cudatoolkit==11.1.1 \
    pdbfixer \
    pip \
    python=3.10 \
    && conda clean --all --force-pkgs-dirs --yes

RUN wget -q -P /app/alphafold/alphafold/common/ \
    https://git.scicore.unibas.ch/schwede/openstructure/-/raw/7102c63615b64735c4941278d92b554ec94415f8/modules/mol/alg/src/stereo_chemical_props.txt

# Install pip packages.
RUN pip3 install --upgrade pip --no-cache-dir \
    && pip3 install -r /app/alphafold/requirements.txt --no-cache-dir \
    && pip3 install --upgrade --no-cache-dir \
    jax==0.3.25 \
    jaxlib==0.3.25+cuda11.cudnn805 \
    -f https://storage.googleapis.com/jax-releases/jax_cuda_releases.html

RUN sed -i "s|alphafold/common/stereo_chemical_props.txt|/app/alphafold/alphafold/common/stereo_chemical_props.txt|g" /app/alphafold/alphafold/common/residue_constants.py

# Add SETUID bit to the ldconfig binary so that non-root users can run it.
RUN chmod u+s /sbin/ldconfig.real

# We need to run `ldconfig` first to ensure GPUs are visible, due to some quirk
# with Debian. See https://github.com/NVIDIA/nvidia-docker/issues/1399 for
# details.
RUN cd /app/alphafold
RUN ldconfig
