# Start with Ubuntu base
FROM ubuntu:22.04

USER root
SHELL ["/bin/bash", "-c"]

# Set environment variables
ENV DEBIAN_FRONTEND=noninteractive \
    TZ=Etc/UTC \
    PATH="/opt/conda/bin:/opt/conda/condabin:/opt/conda/envs/ecotyper/bin:$PATH" \
    R_LIBS="/opt/conda/envs/ecotyper/lib/R/library:/ecotyper/R/library" \
    ECOTYPER_HOME="/ecotyper"

# Install system dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
    wget \
    ca-certificates \
    git \
    libcurl4-openssl-dev \
    libssl-dev \
    libxml2-dev \
    build-essential \
    r-base \
    r-base-dev \
    libcairo2-dev \
    libxt-dev \
    libgraphicsmagick1-dev \
    default-jdk \
    xvfb \
    xauth \
    xfonts-base \
    && rm -rf /var/lib/apt/lists/*

# Set up R environment
RUN echo "options(repos = c(CRAN = 'https://cloud.r-project.org'))" > /root/.Rprofile

# Install Conda
RUN wget --quiet https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh && \
    mkdir -p /opt && \
    bash miniconda.sh -b -p /opt/conda && \
    rm miniconda.sh && \
    ln -s /opt/conda/etc/profile.d/conda.sh /etc/profile.d/conda.sh && \
    echo ". /opt/conda/etc/profile.d/conda.sh" >> ~/.bashrc && \
    echo "conda activate base" >> ~/.bashrc && \
    find /opt/conda/ -follow -type f -name '*.a' -delete && \
    find /opt/conda/ -follow -type f -name '*.js.map' -delete && \
    /opt/conda/bin/conda clean -afy

ENV PATH=/opt/conda/bin:$PATH

# Update conda and create environment
RUN conda update -y -n base -c defaults conda && \
    conda create -n ecotyper -c conda-forge -c bioconda \
    python=3.8 \
    r-base=4.1.3 \
    r-matrix \
    r-mass \
    r-nmf \
    r-rcpp \
    r-data.table \
    r-reshape2 \
    r-plyr \
    r-stringr \
    r-ggplot2 \
    r-rcolorbrewer \
    r-circlize \
    r-cowplot \
    r-viridis \
    r-gridextra \
    r-ggpubr \
    r-cluster \
    r-matrixtests \
    r-doparallel \
    r-foreach \
    r-optparse \
    r-argparse \
    r-config \
    r-colorspace \
    r-rjson \
    bioconductor-biobase \
    bioconductor-complexheatmap \
    bioconductor-genomicranges \
    -y && \
    conda clean -afy

# Clone EcoTyper and setup directory structure
RUN git clone --depth 1 https://github.com/digitalcytometry/ecotyper.git /ecotyper && \
    cd /ecotyper && \
    mkdir -p EcoTyper/Carcinoma/Carcinoma_Fractions/Analysis/rank_selection && \
    mkdir -p EcoTyper/Lymphoma/Lymphoma_Fractions/Analysis/rank_selection && \
    mkdir -p EcoTyper/Carcinoma/Carcinoma_Fractions/Cell_States/recovery && \
    mkdir -p EcoTyper/Carcinoma/Carcinoma_Fractions/Ecotypes/recovery && \
    mkdir -p EcoTyper/Lymphoma/Lymphoma_Fractions/Cell_States/recovery && \
    mkdir -p EcoTyper/Lymphoma/Lymphoma_Fractions/Ecotypes/recovery && \
    chmod -R +x pipeline/*.R && \
    chmod -R +x *.R

# Setup EcoTyper permissions
RUN cd /ecotyper && \
    ln -sf /ecotyper/pipeline pipeline && \
    chmod -R 755 /ecotyper/EcoTyper && \
    chmod -R 755 /ecotyper/pipeline

# Setup CIBERSORTx
RUN mkdir -p /src
WORKDIR /src

# Copy CIBERSORTx files
COPY ./CIBERSORTx-fractionshires/ /src/

# Install R dependencies for CIBERSORTx
RUN R -e "if (!require('BiocManager', quietly=TRUE)) install.packages('BiocManager', repos='https://cloud.r-project.org/')" && \
    Rscript /src/install_R_dependencies.R

# Add src to PATH
ENV PATH="/src:${PATH}"

# Setup final environment
RUN echo ". /opt/conda/etc/profile.d/conda.sh" >> ~/.bashrc && \
    echo "conda activate ecotyper" >> ~/.bashrc

#Add pandas 
RUN conda install -n ecotyper anaconda::pandas

# Set default command
CMD ["/bin/bash"]
