-
Notifications
You must be signed in to change notification settings - Fork 12
/
KUL_dcm2bids_new.sh
executable file
·1591 lines (1152 loc) · 49.3 KB
/
KUL_dcm2bids_new.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
# set -x
# Bash shell script to convert dicoms to bids format
#
# Requires dcm2bids, dcm2niix, Mrtrix3
#
# @ Stefan Sunaert - UZ/KUL - [email protected]
# @ Ahmed Radwan - KUL - [email protected]
#
version="v0.9 - dd 29/11/2021"
# Notes
# - NOW USES https://github.com/UNFmontreal/Dcm2Bids
# - works for GE/Siemens/Philips
# - wrap around for multiple subjects: use KUL_multisubjects_dcm2bids
export LANG=us.UTF-8
# ----------------------------------- MAIN ---------------------------------------------
# this script defines a few functions:
# - Usage (for information to the novice user)
# - kul_e2cl from KUL_main_functions (for logging)
# - kul_dcmtags (for reading specific parameters from dicom header)
# source general functions
kul_main_dir=$(dirname "$0")
script=$(basename "$0")
source $kul_main_dir/KUL_main_functions.sh
# $cwd & $log_dir is made in main_functions
# BEGIN LOCAL FUNCTIONS --------------
# --- function Usage ---
function Usage {
cat <<USAGE
`basename $0` converts dicoms to bids format
Usage:
`basename $0` -d dicom_dir -p subject (participant) -c config_file -o bids_dir <OPT_ARGS>
Depends on a config file that defines parameters with sequence information, e.g.
For Philips dicom we need to manually specify the mb and pe_dir:
# Identifier,search-string,fmritask/inteded_for,mb,pe_dir,acq_label
# Structural scans
T1w,T1_PRE
cT1w,T1_Post
FLAIR,3D_FLAIR
T2w,3D_T2
SWI,SWI
MTI,mtc_2dyn
# functional scans
func,rsfMRI_MB6,rest,6,j,singleTE
sbref,rsfMRI_SBREF,rest,1,j,singleTE
func,MB_mTE,rest,4,j,multiTE
sbref,mTE_SBREF,rest,4,j,multiTE
func,MB2_hand,HAND,2,j
func,MB2_lip,LIP,2,j
func,MB2_nback,nback,2,j
sbref,MB2_SBREF,HAND nback,1,j
# fmap: 'task' is now 'IntendedFor' (the func images/tasks that the B0_map is used for SDC)
fmap,B0_map,[HAND LIP nback]
# dMRI
dwi,p1_b1200,-,3,j-,b1200
dwi,p2_b0,-,3,j-,b0
dwi,p3_b2500,-,3,j-,b2500
dwi,p4_b2500,-,3,j,rev
# ASL support is very limited (not BIDS compliant for now)
# Indentifier, search-string
ASL,pCASL
explains that the T1w scan should be found by the search string "T1_PRE"
func by rsfMRI, and has multiband_factor 6, and pe_dir = j
the sbref will be used for the tb_fMRI for both tasks (hands and nback)
For Siemens and GE dicom it can be as simple as:
# Identifier,search-string,fmritask/inteded_for,mb,pe_dir,acq_label
# Structural scans
T1w,T1_PRE
cT1w,T1_Post
FLAIR,3D_FLAIR
T2w,3D_T2
# functional scans
func,rsfMRI_MB6,rest,-,-,singleTE
sbref,rsfMRI_SBREF,rest,-,-,singleTE
func,MB_mTE,rest,-,-,multiTE
sbref,mTE_SBREF,rest,-,-,multiTE
func,MB2_hand,HAND,-,-
func,MB2_lip,LIP,-,-
func,MB2_nback,nback,-,-
sbref,MB2_SBREF,HAND nback,-,-
# dMRI
dwi,p1_b1200,-,-,-,b1200
dwi,p2_b0,-,-,-,b0
dwi,p3_b2500,-,-,-,b2500
dwi,p4_b2500,-,-,-,rev
# fmap, SWI, MTC and ASL have not been tested on Siemens or GE
Example:
`basename $0` -p pat001 -d pat001.zip -c definitions_of_sequences.txt -o BIDS
Required arguments:
-d: dicom_zip_file (the zip or tar.gz containing all your dicoms, or directory containing dicoms)
-p: participant (anonymised name of the subject in bids convention)
-c: definitions of sequences (T1w=MPRAGE,dwi=seq, etc..., see above)
Optional arguments:
-o: bids directory
-s: session (for longitudinal study with multiple timepoints)
-t: temporary directory (default = /tmp)
-e: copy task-*_events.tsv from config to BIDS dir
-a: further anonymise the subject by using pydeface (takes much longer)
-x: expert mode
-v: verbose
USAGE
exit 1
}
# check if jsontool is installed and install it if not
if [[ $(which jsontool) ]]; then
echo " jsontool already installed, good" $log
else
echo " jsontool not installed, installing it with pip using pip install jsontool" $log
pip install jsontool
fi
# check if pydeface is installed and install it if not
if [[ $(which pydeface) ]]; then
echo " pydeface already installed, good" $log
else
echo " pydeface not installed, installing it with pip using pip install jsontool" $log
pip install pydeface
fi
# check if the correct dcm2bids is installed and install it if not
if [[ $(which dcm2bids_scaffold) ]]; then
echo " dcm2bids already installed, good" $log
else
echo " dcm2bids not installed, installing it with pip using pip install dcm2bids" $log
pip uninstall Dcm2Bids
pip install dcm2bids
fi
# check if jq is installed and install it if not
if [[ $(which jq) ]]; then
echo " jq already installed, good" $log
else
echo " jq not installed, installing it with your help..." $log
sudo apt install jq
fi
# --- function kul_dcmtags (for reading specific parameters from dicom header & calculating missing BIDS parameters) ---
function kul_dcmtags {
local dcm_file=$1
local out=$final_dcm_tags_file
# 0/ Read out standard tags for logging
local seriesdescr=$(dcminfo "$dcm_file" -tag 0008 103E | cut -c 13-)
local protocolname=$(dcminfo "$dcm_file" -tag 0008 1030 | cut -c 13-)
local manufacturer=$(dcminfo "$dcm_file" -tag 0008 0070 | cut -c 13-)
local software=$(dcminfo "$dcm_file" -tag 0018 1020 | cut -c 13-)
local imagetype=$(dcminfo "$dcm_file" -tag 0008 0008 2>/dev/null | cut -c 13- | head -n 1)
local patid=$(dcminfo "$dcm_file" -tag 0010 0020 | cut -c 13-)
local pixelspacing=$(dcminfo "$dcm_file" -tag 0028 0030 2>/dev/null | cut -c 13- | head -n 1)
local slicethickness=$(dcminfo "$dcm_file" -tag 0018 0088 2>/dev/null | cut -c 13- | head -n 1)
local acquisitionMatrix=$(dcminfo "$dcm_file" -tag 0018 1310 2>/dev/null | cut -c 13- | head -n 1)
local FovAP=$(dcminfo "$dcm_file" -tag 2005 1074 | cut -c 13-)
local FovFH=$(dcminfo "$dcm_file" -tag 2005 1075 | cut -c 13-)
local FovRL=$(dcminfo "$dcm_file" -tag 2005 1076 | cut -c 13-)
# local echonumber=$(dcminfo "$dcm_file" -tag 0018 0086 2>/dev/null | cut -c 13- | head -n 1)
# need to add local echonumber or something similar for mTE (0018,0086)
# Now we need to determine what vendor it is.
# Philips needs all the following calculations
# Siemens works out of the box
# GE most recent version also seem to work fine
#echo $manufacturer
if [ "$manufacturer" = "SIEMENS" ]; then
slicetime_provided_by_vendor=1
ees_trt_provided_by_vendor=1
#elif [ "$manufacturer" = 'GE ]
# need to be tested
else
slicetime_provided_by_vendor=0
ees_trt_provided_by_vendor=0
fi
# 1/ Calculate ess/trt; needed are : FieldStrength, WaterFatShift, EPIFactor
# Note: only calculate it when it is provided (sometimes this has been thrown away by anonymising the dicom-data)
# We check whether needed tags exist
tags_are_present=1
test_waterfatshift=$(dcminfo "$dcm_file" -tag 2001 1022)
if [ -z "$test_waterfatshift" ]; then
tags_are_present=0
local waterfatshift="empty"
else
local waterfatshift=$(dcminfo "$dcm_file" -tag 2001 1022 | awk '{print $(NF)}')
fi
test_fieldstrength=$(dcminfo "$dcm_file" -tag 0018 0087)
if [ -z "$test_fieldstrength" ]; then
tags_are_present=0
local fieldstrength="empty"
else
local fieldstrength=$(dcminfo "$dcm_file" -tag 0018 0087 | awk '{print $(NF)}')
fi
test_epifactor=$(dcminfo "$dcm_file" -tag 2001 1013)
if [ -z "$test_epifactor" ]; then
tags_are_present=0
local epifactor="empty"
else
local epifactor=$(dcminfo "$dcm_file" -tag 2001 1013 | awk '{print $(NF)}')
fi
if [ $tags_are_present -eq 0 ]; then
ees_sec="empty"
trt_sec="empty"
else
local water_fat_diff_ppm=3.3995
local resonance_freq_mhz_tesla=42.576
local water_fat_shift_hz=$(echo $fieldstrength $water_fat_diff_ppm $resonance_freq_mhz_tesla echo $fieldstrength $water_fat_diff_ppm | awk '{print $1 * $2 * $3}')
#effective_echo_spacing_msec = 1000 * WFS_PIXEL/(water_fat_shift_hz * (EPI_FACTOR + 1))
ees_sec=$(echo $waterfatshift $water_fat_shift_hz $epifactor | awk '{print $1 / ($2 * ($3 + 1))}')
#total_readout_time_fsl_msec = EPI_FACTOR * effective_echo_spacing_msec;
trt_sec=$(echo $epifactor $ees_sec | awk '{print $1 * $2 }')
fi
# 2/ Calculate slice SliceTiming
#function SliceTime=KUL_slicetiming(MB, NS, TR)
#%MB = 1; % Multiband factor
#%NS = 30; % Number of Slices
#%TR = 1.7; % TR in seconds
#st = repmat(0:TR/(NS/MB):TR-.0000001,1,MB);
#SliceTime = sprintf('%.8f,' , st);
#SliceTime = ['"SliceTiming": [' SliceTime(1:end-1) ']'];
#end
multiband_factor=$mb
# if mb is not found in the config file or is set to 0
# simply set it to 1 and run
if [[ -z ${multiband_factor} ]] || [[ ${multiband_factor} == 0 ]]; then
multiband_factor=1
fi
tags_are_present=1
test_number_of_slices=$(dcminfo "$dcm_file" -tag 2001 1018)
if [ -z "$test_number_of_slices" ]; then
tags_are_present=0
local number_of_slices="empty"
else
local number_of_slices=$(dcminfo "$dcm_file" -tag 2001 1018 | awk '{print $(NF)}')
fi
test_repetion_time_msec=$(dcminfo "$dcm_file" -tag 0018 0080 )
if [ -z "$test_repetion_time_msec" ]; then
tags_are_present=0
local repetion_time_msec="empty"
else
local repetion_time_msec=$(dcminfo "$dcm_file" -tag 0018 0080 | awk '{print $(NF)}')
fi
test_slice_scan_order=$(dcminfo "$dcm_file" -tag 2005 1081 )
if [ -z "$test_slice_scan_order" ]; then
#tags_are_present=0
local slice_scan_order="empty"
else
local slice_scan_order=$(dcminfo "$dcm_file" -tag 2005 1081 | head -n 1 | awk '{print $(NF)}')
fi
if [ $tags_are_present -eq 1 ]; then
if [ ! $multiband_factor = "" ];then
#single_slice_time (in seconds)
local single_slice_time=$(echo $repetion_time_msec $number_of_slices $multiband_factor | awk '{print $1 / ($2 / $3) / 1000}')
# clear variables
unset slit
unset slit1
unset slit2
# number of excitations given multiband
# e = n. of excitations/slices per band
echo $number_of_slices
echo $multiband_factor
local e=$(echo $number_of_slices $multiband_factor | awk '{print ($1 / $2) -1 }')
local spb=$((${e}+1));
#echo $e
#echo $spb
#echo "${slice_scan_order}"
# here we need to adapt to account for different slice orders
# e.g.
if [[ "${slice_scan_order}" == "rev. central" ]]; then
# this is a bit different from interleaved... namely we split it into 2 gps
# lower group is regular ascending and second group is regular descending
half_e=$(echo "scale=2;(${spb}/2)" | bc | awk '{print int($1+0.5)}')
for (( zc=0; zc<${half_e}; zc++ )); do
sl1=$(echo $zc $single_slice_time | awk '{print $1 * $2}')
if [[ -z ${slit1} ]]; then
slit1="${sl1}"
else
slit1="${slit1}, ${sl1}"
fi
done
for (( zx=${e}; zx>=${half_e}; zx-- )); do
sl2=$(echo $zx $single_slice_time | awk '{print $1 * $2}')
if [[ -z ${slit2} ]]; then
slit2="${sl2}"
else
slit2="${slit2}, ${sl2}"
fi
done
slit="${slit1}, ${slit2}"
echo "${slit}"
for iz in ${!tmp_order[@]}; do
sl=$(echo $((${tmp_order[$iz]})) ${single_slice_time} | awk '{print $1 * $2}');
echo ${sl}
if [[ -z ${slit} ]]; then
slit="${sl}"
echo ${slit}
else
slit="${slit}, ${sl}"
echo ${slit}
fi
done
# interleaved is ready!
elif [[ "${slice_scan_order}" == "interleaved" ]]; then
declare -a tmp_order
step=$(echo "sqrt(${spb})" | bc)
unset tmp_order curr bh ik slgp;
slgp=0;
declare -a tmp_order;
tmp_order[0]=0;
for ik in $(seq 1 ${e}); do
bh=$((${ik}-1));
curr=$((${tmp_order[$bh]}+${step}));
if [[ ${curr} -gt ${e} ]]; then
((slgp++));
curr=${slgp};
fi;
tmp_order[$ik]=${curr};
done;
for iz in ${!tmp_order[@]}; do
sl=$(echo $((${tmp_order[$iz]})) ${single_slice_time} | awk '{print $1 * $2}');
echo ${sl}
if [[ -z ${slit} ]]; then
slit="${sl}"
echo ${slit}
else
slit="${slit}, ${sl}"
echo ${slit}
fi
done
elif [[ "${slice_scan_order}" == "FH" ]]; then
for (( c=0; c<=$e; c++ )); do
sl=$(echo $c $single_slice_time | awk '{print $1 * $2}')
if [[ -z ${slit} ]]; then
slit="${sl}"
else
slit="${slit}, ${sl}"
fi
done
elif [[ "${slice_scan_order}" == "HF" ]]; then
for (( c=${e}; c>=0; c-- )); do
sl=$(echo $c $single_slice_time | awk '{print $1 * $2}')
if [[ -z ${slit} ]]; then
slit="${sl}"
else
slit="${slit}, ${sl}"
fi
done
elif [[ "${slice_scan_order}" == "default" ]]; then
echo ${slice_scan_order}
a=$((${spb} %2));
echo ${a}
# this is still untested
if [[ "${spb}" -le 6 ]]; then
step=2;
hlpp=$(($((${e}+1))/${step}));
lpsl=0;
lpal=${lpsl};
hpsl=${e};
hpal=${hpsl};
order=0;
for ii in $(seq 0 1 ${spb}); do
if [[ ${lpal} -lt $((${hlpp}-1)) ]]; then
tmp_order[${order}]=${lpal};
lpal=$((${lpal}+${step}));
((order++))
elif [[ ${hpal} -ge $((${hlpp}-1)) ]]; then
tmp_order[${order}]=${hpal};
hpal=$((${hpal}-${step}));
((order++))
else
lpal=$((${lpsl}+1))
hpal=$((${hpsl}-1))
fi
done
# We will not add a 1 as done in the matlab version but we iterate over spb not e also
for iz in ${!tmp_order[@]}; do
sl=$(echo $((${tmp_order[$iz]})) ${single_slice_time} | awk '{print $1 * $2}');
if [[ -z ${slit} ]]; then
slit="${sl}"
else
slit="${slit}, ${sl}"
fi
done
# this is still untested
elif [[ "${spb}" == 8 ]]; then
declare -a tmp_order
step=$(echo "sqrt(${spb})" | bc)
echo "step is ${step}"
unset tmp_order curr bh ik slgp;
slgp=0;
declare -a tmp_order;
tmp_order[0]=0;
echo ${tmp_order[@]};
for ik in $(seq 1 ${e}); do
echo " ik is ${ik}";
bh=$((${ik}-1));
echo "bh is ${bh}";
curr=$((${tmp_order[$bh]}+${step}));
echo "tmp_order of bh is ${tmp_order[$bh]};
echo "initially tmp_order of ik is ${tmp_order[$ik]};
echo "step is ${step}";
if [[ ${curr} -gt ${e} ]]; then
echo "slgp is ${slgp}";
((slgp++));
echo "inceremented slgp is ${slgp}";
curr=${slgp};
echo "now curr = ${slgp}";
fi;
tmp_order[$ik]=${curr};
echo "tmp_order of ik is ${tmp_order[$ik]}";
done;
echo ${tmp_order[@]}
for iz in ${!tmp_order[@]}; do
sl=$(echo $((${tmp_order[$iz]})) ${single_slice_time} | awk '{print $1 * $2}');
if [[ -z ${slit} ]]; then
slit="${sl}"
else
slit="${slit}, ${sl}"
fi
done
else
# if we have an odd no. of slices per band
if [[ ${a} == 1 ]]; then
for zc in $(seq 0 2 ${e} ); do
sl1=$(echo $zc $single_slice_time | awk '{print $1 * $2}')
if [[ -z ${slit1} ]]; then
slit1="${sl1}"
else
slit1="${slit1}, ${sl1}"
fi
done
for zx in $(seq 1 2 ${e} ); do
sl2=$(echo $zx $single_slice_time | awk '{print $1 * $2}')
if [[ -z ${slit2} ]]; then
slit2="${sl2}"
else
slit2="${slit2}, ${sl2}"
fi
done
slit="${slit1}, ${slit2}"
echo "${slit}"
# if we have an odd no. of slices per band
elif [[ ${a} == 0 ]]; then
step=2;
hlpp=$(($((${e}+1))/${step}));
lpsl=0;
lpal=${lpsl};
hpsl=${e};
hpal=${hpsl};
order=0;
for ii in $(seq 0 1 ${spb}); do
if [[ ${lpal} -lt $((${hlpp}-1)) ]]; then
tmp_order[${order}]=${lpal};
lpal=$((${lpal}+${step}));
((order++))
elif [[ ${hpal} -ge $((${hlpp}-1)) ]]; then
tmp_order[${order}]=${hpal};
hpal=$((${hpal}-${step}));
((order++))
else
lpal=$((${lpsl}+1))
hpal=$((${hpsl}-1))
fi
done
# We will not add a 1 as done in the matlab version but we iterate over spb not e also
for iz in ${!tmp_order[@]}; do
sl=$(echo $((${tmp_order[$iz]})) ${single_slice_time} | awk '{print $1 * $2}');
if [[ -z ${slit} ]]; then
slit="${sl}"
else
slit="${slit}, ${sl}"
fi
done
fi
fi
fi
slit2=$slit
rep=$(echo $multiband_factor | awk '{print $1 - 1}')
for (( c=1; c<=$rep; c++ )); do
slit2="$slit2, $slit"
done
slice_time=[$slit2]
fi
else
slice_time="empty"
fi
if [ $silent -eq 0 ]; then
echo " patid = $patid"
echo " the dicom file we are reading = $dcm_file"
echo " manufacturer = $manufacturer"
echo " software version = $software"
echo " imagetype = $imagetype"
echo " acquisitionMatrix = $acquisitionMatrix"
echo " FovAP = $FovAP"
echo " FovFH = $FovFH"
echo " FovRL = $FovRL"
echo " pixelspacing = $pixelspacing"
echo " slicethickness = $slicethickness"
echo " series = $seriesdescr"
echo " fieldstrength = $fieldstrength"
echo " waterfatshift = $waterfatshift"
echo " epifactor = $epifactor"
echo " calulated ees = $ees_sec"
echo " calculated trt = $trt_sec"
echo " number of slices = $number_of_slices"
echo " repetion_time_msec = $repetion_time_msec"
echo " slice_scan_order = $slice_scan_order"
if [ ! $multiband_factor = "" ];then
echo " multiband_factor = $mb"
echo " calculated single_slice_time = $single_slice_time"
echo " number of excitations - 1 = $e"
echo " for 1 multiband = $slit"
echo " complete slice_time = $slice_time"
fi
fi
if [ ! -f $out ]; then
echo -e "participant,session,dcm_file,manufacturer,software_version,series_descr,imagetype,fieldstrength,acquisitionMatrix,FovAP,FovFH,FovRL,pixelspacing,slicethickness,epifactor,wfs,ees_sec,trt_sec,nr_slices,slice_scan_order,repetion_time_msec,multiband_factor" > $out
fi
echo -e "$subj,${sess},$dcm_file,$manufacturer,$software,$seriesdescr,$imagetype,$fieldstrength,$acquisitionMatrix,$FovAP,$FovFH,$FovRL,$pixelspacing,$slicethickness,$epifactor,$waterfatshift,$ees_sec,$trt_sec,$number_of_slices,$slice_scan_order,$repetion_time_msec,$multiband_factor" >> $out
}
function kul_find_relevant_dicom_file {
kul_e2cl " Searching for ${identifier} using search_string $ss" $log
# find the search_string in the dicom dump_file
# search for search_string in dump_file, find ORIGINAL, remove dicom tags, sort, take first line, remove trailing space
seq_file=$(grep "$ss" $dump_file | grep ORIGINAL - | cut -f1 -d"[" | sort | head -n 1 | sed -e 's/[[:space:]]*$//')
#echo "seq_file: $seq_file"
if [ "$seq_file" = "" ]; then
kul_e2cl " ${identifier} dicoms are NOT FOUND" $log
seq_found=0
else
seq_found=1
kul_e2cl " a relevant ${identifier} dicom is $(basename "${seq_file}") " $log
fi
}
# END LOCAL FUNCTIONS --------------
# CHECK COMMAND LINE OPTIONS -------------
#
# Set defaults
sess=""
bids_output=BIDS
# Set flags
subj_flag=0
sess_flag=0
dcm_flag=0
conf_flag=0
bids_flag=0
tmp_flag=0
events_flag=0
n_sbref_tasks=0
silent=1
anon=0
xpert=0
if [ "$#" -lt 4 ]; then
Usage >&2
exit 1
else
while getopts "c:d:p:o:s:t:avehx" OPT; do
case $OPT in
d) #dicom_zip_file
dcm_flag=1
dcm=$OPTARG
;;
p) #participant
subj_flag=1
subj=$OPTARG
;;
c) #config_file
conf_flag=1
conf=$OPTARG
;;
o) #bids output directory
bids_flag=1
bids_output=$OPTARG
;;
s) #session
sess_flag=1
sess=$OPTARG
;;
a) #pydeface
anon=1
;;
a) #xpert
xpert=1
;;
v) #verbose
silent=0
;;
e) #events for task based fmri
events_flag=1
;;
t) #temporary directory
tmp_flag=1
tempo=$OPTARG
;;
h) #help
Usage >&2
exit 0
;;
\?)
echo "Invalid option: -$OPTARG" >&2
echo
Usage >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
echo
Usage >&2
exit 1
;;
esac
done
fi
# check for required options
if [ $dcm_flag -eq 0 ] ; then
echo
echo "Option -d is required: give the file with your raw dicoms either .zip of tar.gz" >&2
echo
exit 2
fi
if [ $subj_flag -eq 0 ] ; then
echo
echo "Option -p is required: give the anonymised name of a subject this will create a directory subject_preproc with results." >&2
echo
exit 2
fi
if [ $conf_flag -eq 0 ] ; then
echo
echo "Option -c is required: give the path to the file that describes the sequences" >&2
echo
exit 2
elif [ ! -f $conf ] ; then
echo
echo "The config file $conf does not exist"
echo
exit 2
fi
# INITIATE ---
# main log file naming
d=$(date "+%Y-%m-%d_%H-%M-%S")
log=$log_dir/${subj}_main_log_${d}.txt
# file for initial dicom tags
dump_file=${log_dir}/${subj}_${sess}_initial_dicom_info.txt
# file with final dicom tags
final_dcm_tags_file=${log_dir}/${subj}_${sess}_final_dicom_info.csv
# location of bids_config_json_file
bids_config_json_file=${log_dir}/${subj}_${sess}_bids_config.json
# location of dcm2niix_log_file
dcm2niix_log_file=$log_dir/${subj}_${sess}_dcm2niix_log_file.txt
if [[ $tmp_flag -eq 1 ]] ; then
tmp=${cwd}/${tempo}
else
tmp="/tmp/${subj}"
fi
rm -fr ${tmp}
# exit
# remove previous existances to start fresh
rm -f $dump_file
rm -f $final_dcm_tags_file
rm -f $bids_config_json_file
# rm -fr ${cwd}/${tmp}/$subj
# ----------- SAY HELLO ----------------------------------------------------------------------------------
if [[ $silent -eq 0 ]]; then
echo " The script you are running has basename `basename "$0"`, located in dirname $kul_main_dir"
echo " The present working directory is `pwd`"
fi
# uncompress the zip file with dicoms or link the directory to tmp
# clear the /tmp directory
mkdir -p ${tmp}
if [[ -d "$dcm" ]]; then
# it is a directory
kul_e2cl " you gave the directory $dcm as input; linking to to $tmp/$subj" $log
ln -s "${cwd}/${dcm}" $tmp/$subj
else
kul_e2cl " uncompressing the zip file $dcm to $tmp/$subj" $log
# Check the extention of the archive
arch_ext="${dcm##*.}"
#echo $arch_ext
if [[ $arch_ext = "zip" ]]; then
unzip -q -o ${dcm} -d ${tmp}
elif [[] $arch_ext = "tar" ]]; then
tar --strip-components=5 -C ${tmp} -xzf ${dcm}
fi
fi
# dump the dicom tags of all dicoms in a file
kul_e2cl " brute force extraction of some relevant dicom tags of all dicom files of subject $subj into file $dump_file" $log
# check if a DICOMIR file exists and archive it (we do a brute force extraction and don't need the DICOMDIR file)
test_DICOMDIR=($(find -L ${tmp} -type f -name "DICOMDIR" ))
#echo "DICOMDIR = $test_DICOMDIR"
if [[ $test_DICOMDIR == "" ]]; then
echo " OK there is no DICOMDIR"
else
gzip $test_DICOMDIR
fi
# Do the bruce force extract
echo hello > $dump_file
task(){
dcm1=$(dcminfo "$dcm_file" -tag 0008 103E -tag 0018 1030 -tag 0008 0008 -tag 0008 0070 -tag 0020 0011 -nthreads 4 2>/dev/null | tr -s '\n' ' ')
echo "$dcm_file" $dcm1 >> $dump_file
}
(
find -L ${tmp} -type f |
while IFS= read -r dcm_file; do
task
done
)
kul_e2cl " done reading dicom tags of $dcm" $log
# create empty bids description
# bids=""
# we read the config file
declare -a sub_bids
while IFS=, read identifier search_string task mb pe_dir acq_label expert_ss expert_val; do
bs=$(( $bs + 1))
if [[ ! ${identifier} == \#* ]]; then
echo "ss: $ss"
if [ $xpert -eq 1 ]; then
ss=${search_string}
else
ss=${expert_val}
#ss=${search_string}
fi
echo "ss: $ss"
if [[ ${identifier} == "T1w" ]]; then
kul_find_relevant_dicom_file
if [ $seq_found -eq 1 ]; then
# read the relevant dicom tags
kul_dcmtags "${seq_file}"
# add an acq_label if any
if [ "$acq_label" = "" ];then
sub_bids_acq=""
else
sub_bids_acq=", \"customLabels\": \"acq-${acq_label}\" "
fi
echo $sub_bids_acq
sub_bids_T1="{ \"dataType\": \"anat\", \"modalityLabel\": \"T1w\", \"criteria\": $ss $sub_bids_acq}"
echo $sub_bids_T1
sub_bids_[$bs]=$(echo ${sub_bids_T1} | python -m json.tool )
fi
fi
if [[ ${identifier} == "cT1w" ]]; then
kul_find_relevant_dicom_file
if [ $seq_found -eq 1 ]; then
# read the relevant dicom tags
kul_dcmtags "${seq_file}"
sub_bids_T1="{\"dataType\": \"anat\", \"modalityLabel\": \"T1w\", \"criteria\": $ss ,
\"customLabels\": \"ce-gadolinium\",
\"sidecarChanges\": {\"KUL_dcm2bids\": \"yes\",\"ContrastBolusIngredient\": \"gadolinium\"}
}"
sub_bids_[$bs]=$(echo ${sub_bids_T1} | python -m json.tool )
fi
fi
if [[ ${identifier} == "T2w" ]]; then
kul_find_relevant_dicom_file
if [ $seq_found -eq 1 ]; then
# read the relevant dicom tags
kul_dcmtags "${seq_file}"
sub_bids_T2="{\"dataType\": \"anat\", \"modalityLabel\": \"T2w\", \"criteria\": $ss }"
sub_bids_[$bs]=$(echo ${sub_bids_T2} | python -m json.tool)
fi
fi
if [[ ${identifier} == "PDw" ]]; then
kul_find_relevant_dicom_file
if [ $seq_found -eq 1 ]; then
# read the relevant dicom tags
kul_dcmtags "${seq_file}"
sub_bids_PD="{\"dataType\": \"anat\", \"modalityLabel\": \"PDw\", \"criteria\": $ss }"
sub_bids_[$bs]=$(echo ${sub_bids_PD} | python -m json.tool)
fi
fi
if [[ ${identifier} == "FGATIR" ]]; then
kul_find_relevant_dicom_file