forked from ahheckel/FSL-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.sh
executable file
·4353 lines (3573 loc) · 213 KB
/
main.sh
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# Written by Andreas Heckel
# University of Heidelberg
# https://github.com/ahheckel
# 12/01/2012
export LC_ALL=C # set locale to C (ensures proper sorting)
echo "---------------------------"
# is another instance running ?
if [ -d $(dirname $0)/.lockdir121978 ] ; then echo "$0 : --> another instance is already running - exiting." ; exit 1 ; fi
# start in background mode ?
if [ x"$1" = "xbg" ] ; then
trap 'echo -e "\n********************\nTo cancel background job type \n kill $(cat ./log | grep Job | cut -d : -f 2)\n\nTo view logfile type \n tail -f log\n"********************' EXIT
cd $(dirname $0)
nohup ./run_script.sh &> log &
tail -f log
exit 0
fi
# check if superuser
if [ "$(id -u)" = "0" ]; then
echo "$(basename $0): This script must not be run as root !" 1>&2
exit 1
fi
# echo date
startdate=$(date) ; echo "'$0' started on ${startdate}."
startdate_sec=$(date +"%s")
# exit on error
set -e
# define error trap
trap 'finishdate_sec=$(date +"%s") ; diff=$(($finishdate_sec-$startdate_sec)) ; echo "$0 : An ERROR has occured on `date` (Job-Id : $$). Time elapsed since start: $(echo "scale=4 ; $diff / 3600" | bc -l) h ($(echo "scale=0 ; $diff / 60" | bc -l) min)"' ERR # don't exit on trap (!)
# define and change to working directory
wd=$(dirname $0); cd $wd ; wd=`pwd`
# display FSL version
if [ x$FSL_DIR = "x" ] ; then FSL_DIR="$FSLDIR" ; fi
if [ x$FSL_DIR = "x" ] ; then echo "ERROR : \$FSL_DIR and \$FSLDIR variable not defined - exiting." ; exit 1 ; fi
fslversion=$(cat $(dirname $(dirname `which imglob`))/etc/fslversion)
echo ""; echo "FSL version is '${fslversion}'." ; # sleep 1
# display FREESURFER version
echo "FREESURFER build-stamp is '`cat $FREESURFER_HOME/build-stamp.txt`'." ; echo ""
# display Job-Id
echo "Job-Id : $$" ; echo ""
# source environment variables
if [ ! -f ./globalvars ] ; then echo "ERROR: 'globalvars' not found - exiting." ; exit 1 ; fi
source ./globalvars
# define subdirs
scriptdir=$studydir/rsc/scripts
tmpltdir=$studydir/rsc/scripts/templates
tmpdir=$studydir/.tmp
# make temp directory
mkdir -p $tmpdir
# source environment functions
source $scriptdir/globalfuncs
# check for RAM dumps
dumps=$(find ./ -maxdepth 2 -name "core*")
#dumps=$(find ./ -name "core*")
if [ $(echo $dumps | wc -w) -gt 0 ] ; then
echo "ERROR: memory dumps (core-files) detected - you should look into this. Exiting... "
echo $dumps | row2col
exit 1
fi
# export JOB-ID variable and check if fsl_sub is patched accordingly
if [ $(cat `which fsl_sub` | grep GRID_JOB_ID_FILE | grep HKL | wc -l) -eq 3 -a x"$SGE_ROOT" != "x" ] ; then
echo -n "NOTE: fsl_sub is patched for job control ; "
GRID_JOB_ID_FILE=$wd/.jid.grid.$$
echo "touching job-ID file '$GRID_JOB_ID_FILE'"
touch $GRID_JOB_ID_FILE
export GRID_JOB_ID_FILE
elif [ $(cat `which fsl_sub` | grep GRID_JOB_ID_FILE | grep HKL | wc -l) -lt 3 -a x"$SGE_ROOT" != "x" ] ; then
echo "ERROR: fsl_sub is NOT patched for job control! Exiting... " ; exit 1
fi
# check if FREESURFER's fsl_sub is patched
for fs_fsl_sub in $FREESURFER_HOME/bin/fsl_sub_mgh FREESURFER_HOME/bin/fsl_sub_seychelles ; do
if [ -f $fs_fsl_sub ] ; then
if [ "$(readlink $fs_fsl_sub)" = "$FSLDIR/bin/fsl_sub" ] ; then
echo "NOTE: '$fs_fsl_sub' is linked to '$FSLDIR/bin/fsl_sub'"
else
echo "ERROR: '$fs_fsl_sub' is NOT linked to '$FSLDIR/bin/fsl_sub'! Exiting... " ; exit 1
fi
fi
done
echo ""
# create and check lock
set +e
lock="$wd/.lockdir121978"
echo "creating lock [ '$lock' ]"
mkdir $lock &>/dev/null
if [ $? -gt 0 ] ; then echo "$0 : --> another instance is already running - exiting." ; exit 1 ; fi
lock=""
echo ""
set -e
# create subjects-dir.
mkdir -p $subjdir
# remove lock on exit
trap 'set +e ; echo --------------------------- ; delJIDs $GRID_JOB_ID_FILE ; save_config $studydir $subjdir "$startdate" ; rmdir $wd/.lockdir121978 ; echo "Lock removed." ; time_elapsed $startdate_sec ; echo "Exiting on `date`" ; echo --------------------------- ; exit' EXIT
# remove duplicates in string arrays (to avoid collisions on the cluster)
FIRSTLEV_SUBJECTS=$(echo $FIRSTLEV_SUBJECTS | row2col | sort -u)
FIRSTLEV_SESSIONS_FUNC=$(echo $FIRSTLEV_SESSIONS_FUNC | row2col | sort -u)
FIRSTLEV_SESSIONS_STRUC=$(echo $FIRSTLEV_SESSIONS_STRUC | row2col | sort -u)
SECONDLEV_SUBJECTS=$(echo $SECONDLEV_SUBJECTS_SUBJECTS | row2col | sort -u)
SECONDLEV_SESSIONS_FUNC=$(echo $SECONDLEV_SUBJECTS_SESSIONS_FUNC | row2col | sort -u)
SECONDLEV_SESSIONS_STRUC=$(echo $SECONDLEV_SUBJECTS_SESSIONS_STRUC | row2col | sort -u)
BOLD_SLICETIMING_VALUES=$(echo $BOLD_SLICETIMING_VALUES | row2col | sort -u)
BOLD_SMOOTHING_KRNLS=$(echo $BOLD_SMOOTHING_KRNLS | row2col | sort -u)
BOLD_HPF_CUTOFFS=$(echo $BOLD_HPF_CUTOFFS | row2col | sort -u)
BOLD_DENOISE_SMOOTHING_KRNLS=$(echo $BOLD_DENOISE_SMOOTHING_KRNLS | row2col | sort -u)
BOLD_DENOISE_MASKS_NAT=$(echo $BOLD_DENOISE_MASKS_NAT | row2col | sort -u)
BOLD_DENOISE_MASKS_MNI=$(echo $BOLD_DENOISE_MASKS_MNI | row2col | sort -u)
BOLD_DENOISE_USE_MOVPARS_NAT=$(echo $BOLD_DENOISE_USE_MOVPARS_NAT | row2col | sort -u)
BOLD_DENOISE_USE_MOVPARS_MNI=$(echo $BOLD_DENOISE_USE_MOVPARS_MNI | row2col | sort -u)
BOLD_MNI_RESAMPLE_FUNCDATAS=$(echo $BOLD_MNI_RESAMPLE_FUNCDATAS | row2col | sort -u)
BOLD_MNI_RESAMPLE_RESOLUTIONS=$(echo $BOLD_MNI_RESAMPLE_RESOLUTIONS | row2col | sort -u)
ALFF_DENOISE_MASKS_NAT=$(echo $ALFF_DENOISE_MASKS_NAT | row2col | sort -u)
ALFF_DENOISE_USE_MOVPARS_NAT=$(echo $ALFF_DENOISE_USE_MOVPARS_NAT | row2col | sort -u)
ALFF_RESAMPLING_RESOLUTIONS=$(echo $ALFF_RESAMPLING_RESOLUTIONS | row2col | sort -u)
ALFF_INCLUDED_SUBJECTS=$(echo $ALFF_INCLUDED_SUBJECTS | row2col | sort -u)
ALFF_INCLUDED_SESSIONS=$(echo $ALFF_INCLUDED_SESSIONS | row2col | sort -u)
TBSS_INCLUDED_SUBJECTS=$(echo $TBSS_INCLUDED_SUBJECTS | row2col | sort -u)
TBSS_INCLUDED_SESSIONS=$(echo $TBSS_INCLUDED_SESSIONS | row2col | sort -u)
TBSS_THRES=$(echo $TBSS_THRES | row2col | sort -u)
FS_STATS_MEASURES=$(echo $FS_STATS_MEASURES | row2col | sort -u)
FS_STATS_SMOOTHING_KRNLS=$(echo $FS_STATS_SMOOTHING_KRNLS | row2col | sort -u)
VBM_INCLUDED_SUBJECTS=$(echo $VBM_INCLUDED_SUBJECTS | row2col | sort -u)
VBM_INCLUDED_SESSIONS=$(echo $VBM_INCLUDED_SESSIONS | row2col | sort -u)
VBM_SMOOTHING_KRNL=$(echo $VBM_SMOOTHING_KRNL | row2col | sort -u)
MELODIC_INCLUDED_SUBJECTS=$(echo $MELODIC_INCLUDED_SUBJECTS | row2col | sort -u)
MELODIC_INCLUDED_SESSIONS=$(echo $MELODIC_INCLUDED_SESSIONS | row2col | sort -u)
MELODIC_CMD_INPUT_FILES=$(echo $MELODIC_CMD_INPUT_FILES | row2col | sort -u)
MELODIC_CMD_INCLUDED_SUBJECTS=$(echo $MELODIC_CMD_INCLUDED_SUBJECTS | row2col | sort -u)
MELODIC_CMD_INCLUDED_SESSIONS=$(echo $MELODIC_CMD_INCLUDED_SESSIONS | row2col | sort -u)
DUALREG_INCLUDED_SUBJECTS=$(echo $DUALREG_INCLUDED_SUBJECTS | row2col | sort -u)
DUALREG_INCLUDED_SESSIONS=$(echo $DUALREG_INCLUDED_SESSIONS | row2col | sort -u)
DUALREG_INPUT_ICA_DIRNAMES=$(echo $DUALREG_INPUT_ICA_DIRNAMES | row2col | sort -u)
DUALREG_IC_FILENAMES=$(echo $DUALREG_IC_FILENAMES | row2col | sort -u)
DUALREG_INPUT_BOLD_FILES=$(echo $DUALREG_INPUT_BOLD_FILES | row2col | sort -u)
DUALREG_USE_MOVPARS_HPFS=$(echo $DUALREG_USE_MOVPARS_HPFS | row2col | sort -u)
DUALREG_USE_MOVPARS_TYPES=$(echo $DUALREG_USE_MOVPARS_TYPES | row2col | sort -u)
FSLNETS_DREGDIRS=$(echo $FSLNETS_DREGDIRS | row2col | sort -u)
# define denoise tags
_m=$(for i in $BOLD_DENOISE_MASKS_NAT ; do remove_ext $i | cut -d _ -f 2 ; done) ; dntag_boldnat=$(rem_blanks "$BOLD_DENOISE_USE_MOVPARS_NAT")$(rem_blanks "$_m")
_m=$(for i in $BOLD_DENOISE_MASKS_MNI ; do remove_ext $i | cut -d _ -f 2 ; done) ; dntag_boldmni=$(rem_blanks "$BOLD_DENOISE_USE_MOVPARS_MNI")$(rem_blanks "$_m")
_m=$(for i in $ALFF_DENOISE_MASKS_NAT ; do remove_ext $i | cut -d _ -f 2 ; done) ; dntag_alff=$(rem_blanks "$ALFF_DENOISE_USE_MOVPARS_NAT")$(rem_blanks "$_m")
# ----- create 1st level subject- and session files -----
if [ "x$FIRSTLEV_SUBJECTS" != "x" -a "x$FIRSTLEV_SESSIONS_FUNC" != "x" -a "x$FIRSTLEV_SESSIONS_STRUC" != "x" ] ; then
echo "creating subjects file..."
#errflag=0
#for i in $FIRSTLEV_SUBJECTS ; do if [ ! -d ${subjdir}/$i ] ; then errflag=1 ; echo "ERROR: '${subjdir}/$i' does not exist!" ; fi ; done
#if [ $errflag -eq 1 ] ; then echo "...exiting." ; exit 1 ; fi
echo $FIRSTLEV_SUBJECTS | row2col > ${subjdir}/subjects
cat -n ${subjdir}/subjects
echo ""
for subj in `cat ${subjdir}/subjects` ; do
if [ ! -d ${subjdir}/$subj ] ; then mkdir -p ${subjdir}/$subj ; fi
echo "creating functional session file for subject '$subj': "[ $FIRSTLEV_SESSIONS_FUNC ]""
echo $FIRSTLEV_SESSIONS_FUNC | row2col > ${subjdir}/$subj/sessions_func
echo "creating structural session file for subject '$subj': "[ $FIRSTLEV_SESSIONS_STRUC ]""
echo $FIRSTLEV_SESSIONS_STRUC | row2col > ${subjdir}/$subj/sessions_struc
done
echo ""
fi
# ----- CHECKS -----
# are all required progs / files installed ?
$scriptdir/_check_progs.sh ; echo ""
## make scripts executable
#dos2unix -q $scriptdir/*
#chmod +x $scriptdir/*.sh
# check presence of info files
if [ $CHECK_INFOFILES = 1 ] ; then
# is subjects file present ?
if [ ! -f ${subjdir}/subjects ] ; then
echo "Subjects file not present - proposal:"
cd $subjdir
files=`find ./?* -maxdepth 0 -type d | sort | cut -d / -f 2 | grep -v $(basename $FS_subjdir) | grep -v $(basename $FS_sessdir)`
for i in $files ; do echo $i ; done
read -p "Press Key to accept these entries, otherwise abort with Conrol-C..."
echo $files | row2col > subjects
files=""
cd $wd
fi
# is sessions file present ? (in case of multisession designs)
for subj in `cat ${subjdir}/subjects` ; do
if [ ! -f $subjdir/$subj/sessions_struc -o ! -f $subjdir/$subj/sessions_func ] ; then
if [ $(find $subjdir/$subj/ -maxdepth 1 -type d | wc -l) -eq 1 ] ; then
echo "WARNING : No session files detected. Since no subdirectories in $subjdir/$subj were detected -> assuming single session design. Now creating empty session files..."
echo '.' > $subjdir/$subj/sessions_struc
echo '.' > $subjdir/$subj/sessions_func
echo "done."
fi
fi
if [ ! -f ${subjdir}/${subj}/sessions_struc ] ; then
read -p "Session File for structural processing not present in ${subjdir}/${subj}. You will need to create that file. Exiting..." ; exit 1 ;
fi
if [ ! -f ${subjdir}/${subj}/sessions_func ] ; then
read -p "Session File for functional processing not present in ${subjdir}/${subj}. You will need to create that file. Exiting..." ; exit 1 ;
fi
done
# are bet info present ?
if [ ! -f ${subjdir}/config_bet_lowb ] ; then
read -p "Bet info file for the diffusion images not present in ${subjdir}. Press Key to create the default template."
for subj in `cat $subjdir/subjects`; do for sess in `cat $subjdir/$subj/sessions_struc` ; do echo "$(subjsess) $BETLOWB_INFO" | tee -a $subjdir/config_bet_lowb ; done ; done
fi
if [ ! -f ${subjdir}/config_bet_magn ] ; then
read -p "Bet info file for the magnitude images not present in ${subjdir}. Press Key to create the default template."
for subj in `cat $subjdir/subjects`; do for sess in `cat $subjdir/$subj/sessions_* | sort | uniq` ; do echo "$(subjsess) $BETMAGN_INFO" | tee -a $subjdir/config_bet_magn ; done ; done
fi
if [ ! -f ${subjdir}/config_bet_struc0 ] ; then
read -p "Bet info file for the structural images (prae - std. space masking) not present in ${subjdir}. Press Key to create the default template."
for subj in `cat $subjdir/subjects`; do for sess in `cat $subjdir/$subj/sessions_struc` ; do echo "$(subjsess) $BETSTRUC0_INFO" | tee -a $subjdir/config_bet_struc0 ; done ; done
fi
if [ ! -f ${subjdir}/config_bet_struc1 ] ; then
read -p "Bet info file for the structural images (post - std. space masking) not present in ${subjdir}. Press Key to create the default template."
for subj in `cat $subjdir/subjects`; do for sess in `cat $subjdir/$subj/sessions_struc` ; do echo "$(subjsess) $BETSTRUC1_INFO" | tee -a $subjdir/config_bet_struc1 ; done ; done
fi
if [ ! -f ${subjdir}/config_unwarp_dwi ] ; then
read -p "DWI-unwarp info file for diffusion images not present in ${subjdir}. Press Key to create the default template."
for subj in `cat $subjdir/subjects`; do for sess in `cat $subjdir/$subj/sessions_struc` ; do echo "$(subjsess) $DWIUNWARP_INFO" | tee -a $subjdir/config_unwarp_dwi ; done ; done
fi
if [ ! -f ${subjdir}/config_unwarp_bold ] ; then
read -p "BOLD-unwarp info file not present in ${subjdir}. Press Key to create the default template."
for subj in `cat $subjdir/subjects`; do for sess in `cat $subjdir/$subj/sessions_func` ; do echo "$(subjsess) $BOLDUNWARP_INFO" | tee -a $subjdir/config_unwarp_bold ; done ; done
fi
# check acquisition-parameter files
if [ ! -f ${subjdir}/config_acqparams_bold ] ; then
read -p "BOLD acquisition parameter info file not present in ${subjdir}. Press Key to create a template."
err=0 ;
if [ x$TR_bold = x ] ; then echo "ERROR: TR not defined (need dummy at least)." ; err=1 ; fi
if [ x$TE_bold = x ] ; then echo "ERROR: TE not defined (need dummy at least)." ; err=1 ; fi
if [ x$EES_bold = x ] ; then echo "ERROR: ESP not defined (need dummy at least)." ; err=1 ; fi
if [ $err -eq 1 ] ; then exit 1 ; fi
printf "#ID\t TR (s)\t TE (ms)\t EES (ms)\n" > ${subjdir}/config_acqparams_bold
for subj in `cat $subjdir/subjects`; do for sess in `cat $subjdir/$subj/sessions_func` ; do printf "$(subjsess)\t $TR_bold\t $TE_bold\t $EES_bold\n" | tee -a $subjdir/config_acqparams_bold ; done ; done
fi
if [ ! -f ${subjdir}/config_acqparams_dwi ] ; then
read -p "DWI acquisition parameter info file not present in ${subjdir}. Press Key to create a template."
err=0 ;
if [ x$TR_diff = x ] ; then echo "ERROR: TR not defined (need dummy at least)." ; err=1 ; fi
if [ x$TE_diff = x ] ; then echo "ERROR: TE not defined (need dummy at least)." ; err=1 ; fi
if [ x$EES_diff = x ] ; then echo "ERROR: ESP not defined (need dummy at least)." ; err=1 ; fi
if [ x$TROT_topup = x ] ; then echo "ERROR: TROT (topup) not defined (need dummy at least)." ; err=1 ; fi
if [ $err -eq 1 ] ; then exit 1 ; fi
printf "#ID\t TR (s)\t TE (ms)\t EES (ms)\t TOPUP-TROT (s)\n" > ${subjdir}/config_acqparams_dwi
for subj in `cat $subjdir/subjects`; do for sess in `cat $subjdir/$subj/sessions_struc` ; do printf "$(subjsess)\t $TR_diff\t $TE_diff\t $EES_diff\t $TROT_topup\n" | tee -a $subjdir/config_acqparams_dwi ; done ; done
fi
# are params defined as global variables ? if vars are empty -> set flag, so that params are retrieved from info files.
getTR_bold=0 ; getTE_bold=0 ; getEES_bold=0
getTR_diff=0 ; getTE_diff=0 ; getEES_diff=0 ; getTROT_topup=0
if [ x$TR_bold = x ] ; then getTR_bold=1 ; fi
if [ x$TE_bold = x ] ; then getTE_bold=1 ; fi
if [ x$EES_bold = x ] ; then getEES_bold=1 ; fi
if [ x$TR_diff = x ] ; then getTR_diff=1 ; fi
if [ x$TE_diff = x ] ; then getTE_diff=1 ; fi
if [ x$EES_diff = x ] ; then getEES_diff=1 ; fi
if [ x$TROT_topup = x ] ; then getTROT_topup=1 ; fi
echo "checking acquisition parameters in info files..."
for subj in `cat $subjdir/subjects`; do
for sess in `cat $subjdir/$subj/sessions_func` ; do
#echo "subj $subj , sess $sess : checking '$subjdir/config_acqparams_bold'"
defineBOLDparams $subjdir/config_acqparams_bold $subj $sess
done
done
for subj in `cat $subjdir/subjects`; do
for sess in `cat $subjdir/$subj/sessions_struc` ; do
#echo "subj $subj , sess $sess : checking '$subjdir/config_acqparams_dwi'"
defineDWIparams $subjdir/config_acqparams_dwi $subj $sess
done
done
echo "...done." ; echo ""
# is registration mapping file present ?
if [ ! -f ${subjdir}/config_func2highres.reg ] ; then
echo "WARNING: Registration mapping between functionals and t1 structural not found -> file created. You may need to edit that file..."
touch ${subjdir}/config_func2highres.reg
fi
# are template files present?
if [ ! -f ${tmpltdir}/template_tracula.rc ] ; then
read -p "TRACULA template file not found. You may need to create that file..."
fi
if [ ! -f ${tmpltdir}/template_preprocBOLD.fsf ] ; then
read -p "FEAT template file for BOLD preprocessing not found. You may need to create that file..."
fi
if [ ! -f ${tmpltdir}/template_makeXfmMatrix.m ] ; then
read -p "WARNING: OCTAVE file 'template_makeXfmMatrix.m' not found. You will need that file for TOPUP-related b-vector correction. Press key to continue..."
fi
if [ ! -f ${tmpltdir}/template_gICA.fsf ] ; then
read -p "WARNING: MELODIC template file not found. You may need to create that file..."
fi
fi
# are all subjects registered in struc. and func. infofiles ?
errpause=0
# ...in func. infofiles
for infofile in config_bet_magn config_unwarp_bold config_func2highres.reg ; do
for subj in `cat $subjdir/subjects` ; do
if [ $infofile = "config_func2highres.reg" ] ; then
for sess in `cat $subjdir/$subj/sessions_func` ; do
line=$(cat $subjdir/$infofile | awk '{print $1}' | grep -nx $(subjsess) || true)
if [ "x$line" = "x" ] ; then
errpause=1
echo "WARNING : '$infofile' : entry for '$(subjsess)' not found ! This may or may not be a problem depending on your setup."
if [ "$(cat ${subjdir}/${subj}/sessions_struc)" = "." ] ; then
read -p "Press key to add default value."
echo "$(subjsess) ." | tee -a ${subjdir}/$infofile
fi
fi
done
fi
line=$(cat $subjdir/$infofile | awk '{print $1}' | grep -nx ${subj} || true)
if [ "x$line" = "x" ] ; then
for sess in `cat $subjdir/$subj/sessions_func` ; do
line=$(cat $subjdir/$infofile | awk '{print $1}' | grep -nx $(subjsess) || true)
if [ "x$line" = "x" ] ; then
errpause=1
echo "WARNING : '$infofile' : entry for '$(subjsess)' not found ! This may or may not be a problem depending on your setup."
if [ $infofile = "config_bet_magn" ] ; then read -p "Press key to add default value." ; echo "$(subjsess) $BETMAGN_INFO" | tee -a ${subjdir}/$infofile ; fi
if [ $infofile = "config_unwarp_bold" ] ; then read -p "Press key to add default value." ; echo "$(subjsess) $DWIUNWARP_INFO" | tee -a ${subjdir}/$infofile ; fi
fi
done
fi
done
sort ${subjdir}/$infofile | uniq > $tmpdir/_${infofile} ; mv $tmpdir/_${infofile} ${subjdir}/$infofile
done
# ...in struc. infofiles
for infofile in config_bet_magn config_bet_lowb config_bet_struc0 config_bet_struc1 config_unwarp_dwi ; do
for subj in `cat $subjdir/subjects` ; do
line=$(cat $subjdir/$infofile | awk '{print $1}' | grep -nx ${subj} || true)
if [ "x$line" = "x" ] ; then
for sess in `cat $subjdir/$subj/sessions_struc` ; do
line=$(cat $subjdir/$infofile | awk '{print $1}' | grep -nx $(subjsess) || true)
if [ "x$line" = "x" ] ; then
errpause=1
echo "WARNING : '$infofile' : entry for '$(subjsess)' not found ! This may or may not be a problem depending on your setup."
if [ $infofile = "config_bet_magn" ] ; then read -p "Press key to add default value." ; echo "$(subjsess) $BETMAGN_INFO" | tee -a ${subjdir}/$infofile ; fi
if [ $infofile = "config_bet_lowb" ] ; then read -p "Press key to add default value." ; echo "$(subjsess) $BETLOWB_INFO" | tee -a ${subjdir}/$infofile ; fi
if [ $infofile = "config_bet_struc0" ] ; then read -p "Press key to add default value." ; echo "$(subjsess) $BETSTRUC0_INFO" | tee -a ${subjdir}/$infofile ; fi
if [ $infofile = "config_bet_struc1" ] ; then read -p "Press key to add default value." ; echo "$(subjsess) $BETSTRUC1_INFO" | tee -a ${subjdir}/$infofile ; fi
if [ $infofile = "config_unwarp_dwi" ] ; then read -p "Press key to add default value." ; echo "$(subjsess) $DWIUNWARP_INFO" | tee -a ${subjdir}/$infofile ; fi
fi
done
fi
done
sort ${subjdir}/$infofile | uniq > $tmpdir/_${infofile} ; mv $tmpdir/_${infofile} ${subjdir}/$infofile
done
if [ $errpause -eq 1 ] ; then echo "" ; echo "***CHECK*** (sleeping 2 seconds)..." ; sleep 2 ; echo "" ; fi
# list input files for each subject and session
checklist=""
if [ ! "x$pttrn_diffs" = "x" ] ; then checklist=$checklist" "$pttrn_diffs ; else checklist=$checklist" "0 ; fi
if [ ! "x$pttrn_bvals" = "x" ] ; then checklist=$checklist" "$pttrn_bvals ; else checklist=$checklist" "0 ; fi
if [ ! "x$pttrn_bvecs" = "x" ] ; then checklist=$checklist" "$pttrn_bvecs ; else checklist=$checklist" "0 ; fi
if [ ! "x$pttrn_strucs" = "x" ] ; then checklist=$checklist" "$pttrn_strucs ; else checklist=$checklist" "0 ; fi
if [ ! "x$pttrn_fmaps" = "x" ] ; then checklist=$checklist" "$pttrn_fmaps ; else checklist=$checklist" "0 ; fi
if [ ! "x$pttrn_bolds" = "x" ] ; then checklist=$checklist" "$pttrn_bolds ; else checklist=$checklist" "0 ; fi
# header line
for subj in `cat $subjdir/subjects` ; do
for sess in `cat $subjdir/$subj/sessions_* | sort | uniq` ; do
nchars=`printf "%3i subj %s , sess %s :" 1 $subj $sess`
nchars=${#nchars}
break
done
done
printf "%${nchars}s DWI BVAL BVEC STRC FMAP BOLD \n"
# cycle through...
n=1
for subj in `cat $subjdir/subjects` ; do
for sess in `cat $subjdir/$subj/sessions_* | sort | uniq` ; do
out=""
for i in $checklist ; do
if [ $i = "0" ] ; then
out=$out" "0
else
out=$out" "$(ls $srcdir/$subj/$sess/$i 2>/dev/null | wc -l)
fi
done
printf "%3i subj %s , sess %s :%s \n" $n $subj $sess "$out"
n=$[$n+1]
done
done
# display selected modules
echo "" ; echo "1ST LEVEL processing streams selected:"
echo -n "--- Scratch : " ; [ $SCRATCH = 1 ] && echo -n "SCRATCH " ; echo ""
echo -n "--- FMAP : " ; [ $FIELDMAP_STG1 = 1 ] && echo -n "STG1 " ; [ $FIELDMAP_STG2 = 1 ] && echo -n "STG2 " ; echo ""
echo -n "--- TOPUP : " ; [ $TOPUP_STG1 = 1 ] && echo -n "STG1 " ; [ $TOPUP_STG2 = 1 ] && echo -n "STG2 " ; [ $TOPUP_STG3 = 1 ] && echo -n "STG3 " ; [ $TOPUP_STG4 = 1 ] && echo -n "STG4 " ; [ $TOPUP_STG5 = 1 ] && echo -n "STG5 " ; [ $TOPUP_STG6 = 1 ] && echo -n "STG6 " ; echo ""
echo -n "--- FDT : " ; [ $FDT_STG1 = 1 ] && echo -n "STG1 " ; [ $FDT_STG2 = 1 ] && echo -n "STG2 " ; [ $FDT_STG3 = 1 ] && echo -n "STG3 " ; [ $FDT_STG4 = 1 ] && echo -n "STG4 " ; echo ""
echo -n "--- BPX : " ; [ $BPX_STG1 = 1 ] && echo -n "STG1 " ; echo ""
echo -n "--- RECON-ALL : " ; [ $RECON_STG1 = 1 ] && echo -n "STG1 " ; [ $RECON_STG2 = 1 ] && echo -n "STG2 " ; [ $RECON_STG3 = 1 ] && echo -n "STG3 " ; [ $RECON_STG4 = 1 ] && echo -n "STG4 " ; [ $RECON_STG5 = 1 ] && echo -n "STG5 " ; echo ""
echo -n "--- VBM : " ; [ $VBM_STG1 = 1 ] && echo -n "STG1 " ; [ $VBM_STG2 = 1 ] && echo -n "STG2 " ; [ $VBM_STG3 = 1 ] && echo -n "STG3 " ; [ $VBM_STG4 = 1 ] && echo -n "STG4 " ; [ $VBM_STG5 = 1 ] && echo -n "STG5 " ; echo ""
echo -n "--- TRACULA : " ; [ $TRACULA_STG1 = 1 ] && echo -n "STG1 " ; [ $TRACULA_STG2 = 1 ] && echo -n "STG2 " ; [ $TRACULA_STG3 = 1 ] && echo -n "STG3 " ; [ $TRACULA_STG4 = 1 ] && echo -n "STG4 " ; echo ""
echo -n "--- AUTOPTX : " ; [ $AUTOPTX_STG1 = 1 ] && echo -n "STG1 " ; echo ""
echo -n "--- BOLD : " ; [ $BOLD_STG1 = 1 ] && echo -n "STG1 " ; [ $BOLD_STG2 = 1 ] && echo -n "STG2 " ; [ $BOLD_STG3 = 1 ] && echo -n "STG3 " ; [ $BOLD_STG4 = 1 ] && echo -n "STG4 " ; [ $BOLD_STG5 = 1 ] && echo -n "STG5 " ; echo ""
echo -n "--- ALFF : " ; [ $ALFF_STG1 = 1 ] && echo -n "STG1 " ; [ $ALFF_STG2 = 1 ] && echo -n "STG2 " ; [ $ALFF_STG3 = 1 ] && echo -n "STG3 " ; echo ""
echo "2ND LEVEL processing streams selected:"
echo -n "--- TBSS : " ; [ $TBSS_STG1 = 1 ] && echo -n "STG1 " ; [ $TBSS_STG2 = 1 ] && echo -n "STG2 " ; [ $TBSS_STG3 = 1 ] && echo -n "STG3 " ; [ $TBSS_STG4 = 1 ] && echo -n "STG4 " ; [ $TBSS_STG5 = 1 ] && echo -n "STG5 " ; echo ""
echo -n "--- FS_STATS : " ; [ $FS_STATS_STG1 = 1 ] && echo -n "STG1 " ; [ $FS_STATS_STG2 = 1 ] && echo -n "STG2 " ; [ $FS_STATS_STG3 = 1 ] && echo -n "STG3 " ; [ $FS_STATS_STG4 = 1 ] && echo -n "STG4 " ; echo ""
echo -n "--- VBM_2NDLEV : " ; [ $VBM_2NDLEV_STG1 = 1 ] && echo -n "STG1 " ; [ $VBM_2NDLEV_STG2 = 1 ] && echo -n "STG2 " ; [ $VBM_2NDLEV_STG3 = 1 ] && echo -n "STG3 " ; echo ""
echo -n "--- MELODIC_2NDLEV : " ; [ $MELODIC_2NDLEV_STG1 = 1 ] && echo -n "STG1 " ; [ $MELODIC_2NDLEV_STG2 = 1 ] && echo -n "STG2 " ; echo ""
echo -n "--- MELODIC_CMD : " ; [ $MELODIC_CMD_STG1 = 1 ] && echo -n "STG1 " ; echo ""
echo -n "--- DUALREG : " ; [ $DUALREG_STG1 = 1 ] && echo -n "STG1 " ; [ $DUALREG_STG2 = 1 ] && echo -n "STG2 " ; echo ""
echo -n "--- FSLNETS : " ; [ $FSLNETS_STG1 = 1 ] && echo -n "STG1 " ; echo ""
echo -n "--- ALFF_2NDLEV : " ; [ $ALFF_2NDLEV_STG1 = 1 ] && echo -n "STG1 " ; [ $ALFF_2NDLEV_STG2 = 1 ] && echo -n "STG2 " ; echo ""
echo ""
echo "***CHECK*** (sleeping 2 seconds)..."
sleep 2
# check SGE
if [ x"$SGE_ROOT" != "x" ] ; then
echo ""
qstat -f
echo ""
else
echo ""
echo "SGE_ROOT variable not set -> not using randomise_parallel."
RANDOMISE_PARALLEL=0
echo ""
fi
# check if source directory exists (where nifti originals are located)
if [ ! -d $srcdir ] ; then
echo "ERROR: '$srcdir' (where nifti originals should be located) does not exist - creating it and exiting..."
for subj in `cat $subjdir/subjects`; do
for sess in `cat $subjdir/$subj/sessions_struc` ; do
mkdir -p $srcdir/$subj/$sess
done
done
exit 1
fi
# dos2unix bval/bvec textfiles (just in case...)
echo "Ensuring UNIX line endings in bval-/bvec textfiles..."
err=0
for subj in `cat $subjdir/subjects` ; do
for sess in `cat $subjdir/$subj/sessions_struc` ; do
dwi_txtfiles=""
if [ x${pttrn_diffspuls} != "x" ] ; then
if [ x${pttrn_bvalsplus} != "x" ] ; then dwi_txtfiles=$dwi_txtfiles" "$srcdir/$subj/$sess/$pttrn_bvalsplus ; fi
if [ x${pttrn_bvalsminus} != "x" ] ; then dwi_txtfiles=$dwi_txtfiles" "$srcdir/$subj/$sess/$pttrn_bvalsminus ; fi
if [ x${pttrn_bvecsplus} != "x" ] ; then dwi_txtfiles=$dwi_txtfiles" "$srcdir/$subj/$sess/$pttrn_bvecsplus ; fi
if [ x${pttrn_bvecsminus} != "x" ] ; then dwi_txtfiles=$dwi_txtfiles" "$srcdir/$subj/$sess/$pttrn_bvecsminus ; fi
fi
if [ x${pttrn_diffs} != "x" ] ; then
if [ x${pttrn_bvals} != "x" ] ; then dwi_txtfiles=$dwi_txtfiles" "$srcdir/$subj/$sess/$pttrn_bvals ; fi
if [ x${pttrn_bvecs} != "x" ] ; then dwi_txtfiles=$dwi_txtfiles" "$srcdir/$subj/$sess/$pttrn_bvecs ; fi
fi
dwi_txtfiles=$(echo $dwi_txtfiles| row2col | sort | uniq)
for i in $dwi_txtfiles ; do
#echo " $i"
if [ $(ls $i | wc -l) -eq 0 ] ; then echo "ERROR: 'ls $i' is empty." ; err=1 ; continue ; fi
if [ $(ls $i | wc -l) -gt 1 ] ; then echo "ERROR: 'ls $i' is ambiguous (more than one result)." ; err=1 ; continue ; fi
dos2unix -q $i
done
done
done
if [ $err -eq 1 ] ; then echo "An ERROR has occured - exiting..." ; exit 1 ; fi
echo "...done." ; echo ""
# check bvals, bvecs and diff. files for consistent number of entries
if [ $CHECK_CONSISTENCY_DIFFS = 1 ] ; then
if [ x${pttrn_bvals} != "x" -a x${pttrn_bvecs} != "x" -a x${pttrn_diffs} != "x" ] ; then
echo "Checking bvals/bvecs- and DWI files for consistent number of entries..."
for subj in `cat $subjdir/subjects` ; do
for sess in `cat $subjdir/$subj/sessions_struc` ; do
fldr=$srcdir/$subj/$sess/
echo -n "subj $subj , sess $sess : "
checkConsistency "$fldr/$pttrn_diffs" "$fldr/$pttrn_bvals" "$fldr/$pttrn_bvecs"
done
done
echo ""
fi
fi
# Updating MELODIC templates
echo "Copying ICA templates..."
mkdir -p $gicadir/templates.gica/groupmelodic.ica
cp $(dirname $scriptdir)/fsl/templates/rsn* $gicadir/templates.gica/groupmelodic.ica/
echo "...done." ; echo ""
# make log directory for fsl_sub
mkdir -p $logdir
$scriptdir/delbrokenlinks.sh $logdir 1 # delete broken symlinks
# make directory for 2nd level GLMs
mkdir -p $glmdir_tbss
mkdir -p $glmdir_vbm
# create freesurfer subjects dir
for subj in `cat $subjdir/subjects`; do
for sess in `cat $subjdir/$subj/sessions_struc` ; do
mkdir -p $FS_subjdir/$(subjsess)
done
done
# create freesurfer sessions dir
for subj in `cat $subjdir/subjects`; do
for sess in `cat $subjdir/$subj/sessions_func` ; do
mkdir -p $FS_sessdir/$(subjsess)
done
done
# wait until cluster is ready...
waitIfBusy
# change to subjects directory...
cd $subjdir
echo ""
###########################
# ----- BEGIN SCRATCH -----
###########################
if [ $SCRATCH -eq 1 ]; then
echo "----- BEGIN SCRATCH -----"
exit
fi
#########################
# ----- END SCRATCH -----
#########################
waitIfBusy
############################
# ----- BEGIN FIELDMAP -----
############################
# FIELDMAP prepare
if [ $FIELDMAP_STG1 -eq 1 ]; then
echo "----- BEGIN FIELDMAP_STG1 -----"
for subj in `cat $subjdir/subjects` ; do
for sess in `cat $subjdir/$subj/sessions_* | sort | uniq` ; do
if [ $(ls ${srcdir}/${subj}/${sess}/${pttrn_fmaps} 2>/dev/null | wc -l) -eq 0 ] ; then echo "FIELDMAP : subj $subj , sess $sess : no fieldmap found - continuing..." ; continue ; fi
fldr=$subjdir/${subj}/${sess}/fm
# create fieldmap directory
mkdir -p $fldr
# find magnitude
fm_m=`ls ${srcdir}/${subj}/${sess}/${pttrn_fmaps} | sed -n 1p` # first in listing is magnitude (last is phase-difference volume) (!)
imcp $fm_m $fldr
# split magnitude
echo "FIELDMAP : subj $subj , sess $sess : extracting magnitude image ${fm_m}..."
fslroi $fm_m ${fldr}/magn 0 1 # extract first of the two magnitude images, do not fsl_sub (!)
#fslmaths $fm_m -Tmean ${fldr}/magn # the mean has better SNR
# betting magnitude (with variable FI thresholds)
for f in 0.50 0.40 0.30 ; do
echo "FIELDMAP : subj $subj , sess $sess : betting magnitude image with fi=${f}..."
fsl_sub -l $logdir -N fm_bet_$(subjsess) bet ${fldr}/magn ${fldr}/magn_brain_${f} -m -f $f
done
done
done
fi
waitIfBusy
# FIELDMAP create
if [ $FIELDMAP_STG2 -eq 1 ]; then
echo "----- BEGIN FIELDMAP_STG2 -----"
for subj in `cat $subjdir/subjects` ; do
for sess in `cat $subjdir/$subj/sessions_* | sort | uniq` ; do
if [ $(ls ${srcdir}/${subj}/${sess}/${pttrn_fmaps} 2>/dev/null | wc -l) -eq 0 ] ; then echo "FIELDMAP : subj $subj , sess $sess : no fieldmap found - continuing..." ; continue ; fi
fldr=$subjdir/${subj}/${sess}/fm
# get bet threshold
f=`getBetThres ${subjdir}/config_bet_magn $subj $sess`
# bet, if necessary
if [ $f = "mod" ] ; then
if [ ! -f ${fldr}/magn_brain_${f}.nii.gz -o ! -f ${fldr}/magn_brain_${f}_mask.nii.gz ] ; then
echo "FIELDMAP : subj $subj , sess $sess : ERROR : externally modified volume (magn_brain_${f}.nii.gz) & mask (magn_brain_${f}_mask.nii.gz) not found - exiting..." ; exit 1
fi
else
echo "FIELDMAP : subj $subj , sess $sess : betted magnitude image with fi=${f}..."
bet ${fldr}/magn ${fldr}/magn_brain_${f} -m -f $f
fi
# find phase image
fm_p=`ls ${srcdir}/${subj}/${sess}/${pttrn_fmaps} | tail -n 1` # last in list is phase image, check pattern (!)
# copy files to fieldmap folder
imcp $fm_p $fldr
echo "FIELDMAP : subj $subj , sess $sess : using magn_brain_${f}_mask"
imcp $fldr/magn_brain_${f} $fldr/magn_brain
imcp $fldr/magn_brain_${f}_mask $fldr/magn_brain_mask
# define stats for the phase image
range=`fslstats $fm_p -R`
min=`echo $range | cut -d " " -f1`
max=`echo $range | cut -d " " -f2`
add=$(echo "-($min + $max)/2" | bc -l)
div=$(echo "$max + $add" | bc -l)
# display info
printf "FIELDMAP : subj $subj , sess $sess : phase image $fm_p - info: min: %.2f max: %.2f add: %.2f div: %.2f pi: %.5f \n" $min $max $add $div $PI
# erode by one voxel (default kernel 3x3x3)
echo "FIELDMAP : subj $subj , sess $sess : eroding brain mask a bit..."
fslmaths $fldr/magn_brain_${f}_mask -ero $fldr/magn_brain_mask_ero
# scale to -pi ... +pi
fslmaths $fm_p -add $add -div $div -mul $PI $fldr/phase_rad -odt float
echo "FIELDMAP : subj $subj , sess $sess : phase image is scaled to `fslstats $fldr/phase_rad -R`"
# unwrap
echo "FIELDMAP : subj $subj , sess $sess : unwrapping phase image..."
prelude -p $fldr/phase_rad -m $fldr/magn_brain_mask_ero -a $fldr/magn_brain_${f} -o $fldr/uphase_rad
# divide by echo time difference
echo "FIELDMAP : subj $subj , sess $sess : normalizing phase image to dTE (${deltaTEphase}) and centering to median (P50)..."
fslmaths $fldr/uphase_rad -div $deltaTEphase $fldr/fmap_rads
# center to median
p50=`fslstats $fldr/fmap_rads -k $fldr/magn_brain_mask_ero -P 50`
echo "FIELDMAP : subj $subj , sess $sess : P50: $p50"
fslmaths $fldr/fmap_rads -sub $p50 -mas $fldr/magn_brain_mask_ero $fldr/fmap_rads_masked
# jetzt noch swi ohne swi:
# smooth with gauss kernel of s=xx mm and subtract to get filtered image
fslmaths $fldr/uphase_rad -s 10 $fldr/uphase_rad_s10
fslmaths $fldr/uphase_rad -sub $fldr/uphase_rad_s10 $fldr/uphase_rad_filt
# center pSWI to median
p50=`fslstats $fldr/uphase_rad_filt -k $fldr/magn_brain_mask_ero -P 50`
echo "FIELDMAP : subj $subj , sess $sess : pSWI: P50: $p50"
fslmaths $fldr/uphase_rad_filt -sub $p50 -mas $fldr/magn_brain_mask_ero $fldr/uphase_rad_filt_masked
done
done
fi
##########################
# ----- END FIELDMAP -----
##########################
waitIfBusy
#########################
# ----- BEGIN TOPUP -----
#########################
# TOPUP prepare
if [ $TOPUP_STG1 -eq 1 ] ; then
echo "----- BEGIN TOPUP_STG1 -----"
# check bvals, bvecs and dwi files for consistent number of entries
errflag=0
echo "TOPUP : Checking bvals/bvecs- and DWI files for consistent number of entries..."
for subj in `cat subjects` ; do
for sess in `cat ${subj}/sessions_struc` ; do
fldr=$srcdir/$subj/$sess
i=1
for dwi_p in $(ls $fldr/$pttrn_diffsplus) ; do
dwi_m=$(ls $fldr/$pttrn_diffsminus | sed -n ${i}p)
n_bvalsplus=`ls $fldr/$pttrn_bvalsplus | sed -n ${i}p | xargs cat | wc -w` ; n_bvecsplus=`ls $fldr/$pttrn_bvecsplus | sed -n ${i}p | xargs cat | wc -w`
n_bvalsminus=`ls $fldr/$pttrn_bvalsminus | sed -n ${i}p | xargs cat | wc -w` ; n_bvecsminus=`ls $fldr/$pttrn_bvecsminus | sed -n ${i}p | xargs cat | wc -w`
nvolplus=`countVols "$dwi_p"` ; nvolminus=`countVols "$dwi_m"`
if [ $n_bvalsplus -eq $nvolplus -a $n_bvecsplus=$(echo "scale=0 ; 3*$n_bvalsplus" | bc -l) ] ; then
echo "TOPUP : subj $subj , sess $sess : $(basename $dwi_p) : consistent number of entries in bval/bvec/dwi files ($n_bvalsplus)"
else
echo "TOPUP : subj $subj , sess $sess : $(basename $dwi_p) : ERROR : inconsistent number of entries in bval:$n_bvalsplus / bvec:$(echo "scale=0; $n_bvecsplus/3" | bc -l) / dwi:$nvolplus" ; errflag=1
fi
if [ $n_bvalsminus -eq $nvolminus -a $n_bvecsminus=$(echo "scale=0 ; 3*$n_bvalsminus" | bc -l) ] ; then
echo "TOPUP : subj $subj , sess $sess : $(basename $dwi_m) : consistent number of entries in bval/bvec/dwi files ($n_bvalsminus)"
else
echo "TOPUP : subj $subj , sess $sess : $(basename $dwi_m) : ERROR : inconsistent number of entries in bval:$n_bvalsminus / bvec:$(echo "scale=0; $n_bvecsminus/3" | bc -l) / dwi:$nvolminus" ; errflag=1
fi
if [ $n_bvalsplus -eq $n_bvalsminus ] ; then
echo "TOPUP : subj $subj , sess $sess : blip(+/-) : consistent number of entries ($n_bvalsminus)"
else
echo "TOPUP : subj $subj , sess $sess : blip(+/-) : ERROR : inconsistent number of entries (+: $n_bvalsplus -: $n_bvalsminus)" ; errflag=1
fi
i=$[$i+1]
done
done
done
if [ $errflag -eq 1 ] ; then echo "DWI consistency check : Exiting due to errors !" ; exit 1 ; fi
fldr="" ; n_bvalsplus="" ; n_bvalsminus="" ; n_bvecsplus="" ; n_bvecsminus="" ; nvolplus="" ; nvolminus="" ; errflag="" ; subj="" ; sess="" ; i="" ; dwi_m="" ; dwi_p=""
echo "TOPUP : ...done."
# end check
for subj in `cat subjects` ; do
for sess in `cat ${subj}/sessions_struc` ; do
# check if we have acquisition parameters
defineDWIparams $subjdir/config_acqparams_dwi $subj $sess
if [ "x$pttrn_diffsplus" = "x" -o "x$pttrn_diffsminus" = "x" -o "x$pttrn_bvalsplus" = "x" -o "x$pttrn_bvalsminus" = "x" -o "x$pttrn_bvecsplus" = "x" -o "x$pttrn_bvecsminus" = "x" ] ; then
echo "TOPUP : subj $subj , sess $sess : ERROR : file search pattern for blipUp/blipDown DWIs not set..."
continue
fi
fldr=${subjdir}/${subj}/${sess}/topup
mkdir -p $fldr
# display info
echo "TOPUP : subj $subj , sess $sess : preparing TOPUP... "
# are the +- diffusion files in equal number ?
n_plus=`ls $srcdir/$subj/$sess/$pttrn_diffsplus | wc -l`
n_minus=`ls $srcdir/$subj/$sess/$pttrn_diffsminus | wc -l`
if [ ! $n_plus -eq $n_minus ] ; then
echo "TOPUP : subj $subj , sess $sess : ERROR : number of +blips diff. files ($n_plus) != number of -blips diff. files ($n_minus) - continuing loop..."
continue
elif [ $n_plus -eq 0 -a $n_minus -eq 0 ] ; then
echo "TOPUP : subj $subj , sess $sess : ERROR : no blip-up/down diffusion files found for TOPUP (+/- must be part of the filename) - continuing loop..."
continue
fi
# count +/- bvec/bval-files
ls $srcdir/$subj/$sess/$pttrn_bvecsplus > $fldr/bvec+.files
ls $srcdir/$subj/$sess/$pttrn_bvecsminus > $fldr/bvec-.files
cat $fldr/bvec-.files $fldr/bvec+.files > $fldr/bvec.files
ls $srcdir/$subj/$sess/$pttrn_bvalsplus > $fldr/bval+.files
ls $srcdir/$subj/$sess/$pttrn_bvalsminus > $fldr/bval-.files
cat $fldr/bval-.files $fldr/bval+.files > $fldr/bval.files
n_vec_plus=`cat $fldr/bvec+.files | wc -l`
n_vec_minus=`cat $fldr/bvec-.files | wc -l`
n_val_plus=`cat $fldr/bval+.files | wc -l`
n_val_minus=`cat $fldr/bval-.files | wc -l`
# are the +/- bvec-files equal in number ?
if [ ! $n_vec_plus -eq $n_vec_minus ] ; then
echo "TOPUP : subj $subj , sess $sess : ERROR : number of +blips bvec-files ($n_vec_plus) != number of -blips bvec-files ($n_vec_minus) - continuing loop..."
continue
elif [ $n_vec_plus -eq 0 -a $n_vec_minus -eq 0 ] ; then
echo "TOPUP : subj $subj , sess $sess : ERROR : no blip-up/down bvec-files found for TOPUP (+/- must be part of the filename) - continuing loop..."
continue
fi
# are the +/- bval-files equal in number ?
if [ ! $n_val_plus -eq $n_val_minus ] ; then
echo "TOPUP : subj $subj , sess $sess : ERROR : number of +blips bval-files ($n_val_plus) != number of -blips bval-files ($n_val_minus) - continuing loop..."
continue
elif [ $n_val_plus -eq 0 -a $n_val_minus -eq 0 ] ; then
echo "TOPUP : subj $subj , sess $sess : ERROR : no blip-up/down bval-files found for TOPUP (+/- must be part of the filename) - continuing loop..."
continue
fi
# concatenate +bvecs and -bvecs
concat_bvals $srcdir/$subj/$sess/"$pttrn_bvalsminus" $fldr/bvals-_concat.txt
concat_bvals $srcdir/$subj/$sess/"$pttrn_bvalsplus" $fldr/bvals+_concat.txt
concat_bvecs $srcdir/$subj/$sess/"$pttrn_bvecsminus" $fldr/bvecs-_concat.txt
concat_bvecs $srcdir/$subj/$sess/"$pttrn_bvecsplus" $fldr/bvecs+_concat.txt
nbvalsplus=$(wc -w $fldr/bvals+_concat.txt | cut -d " " -f 1)
nbvalsminus=$(wc -w $fldr/bvals-_concat.txt | cut -d " " -f 1)
nbvecsplus=$(wc -w $fldr/bvecs+_concat.txt | cut -d " " -f 1)
nbvecsminus=$(wc -w $fldr/bvecs-_concat.txt | cut -d " " -f 1)
# check number of entries in concatenated bvals/bvecs files
n_entries=`countVols $srcdir/$subj/$sess/"$pttrn_diffsplus"`
if [ $nbvalsplus = $nbvalsminus -a $nbvalsplus = $n_entries -a $nbvecsplus = `echo "3*$n_entries" | bc` -a $nbvecsplus = $nbvecsminus ] ; then
echo "TOPUP : subj $subj , sess $sess : number of entries in bvals- and bvecs files consistent ($n_entries entries)."
else
echo "TOPUP : subj $subj , sess $sess : ERROR : number of entries in bvals- and bvecs files NOT consistent - continuing loop..."
echo "(diffs+: $n_entries ; bvals+: $nbvalsplus ; bvals-: $nbvalsminus ; bvecs+: $nbvecsplus /3 ; bvecs-: $nbvecsminus /3)"
continue
fi
# check if +/- bval entries are the same
i=1
for bval in `cat $fldr/bvals+_concat.txt` ; do
if [ $bval != $(cat $fldr/bvals-_concat.txt | cut -d " " -f $i) ] ; then
echo "TOPUP : subj $subj , sess $sess : ERROR : +bval entries do not match -bval entries (they should have the same values !) - exiting..."
exit
fi
i=$[$i+1]
done
# getting unwarp direction
uw_dir=`getUnwarpDir ${subjdir}/config_unwarp_dwi $subj $sess`
echo "TOPUP : subj $subj , sess $sess : unwarp direction is '$uw_dir'."
x=0 ; y=0 ; z=0;
if [ "$uw_dir" = "+x" ] ; then x=1 ; fi
if [ "$uw_dir" = "-x" ] ; then x=-1 ; fi
if [ "$uw_dir" = "+y" ] ; then y=-1 ; fi # sic! (so that TOPUP and SIEMENS phasemaps match in sign)
if [ "$uw_dir" = "-y" ] ; then y=1 ; fi # sic! (so that TOPUP and SIEMENS phasemaps match in sign)
if [ "$uw_dir" = "+z" ] ; then z=1 ; fi
if [ "$uw_dir" = "-z" ] ; then z=-1 ; fi
mx=$(echo "scale=0; -1 * ${x}" | bc -l)
my=$(echo "scale=0; -1 * ${y}" | bc -l)
mz=$(echo "scale=0; -1 * ${z}" | bc -l)
blipdownline="$mx $my $mz $TROT_topup"
blipupline="$x $y $z $TROT_topup"
# display info
echo "TOPUP : subj $subj , sess $sess : example blip-down line:"
echo " $blipdownline"
echo "TOPUP : subj $subj , sess $sess : example blip-up line:"
echo " $blipupline"
# creating index file for TOPUP
echo "TOPUP : subj $subj , sess $sess : creating index file for TOPUP..."
rm -f $fldr/$(subjsess)_acqparam.txt ; rm -f $fldr/$(subjsess)_acqparam_inv.txt ; rm -f $fldr/diff.files # clean-up previous runs...
diffsminus=`ls ${srcdir}/${subj}/${sess}/${pttrn_diffsminus}`
for file in $diffsminus ; do
nvol=`fslinfo $file | grep ^dim4 | awk '{print $2}'`
echo "$file n:${nvol}" | tee -a $fldr/diff.files
for i in `seq 1 $nvol`; do
echo "$blipdownline" >> $fldr/$(subjsess)_acqparam.txt
echo "$blipupline" >> $fldr/$(subjsess)_acqparam_inv.txt
done
done
diffsplus=`ls ${srcdir}/${subj}/${sess}/${pttrn_diffsplus}`
for file in $diffsplus ; do
nvol=`fslinfo $file | grep ^dim4 | awk '{print $2}'`
echo "$file n:${nvol}" | tee -a $fldr/diff.files
for i in `seq 1 $nvol`; do
echo "$blipupline" >> $fldr/$(subjsess)_acqparam.txt
echo "$blipdownline" >> $fldr/$(subjsess)_acqparam_inv.txt
done
done
# merging diffusion images for TOPUP
echo "TOPUP : subj $subj , sess $sess : merging diffs... "
fsl_sub -l $logdir -N topup_fslmerge_$(subjsess) fslmerge -t $fldr/diffs_merged $(cat $fldr/diff.files | cut -d " " -f 1)
done
done
waitIfBusy
# perform eddy-correction, if applicable
if [ $TOPUP_USE_EC -eq 1 ] ; then
for subj in `cat subjects` ; do
for sess in `cat ${subj}/sessions_struc` ; do
fldr=${subjdir}/${subj}/${sess}/topup
# cleanup previous runs...
rm -f $fldr/ec_diffs_merged_???_*.nii.gz # removing temporary files from prev. run
if [ ! -z "$(ls $fldr/ec_diffs_merged_???.ecclog 2>/dev/null)" ] ; then
echo "TOPUP : subj $subj , sess $sess : WARNING : eddy_correct logfile(s) from a previous run detected - deleting..."
rm $fldr/ec_diffs_merged_???.ecclog # (!)
fi
# eddy-correct each run...
for i in `seq -f %03g 001 $(cat $fldr/diff.files | wc -l)` ; do # note: don't use seq -w (bash compatibility issues!) (!)
dwifile=$(cat $fldr/diff.files | sed -n ${i}p | cut -d " " -f 1)
bvalfile=$(cat $fldr/bval.files | sed -n ${i}p)
# get B0 index
b0img=`getB0Index $bvalfile $fldr/ec_ref_${i}.idx | cut -d " " -f 1` ; min=`getB0Index $bvalfile $fldr/ec_ref_${i}.idx | cut -d " " -f 2`
# create a task file for fsl_sub, which is needed to avoid accumulations when SGE does a re-run on error
echo "rm -f $fldr/ec_diffs_merged_${i}*.nii.gz ; \
rm -f $fldr/ec_diffs_merged_${i}.ecclog ; \
$scriptdir/eddy_correct.sh $dwifile $fldr/ec_diffs_merged_${i} $b0img $TOPUP_EC_DOF $TOPUP_EC_COST trilinear" > $fldr/topup_ec_${i}.cmd
# eddy-correct
echo "TOPUP : subj $subj , sess $sess : eddy_correction of '$dwifile' (ec_diffs_merged_${i}) is using volume no. $b0img as B0 (val:${min})..."
fsl_sub -l $logdir -N topup_eddy_correct_$(subjsess) -t $fldr/topup_ec_${i}.cmd
done
done
done
waitIfBusy
# plot ecclogs...
for subj in `cat subjects` ; do
for sess in `cat ${subj}/sessions_struc` ; do
fldr=${subjdir}/${subj}/${sess}/topup
cd $fldr
for i in `seq -f %03g 001 $(cat $fldr/diff.files | wc -l)` ; do # note: don't use seq -w (bash compatibility issues!) (!)
echo "TOPUP : subj $subj , sess $sess : plotting ec_diffs_merged_${i}.ecclog..."
eddy_correct_plot ec_diffs_merged_${i}.ecclog $(subjsess)-${i}
# horzcat
pngappend ec_disp.png + ec_rot.png + ec_trans.png ec_${i}.png
# accumulate
if [ $i -gt 1 ] ; then
pngappend ec_plot.png - ec_${i}.png ec_plot.png
else
cp ec_${i}.png ec_plot.png
fi
# cleanup
rm ec_disp.png ec_rot.png ec_trans.png ec_${i}.png
done
cd $subjdir
done
done
fi
fi
waitIfBusy
# TOPUP low-B images: create index and extract
if [ $TOPUP_STG2 -eq 1 ] ; then
echo "----- BEGIN TOPUP_STG2 -----"
for subj in `cat subjects` ; do
for sess in `cat ${subj}/sessions_struc` ; do
fldr=${subjdir}/${subj}/${sess}/topup
if [ ! -f $fldr/$(subjsess)_acqparam.txt ] ; then echo "TOPUP : subj $subj , sess $sess : ERROR : parameter file $fldr/$(subjsess)_acqparam.txt not found - continuing loop..." ; continue ; fi
# display info
echo "TOPUP : subj $subj , sess $sess : concatenate bvals and bvecs... "
echo "`cat $fldr/bvals-_concat.txt`" "`cat $fldr/bvals+_concat.txt`" > $fldr/bvals_concat.txt
paste -d " " $fldr/bvecs-_concat.txt $fldr/bvecs+_concat.txt > $fldr/bvecs_concat.txt
# get B0 index
min=`row2col $fldr/bvals_concat.txt | getMin` # find minimum value (usually representing the "B0" image)
echo "TOPUP : subj $subj , sess $sess : minimum b-value in merged diff. is $min"
b0idces=`getIdx $fldr/bvals_concat.txt $min`
echo $b0idces | row2col > $fldr/lowb.idx
# creating index file for topup (only low-B images)
echo "TOPUP : subj $subj , sess $sess : creating index file for TOPUP (only low-B images)..."
rm -f $fldr/$(subjsess)_acqparam_lowb.txt ; rm -f $fldr/$(subjsess)_acqparam_lowb_inv.txt # clean-up previous runs...
for b0idx in $b0idces ; do
line=`echo "$b0idx + 1" | bc -l`
cat $fldr/$(subjsess)_acqparam.txt | sed -n ${line}p >> $fldr/$(subjsess)_acqparam_lowb.txt
cat $fldr/$(subjsess)_acqparam_inv.txt | sed -n ${line}p >> $fldr/$(subjsess)_acqparam_lowb_inv.txt
done
# extract B0 images
lowbs=""
for b0idx in $b0idces ; do
echo "TOPUP : subj $subj , sess $sess : found B0 image in merged diff. at pos. $b0idx (val:${min}) - extracting..."
lowb="$fldr/b${min}_`printf '%05i' $b0idx`"
fsl_sub -l $logdir -N topup_fslroi_$(subjsess) fslroi $fldr/diffs_merged $lowb $b0idx 1
lowbs=$lowbs" "$lowb
done
# save filenames to text file
echo "$lowbs" > $fldr/lowb.files; lowbs=""
# wait here to prevent overload...
waitIfBusy
done
done
fi
waitIfBusy
# TOPUP merge B0 images
if [ $TOPUP_STG3 -eq 1 ] ; then
echo "----- BEGIN TOPUP_STG3 -----"
for subj in `cat subjects` ; do
for sess in `cat ${subj}/sessions_struc` ; do
fldr=${subjdir}/${subj}/${sess}/topup
if [ ! -f $fldr/$(subjsess)_acqparam.txt ] ; then echo "TOPUP : subj $subj , sess $sess : ERROR : parameter file $fldr/$(subjsess)_acqparam.txt not found - continuing loop..." ; continue ; fi
# merge B0 images
echo "TOPUP : subj $subj , sess $sess : merging low-B volumes..."
fsl_sub -l $logdir -N topup_fslmerge_$(subjsess) fslmerge -t $fldr/$(subjsess)_lowb_merged $(cat $fldr/lowb.files)
done
done
fi
waitIfBusy
# TOPUP execute TOPUP
if [ $TOPUP_STG4 -eq 1 ] ; then