-
Notifications
You must be signed in to change notification settings - Fork 0
/
run
executable file
·169 lines (133 loc) · 6.51 KB
/
run
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/bin/bash
FLYWHEEL_BASE=/flywheel/v0
MANIFEST_FILE=$FLYWHEEL_BASE/manifest.json
CONFIG_FILE=$FLYWHEEL_BASE/config.json
ANALYSIS_ID=$(jq -r '.destination.id' $CONFIG_FILE)
INPUT_DIR=$FLYWHEEL_BASE/input
OUTPUT_DIR=$FLYWHEEL_BASE/output
FREEQC_OUTPUT_DIR=$OUTPUT_DIR/"$ANALYSIS_ID" # Sept 15: I want this directory created, but I don't want the inputs in here
WORKING_DIR=$OUTPUT_DIR/"$ANALYSIS_ID"_work
#BIDS_DIR=$OUTPUT_DIR/bids_dataset
#EXE_SCRIPT=$OUTPUT_DIR/freeqc_run.sh
CONTAINER='[flywheel/freeqc-fw-hpc]'
#https://github.com/PennBBL/rewardAnalysisReprocScripts/blob/master/freesurfer/cnrEulerCalc.sh
echo "Below is the contents of the flywheel base" #Debug
ls -ltr ${FLYWHEEL_BASE} #Debug
echo "Below is the contents of the input directory" #Debug
ls -ltr ${INPUT_DIR} #Debug
echo "Below is the contents of the output directory" #Debug
ls -ltr ${OUTPUT_DIR} #Debug
# CRITICAL: re-create the environment
cat ${FLYWHEEL_BASE}/docker-env.sh
source ${FLYWHEEL_BASE}/docker-env.sh
function parse_config {
CONFIG_FILE=$FLYWHEEL_BASE/config.json
MANIFEST_FILE=$FLYWHEEL_BASE/manifest.json
if [[ -f $CONFIG_FILE ]]; then
echo "$(cat $CONFIG_FILE | jq -r '.config.'$1)"
else
CONFIG_FILE=$MANIFEST_FILE
echo "$(cat $CONFIG_FILE | jq -r '.config.'$1'.default')"
fi
}
#CONFIG_FILE=$FLYWHEEL_BASE/config.json
#cat ${CONFIG_FILE} | grep 'save_partial_outputs' | cut -d ':' -f 2 |
config_save_outputs="$(parse_config 'save_partial_outputs')"
session_label=`python ${FLYWHEEL_BASE}/get_seslabel.py`
#echo -e "$CONTAINER BEGIN FREEQC DOCKERFILE\n"
#cat ${FLYWHEEL_BASE}/*Dockerfile
#echo -e "$CONTAINER END FREEQC DOCKERFILE \n"
# Unzip fmriprep output
#unzip ${INPUT_DIR}/fmriprepdir/*.zip
unzip ${INPUT_DIR}/fmriprepdir/*.zip -d ${FLYWHEEL_BASE}
#############################################################
##################### PROCESSING STEPS #####################
#############################################################
license=`find ${FLYWHEEL_BASE} -name 'license.txt'`
cp ${license} ${FREESURFER_HOME}/
export SUBJECTS_DIR=$FREESURFER_HOME/subjects
FUNCTIONALS_DIR=$FREESURFER_HOME/sessions
source $FREESURFER_HOME/FreeSurferEnv.sh
freesurfer_dir=`find ${FLYWHEEL_BASE}/ -type d -name "freesurfer"` #September 18, 2020: Debug line
echo "This is the freesurfer directory: ${freesurfer_dir}"
ls -ltr ${freesurfer_dir}
####### fMRIPrep outputs that pass QA #######
#InDir=${INPUT_DIR}/fmriprepdir/*/freesurfer
InDir=${freesurfer_dir}
OutDir=${OUTPUT_DIR}
subj=`ls -d ${InDir}/sub* | sed 's#.*/##'`
bblid=`echo ${subj} | cut -d "-" -f 2`
seslabel=`echo ${session_label} | cut -d "-" -f 2`
surfDir=${InDir}/${subj}/surf ### MIGHT HAVE TO GIT RID OF DIRS BETWEEN INPUT AND FREESURFER
mriDir=${InDir}/${subj}/mri
# ----- CNR ----- #
mri_cnr ${surfDir} ${mriDir}/orig.mgz > ${OutDir}/cnr.txt
total=`grep "total CNR" ${OutDir}/cnr.txt | cut -f 4 -d " "`
cnr=`grep "gray/white CNR" ${OutDir}/cnr.txt`
cnr_graycsf_lh=`echo $cnr | cut -d "," -f 2 | cut -d "=" -f 2 | cut -d " " -f 2`
cnr_graycsf_rh=`echo $cnr | cut -d "," -f 3 | cut -d "=" -f 2 | cut -d " " -f 2`
cnr_graywhite_lh=`echo $cnr | cut -d "," -f 1 | cut -d "=" -f 2 | cut -d " " -f 2`
cnr_graywhite_rh=`echo $cnr | cut -d "," -f 2 | cut -d "=" -f 3 | cut -d " " -f 2`
# ---- Euler ---- #
script -c "mris_euler_number ${surfDir}/lh.orig.nofix" ${OutDir}/out_lh.txt
holes_lh=`cat ${OutDir}/out_lh.txt | grep "euler" | cut -d ">" -f 2 | cut -d "h" -f 1 | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'`
euler_lh=`cat ${OutDir}/out_lh.txt | grep "euler" | cut -d "=" -f 4 | cut -d " " -f 2`
script -c "${FREESURFER_HOME}/bin/mris_euler_number ${surfDir}/rh.orig.nofix" ${OutDir}/out_rh.txt
holes_rh=`cat ${OutDir}/out_rh.txt | grep "euler" | cut -d ">" -f 2 | cut -d "h" -f 1 | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'`
euler_rh=`cat ${OutDir}/out_rh.txt | grep "euler" | cut -d "=" -f 4 | cut -d " " -f 2`
holes_total=`expr $holes_lh + $holes_rh`
euler_total=`expr $euler_lh + $euler_rh`
# ----- CSV ----- #
echo "bblid,seslabel,cnr_graycsf_lh,cnr_graycsf_rh,cnr_graywhite_lh,cnr_graywhite_rh,holes_lh,holes_rh,holes_total,euler_lh,euler_rh,euler_total" > ${OutDir}/${subj}_${session_label}_quality.csv
echo "${bblid},${seslabel},${cnr_graycsf_lh},${cnr_graycsf_rh},${cnr_graywhite_lh},${cnr_graywhite_rh},${holes_lh},${holes_rh},${holes_total},${euler_lh},${euler_rh},${euler_total}" >> ${OutDir}/${subj}_${session_label}_quality.csv
# Remove unnecessary files
rm ${OutDir}/cnr.txt
rm ${OutDir}/out_lh.txt
rm ${OutDir}/out_rh.txt
# Quantify regional values
bash ${FLYWHEEL_BASE}/stats2table_bash.sh ${subj} ${session_label}
# Put in bblid and seslabel columns
python ${FLYWHEEL_BASE}/idcols.py
qualcsv=`find ${OutDir} -name "*qual*.csv"`
dktcsv=`find ${OutDir} -name "*rh_DKTatlas_volume.csv"`
if [[ ! -f "${qualcsv}" ]] ; then
echo "Quality csv does not exist"
exit 1
fi
if [[ ! -f "${dktcsv}" ]] ; then
echo "DKT csv does not exist"
exit 1
fi
#############################################################
#############################################################
#############################################################
#if [[ $FREEQC_EXITSTATUS == 0 ]] ; then ### Some other check of outputs
# Generate zipped output of freeqc
cd "$OUTPUT_DIR" # September 3, 2020: Seems redundant
echo "$CONTAINER generating zip archive from outputs..."
mkdir "$OUTPUT_DIR"/freeqc_"${subj}"_"${session_label}"_"${ANALYSIS_ID}"
FREEQC_OUTPUT_DIR="$OUTPUT_DIR"/freeqc_"${subj}"_"${session_label}"_"${ANALYSIS_ID}"
mv ${OutDir}/*.csv ${FREEQC_OUTPUT_DIR}
time zip -q -r "$OUTPUT_DIR"/freeqc_"${subj}"_"${session_label}"_"${ANALYSIS_ID}" $(basename "$FREEQC_OUTPUT_DIR")
chmod -R 777 $OUTPUT_DIR
#if [[ $config_save_outputs == 'true' ]] ; then
# echo "$CONTAINER Error occurred. Config 'save_outputs' set to true. Zipping up outputs."
# cd "$OUTPUT_DIR"
# time zip -q -r "$OUTPUT_DIR"/debug_freeqc_"$ANALYSIS_ID" $(basename "$FREEQC_OUTPUT_DIR")
# time zip -q -r "$OUTPUT_DIR"/debug_freeqc_work_"$ANALYSIS_ID" $(basename "$WORKING_DIR")
# chmod -R 777 $OUTPUT_DIR
# COPY ANALYSIS TO PROJECT LEVEL IF MULTI-SESSION. Q: Why project level? And not subject?
# Use Python SDK to accomplish this task
/usr/local/miniconda/bin/python ${FLYWHEEL_BASE}/move_to_project.py
if [[ $? != 0 ]]; then
echo "$CONTAINER Problem resolving multi-session structure! Exiting (1)"
exit 1
fi
#else
# echo "$CONTAINER Errors encountered during execution. Save outputs config not set. Cleaning up and exiting."
#fi
# Clean up
rm -rf "$WORKING_DIR"
rm -rf "$FREEQC_OUTPUT_DIR"
echo -e "Wrote: `ls -lh $OUTPUT_DIR`"
#exit $FREEQC_EXITSTATUS