FROM continuumio/anaconda3
MAINTAINER Kael Dai <kaeld@alleninstitute.org>

RUN apt-get update && apt-get install -y automake \
                                         libtool \
                                         build-essential \
                                         libncurses5-dev \
                                         git

ENV BUILD_DIR=/home/build
ENV HOME_DIR=/home/shared
ENV WORK_DIR=${HOME_DIR}/workspace
ENV PYTHON_VER=3.8
ENV PYTHON_ENV=python${PYTHON_VER}

ENV GIT_REPO=https://github.com/AllenInstitute/bmtk.git
ENV GIT_BRANCH=develop

RUN mkdir -p ${BUILD_DIR}
RUN mkdir -p ${HOME_DIR}
RUN mkdir -p ${WORK_DIR}

#RUN conda update -n base -c defaults conda \
#    && conda install python=3.6
RUN conda install -y numpy h5py lxml pandas matplotlib jsonschema scipy cmake anaconda mpi4py mpich-mpicc

### Install NEURON for BioNet
ENV NRN_VER=7.7
ENV NRN_INSTALL_DIR=${BUILD_DIR}/nrn

RUN conda uninstall --force realine ncurses
RUN cd ${BUILD_DIR} \
    && wget --quiet https://neuron.yale.edu/ftp/neuron/versions/v${NRN_VER}/nrn-${NRN_VER}.tar.gz -O nrn.tar.gz \
    && tar xfz nrn.tar.gz \
    && cd nrn-${NRN_VER} \
    && ./configure --prefix=${NRN_INSTALL_DIR} --without-x --with-nrnpython=$(which python) --without-memacs \
    && make \
    && make install \
    && cd src/nrnpython \
    && python setup.py install

ENV PATH=${NRN_INSTALL_DIR}/x86_64/bin:${PATH}
ENV LD_LIBRARY_PATH=${NRN_INSTALL_DIR}/x86_64/lib:${LD_LIBRARY_PATH}
ENV PYTHONPATH=${NRN_INSTALL_DIR}/lib/python:${PYTHONPATH}


### Install NEST for PointNet
ENV NEST_VER=2.20.0
ENV NEST_INSTALL_DIR=${BUILD_DIR}/nest/build

RUN cd ${BUILD_DIR} \
    && conda install -y gsl \
    && wget --quiet https://github.com/nest/nest-simulator/archive/v${NEST_VER}.tar.gz -O nest.tar.gz \
    && tar xfz nest.tar.gz \
    && cd nest-simulator-${NEST_VER} \
    && mkdir build && cd build \
    && cmake -DCMAKE_INSTALL_PREFIX=${NEST_INSTALL_DIR} -Dwith-gsl=ON -Dwith-python=ON -Dwith-ltdl=ON .. \
    && make \
    && make install

# Taken from /home/shared/nest/bin/nest_vars.sh, needed to run nest and pynest in jupyter
ENV NEST_DATA_DIR=${NEST_INSTALL_DIR}/share/nest
ENV NEST_DOC_DIR=${NEST_INSTALL_DIR}/share/doc/nest
ENV NEST_MODULE_PATH=${NEST_INSTALL_DIR}/lib/nest
ENV NEST_PYTHON_PREFIX=${NEST_INSTALL_DIR}/lib/${PYTHON_ENV}/site-packages
ENV PYTHONPATH=${NEST_PYTHON_PREFIX}:${PYTHONPATH}
ENV PATH=${NEST_INSTALL_DIR}/bin:${PATH}


### Install DiPDE for PopNet
RUN cd ${BUILD_DIR}; \
    git clone https://github.com/AllenInstitute/dipde.git dipde; \
    pwd; \
    cd dipde; \
    pip install -e .;


### Install AllenSDK (Not used by bmtk, but used by some notebooks to fetch cell-types files)
# RUN pip install allensdk


### Install the bmtk
RUN cd ${BUILD_DIR} \
#    && git clone --branch feature/modeling_tutorial https://github.com/kaeldai/bmtk.git \
    && git clone --branch ${GIT_BRANCH} ${GIT_REPO} \
    && cd bmtk \
    && python setup.py install

# Setup the examples and tutorials
RUN cd ${BUILD_DIR}/bmtk/docs; \
    cp -R examples ${HOME_DIR}; \
    cp -R tutorial ${HOME_DIR}

### Setup components directories for tutorials, including compiling neuron modfiles
RUN cd ${HOME_DIR}/tutorial; \
    cp -R ../examples/*_components .; \
    cd biophys_components/mechanisms; \
    nrnivmodl modfiles/

### Pre-compile mechanisms for BioNet examples
RUN cd ${HOME_DIR}/examples/biophys_components/mechanisms; \
    nrnivmodl modfiles/

### Pre-compile mechanisms for BioNet examples
# COPY ../docs/tutorial/modeling_tut_2021 ${HOME_DIR}/modeling_tut_2021

# RUN cd ${HOME_DIR}/tutorial/modeling_tut_2021
RUN cd ${HOME_DIR}/tutorial/modeling_tut_2021/components/mechanisms \
    && nrnivmodl modfiles/

# Create an entry point for running the image
COPY entry_script.sh ${BUILD_DIR}
RUN chmod +x ${BUILD_DIR}/entry_script.sh

ENTRYPOINT ["/home/build/entry_script.sh"]
