-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathaseg2srf
executable file
·129 lines (109 loc) · 3.88 KB
/
aseg2srf
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
#!/bin/bash
# Print usage if no argument is given
if [ -z "$1" ]; then
cat <<EOU
Generate surfaces for the subcortical structures
segmented with FreeSurfer.
Usage:
sub2srf -s <list of subjects> [-l <list of labels>] [-d]
Options:
-s <list> Specify a list of subjects between quotes,
e.g. -s "john bill mary mark" or a text file
containing one subject per line.
-l <list> Specify a list of labels between quotes,
e.g. "10 11 14 52 253", or a text file
containing one label per line, or ignore
this option to convert all labels.
-o <output_dir> Output directory
-d Debug mode. Leave all temporary files.
Requirements:
FreeSurfer must have been configured and the variables
FREESURFER_HOME and SUBJECTS_DIR must have been correctly set.
_____________________________________
Anderson M. Winkler
Institute of Living / Yale University
Jul/2009
http://brainder.org
EOU
exit
fi
# List of labels to be converted if no list is specified
LABLIST="4 5 7 8 10 11 12 13 14 15 16 17 18 26 28 43 44 46 47 49 50 51 52 53 54 58 60 251 252 253 254 255"
# Check and accept arguments
SBJLIST=""
ASEG="aseg.mgz"
OUTDIR="aseg2srf"
DEBUG=N
while getopts 'a:s:l:o:d' OPTION
do
case ${OPTION} in
a) ASEG=${OPTARG} ;;
s) SBJLIST=$( [[ -f ${OPTARG} ]] && cat ${OPTARG} || echo "${OPTARG}" ) ;;
l) LABLIST=$( [[ -f ${OPTARG} ]] && cat ${OPTARG} || echo "${OPTARG}" ) ;;
o) OUTDIR=${OPTARG} ;;
d) DEBUG=Y ;;
esac
done
# Prepare a random string to save temporary files
#if hash md5 2> /dev/null ; then
# RND0=$(head -n 1 /dev/random | md5)
#elif hash md5sum 2> /dev/null ; then
# RND0=$(head -n 1 /dev/random | md5sum)
#fi
#RNDSTR=${RND0:0:12}
RNDSTR=${RANDOM:0:12}
# Define a function for Ctrl+C as soon as the RNDSTR is defined
trap bashtrap INT
bashtrap()
{
break ; break
[[ "${s}" != "" ]] && rm -rf ${SUBJECTS_DIR}/${s}/tmp/${RNDSTR} # try to delete temp files
exit 1
}
# For each subject
for s in ${SBJLIST} ; do
# Create directories for temp files and results
mkdir -p ${SUBJECTS_DIR}/${s}/tmp/${RNDSTR}
mkdir -p ${SUBJECTS_DIR}/${s}/${OUTDIR}
# For each label
for lab in ${LABLIST} ; do
# Label string
lab0=$(printf %05d ${lab})
# Extract label, because it seems that mri_pretess cannot handle labels > 255.
python $UTILDIR/extract_label.py ${SUBJECTS_DIR}/${s}/mri/${ASEG} ${lab} \
${SUBJECTS_DIR}/${s}/tmp/${RNDSTR}/aseg_${lab0}.mgz
# Pre-tessellate
echo "==> Pre-tessellating: ${s}, ${lab0}"
${FREESURFER_HOME}/bin/mri_pretess \
${SUBJECTS_DIR}/${s}/tmp/${RNDSTR}/aseg_${lab0}.mgz 1 \
${SUBJECTS_DIR}/${s}/mri/norm.mgz \
${SUBJECTS_DIR}/${s}/tmp/${RNDSTR}/aseg_${lab0}_filled.mgz
# Tessellate
echo "==> Tessellating: ${s}, ${lab0}"
${FREESURFER_HOME}/bin/mri_tessellate -n \
${SUBJECTS_DIR}/${s}/tmp/${RNDSTR}/aseg_${lab0}_filled.mgz 1 \
${SUBJECTS_DIR}/${s}/tmp/${RNDSTR}/aseg_${lab0}_notsmooth
# Smooth
echo "==> Smoothing: ${s}, ${lab0}"
${FREESURFER_HOME}/bin/mris_smooth -nw \
${SUBJECTS_DIR}/${s}/tmp/${RNDSTR}/aseg_${lab0}_notsmooth \
${SUBJECTS_DIR}/${s}/tmp/${RNDSTR}/aseg_${lab0}
# Convert to ASCII
echo "==> Converting to ASCII: ${s}, ${lab0}"
${FREESURFER_HOME}/bin/mris_convert \
${SUBJECTS_DIR}/${s}/tmp/${RNDSTR}/aseg_${lab0} \
${SUBJECTS_DIR}/${s}/tmp/${RNDSTR}/aseg_${lab0}.asc
mv ${SUBJECTS_DIR}/${s}/tmp/${RNDSTR}/aseg_${lab0}.asc \
${SUBJECTS_DIR}/${s}/${OUTDIR}/aseg_${lab0}.srf
done
# Get rid of temp files
if [ "${DEBUG}" == "Y" ] ; then
echo "==> Temporary files for ${s} saved at:"
echo "${SUBJECTS_DIR}/${s}/tmp/${RNDSTR}"
else
echo "==> Removing temporary files for ${s}"
rm -rf ${SUBJECTS_DIR}/${s}/tmp/${RNDSTR}
fi
echo "==> Done: ${s}"
done
exit 0