-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainscrcall_FS_cort_recon
executable file
·282 lines (215 loc) · 14.1 KB
/
mainscrcall_FS_cort_recon
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#! /bin/bash
# This script will perform cortical reconstruction with Free Surfer
# Sourcing of functions library files #
. mainscrcall_funclib
# Setting Ctrl + C as key combination to go back to PROCESSING menu. Also signals to the log file (=if there is one) that this script was interrupted
trap "echo -en '\n\nGoing back to the PROCESSING menu '; sleep 2; log_file_scripts_used interrupt; exit" SIGINT
# Checking if variables are not empty
emptvar=${MEDIA}
var_checked="THE MAIN FOLDER HOLDING MRI DATA"
checkemptvar
emptvar=${EXPERIMENT}
var_checked="THE EXPERIMENT NAME"
checkemptvar
emptvar="${SUBJLIST}"
var_checked="THE SUBJECT(S) TO BE PROCESSED"
checkemptvar
emptvar="${SUBJS_FOLDERS_PATH}"
var_checked="THE FOLDER CONTAINING ALL SUBJECTS' FOLDERS"
checkemptvar
########## FUNCTIONS USED IN THIS SCRIPT ##########
function process_cort_recon {
# This function will prepare a PPSS file for paralell processing of FREESURFER cortical reconstruction
# It uses 1 parameter:
# 1) The recon-all flag to determine what steps are to be processed
# The "local" before the variable ensure that the variable is limited only to within this function
local autorecon_chosen="$1" # recon-all flag
echo -e "\n"
echo -e "Initiating cortical reconstruction processing:"
# Checking if a full analysis file was created for the subject before calling FEAT
for SUBJ in ${subj_recon_process}
do
#####################################
echo "recon-all -subjid ${SUBJ} ${autorecon_chosen}" >> ${temp_folder}/${ppss_command_file}
##### Creating a LOG file with the commands used for later QA if needed #####
echo "OS name and version: ${os_nameandversion}; Command: recon-all; Freesurfer `cat $FREESURFER_HOME/build-stamp.txt`; Date and time: `current_date_time 2`" > ${SUBJECTS_DIR}/LOGFILE_cortical-reconstruction_reconall_${SUBJ}.txt
echo -e "Command \"recon-all\" to process ANATOMICAL files using Freesurfer.\nFlags used and their meaning:\n-all: FULL PROCESSING\n-autorecon1: runs steps 1-5 out of 30\n-autorecon2: runs steps 6-23 out of 30\n-autorecon3: runs steps 24-30 out of 30" >> ${SUBJECTS_DIR}/LOGFILE_cortical-reconstruction_reconall_${SUBJ}.txt
echo -e "\nrecon-all -subjid ${SUBJ} ${autorecon_chosen}" >> ${SUBJECTS_DIR}/LOGFILE_cortical-reconstruction_reconall_${SUBJ}.txt
#####################################
done
}
###########################################################################
############### MAIN MENU ROUTINE ###############
###########################################################################
##### VARIABLES TO BE USED IN THIS SCRIPT #####
# Assigning a variable to hold the main location of the files generated by this script
temp_folder=/tmp/ppss_tempdir
ppss_command_file=subjects_cort_recon.txt
######################################################################
# Script Main menu - Informing the user what this script does and asking for input
clear
# Changing directory to /tmp in case the script comits some error - this will make any commands to be run within /tmp, preventing damage to the file system
cd /tmp
# Removing temporary folders and files created, just in case they were not deleted properly by a previously ran script
rm -r ${temp_folder} 2> /dev/null
# Creating a temporary folder to hold the text file with the subjects to be processed
mkdir ${temp_folder} 2> /dev/null
echo -e "###################################################################"
echo -e "This program will perform cortical reconstruction using FREESURFER"
echo -e "###################################################################"
###########################################################
# FUNCTION CALL: Calling function "FS_subj_mainfolder" from "mainscrcall_funclib" to check the FREESURFER main analysis folder path
FS_subj_mainfolder
###########################################################
# Checking if the subjects to be processed have their folders within the "freesurfer_analysis" folder
echo
echo -e "##### Checking if subjects have the ANATOMICAL image to be processed within its folder in the Freesurfer main analysis folder #####"
# Looping through all subjects
for SUBJ in ${SUBJLIST}
do
# Checking if each subject has a "mri/orig" folder within the main analysis folder
if [ -d ${SUBJECTS_DIR}/${SUBJ}/mri/orig ]
then
# Gathering the ANATOMICAL image file info into variables
anatomical_filename=`ls -1 ${SUBJECTS_DIR}/${SUBJ}/mri/orig/* | awk -F/ '{print $NF}'`
anatomical_path=${SUBJECTS_DIR}/${SUBJ}/mri/orig
echo
echo -e "Subject ID = ${SUBJ}"
echo -e "ANATOMICAL image for processing: ${anatomical_filename}"
echo -e "ANATOMICAL image path: ${anatomical_path}"
# Gathering this subject into a variable for processing
subj_recon_process="${subj_recon_process} ${SUBJ}"
else
echo
echo -e "Subject \"${SUBJ}\" does not have a valid folder named \"mri/orig\" folder, and will NOT be processed"
echo
echo -en "Press anykey to continue "
read -n1 anykey
# Gathering this subject into a variable for NOT processing
subj_recon_NOT_process="${subj_recon_NOT_process} ${SUBJ}"
fi
done
# Giving the user option to look at the output above before continuing
echo
echo -en "Press anykey to continue "
read -n1 anykey
# Listing the subjects with valid ANATOMICAL images to be processed to the user
echo -e "\n"
echo -e "Checking the subjects with valid ANATOMICAL images to be processed"
# Checking there are subjects to be processed
if [ -n "${subj_recon_process}" ] # Check if this variable has contents
then
echo
echo -e "All subjects listed here will be processed:\n"
# Making the subjects within the "SUBJLIST" variable to be listed in a numbered list for the user
# Establishing the initial numbering
count=1
for subj in ${subj_recon_process}
do
# This is to add trailing zeros to this number, so instead of "2" you have "002" for example. Maybe needed if subject's listing gets mixed up due to numbers like "10" being listed before "2", e.g. 1 10 11 2 3 4
counter=`printf "%02d" ${count}`
echo "${counter}) ${subj}"
# Adding a unit to the numbering variable
((count++))
done
# Cleaning the contents of the numbering variable
unset count
fi
# Checking there are subjects NOT to be processed
if [ -n "${subj_recon_NOT_process}" ] # Check if this variable has contents
then
echo
echo -e "The following subjects will NOT be processed due to lack of a \"mri/orig\" folder:\n"
# Making the subjects within the "SUBJLIST" variable to be listed in a numbered list for the user
# Establishing the initial numbering
count=1
for subj in ${subj_recon_NOT_process}
do
# This is to add trailing zeros to this number, so instead of "2" you have "002" for example. Maybe needed if subject's listing gets mixed up due to numbers like "10" being listed before "2", e.g. 1 10 11 2 3 4
counter=`printf "%02d" ${count}`
echo "${counter}) ${subj}"
# Adding a unit to the numbering variable
((count++))
done
# Cleaning the contents of the numbering variable
unset count
fi
# MAIN MENU: Showing the user the options to do all steps of "recon-all" or do it step-wise
while [ 1 ]
do
echo -e "\n"
echo -e "Please select which steps of \"recon-all\" to run (all or selected ones)"
echo -e "Remember that you must need to check \"recon-all\" output for any errors after processing is done"
echo
echo -e "\\tSelect the \"recon-all\" directive to be used below:\n"
echo -e "\t1. recon-all -all (FULL PROCESSING, done in 20-30 hrs)\n"
echo -e "\t2. recon-all -autorecon1 (runs steps 1-5 out of 30, done in about 90 min)"
echo -e "\t3. recon-all -autorecon2 (runs steps 6-23 out of 30, done in 20+ hrs)"
echo -e "\t4. recon-all -autorecon3 (runs steps 24-30 out of 30, done in 30-60 min)"
echo -e "\t0. Abort and go back to PROCESSING MENU\n"
echo -en "\t\tEnter your option here: "
read option_recon
case ${option_recon} in
0)
echo -e "\n"
echo -en "Going back to the PROCESSING MENU "
sleep 2
exit
;;
1)
# Creating a log file. STEP 1 of 2 = start call
# Calling the function "log_file_scripts_used" in "functionslibr_FSL" to output the characteristics of this script. It uses 4 parameters: 1. Flag to signal if this function is being called when the calling script starts or when it has finished; 2. The calling script and its purposes (separated by a semicolon); 3. The analysis software type (FSL, Freesurfer) related to the script's use; 4. The tools used (e.g., fsl_motion_outliers, fslreorient2std, etc.)
log_file_scripts_used start "FS_cort_recon:Perform cortical reconstruction using FREESURFER" Freesurfer "(process_cort_recon) recon-all -all"
# FUNCTION CALL: Calling the "process_cort_recon" function to run the recon-all command
process_cort_recon -all
# Calling the function "parallel_processing_ppss" to run the command. Note 2 parameters: 1) path to the folder holding the temporary files for this script; 2) the name of the files holding the commands for PPSS to run. Added a 3rd parameter to signal the function "parallel_processing_ppss" that the PPSS log files will be deleted there.
# Not using the 4th parameter (list of unique identifiers for differentiating commands, e.g. a list of subjects), SO ALL SUBJECT'S FILES ARE PROCESSED IN A ROW, INSTEAD OF ON A SUBJECT-BY-SUBJECT BASIS (FASTER AND OPTIMIZED USE OF PPSS).
parallel_processing_ppss ${temp_folder} ${ppss_command_file} ppss_function
break
;;
2)
# Creating a log file. STEP 1 of 2 = start call
# Calling the function "log_file_scripts_used" in "functionslibr_FSL" to output the characteristics of this script. It uses 4 parameters: 1. Flag to signal if this function is being called when the calling script starts or when it has finished; 2. The calling script and its purposes (separated by a semicolon); 3. The analysis software type (FSL, Freesurfer) related to the script's use; 4. The tools used (e.g., fsl_motion_outliers, fslreorient2std, etc.)
log_file_scripts_used start "FS_cort_recon:Perform cortical reconstruction using FREESURFER" Freesurfer "(process_cort_recon) recon-all -autorecon1"
# FUNCTION CALL: Calling the "process_cort_recon" function to run the recon-all command
process_cort_recon -autorecon1
# Calling the function "parallel_processing_ppss" to run the command. Note 2 parameters: 1) path to the folder holding the temporary files for this script; 2) the name of the files holding the commands for PPSS to run. Added a 3rd parameter to signal the function "parallel_processing_ppss" that the PPSS log files will be deleted there.
# Not using the 4th parameter (list of unique identifiers for differentiating commands, e.g. a list of subjects), SO ALL SUBJECT'S FILES ARE PROCESSED IN A ROW, INSTEAD OF ON A SUBJECT-BY-SUBJECT BASIS (FASTER AND OPTIMIZED USE OF PPSS).
parallel_processing_ppss ${temp_folder} ${ppss_command_file} ppss_function
break
;;
3)
# Creating a log file. STEP 1 of 2 = start call
# Calling the function "log_file_scripts_used" in "functionslibr_FSL" to output the characteristics of this script. It uses 4 parameters: 1. Flag to signal if this function is being called when the calling script starts or when it has finished; 2. The calling script and its purposes (separated by a semicolon); 3. The analysis software type (FSL, Freesurfer) related to the script's use; 4. The tools used (e.g., fsl_motion_outliers, fslreorient2std, etc.)
log_file_scripts_used start "FS_cort_recon:Perform cortical reconstruction using FREESURFER" Freesurfer "(process_cort_recon) recon-all -autorecon2"
# FUNCTION CALL: Calling the "process_cort_recon" function to run the recon-all command
process_cort_recon -autorecon2
# Calling the function "parallel_processing_ppss" to run the command. Note 2 parameters: 1) path to the folder holding the temporary files for this script; 2) the name of the files holding the commands for PPSS to run. Added a 3rd parameter to signal the function "parallel_processing_ppss" that the PPSS log files will be deleted there.
# Not using the 4th parameter (list of unique identifiers for differentiating commands, e.g. a list of subjects), SO ALL SUBJECT'S FILES ARE PROCESSED IN A ROW, INSTEAD OF ON A SUBJECT-BY-SUBJECT BASIS (FASTER AND OPTIMIZED USE OF PPSS).
parallel_processing_ppss ${temp_folder} ${ppss_command_file} ppss_function
break
;;
4)
# Creating a log file. STEP 1 of 2 = start call
# Calling the function "log_file_scripts_used" in "functionslibr_FSL" to output the characteristics of this script. It uses 4 parameters: 1. Flag to signal if this function is being called when the calling script starts or when it has finished; 2. The calling script and its purposes (separated by a semicolon); 3. The analysis software type (FSL, Freesurfer) related to the script's use; 4. The tools used (e.g., fsl_motion_outliers, fslreorient2std, etc.)
log_file_scripts_used start "FS_cort_recon:Perform cortical reconstruction using FREESURFER" Freesurfer "(process_cort_recon) recon-all -autorecon3"
# FUNCTION CALL: Calling the "process_cort_recon" function to run the recon-all command
process_cort_recon -autorecon3
# Calling the function "parallel_processing_ppss" to run the command. Note 2 parameters: 1) path to the folder holding the temporary files for this script; 2) the name of the files holding the commands for PPSS to run. Added a 3rd parameter to signal the function "parallel_processing_ppss" that the PPSS log files will be deleted there.
# Not using the 4th parameter (list of unique identifiers for differentiating commands, e.g. a list of subjects), SO ALL SUBJECT'S FILES ARE PROCESSED IN A ROW, INSTEAD OF ON A SUBJECT-BY-SUBJECT BASIS (FASTER AND OPTIMIZED USE OF PPSS).
parallel_processing_ppss ${temp_folder} ${ppss_command_file} ppss_function
break
;;
*)
echo
echo -e "No valid option chosen"
echo -en "Please re-enter an option "
sleep 2
;;
esac
done
# Removing temporary folders and files created
rm -r ${temp_folder} 2> /dev/null
# Creating a log file. STEP 2 of 2 = finish call
# Calling the function "log_file_scripts_used" in "functionslibr_FSL" to output the characteristics of this script. It uses 1 parameters: 1. Flag to signal if this function is being called when the calling script starts or when it has finished; 2. List of subjects processed
log_file_scripts_used finish "${SUBJLIST}"