-
Notifications
You must be signed in to change notification settings - Fork 7
/
toast.in
1614 lines (1432 loc) · 78.4 KB
/
toast.in
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
# TOAST: TurbO quasar asl bayesian AnalySis Tool
#
# Moss Zhao, Michael Chappell, QUBIC Group, IBME, University of Oxford Image
#
# Copyright (c) 2018-2018 University of Oxford
#
# SHCOPYRIGHT
# Make script use local copies of helper scripts/programs in the same
# directory, if present. This allows for multiple versions of the scripts
# to be used, possibly with bundled dependencies
if [ -z "${FSLDEVDIR}" ]; then
FSLPATH="${FSLDIR}/bin"
else
FSLPATH="${FSLDEVDIR}/bin:${FSLDIR}/bin"
fi
PATH=`dirname $0`:${FSLPATH}:${PATH}
abspath() {
# Return an absolute path if the input is relative
cd "$(dirname "$1")"
printf "%s/%s\n" "$(pwd)" "$(basename "$1")"
}
Usage() {
# For future development, I have commented some potentially useful options
echo "TOAST: TurbO quasar asl bayesian AnalySis Tool"
echo "Beta version: it only works for AMC's file format:"
echo "The input file should be one single file and the t dimension should be in the following order:"
echo "Shift 1 Repeat 1 Tag, Shift 1 Repeat 1 Control, Shift 2 Repeat 1 Tag, Shift 2 Repeat 1 Control."
echo ""
echo "Usage (optional parameters in {}):"
echo "Example (Acquired M0 method in the paper): toast -i <input file> -o <output directory> --struct <High resolution structural data> --calib <M0 file> --tr <TR of M0> --corrcal"
echo "Example (Estimated M0 method in the paper): toast -i <input file> -o <output directory> --infert1 --corrcal"
echo "Example (To estimate ABV, use the previous two methods with this additional option): --inferart"
echo "Example (To correct bolus duration, use the previous two methods with this additional option): --tau <bolus duration from PC MRI data> --infertau"
echo " -i : specify data file"
echo " {-o} : specify output directory"
echo " {-m} : specify brain mask file"
echo ""
echo " Extended options:"
echo " --calib : include a calibration image (this will automatically tigger the --trans option)"
echo " --transoff : transform the calibration image to the current data resolution"
echo " --tr : TR of the calibration image. It needs to be corrected if less than 5.0 sec {default: 5.0 sec}"
echo " --struct : structural image (T1 or T2 weighted)"
echo " --t1 : Set the value for T1 of tissue {default: 1.3 sec}"
echo " --infert1 : Infer T1 of tissue from Turbo QUASAR ASL control data"
echo " --t1b : Set the value for T1 of arterial blood {default: 1.6 sec}"
echo " --corrcal : Correct partial volume effects on the edge of calibration image M0a"
echo " --infertau : estimate bolus duration from data"
echo " --taulowest: lowest possible bolus duration {default: 0.2 sec}"
echo " --inferart : estimate arterial blood volume (ABV) from the arterial blood component"
# Future development
#echo " --disp : include bolus dispersion in the model; choices: none(default), gamma"
# Future development
#echo " --mfree : Do model-free (SVD deconvolution) analysis"
# Future development of partial volume correction options
#echo ""
#echo " Partial volume effects correction options:"
#echo " --pvcorr : Set partial volume effect correction on. You should provide high resolution partial volume estimates and a structural image."
#echo " --fslanat : Name of the directory containing the output from fsl_anat"
#echo " --t1wm : T1 for WM {default: 1.1 s}"
#echo ""
echo " Sequence parameters:"
echo " --tau : Set bolus duration {default: 0.6 s}"
echo " --slicedt : Set the increase in TI with slice {default: 0.035 s}"
echo " --alpha : Inversion efficiency {default: 0.91}"
echo " --lambda : Blood-water coefficient factor {default: 0.9}"
echo " --fa : Flip angle for LL readout {default: 35 degrees}"
echo " --lfa : Lower flip angle for final phase of data {default: 11.7 degrees}"
echo " --tis : comma separated list of TI values"
echo " {default: 0.03,0.63,1.23,1.83,2.43,3.03,3.63,4.23,4.83,5.43,6.03}"
echo " --shift : slice shifting factor {default: 2}"
echo " --break_1 : slice number of first acquisition point (start from 0) {default: 0}"
echo " --break_2 : slice number of middle acquisition point (start from 0) {default: 7}"
echo " --break_3 : slice number of last acquisition point (start from 0) {default: 14}"
echo " --taupat : comma separated list of bolus pattern. 1: label, 0: skip"
echo " {default: 1, 1, 1, 1, 1, 1}"
echo ""
}
Version() {
echo "@GIT_SHA1@ @GIT_DATE@"
exit 0
}
# deal with options
if [ -z $1 ]; then
Usage
exit 1
fi
until [ -z $1 ]; do
# look at this option and determine if has an argument specified by an =
option=`echo $1 | sed s/=.*//`
arg="" #specifies if an argument is to be read from next item on command line (=1 is is when = is used)
if [ $option = $1 ]; then
# no argument to this command has been found with it (i.e. after an =)
# if there is an argument it will be the next option
argument=$2
else
arg=1
argument=`echo $1 | sed s/.*=//`
fi
takeargs=0;boolarg="";isbool="";
case $option in
-o) outflag=1 outdir=$argument
shift;;
-i) inflag=1 infile=$argument #input/data file
shift;;
-m) mask=$argument
shift;;
--calib) calib_data=$argument
trans_status=1 # Need to perform transformation of the calibration image
shift;;
--transoff) trans_status=0 # No need to perform transformation of the calibration image
;;
--tr) TR_calib=$argument # TR of the calibration image (default is 5)
shift;;
--struct) data_struct=$argument
shift;;
--t1b) t1b=$argument
shift;;
--t1) t1=$argument
shift;;
--infert1) infert1=1
;;
--t1wm) t1wm=$argument
shift;;
--slicedt) slicedt=$argument
shift;;
--fa) fa=$argument
shift;;
--lfa) lfa=$argument
shift;;
--shift) shift_factor=$argument
shift;;
--break_1) break_1=$argument
shift;;
--break_2) break_2=$argument
shift;;
--break_3) break_3=$argument
shift;;
--disp) disp=$argument
shift;;
#--mfree) mfree=1
# ;;
#--edgecorr) isbool=1;
# boolarg=edgecorr;
# ;;
--tis) tis=$argument
shift;;
#--iform) iform=$argument # to choose the input form of the data
# shift;;
--tau) tau=$argument
shift;;
--corrcal) corrcal=1
;;
--alpha) alpha=$argument
shift;;
--lambda) lambda=$argument
shift;;
--infertau) infertau=1
;;
--taulowest) tau_lowest=$argument
shift;;
--inferart) infer_arterial=1
;;
--pvcorr) pvcorr=1
;;
#--pvgm) pvgm_highres=$argument
#shift;;
#--pvwm) pvwm_highres=$argument
#shift;;
#--s) strim=$argument # structure images for registration and partial volume estimation
#shift;;
#--fslanat) fslanat=$argument # Directory containing the output from fsl_anat
# shift;;
--ccmds) calibcmds=$argument
shift;;
--debug) debug=1 #debugging option
;;
--version) Version
;;
*) Usage
echo "Error! Unrecognised option on command line: $1"
echo ""
exit 1;;
esac
# sort out a shift required by a command line option that takes arguments
if [ -z $arg ]; then
# an argument has been supplied on the command NOT using an =
if [ $takeargs -eq 1 ]; then
shift;
fi
fi
if [ ! -z $isbool ]; then
# this is an (explicit) boolean setting
if [ ! -z $arg ]; then
# an argument has been supplied on the command using an =
# set the variable based on the argument
case $argument in
on) eval $boolarg=1
;;
off) eval $boolarg=""
;;
1) eval $boolarg=1
;;
0) eval $boolarg=""
;;
*) Usage
echo "Error! Unrecognised setting for boolean option: $1"
echo ""
exit 1;;
esac
else
# no argument has been suppled with this command (NOTE that you cannot supply an arugment to a bool option without an =)
# this sets the variable to true
eval $boolarg=1;
fi
fi
shift
done
#### --- Procedural ---
asl_file=asl_file
fabber=fabber_asl
asl_mfree=asl_mfree ###~/cproject/asl_mfree/asl_mfree
#### --- Housekeeping ---
# set the output directory here if not specified
if [ -z $outflag ]; then
echo "Ouput being placed in basil subdirectory of input directory"
outdir=$indir/quasil;
fi
# Start by looking for the output directory (and create if need be)
count=0
while [ -d $outdir ]; do
outdir=$outdir"+"
count=`expr $count + 1`
if [ $count -gt 20 ]; then
echo "Error: $outdir too many existing output directories (i.e. shall not add another +)"
exit
fi
done
echo "Creating output directory: $outdir"
mkdir $outdir;
# save the starting directory
stdir=`pwd`
# Full path of output directory
outdir=$(abspath $outdir)
# make a temp directory to work in
#tmpbase=`tmpnam`
#tmpbase="haha"
tmpbase="turquat_temp_"
timestamp=$(date +"%H%M%S")
#tempdir=${tmpbase}_turquat
tempdir=$tmpbase$timestamp
mkdir $tempdir
# deal with the TIs
if [ -z $tis ]; then
# default QUASAR list of TIs
tis="0.03,0.63,1.23,1.83,2.43,3.03,3.63,4.23,4.83,5.43,6.03"
fi
# deal with bolus patterns
if [ -z $taupat ]; then
# default QUASAR list of TIs
taupat="1,1,1,1,1,1"
fi
# Create TI list
count=0
tislist=""
tislist_calib="" # only the first four TIs
thetis=`echo $tis | sed 's:,: :g'`
for ti in $thetis; do
count=`expr ${count} + 1`
tislist=`echo $tislist --ti${count}=$ti`
if [ "$count" -le "4" ]; then
tislist_calib=`echo $tislist_calib --ti${count}=$ti`
fi
done
# echo "TIs list: $tislist" >> $log
ntis=$count;
# TIs for MT and non MT effects
# Hard coded. Can Martin fix this?
tislist_calib_MT="--ti1=0.03 --ti2=0.63 --ti3=1.23 --ti4=1.83 --ti5=2.43 --ti6=3.03"
tislist_calib_non_MT="--ti1=3.63 --ti2=4.23 --ti3=4.83 --ti4=5.34 --ti5=6.03"
TI_start_of_MT=0 # TI = 0. Essentially the last signal with MT effects. Also the starting point of non MT saturation recovery
TI_start_of_non_MT=3.03 # TI = 6. Essentially the last signal with MT effects. Also the starting point of non MT saturation recovery
# Create bolus pattern list
count=0
taulist=""
thetaus=`echo $taupat | sed 's:,: :g'`
for current_taupat in $thetaus; do
count=`expr ${count} + 1`
bolus_pattern_list=`echo $bolus_pattern_list --bolus_${count}=$current_taupat`
done
# echo "bolus pattern list: $bolus_pattern_list" >> $log
ntis=$count;
if [ -z $iform ]; then
iform="q"
fi
# parameters
#bolus duration - default 0.64 s
if [ -z $tau ]; then
tau=0.6;
fi
#T1b
if [ -z $t1b ]; then
t1b=1.6;
fi
#T1 - this si the prior value, since T1 will be estimated from the data
if [ -z $t1 ]; then
t1=1.3;
fi
#T1WM
if [ -z $t1wm ]; then
t1wm=1.1;
fi
# calibration parameters
if [ -z $TR_calib ]; then
TR_calib=5;
fi
# sequence parameters
# slicedt
if [ -z $slicedt ]; then
slicedt=0.036;
fi
# Flip angles
if [ -z $fa ]; then
fa=35;
fi
if [ -z $lfa ]; then
lfa=11.7;
fi
if [ -z $alpha ]; then
alpha=0.91;
fi
if [ -z $lambda ]; then
lambda=0.9;
fi
if [ -z $disp ]; then
disp=none;
fi
# Slice shifting factor
if [ -z $shift_factor ]; then
shift_factor=1;
fi
if [ -z $break_1 ]; then
break_1=0;
break_1_result=$break_1;
fi
if [ -z $break_2 ]; then
break_2=7;
break_2_result=`expr $break_2 + 1`;
fi
if [ -z $break_3 ]; then
break_3=14;
break_3_result=$break_3;
fi
# Compute segment length
segment_1_length=`expr $break_2 - $break_1`
segment_2_length=`expr $break_3 - $break_2 + 1`
segment_1_length_result=`expr $break_2_result - $break_1_result`
segment_2_length_result=`expr $break_3_result - $break_2_result + 1`
#### --- Pre-processing ---
echo "Pre-processing"
imcp $infile $tempdir/data
#if [ ! -z $fslanat ]; then
# cp -R $fslanat $tempdir
#fi
cd $tempdir
# Re-arrange the file
# Split the input file into different repeats and shifts
shift_1_repeat_1_tag="shift_1_repeat_1_tag"
shift_1_repeat_1_control="shift_1_repeat_1_control"
shift_1_repeat_1_diff="shift_1_repeat_1_diff"
shift_1_repeat_1_tissue="shift_1_repeat_1_tissue"
shift_1_repeat_1_blood="shift_1_repeat_1_blood"
shift_2_repeat_1_tag="shift_2_repeat_1_tag"
shift_2_repeat_1_control="shift_2_repeat_1_control"
shift_2_repeat_1_diff="shift_2_repeat_1_diff"
shift_2_repeat_1_control_effective="shift_2_repeat_1_control_effective"
shift_2_repeat_1_tag_effective="shift_2_repeat_1_tag_effective"
shift_2_repeat_1_diff_effective="shift_2_repeat_1_diff_effective"
mask_shift_2_effective="mask_shift_2_effective"
shift_2_repeat_1_tissue="shift_2_repeat_1_tissue"
shift_2_repeat_1_blood="shift_2_repeat_1_blood"
fslroi data $shift_1_repeat_1_tag 0 77
fslroi data $shift_1_repeat_1_control 77 77
fslroi data $shift_2_repeat_1_tag 154 77
fslroi data $shift_2_repeat_1_control 231 77
# Create a mask for the whole data. We use shift 1 repeat 1 control data
if [ -z $mask ]; then
# auto generate mask
echo "Creating mask from ASL control data"
fslmaths $shift_1_repeat_1_control -Tmean aslmean
bet aslmean mask
fslmaths mask -bin mask
else
cd $stdir
imcp $mask $tempdir/mask
cd $tempdir
fi
mask=mask
# copy mask to output for future reference
cd $stdir
imcp $tempdir/mask $outdir/mask
cd $tempdir
# Rearrange the slices for shift 2
# Do control image first
# Shift 2 We need to split and rearrange the z direction
# Control
fslroi $shift_2_repeat_1_control data_segment_1 0 64 0 64 $break_1 $segment_1_length
fslroi $shift_2_repeat_1_control data_segment_2 0 64 0 64 $break_2 $segment_2_length
fslmerge -z $shift_2_repeat_1_control_effective data_segment_2 data_segment_1 # Segment 2 was scanned first
# Tag
fslroi $shift_2_repeat_1_tag data_segment_1 0 64 0 64 $break_1 $segment_1_length
fslroi $shift_2_repeat_1_tag data_segment_2 0 64 0 64 $break_2 $segment_2_length
fslmerge -z $shift_2_repeat_1_tag_effective data_segment_2 data_segment_1 # Segment 2 was scanned first
# Mask
fslroi $mask data_segment_1 0 64 0 64 $break_1 $segment_1_length
fslroi $mask data_segment_2 0 64 0 64 $break_2 $segment_2_length
fslmerge -z $mask_shift_2_effective data_segment_2 data_segment_1 # Segment 2 was scanned first
# Take tag control differences
fslmaths $shift_1_repeat_1_tag -sub $shift_1_repeat_1_control $shift_1_repeat_1_diff
fslmaths $shift_2_repeat_1_tag_effective -sub $shift_2_repeat_1_control_effective $shift_2_repeat_1_diff_effective
# Extract Tissue and Arterial blood components
n_dynamics=7
# There are seven dynamics (last one is low flip angle, discard)
# Shift 1
$asl_file --data=$shift_1_repeat_1_diff --ntis=$n_dynamics --ibf=tis --iaf=diff --split=data_diff_ph_
# Tissue component
fslmaths data_diff_ph_000 -add data_diff_ph_001 -add data_diff_ph_003 -add data_diff_ph_004 -div 4 $shift_1_repeat_1_tissue
# Arterial component
fslmaths data_diff_ph_002 -add data_diff_ph_005 -div 2 -sub $shift_1_repeat_1_tissue $shift_1_repeat_1_blood
# Remove the intermediate images
imrm data_diff_ph_000 data_diff_ph_001 data_diff_ph_002 data_diff_ph_003 data_diff_ph_004 data_diff_ph_005 data_diff_ph_006
# Shift 2
$asl_file --data=$shift_2_repeat_1_diff_effective --ntis=$n_dynamics --ibf=tis --iaf=diff --split=data_diff_ph_
# Tissue component
fslmaths data_diff_ph_000 -add data_diff_ph_001 -add data_diff_ph_003 -add data_diff_ph_004 -div 4 $shift_2_repeat_1_tissue
# Arterial component
fslmaths data_diff_ph_002 -add data_diff_ph_005 -div 2 -sub $shift_2_repeat_1_tissue $shift_2_repeat_1_blood
# Remove the intermediate images
imrm data_diff_ph_000 data_diff_ph_001 data_diff_ph_002 data_diff_ph_003 data_diff_ph_004 data_diff_ph_005 data_diff_ph_006
# Here we need to estimate T1 and flip angle correction (g) values using the control data
# We need to split the control data in to MT (first 6 TI) and non MT (last 5 TI) parts
# Now estimate T1 and M0 from Shift 1
$asl_file --data=$shift_1_repeat_1_control --ntis=$n_dynamics --ibf=tis --iaf=diff --split=data_control_
# Split the TIs into MT and non-MT parts. First six TIs have MT, last five TIs no MT
last_TI_1=0
last_TI_2=6
length_1=6
length_2=5
# First six dynamics contains high flip angle signal
fslroi data_control_000 data_control_0_m_1 $last_TI_1 $length_1
fslroi data_control_000 data_control_0_m_2 $last_TI_2 $length_2
fslroi data_control_001 data_control_1_m_1 $last_TI_1 $length_1
fslroi data_control_001 data_control_1_m_2 $last_TI_2 $length_2
fslroi data_control_002 data_control_2_m_1 $last_TI_1 $length_1
fslroi data_control_002 data_control_2_m_2 $last_TI_2 $length_2
fslroi data_control_003 data_control_3_m_1 $last_TI_1 $length_1
fslroi data_control_003 data_control_3_m_2 $last_TI_2 $length_2
fslroi data_control_004 data_control_4_m_1 $last_TI_1 $length_1
fslroi data_control_004 data_control_4_m_2 $last_TI_2 $length_2
fslroi data_control_005 data_control_5_m_1 $last_TI_1 $length_1
fslroi data_control_005 data_control_5_m_2 $last_TI_2 $length_2
fslroi data_control_006 data_control_shift_1_MT_low_fa $last_TI_1 $length_1 # Dynamic 7 contains low flip angle signal
fslroi data_control_006 data_control_shift_1_non_MT_low_fa $last_TI_2 $length_2
echo "Split data into MT and non MT parts..."
# Now take the mean of MT and non-MT part
# You have to use two dynamics (repeats) of the high flip angle data to minimize the MT effects (See Turbo QUASAR paper)
fslmaths data_control_0_m_1 -add data_control_1_m_1 -add data_control_2_m_1 -add data_control_3_m_1 -add data_control_4_m_1 -add data_control_5_m_1 -div 6 data_control_shift_1_MT_high_fa
fslmaths data_control_0_m_2 -add data_control_1_m_2 -add data_control_2_m_2 -add data_control_3_m_2 -add data_control_4_m_2 -add data_control_5_m_2 -div 6 data_control_shift_1_non_MT_high_fa
# Now merge with the low flip angle data
fslmerge -t data_control_shift_1_MT data_control_shift_1_MT_high_fa data_control_shift_1_MT_low_fa
fslmerge -t data_control_shift_1_non_MT data_control_shift_1_non_MT_high_fa data_control_shift_1_non_MT_low_fa
# You also need two initial points for the two part of the data, ie. where the saturation signal recovers from
# For the data with MT effects, the initial signal is zero
# For the data without MT effects, the initial signal is TI = 6
initial_point=5 # TI = 6, starts from zero
initial_length=1 # We only need one point
fslroi data_control_000 data_initial_000 $initial_point $initial_length
fslroi data_control_001 data_initial_001 $initial_point $initial_length
fslroi data_control_002 data_initial_002 $initial_point $initial_length
fslroi data_control_003 data_initial_003 $initial_point $initial_length
fslroi data_control_004 data_initial_004 $initial_point $initial_length
fslroi data_control_005 data_initial_005 $initial_point $initial_length
fslroi data_control_006 data_initial_shift_1_non_MT_low_fa $initial_point $initial_length # The initial signal of the low FA curve without MT effects
# Now compute the mean of the first six dynamics (high FA)
fslmaths data_initial_000 -add data_initial_001 -add data_initial_002 -add data_initial_003 -add data_initial_004 -add data_initial_005 -div 6 data_initial_shift_1_non_MT_high_fa
# Now do the initial signal with MT effects. It should starts from zero but we make it a very low value for easier fitting
fslmaths data_initial_shift_1_non_MT_high_fa -mul 0 -add 0.0000001 -mas mask data_initial_shift_1_MT_high_fa
fslmaths data_initial_shift_1_non_MT_low_fa -mul 0 -add 0.0000001 -mas mask data_initial_shift_1_MT_low_fa
echo "Finished spliting data into MT and non MT parts"
# Merge the first seven TIs
#fslmerge -t shift_1_repeat_1_control_TI_4 data_control_0_m_0 data_control_1_m_0 data_control_2_m_0 data_control_3_m_0 data_control_4_m_0 data_control_5_m_0 data_control_6_m_0
# Remove the intermediate files
#imrm data_control_000 data_control_001 data_control_002 data_control_003 data_control_004 data_control_005 data_control_006
#imrm data_control_0_m_0 data_control_1_m_0 data_control_2_m_0 data_control_3_m_0 data_control_4_m_0 data_control_5_m_0 data_control_6_m_0
# TIs for MT and non MT effects
tislist_calib_MT="--ti1=0.03 --ti2=0.63 --ti3=1.23 --ti4=1.83 --ti5=2.43 --ti6=3.03"
tislist_calib_non_MT="--ti1=3.63 --ti2=4.23 --ti3=4.83 --ti4=5.34 --ti5=6.03"
# Done Moss Edit
# Prepare for a fabber option file
# We estimate these paramters:
# T1, M0t, g(FA correction factor), A(saturation efficiency)
current_options_file="options_calib_shift_1.txt"
echo "Begin T1 and M0 estimation for Shift 1 of the data (non MT part, last 5 TIs)"
echo "# Turbo QUASAR analysis calibration options" >> $current_options_file
echo "--mask=mask" >> $current_options_file
echo "--method=spatialvb" >> $current_options_file
echo "--noise=white" >> $current_options_file
echo "--model=satrecovdualfa" >> $current_options_file
echo "--repeats=1" >> $current_options_file
echo "--phases=1" >> $current_options_file
echo $tislist_calib_non_MT >> $current_options_file
echo "--t1=$t1 --FA=$fa --LFA=$lfa " >> $current_options_file
echo "--slicedt=$slicedt" >> $current_options_file
echo "--t_initial_high_FA=$TI_start_of_non_MT" >> $current_options_file # hard coded value
echo "--t_initial_low_FA=$TI_start_of_non_MT" >> $current_options_file # hard coded value
echo "--print-free-energy" >> $current_options_file
echo "--save-residuals" >> $current_options_file
echo "--save-model-fit" >> $current_options_file
echo "--PSP_byname1=M0t" >> $current_options_file
echo "--PSP_byname1_type=M" >> $current_options_file
echo "--PSP_byname2=T1t" >> $current_options_file
echo "--PSP_byname2_type=N" >> $current_options_file
echo "--PSP_byname3=A" >> $current_options_file
echo "--PSP_byname3_type=N" >> $current_options_file
echo "--PSP_byname4=g" >> $current_options_file
echo "--PSP_byname4_type=N" >> $current_options_file
echo "--PSP_byname5=M0_initial_high_fa" >> $current_options_file
echo "--PSP_byname5_type=I" >> $current_options_file
echo "--PSP_byname5_image=data_initial_shift_1_non_MT_high_fa" >> $current_options_file
echo "--PSP_byname6=M0_initial_low_fa" >> $current_options_file
echo "--PSP_byname6_type=I" >> $current_options_file
echo "--PSP_byname6_image=data_initial_shift_1_non_MT_low_fa" >> $current_options_file
# Now perform model fitting to estimate these parameters
$fabber --data=data_control_shift_1_non_MT --data-order=singlefile --output=output_calib_shift_1 -@ $current_options_file
# Here we need to estimate T1 and flip angle correction (g) values using the control data
# Create a mask for the whole data. We use shift 1 repeat 1 control data
if [ ! -z $infert1 ]; then
echo "Estimating T1 of tissue from ASL control data..."
# Now do the non MT part
# T1, M0t, g(FA correction factor), A(saturation efficiency)
current_options_file="options_calib_shift_1_MT.txt"
echo "Begin T1 and M0 estimation for Shift 1 of the data (MT part, first 6 TIs)"
echo "# Turbo QUASAR analysis calibration options" >> $current_options_file
echo "--mask=mask" >> $current_options_file
echo "--method=spatialvb" >> $current_options_file
echo "--noise=white" >> $current_options_file
echo "--model=satrecovdualfa" >> $current_options_file
echo "--repeats=1" >> $current_options_file
echo "--phases=1" >> $current_options_file
echo $tislist_calib_MT >> $current_options_file
echo "--t1=$t1 --FA=$fa --LFA=$lfa " >> $current_options_file
echo "--slicedt=$slicedt" >> $current_options_file
echo "--t_initial_high_FA=$TI_start_of_MT" >> $current_options_file # hard coded value
echo "--t_initial_low_FA=$TI_start_of_MT" >> $current_options_file # hard coded value
echo "--print-free-energy" >> $current_options_file
echo "--save-residuals" >> $current_options_file
echo "--save-model-fit" >> $current_options_file
echo "--PSP_byname1=M0t" >> $current_options_file
echo "--PSP_byname1_type=M" >> $current_options_file
echo "--PSP_byname2=T1t" >> $current_options_file
echo "--PSP_byname2_type=N" >> $current_options_file
echo "--PSP_byname3=A" >> $current_options_file
echo "--PSP_byname3_type=N" >> $current_options_file
echo "--PSP_byname4=g" >> $current_options_file
echo "--PSP_byname4_type=N" >> $current_options_file
echo "--PSP_byname5=M0_initial_high_fa" >> $current_options_file
echo "--PSP_byname5_type=I" >> $current_options_file
echo "--PSP_byname5_image=data_initial_shift_1_MT_high_fa" >> $current_options_file
echo "--PSP_byname6=M0_initial_low_fa" >> $current_options_file
echo "--PSP_byname6_type=I" >> $current_options_file
echo "--PSP_byname6_image=data_initial_shift_1_MT_low_fa" >> $current_options_file
# Now perform model fitting to estimate these parameters
$fabber --data=data_control_shift_1_MT --data-order=singlefile --output=output_calib_shift_1_MT -@ $current_options_file
else
echo "Use user provided T1 of tissue in CBF quantification..."
fslmaths output_calib_shift_1/mean_T1t -mul 0 -add $t1 -mas mask output_calib_shift_1/mean_T1t
fi
# Now estimate T1 and M0 from shift 2
$asl_file --data=$shift_2_repeat_1_control_effective --ntis=$n_dynamics --ibf=tis --iaf=diff --split=data_control_
# Split the TIs into MT and non-MT parts. First six TIs have MT, last five TIs no MT
last_TI_1=0
last_TI_2=6
length_1=6
length_2=5
fslroi data_control_000 data_control_0_m_1 $last_TI_1 $length_1
fslroi data_control_000 data_control_0_m_2 $last_TI_2 $length_2
fslroi data_control_001 data_control_1_m_1 $last_TI_1 $length_1
fslroi data_control_001 data_control_1_m_2 $last_TI_2 $length_2
fslroi data_control_002 data_control_2_m_1 $last_TI_1 $length_1
fslroi data_control_002 data_control_2_m_2 $last_TI_2 $length_2
fslroi data_control_003 data_control_3_m_1 $last_TI_1 $length_1
fslroi data_control_003 data_control_3_m_2 $last_TI_2 $length_2
fslroi data_control_004 data_control_4_m_1 $last_TI_1 $length_1
fslroi data_control_004 data_control_4_m_2 $last_TI_2 $length_2
fslroi data_control_005 data_control_5_m_1 $last_TI_1 $length_1
fslroi data_control_005 data_control_5_m_2 $last_TI_2 $length_2
fslroi data_control_006 data_control_shift_2_MT_low_fa $last_TI_1 $length_1
fslroi data_control_006 data_control_shift_2_non_MT_low_fa $last_TI_2 $length_2
echo "Split data into MT and non MT parts..."
# Now take the mean of MT and non-MT part
# You have to use two dynamics (repeats) of the high flip angle data to minimize the MT effects (See Turbo QUASAR paper)
fslmaths data_control_0_m_1 -add data_control_1_m_1 -add data_control_2_m_1 -add data_control_3_m_1 -add data_control_4_m_1 -add data_control_5_m_1 -div 6 data_control_shift_2_MT_high_fa
fslmaths data_control_0_m_2 -add data_control_1_m_2 -add data_control_2_m_2 -add data_control_3_m_2 -add data_control_4_m_2 -add data_control_5_m_2 -div 6 data_control_shift_2_non_MT_high_fa
# Now merge with the low flip angle data
fslmerge -t data_control_shift_2_MT data_control_shift_2_MT_high_fa data_control_shift_2_MT_low_fa
fslmerge -t data_control_shift_2_non_MT data_control_shift_2_non_MT_high_fa data_control_shift_2_non_MT_low_fa
# You also need two initial points for the two part of the data, ie. where the saturation signal recovers from
# For the data with MT effects, the initial signal is zero
# For the data without MT effects, the initial signal is TI = 6
initial_point=5 # TI = 6, starts from zero
initial_length=1 # We only need one point
fslroi data_control_000 data_initial_000 $initial_point $initial_length
fslroi data_control_001 data_initial_001 $initial_point $initial_length
fslroi data_control_002 data_initial_002 $initial_point $initial_length
fslroi data_control_003 data_initial_003 $initial_point $initial_length
fslroi data_control_004 data_initial_004 $initial_point $initial_length
fslroi data_control_005 data_initial_005 $initial_point $initial_length
fslroi data_control_006 data_initial_shift_2_non_MT_low_fa $initial_point $initial_length # The initial signal of the low FA curve without MT effects
# Now compute the mean of the first six dynamics (high FA)
fslmaths data_initial_000 -add data_initial_001 -add data_initial_002 -add data_initial_003 -add data_initial_004 -add data_initial_005 -div 6 data_initial_shift_2_non_MT_high_fa
# Now do the initial signal with MT effects. It should starts from zero but we make it a very low value for easier fitting
fslmaths data_initial_shift_2_non_MT_high_fa -mul 0 -add 0.0000001 -mas mask_shift_2_effective data_initial_shift_2_MT_high_fa
fslmaths data_initial_shift_2_non_MT_low_fa -mul 0 -add 0.0000001 -mas mask_shift_2_effective data_initial_shift_2_MT_low_fa
echo "Finished spliting data into MT and non MT parts"
# Merge the first seven TIs
#fslmerge -t shift_1_repeat_1_control_TI_4 data_control_0_m_0 data_control_1_m_0 data_control_2_m_0 data_control_3_m_0 data_control_4_m_0 data_control_5_m_0 data_control_6_m_0
# Remove the intermediate files
#imrm data_control_000 data_control_001 data_control_002 data_control_003 data_control_004 data_control_005 data_control_006
#imrm data_control_0_m_0 data_control_1_m_0 data_control_2_m_0 data_control_3_m_0 data_control_4_m_0 data_control_5_m_0 data_control_6_m_0
# Done Moss Edit
# Prepare for a fabber option file
# We estimate these paramters:
# T1, M0t, g(FA correction factor), A(saturation efficiency)
current_options_file="options_calib_shift_2.txt"
echo "Begin T1 and M0 estimation for Shift 2 of the data (non MT part, last 5 TIs)"
echo "# Turbo QUASAR analysis calibration options" >> $current_options_file
echo "--mask=mask_shift_2_effective" >> $current_options_file
echo "--method=spatialvb" >> $current_options_file
echo "--noise=white" >> $current_options_file
echo "--model=satrecovdualfa" >> $current_options_file
echo "--repeats=1" >> $current_options_file
echo "--phases=1" >> $current_options_file
echo $tislist_calib_non_MT >> $current_options_file
echo "--t1=$t1 --FA=$fa --LFA=$lfa " >> $current_options_file
echo "--slicedt=$slicedt" >> $current_options_file
echo "--t_initial_high_FA=$TI_start_of_non_MT" >> $current_options_file # hard coded value
echo "--t_initial_low_FA=$TI_start_of_non_MT" >> $current_options_file # hard coded value
echo "--print-free-energy" >> $current_options_file
echo "--save-residuals" >> $current_options_file
echo "--save-model-fit" >> $current_options_file
echo "--PSP_byname1=M0t" >> $current_options_file
echo "--PSP_byname1_type=M" >> $current_options_file
echo "--PSP_byname2=T1t" >> $current_options_file
echo "--PSP_byname2_type=N" >> $current_options_file
echo "--PSP_byname3=A" >> $current_options_file
echo "--PSP_byname3_type=N" >> $current_options_file
echo "--PSP_byname4=g" >> $current_options_file
echo "--PSP_byname4_type=N" >> $current_options_file
echo "--PSP_byname5=M0_initial_high_fa" >> $current_options_file
echo "--PSP_byname5_type=I" >> $current_options_file
echo "--PSP_byname5_image=data_initial_shift_2_non_MT_high_fa" >> $current_options_file
echo "--PSP_byname6=M0_initial_low_fa" >> $current_options_file
echo "--PSP_byname6_type=I" >> $current_options_file
echo "--PSP_byname6_image=data_initial_shift_2_non_MT_low_fa" >> $current_options_file
# Now perform model fitting to estimate these parameters
$fabber --data=data_control_shift_2_non_MT --data-order=singlefile --output=output_calib_shift_2 -@ $current_options_file
# Here we need to estimate T1 and flip angle correction (g) values using the control data
# Create a mask for the whole data. We use shift 1 repeat 1 control data
if [ ! -z $infert1 ]; then
echo "Estimating T1 of tissue from ASL control data..."
# Now do the non MT part
# T1, M0t, g(FA correction factor), A(saturation efficiency)
current_options_file="options_calib_shift_2_MT.txt"
echo "Begin T1 and M0 estimation for Shift 2 of the data (MT part, first 5 TIs)"
echo "# Turbo QUASAR analysis calibration options" >> $current_options_file
echo "--mask=mask_shift_2_effective" >> $current_options_file
echo "--method=spatialvb" >> $current_options_file
echo "--noise=white" >> $current_options_file
echo "--model=satrecovdualfa" >> $current_options_file
echo "--repeats=1" >> $current_options_file
echo "--phases=1" >> $current_options_file
echo $tislist_calib_MT >> $current_options_file
echo "--t1=$t1 --FA=$fa --LFA=$lfa " >> $current_options_file
echo "--slicedt=$slicedt" >> $current_options_file
echo "--t_initial_high_FA=$TI_start_of_MT" >> $current_options_file # hard coded value
echo "--t_initial_low_FA=$TI_start_of_MT" >> $current_options_file # hard coded value
echo "--print-free-energy" >> $current_options_file
echo "--save-residuals" >> $current_options_file
echo "--save-model-fit" >> $current_options_file
echo "--PSP_byname1=M0t" >> $current_options_file
echo "--PSP_byname1_type=M" >> $current_options_file
echo "--PSP_byname2=T1t" >> $current_options_file
echo "--PSP_byname2_type=N" >> $current_options_file
echo "--PSP_byname3=A" >> $current_options_file
echo "--PSP_byname3_type=N" >> $current_options_file
echo "--PSP_byname4=g" >> $current_options_file
echo "--PSP_byname4_type=N" >> $current_options_file
echo "--PSP_byname5=M0_initial_high_fa" >> $current_options_file
echo "--PSP_byname5_type=I" >> $current_options_file
echo "--PSP_byname5_image=data_initial_shift_2_MT_high_fa" >> $current_options_file
echo "--PSP_byname6=M0_initial_low_fa" >> $current_options_file
echo "--PSP_byname6_type=I" >> $current_options_file
echo "--PSP_byname6_image=data_initial_shift_2_MT_low_fa" >> $current_options_file
# Now perform model fitting to estimate these parameters
$fabber --data=data_control_shift_2_MT --data-order=singlefile --output=output_calib_shift_2_MT -@ $current_options_file
else
echo "Use user provided T1 of tissue in CBF quantification..."
fslmaths output_calib_shift_2/mean_T1t -mul 0 -add $t1 -mas mask output_calib_shift_2/mean_T1t
fi
# Done Moss edit
# Done estimating T1 and M0
# Model based analysis
# Prepare for a fabber option file
if [ ! -z $infert1 ]; then
# First we copy the original images
imcp $shift_1_repeat_1_tissue shift_1_repeat_1_tissue_original
imcp $shift_2_repeat_1_tissue shift_2_repeat_1_tissue_original
imcp $shift_1_repeat_1_blood shift_1_repeat_1_blood_original
imcp $shift_2_repeat_1_blood shift_2_repeat_1_blood_original
# Get the tissue with MT signals
fslroi $shift_1_repeat_1_tissue shift_1_repeat_1_tissue_MT $last_TI_1 $length_1
fslroi $shift_2_repeat_1_tissue shift_2_repeat_1_tissue_MT $last_TI_1 $length_1
fslroi $shift_1_repeat_1_blood shift_1_repeat_1_blood_MT $last_TI_1 $length_1
fslroi $shift_2_repeat_1_blood shift_2_repeat_1_blood_MT $last_TI_1 $length_1
shift_1_repeat_1_tissue_MT=shift_1_repeat_1_tissue_MT
shift_2_repeat_1_tissue_MT=shift_2_repeat_1_tissue_MT
shift_1_repeat_1_blood_MT=shift_1_repeat_1_blood_MT
shift_2_repeat_1_blood_MT=shift_2_repeat_1_blood_MT
# Get the tissue with non MT signals
fslroi $shift_1_repeat_1_tissue $shift_1_repeat_1_tissue $last_TI_2 $length_2
fslroi $shift_2_repeat_1_tissue $shift_2_repeat_1_tissue $last_TI_2 $length_2
fslroi $shift_1_repeat_1_blood $shift_1_repeat_1_blood $last_TI_2 $length_2
fslroi $shift_2_repeat_1_blood $shift_2_repeat_1_blood $last_TI_2 $length_2
# Use only TIs with MT effects
tislist_MT=$tislist_calib_MT
tislist=$tislist_calib_non_MT
#tislist_MT=$tislist_calib_MT
# Priors
calib_shift_1_dir_MT=output_calib_shift_1_MT
calib_shift_2_dir_MT=output_calib_shift_2_MT
calib_shift_1_dir=output_calib_shift_1
calib_shift_2_dir=output_calib_shift_2
# We also need a T1 of blood under MT effects
# We assume this relationship T1_tissue/T1_tissue_MT = T1_blood/T1_blood_MT
# So T1_blood_MT = T1_tissue_MT * T1_blood / T1_tissue
#fslmaths output_calib_shift_1_MT/mean_T1t -mul $t1b -div $t1 -mas mask output_calib_shift_1_MT/mean_T1b
#fslmaths output_calib_shift_2_MT/mean_T1t -mul $t1b -div $t1 -mas mask_shift_2_effective output_calib_shift_2_MT/mean_T1b
#fslmaths output_calib_shift_1_MT/mean_T1t -mul $t1b -div output_calib_shift_1/mean_T1t -mas mask output_calib_shift_1_MT/mean_T1b
#fslmaths output_calib_shift_2_MT/mean_T1t -mul $t1b -div output_calib_shift_2/mean_T1t -mas mask_shift_2_effective output_calib_shift_2_MT/mean_T1b
# Use full data simple quantification
else
# Priors
calib_shift_1_dir=output_calib_shift_1
calib_shift_2_dir=output_calib_shift_2
fi
echo "Begin model-based analysis"
# Fixed bolus duration
# Condition not to infer bolus duration
# Shift 1
current_options_file="options_tissue_shift_1.txt"
echo "# Turbo QUASAR ASL tissue component analysis options for Shift 1 of the data" > $current_options_file
echo "--mask=mask" >> $current_options_file
echo "--method=spatialvb" >> $current_options_file
echo "--noise=white" >> $current_options_file
echo "--model=turboquasar" >> $current_options_file
echo "--disp=$disp" >> $current_options_file
#echo "--inferart" >> $current_options_file
echo "--repeats=1" >> $current_options_file
echo $tislist >> $current_options_file
echo "--t1=$t1 --t1b=$t1b --t1wm=$t1wm --tau=$tau --fa=$fa " >> $current_options_file
#if [ ! -z $infert1 ]; then
# #echo "--t1_MT=$t1 --t1b_MT=$t1b " >> $current_options_file
#fi
echo "--slicedt=$slicedt" >> $current_options_file
echo $bolus_pattern_list >> $current_options_file
echo "--slice_shift=$shift_factor" >> $current_options_file
echo "--onephase" >> $current_options_file
if [ ! -z $infert1 ]; then
echo "--usecalib " >> $current_options_file # We incorporate previously estimated T1 and g value as priors
echo "--infert1" >> $current_options_file # If we infer T1 we must infer T1 of tissue and blood together (must be two of them)
fi
#echo "--artdir" >> $current_options_file
# Save model fitting results and residue
echo "--save-model-fit" >> $current_options_file
echo "--print-free-energy" >> $current_options_file
echo "--save-residuals" >> $current_options_file
# Here we create a shortcut to the latest results directory
#echo "--link-to-latest" >> $current_options_file
# Here are the settings
echo "--PSP_byname1=ftiss" >> $current_options_file # Estimate CBF
echo "--PSP_byname1_type=M" >> $current_options_file
echo "--PSP_byname2=delttiss" >> $current_options_file # Estimate ATT
echo "--PSP_byname2_type=N" >> $current_options_file
echo "--PSP_byname3=sp_log" >> $current_options_file # Estimate sp log (dispersion parameter, even if dispersion is none)
echo "--PSP_byname3_type=N" >> $current_options_file
echo "--PSP_byname4=s_log" >> $current_options_file # Estimate s log (dispersion parameter, even if dispersion is none)
echo "--PSP_byname4_type=N" >> $current_options_file
if [ ! -z $infert1 ]; then
echo "--PSP_byname5=g" >> $current_options_file # Estimate g (FA correction parameter)
echo "--PSP_byname5_type=I" >> $current_options_file
echo "--PSP_byname5_image=output_calib_shift_1/mean_g" >> $current_options_file
echo "--PSP_byname6=T_1" >> $current_options_file # Estimate T1 of tissue
echo "--PSP_byname6_type=I" >> $current_options_file
echo "--PSP_byname6_image=output_calib_shift_1/mean_T1t" >> $current_options_file
#echo "--PSP_byname7=T_1b" >> $current_options_file # Estimate T1 of blood
#echo "--PSP_byname7_type=I" >> $current_options_file
#echo "--PSP_byname7_image=output_calib_shift_1/mean_T1b" >> $current_options_file
#echo "--PSP_byname7=T_1_MT" >> $current_options_file # Estimate T1 of tissue
#echo "--PSP_byname7_type=I" >> $current_options_file
#echo "--PSP_byname7_image=output_calib_shift_1_MT/mean_T1t" >> $current_options_file
#echo "--PSP_byname8=T_1b_MT" >> $current_options_file # Estimate T1 of blood
#echo "--PSP_byname8_type=I" >> $current_options_file
#echo "--PSP_byname8_image=output_calib_shift_1_MT/mean_T1b" >> $current_options_file
fi
# Now perform model fitting to estimate CBF, ATT, etc
$fabber --data=$shift_1_repeat_1_tissue --data-order=singlefile --output=output_tissue_shift_1 -@ $current_options_file
# Shift 2
# Fixed bolus duration
# Condition not to infer bolus duration
current_options_file="options_tissue_shift_2.txt"
echo "# Turbo QUASAR ASL tissue component analysis options for Shift 2 of the data" > $current_options_file
echo "--mask=mask_shift_2_effective" >> $current_options_file
echo "--method=spatialvb" >> $current_options_file
echo "--noise=white" >> $current_options_file
echo "--model=turboquasar" >> $current_options_file
echo "--disp=$disp" >> $current_options_file
#echo "--inferart" >> $current_options_file
echo "--repeats=1" >> $current_options_file
echo $tislist >> $current_options_file
echo "--t1=$t1 --t1b=$t1b --t1wm=$t1wm --tau=$tau --fa=$fa " >> $current_options_file
#if [ ! -z $infert1 ]; then
# #echo "--t1_MT=$t1 --t1b_MT=$t1b " >> $current_options_file
#fi
echo "--slicedt=$slicedt" >> $current_options_file
echo $bolus_pattern_list >> $current_options_file
echo "--slice_shift=$shift_factor" >> $current_options_file
echo "--onephase" >> $current_options_file
if [ ! -z $infert1 ]; then
echo "--usecalib " >> $current_options_file # We incorporate previously estimated T1 and g value as priors
echo "--infert1" >> $current_options_file # If we infer T1 we must infer T1 of tissue and blood together (must be two of them)
fi
#echo "--artdir" >> $current_options_file
# Save model fitting results and residue
echo "--save-model-fit" >> $current_options_file
echo "--print-free-energy" >> $current_options_file
echo "--save-residuals" >> $current_options_file
# Here we create a shortcut to the latest results directory
#echo "--link-to-latest" >> $current_options_file
# Here are the settings
echo "--PSP_byname1=ftiss" >> $current_options_file # Estimate CBF
echo "--PSP_byname1_type=M" >> $current_options_file
echo "--PSP_byname2=delttiss" >> $current_options_file # Estimate ATT
echo "--PSP_byname2_type=N" >> $current_options_file
echo "--PSP_byname3=sp_log" >> $current_options_file # Estimate sp log (dispersion parameter, even if dispersion is none)
echo "--PSP_byname3_type=N" >> $current_options_file
echo "--PSP_byname4=s_log" >> $current_options_file # Estimate s log (dispersion parameter, even if dispersion is none)
echo "--PSP_byname4_type=N" >> $current_options_file
if [ ! -z $infert1 ]; then
echo "--PSP_byname5=g" >> $current_options_file # Estimate g (FA correction parameter)
echo "--PSP_byname5_type=I" >> $current_options_file
echo "--PSP_byname5_image=output_calib_shift_2/mean_g" >> $current_options_file
echo "--PSP_byname6=T_1" >> $current_options_file # Estimate T1 of tissue
echo "--PSP_byname6_type=I" >> $current_options_file
echo "--PSP_byname6_image=output_calib_shift_2/mean_T1t" >> $current_options_file
#echo "--PSP_byname7=T_1b" >> $current_options_file # Estimate T1 of blood
#echo "--PSP_byname7_type=I" >> $current_options_file
#echo "--PSP_byname7_image=output_calib_shift_2/mean_T1b" >> $current_options_file
#echo "--PSP_byname7=T_1_MT" >> $current_options_file # Estimate T1 of tissue
#echo "--PSP_byname7_type=I" >> $current_options_file
#echo "--PSP_byname7_image=output_calib_shift_2_MT/mean_T1t" >> $current_options_file
#echo "--PSP_byname8=T_1b_MT" >> $current_options_file # Estimate T1 of blood
#echo "--PSP_byname8_type=I" >> $current_options_file
#echo "--PSP_byname8_image=output_calib_shift_2_MT/mean_T1b" >> $current_options_file
fi
# Now perform model fitting to estimate CBF, ATT, etc
$fabber --data=$shift_2_repeat_1_tissue --data-order=singlefile --output=output_tissue_shift_2 -@ $current_options_file
# Condition to estimate bolus duration
if [ ! -z $infertau ]; then
if [ -z $tau_lowest ]; then
tau_lowest=0.2;
fi
# We need two steps to estimate it
# Step 1: Estimate CBF and ATT (as the previous step)
# Step 2: Incorporate results from Step 1 to estimate bolus duration and refine CBF and ATT
echo "Inferring bolus duration..."
# Shift 1
echo "Estimating bolus duration from Shift 1 of the data..."
# Rename the results of the previous step
mv output_tissue_shift_1 output_tissue_shift_1_step_1
# Now we need to create a new MVN file to include the new bolus duration parameter
mvntool --input=output_tissue_shift_1_step_1/finalMVN --output=output_tissue_shift_1_step_1/finalMVN2 --mask=mask --param=3 --new --val=1 --var=1