-
Notifications
You must be signed in to change notification settings - Fork 1
/
s1.dataset
executable file
·358 lines (347 loc) · 10.8 KB
/
s1.dataset
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
#!/bin/tcsh
# This script will create AFNI datasets from raw dicom files using to3d.
#
#<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
# Script assumptions:
#
# 1. Directory structure:
# $source_dir/ --> defined below
# $subj/
# 2*/ --> raw data from scanner usually a folder named with the date, year first
# $runs[A:B]/ --> as defined by runs identifiers below
# *.dcm
#
# File structure to be set up:
# $dest_dir/ --> defined below
# $subj/
# bold/
# AFNI datasets
#
# 2. A note about conflicting files:
# Anatomical - script can deal with 2 T1 conflicts, more than that it will skip and print the conflict to the log
# in this case you'll have to do some manual editing
# Functional - script can deal with any number of conflicts, it will choose the directory with the number of
# volumes specified in the preamble ($vA or $vB)
# HOWEVER - if there are conflicting directories and both (or more) have the correct number of dicoms
# then it will skip and note the conflict to the log.... you'll have to do some manual editing
#<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
# Enter subjects
set subj = ()
# source_dir and dest_dir CAN be the same, but both must be specified
set source_dir = # If this does not exist, script will fail
set dest_dir = # If this does not exist, it will be created
# log file will include conflicts, failed runs, missing runs/subjects, and what runs were completed successfully
set log_name = dataset.log # filename for log
set log_dir = # directory for log
set ow_log = no # do you want to overwrite previous log?
# if set to 'no', the log from current iteration will be appended to previous one
# Run Identifiers:
# Each run needs a unique identifier in order to be located in the raw data directory
# run identifiers must come from the name of the raw dicom directories, this is how the script will find the dicoms
# runs with the same run parameters can be grouped together
# to find scan parameters run:
# > dicom_hdr image000001.dcm
# and reference the columns below
set runsA = ()
set vA = # set number of volumes per run in $runsA
set sA = # set number of slices per volume in $runsA; Group 0019 100a from dicom_hdr
set trA = # set TR length (in msec) in runsA; Group 0018 0080
set runsB = ()
set vB = # set number of volumes per run in $runsB
set sB = # set number of slices per volume in $runsB; Group 0019 100a from dicom_hdr
set trB = # set TR length (in msec) in runsB; Group 0018 0080
# Set script options
set overwrite = no #be careful! # yes -> This will delete the ENTIRE bold directory and start anew
# no -> if conflicting file exists, step skipped
set fs_setup = yes # yes -> get directory structure set up to run recon-all, and create orig/001.mgz
# no -> skip freesurfer set up
# known issues to be solved later:
#<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
#<><><><><><><><><><><><><><><> Do Not Change <><><><><><><><><><><><><><><><><>
#<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
# Set up log file
if (! -d $dest_dir) mkdir $dest_dir
if (-d $log_dir) then
set log = $log_dir/$log_name
else
echo " "
echo "Specified log directory does not exist, I will put log in:" ~
echo " "
set log = ~/$log_name
endif
if ($ow_log == yes && -f $log) \rm -f $log
set now = `date +"%a %b %d %Y %r"`
if (! -f $log) then
touch $log
else
echo " " >> $log
endif
echo "=========================================================" >> $log
echo "s1.dataset ran by $user on $now " >> $log
echo "=========================================================" >> $log
echo " " >> $log
set nonomatch
# Directory check...
if (! -d $source_dir) then
echo " "
echo "Source directory does not exist, script failing..." | tee -a $log
echo "It's a trap! - Admiral Ackbar" | tee -a $log
echo " "
exit
else
cd $source_dir
endif
# Now for the datasets...
foreach s ($subj)
echo "========================================================================="
echo "========================================================================="
echo " "
echo "Current Subject: $s"
echo $s >> $log
echo "------" >> $log
echo " " >> $log
# Look for subject directory and set up destination directory
if (! -d $s) then
echo " "
echo "Cannot find" $s | tee -a $log
echo " " >> $log
else
# Remove previous bold directory if specified
if ($overwrite == yes && -d $dest_dir/$s/bold) then
echo "Removing previous files"
\rm -rf $dest_dir/$s/bold
endif
if (! -d $dest_dir/$s) mkdir $dest_dir/$s
if (! -d $dest_dir/$s/bold) mkdir $dest_dir/$s/bold
if ($fs_setup == yes) then
if (! -d $dest_dir/$s/mri) mkdir $dest_dir/$s/mri
if (! -d $dest_dir/$s/mri/orig) mkdir $dest_dir/$s/mri/orig
endif
set sub_dir = $dest_dir/$s/bold
# c_runs = current, d_runs = done, m_runs = missing, f = failed
set c_runs = ()
set d_runs = ()
set m_runs = ()
set f_runs = ()
# Enter dicom directory
if (! -d $s/`\ls $s | grep 2 | grep - | grep .` ) then
echo " "
echo "Cannot find dicom directory" | tee -a $log
echo " "
continue
else
cd $s/2*-*.*
endif
# Copy anatomy
echo " "
echo "======================== Creating Anatomy Dataset ======================="
echo " "
set var = `\ls | egrep t1_mpr`
if ($#var == 0) then
echo "Cannot find anatomical directory"
set m_runs = ($m_runs T1)
else if ($#var < 3) then
if ($#var == 2) then
echo " "
echo "Conflict found: T1" | tee -a $log
echo " Using the force..."
echo " "
set r1 = `ls -l $var[1] | egrep -c '^-'`
set r2 = `ls -l $var[2] | egrep -c '^-'`
if ($r1 > $r2) then
echo " Run used: $var[1]" | tee -a $log
echo " "
set dir = $var[1]
else
echo " Run used: $var[2]" | tee -a $log
echo " "
set dir = $var[2]
endif
else if ($#var == 1) then
set dir = $var
endif
cd $dir
if ($fs_setup == yes) then
echo " "
echo "Creating 001.mgz for Freesurfer"
echo " "
if (! -f $dest_dir/$s/mri/orig/001.mgz) then
mri_convert image000001.dcm $dest_dir/$s/mri/orig/001.mgz
if (! -f $dest_dir/$s/mri/orig/001.mgz) then
echo "WARNING: mri_convert failed..." >> $log
endif
else
echo " "
echo "Freesurfer directory already exists..."
echo " "
endif
endif
if (! -f $sub_dir/${s}_T1+orig.BRIK) then
to3d -prefix ${s}_T1 -session $sub_dir *.dcm
if (-f $sub_dir/${s}_T1+orig.BRIK) then
set c_runs = ($c_runs T1)
else
echo "WARNING:to3d failed on ${s}_T1+orig"
set f_runs = ($f_runs T1)
endif
cd ..
else
echo "${s}_T1+orig already exists"
set d_runs = ($d_runs T1)
cd ..
endif
else if ($#var >= 3) then
echo "More that 2 conflicts in T1" | tee -a $log
set f_runs = ($f_runs T1)
endif
# Copy functional Group A
echo " "
echo "================ Creating Functional Datasets in Group A ================"
if ($#runsA != 0) then
foreach r ($runsA)
echo " "
echo "Current Run: $r"
echo " "
set var = `\ls | egrep $r`
if ($#var == 0) then
echo "Cannot find" $r
echo " "
set m_runs = ($m_runs $r)
else
set dir = ()
if ($#var > 1) then
echo " "
echo "Conflict found: $r" | tee -a $log
echo " Using the force..."
echo " "
foreach v ($var)
cd $v
if (`ls -l *.dcm | egrep -c '^-'` == $vA) then
set dir = ($dir $v)
endif
cd ..
end
if ($#dir == 1) echo " Run used: $dir" | tee -a $log
echo " "
else
set dir = ($var)
endif
if ($#dir == 1) then
cd $dir
if (`ls -l *.dcm | egrep -c '^-'` == $vA) then
if (! -f $sub_dir/${s}_${r}+orig.BRIK) then
to3d -time:zt $sA $vA $trA FROM_IMAGE -prefix ${s}_$r -session $sub_dir *.dcm
if (-f $sub_dir/${s}_${r}+orig.BRIK) then
set c_runs = ($c_runs $r)
else
echo "WARNING: to3d failed on ${s}_${r}+orig"
set f_runs = ($f_runs $r)
endif
else
echo "${s}_${r}+orig already exists"
echo " "
set d_runs = ($d_runs $r)
endif
else
echo "WARNING $r only has `ls -l *.dcm | egrep -c '^-'` TRs, you specified $vA" | tee -a $log
set f_runs = ($f_runs $r)
endif
cd ..
else
echo " "
echo " Conflicting good runs" | tee -a $log
echo " "
set f_runs = ($f_runs $r)
endif
endif
end
else
echo " "
echo "No runs in block A"
echo " "
endif
# Copy functional Group B
echo " "
echo "================ Creating Functional Datasets in Group B ================"
if ($#runsB != 0) then
foreach r ($runsB)
echo " "
echo "Current Run: $r"
echo " "
set var = `\ls | egrep $r`
if ($#var == 0) then
echo "Cannot find" $r
echo " "
set m_runs = ($m_runs $r)
else
set dir = ()
if ($#var > 1) then
echo " "
echo "Conflict found: $r" | tee -a $log
echo " Using the force..."
echo " "
foreach v ($var)
cd $v
if (`ls -l *.dcm | egrep -c '^-'` == $vB) then
set dir = ($dir $v)
endif
cd ..
end
if ($#dir == 1) echo " Run used: $dir" | tee -a $log
echo " "
else
set dir = ($var)
endif
if ($#dir == 1) then
cd $dir
if (`ls -l *.dcm | egrep -c '^-'` == $vB) then
if (! -f $sub_dir/${s}_${r}+orig.BRIK) then
to3d -time:zt $sB $vB $trB FROM_IMAGE -prefix ${s}_$r -session $sub_dir *.dcm
if (-f $sub_dir/${s}_${r}+orig.BRIK) then
set c_runs = ($c_runs $r)
else
echo "WARNING: to3d failed on ${s}_${r}+orig"
set f_runs = ($f_runs $r)
endif
else
echo "${s}_${r}+orig already exists"
echo " "
set d_runs = ($d_runs $r)
endif
else
echo "WARNING $r only has `ls -l *.dcm | egrep -c '^-'` TRs, you specified $vA" | tee -a $log
set f_runs = ($f_runs $r)
endif
cd ..
else
echo " "
echo " Conflicting good runs" | tee -a $log
echo " "
set f_runs = ($f_runs $r)
endif
endif
end
else
echo " "
echo "No runs in block B"
echo "Good, I was tired..."
echo " "
endif
# Check for final files
if ($#d_runs != 0) echo "Previously exist:" $d_runs >> $log
if ($#m_runs != 0) echo "Missing:" $m_runs >> $log
if ($#f_runs != 0) echo "Failed:" $f_runs >> $log
if ($#c_runs == 0) set c_runs = "none"
echo "Datasets created:" $c_runs >> $log
echo " " >> $log
endif
cd $source_dir
echo " "
end
unset nonomatch
echo " "
echo "=================================================="
echo "Datasets have been created, excited you are - Yoda"
echo "=================================================="
echo " "
exit