From b530407f5add839356717e375842ce264502382d Mon Sep 17 00:00:00 2001 From: Michael Lukowski Date: Tue, 13 Aug 2019 10:19:39 -0500 Subject: [PATCH] adding jupyter brain image --- jupyter-brain/Dockerfile | 97 ++++++++++ jupyter-brain/README.md | 8 + jupyter-brain/build.sh | 3 + jupyter-brain/docker_command_backup.txt | 2 + jupyter-brain/extract_subcortical.sh | 16 ++ jupyter-brain/extract_subfields.sh | 33 ++++ jupyter-brain/initialize_subDir.sh | 4 + jupyter-brain/license | 1 + jupyter-brain/outlierDetection.sh | 33 ++++ jupyter-brain/resources/custom.js | 3 + .../resources/jupyter_notebook_config.py | 6 + jupyter-brain/resources/templates/page.html | 180 ++++++++++++++++++ jupyter-brain/test.sh | 8 + 13 files changed, 394 insertions(+) create mode 100644 jupyter-brain/Dockerfile create mode 100644 jupyter-brain/README.md create mode 100755 jupyter-brain/build.sh create mode 100755 jupyter-brain/docker_command_backup.txt create mode 100755 jupyter-brain/extract_subcortical.sh create mode 100755 jupyter-brain/extract_subfields.sh create mode 100755 jupyter-brain/initialize_subDir.sh create mode 100644 jupyter-brain/license create mode 100755 jupyter-brain/outlierDetection.sh create mode 100644 jupyter-brain/resources/custom.js create mode 100644 jupyter-brain/resources/jupyter_notebook_config.py create mode 100755 jupyter-brain/resources/templates/page.html create mode 100755 jupyter-brain/test.sh diff --git a/jupyter-brain/Dockerfile b/jupyter-brain/Dockerfile new file mode 100644 index 00000000..ee828eaa --- /dev/null +++ b/jupyter-brain/Dockerfile @@ -0,0 +1,97 @@ +# Copyright (c) Jupyter Development Team. +# Distributed under the terms of the Modified BSD License. +FROM jupyter/scipy-notebook + +USER root + +# R pre-requisites +RUN apt-get update && \ + apt-get install -y --no-install-recommends \ + fonts-dejavu \ + tzdata \ + gfortran \ + gcc \ + libssl1.0.0 \ + libcurl4-openssl-dev \ + libssl-dev \ + zlib1g \ + zlib1g-dev && apt-get clean && \ + rm -rf /var/lib/apt/lists/* + +USER $NB_UID + +# R packages including IRKernel which gets installed globally. +RUN conda config --system --append channels r && \ + conda install --quiet --yes \ + 'rpy2' \ + 'r-base=3.4.3' \ + 'r-irkernel' \ + 'r-plyr' \ + 'r-devtools' \ + 'r-tidyverse' \ + 'r-shiny' \ + 'r-rmarkdown' \ + 'r-forecast' \ + 'r-rsqlite' \ + 'r-reshape2' \ + 'r-nycflights13' \ + 'r-caret' \ + 'r-rcurl' \ + 'r-crayon' \ + 'r-randomforest' \ + 'r-rmysql' \ + 'r-gplots' \ + 'r-gtools' \ + 'r-catools' \ + && conda clean -tipsy && \ + fix-permissions $CONDA_DIR && \ + fix-permissions /home/$NB_USER + + +RUN echo "load.lib<-c('httr','jsonlite','dplyr','gplots','ggplot2','ggpubr','RColorBrewer','xml2','repr','VennDiagram'); install.lib<-load.lib[!load.lib %in% installed.packages()]; install.packages(install.lib,dependencies=TRUE, repos='http://cran.us.r-project.org', type='source')" | R --no-save +RUN echo "source('https://www.bioconductor.org/biocLite.R'); load.s3<-c('maftools', 'phyloseq', 'DESeq2', 'dada2', 'vcfR'); install.s3<-load.s3[!load.s3 %in% installed.packages()]; for(s3 in install.s3) biocLite(s3);" | R --no-save + +USER root +COPY resources/templates/ /opt/conda/lib/python3.6/site-packages/notebook/templates +USER $NB_UID + +COPY resources/custom.js /home/$NB_USER/.jupyter/custom/ +COPY resources/jupyter_notebook_config.py /home/$NB_USER/.jupyter/tmp.py +RUN cat /home/$NB_USER/.jupyter/tmp.py >> /home/$NB_USER/.jupyter/jupyter_notebook_config.py && rm /home/$NB_USER/.jupyter/tmp.py + + +# this section is based off of: https://github.com/uc-cdis/bc-cwl/blob/master/brainCommonPipelineDocker4CWL/docker-freesurfer-master/Dockerfile +USER root + +COPY license /usr/local/freesurfer/license.txt + +ENV FREESURFER_HOME /usr/local/freesurfer +ENV FMRI_ANALYSIS_DIR /usr/local/freesurfer/fsfast +ENV FSFAST_HOME /usr/local/freesurfer/fsfast +ENV FUNCTIONALS_DIR /usr/local/freesurfer/sessions +ENV LOCAL_DIR /usr/local/freesurfer/local +ENV MINC_BIN_DIR /usr/local/freesurfer/mni/bin +ENV MINC_LIB_DIR /usr/local/freesurfer/mni/lib +ENV MNI_DATAPATH /usr/local/freesurfer/mni/data +ENV MNI_DIR /usr/local/freesurfer/mni +ENV MNI_PERL5LIB /usr/local/freesurfer/mni/share/perl5 +ENV PERL5LIB /usr/local/freesurfer/mni/share/perl5 +ENV SUBJECTS_DIR /usr/local/freesurfer/subjects +ENV PATH $PATH:/usr/local/freesurfer/bin:/usr/local/freesurfer/fsfast/bin:/usr/local/freesurfer/tktools:/usr/local/freesurfer/mni/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin + +ADD extract_subfields.sh /mnt/ +ADD initialize_subDir.sh /mnt/ +ADD extract_subcortical.sh /mnt/ +ADD outlierDetection.sh /mnt/ + +RUN apt-get update && apt-get install -y --no-install-recommends curl tar tcsh libglu1-mesa libgomp1 libjpeg62 libxext6 libxtst6 libxt6 libxt-dev libxmu6 \ + && curl ftp://surfer.nmr.mgh.harvard.edu/pub/dist/freesurfer/6.0.0/freesurfer-Linux-centos6_x86_64-stable-pub-v6.0.0.tar.gz | tar xvz -C /usr/local \ + && apt-get install -y --no-install-recommends jq bc libsys-hostname-long-perl && ldconfig && mkdir -p /N/u /N/home /N/dc2 /N/soft \ + && curl "https://surfer.nmr.mgh.harvard.edu/fswiki/MatlabRuntime?action=AttachFile&do=get&target=runtime2012bLinux.tar.gz" -o "/usr/local/freesurfer/runtime2012b.tar.gz" \ + && tar xf /usr/local/freesurfer/runtime2012b.tar.gz -C /usr/local/freesurfer/ \ + && /bin/rm /usr/local/freesurfer/runtime2012b.tar.gz \ + && apt-get remove -y curl \ + && rm -rf /var/lib/apt/lists/* + + +USER $NB_UID \ No newline at end of file diff --git a/jupyter-brain/README.md b/jupyter-brain/README.md new file mode 100644 index 00000000..2747b027 --- /dev/null +++ b/jupyter-brain/README.md @@ -0,0 +1,8 @@ +# TL;DR + +Jupyter notebook with freesurfer installed. + +## Notes + +Freesurfer is big (11 Gig), so this docker image is big, so the `quay` build fails, +so we for now just build locally, tag, and push to quay.io/cdis/jupyterbrain diff --git a/jupyter-brain/build.sh b/jupyter-brain/build.sh new file mode 100755 index 00000000..8fd871b6 --- /dev/null +++ b/jupyter-brain/build.sh @@ -0,0 +1,3 @@ +docker build -t brainlife/freesurfer . && \ + docker tag brainlife/freesurfer brainlife/freesurfer:6.0.0 && \ + docker push brainlife/freesurfer diff --git a/jupyter-brain/docker_command_backup.txt b/jupyter-brain/docker_command_backup.txt new file mode 100755 index 00000000..833bd2e9 --- /dev/null +++ b/jupyter-brain/docker_command_backup.txt @@ -0,0 +1,2 @@ +docker run -it --rm -v /Users/xingyankuang/Downloads/docker-freesurfer-master:/input brainlife/freesurfer mri_convert input/sub-01_T1w.nii.gz input/test.mgz +docker run -it --rm -v /Users/xingyankuang/Downloads/subjectDir:/usr/local/freesurfer/subjects brainlife/freesurfer recon-all -s subj10159 -hippocampal-subfields-T1 \ No newline at end of file diff --git a/jupyter-brain/extract_subcortical.sh b/jupyter-brain/extract_subcortical.sh new file mode 100755 index 00000000..b42f16cc --- /dev/null +++ b/jupyter-brain/extract_subcortical.sh @@ -0,0 +1,16 @@ +#!/bin/bash +cd $1 +outputF=$3 +subList=$2 +#bash +echo "SubjID,LLatVent,RLatVent,Lthal,Rthal,Lcaud,Rcaud,Lput,Rput,Lpal,Rpal,Lhippo,Rhippo,Lamyg,Ramyg,Laccumb,Raccumb,ICV" > $outputF +for sub_id in $(cat $subList); do +#for sub_id in `ls -d subj*`; do +printf "%s," "${sub_id}" >> $outputF +for x in Left-Lateral-Ventricle Right-Lateral-Ventricle Left-Thalamus-Proper Right-Thalamus-Proper Left-Caudate Right-Caudate Left-Putamen Right-Putamen Left-Pallidum Right-Pallidum Left-Hippocampus Right-Hippocampus Left-Amygdala Right-Amygdala Left-Accumbens-area Right-Accumbens-area; do +printf "%g," `grep ${x} ${sub_id}/stats/aseg.stats | awk '{print $4}'` >> $outputF +done +printf "%g" `cat ${sub_id}/stats/aseg.stats | grep IntraCranialVol | awk -F, '{print $4}'` >> $outputF +echo "" >> $outputF +done + diff --git a/jupyter-brain/extract_subfields.sh b/jupyter-brain/extract_subfields.sh new file mode 100755 index 00000000..661c1f5f --- /dev/null +++ b/jupyter-brain/extract_subfields.sh @@ -0,0 +1,33 @@ +#!/bin/bash +cd $1 +outputF=$3 +subList=$2 +echo 'SubjID,L_Hippocampal_tail,L_subiculum,L_CA1,L_hippocampal-fissure,L_presubiculum,L_parasubiculum,L_molecular_layer_HP,L_GC-ML-DG,L_CA3,L_CA4,L_fimbria,L_HATA,L_Whole_hippocampus,R_Hippocampal_tail,R_subiculum,R_CA1,R_hippocampal-fissure,R_presubiculum,R_parasubiculum,R_molecular_layer_HP,R_GC-ML-DG,R_CA3,R_CA4,R_fimbria,R_HATA,R_Whole_hippocampus,Lhippo,Rhippo,eTIV,Brain,TotalGM' > $outputF + +#for subj_id in $(ls -d 00*); do # approach A: create the list here, e. g. for rootname 'WG_' in this example +for subj_id in $(cat $subList); do # approach B: use an explicit list +printf "%s," "${subj_id}" >> $outputF + + + +for x in Hippocampal_tail subiculum CA1 hippocampal-fissure presubiculum parasubiculum molecular_layer_HP GC-ML-DG CA3 CA4 fimbria HATA Whole_hippocampus; do +printf "%g," `grep -w ${x} ${subj_id}/mri/lh.hippoSfVolumes-T1.v10.txt | awk '{print $2}'` >> $outputF +done + +for x in Hippocampal_tail subiculum CA1 hippocampal-fissure presubiculum parasubiculum molecular_layer_HP GC-ML-DG CA3 CA4 fimbria HATA Whole_hippocampus; do +printf "%g," `grep -w ${x} ${subj_id}/mri/rh.hippoSfVolumes-T1.v10.txt | awk '{print $2}'` >> $outputF +done + + +for x in Left-Hippocampus Right-Hippocampus; do +printf "%g," `grep ${x} ${subj_id}/stats/aseg.stats | awk '{print $4}'` >> $outputF +done + +printf "%g," `cat ${subj_id}/stats/aseg.stats | grep IntraCranialVol | awk -F, '{print $4}'` >> $outputF +printf "%g," `cat ${subj_id}/stats/aseg.stats | grep 'Brain Segmentation Volume,' | awk -F, '{print $4}'` >> $outputF +printf "%g" `cat ${subj_id}/stats/aseg.stats | grep 'Total gray matter volume' | awk -F, '{print $4}'` >> $outputF + +echo "" >> $outputF + +done +#/bin/bash extract_subfields.sh subjectDir_test list_subjects.txt hippo_subfield_volume diff --git a/jupyter-brain/initialize_subDir.sh b/jupyter-brain/initialize_subDir.sh new file mode 100755 index 00000000..b53f93f8 --- /dev/null +++ b/jupyter-brain/initialize_subDir.sh @@ -0,0 +1,4 @@ +#!/bin/bash +dir_name=$1 + +cp -r /usr/local/freesurfer/subjects/fsaverage $dir_name diff --git a/jupyter-brain/license b/jupyter-brain/license new file mode 100644 index 00000000..2399a5ab --- /dev/null +++ b/jupyter-brain/license @@ -0,0 +1 @@ +hayashis@iu.edu 29511 *CPmh9xvKQKHE FSg0ijTusqaQc diff --git a/jupyter-brain/outlierDetection.sh b/jupyter-brain/outlierDetection.sh new file mode 100755 index 00000000..a49886cb --- /dev/null +++ b/jupyter-brain/outlierDetection.sh @@ -0,0 +1,33 @@ +#!/bin/bash +summary_state=$1 +extracted_volumes=$2 +out=$3 +echo "Region,MNminus1.5IQI,MNplus1.5IQI" > IQI_range.csv +#sed '1d' $summary_state | grep -v "Assym" | grep -v "Avg_" | awk ' { print $1","$4-2.698*$5","$4+2.698*$5 } ' >> IQI_range.csv +sed '1d' $summary_state | grep -v "Assym" | grep -v "Avg_" | awk ' { print $1","$4-2.3*$5","$4+2.3*$5 } ' >> IQI_range.csv +N_regions=`cat $extracted_volumes | head -n 1 | awk -F "," ' { print NF } '` + +echo ' ' > $out +for (( c=2; c<=$N_regions; c++)) +do roi=`cat $extracted_volumes | head -n 1 | awk -F "," ' { print $'$c' } '` +IQI_low=`cat IQI_range.csv | grep $roi | awk -F "," ' { print $2 }'` +IQI_high=`cat IQI_range.csv | grep $roi | awk -F "," ' { print $3 }'` +IQI_low_int=`printf "%10.0f\n" $IQI_low` +IQI_high_int=`printf "%10.0f\n" $IQI_high` +echo "Looking for subjects with "$roi" volumes outside of "$IQI_low":"$IQI_high >> $out +cnt=1 +for textline in `sed '1d' $extracted_volumes | awk -F "," ' { print $'$c' } '` +do cnt=`echo $cnt+1 | bc ` +textline_int=`printf "%10.0f\n" $textline` +if [ $textline_int -gt $IQI_high_int ]; +then sid=`cat $extracted_volumes | head -n $cnt | tail -n 1 | awk -F "," ' { print $1 } '` +echo $sid" has a "$roi" of "$textline", this volume is high" >> $out +fi +if [ $textline_int -lt $IQI_low_int ]; +then sid=`cat $extracted_volumes | head -n $cnt | tail -n 1 | awk -F "," ' { print $1 } '` +echo $sid" has a "$roi" of "$textline", this volume is low" >> $out +fi +done +done +#cat $out | grep "has" | awk -F/ ' { print $NF } ' | awk ' { print $1 } '| sort | uniq + diff --git a/jupyter-brain/resources/custom.js b/jupyter-brain/resources/custom.js new file mode 100644 index 00000000..2f6d4c13 --- /dev/null +++ b/jupyter-brain/resources/custom.js @@ -0,0 +1,3 @@ +define(['base/js/namespace'], function(Jupyter){ + Jupyter._target = '_self'; +}) diff --git a/jupyter-brain/resources/jupyter_notebook_config.py b/jupyter-brain/resources/jupyter_notebook_config.py new file mode 100644 index 00000000..a202587d --- /dev/null +++ b/jupyter-brain/resources/jupyter_notebook_config.py @@ -0,0 +1,6 @@ +c.NotebookApp.tornado_settings = { + 'headers': { + 'Content-Security-Policy': + "frame-ancestors 'self'" + } +} diff --git a/jupyter-brain/resources/templates/page.html b/jupyter-brain/resources/templates/page.html new file mode 100755 index 00000000..cbb26256 --- /dev/null +++ b/jupyter-brain/resources/templates/page.html @@ -0,0 +1,180 @@ + + + + + + + {% block title %}Jupyter Notebook{% endblock %} + {% block favicon %}{% endblock %} + + + + + + {% block stylesheet %} + + {% endblock %} + + + + + + + + + {% block meta %} + {% endblock %} + + + + + + + + + +
+{% block site %} +{% endblock %} +
+ +{% block after_site %} +{% endblock %} + +{% block script %} +{% endblock %} + + + + + diff --git a/jupyter-brain/test.sh b/jupyter-brain/test.sh new file mode 100755 index 00000000..3770b35a --- /dev/null +++ b/jupyter-brain/test.sh @@ -0,0 +1,8 @@ + +#docker run -it --rm brainlife/freesurfer mri_convert testdata/test.nii.gz test.mgz + +#export FREESURFER_LICENSE="~/.config/freesurfer.license.txt" +#singularity exec -e docker://brainlife/freesurfer bash -c "cp $FREESURFER_LICENSE /usr/local/freesurfer/license.txt && mri_convert testdata/t1.nii.gz test.mgz" + +export FREESURFER_LICENSE="hayashis@iu.edu 29511 *CPmh9xvKQKHE FSg0ijTusqaQc" +singularity exec -e docker://brainlife/freesurfer:6.0.0 bash -c "echo $FREESURFER_LICENSE > /usr/local/freesurfer/license.txt && mri_convert testdata/t1.nii.gz test.mgz"