forked from HBClab/RestingState
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseedVoxelCorrelation_test.sh
executable file
·1039 lines (833 loc) · 39.6 KB
/
seedVoxelCorrelation_test.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
##################################################################################################################
# Time Series Correlation from Seed/ROI masks
# 1. Push Seed masks from MNI to EPI space
# 2. Calculate Time-series for each Seed of interest
# 3. Time-Series Correlation/Zmap Creation
# 4. Seed zmap QC (push to highres (T1) and standard (MNI)
##################################################################################################################
# nuisancefeat=nuisancereg.feat
melodicfeat=melodic.ica
##Check of all ROIs (from ROIs directory), that can be used for seeding
scriptPath=`perl -e 'use Cwd "abs_path";print abs_path(shift)' $0`
scriptDir=`dirname $scriptPath`
knownRois=`ls -1 $scriptDir/ROIs/*nii* | awk -F"/" '{print $NF}' | awk -F"." '{print $1}'`
function printCommandLine {
echo "Usage: seedVoxelCorrelation.sh -E restingStateImage -r roi -m motionScrubFlag -n -f"
echo " where"
echo " -E resting state image"
echo " *Top-level RestingState.nii.gz image"
echo " -m use MotionScrub EPI: O,1 or 2 (default is 0/no)"
echo " 0 = use non-motionscrubbed EPI only (default)"
echo " 1 = use motionscrubbed EPI only"
echo " 2 = use motionscrubbed and non-motionscrubbed EPI (parallel output)"
echo " -r roi for seed voxel (can be used multiple times)"
echo " *e.g. -r pccrsp -r icalc"
echo " -R Data file with seed list, one seed per line"
echo " **Use ONLY one option, -r or -R, NOT both"
echo " -n Nuisance regression method"
echo " 1 = compcor only"
echo " 2 = compcor + GM FAST regression"
echo " 3 = compcor + GM mask regression"
echo " blank = classic"
echo " -f (fieldMap registration correction)"
echo " *Only set this flag if FieldMap correction was used during qualityCheck"
echo " **This affects only the EPI to T1 QC images (invoked via '-V')"
echo " -V Review Results (default is to NOT view results). Setting of this flag will spit out time-series plots of seed/ROI"
echo ""
echo "Existing seeds:"
echo "$knownRois"
exit 1
}
# Parse Command line arguments
while getopts “hE:m:r:R:n:f” OPTION
do
case $OPTION in
h)
printCommandLine
;;
E)
epiData=$OPTARG
;;
m)
motionscrubFlag=$OPTARG
;;
r)
roiList=`echo $roiList $OPTARG`
roiInd=1
;;
R)
roiList=`cat $OPTARG`
roiInFile=$OPTARG
;;
n)
nuisanceFlag=$OPTARG
;;
f)
fieldMapFlag=1
;;
?)
echo "ERROR: Invalid option"
printCommandLine
;;
esac
done
case "$nuisanceFlag" in
1)
nuisancefeat=nuisancereg_compcor.feat
seedcorrDir=$(dirname ${epiData})/seedCorrelation_compcor
;;
2)
nuisancefeat=nuisancereg_compcor_wGMR.feat
seedcorrDir=$(dirname ${epiData})/seedCorrelation_compcor_wGMR
;;
3)
nuisancefeat=nuisancereg_compcor_wGMRv1.feat
seedcorrDir=$(dirname ${epiData})/seedCorrelation_compcor_wGMRv1
;;
"")
nuisancefeat=nuisancereg.feat
seedcorrDir=$(dirname ${epiData})/seedCorrelation
;;
esac
#A few default parameters (if input not specified, these parameters are assumed)
if [[ $reviewResults == "" ]]; then
reviewResults=0
fi
if [[ $motionscrubFlag == "" ]]; then
motionscrubFlag=0
fi
if [[ $fieldMapFlag == "" ]]; then
fieldMapFlag=0
fi
echo $reviewResults
for roi in $roiList
do
testRoi=`echo $knownRois | grep $roi`
if [ "$testRoi" == "" ]; then
echo "Error: Invalid ROI specified (${roi})"
echo "Valids ROIs: $knownRois"
exit 1
fi
done
if [[ "$roiList" == "" ]]; then
echo "Error: At least one ROI must be specified using the -r option."
exit 1
fi
if [[ "$epiData" == "" ]]; then
echo "Error: The restingStateImage (-E) is a required option."
exit 1
fi
#If new seeds are added, echo them out to the rsParams file (only if they don't already exist in the file)
#Making a *strong* assumption that any ROI lists added after initial processing won't reuse the first ROI (e.g. pccrsp)
indir=`dirname $epiData`
seedTestBase=`cat $indir/rsParams | grep "seeds=" | awk -F"=" '{print $2}' | awk -F"-r " '{for (i=2; i<=NF; i++) print $i}'`
seedTest=`echo $seedTestBase | awk '{print $1}'`
roiTest=`echo $roiList | awk '{print $1}'`
for i in $roiList
do
seeds="$seeds -r $i"
done
if [[ "$seedTest" != "$roiTest" ]]; then
echo "seeds=$seeds" >> $indir/rsParams
fi
subjectDir=`dirname $indir`
roiDir=${scriptDir}/ROIs
#epiwarpDir=$indir/EPItoT1optimized
#t1warpDir=$indir/T1forWarp
if [ $motionscrubFlag == 0 ]; then
filename=run_firstlevelseeding_parallel.m
elif [ $motionscrubFlag = 1 ]; then
filename2=run_firstlevelseeding_parallel_ms.m
else
filename=run_firstlevelseeding_parallel.m
filename2=run_firstlevelseeding_parallel_ms.m
fi
##Echo out all input parameters into a log
logDir=$indir
echo "$scriptPath" >> $logDir/rsParams_log
echo "------------------------------------" >> $logDir/rsParams_log
echo "-E $epiData" >> $logDir/rsParams_log
echo "-m $motionscrubFlag" >> $logDir/rsParams_log
if [[ $roiInd == 1 ]]; then
echo "$seeds" >> $logDir/rsParams_log
else
echo "-R $roiInFile" >> $logDir/rsParams_log
fi
if [[ $fieldMapFlag == 1 ]]; then
echo "-f" >> $logDir/rsParams_log
fi
if [[ $reviewResults == 1 ]]; then
echo "-V" >> $logDir/rsParams_log
fi
echo "`date`" >> $logDir/rsParams_log
echo "" >> $logDir/rsParams_log
echo "" >> $logDir/rsParams_log
echo "Running $0 ..."
#### Mapping ROIs To Functional Space ############
echo "...Transforming ROIs to EPI space"
cd $indir
echo $(PWD)
if [ -e $indir/seeds.txt ]; then
rm $indir/seeds.txt
fi
# Map the ROIs
for roi in $roiList; do
if [ ! -f ${nuisancefeat}/stats/${roi}_mask.nii.gz ]; then #TW edit
echo "......Mapping $roi from MNI (standard) to subject EPI (func) space"
#Source MNI to EPI warp file
MNItoEPIWarp=`cat $indir/rsParams | grep "MNItoEPIWarp=" | tail -1 | awk -F"=" '{print $2}'`
#Apply the nonlinear warp from MNI to EPI
applywarp --ref=${epiData} --in=${roiDir}/${roi}.nii.gz --out=${nuisancefeat}/stats/${roi}_mask.nii.gz --warp=${MNItoEPIWarp} --datatype=float
#Threshold and binarize output
fslmaths ${nuisancefeat}/stats/${roi}_mask.nii.gz -thr 0.5 ${nuisancefeat}/stats/${roi}_mask.nii.gz
fslmaths ${nuisancefeat}/stats/${roi}_mask.nii.gz -bin ${nuisancefeat}/stats/${roi}_mask.nii.gz
#Check to see that resultant, warped file has any volume (if seed is too small, warped output may have a zero volume)
seedVol=`fslstats ${nuisancefeat}/stats/${roi}_mask.nii.gz -V | awk '{print $2}'`
if [[ $seedVol == 0.000000 ]]; then
echo $roi >> ${nuisancefeat}/stats/seedsTooSmall.txt
rm ${nuisancefeat}/stats/${roi}_mask.nii.gz
else
# Account for $motionscrubFlag
# Extract the time-series per ROI
# Will need the "normal" time-series, regardless of motion-scrubbing flag so, if condition = 1 or 2, write out regular time-series
if [[ $motionscrubFlag == 0 ]]; then
fslmeants -i ${nuisancefeat}/stats/res4d_normandscaled -o ${nuisancefeat}/stats/${roi}_residvol_ts.txt -m ${nuisancefeat}/stats/${roi}_mask.nii.gz
elif [[ $motionscrubFlag == 1 ]]; then
fslmeants -i ${nuisancefeat}/stats/res4d_normandscaled_motionscrubbed -o ${nuisancefeat}/stats/${roi}_residvol_ms_ts.txt -m ${nuisancefeat}/stats/${roi}_mask.nii.gz
else
fslmeants -i ${nuisancefeat}/stats/res4d_normandscaled -o ${nuisancefeat}/stats/${roi}_residvol_ts.txt -m ${nuisancefeat}/stats/${roi}_mask.nii.gz
fslmeants -i ${nuisancefeat}/stats/res4d_normandscaled_motionscrubbed -o ${nuisancefeat}/stats/${roi}_residvol_ms_ts.txt -m ${nuisancefeat}/stats/${roi}_mask.nii.gz
fi
#Output of fslmeants is a text file with space-delimited values. There is only one "true" ts value (first column) and the blank space is interpreted as a "0" value in matlab. Write to temp file then move (rewrite original)
if [[ $motionscrubFlag == 0 ]]; then
cat ${nuisancefeat}/stats/${roi}_residvol_ts.txt | awk '{print $1}' > ${nuisancefeat}/stats/temp_${roi}_residvol_ts.txt
mv ${nuisancefeat}/stats/temp_${roi}_residvol_ts.txt ${nuisancefeat}/stats/${roi}_residvol_ts.txt
elif [[ $motionscrubFlag == 1 ]]; then
cat ${nuisancefeat}/stats/${roi}_residvol_ms_ts.txt | awk '{print $1}' > ${nuisancefeat}/stats/temp_${roi}_residvol_ms_ts.txt
mv ${nuisancefeat}/stats/temp_${roi}_residvol_ms_ts.txt ${nuisancefeat}/stats/${roi}_residvol_ms_ts.txt
else
cat ${nuisancefeat}/stats/${roi}_residvol_ts.txt | awk '{print $1}' > ${nuisancefeat}/stats/temp_${roi}_residvol_ts.txt
cat ${nuisancefeat}/stats/${roi}_residvol_ms_ts.txt | awk '{print $1}' > ${nuisancefeat}/stats/temp_${roi}_residvol_ms_ts.txt
mv ${nuisancefeat}/stats/temp_${roi}_residvol_ts.txt ${nuisancefeat}/stats/${roi}_residvol_ts.txt
mv ${nuisancefeat}/stats/temp_${roi}_residvol_ms_ts.txt ${nuisancefeat}/stats/${roi}_residvol_ms_ts.txt
fi
echo "$roi" >> $indir/seeds.txt
fi
else #TW edit
echo "$roi has already been mapped from MNI to EPI" #TW edit
echo "$roi" >> $indir/seeds.txt
fi #TW edit
done
roiList2=`cat $indir/seeds.txt`
#################################
#### Seed Transform QC Images ############
echo "...QC Image Setup"
###Create QC images of seed/ROI overlaid on RestingState EPI. Place in top level directory and report in HTML file
##Create underlay/overlay NIFTI files for QC check
#Create a temp directory
seedQCdir=$indir/$nuisancefeat/stats/seedQC
if [ ! -e $seedQCdir/temp ]; then
mkdir -p $seedQCdir/temp
fi
#Create underlay/overlay images for each seed
for roi in $roiList2
do
for suff in sagittal coronal axial; do ###TW edit
if [ ! -f $seedQCdir/${roi}_overlay_${suff}.nii ]; then
for splitdirection in x y z
do
echo "......Preparing $roi ($splitdirection)"
underlayBase=$indir/mcImgMean.nii.gz
overlayBase=$indir/$nuisancefeat/stats/${roi}_mask.nii.gz
#Compute Center-Of-Gravity for seed mask to determine which axial slice to use for both underlay and overlay
#Adding 0.5 to COG for xyz dimensions to handle rounding issues
#Need to account for slices named 0007, 0017, 0107, etc. (have to be able to handle 4-digit numbers)
if [[ $splitdirection == "x" ]]; then
suffix=sagittal
sliceCutTEMP=`fslstats $overlayBase -C | awk '{printf("%d\n",$1 + 0.5)}'`
sliceCutLength=`echo $sliceCutTEMP | awk '{print length($1)}'`
if [[ $sliceCutLength == 1 ]]; then
sliceCut=000${sliceCutTEMP}
elif [[ $sliceCutLength == 2 ]]; then
sliceCut=00${sliceCutTEMP}
else
sliceCut=0${sliceCutTEMP}
fi
elif [[ $splitdirection == "y" ]]; then
suffix=coronal
sliceCutTEMP=`fslstats $overlayBase -C | awk '{printf("%d\n",$2 + 0.5)}'`
sliceCutLength=`echo $sliceCutTEMP | awk '{print length($1)}'`
if [[ $sliceCutLength == 1 ]]; then
sliceCut=000${sliceCutTEMP}
elif [[ $sliceCutLength == 2 ]]; then
sliceCut=00${sliceCutTEMP}
else
sliceCut=0${sliceCutTEMP}
fi
else
suffix=axial
sliceCutTEMP=`fslstats $overlayBase -C | awk '{printf("%d\n",$3 + 0.5)}'`
sliceCutLength=`echo $sliceCutTEMP | awk '{print length($1)}'`
if [[ $sliceCutLength == 1 ]]; then
sliceCut=000${sliceCutTEMP}
elif [[ $sliceCutLength == 2 ]]; then
sliceCut=00${sliceCutTEMP}
else
sliceCut=0${sliceCutTEMP}
fi
fi
#Split apart seed mask and example EPI image
fslsplit $underlayBase $seedQCdir/temp/underlay_split_${suffix} -${splitdirection}
fslsplit $overlayBase $seedQCdir/temp/overlay_split_${suffix} -${splitdirection}
#Set variables for underlay and overlay images
underlayImage=`ls -1 $seedQCdir/temp | grep "underlay_split_${suffix}" | grep $sliceCut`
overlayImage=`ls -1 $seedQCdir/temp | grep "overlay_split_${suffix}" | grep $sliceCut`
#Copy over underlay/overlay images, uncompress
##Will need to check for presence of unzipped NIFTI file (from previous runs (otherwise "clobber" won't work))
if [[ -e $seedQCdir/${roi}_underlay_${suffix}.nii ]]; then
if [[ ! -e $seedQCdir/oldSeeds ]]; then
mkdir $seedQCdir/oldSeeds
fi
mv $seedQCdir/${roi}_underlay_${suffix}.nii $seedQCdir/oldSeeds
fi
cp $seedQCdir/temp/$underlayImage $seedQCdir/${roi}_underlay_${suffix}.nii.gz
#gunzip $seedQCdir/${roi}_underlay_${suffix}.nii.gz
if [[ -e $seedQCdir/${roi}_overlay_${suffix}.nii ]]; then
if [[ ! -e $seedQCdir/oldSeeds ]]; then
mkdir $seedQCdir/oldSeeds
fi
mv $seedQCdir/${roi}_overlay_${suffix}.nii $seedQCdir/oldSeeds
fi
cp $seedQCdir/temp/$overlayImage $seedQCdir/${roi}_overlay_${suffix}.nii.gz
#gunzip $seedQCdir/${roi}_overlay_${suffix}.nii.gz
##Need to reorient coronal and sagittal images in order for matlab to process correctly (axial is already OK)
#Coronal images will also need the orientation swapped to update header AND image info
if [ $suffix == "sagittal" ]; then
fslswapdim $seedQCdir/${roi}_underlay_${suffix}.nii.gz y z x $seedQCdir/${roi}_underlay_${suffix}.nii.gz
fslswapdim $seedQCdir/${roi}_overlay_${suffix}.nii.gz y z x $seedQCdir/${roi}_overlay_${suffix}.nii.gz
elif [ $suffix == "coronal" ]; then
fslswapdim $seedQCdir/${roi}_underlay_${suffix}.nii.gz x z y $seedQCdir/${roi}_underlay_${suffix}.nii.gz
fslorient -swaporient $seedQCdir/${roi}_underlay_${suffix}.nii.gz
fslswapdim $seedQCdir/${roi}_overlay_${suffix}.nii.gz x z y $seedQCdir/${roi}_overlay_${suffix}.nii.gz
fslorient -swaporient $seedQCdir/${roi}_overlay_${suffix}.nii.gz
fi
#Need to gunzip the files for use with matlab
gunzip -f $seedQCdir/${roi}_underlay_${suffix}.nii.gz
gunzip -f $seedQCdir/${roi}_overlay_${suffix}.nii.gz
done
fi
done
done
#Create an output directory for QC seed images
seedQCOutdir=$indir/seedQC
if [ ! -e $seedQCOutdir ]; then
mkdir $seedQCOutdir
fi
#Create overlaps of seed_mask registered to EPI space using Octave
echo "...Creating QC Images of ROI/Seed Registration To Functional Space"
filenameQC=run_seedregistrationcheck.m;
cat > $filenameQC << EOF
% It is matlab script
close all;
clear all;
addpath('${scriptDir}');
niftiScripts=['${scriptDir}','/Octave/nifti'];
addpath(niftiScripts);statsScripts=['${scriptDir}','/Octave/statistics'];
statsScripts=['${scriptDir}','/Octave/statistics'];
addpath(statsScripts);
fid=fopen('$indir/seeds.txt');
roiList=textscan(fid,'%s');
fclose(fid);
seedDir='$seedQCdir';
imageDir='$seedQCOutdir';
seedregistrationcheck(seedDir,roiList,imageDir)
quit;
EOF
# Run script using Matlab or Octave
haveMatlab=`which matlab`
if [ "$haveMatlab" == "" ]; then
octave --no-window-system $indir/$filenameQC
else
matlab -nodisplay -r "run $indir/$filenameQC"
fi
#Remove temp directory of "split" files. Keep only underaly and overlay base images
rm -rf $seedQCdir/temp
#################################
#### Output Images To HTML File ############
#Display Coronal,Sagittal,Axial on one line
#Put header of seed type
echo "<hr>" >> ${indir}/analysisResults.html
#echo "<h2>Seed Registration QC</h2>" >> ${indir}/analysisResults.html
echo "<h2>Seed Registration QC (Neurological View, Right=Right)</h2>" >> ${indir}/analysisResults.html
for roi in $roiList2
do
echo "<br><b>$roi</b><br>" >> ${indir}/analysisResults.html
echo "<img src=\"$seedQCOutdir/${roi}_coronal.png\" alt=\"${roi}_coronal seed QC\"><img src=\"$seedQCOutdir/${roi}_sagittal.png\" alt=\"${roi}_sagittal seed QC\"><img src=\"$seedQCOutdir/${roi}_axial.png\" alt=\"${roi}_axial seed QC\"><br>" >> $indir/analysisResults.html
done
#################################
#### Seed Voxel Correlation (Setup) ############
echo "...Seed Voxel Correlation Setup"
#Dimensions of EPI data
numXdim=`fslinfo $epiData | grep ^dim1 | awk '{print $2}'`
numYdim=`fslinfo $epiData | grep ^dim2 | awk '{print $2}'`
numZdim=`fslinfo $epiData | grep ^dim3 | awk '{print $2}'`
###TW edit
mv $indir/seeds.txt $indir/seeds_orig.txt
for roi in `cat $indir/seeds_orig.txt`; do
if [ ! -f $indir/$nuisancefeat/stats/${roi}_ms/cope1.nii ]; then
echo $roi >> $indir/seeds_ms.txt
fi
if [ ! -f $indir/$nuisancefeat/stats/${roi}/cope1.nii ]; then
echo $roi >> $indir/seeds.txt
fi
done
#Perform the Correlation
#Take into account $motionscrubFlag
##Check into matlab about fixing motion-scrubbing (Power method)
if [[ $motionscrubFlag == 0 ]]; then
# If $motionscrubFlag == 0 (no motionscrub), res4dnormandscaled never gets unzipped
if [[ -e $indir/$nuisancefeat/stats/res4d_normandscaled.nii.gz ]]; then
gunzip -f $indir/$nuisancefeat/stats/res4d_normandscaled.nii.gz
fi
echo "...Creating Octave script"
cat > $filename << EOF
% It is matlab script
addpath('${scriptDir}')
statsScripts=['${scriptDir}','/Octave/nifti'];
addpath(statsScripts)
fid=fopen('$indir/seeds.txt');
roiList=textscan(fid,'%s');
fclose(fid);
funcvoldim=[$numXdim $numYdim ${numZdim}];
doFisherZ=1;
motion_scrub=0;
input='res4d_normandscaled.nii';
firstlevelseeding_parallel('$indir',roiList,'$nuisancefeat',funcvoldim,input,motion_scrub,doFisherZ)
quit
EOF
elif [[ $motionscrubFlag == 1 ]]; then
echo "...Creating Octave script (motionscrubbed data)"
cat > $filename2 << EOF
% It is matlab script
addpath('${scriptDir}')
statsScripts=['${scriptDir}','/Octave/nifti'];
addpath(statsScripts)
fid=fopen('$indir/seeds_ms.txt');
roiList=textscan(fid,'%s');
fclose(fid);
funcvoldim=[$numXdim $numYdim ${numZdim}];
doFisherZ=1;
motion_scrub=1;
input='res4d_normandscaled_motionscrubbed.nii';
firstlevelseeding_parallel('$indir',roiList,'$nuisancefeat',funcvoldim,input,motion_scrub,doFisherZ)
quit
EOF
else
echo "...Creating Octave script"
cat > $filename << EOF
% It is matlab script
addpath('${scriptDir}')
statsScripts=['${scriptDir}','/Octave/nifti'];
addpath(statsScripts)
fid=fopen('$indir/seeds.txt');
roiList=textscan(fid,'%s');
fclose(fid);
funcvoldim=[$numXdim $numYdim ${numZdim}];
doFisherZ=1;
motion_scrub=0;
input='res4d_normandscaled.nii';
firstlevelseeding_parallel('$indir',roiList,'$nuisancefeat',funcvoldim,input,motion_scrub,doFisherZ)
quit
EOF
echo "...Creating Octave script (motionscrubbed data)"
cat > $filename2 << EOF
% It is matlab script
addpath('${scriptDir}')
statsScripts=['${scriptDir}','/Octave/nifti'];
addpath(statsScripts)
fid=fopen('$indir/seeds_ms.txt');
roiList=textscan(fid,'%s');
fclose(fid);
funcvoldim=[$numXdim $numYdim ${numZdim}];
doFisherZ=1;
motion_scrub=1;
input='res4d_normandscaled_motionscrubbed.nii';
firstlevelseeding_parallel('$indir',roiList,'$nuisancefeat',funcvoldim,input,motion_scrub,doFisherZ)
quit
EOF
fi
#################################
if [ ! -f $indir/seeds.txt ] || [ ! -f $indir/seeds_ms.txt ]; then
echo "no seeds to correlate."
else
#### Seed Voxel Correlation (Execution) ############
echo "...Correlating Seeds With Time Series Data"
# Run script using Matlab or Octave
# Check for $motionscrubFlag, run appropriate file(s)
haveMatlab=`which matlab`
if [[ "$haveMatlab" == "" ]]; then
if [[ $motionscrubFlag == 0 ]]; then
octave --no-window-system $indir/$filename
elif [[ $motionscrubFlag == 1 ]]; then
octave --no-window-system $indir/$filename2
else
octave --no-window-system $indir/$filename
octave --no-window-system $indir/$filename2
fi
else
if [[ $motionscrubFlag == 0 ]]; then
matlab -nodisplay -r "run $indir/$filename"
elif [[ $motionscrubFlag == 1 ]]; then
matlab -nodisplay -r "run $indir/$filename2"
else
matlab -nodisplay -r "run $indir/$filename"
matlab -nodisplay -r "run $indir/$filename2"
fi
fi
fi
#################################
##TW edit
mv $indir/seeds_orig.txt $indir/seeds.txt
if [ -f $indir/seeds_ms.txt ]; then
rm $indir/seeds_ms.txt
fi
#### Zstat Results (to T1/MNI) ############
echo "...Creating zstat Results Directory"
#Check for existence of output directory
if [[ ! -e ${seedcorrDir} ]]; then
mkdir ${seedcorrDir}
fi
#Copy over anatomical files to results directory
#T1 (highres)
cp $indir/${nuisancefeat}/reg/highres.nii.gz ${seedcorrDir}
#T1toMNI (highres2standard)
cp $indir/${nuisancefeat}/reg/highres2standard.nii.gz ${seedcorrDir}
#MNI (standard)
cp $indir/${nuisancefeat}/reg/standard.nii.gz ${seedcorrDir}
#HTML setup
echo "<hr><h2>Seed Time Series</h2>" >> $indir/analysisResults.html
for roi in $roiList2
do
echo "...Mapping Correlation For $roi To Subject T1, MNI"
####Adjust for motion scrubbing
if [[ $motionscrubFlag == 0 ]]; then
#No motionscrubbing
if [ -e ${roi}.png ]; then
rm ${roi}.png
fi
#Check for FieldMap registration correction
if [[ $fieldMapFlag == 1 ]]; then
#Nonlinear warp from EPI to T1
applywarp --in=$indir/${nuisancefeat}/stats/${roi}/cope1.nii \
--ref=$indir/${nuisancefeat}/reg/highres.nii.gz \
--out=${seedcorrDir}/${roi}_highres_zmap.nii.gz \
--warp=$indir/${nuisancefeat}/reg/example_func2highres_warp.nii.gz \
--datatype=float
else
#Affine Transform from EPI to T1
flirt -in $indir/${nuisancefeat}/stats/${roi}/cope1.nii \
-ref $indir/${nuisancefeat}/reg/highres.nii.gz \
-out ${seedcorrDir}/${roi}_highres_zmap.nii.gz \
-applyxfm -init $indir/${nuisancefeat}/reg/example_func2highres.mat \
-datatype float
fi
#Mask out data with T1 mask (create temporary binary of skull-stripped T1)
fslmaths $indir/${nuisancefeat}/reg/highres.nii.gz -bin $indir/${nuisancefeat}/reg/highres_mask.nii.gz -odt char
fslmaths ${seedcorrDir}/${roi}_highres_zmap.nii.gz -mas $indir/${nuisancefeat}/reg/highres_mask.nii.gz ${seedcorrDir}/${roi}_highres_zmap_masked.nii.gz
rm $indir/${nuisancefeat}/reg/highres_mask.nii.gz
#Nonlinear warp from EPI to MNI
applywarp --in=$indir/${nuisancefeat}/stats/${roi}/cope1.nii \
--ref=$indir/${nuisancefeat}/reg/standard.nii.gz \
--out=${seedcorrDir}/${roi}_standard_zmap.nii.gz \
--warp=$indir/${nuisancefeat}/reg/example_func2standard_warp.nii.gz \
--datatype=float
#Mask out data with MNI mask
fslmaths ${seedcorrDir}/${roi}_standard_zmap.nii.gz -mas $FSLDIR/data/standard/MNI152_T1_2mm_brain_mask.nii.gz ${seedcorrDir}/${roi}_standard_zmap_masked.nii.gz
#Warp seed from MNI to T1
applywarp --in=${roiDir}/${roi}.nii.gz \
--ref=$indir/${nuisancefeat}/reg/highres.nii.gz \
--out=${seedcorrDir}/${roi}_highres.nii.gz \
--warp=$indir/${nuisancefeat}/reg/standard2highres_warp.nii.gz \
--interp=nn
#Copy over Seed ROI
cp ${roiDir}/${roi}.nii.gz ${seedcorrDir}/${roi}_standard.nii.gz
# Summarize the time series data for report
#echo "set term png" > $indir/gnuplotCmds
#echo "set output \"$indir/${roi}.png\"" >> $indir/gnuplotCmds
#echo "set title \"$roi Time Series\"" >> $indir/gnuplotCmds
#echo "set xlabel \"Time Points\"" >> $indir/gnuplotCmds
#echo "plot \"$indir/${nuisancefeat}/stats/${roi}_residvol_ts.txt\" with lines" >> $indir/gnuplotCmds
#cat $indir/gnuplotCmds | gnuplot
#Cleanup
#rm $indir/gnuplotCmds
##Creating new plots with fsl_tsplot
#~2.2% plotting difference between actual Ymin and Ymax values (higher and lower), with fsl_tsplot
yMax=`cat $indir/${nuisancefeat}/stats/${roi}_residvol_ts.txt | sort -r | tail -1 | awk '{print ($1+($1*0.0022))}'`
yMin=`cat $indir/${nuisancefeat}/stats/${roi}_residvol_ts.txt | tail -1 | awk '{print ($1-($1*0.0022))}'`
fsl_tsplot -i $indir/${nuisancefeat}/stats/${roi}_residvol_ts.txt -t "$roi Time Series" -u 1 --start=1 -x 'Time Points (TR)' --ymin=$yMin --ymax=$yMax -w 800 -h 300 -o $indir/${roi}.png
echo "<br><img src=\"$indir/${roi}.png\" alt=\"$roi seed\"><br>" >> $indir/analysisResults.html
elif [[ $motionscrubFlag == 1 ]]; then
#Only motionscrubbed data
if [ -e ${roi}_ms.png ]; then
rm ${roi}_ms.png
fi
#Check for FieldMap registration correction
if [[ $fieldMapFlag == 1 ]]; then
#Nonlinear warp from EPI to T1
applywarp --in=$indir/${nuisancefeat}/stats/${roi}_ms/cope1.nii \
--ref=$indir/${nuisancefeat}/reg/highres.nii.gz \
--out=${seedcorrDir}/${roi}_ms_highres_zmap.nii.gz \
--warp=$indir/${nuisancefeat}/reg/example_func2highres_warp.nii.gz \
--datatype=float
else
#Affine Transform from EPI to T1
flirt -in $indir/${nuisancefeat}/stats/${roi}_ms/cope1.nii \
-ref $indir/${nuisancefeat}/reg/highres.nii.gz \
-out ${seedcorrDir}/${roi}_ms_highres_zmap.nii.gz \
-applyxfm -init $indir/${nuisancefeat}/reg/example_func2highres.mat \
-datatype float
fi
#Mask out data with T1 mask (create temporary binary of skull-stripped T1)
fslmaths $indir/${nuisancefeat}/reg/highres.nii.gz -bin $indir/${nuisancefeat}/reg/highres_mask.nii.gz -odt char
fslmaths ${seedcorrDir}/${roi}_ms_highres_zmap.nii.gz -mas $indir/${nuisancefeat}/reg/highres_mask.nii.gz ${seedcorrDir}/${roi}_ms_highres_zmap_masked.nii.gz
rm $indir/${nuisancefeat}/reg/highres_mask.nii.gz
#Nonlinear warp from EPI to MNI
applywarp --in=$indir/${nuisancefeat}/stats/${roi}_ms/cope1.nii \
--ref=$indir/${nuisancefeat}/reg/standard.nii.gz \
--out=${seedcorrDir}/${roi}_ms_standard_zmap.nii.gz \
--warp=$indir/${nuisancefeat}/reg/example_func2standard_warp.nii.gz \
--datatype=float
#Mask out data with MNI mask
fslmaths ${seedcorrDir}/${roi}_ms_standard_zmap.nii.gz -mas $FSLDIR/data/standard/MNI152_T1_2mm_brain_mask.nii.gz ${seedcorrDir}/${roi}_ms_standard_zmap_masked.nii.gz
#Warp seed from MNI to T1
applywarp --in=${roiDir}/${roi}.nii.gz \
--ref=$indir/${nuisancefeat}/reg/highres.nii.gz \
--out=${seedcorrDir}/${roi}_highres.nii.gz \
--warp=$indir/${nuisancefeat}/reg/standard2highres_warp.nii.gz \
--interp=nn
#Copy over Seed ROI
cp ${roiDir}/${roi}.nii.gz ${seedcorrDir}/${roi}_standard.nii.gz
#Look for the presence of deleted volumes. ONLY create "spike" (ms) images if found, otherwise default to non-motionscrubbed images
scrubDataCheck=`cat $indir/$nuisancefeat/stats/deleted_vols.txt | head -1`
if [[ $scrubDataCheck != "" ]]; then
#Presence of scrubbed volumes
#Split up deleted vols/spikes (from motionscrubbing), to echo out command for gnuplot
#spikeCount=`cat $indir/$nuisancefeat/stats/deleted_vols.txt | awk -F" " '{print NF}'`
#xmax=`cat $indir/$nuisancefeat/stats/${roi}_residvol_ms_ts.txt | wc -l`
#ymin=`cat $indir/$nuisancefeat/stats/${roi}_residvol_ms_ts.txt | sort -g | head -1 | awk '{print $1}'`
#ymax=`cat $indir/$nuisancefeat/stats/${roi}_residvol_ms_ts.txt | sort -g | tail -1 | awk '{print $1}'`
#Summarize the time series data for report
#echo "set term png" > $indir/gnuplotCmds
#echo "set output \"$indir/${roi}_ms.png\"" >> $indir/gnuplotCmds
#echo "set title \"${roi}_ms Time Series (With Deleted Volumes)\"" >> $indir/gnuplotCmds
#echo "set xlabel \"Time Points\"" >> $indir/gnuplotCmds
#echo "set xrange [0:$xmax]" >> $indir/gnuplotCmds
#echo "set yrange [$ymin:$ymax]" >> $indir/gnuplotCmds
#i=1
#while [ $i -le $spikeCount ]
#do
#spikeNum=`cat $indir/$nuisancefeat/stats/deleted_vols.txt | awk -F" " -v cutfield=$i '{print $cutfield}'`
#echo "set arrow $i from $spikeNum,$ymin to $spikeNum,$ymax nohead lc rgb '"green"'" >> $indir/gnuplotCmds
#let i+=1
#done
#echo "plot \"$indir/${nuisancefeat}/stats/${roi}_residvol_ts.txt\" with lines lc rgb '"blue"'" >> $indir/gnuplotCmds
#cat $indir/gnuplotCmds | gnuplot
#Cleanup
#rm $indir/gnuplotCmds
##Creating new plots with fsl_tsplot
#~2.2% plotting difference between actual Ymin and Ymax values (higher and lower), with fsl_tsplot
yMax=`cat $indir/${nuisancefeat}/stats/${roi}_residvol_ms_ts.txt | sort -r | tail -1 | awk '{print ($1+($1*0.0022))}'`
yMin=`cat $indir/${nuisancefeat}/stats/${roi}_residvol_ms_ts.txt | tail -1 | awk '{print ($1-($1*0.0022))}'`
#Log the "scrubbed TRs"
xNum=`cat $indir/${nuisancefeat}/stats/${roi}_residvol_ms_ts.txt | wc -l`
count=1
while [ $count -le $xNum ]; do
tsPlotIn=`cat $indir/${nuisancefeat}/stats/${roi}_residvol_ms_ts.txt | head -${count} | tail -1`
delPlotCheck=`cat $indir/${nuisancefeat}/stats/deleted_vols.txt | awk '{$1=$1}1' | grep -E '(^| )'${count}'( |$)'`
if [ "$delPlotCheck" == "" ]; then
delPlot=$yMin
else
delPlot=$yMax
fi
echo $delPlot >> $indir/${nuisancefeat}/stats/${roi}_censored_TRplot.txt
let count=count+1
done
#Plot of normal data showing scrubbed TRs
###TW edit fsl_tsplot -i $indir/${nuisancefeat}/stats/${roi}_residvol_ts.txt,$indir/${nuisancefeat}/stats/${roi}_censored_TRplot.txt -t "$roi Time Series" -u 1 --start=1 -x 'Time Points (TR)' -a ",Scrubbed_TR" --ymin=$yMin --ymax=$yMax -w 800 -h 300 -o $indir/${roi}.png
#Plot of "scrubbed" data
fsl_tsplot -i $indir/${nuisancefeat}/stats/${roi}_residvol_ms_ts.txt -t "$roi Time Series (Scrubbed)" -u 1 --start=1 -x 'Time Points (TR)' --ymin=$yMin --ymax=$yMax -w 800 -h 300 -o $indir/${roi}_ms.png
echo "<br><img src=\"$indir/${roi}.png\" alt=\"${roi} seed\"><img src=\"$indir/${roi}_ms.png\" alt=\"${roi}_ms seed\"><br>" >> $indir/analysisResults.html
else
#Absence of scrubbed volumes
# Summarize the time series data for report
#echo "set term png" > $indir/gnuplotCmds
#echo "set output \"$indir/${roi}_ms.png\"" >> $indir/gnuplotCmds
#echo "set title \"${roi}_ms Time Series\"" >> $indir/gnuplotCmds
#echo "set xlabel \"Time Points\"" >> $indir/gnuplotCmds
#echo "plot \"$indir/${nuisancefeat}/stats/${roi}_residvol_ts.txt\" with lines" >> $indir/gnuplotCmds
#cat $indir/gnuplotCmds | gnuplot
#Cleanup
#rm $indir/gnuplotCmds
##Creating new plots with fsl_tsplot
#~2.2% plotting difference between actual Ymin and Ymax values (higher and lower), with fsl_tsplot
yMax=`cat $indir/${nuisancefeat}/stats/${roi}_residvol_ms_ts.txt | sort -r | tail -1 | awk '{print ($1+($1*0.0022))}'`
yMin=`cat $indir/${nuisancefeat}/stats/${roi}_residvol_ms_ts.txt | tail -1 | awk '{print ($1-($1*0.0022))}'`
fsl_tsplot -i $indir/${nuisancefeat}/stats/${roi}_residvol_ms_ts.txt -t "$roi Time Series" -u 1 --start=1 -x 'Time Points (TR)' --ymin=$yMin --ymax=$yMax -w 800 -h 300 -o $indir/${roi}.png
echo "<br><img src=\"$indir/${roi}.png\" alt=\"$roi seed\"><br>" >> $indir/analysisResults.html
fi
else
#motionScrubFlag=2
##Non-motionscrubbed data
if [ -e ${roi}.png ]; then
rm ${roi}.png
fi
if [ -e ${roi}_ms.png ]; then
rm ${roi}_ms.png
fi
#Check for FieldMap registration correction
if [[ $fieldMapFlag == 1 ]]; then
#Nonlinear warp from EPI to T1
applywarp --in=$indir/${nuisancefeat}/stats/${roi}/cope1.nii \
--ref=$indir/${nuisancefeat}/reg/highres.nii.gz \
--out=${seedcorrDir}/${roi}_highres_zmap.nii.gz \
--warp=$indir/${nuisancefeat}/reg/example_func2highres_warp.nii.gz \
--datatype=float
else
#Affine Transform from EPI to T1
flirt -in $indir/${nuisancefeat}/stats/${roi}/cope1.nii \
-ref $indir/${nuisancefeat}/reg/highres.nii.gz \
-out ${seedcorrDir}/${roi}_highres_zmap.nii.gz \
-applyxfm -init $indir/${nuisancefeat}/reg/example_func2highres.mat \
-datatype float
fi
#Mask out data with T1 mask (create temporary binary of skull-stripped T1)
fslmaths $indir/${nuisancefeat}/reg/highres.nii.gz -bin $indir/${nuisancefeat}/reg/highres_mask.nii.gz -odt char
fslmaths ${seedcorrDir}/${roi}_highres_zmap.nii.gz -mas $indir/${nuisancefeat}/reg/highres_mask.nii.gz ${seedcorrDir}/${roi}_highres_zmap_masked.nii.gz
#Nonlinear warp from EPI to MNI
applywarp --in=$indir/${nuisancefeat}/stats/${roi}/cope1.nii \
--ref=$indir/${nuisancefeat}/reg/standard.nii.gz \
--out=${seedcorrDir}/${roi}_standard_zmap.nii.gz \
--warp=$indir/${nuisancefeat}/reg/example_func2standard_warp.nii.gz \
--datatype=float
#Mask out data with MNI mask
fslmaths ${seedcorrDir}/${roi}_standard_zmap.nii.gz -mas $FSLDIR/data/standard/MNI152_T1_2mm_brain_mask.nii.gz ${seedcorrDir}/${roi}_standard_zmap_masked.nii.gz
#Warp seed from MNI to T1
applywarp --in=${roiDir}/${roi}.nii.gz \
--ref=$indir/${nuisancefeat}/reg/highres.nii.gz \
--out=${seedcorrDir}/${roi}_highres.nii.gz \
--warp=$indir/${nuisancefeat}/reg/standard2highres_warp.nii.gz \
--interp=nn
#Copy over Seed ROI
cp ${roiDir}/${roi}.nii.gz ${seedcorrDir}/${roi}_standard.nii.gz
##Motionscrubbed data
#Check for FieldMap registration correction
if [[ $fieldMapFlag == 1 ]]; then
#Nonlinear warp from EPI to T1
applywarp --in=$indir/${nuisancefeat}/stats/${roi}_ms/cope1.nii \
--ref=$indir/${nuisancefeat}/reg/highres.nii.gz \
--out=${seedcorrDir}/${roi}_ms_highres_zmap.nii.gz \
--warp=$indir/${nuisancefeat}/reg/example_func2highres_warp.nii.gz \
--datatype=float
else
#Affine Transform from EPI to T1
flirt -in $indir/${nuisancefeat}/stats/${roi}_ms/cope1.nii \
-ref $indir/${nuisancefeat}/reg/highres.nii.gz \
-out ${seedcorrDir}/${roi}_ms_highres_zmap.nii.gz \
-applyxfm -init $indir/${nuisancefeat}/reg/example_func2highres.mat \
-datatype float
fi
#Mask out data with T1 mask (remove temporary binary of skull-stripped T1)
fslmaths ${seedcorrDir}/${roi}_ms_highres_zmap.nii.gz -mas $indir/${nuisancefeat}/reg/highres_mask.nii.gz ${seedcorrDir}/${roi}_ms_highres_zmap_masked.nii.gz
rm $indir/${nuisancefeat}/reg/highres_mask.nii.gz
#Nonlinear warp from EPI to MNI
applywarp --in=$indir/${nuisancefeat}/stats/${roi}_ms/cope1.nii \
--ref=$indir/${nuisancefeat}/reg/standard.nii.gz \
--out=${seedcorrDir}/${roi}_ms_standard_zmap.nii.gz \
--warp=$indir/${nuisancefeat}/reg/example_func2standard_warp.nii.gz \
--datatype=float
#Mask out data with MNI mask
fslmaths ${seedcorrDir}/${roi}_ms_standard_zmap.nii.gz -mas $FSLDIR/data/standard/MNI152_T1_2mm_brain_mask.nii.gz ${seedcorrDir}/${roi}_ms_standard_zmap_masked.nii.gz
#Look for the presence of deleted volumes. ONLY create "spike" (ms) images if found, otherwise default to non-motionscrubbed images
scrubDataCheck=`cat $indir/$nuisancefeat/stats/deleted_vols.txt | head -1`
if [[ $scrubDataCheck != "" ]]; then
#Presence of scrubbed volumes
#Split up deleted vols/spikes (from motionscrubbing), to echo out command for gnuplot
#spikeCount=`cat $indir/$nuisancefeat/stats/deleted_vols.txt | awk -F" " '{print NF}'`
#xmax=`cat $indir/$nuisancefeat/stats/${roi}_residvol_ts.txt | wc -l`
#ymin=`cat $indir/$nuisancefeat/stats/${roi}_residvol_ts.txt | sort -g | head -1 | awk '{print $1}'`
#ymax=`cat $indir/$nuisancefeat/stats/${roi}_residvol_ts.txt | sort -g | tail -1 | awk '{print $1}'`
#Summarize the time series data for report
#echo "set term png" > $indir/gnuplotCmds
#echo "set output \"$indir/${roi}_ms.png\"" >> $indir/gnuplotCmds
#echo "set title \"${roi}_ms Time Series (With Deleted Volumes)\"" >> $indir/gnuplotCmds
#echo "set xlabel \"Time Points\"" >> $indir/gnuplotCmds
#echo "set xrange [0:$xmax]" >> $indir/gnuplotCmds
#echo "set yrange [$ymin:$ymax]" >> $indir/gnuplotCmds
#i=1
#while [ $i -le $spikeCount ]
#do
#spikeNum=`cat $indir/$nuisancefeat/stats/deleted_vols.txt | awk -F" " -v cutfield=$i '{print $cutfield}'`
#echo "set arrow $i from $spikeNum,$ymin to $spikeNum,$ymax nohead lc rgb '"green"'" >> $indir/gnuplotCmds
#let i+=1
#done
#echo "plot \"$indir/${nuisancefeat}/stats/${roi}_residvol_ts.txt\" with lines lc rgb '"blue"'" >> $indir/gnuplotCmds
#cat $indir/gnuplotCmds | gnuplot
#Cleanup
#rm $indir/gnuplotCmds
##Creating new plots with fsl_tsplot
#~2.2% plotting difference between actual Ymin and Ymax values (higher and lower), with fsl_tsplot
yMax=`cat $indir/${nuisancefeat}/stats/${roi}_residvol_ts.txt | sort -r | tail -1 | awk '{print ($1+($1*0.0022))}'`
yMin=`cat $indir/${nuisancefeat}/stats/${roi}_residvol_ts.txt | tail -1 | awk '{print ($1-($1*0.0022))}'`
#Log the "scrubbed TRs"
xNum=`cat $indir/${nuisancefeat}/stats/${roi}_residvol_ts.txt | wc -l`
count=1
while [ $count -le $xNum ]; do
tsPlotIn=`cat $indir/${nuisancefeat}/stats/${roi}_residvol_ts.txt | head -${count} | tail -1`
delPlotCheck=`cat $indir/${nuisancefeat}/stats/deleted_vols.txt | awk '{$1=$1}1' | grep -E '(^| )'${count}'( |$)'`
if [ "$delPlotCheck" == "" ]; then
delPlot=$yMin
else
delPlot=$yMax
fi
echo $delPlot >> $indir/${nuisancefeat}/stats/${roi}_censored_TRplot.txt
let count=count+1
done
#Plot of normal data showing scrubbed TRs
fsl_tsplot -i $indir/${nuisancefeat}/stats/${roi}_residvol_ts.txt,$indir/${nuisancefeat}/stats/${roi}_censored_TRplot.txt -t "$roi Time Series" -u 1 --start=1 -x 'Time Points (TR)' -a ",Scrubbed_TR" --ymin=$yMin --ymax=$yMax -w 800 -h 300 -o $indir/${roi}.png
#Plot of "scrubbed" data
fsl_tsplot -i $indir/${nuisancefeat}/stats/${roi}_residvol_ms_ts.txt -t "$roi Time Series (Scrubbed)" -u 1 --start=1 -x 'Time Points (TR)' --ymin=$yMin --ymax=$yMax -w 800 -h 300 -o $indir/${roi}_ms.png
echo "<br><img src=\"$indir/${roi}.png\" alt=\"${roi} seed\"><img src=\"$indir/${roi}_ms.png\" alt=\"${roi}_ms seed\"><br>" >> $indir/analysisResults.html
else
#No scrubbed TRs
#Split up deleted vols/spikes (from motionscrubbing), to echo out command for gnuplot
#spikeCount=`cat $indir/$nuisancefeat/stats/deleted_vols.txt | awk -F" " '{print NF}'`
#xmax=`cat $indir/$nuisancefeat/stats/${roi}_residvol_ts.txt | wc -l`
#ymin=`cat $indir/$nuisancefeat/stats/${roi}_residvol_ts.txt | sort -g | head -1 | awk '{print $1}'`
#ymax=`cat $indir/$nuisancefeat/stats/${roi}_residvol_ts.txt | sort -g | tail -1 | awk '{print $1}'`