-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainscrcall_ASL_tag-control-subtraction
executable file
·369 lines (293 loc) · 15.3 KB
/
mainscrcall_ASL_tag-control-subtraction
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#! /bin/bash
# Process tag-control subtraction for ASL files #
# Uses the FSL tool "asl_file"
# 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 FMRI DATA"
checkemptvar
emptvar=${EXPERIMENT}
var_checked="THE EXPERIMENT NAME"
checkemptvar
emptvar="${SUBJLIST}"
var_checked="THE SUBJECT(S) TO BE PROCESSED"
checkemptvar
########## FUNCTIONS USED IN THIS SCRIPT ##########
function tag-control-subtraction_command {
# This function will produce a text file with a list of the commands using "asl_file" to be processed by PPSS
for SUBJ in ${SUBJLIST}
do
# Creating a subfolder for this subject within the main output folder
mkdir -p ${asl_output_folder_path}/${SUBJ} 2> /dev/null
for item in `ls ${asl_mainfolder_path}/${SUBJ}/${modality_folder_name}/${modality_subfolder_name}??_${modality_contrast}/*${inputfile_marker}.nii.gz`
do
# Getting only the file name, without extension, into a variable
file_name_noextension=`echo ${item} | sed 's!'${asl_mainfolder_path}/${SUBJ}/${modality_folder_name}/${modality_subfolder_name}.._${modality_contrast}/'!!g' | sed 's/.nii.gz//'`
##### Creating the "asl_file" command for PPSS processing #####
echo -e "asl_file --data=${item} --ntis=${postlabeldelay} --iaf=${data_order} --diff --out=${asl_output_folder_path}/${SUBJ}/${file_name_noextension}_diff --mean=${asl_output_folder_path}/${SUBJ}/${file_name_noextension}_diff_meanperfusion" >> ${temp_folder}/${ppss_command_file}
###############################################################
##### Creating a LOG file with the "asl_file" command used for later QA if needed #####
echo "OS name and version: ${os_nameandversion}; Command: asl_file; FSL `cat $FSLDIR/etc/fslversion`; Date and time: `current_date_time 2`" > ${asl_output_folder_path}/${SUBJ}/LOGFILE_asl_file_tag-control-subtraction_${file_name_noextension}.txt
echo -e "Command asl_file for doing tag-control subtraction" >> ${asl_output_folder_path}/${SUBJ}/LOGFILE_asl_file_tag-control-subtraction_${file_name_noextension}.txt
echo -e "\nasl_file --data=${item} --ntis=${postlabeldelay} --iaf=${data_order} --diff --out=${asl_output_folder_path}/${SUBJ}/${file_name_noextension}_diff --mean=${asl_output_folder_path}/${SUBJ}/${file_name_noextension}_diff_meanperfusion" >> ${asl_output_folder_path}/${SUBJ}/LOGFILE_asl_file_tag-control-subtraction_${file_name_noextension}.txt
###############################################################
done
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_ASL-tag-control-subtraction # Where the temporary files will be held
ppss_command_file=ASL-tag-control-subtraction_list.txt
# Variables to establish imaging modality
modality_folder_name=rest
modality_subfolder_name=resting
modality_contrast=ASL
asl_mainfolder_path=${SUBJS_FOLDERS_PATH} # Where the subjects folders are. Can be modified by the user in this script.
asl_folder_processed=processed_asl_data # Where the output of ASL-related FSL tools will be saved (within each subjects folder)
asl_output_folder_path=${asl_mainfolder_path}/processed_asl_data
######################################################################
######### 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
# Anouncing the purpose of this script
echo -e "###################################"
echo -e "##### TAG-CONTROL SUBTRACTION #####"
echo -e "###################################"
##### GATHERING THE PATH TO THE ANALYSIS FOLDER #####
# FUNCTION CALL: calls the function "analysis_mainfolder" in "mainscrcall_funclib" to allow the user to select the path to the subject's folders
analysis_mainfolder ASL_original
# Assigning the output variable from the function above to a local variable
asl_mainfolder_path=${ANALYSIS_MAINFOLDER_PATH}
######################################################
# Giving instructions to the user
echo
echo -e "This program will process *** TAG-CONTROL SUBTRACTION *** in ASL timeseries files"
echo -e "This will generate:\n1. Tag-control differenced file with (n vols in orig image)/2 volume to be used by \"oxford_asl\" tool;\n2. (relative) perfusion image (same units as the original data)."
echo
echo -e "This processing will be done using the \"asl_file\" tool from FSL"
echo -e "Some initial information is needed prior to processing. Please choose below:"
# While loop for user input
while [ 1 ]
do
echo
echo -en "\tData file for input is (1) ORIGINAL or (2) MOTION CORRECTED: "
read inputfile_origmoco_user
echo
echo -en "\tNumber of inversion time(s) (TI)/post-labelling delay(s) (check ASL pulse sequence): "
read postlabeldelay_user
echo
echo -en "\tData order is (1) TAG-CONTROL or (2) CONTROL-TAG: "
read data_order_user
echo
echo -en "\tFULL PATH for the folder to hold ** OUTPUT ** from \"asl_file\" (Hit ENTER for DEFAULT=${asl_output_folder_path}): "
read asl_output_folder_path_user
# For Data file for input
if [ -n "${inputfile_origmoco_user}" ] && [ ${inputfile_origmoco_user} = 1 ]
then
inputfile_origmoco_user=original
elif [ -n "${inputfile_origmoco_user}" ] && [ ${inputfile_origmoco_user} = 2 ]
then
inputfile_origmoco_user=motion_corrected
else
inputfile_origmoco_user="not entered"
fi
# For Data order
if [ -n "${data_order_user}" ] && [ ${data_order_user} = 1 ]
then
data_order_user=tag-control
elif [ -n "${data_order_user}" ] && [ ${data_order_user} = 2 ]
then
data_order_user=control-tag
else
data_order_user="not entered"
fi
# For output folder path
if [ -z "${asl_output_folder_path_user}" ]
then
# In this case, the user hit ENTER thus it will be used the DEFAULT path
asl_output_folder_path_user=${asl_output_folder_path}
fi
# Asking user's confirmation of data entered
echo -e "\n"
echo -e "Information entered:"
echo -e "\tData file for input: ${inputfile_origmoco_user}"
echo -e "\tNumber of TI/post label delay: ${postlabeldelay_user}"
echo -e "\tData order: ${data_order_user}"
echo -e "\tOutput folder path: ${asl_output_folder_path_user}"
echo
echo -en "Is this correct? (y/n) "
read yesno
if [ -n "${yesno}" ] && [ ${yesno} = y ]
then
# Assigning the values for the variables to be used for processing
postlabeldelay=${postlabeldelay_user}
if [ ${data_order_user} = tag-control ]
then
data_order=tc
elif [ ${data_order_user} = control-tag ]
then
data_order=ct
fi
asl_output_folder_path=${asl_output_folder_path_user}
break
else
echo
echo -en "Please correct the information entered "
sleep 1
# To have a newline between the sentence above and the next (if used in the next, it will make it have 2 newlines)
echo
fi
done
# # Based on the user's initial information, now will gather the parameters for "asl_file"
# echo
# echo -e "Gathering now the specific parameters for \"asl_file\""
# ##### ASL acquisition type #####
# # This will influence the TI duration: cASL/pcASL TI=Post Label Delay + Labelling duration; pASL=ost Label Delay only (TI is instantaneous)
# if [ ${acquisition_type_user} = cASL/pcASL ]
# then
# echo
# echo -en "Enter the total duration for Post label delay + labelling duration in seconds"
# fi
##### Data file for input #####
# This will determine if will use the original ASL timeseries or the motion corrected
if [ ${inputfile_origmoco_user} = original ]
then
echo
echo -e "This script will look for a file in this folder:\n${asl_mainfolder_path}/subjID/rest/resting0?_ASL/filename_stdMNI.nii.gz"
# Assigning the user's decision to a variable
inputfile_marker=_stdMNI
elif [ ${inputfile_origmoco_user} = motion_corrected ]
then
echo
echo -e "*** To use motion corrected ASL timeseries, you must have run MCFLIRT on the original timeseries ***"
echo -e "This script will look for a file in this folder:\n${asl_mainfolder_path}/subjID/rest/resting0?_ASL/filename_mcf.nii.gz"
# Assigning the user's decision to a variable
inputfile_marker=_mcf
fi
echo
echo -e "\t1. Proceed with TAG-CONTROL subtraction"
echo -e "\t0. Back to main menu\n"
# Ask for user input to continue
echo -en "Please enter your option: "
read proceed
while [ 1 ]
do
case ${proceed} in
0)
echo
echo -en "Going back to the PROCESSING menu "
sleep 2
break
;;
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 "mainscrcall_ASL_tag-control-subtraction:Perform tag-control subtraction in ASL timeseries" FSL "(tag-control-subtraction_command) asl_file"
####### MAIN PATH TO SUBJECTS FOLDER CHECK ##############
# Checking if used the original path to ASL files or the user entered another one ("default_path" variable originates from function "analysis_mainfolder" in "mainscrcall_funclib")
if [ ${default_path} = yes ]
then
# Creating a log file. STEP 1.5 of 2 = midscript call
# Calling the function "log_file_scripts_used" in "mainscrcall_funclib" 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. Reason for midscript call
log_file_scripts_used midscript "Used the default path for ASL data: ${asl_mainfolder_path}"
elif [ ${default_path} = no ]
then
# Creating a log file. STEP 1.5 of 2 = midscript call
# Calling the function "log_file_scripts_used" in "mainscrcall_funclib" 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. Reason for midscript call
log_file_scripts_used midscript "Used an alternative path for ASL data: ${asl_mainfolder_path}"
fi
#########################################################
# Giving information to the user on what this script will do
echo
echo -e "These are the subjects listed for processing and the respective input file(s):\n"
# Making the subjects within the "SUBJLIST" variable to be listed in a numbered list for the user
# Establishing the initial subject numbering
count=1
for SUBJ in ${SUBJLIST}
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
echo "${counter}) ${SUBJ}"
# Showing the input files
if [ ${inputfile_marker} = _stdMNI ]
then
# Gathering the files available into a variable. Note the use of dots as wildcards for a single character (like "?" in the terminal)
inputfile_listed=`ls ${asl_mainfolder_path}/${SUBJ}/${modality_folder_name}/${modality_subfolder_name}??_${modality_contrast}/*${inputfile_marker}.nii.gz 2> /dev/null | sed 's!'${asl_mainfolder_path}/${SUBJ}/${modality_folder_name}/${modality_subfolder_name}.._${modality_contrast}/'!!g'`
# Checking if there are files listed
if [ -n "${inputfile_listed}" ]
then
echo -e "Input files: \n${inputfile_listed}"
else
echo -e "Input files: ** None found **"
fi
elif [ ${inputfile_marker} = _mcf ]
then
# Gathering the files available into a variable. Note the use of dots as wildcards for a single character (like "?" in the terminal)
inputfile_listed=`ls ${asl_mainfolder_path}/${SUBJ}/${modality_folder_name}/${modality_subfolder_name}??_${modality_contrast}/*${inputfile_marker}.nii.gz 2> /dev/null | sed 's!'${asl_mainfolder_path}/${SUBJ}/${modality_folder_name}/${modality_subfolder_name}.._${modality_contrast}/'!!g'`
# Checking if there are files listed
if [ -n "${inputfile_listed}" ]
then
echo -e "Input files: \n${inputfile_listed}"
else
echo -e "Input files: ** None found **"
fi
fi
# Adding a unit to the numbering variable
((count++))
done
# Cleaning the contents of the variable below
unset count counter
# Warning the user about the script assumptions
echo
echo -e "*** Please note that this script will use the ASL timeseries files listed above ***"
echo -e "If they are not correct, you should abort now pressing \"Ctrl + c\""
echo -en "Otherwise, press any key to continue "
read -n1 anykey
# Telling the user that processing will start now
echo
echo -e "### Start processing TAG-CONTROL subtraction for all subjects ###"
# FUNCTION CALL: Calls the function "tag-control-subtraction_command" to produce a text file with a list of the commands using "asl_file" to be processed by PPSS
tag-control-subtraction_command
# Checking if there is a PPSS processing file
if [ -f "${temp_folder}/${ppss_command_file}" ] # Checking if this file exists and is a file
then
# Calling the function "parallel_processing_ppss" to run the motion outliers command. Note 4 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; 3) Signal the function "parallel_processing_ppss" that the PPSS log files will be deleted there; 4) Number of parallel processes to be run (empty here, so the function will collected this information).
parallel_processing_ppss ${temp_folder} ${ppss_command_file} ppss_function
echo
echo -e "Finished processing at: `current_date_time 2`"
else
echo
echo -en "*** No PPSS processing file was generated - please check the reason ***"
echo -en "Press any key to continue "
read -n1 anykey
fi
# Ask for user input to continue
echo
echo -en "Press any key to continue to the PROCESSING menu "
read -n1 anykey
break
;;
*)
echo -e "No valid option chosen"
echo -en "Please re-enter an option "
sleep 2
;;
esac
done
# 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}"
# Removing temporary folders and files created
rm -r ${temp_folder} 2> /dev/null