-
Notifications
You must be signed in to change notification settings - Fork 0
/
anz_preprocess.m
1775 lines (1687 loc) · 79.4 KB
/
anz_preprocess.m
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
function anz_preprocess(subjectdir, atlasdir, species, TR, trimf, ...
stc, sm, fil, gsr, wmcsfr, slice, PB)
%% ________________________________________________________________________
%
%
% Anzar's Preprocessing Pipeline
%
% _________________________________________________________________________
%
%
% Authored by: Anzar Abbas
% anzar.abbas@emory.edu
% Last updated 11/01/2016
%
% This function can preprocess functional MRI data from an individual
% subject with an anatomical scan and one or more functional scans. The
% preprocessing can be applied to data from rodents, monkeys, and humans,
% as long as a standard space MRI atlas exists for that species.
%
% It is important to note that this function relies heavily on FSL
% (https://fsl.fmrib.ox.ac.uk), which must be downloaded and installed.
% Additionally, this function also requires the use of a NIfTI toolbox
% by Jimmy Shen, which has been provided with this function.
%
% A document accompanying this function, named anz_preprocess_manual.pdf,
% should specify the details of the preprocessing pipeline being applied
% and how to address the paramters that need to be inputted into the
% function. A shorter description of the parameters has been given here as
% well.
%
%
% _____________________________ subjectdir ________________________________
%
% A string which specifies the entire path to the folder in which all of
% the subject's data is stored. This folder should have two types of files
%
% 1 - The anatomical image, saved as a NIfTI file, and named 't1'.
%
% 2 - The functional image(s), saved as a NIfTI file. One subject may
% have more than one functional scan. The name of the functional
% scan must begin with 'f' and be followed by two digits specifying
% the scan number. For example, a subject with one functional scan
% will have only one file named 'f01'. A subject with two or more
% functional scans will have multiple functional scans named f01,
% f02, ..., f10, and so on.
%
% However, in case of data from rodents, the user will also have to have
% pre-made anatomical and functional brain masks saved in the subject
% folder:
%
% 3 - An anatomical mask that is a binary 3D image in which everything
% that is brain is specified with the value of 1 and everything that
% is not brain is specified with a value of 0. This file must be
% named 't1_mask'.
%
% 4 - A functional mask for each scan that is a binary 3D image in which
% everything that is brain is specified with the value of 1 and
% everything that is not brain is specified with a value of 0. This
% file must be named 'f??_mask', with the question marks being the
% scan number.
%
% It is often helpful to have the subjectdir previously saved as a string
% variable in MATLAB.
%
% _______________________________ atlasdir ________________________________
%
% This is a string which specifies the entire path to the folder in which
% the standard space atlas data is stored. This folder should have the
% following files:
%
% 1 - The atlas T1 brain image. This is an anatomical image of the
% brain in which only neural tissue is present. This file must be
% named 'atlas_t1.nii.gz'
%
% 2 - The atlas brain mask image. This is a binary image in which
% everything that is brain is specified with the value of 1 and
% everything that is not brain is specified with a value of 0. This
% file must be named 'atlas_t1_brain_mask.nii.gz'
%
% 3 - In the case of rodents only, a segmented image of the brain. This
% is a standard image distributed with atlases in which all areas
% that are CSF are specified with a value of 1, all areas that are
% gray matter are specified with a value of 2, and all areas that are
% white matter are specified with a value of 3. This file must be
% named 'atlas_t1_brain_seg.nii.gz'
%
% Example atlases are provided with this function.
%
% It is often helpful to have the atlasdir previously saved as a string
% variable in MATLAB.
%
% ________________________________ species ________________________________
%
% A 1/2 input specifiying if you are working with rodents (1) or primates
% (2). Primates can mean both monkeys and humans. Depending on the answer
% to this question, the preprocessing pipeline will carry out the
% appropriate steps.
%
% __________________________________ TR ___________________________________
%
% The TR of the functional scan(s) in seconds.
%
% _________________________________ trim __________________________________
%
% How many timepoints the user wants to cut out from the beginning of the
% functional scan. The user can also enter 0 here to avoid trimming the
% functional scan.
%
% __________________________________ stc __________________________________
%
% Slice time correction - If the user does not want to conduct slice time
% correction, this value should be inputted as 0. If the user does want to
% conduct slice time correction, then this will be a 1x2 matrix. The first
% value in the matrix is about the slice acquisision (SA). If the SA was
% bottom up, then enter 1. If the SA was top down, then enter 2. The second
% value in the matrix is about the slice intervals. If the slices were
% collected in series, then enter 1. If they were collected in an
% interleaved fashion, then enter 2 here.
%
% ___________________________________ sm __________________________________
%
% Smoothing - The amount the user would like to spatially filter or smooth
% the functional data in mm. For reference, this is typically 6mm for
% humans, 2mm for macaques, and 0.5mm for rats.
%
% ___________________________________ gsr _________________________________
%
% Global signal regression - 1/0 input describing whether or not the user
% would like to conduct global signal regression on the functional data.
%
% _________________________________ wmcsfr ________________________________
%
% White matter and CSF signal regression - 1/0 input describing whether
% or not the user would like to conduct white matter and CSF signal
% regression on the functional data. This value is always 0 for rodents as
% this function does not conduct WM/CSF signal regression in rodents.
%
% ___________________________________ fil _________________________________
%
% Temporal filter - All functional scans will undergo tempral filtering.
% fil is a 1x2 matrix in which the first value is the high pass filter and
% the second value is the low pass filter. Both values are in Hz.
%
% _________________________________ slice _________________________________
%
% Reduce by one spatial dimension - 1/0 input describing whether or not the
% user would like to slice all 3D brain volumes (anatomical and functional)
% into 2D matrices with all the slices for each timepoint laid out in one
% image. This will convert 3D anatomical images to 2D anatomical images and
% 4D functional images to 3D functional images. This is helpful for
% visualization of all slices simultaneously.
%
% ___________________________________ PB __________________________________
%
% PushBullet - Optional input - Pushbullet is a tool that I use to keep me
% updated on processes I know will take a long time. This preprocessing
% pipeline is one of them. Entering a pushbullet API key specific to the
% user here can keep them updated on the progress of the preprocessing
% pipeline. What's even more useful is that they will also be notified if
% the code errs, so they can tend to it sooner rather than later. To get
% your own API key, follow the instructions on the Pushbullet website.
%
% _________________________________________________________________________
%% ______________________________________________________________________ %
% %
% Setup %
% _______________________________________________________________________ %
fprintf('\nBeginning preprocessing pipeline\n\n');
if ismac == 1
fsldir = '/Applications/fsl/bin/';
setenv('FSLDIR','/Applications/fsl');
setenv('FSLOUTPUTTYPE','NIFTI_GZ');
else
fsldir = '/usr/local/fsl/bin/';
setenv('FSLDIR','/usr/local/fsl');
setenv('FSLOUTPUTTYPE','NIFTI_GZ');
end
% Setting FSL environment and output filetype to '.nii.gz' Saving the
% directories in which all the FSL functions are stored. This part of the
% script is making the assumption that the FSL directory on your computer
% is in the default location that FSL uses when it is being installed. If
% that is not the case, you should change the path of the directory in the
% code above.
fprintf('Pushbullet key ')
if nargin > 12
p = Pushbullet(PB);
fprintf('assigned\n')
else
fprintf('not assigned\n\n');
end
% Entering the API key for the user if they care to use pushbullet during
% the preprocessing pipeline (recommended by author but not at all
% necessary).
k = strfind(subjectdir,'subject');
sub = [subjectdir(k:end),' - '];
clear k
% This is simply helping with the text that will be displayed as the
% function is running. 'sub' is a string with the name of the subject that
% will be printed with every update
warning('off','all');
% There are a few warnings that MATLAB outputs depending on the type of
% computer this function that is being run on. As far as my understanding
% goes, they are irrelevant, hence the function is temporarily turning off
% warnings so that the updates being printed as the code runs look clean.
%
% _______________________________________________________________________ %
cd (subjectdir)
switch species
case 1
%% ______________________________________________________________________ %
% %
% Rodents %
% _______________________________________________________________________ %
% fprintf('Working on rodent data\n\n')
%
% % Anatomical Image Reorientation ________________________________ %
% %
% T = datetime('now');
% time = whatsthetime(T);
% fprintf([time,sub,'Anatomical image reorientation ... '])
% % Every step in this function will print an update in the command
% % window. That update will include the time and the name of the
% % subject and scan being preprocessed. The above three lines of
% % code will repeat with every step.
% cmd = [fsldir,'fslreorient2std t1 t1_reorient'];
% system(cmd);
% fprintf('Done\n')
% % Reorienting the anatomical image to standard just in case it
% % isn't already.
% % _______________________________________________________________ %
%
%
%
% % Anatomical Mask Reorientation _________________________________ %
% %
% T = datetime('now');
% time = whatsthetime(T);
% fprintf([time,sub,'Anatomical mask reorientation ... '])
% cmd = [fsldir,'fslreorient2std t1_mask t1_mask'];
% system(cmd);
% fprintf('Done\n')
% % Reorienting the user-created anatomical image mask to standard
% % just in case it isn't already.
% % _______________________________________________________________ %
%
%
%
% % Anatomical Brain Extraction ___________________________________ %
% %
% T = datetime('now');
% time = whatsthetime(T);
% fprintf([time,sub,'Anatomical brain extraction ... '])
% cmd = [fsldir,'fslmaths t1_reorient -mul t1_mask', ...
% ' t1_reorient_brain'];
% system(cmd);
% fprintf('Done\n')
% % Multiplying the anatomical image by the brain mask to get a brain
% % extracted image.
% % _______________________________________________________________ %
%
%
%
% % Anatomical Brain Registration to Atlas ________________________ %
% %
% T = datetime('now');
% time = whatsthetime(T);
% fprintf([time,sub,'Anatomical brain registration to atlas ... '])
% cmd = [fsldir,'flirt -in t1_reorient_brain -ref ', ...
% atlasdir,'/atlas_t1_brain -out t1_reorient_brain_regatlas', ...
% ' -omat t1_to_atlas.mat'];
% system(cmd);
% system('rm t1_to_atlas.mat');
% fprintf('Done\n')
% % Using FSL's FLIRT tool to register the anatomcial images to the
% % rat atlas brain image.
% % _______________________________________________________________ %
%
%
%
% % Anatomical Brain Segmentation _________________________________ %
% %
% T = datetime('now');
% time = whatsthetime(T);
% fprintf([time,sub,'Anatomical brain segmentation ... '])
% nii = load_untouch_nii([atlasdir,'/atlas_t1_brain_seg.nii.gz']);
% atlas_seg = nii.img;
% % Loading the atlas file that is all three tissues segmented.
% csf = 1 .* (atlas_seg == 1);
% % Creating CSF mask
% gm = 1 .* (atlas_seg == 2);
% % Creating gray matter mask
% wm = 1 .* (atlas_seg == 3);
% % Creating white matter mask
% nii.img = csf;
% save_untouch_nii(nii,'t1_reorient_brain_regatlas_csf.nii.gz');
% % Saving the CSF binary mask 3D image
% nii.img = gm;
% save_untouch_nii(nii,'t1_reorient_brain_regatlas_gm.nii.gz');
% % Saving the GM binary mask 3D image
% nii.img = wm;
% save_untouch_nii(nii,'t1_reorient_brain_regatlas_wm.nii.gz');
% % Saving the WM binary mask 3D image
% clear nii atlas_seg
% fprintf('Done\n')
% % Normally (in the case of primates), the function uses FSL's FAST
% % tool to segment the individual's anatomical scan. However, FAST
% % does not work reliably on rodent data. Since the data is
% % registered to a standard space atlas, the function instead uses
% % the tissue segmentation masks from the atlas.
% % _______________________________________________________________ %
%
%
%
% % Slicing anatomical images into 2D matrices ____________________ %
% %
% T = datetime('now');
% time = whatsthetime(T);
% fprintf([time,sub,'Slicing anatomical images into 2D images ... '])
% if slice == 1
% nii = load_nii('t1_reorient_brain_regatlas.nii.gz');
% anatomical_images_3d = cell(4,1);
% % This will hold the t1 image as well as all the segmented
% % tissue maps
% anatomical_images_3d{1} = nii.img;
% % The first index holds the t1 image
% anatomical_images_3d{2} = csf;
% anatomical_images_3d{3} = gm;
% anatomical_images_3d{4} = wm;
% % The other three indices hold the CSF, gray matter, and white
% % matter tissue maps respectively
% [~,~,z] = size(anatomical_images_3d{1});
% % Getting dimensions of T1 image
% num_boxes = z;
% % Predefining the number of boxes our image is going to have
% if rem(sqrt(z),1) >= eps
% % If the brain in the z dimension is not a perfect square
% while rem(sqrt(z),1) >= eps
% % Well then while that's the case
% num_boxes = num_boxes + 1;
% % Adding one to the number of boxes
% end
% end
% anatomical_image_slices = cell(4,1);
% % This cell array will hold all the cell arrays that will hold
% % all the slices of the brains for each of the anatomical
% % images
% anatomical_images_2d = cell(4,1);
% % This is the cell array that will hold the 2D image for each
% % of the anatomical images
% for i = 1:4
% anatomical_image_slices{i} = cell(num_boxes,1);
% % This is the cell array that will hold the slices for one
% % anatomical image
% for j = 1:z
% temp = squeeze(anatomical_images_3d{i}(:,:,j));
% % For each slice in each anatomical image
% temp = rot90(temp,1);
% % Rotate to make anterior face upwards
% anatomical_image_slices{i}{j} = temp;
% end
% [x,y] = size(anatomical_image_slices{i}{1});
% % Getting x and y dimensions of each box
% empty_space = zeros(x,y);
% % Making empty space for the empty boxes
% for j = z+1:z+(num_boxes-z)
% % Going through the empty boxes
% anatomical_image_slices{i}{j} = empty_space;
% % And filling them with empty space
% end
% anatomical_images_2d{i} = zeros(x*sqrt(num_boxes), ...
% y*sqrt(num_boxes));
% % Predefining the 2D anatomical image
% count = 0;
% % Going to count through the slices
% for row = 1:sqrt(num_boxes)
% for column = 1:sqrt(num_boxes)
% count = count + 1;
% % Going through each row and column
% anatomical_images_2d{i}((row*x-x+1):(row*x), ...
% (column*y-y+1):(column*y)) = ...
% anatomical_image_slices{i}{count};
% % Organizing the brain slices in rows and columns
% end
% end
% end
% anat = anatomical_images_2d{1}; %#ok<*NASGU>
% csf = anatomical_images_2d{2};
% gm = anatomical_images_2d{3};
% wm = anatomical_images_2d{4};
% % Renaming all the sliced anatomical images that have been laid
% % out as 2D images
% save('t1_reorient_brain_regatlas_2D.mat','anat');
% save('t1_reorient_brain_regatlas_csf_2D.mat','csf');
% save('t1_reorient_brain_regatlas_gm_2D.mat','gm');
% save('t1_reorient_brain_regatlas_wm_2D.mat','wm');
% % Saving each of those matrices as .mat files
% clear nii anatomical_images_3d z anatomical_images_slices
% clear anatomical_images_2d i j temp x y empty_space count
% clear row column anat
% fprintf('Done\n')
% else
% fprintf('Skipped\n')
% end
% % This step cuts the 3D anatomical images slice by slice and lays
% % out all the slices in one plane. If the user chooses to do this,
% % they can view all slices of the brain at the same time by loadinf
% % the .mat files that have just been saved.
% % _______________________________________________________________ %
%
%
%
% % Going through each functional scan ____________________________ %
% %
% f_scans = dir('f*');
% f_scans = {f_scans.name}';
% count = 0;
% indices = 0;
% for i = 1:length(f_scans)
% if length(f_scans{i}) > 10
% count = count + 1;
% indices(count) = i; %#ok<*AGROW>
% end
% end
% try
% f_scans(indices) = [];
% catch
% end
% for i = 1:length(f_scans)
% f_scans{i} = f_scans{i}(1:3);
% end
% clear i
% % Creating a cell array of all the raw functional scans in which
% % just the name of the functional scans are save in a string.
% for i = 1:length(f_scans)
% filename = f_scans{i};
% scan = [f_scans{i},' - '];
% fprintf('\n')
% % This is the file that we're currently working on at all times and
% % it will update with each step that we conduct
% %
% % The code above is simply setting up a for loop for every
% % functional scan that this particular subject has. If the code is
% % erring at this part of the function, please make sure that all
% % functional scans have been named in the way specified above and
% % in the manual.
% % _______________________________________________________________ %
%
%
%
% % Reorienting functional scan _______________________________ %
% %
% T = datetime('now');
% time = whatsthetime(T);
% fprintf([time,sub,scan,'Reorienting functional scan ... '])
% cmd = [fsldir,'fslreorient2std ', ...
% filename,' ', filename, '_reorient'];
% system(cmd);
% filename = [filename,'_reorient'];
% fprintf('Done\n')
% % Reorienting the functional scan to standard, just in case it
% % isn't already
% % ___________________________________________________________ %
%
%
%
% % Trimming functional scan __________________________________ %
% %
% T = datetime('now');
% time = whatsthetime(T);
% fprintf([time,sub,scan,'Trimming functional scan ... '])
% if trimf ~= 0
% cmd = [fsldir,'fslnvols ',filename];
% [~,timepoints] = system(cmd);
% % Finding out how many total timepoints are in the
% % functional scan as we will be needing that number in the
% % next step
% cmd = [fsldir,'fslroi ',filename,' ',filename,'_trim ', ...
% num2str(trimf),' ',num2str(str2double((timepoints)))];
% system(cmd);
% filename = [filename,'_trim'];
% fprintf('Done\n')
% else
% fprintf('Skipped\\n')
% end
% clear trim timepoints
% % Trimming the front of the functional scan, if specified by
% % user - and updating the filename to include _trim in it if we
% % did conduct the trimming. Reasoning for this trimming is
% % included in the manual.
% % ___________________________________________________________ %
%
%
%
% % Slice time correction _____________________________________ %
% %
% T = datetime('now');
% time = whatsthetime(T);
% fprintf([time,sub,scan,'Slice time correction ... '])
% if stc ~= 0
% if stc(1) == 1
% if stc(2) == 1
% cmd = [fsldir,'slicetimer -i ',filename,' -o ', ...
% filename,'_stc -r ',num2str(TR)];
% system(cmd);
% % Running slice time correction with slices
% % acquired bottom up and in series
% else
% cmd = [fsldir,'slicetimer -i ',filename,' -o ', ...
% filename,'_stc -r ',num2str(TR),' --odd'];
% system(cmd);
% % Running slice time correction with slices
% % acquired bottom up and interleaved
% end
% else
% if stc(2) == 1
% cmd = [fsldir,'slicetimer -i ',filename,' -o ', ...
% filename,'_stc -r ',num2str(TR), ' --down'];
% system(cmd);
% % Running slice time correction with slices
% % acquired top down and in series
% else
% cmd = [fsldir,'slicetimer -i ',filename,' -o ', ...
% filename,'_stc -r ',num2str(TR), ...
% ' --down --odd'];
% system(cmd);
% % Running slice time correction with slices
% % acquired bottom down and interleaved
% end
% end
% filename = [filename,'_stc'];
% % Updating the filename only if we slice time corrected
% fprintf('Done\n')
% else
% fprintf('Skipped\n')
% end
% % This whole if statement is conducting the slice time
% % correction if it was specified by the user in the way that it
% % was specified by the user. If there are further complications
% % in slice time correction, the code above can be adjusted to
% % accomodate the way the scans were collected.
% % ___________________________________________________________ %
%
%
%
% % Motion correction of functional scans _____________________ %
% %
% T = datetime('now');
% time = whatsthetime(T);
% fprintf([time,sub,scan,'Motion correction ... '])
% cmd = [fsldir,'mcflirt -in ',filename,' -out ', ...
% filename,'_mc -refvol 0 -plots'];
% system(cmd);
% filename = [filename,'_mc'];
% % Conducting motion correction on the functional scans and
% % saving all the motion paramters. Below, I will plot out the
% % motion parameters as an output.
% par = load([filename,'.par']);
% fig = figure('visible','off','position',get(0,'screensize'));
% % Creating a hidden figure that will eventually be saved as a
% % JPEG file for the user's convenience.
% subplot(2,1,1);
% hold on;
% plot(par(:,1));
% plot(par(:,2));
% plot(par(:,3));
% title('Rotation in radians');
% legend('X','Y','Z');
% set(gca,'ylim',[-0.2 0.2],'fontsize',12);
% ylabel('Rotations (rad)');
% xlabel('Timepoints');
% % Plotting the rotation paramters
% subplot(2,1,2);
% hold on
% plot(par(:,4));
% plot(par(:,5));
% plot(par(:,6));
% title('Transformation in mm');
% legend('X','Y','Z');
% set(gca,'ylim',[-3 3],'fontsize',12);
% ylabel('Transformations (mm)');
% xlabel('Timepoints');
% % Plotting the transformation parameters
% saveas(fig,[filename,'_visual.jpg']);
% % Saving
% fprintf('Done\n')
% % This series of steps is using FSL's MCFLIRT tool to conduct
% % motion correction and then plotting out and saving the motion
% % parameters as a separate JPEG file that can be viewed later.
% % ___________________________________________________________ %
%
%
%
% % Functional Mask Reorientation _____________________________ %
% %
% T = datetime('now');
% time = whatsthetime(T);
% fprintf([time,sub,scan,'Functional scan reorientation ... '])
% cmd = [fsldir,'fslreorient2std ',scan(1:3),'_mask ', ...
% scan(1:3),'_mask'];
% system(cmd);
% % Reorienting the user-created functional mask to standard just
% % in case it isn't already
% fprintf('Done\n')
% % ___________________________________________________________ %
%
%
%
% % Functional Brain Extraction _______________________________ %
% %
% T = datetime('now');
% time = whatsthetime(T);
% fprintf([time,sub,scan,'Brain extraction from skull ... '])
% cmd = [fsldir,'fslmaths ', filename, ...
% ' -mul ',scan(1:3),'_mask ',filename,'_brain'];
% system(cmd);
% filename = [filename,'_brain'];
% % Multiplying the functional scan by the brain mask to get a
% % brain extracted image
% fprintf('Done\n')
% %
% % ___________________________________________________________ %
%
%
%
% % Functional Brain Registration to Atlas ____________________ %
% %
% T = datetime('now');
% time = whatsthetime(T);
% fprintf([time,sub,scan,'Registration to atlas space ... '])
% cmd = [fsldir,'flirt -in ',filename,' -ref ', ...
% atlasdir,'/atlas_t1_brain -omat f_to_atlas.mat'];
% system(cmd);
% cmd = [fsldir,'flirt -in ',filename,' -ref ', ...
% atlasdir,'/atlas_t1_brain -applyxfm -init ', ...
% 'f_to_atlas.mat -out ',filename,'_regatlas'];
% system(cmd);
% cmd = [fsldir,'fslmaths ',filename,'_regatlas -mas ', ...
% atlasdir,'/atlas_t1_brain_mask ',filename,'_regatlas'];
% system(cmd);
% system('rm f_to_atlas.mat');
% filename = [filename,'_regatlas'];
% % Using FSL's FLIRT to register ther functional scan to the
% % atlas brain image
% fprintf('Done\n')
% % ___________________________________________________________ %
%
%
%
% % Smoothing of Functional Scan ______________________________ %
% %
% T = datetime('now');
% time = whatsthetime(T);
% fprintf([time,sub,scan,'Spatial smoothing ... '])
% cmd = [fsldir,'fslmaths ', filename, ' -s ', num2str(sm), ...
% ' ',filename,'_sm'];
% system(cmd);
% filename = [filename,'_sm'];
% % Using FSL's fslmaths tool to smooth the functional scans by
% % however much the user specified
% fprintf('Done\n')
% % ___________________________________________________________ %
%
%
%
% % Temporal Filtering of functional data _____________________ %
% %
% T = datetime('now');
% time = whatsthetime(T);
% fprintf([time,sub,scan,'Temporal filtering ... '])
% % Starting this step, we are no longer using FSL for
% % preprocessing. Rather, we're loading the NIfTI files into
% % MATLAB and performing some steps on them directly. This is
% % why the NIfTI toolbox provided with this function is
% % critical.
% nii = load_untouch_nii([filename,'.nii.gz']);
% f = nii.img;
% [X,Y,Z,T] = size(f);
% % Getting the dimensions of the functional scan
% f = reshape(f,[X*Y*Z,T]);
% % Reshaping the scan into a 2D matrix of voxels over time
% f = f';
% tc = f;
% SF = 1/TR';
% % Calculating the sampling frequency using the TR
% x = size(tc,1);
% tc_mirror = tc(end:-1:1,:);
% tc_extended = [tc_mirror; tc; tc_mirror];
% tc_fft = fft(tc_extended);
% high_cutoff = round(size(tc_extended,1)*(fil(1)/SF));
% tc_fft(1:high_cutoff,:) = 0;
% low_cutoff = round(size(tc_extended,1)*(fil(2)/SF));
% tc_fft(low_cutoff:end,:) = 0;
% tc_filtered = ifft(tc_fft);
% temp = 2*real(tc_filtered);
% tc_filtered = temp(x+1:2*x,:);
% f = tc_filtered';
% % The above steps are applying a FFT filter on the functional
% % data
% f = reshape(f,[X,Y,Z,T]);
% nii.img = f;
% save_untouch_nii(nii,[filename,'_fil.nii.gz']);
% cmd = [fsldir,'fslmaths ',filename,'_fil -mas ', atlasdir, ...
% '/atlas_t1_brain_mask ',filename,'_fil'];
% system(cmd);
% % Just to cut down on file size, I'm multiplying the functional
% % scan by the brain mask again
% filename = [filename,'_fil'];
% fprintf('Done\n')
% clear f1 tc_filtered temp tc_fft low_cutoff high_cutoff
% clear tc_extended tc_mirror x
% % ___________________________________________________________ %
%
%
%
% % Global signal regression __________________________________ %
% %
% T = datetime('now');
% time = whatsthetime(T);
% fprintf([time,sub,scan,'Global signal regression ... '])
% if gsr == 1
% nii = load_untouch_nii([filename,'.nii.gz']);
% f = nii.img;
% % Loading the functional scan into Matlab
% f_gsr = f;
% % Predefining the global signal regressed functional scan
% f_mean = zeros(1,size(f,4));
% % Predefining what will me the mean of the functional scan
% for t = 1:size(f,4)
% temp = f(:,:,:,t);
% temp_sum = sum(temp(:));
% temp_length = length(find(temp(:)));
% temp_mean = temp_sum/temp_length;
% f_mean(t) = temp_mean;
% end
% % Taking the mean of the functional timeseries. This is the
% % functional timecourse.
% f_mean_zsc = zscore(f_mean);
% % Z-scoring the mean
% for x = 1:size(f,1)
% for y = 1:size(f,2)
% for z = 1:size(f,3)
% v = double(squeeze(f(x,y,z,:)));
% beta = (f_mean_zsc * f_mean_zsc') \ ...
% (f_mean_zsc * v);
% f_gsr(x,y,z,:) = v' - f_mean_zsc * beta;
% end
% end
% end
% % Regressing the global signal from the functional
% % timeseries
% f = f_gsr;
% filename = [filename,'_gsr'];
% nii.img = f;
% save_untouch_nii(nii,[filename,'.nii.gz']);
% % Saving the global signal regressed functional scan
% fprintf('Done\n')
% else
% fprintf('Skipped\n')
% end
% % Conducting global signal regression using GLM regression on
% % the functional data if the user specified.
% % ___________________________________________________________ %
%
%
%
% % Z-scoring all voxels in functional scan ___________________ %
% %
% T = datetime('now');
% time = whatsthetime(T);
% fprintf([time,sub,scan,'Z-scoring ... '])
% if gsr ~= 1
% nii = load_untouch_nii([filename,'.nii.gz']);
% f = nii.img;
% % Loading the functional scan in case it wasn't already
% % loaded in the previous step
% end
% mask = load_nii([atlasdir,'/atlas_t1_brain_mask.nii.gz']);
% mask = mask.img;
% % Loading the T1 mask
% [x,y,z,~] = size(f);
% % Assigning variables to the dimensions of the scan
% for j = 1:x
% for k = 1:y
% for l = 1:z
% if mask(j,k,l) == 1
% voxel = f(j,k,l,:);
% % Working on one brain voxel at a time
% voxel_zsc = zscore(voxel);
% % Z-scoring the voxel signal
% f(j,k,l,:) = voxel_zsc;
% % Adding the zscored value to the zscored
% % functional scan matrix
% end
% end
% end
% end
% nii.img = f;
% save_untouch_nii(nii,[filename,'_zsc.nii.gz']);
% filename = [filename,'_zsc'];
% clear nii x y z j k l
% fprintf('Done\n')
% % Saving the newly z-scored functional scan
% % ___________________________________________________________ %
%
%
%
% % Slicing functional images into 3D matrices ________________ %
% %
% T = datetime('now');
% time = whatsthetime(T);
% fprintf([time,sub,scan,'Slicing into 3D matrices ... '])
% if slice == 1
% [~,~,z,~] = size(f);
% % Getting dimensions of functional scan
% num_boxes = z;
% % Predefining the number of boxes the output image is going
% % to have.
% if rem(sqrt(z),1) >= eps
% % If the brain in the z dimension is not a perfect
% % square
% while rem(sqrt(num_boxes),1) >= eps
% % Well then while that's the case
% num_boxes = num_boxes + 1;
% % Adding to the number of boxes
% end
% end
% slices = cell(num_boxes,1);
% % Predefining cell array that will hold timeseries for all
% % slices
% for j = 1:z
% % Going through all slices
% temp = squeeze(f(:,:,j,:));
% % This is the timeseries for a slice
% temp = rot90(temp,1);
% % Rotating the image to make anterior face upwards
% slices{j} = temp;
% % Adding timeseries for each slice in each cell
% end
% [x,y,t] = size(slices{1});
% % Updating these values because of the rotation
% empty_space = zeros(x,y,t);
% % Empty space to go in empty boxes
% for j = z+1:z+(num_boxes-z)
% % Going through the rest of the cells in slices
% slices{i} = empty_space;
% % Adding empty space into cell
% end
% f_sliced = zeros(x*sqrt(num_boxes),y*sqrt(num_boxes),t);
% % Predefining the output image shape
% count = 0;
% % Will be counting through the boxes
% for row = 1:sqrt(num_boxes)
% % For every row of brain slices
% for column = 1:sqrt(num_boxes)
% % For every column of brain slices
% count = count + 1;
% % Updating the count
% f_sliced((row*x-x+1):(row*x),(column*y-y+1): ...
% (column*y),:) = slices{count};
% % Organizing brain slices in rows and columns
% end
% end
% func = f_sliced;
% filename = [filename,'_3D'];
% save([filename,'.mat'],'func');
% fprintf('Done\n')
% else
% fprintf('Skipped\n')
% end
% % The abve steps are slicing the 4D functional scans into 3D
% % functional scans by removing one of the spatial dimensions.
% % ___________________________________________________________ %
%
% T = datetime('now');
% time = whatsthetime(T);
% fprintf([time,sub,scan,'Finished preprocessing\n'])
%
% if nargin > 12
% string = [sub,scan,'Functional scan preprocessed'];
% p.pushNote([],string,'');
% % Keeping the user updated with progress for each scan
% end
%
% end
% % Ending everything that we were doing to each functional scan
%
% % _______________________________________________________________________ %
case 2 % Primates
%% ______________________________________________________________________ %
% %
% Primates %
% _______________________________________________________________________ %
fprintf('Working on primate/human data\n\n')
% Anatomical Image Reorientation ________________________________ %
%
T = datetime('now');
time = whatsthetime(T);
fprintf([time,sub,'Anatomical brain reorientation ... '])
% Every step in this function will print an update in the command
% window. That update will include the time and the name of the
% subject and scan being preprocessed. The above three lines of
% code will repeat with every step.
cmd = [fsldir,'fslreorient2std t1 t1_reorient'];
system(cmd);
fprintf('Done\n')
% Reorienting the anatomical image to standard just in case it
% isn't already.
% _______________________________________________________________ %
% Anatomical Brain Registration to Atlas ________________________ %
%
T = datetime('now');
time = whatsthetime(T);
fprintf([time,sub,'Anatomical brain registration to atlas ... '])
cmd = [fsldir,'flirt -in t1 -ref ', atlasdir, ...
'/atlas_t1 -out t1_reorient_regatlas', ...
' -omat t1_to_atlas.mat'];
system(cmd);
system('rm t1_to_atlas.mat');
fprintf('Done\n')
% Using FSL's flirt to register the anatomcial images to the atlas
% anatomical image.
% _______________________________________________________________ %
% Anatomical Brain Extraction ___________________________________ %
%
T = datetime('now');
time = whatsthetime(T);
fprintf([time,sub,'Anatomical brain extraction ...'])
cmd = [fsldir,'fslmaths t1_reorient_regatlas -mas ', atlasdir, ...
'/atlas_t1_brain_mask t1_reorient_regatlas_brain'];
system(cmd);
fprintf('Done\n')
% Using FSL's fslmaths tool to separate out the brain from the
% anatomical image
% _______________________________________________________________ %
% Anatomical Brain Segmentation _________________________________ %
%
T = datetime('now');
time = whatsthetime(T);
fprintf([time,sub,'Anatomical brain segmentation ... '])
cmd = [fsldir,'fast -g t1_reorient_regatlas_brain'];
system(cmd);
system('rm t1_reorient_regatlas_brain_seg.nii.gz');
system(['mv t1_reorient_regatlas_brain_seg_0.nii.gz ', ...
't1_reorient_regatlas_brain_csf.nii.gz']);
system(['mv t1_reorient_regatlas_brain_seg_1.nii.gz ', ...
't1_reorient_regatlas_brain_gm.nii.gz']);
system(['mv t1_reorient_regatlas_brain_seg_2.nii.gz ', ...
't1_reorient_regatlas_brain_wm.nii.gz']);
system('rm *pve*'); system('rm *mixeltype*');
% Using FSL's FAST tool to separate out the three main tissue types
% in the brain and then renaming them so that their filenames are
% more indicative of what they actually are
fprintf('Done\n')
% _______________________________________________________________ %
% Slicing anatomical images into 2D matrices ____________________ %
%
T = datetime('now');
time = whatsthetime(T);
fprintf([time,sub,'Anatomical brain slicing ... '])
if slice == 1