Skip to content

Commit

Permalink
adding jupyter brain image
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelLukowski committed Aug 13, 2019
1 parent 174c22f commit b530407
Show file tree
Hide file tree
Showing 13 changed files with 394 additions and 0 deletions.
97 changes: 97 additions & 0 deletions jupyter-brain/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions jupyter-brain/README.md
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions jupyter-brain/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
docker build -t brainlife/freesurfer . && \
docker tag brainlife/freesurfer brainlife/freesurfer:6.0.0 && \
docker push brainlife/freesurfer
2 changes: 2 additions & 0 deletions jupyter-brain/docker_command_backup.txt
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions jupyter-brain/extract_subcortical.sh
Original file line number Diff line number Diff line change
@@ -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

33 changes: 33 additions & 0 deletions jupyter-brain/extract_subfields.sh
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions jupyter-brain/initialize_subDir.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
dir_name=$1

cp -r /usr/local/freesurfer/subjects/fsaverage $dir_name
1 change: 1 addition & 0 deletions jupyter-brain/license
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[email protected] 29511 *CPmh9xvKQKHE FSg0ijTusqaQc
33 changes: 33 additions & 0 deletions jupyter-brain/outlierDetection.sh
Original file line number Diff line number Diff line change
@@ -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

3 changes: 3 additions & 0 deletions jupyter-brain/resources/custom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
define(['base/js/namespace'], function(Jupyter){
Jupyter._target = '_self';
})
6 changes: 6 additions & 0 deletions jupyter-brain/resources/jupyter_notebook_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
c.NotebookApp.tornado_settings = {
'headers': {
'Content-Security-Policy':
"frame-ancestors 'self'"
}
}
180 changes: 180 additions & 0 deletions jupyter-brain/resources/templates/page.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
<!DOCTYPE HTML>
<html>

<head>
<meta charset="utf-8">

<title>{% block title %}Jupyter Notebook{% endblock %}</title>
{% block favicon %}<link id="favicon" rel="shortcut icon" type="image/x-icon" href="{{static_url("base/images/favicon.ico") }}">{% endblock %}
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<link rel="stylesheet" href="{{static_url("components/jquery-ui/themes/smoothness/jquery-ui.min.css") }}" type="text/css" />
<link rel="stylesheet" href="{{static_url("components/jquery-typeahead/dist/jquery.typeahead.min.css") }}" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">

{% block stylesheet %}
<link rel="stylesheet" href="{{ static_url("style/style.min.css") }}" type="text/css"/>
{% endblock %}
<link rel="stylesheet" href="{{ base_url }}custom/custom.css" type="text/css" />
<script src="{{static_url("components/es6-promise/promise.min.js")}}" type="text/javascript" charset="utf-8"></script>
<script src="{{static_url('components/preact/index.js')}}" type="text/javascript"></script>
<script src="{{static_url('components/proptypes/index.js')}}" type="text/javascript"></script>
<script src="{{static_url('components/preact-compat/index.js')}}" type="text/javascript"></script>
<script src="{{static_url('components/requirejs/require.js') }}" type="text/javascript" charset="utf-8"></script>
<script>
require.config({
{% if version_hash %}
urlArgs: "v={{version_hash}}",
{% endif %}
baseUrl: '{{static_url("", include_version=False)}}',
paths: {
'auth/js/main': 'auth/js/main.min',
custom : '{{ base_url }}custom',
nbextensions : '{{ base_url }}nbextensions',
kernelspecs : '{{ base_url }}kernelspecs',
underscore : 'components/underscore/underscore-min',
backbone : 'components/backbone/backbone-min',
jed: 'components/jed/jed',
jquery: 'components/jquery/jquery.min',
json: 'components/requirejs-plugins/src/json',
text: 'components/requirejs-text/text',
bootstrap: 'components/bootstrap/js/bootstrap.min',
bootstraptour: 'components/bootstrap-tour/build/js/bootstrap-tour.min',
'jquery-ui': 'components/jquery-ui/jquery-ui.min',
moment: 'components/moment/min/moment-with-locales',
codemirror: 'components/codemirror',
termjs: 'components/xterm.js/xterm',
typeahead: 'components/jquery-typeahead/dist/jquery.typeahead.min',
},
map: { // for backward compatibility
"*": {
"jqueryui": "jquery-ui",
}
},
shim: {
typeahead: {
deps: ["jquery"],
exports: "typeahead"
},
underscore: {
exports: '_'
},
backbone: {
deps: ["underscore", "jquery"],
exports: "Backbone"
},
bootstrap: {
deps: ["jquery"],
exports: "bootstrap"
},
bootstraptour: {
deps: ["bootstrap"],
exports: "Tour"
},
"jquery-ui": {
deps: ["jquery"],
exports: "$"
}
},
waitSeconds: 30,
});

require.config({
map: {
'*':{
'contents': '{{ contents_js_source }}',
}
}
});

// error-catching custom.js shim.
define("custom", function (require, exports, module) {
try {
var custom = require('custom/custom');
console.debug('loaded custom.js');
return custom;
} catch (e) {
console.error("error loading custom.js", e);
return {};
}
})

document.nbjs_translations = {{ nbjs_translations|safe }};
document.documentElement.lang = navigator.language.toLowerCase();
</script>

{% block meta %}
{% endblock %}

</head>

<body class="{% block bodyclasses %}{% endblock %}"
{% block params %}
{% if logged_in and token %}
data-jupyter-api-token="{{token | urlencode}}"
{% endif %}
{% endblock params %}
dir="ltr">

<noscript>
<div id='noscript'>
{% trans %}Jupyter Notebook requires JavaScript.{% endtrans %}<br>
{% trans %}Please enable it to proceed. {% endtrans %}
</div>
</noscript>

<div id="header">
<div id="header-container" class="container">
<div id="ipython_notebook" class="nav navbar-brand"><a href="{{default_url}}
{%- if logged_in and token -%}?token={{token}}{%- endif -%}" title='{% trans %}dashboard{% endtrans %}'>
{% block logo %}<img src='{{static_url("base/images/logo.png") }}' alt='Jupyter Notebook'/>{% endblock %}
</a></div>

{% block headercontainer %}
{% endblock %}

</div>
<div class="header-bar"></div>

{% block header %}
{% endblock %}
</div>

<div id="site">
{% block site %}
{% endblock %}
</div>

{% block after_site %}
{% endblock %}

{% block script %}
{% endblock %}

<script type='text/javascript'>
function _remove_token_from_url() {
if (window.location.search.length <= 1) {
return;
}
var search_parameters = window.location.search.slice(1).split('&');
for (var i = 0; i < search_parameters.length; i++) {
if (search_parameters[i].split('=')[0] === 'token') {
// remote token from search parameters
search_parameters.splice(i, 1);
var new_search = '';
if (search_parameters.length) {
new_search = '?' + search_parameters.join('&');
}
var new_url = window.location.origin +
window.location.pathname +
new_search +
window.location.hash;
window.history.replaceState({}, "", new_url);
return;
}
}
}
_remove_token_from_url();
</script>
</body>

</html>
Loading

0 comments on commit b530407

Please sign in to comment.