-
Notifications
You must be signed in to change notification settings - Fork 1
/
run
executable file
·159 lines (133 loc) · 5.47 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
#! /bin/bash
#
#
CONTAINER="[willtack/hasl]"
echo -e "$CONTAINER Initiated"
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 $MANIFEST_FILE | jq -r '.config.'"$1"'.default')"
fi
}
# Built to flywheel-v0 spec.
FLYWHEEL_BASE=/flywheel/v0
MCR_ROOT=/usr/local/MATLAB/MATLAB_Runtime/v97/ # double check this
INPUT_DIR=${FLYWHEEL_BASE}/input
OUTPUT_DIR=${FLYWHEEL_BASE}/output
<<<<<<< HEAD
CODE_DIR=${FLYWHEEL_BASE}/app/full_analysis07/for_redistribution_files_only
=======
CODE_DIR=${FLYWHEEL_BASE}/app/full_analysis/07/for_redistribution_files_only
>>>>>>> 630c740238ca16a934912e38ddf7c544d67eb3c2
# Make sure that /output directory exists
if [[ ! -d "${OUTPUT_DIR}" ]]
then
echo "${CONTAINER} ${OUTPUT_DIR} not found!"
exit 1
fi
# Set paths to input files
aslFile=$(find ${INPUT_DIR}/asl/ -type f | grep .nii)
m0File=$(find ${INPUT_DIR}/m0/ -type f | grep .nii)
mprageFile=$(find ${INPUT_DIR}/mprage/ | grep .nii)
# Check for required inputs
if [[ -z "$aslFile" ]] || [[ -z "$m0File" ]] || [[ -z "$mprageFile" ]]; then
echo -e "$CONTAINER One or more input files were not found! Exiting!"
exit 1
fi
ls -Rl ${INPUT_DIR}
ls -al
# Get file parts for MPRAGE
FILE_PREFIX="$(parse_config 'file_prefix')"
mprageFileBet="${OUTPUT_DIR}"/"${FILE_PREFIX}_mprage_bet.nii.gz"
# extract brain
bet2 "${mprageFile}" "${mprageFileBet}" -v -f 0.5 -g 0
# Run the Matlab executable
chmod +x ${CODE_DIR}/run_full_analysis.sh
chmod +x ${CODE_DIR}/full_analysis
time ${CODE_DIR}/run_full_analysis.sh "${MCR_ROOT}"\
"${aslFile}"\
"${m0File}"\
"${mprageFileBet}"\
"${OUTPUT_DIR}"
# Check exit status
exit_status=$?
if [[ $exit_status != 0 ]]; then
echo -e "$CONTAINER An error occurred during execution of the Matlab executable. Exiting!"
exit 1
fi
# Rename output files
TT_FILE_ORIG=$(find $OUTPUT_DIR -type f | grep tt.nii)
TT_FILE="${OUTPUT_DIR}"/"${FILE_PREFIX}_tt.nii"
TT_MNI="${OUTPUT_DIR}"/"${FILE_PREFIX}_MNI152_tt.nii"
CBF_FILE="${OUTPUT_DIR}"/"${FILE_PREFIX}_cbf.nii"
TTCBF_FILE="${OUTPUT_DIR}"/"${FILE_PREFIX}_ttcbf.nii"
PW_FILE="${OUTPUT_DIR}"/"${FILE_PREFIX}_pw.nii"
mv $TT_FILE_ORIG $TT_FILE
mv $(find $OUTPUT_DIR -type f | grep -v tt | grep cbf.nii) $CBF_FILE
mv $(find $OUTPUT_DIR -type f | grep ttcbf.nii) $TTCBF_FILE
mv $(find $OUTPUT_DIR -type f | grep pw.nii) $PW_FILE
# Register TT map to MNI152 via MPRAGE scan
flirt -ref "${mprageFileBet}" -in "${TT_FILE}" -omat $(pwd)/tt2struc.mat
flirt -ref ${FSLDIR}/data/standard/MNI152_T1_2mm_brain -in "${mprageFileBet}" -omat $(pwd)/struc2mni_affine.mat
fnirt --in="${mprageFile}" --aff=struc2mni_affine.mat --cout=struc2standard_warp --config=T1_2_MNI152_2mm
applywarp --ref=${FSLDIR}/data/standard/MNI152_T1_2mm.nii.gz --in=${TT_FILE} --warp=struc2standard_warp \
--premat=tt2struc.mat --out=${TT_MNI}
# Invert warps to bring vascular territory masks into same space as transit time map
convert_xfm -omat $(pwd)/struc2tt.mat -inverse $(pwd)/tt2struc.mat
invwarp --ref="${mprageFile}" --warp=struc2standard_warp --out=struc2standard_warp_inv
TERRITORIES_DIR="${OUTPUT_DIR}/vascular_territories"
mkdir "${TERRITORIES_DIR}"
# Apply warps to every mask
for mask in ${FLYWHEEL_BASE}/territories/*.nii.gz; do
new_mask_name="${FILE_PREFIX}_$(basename ${mask} .nii.gz)_native.nii.gz"
applywarp --ref="${TT_FILE}" --in="${mask}" --warp=struc2standard_warp_inv --postmat=struc2tt.mat --out=${TERRITORIES_DIR}/${new_mask_name}
fslmaths ${TERRITORIES_DIR}/${new_mask_name} -thrP 50 -bin ${TERRITORIES_DIR}/${new_mask_name}
done
# segment mprage to create gray and white matter masks
fast --segments -v -o "${FILE_PREFIX}" ${mprageFileBet}
gmSeg=$(find . -type f | grep _seg_1)
wmSeg=$(find . -type f | grep _seg_2)
for x in /flywheel/v0/*_seg*.nii.gz; do
basename=$(basename $x .nii.gz)
output="${TERRITORIES_DIR}"/${basename}_native.nii.gz
flirt -in $x -ref ${TT_FILE} -applyxfm -init struc2tt.mat -out $output
fslmaths $output -thrP 50 -bin $output
# cp $output ${TERRITORIES_DIR}/
done
# split perfusion weighted image and create deltaM/M0 images
deltaM_DIR="${OUTPUT_DIR}/deltaM"
mkdir "${deltaM_DIR}"
fslsplit $PW_FILE "${deltaM_DIR}/${FILE_PREFIX}_deltaM_"
for x in /flywheel/v0/output/deltaM/*; do
basename=$(basename $x .nii.gz)
fslmaths "${x}" -div "${m0File}" "${deltaM_DIR}/${basename}_dMM0"
done
rename 's/0007/control/' "${deltaM_DIR}/*.nii.gz"
# warp file stuff
WARPS_DIR="${OUTPUT_DIR}/warp_files"
mkdir ${WARPS_DIR}
mv struc2standard*.nii.gz "${WARPS_DIR}/"
mv *.mat "${WARPS_DIR}/"
# Get a list of the files in the output directory
outputs=$(find $OUTPUT_DIR/* -maxdepth 0 -type f)
# If outputs exist, generate metadata, and exit
if [[ -z $outputs ]]; then
echo "$CONTAINER No results found in output directory... Exiting"
exit 1
else
cd $OUTPUT_DIR
pushd deltaM; zip -r ../"${FILE_PREFIX}"_deltaM.zip *; popd
pushd vascular_territories; zip -r ../"${FILE_PREFIX}"_vascular_territories.zip *; popd
pushd warp_files; zip -r ../"${FILE_PREFIX}"_warp_files.zip *; popd
rm -rf MPRAGE ASL M0 ${TERRITORIES_DIR} # remove the intermediate directories. just keep the derived maps
rm -rf ${WARPS_DIR}
rm -rf ${deltaM_DIR}
echo -e "$CONTAINER Wrote: `ls`"
echo -e "$CONTAINER Done!"
fi
# Exit
exit 0