-
Notifications
You must be signed in to change notification settings - Fork 0
/
RS_FetfMRI.m
2369 lines (2047 loc) · 109 KB
/
RS_FetfMRI.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
%% Requirements:
% 1) Matlab2013 or above;
% 2) SPM12 or above;
% 3) RS-FetfMRI package: 'art', 'Template' folder and subfolder
% 'Create_Template.m' Matlab function
%% Initialization: We recomend to see the Video tutorial and to read the User manual provided in the github repository
% 1) Add the ‘art’ path folder in Matlab;
% 2) Inside the ‘art’ folder there are three ‘.cfg’ text files. In
% these files, three paths need to be modified (‘image_dir’,
% ‘motion_dir’ and ‘mask_file’):
% - File_1Srub.cfg: Update each path maintaining this
% folder at the end:
% 1. image_dir: /M2_WS_03_Scrub/9999/
% 2. motion_dir: /M2_WS_03_Scrub/9999/
% 3. mask_file: /M2_WS_03_Scrub/9999/meanm9999_001.nii
% - File_2Scrb_orig.cfg AND File_2Scrb.cfg: Update each path but maintain this folder at the end:
% 1. image_dir: /M4_BS_02_Scrub/
% 2. motion_dir: /M4_BS_02_Scrub/Motion_file/
% 3. mask_file: /M4_BS_02_Scrub/9999/r2mr1m9999_0001.nii
% 3) Download The user must download the CRL Fetal Brain Atlas images – GW 21 through 37 –
% from <http://crl.med.harvard.edu/research/fetal_brain_atlas/> and then unzip all of the ‘STA*.nii.gz’ files as demonstrated in the example for a 21st Gestational Week (GW) below:
% STA21.nii.gz STA21.nii or STA37exp.nii.gz STA37.nii
% All of the ‘STA*.nii’ files must be placed inside the subfolder ‘Template_orig’ which is found within the ‘Templates’ folder (if not, create it).
%% Follow the MANUAL and the Video for more detailed code explanation.
%% INITIALIZATION: Path creation and Setting Paths
close all
clc
clear all
% General Path set up:
global path_general;
path=cd();
path_to_cffg_file=[path,'/art/'];
path_general=[path,'/'];
addpath(path_to_cffg_file);
cd(path_general)
% Folders Creation
folder2create={'M1_PP_01_OrigVol','M1_PP_02_4Dto3D','M1_PP_03_Reorient','M1_PP_04_Rename','M2_WS_01_Mask','M2_WS_02_Realign','M2_WS_03_Scrub','M2_WS_04_Rename','M3_WS_01_SegRefVols','M3_WS_02_MaskRefVols','M3_WS_03_RealignMaskRefVols','M3_WS_04_Rename','M4_BS_01_Realign_Reslice','M4_BS_02_Scrub','M4_BS_03_Rename','M5_BS_01_SegMeanRefVol','M5_BS_02_MaskMeanRefVol','M5_BS_03_NormMaskMeanRefVol','M6_BS_01_MaskAllVols','M6_BS_02_NormMaskAllVols'};
for num_folder=1:size(folder2create,2)
if ~exist(folder2create{1,num_folder}, 'dir')
mkdir(folder2create{1,num_folder});
else
disp([folder2create{1,num_folder},' already exists.'])
end
end
% Paths name creation:
%Module1
path_to_M1_PP_01_OrigVol=[path_general,'M1_PP_01_OrigVol'];
path_to_M1_PP_02_4Dto3D=[path_general,'M1_PP_02_4Dto3D'];
path_to_M1_PP_03_Reorient=[path_general,'M1_PP_03_Reorient'];
path_to_M1_PP_04_Rename=[path_general,'M1_PP_04_Rename'];
%Module2
path_to_M2_WS_01_Mask=[path_general,'M2_WS_01_Mask'];
path_to_M2_WS_02_Realign=[path_general,'M2_WS_02_Realign'];
path_to_M2_WS_03_Scrub=[path_general,'M2_WS_03_Scrub'];
path_to_M2_WS_04_Rename=[path_general,'M2_WS_04_Rename'];
%Module3
path_to_M3_WS_01_SegRefVols=[path_general,'M3_WS_01_SegRefVols'];
path_to_M3_WS_02_MaskRefVols=[path_general,'M3_WS_02_MaskRefVols'];
path_to_M3_WS_03_RealignMaskRefVols=[path_general,'M3_WS_03_RealignMaskRefVols'];
path_to_M3_WS_04_Rename=[path_general,'M3_WS_04_Rename'];
%Module4
path_to_M4_BS_01_Realign_Reslice=[path_general,'M4_BS_01_Realign_Reslice'];
path_to_M4_BS_02_Scrub=[path_general,'M4_BS_02_Scrub'];
path_to_M4_BS_03_Rename=[path_general,'M4_BS_03_Rename'];
%Module5
path_to_M5_BS_01_SegMeanRefVol=[path_general,'M5_BS_01_SegMeanRefVol'];
path_to_M5_BS_02_MaskMeanRefVol=[path_general,'M5_BS_02_MaskMeanRefVol'];
path_to_M5_BS_03_NormMaskMeanRefVol=[path_general,'M5_BS_03_NormMaskMeanRefVol'];
%Module6
path_to_M6_BS_01_MaskAllVols=[path_general,'M6_BS_01_MaskAllVols'];
path_to_M6_BS_02_NormMaskAllVols=[path_general,'M6_BS_02_NormMaskAllVols'];
%Template
path_original_tmp=[path_general,'Template/Template_for_session'];
cd(path_general)
save('All_paths','path_general','path_original_tmp','path_to_cffg_file','path_to_M1_PP_01_OrigVol','path_to_M1_PP_02_4Dto3D','path_to_M1_PP_03_Reorient','path_to_M1_PP_04_Rename','path_to_M2_WS_01_Mask','path_to_M2_WS_02_Realign','path_to_M2_WS_03_Scrub','path_to_M2_WS_04_Rename','path_to_M3_WS_01_SegRefVols','path_to_M3_WS_02_MaskRefVols','path_to_M3_WS_03_RealignMaskRefVols','path_to_M3_WS_04_Rename','path_to_M4_BS_01_Realign_Reslice','path_to_M4_BS_02_Scrub','path_to_M4_BS_03_Rename','path_to_M5_BS_01_SegMeanRefVol','path_to_M5_BS_02_MaskMeanRefVol','path_to_M5_BS_03_NormMaskMeanRefVol','path_to_M6_BS_01_MaskAllVols','path_to_M6_BS_02_NormMaskAllVols');
spm_input('Please copy your original volumes folder into the folder -M1_PP_01_OrigVol- and press yes to start the processing:','-1','bd','Yes');
%% MODULO 1:
disp('---------------------------------------------------------------------------------')
disp('---------------------------------------------------------------------------------')
disp('|||\\\\///||| //°°°\\ |||\\ ||| ||| ||| ||||||| //|| ')
disp('||| \\\///||| // \\ ||| \\ ||| ||| ||| ||| // || ')
disp('||| \\// ||| (( )) ||| || ||| ||| ||| ||||||| || ')
disp('||| \/ ||| \\ // ||| // ||| ||| ||| ||| || ')
disp('||| ||| \\___// |||// ||||||||||| ||||||| ||||||| __||__')
disp('---------------------------------------------------------------------------------')
disp('------------------ Functional Magnetic Resonance imaging fMRI -------------------')
disp('-------------------------- Within Session Processing ---------------------------')
disp('---------------------From original session to reorientation----------------------')
disp('---------------------------------------------------------------------------------')
disp(' . . . . . . . . . ')
disp(' ./ ( ( ) . ).( )).\. ')
disp(' .) ) ) ( . ( . ) ( ..\. ')
disp(' .() ( ( ) . ) . ( )) ( | ')
disp(' .(( ) ) ) ( . ( ..) ()__/ ')
disp(' .\( / / . ) _)_)_______/ ')
disp(' .( ( ( (__/__/ . ')
disp(' \._)_). ) ')
disp(' ._)_)./ ')
disp(' ._)_)./ ')
disp(' ')
%% Creation of folders for each session. Renaming nifti files with default session name inside M1_PP_01_OrigVol.
cd(path_general)
diary Module1_logfile
disp('First of all, Good Day! Thanks for using this program! ')
disp(' ')
disp('Creating folder and Renaming 4D file with default name.')
cd(path_to_M1_PP_01_OrigVol);
list=dir();
% Creating new session names ('1001','1002', etc.)
list(ismember({list.name},{'.','..','.DS_Store'}))=[];
Session_name = cell(length(list),1);
for i=1:length(list)
Session_name{i,1}=num2str(i,'%02d');
Session_name{i,1}=['10',Session_name{i,1}];
disp(['Default name for session ',num2str(i,'%01d'),' : ',Session_name{i,1}]);
end
% Renaming session folders to standard ('1001','1002', etc.) and copying file in M1_PP_02_4Dto3D
for i=1:size(Session_name,1)
cd([path_to_M1_PP_01_OrigVol,'/',list(i).name])
name_orig=dir('*.nii');
name_orig(ismember({name_orig.name},{'.','..','.DS_Store'}))=[];
if size(name_orig,1)==1
copyfile([path_to_M1_PP_01_OrigVol,'/',list(i).name,'/',name_orig.name],[path_to_M1_PP_02_4Dto3D,'/',Session_name{i,1},'.nii'])
else
spm_input(['Error: More than one .nii detected in folder (',list(i).name,') . Inside each folder there must be only one Nifti file. Check the name_orig variable and rerun the code. '],'-1','bd','Yes');
disp('More than one .nii detected in folder. Inside each folder there must be only one Nifti file. See the name_orig variable.');
return
end
disp(['Naming for Session ',Session_name{i,1},' completed!!'])
end
%% Converting session from 4D to 3D; From M1_PP_01_OrigVol to M1_PP_02_4Dto3D
disp('Converting session from 4D to 3D using SPM:')
for i=1:size(Session_name,1)
cd(path_to_M1_PP_02_4Dto3D)
mkdir(Session_name{i,1})
end
% 4D to 3D using SPM:
clear matlabbatch
for i=1:size(Session_name,1)
input=[path_to_M1_PP_02_4Dto3D,'/',Session_name{i,1},'.nii'];
output=[path_to_M1_PP_02_4Dto3D,'/',Session_name{i,1}];
matlabbatch{1}.spm.util.split.vol = {input};
matlabbatch{1}.spm.util.split.outdir = {output};
spm_jobman('run',matlabbatch);
disp(['Session ',Session_name{i,1},' 4D to 3D conversion completed!!'])
end
% Number of volume for session:
for i=1:size(Session_name,1)
clear list_tmp
list_tmp=length(dir([path_to_M1_PP_02_4Dto3D,'/',Session_name{i,1},'/',Session_name{i,1},'*.nii']));
vol4session_tmp(i)=list_tmp;
vol4session=min(vol4session_tmp);
end
% From M1_PP_02_4Dto3D to M1_PP_03_Reorient
disp('Copying files from M1_PP_02_4Dto3D to M1_PP_03_Reorient')
for i=1:size(Session_name,1)
from=[path_to_M1_PP_02_4Dto3D,'/',Session_name{i,1}];
to=[path_to_M1_PP_03_Reorient,'/',Session_name{i,1}];
copyfile(from,to)
end
%Reference volume selection for each session
cd(path_to_M1_PP_03_Reorient)
file2show = cell(6,1);
REF_vol=cell(1,size(Session_name,1));
for i=1:size(Session_name,1)
clear input file2show
for j=1:6
%file2show{j,1}=[path_to_M1_PP_03_Reorient,'/',Session_name{i,1},'/',Session_name{i,1},'_',num2str(j*5+(j-1)*5,'%05d'),'.nii'];
file2show{j,1}=[path_to_M1_PP_03_Reorient,'/',Session_name{i,1},'/',Session_name{i,1},'_',num2str(((floor(vol4session/6)*j)),'%05d'),'.nii'];
answ_tmp(j)=((floor(vol4session/6)*j));
end
spm_check_registration(file2show{1,:},file2show{2,:},file2show{3,:},file2show{4,:},file2show{5,:},file2show{6,:});
answ={[num2str(answ_tmp(1)),'|',num2str(answ_tmp(2)),'|',num2str(answ_tmp(3)),'|',num2str(answ_tmp(4)),'|',num2str(answ_tmp(5)),'|',num2str(answ_tmp(6))]};
promt=['Choose the Reference for session ',Session_name{i,1}, ' : '];
spm_input(['This step is one of the most important! Six images will be displayed per session: ',Session_name{i,1},'. 1) Choose a reference image; 2) Reorient the chosen reference volume *be sure to set the origin at the level of Anterior Commissure* (See Manual); 3) Apply the transformation parameters to all volumes of the current session.'],'-1','bd','Yes');
REF_vol1=spm_input(promt,'-1','m',answ,[answ_tmp(1),answ_tmp(2),answ_tmp(3),answ_tmp(4),answ_tmp(5),answ_tmp(6)]);
REF_vol(i)={num2str(REF_vol1,'%05d')};
spm_image('Display',[path_to_M1_PP_03_Reorient,'/',Session_name{i,1},'/',Session_name{i,1},'_',REF_vol{1,i},'.nii']);
if i== size(Session_name,1)
try
spm_input('Reorienting Ref. image:','-1','b','Continue|--',[1,0],1);
catch
end
else
try
spm_input('Reorient and press continue::','-1','b','Continue|--',[1,0],1);
catch
end
end
end
%Check Reference volume new orientation for each session
for i=1:size(Session_name,1)
clear input file2show answ_tmp
for j=1:6
%file2show{j,1}=[path_to_M1_PP_03_Reorient,'/',Session_name{i,1},'/',Session_name{i,1},'_',num2str(j*5+(j-1)*5,'%05d'),'.nii'];
file2show{j,1}=[path_to_M1_PP_03_Reorient,'/',Session_name{i,1},'/',Session_name{i,1},'_',num2str(((floor(vol4session/6)*j)),'%05d'),'.nii'];
end
spm_check_registration(file2show{1,:},file2show{2,:},file2show{3,:},file2show{4,:},file2show{5,:},file2show{6,:});
if i==1
spm_input(['These are the reoriented images. Here you have the chance to see and, if needed, to return to the reference image orientation section for the session: ',Session_name{i,1}],'-1','bd','Yes');
end
Promt='Check reoriented images:';
m = spm_input(Promt,'-1','Continue|Reorient',['Y','N']);
if m=='Y'
continue
else
clear input file2show
for j=1:6
%file2show{j,1}=[path_to_M1_PP_03_Reorient,'/',Session_name{i,1},'/',Session_name{i,1},'_',num2str(j*5+(j-1)*5,'%05d'),'.nii'];
file2show{j,1}=[path_to_M1_PP_03_Reorient,'/',Session_name{i,1},'/',Session_name{i,1},'_',num2str(((floor(vol4session/6)*j)),'%05d'),'.nii'];
answ_tmp(j)=((floor(vol4session/6)*j));
end
answ={[num2str(answ_tmp(1)),'|',num2str(answ_tmp(2)),'|',num2str(answ_tmp(3)),'|',num2str(answ_tmp(4)),'|',num2str(answ_tmp(5)),'|',num2str(answ_tmp(6))]};
spm_check_registration(file2show{1,:},file2show{2,:},file2show{3,:},file2show{4,:},file2show{5,:},file2show{6,:});
promt=['Choose the Reference for session ',Session_name{i,1}, ' : '];
REF_vol1=spm_input(promt,'-1','m',answ,[answ_tmp(1),answ_tmp(2),answ_tmp(3),answ_tmp(4),answ_tmp(5),answ_tmp(6)]);
REF_vol(i)={num2str(REF_vol1,'%05d')};
spm_image('Display',[path_to_M1_PP_03_Reorient,'/',Session_name{i,1},'/',Session_name{i,1},'_',REF_vol{1,i},'.nii']);
if i== size(Session_name,1)
try
spm_input('Reorienting Ref. image:','-1','b','Continue|--',[1,0],1);
catch
end
else
try
spm_input('Reorient and press continue::','-1','b','Continue|--',[1,0],1);
catch
end
end
end
end
%Copying files from M1_PP_03_Reorient to M1_PP_04_Rename
disp('Copying files from M1_PP_03_Reorient to M1_PP_04_Rename')
for i=1:size(Session_name,1)
disp(['Copying files from M1_PP_03_Reorient to M1_PP_04_Rename for session: ',Session_name{i,1}])
from=[path_to_M1_PP_03_Reorient,'/',Session_name{i,1}];
to=[path_to_M1_PP_04_Rename,'/',Session_name{i,1}];
copyfile(from,to)
end
disp('Copying files from M1_PP_03_Reorient to M1_PP_04_Rename Completed')
% Placing reference Volume of each session at first place
for i=1:size(Session_name,1)
f = [path_to_M1_PP_04_Rename,'/',Session_name{i,1}];
cd(f)
ref_path = [path_to_M1_PP_04_Rename,'/',Session_name{i,1},'/',Session_name{i,1},'_',REF_vol{1,i},'.nii'];
new_name=[path_to_M1_PP_04_Rename,'/',Session_name{i,1},'/',Session_name{i,1},'_00000.nii'];
movefile(ref_path,new_name); % chiamo la ref 00015.nii come 'Ref.nii'
files = dir('*.nii');
ii=1;
for id = 1:length(files)
num=[Session_name{i,1},'_',num2str(id,'%03d'),'.nii'];
movefile([path_to_M1_PP_04_Rename,'/',Session_name{i,1},'/',files(ii).name],[path_to_M1_PP_04_Rename,'/',Session_name{i,1},'/',num]);
ii=ii+1;
end
clear files
disp(['Reference Volume: ',REF_vol{1,i}, ' was placed at the beginning of the Session: ', Session_name{i,1},'. Completed!!'])
end
% Saving Variables of module 1:
cd(path_general)
save('Variables_M1','REF_vol','Session_name','vol4session')
diary off
disp('End of Module 1!')
spm_input('Orientation of all of the Fetal Functional images completed! Proceed with 1st-pass Masking, Realignment and 1st-pass Scrubbing procedure in module 2.','-1','bd','Ok!');
%% Module 2
disp('---------------------------------------------------------------------------------')
disp('---------------------------------------------------------------------------------')
disp('|||\\\\///||| //°°°\\ |||\\ ||| ||| ||| ||||||| //-\\ ')
disp('||| \\\///||| // \\ ||| \\ ||| ||| ||| ||| // // ')
disp('||| \\// ||| (( )) ||| || ||| ||| ||| ||||||| // ')
disp('||| \/ ||| \\ // ||| // ||| ||| ||| ||| // ')
disp('||| ||| \\___// |||// ||||||||||| ||||||| ||||||| //___ ')
disp('---------------------------------------------------------------------------------')
disp('------------------ Functional Magnetic Resonance Imaging fMRI -------------------')
disp('-------------------------- Within Session Processing ----------------------------')
disp('---From 1-pass Masking with session-specific reoriented fetal-specific mask -----')
disp('-------------to Realignment and 1st pass Scrubbing procedure with ART------------')
disp(' . . . . . . . . . ')
disp(' ./ ( ( ) . ).( )).\. ')
disp(' .) ) ) ( . ( . ) ( ..\. ')
disp(' .() ( ( ) . ) . ( )) ( | ')
disp(' .(( ) ) ) ( . ( ..) ()__/ ')
disp(' .\( / / . ) _)_)_______/ ')
disp(' .( ( ( (__/__/ . ')
disp(' \._)_). ) ')
disp(' ._)_)./ ')
disp(' ._)_)./ ')
disp(' ')
% Copying files inside fMRI masking and creating folders for templates realignement (Session_name_template and Session_name_template_anp)
disp('Copying files from M1_PP_04_Rename to M2_WS_01_Mask.')
for i=1:size(Session_name,1)
from=[path_to_M1_PP_04_Rename,'/',Session_name{i,1},'/*.nii'];
to=[path_to_M2_WS_01_Mask,'/',Session_name{i,1}];
copyfile(from,to)
cd(to)
mkdir([Session_name{i,1},'_template'])
mkdir([Session_name{i,1},'template_anp'])
end
% Creating and centering all template for each session:
cd(path_general)
diary Module2_logfile
spm_input(['Creating session-specific template mask in the user image space for each session (voxel resolution, image dimensions and orientation). Template creation will take approximately ',num2str((((size(Session_name,1)*110))*2)/60),' minutes (for 1.5T).'],'-1','bd','Yes');
cd(path_general)
Create_Template(path_original_tmp,path_to_M1_PP_04_Rename,path_to_M2_WS_01_Mask,Session_name);
% Template Translation
num_template=17;
for i=1:size(Session_name,1)
disp(['Template mask translation for Session: ',Session_name{i,1}])
%Calculating origin for reference Volume:
VV=spm_vol([path_to_M2_WS_01_Mask,'/',Session_name{i,1},'/',Session_name{i,1},'_001.nii']);
Sess_origin = VV.mat\[0 0 0 1]';
for j=1:num_template
%Calculating origin for each template:
clear input output option cmd matlabbatch cmdout_tmp
V=spm_vol([path_to_M2_WS_01_Mask,'/',Session_name{i,1},'/',Session_name{i,1},'_template/scGW_',num2str(j+20),'.nii']);
temp_origin = V.mat\[0 0 0 1]';
% Calculating origins differences:
x_diff=(Sess_origin(1,1) - temp_origin(1,1));
y_diff=(Sess_origin(2,1) - temp_origin(2,1));
z_diff=(Sess_origin(3,1) - temp_origin(3,1));
%Translating original templates:
matlabbatch{1}.spm.util.reorient.srcfiles = {[path_to_M2_WS_01_Mask,'/',Session_name{i,1},'/',Session_name{i,1},'_template/scGW_',num2str(j+20),'.nii']};
matlabbatch{1}.spm.util.reorient.transform.transM = [1 0 0 x_diff
0 1 0 y_diff
0 0 1 z_diff
0 0 0 1];
matlabbatch{1}.spm.util.reorient.prefix = Session_name{i,1};
spm_jobman('run',matlabbatch);
%Translating smoothed templates:
for jjj=1:3
matlabbatch{1}.spm.util.reorient.srcfiles = {[path_to_M2_WS_01_Mask,'/',Session_name{i,1},'/',Session_name{i,1},'template_anp/banp_',num2str(jjj),'_',num2str(j+20),'.nii']};
matlabbatch{1}.spm.util.reorient.transform.transM = [1 0 0 x_diff
0 1 0 y_diff
0 0 1 z_diff
0 0 0 1];
matlabbatch{1}.spm.util.reorient.prefix = [Session_name{i,1}];
spm_jobman('run',matlabbatch);
end
end
cd([path_to_M2_WS_01_Mask,'/',Session_name{i,1},'/',Session_name{i,1},'_template'])
delete s*
cd([path_to_M2_WS_01_Mask,'/',Session_name{i,1},'/',Session_name{i,1},'template_anp'])
delete ban*
disp(['Template masks translation for Session ',Session_name{i,1},' Completed!'])
end
% User insert Gestational Week and choose one Template:
template2show = cell(1);
clear input
k=1;
for j=21:37
template2show{k,1}=[path_to_M2_WS_01_Mask,'/',Session_name{1,1},'/',Session_name{1,1},'_template/',Session_name{1,1},'scGW_',num2str(j),'.nii'];
k=k+1;
end
ref2show=[path_to_M2_WS_01_Mask,'/',Session_name{1,1},'/',Session_name{1,1},'_001.nii'];
spm_check_registration(ref2show,template2show{:,:});
% User select one Template:
spm_input(['The chosen session-specific template mask should include the entire fetal brain and a limited quantity of the surrounding anatomical structures (black edges). Please use the zoom function for a more accurate choice.'],'-1','bd','Yes');
GW2beused=spm_input('Choose the session-specific template mask:','-1','m','21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37',[21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37],30);
% First Template choice
clear input template2show ref2show temp2show
template2show = cell(1);
for j=1:3
template2show{j,1}=[path_to_M2_WS_01_Mask,'/',Session_name{1,1},'/',Session_name{1,1},'template_anp/',Session_name{1,1},'banp_',num2str(j),'_',num2str(GW2beused),'.nii'];
end
ref2show=[path_to_M2_WS_01_Mask,'/',Session_name{1,1},'/',Session_name{1,1},'_001.nii'];
temp2show=[path_to_M2_WS_01_Mask,'/',Session_name{1,1},'/',Session_name{1,1},'_template/',Session_name{1,1},'scGW_',num2str(GW2beused),'.nii'];
spm_check_registration(ref2show,temp2show,template2show{:,:});
% Final Template choice
spm_input('For the session-specific template mask you have chosen, select the one that covers the whole brain in each direction (es. L/R, A/P; I/S).','-1','bd','Ok!');
template2bused=spm_input('Insert a number between 1 and 4: ','-1','m','1|2|3|4',[1,2,3,4]);
% Applying the session-specific template mask (1-pass Masking):
disp('Masking images with the chosen session-specific template mask (1-pass Masking): ');
clear matlabbatch list_to_mask
for i=1:size(Session_name,1)
cd([path_to_M2_WS_01_Mask,'/',Session_name{i,1}]);
list_to_mask=dir('*.nii');
for id=1:length(list_to_mask)
clear matlabbatch list_to_mask
matlabbatch{1}.spm.util.imcalc.input{1,1} = char(strcat(path_to_M2_WS_01_Mask,'/',Session_name{i,1},'/',Session_name{i,1},'_',num2str(id,'%03d'),'.nii,1'));
if template2bused==1
matlabbatch{1}.spm.util.imcalc.input{2,1} = char(strcat(path_to_M2_WS_01_Mask,'/',Session_name{i,1},'/',Session_name{i,1},'_template/',Session_name{i,1},'scGW_',num2str(GW2beused),'.nii,1'));
else
matlabbatch{1}.spm.util.imcalc.input{2,1} = char(strcat(path_to_M2_WS_01_Mask,'/',Session_name{i,1},'/',Session_name{i,1},'template_anp/',Session_name{i,1},'banp_',num2str(template2bused-1),'_',num2str(GW2beused),'.nii,1'));
end
matlabbatch{1}.spm.util.imcalc.output = char(strcat('m',Session_name{i,1},'_', num2str(id,'%03d'), '.nii'));
matlabbatch{1}.spm.util.imcalc.outdir = {strcat(path_to_M2_WS_01_Mask,'/',Session_name{i,1})};
matlabbatch{1}.spm.util.imcalc.expression = 'i1.*i2';
matlabbatch{1}.spm.util.imcalc.options.dmtx = 0;
matlabbatch{1}.spm.util.imcalc.options.mask = 0;
matlabbatch{1}.spm.util.imcalc.options.interp = 0;
matlabbatch{1}.spm.util.imcalc.options.dtype = 4;
spm_jobman('run',matlabbatch);
end
tmp=dir([Session_name{i,1},'*nii']);
delete(tmp.name);
clear tmp
end
% 1-pass Masking review:
for i=1:size(Session_name,1)
clear input ref2show file2show temp2beshow
for j=1:6
%file2show{j,1}=[path_to_M2_WS_01_Mask,'/',Session_name{i,1},'/m',Session_name{i,1},'_',num2str(j*5+(j-1)*5,'%03d'),'.nii'];
file2show{j,1}=[path_to_M2_WS_01_Mask,'/',Session_name{i,1},'/m',Session_name{i,1},'_',num2str(((floor(vol4session/6)*j)),'%03d'),'.nii'];
end
ref2show=[path_to_M2_WS_01_Mask,'/',Session_name{i,1},'/m',Session_name{i,1},'_001.nii'];
nomask2show=[path_to_M1_PP_04_Rename,'/',Session_name{i,1},'/',Session_name{i,1},'_001.nii'];
if template2bused==1
temp2beshow=char(strcat(path_to_M2_WS_01_Mask,'/',Session_name{i,1},'/',Session_name{i,1},'_template/',Session_name{i,1},'scGW_',num2str(GW2beused),'.nii,1'));
else
temp2beshow= char(strcat(path_to_M2_WS_01_Mask,'/',Session_name{i,1},'/',Session_name{i,1},'template_anp/',Session_name{i,1},'banp_',num2str(template2bused-1),'_',num2str(GW2beused),'.nii,1'));
end
spm_check_registration(ref2show,nomask2show,temp2beshow,file2show{:,:});
if i<size(Session_name,1)
spm_input('1st-pass mask review:','-1','b','Continue|--',[1,0]);
else
spm_input('1st-pass mask review:','-1','b','Proceed|--',[1,0]);
end
end
% Copying files from 0M2_WS_01_Mask to M2_WS_02_Realign
disp('Copying files from M2_WS_01_Mask to M2_WS_02_Realign.')
for i=1:size(Session_name,1)
from=[path_to_M2_WS_01_Mask,'/',Session_name{i,1},'/m*'];
to=[path_to_M2_WS_02_Realign,'/',Session_name{i,1}];
copyfile(from,to)
end
% Realignment Within Session to first volume(all volumes to their reference):
clear answ_tmp answ
whg2show=cell(GW2beused-21,1);
k=1;
clear answ
for j=21:GW2beused
whg2show{k,1}=[path_to_M2_WS_01_Mask,'/',Session_name{1,1},'/',Session_name{1,1},'_template/',Session_name{1,1},'scGW_',num2str(j),'.nii'];
answ_tmp(k)=j;
k=k+1;
end
ref2show=[path_to_M2_WS_02_Realign,'/',Session_name{1,1},'/m',Session_name{1,1},'_001.nii'];
spm_check_registration(ref2show,whg2show{:,:});
answw='';
for hh=1:length(answ_tmp)
answ=num2str(answ_tmp(hh));
if hh<length(answ_tmp)
answw=[answw,answ,'|'];
else
answw=[answw,answ];
end
end
% The user select the Tissue-Weighting mask:
spm_input('Now you should select the Tissue-Weighting mask that will be used to calculate Within Session motion parameters in the Realignment algorithm. Therefore it should completely fit within the brain. ','-1','bd','Ok!');
wgh_winner=spm_input('Choose the Tissue-Weighting mask: ','-1','m',answw,answ_tmp);
wgh_winner=num2str(wgh_winner);
wgh_winner=cellstr(wgh_winner);
clear matlabbatch list list_tmpp path_tmp list_tmpp lista_volumi path_folder_tmp
for ii=1:size(Session_name,1)
clear matlabbatch list
path_folder_tmp=[path_to_M2_WS_02_Realign,'/',Session_name{ii,1},'/']; %Prima cartella
cd(path_folder_tmp);
lista_volumi=dir('*.nii');u=0;% lista volumi nella cartella
for j=1:length(lista_volumi)
TTFF = length(regexpi(lista_volumi(j).name,'nii'));
if TTFF
u=u+1;
list_tmpp(u,:)=lista_volumi(j).name;
end
end
list=cellstr(list_tmpp);
for iii=1:length(list)
path_folder_ttmp(iii,:)=[path_folder_tmp,list(iii)];% Creating path for each volume
path_tmp(iii,:)=[path_folder_ttmp{iii,:}];
end
path=cellstr(path_tmp); %All session volumes for realignment
% Realign: Estimate
clear matlabbatch
matlabbatch{1}.spm.spatial.realign.estwrite.data = {path}';
matlabbatch{1}.spm.spatial.realign.estwrite.eoptions.quality = 0.9;
matlabbatch{1}.spm.spatial.realign.estwrite.eoptions.sep = 4;
matlabbatch{1}.spm.spatial.realign.estwrite.eoptions.fwhm = 5;
matlabbatch{1}.spm.spatial.realign.estwrite.eoptions.rtm = 0;
matlabbatch{1}.spm.spatial.realign.estwrite.eoptions.interp = 4;
matlabbatch{1}.spm.spatial.realign.estwrite.eoptions.wrap = [0 0 0];
matlabbatch{1}.spm.spatial.realign.estwrite.eoptions.weight = {[path_to_M2_WS_01_Mask,'/',Session_name{ii,1},'/',Session_name{ii,1},'_template/',Session_name{ii,1},'scGW_',wgh_winner{1,1},'.nii,1']};
matlabbatch{1}.spm.spatial.realign.estwrite.roptions.which = [0 1];
matlabbatch{1}.spm.spatial.realign.estwrite.roptions.interp = 4;
matlabbatch{1}.spm.spatial.realign.estwrite.roptions.wrap = [0 0 0];
matlabbatch{1}.spm.spatial.realign.estwrite.roptions.mask = 1;
matlabbatch{1}.spm.spatial.realign.estwrite.roptions.prefix = 'r1';
spm_jobman('run',matlabbatch);
clear list path_tmp list_tmpp lista_volumi path_folder_tmp
disp(['Realignment of ',Session_name{ii,1},' volumes to reference volume completed!!'])
end
% Renaming (r1 prefix):
for i=1:size(Session_name,1)
cd([path_to_M2_WS_02_Realign,'/',Session_name{i,1}]);
list=dir(['m',Session_name{i,1},'*']);
for ii=1:size(list,1)
current_name=list(ii).name;
movefile([path_to_M2_WS_02_Realign,'/',Session_name{i,1},'/',current_name],[path_to_M2_WS_02_Realign,'/',Session_name{i,1},'/r1',current_name]);
end
end
spm_input('Within each Session folder found in the M2_WS_02_Realign folder you can find the: 1) output of each realigned volume with -r1- as the prefix; 2) mean volume calculated from the average of all session volumes; 3) a txt file (rp*.txt) and a ps file (*.ps) reporting translation and rotation motion parameters and charts of each volume with respect to the reference image. ','-1','bd','Ok!');
% Copying files from M2_WS_02_Realign to M2_WS_03_Scrub
disp('Copy from M2_WS_02_Realign to M2_WS_03_Scrub!!')
for i=1:size(Session_name,1)
from=[path_to_M2_WS_02_Realign,'/',Session_name{i,1}];
to=[path_to_M2_WS_03_Scrub,'/',Session_name{i,1}];
copyfile(from,to)
cd(to)
delete('*ps')
end
disp('Copying from M2_WS_02_Realign to M2_WS_03_Scrub Completed!!')
% ART
disp('1st-pass scrubbing procedure with Artifact Detection Tool is running:')
%Configuring text file:
for i=1:size(Session_name,1)
cd(path_to_cffg_file)
fid = fopen('File_1Srub.cfg','rt');
X = fread(fid) ;
fclose(fid) ;
X = char(X.') ;
% (replacing '9999' string with correct session name)
folder_out=path_to_M2_WS_03_Scrub;
if i==1
Y = strrep(X, '9999', Session_name{i,1});
else
Y = strrep(X, Session_name{i-1,1}, Session_name{i,1});
end
fid2 = fopen('File_1Srub.cfg','wt'); %sovrascrivo il file ogni volta;
fwrite(fid2,Y) ;
fclose (fid2) ;
disp(['ART for ', Session_name{i,1},' is Running!!'])
cd(path_to_cffg_file)
art('sess_file','File_1Srub.cfg');
disp(['ART for ', Session_name{i,1},' Completed!!'])
end
% Restoring text file.
cd(path_to_cffg_file)
fid = fopen('File_1Srub.cfg','rt');
X = fread(fid);
fclose(fid);
X = char(X.');
Y = strrep(X,Session_name{end,1}, '9999');
fid2 = fopen('File_1Srub.cfg','wt');%sovrascrivo il file ogni volta;
fwrite(fid2,Y);
fclose (fid2);
disp('ART for all sessions Completed!!')
spm_input('Check the Artifact Detection Tool outlier graphs. This inspection could help you in the next section where you will be ask to remove/keep outlier images. ','-1','bd','Ok!');
close all
%% From M2_WS_03_Scrub to M2_WS_04_Rename
disp('Copying files from M2_WS_03_Scrub to M2_WS_04_Rename and moving ART detected ouliers into a separate subfolder:')
for i=1:size(Session_name,1)
cd([path_to_M2_WS_03_Scrub,'/',Session_name{i,1}])
mkdir('scrubbed_volumes');
load('art_regression_outliers.mat')
index = and(any(R == 1, 2), any(R == 1, 2));
outliers=find(index); %outliers variable contains scrubbed volumes for ART
%Keeping first volume if scrubbed
if sum(outliers==1)==1
outliers(1)=[];
disp('Reference Volume was detected as an outliers, but it was kept.');
end
list_totale=dir('r1*');
disp(['Deleting volumes number ',num2str(outliers') ,' from Session ',Session_name{i,1},'.'])
lista_to_delete=list_totale(outliers,:);
if size(lista_to_delete,1)==0
disp(['No detected outliers for session ',Session_name{i,1},'.'])
else
for jj=1:size(lista_to_delete,1)
from=([path_to_M2_WS_03_Scrub,'/',Session_name{i,1},'/',lista_to_delete(jj).name]);
to=([path_to_M2_WS_03_Scrub,'/',Session_name{i,1},'/scrubbed_volumes']);
movefile(from,to);
end
end
clear list_totale lista_to_delete outliers index R
end
disp('1-pass scrubbed procedure Completed!')
close all
%% Scrubbed volumes visualization and choice to remove/keep the volumes:
spm_input('SPM display window will show scrubbed volume(s) for each session. Instructions for removing detected outliers: ''Yes'' to scrub all of the volumes, ''No'' to keep all of the volumes or ''OnebyOne'' button to select the volume(s) you want to keep for the current session. ','-1','bd','Ok!');
clear ref2show
for i=1:size(Session_name,1)
ref2show{1,1}=[path_to_M2_WS_03_Scrub,'/',Session_name{i,1},'/r1m',Session_name{i,1},'_001.nii,1'];
clear img2show list
cd([path_to_M2_WS_03_Scrub,'/',Session_name{i,1},'/scrubbed_volumes'])
list=dir('*nii');
if size(list,1)>=23
part_disp=floor(size(list,1)/9);
disp(['Scrubbed volumes for session ',Session_name{i,1},' will be splitted in ',part_disp, ' Display.'])
resto = mod(size(list,1),9);
if resto==0
%part_disp=part_disp;
else
part_disp=part_disp+1;
end
for d=1:part_disp
if (resto~=0 && d==part_disp)
clear list_current
for nnn=1:resto
list_current{nnn,1}=list((9*(d-1)+1)+(nnn-1)).name;
end
else
clear list_current
for nnn=1:9
list_current{nnn,1}=list((9*(d-1)+1)+(nnn-1)).name;
end
end
clear img2show
for j=1:size(list_current,1)
img2show{j,1}=[path_to_M2_WS_03_Scrub,'/',Session_name{i,1},'/scrubbed_volumes/', list_current{j,1},',1'];%mask
end
spm_check_registration(ref2show{1,1},img2show{:,:});
m=spm_input(['Scrub for Session ',Session_name{i,1}],'-1','b','Yes|No|OnebyOne',['Y','N','o']); m=char(m);
%m=input(['Do you want to scrubb all volumes for session ',Session_name{i,1},'? Press Y to scrubb all, Press N to keep all or any button to select volumes you want to keep: '],'s');
if m=='Y'
continue
elseif m=='N'
for jj=1:size(list_current,1)
movefile([path_to_M2_WS_03_Scrub,'/',Session_name{i,1},'/scrubbed_volumes/', list_current{jj,1}],[path_to_M2_WS_03_Scrub,'/',Session_name{i,1},'/', list_current{jj,1}]);
end
else
for jjj=1:size(list_current,1)
%prmt=['Do you want to keep ',list_current{jjj,1} ,' volume? (Press 1 to keep, 0 to scrub): '];
spm_check_registration(ref2show{1,1},img2show{jjj,:});
takebackscrub(jjj)=spm_input(['Keep ',list(jj).name ,'?'],'-1','y/n',[1,0]);
if takebackscrub(jjj)
movefile([path_to_M2_WS_03_Scrub,'/',Session_name{i,1},'/scrubbed_volumes/',list_current{jjj,1}],[path_to_M2_WS_03_Scrub,'/',Session_name{i,1},'/',list_current{jjj,1}]);
end
end
end
end
elseif size(list,1)==0
continue
else
clear img2show
for j=1:size(list,1)
img2show{j,1}=[path_to_M2_WS_03_Scrub,'/',Session_name{i,1},'/scrubbed_volumes/', list(j).name,',1'];%mask
end
spm_check_registration(ref2show{1,1},img2show{:,:});
m=spm_input(['Scrub for Session ',Session_name{i,1}],'-1','b','Yes|No|OnebyOne',['Y','N','o']); m=char(m);
if m=='Y'
continue
elseif m=='N'
for jj=1:size(list,1)
movefile([path_to_M2_WS_03_Scrub,'/',Session_name{i,1},'/scrubbed_volumes/', list(jj).name],[path_to_M2_WS_03_Scrub,'/',Session_name{i,1},'/', list(jj).name]);
end
else
for jj=1:size(list,1)
prmt=['Do you want to keep ',list(jj).name ,' volume? (Press 1 to keep, 0 to scrub): '];
spm_check_registration(ref2show{1,1},img2show{jj,:});
takebackscrub(jj)=spm_input(['Keep ',list(jj).name ,'?'],'-1','y/n',[1,0]);
if takebackscrub(jj)
movefile([path_to_M2_WS_03_Scrub,'/',Session_name{i,1},'/scrubbed_volumes/',list(jj).name],[path_to_M2_WS_03_Scrub,'/',Session_name{i,1},'/',list(jj).name]);
end
end
end
end
end
%Eliminating session if number of volumes is less than 1/3
flag=zeros(size(Session_name,1),1);
spm_input('Sessions with less than 2/3 of the initial number of volumes will be deleted due to excessive Within Session movement.','-1','bd','Ok!');
disp('Sessions with less than 2/3 of the initial number of volumes will be deleted due to excessive Within Session movement.');
for i=1:size(Session_name,1)
check_volumes=dir([path_to_M2_WS_03_Scrub,'/',Session_name{i,1},'/scrubbed_volumes/*nii']);
if length(check_volumes)>=floor(vol4session*2/3)
flag(i)=0;
disp(['Session ',Session_name{i,1},' was deleted.'])
else
flag(i)=1;
disp(['Session ',Session_name{i,1},' was kept.'])
end
end
Session_name=Session_name(logical(flag));
spm_input(['Remaining session(s) are: ', num2str(length(Session_name)),' out of ',num2str(length(flag)),'.'],'-1','bd','Ok!');
%% Rename files inside M2_WS_04_Rename:
disp('Renaming each session after 1-pass scrubbing procedure:')
cd(path_to_M2_WS_04_Rename)
for i=1:size(Session_name,1)
mkdir(Session_name{i,1})
end
for i=1:size(Session_name,1)
cd([path_to_M2_WS_03_Scrub,'/',Session_name{i,1}]);
files = dir('r1*');
ii=1;
for id = 1:length(files)
num=['r1m',Session_name{i,1},'_',num2str(id,'%03d'),'.nii'];
copyfile([path_to_M2_WS_03_Scrub,'/',Session_name{i,1},'/',files(ii).name],[path_to_M2_WS_04_Rename,'/',Session_name{i,1},'/',num]);
ii=ii+1;
end
clear files
disp(['Renaming volumes of Session ',Session_name{i,1},', DONE!'])
end
close all
disp('Renaming Completed!')
disp('You have COMLETED MODULE 2!!')
cd(path_general)
save('Variables_M2.mat','template2bused','GW2beused','wgh_winner','Session_name','vol4session')
diary off
cd(path_to_M1_PP_01_OrigVol);
list=dir();
% Get vector to recognize folders only:
list(ismember({list.name},{'.','..'}))=[];
% if the number of session is >1 the proccessing will continue with standard procedure
% if only one session is present then the code will jump to M5 skipping M3 and M4.
if length(list)>1
spm_input('You have more than one session therefore you will proceed with the standard procedure.','-1','bd','Ok!');
%% MODULO 3:
disp('---------------------------------------------------------------------------------')
disp('---------------------------------------------------------------------------------')
disp('|||\\\\///||| //°°°\\ |||\\ ||| ||| ||| ||||||| //°// ')
disp('||| \\\///||| // \\ ||| \\ ||| ||| ||| ||| // ')
disp('||| \\// ||| (( )) ||| || ||| ||| ||| ||||||| \\ ')
disp('||| \/ ||| \\ // ||| // ||| ||| ||| ||| )) ')
disp('||| ||| \\___// |||// ||||||||||| ||||||| ||||||| __// ')
disp('---------------------------------------------------------------------------------')
disp('------------------ Functional Magnetic Resonance Imaging fMRI -------------------')
disp('-------------------------- Within Session Processing ---------------------------')
disp('----------From Session-specific Functional References Segmentation --------------')
disp('--------to Mean inner-brain Masked Functional References Calculation-------------')
disp(' . . . . . . . . . ')
disp(' ./ ( ( ) . ).( )).\. ')
disp(' .) ) ) ( . ( . ) ( ..\. ')
disp(' .() ( ( ) . ) . ( )) ( | ')
disp(' .(( ) ) ) ( . ( ..) ()__/ ')
disp(' .\( / / . ) _)_)_______/ ')
disp(' .( ( ( (__/__/ . ')
disp(' \._)_). ) ')
disp(' ._)_)./ ')
disp(' ._)_)./ ')
disp(' ')
% Creation of session folders inside M3_WS_01_SegRefVols for reference volume segmentations:
cd(path_to_M3_WS_01_SegRefVols);
for i=1:size(Session_name,1)
mkdir(Session_name{i,1})
end
disp('Insertion of each reference volume inside the respective folder:')
for i=1:size(Session_name,1)
from=[path_to_M2_WS_04_Rename,'/',Session_name{i,1},'/','r1m',Session_name{i,1},'_001.nii'];
to=[path_to_M3_WS_01_SegRefVols,'/',Session_name{i,1}];
copyfile(from,to)
disp(['Inserting reference of ',Session_name{i,1},' inside ',Session_name{i,1} ,' folder. Done!'])
end
%% Session-Specific Functional References Segmentation for inner-brain mask calculation:
% Option to remask images:
cd(path_general)
diary Module3_logfile
for i=1:size(Session_name,1)
clear temp2show file2show
disp(['These are the 1-pass session-specific masked images for session: ',Session_name{i,1}]);
% Reference of session i
file2show{1,1}=[path_to_M3_WS_01_SegRefVols,'/',Session_name{i,1},'/r1m',Session_name{i,1},'_001.nii'];
% Template:
k=1;
for j=21:GW2beused
temp2show{k,1}=[path_to_M2_WS_01_Mask,'/',Session_name{i,1},'/',Session_name{i,1},'_template/',Session_name{i,1},'scGW_',num2str(j),'.nii'];
answ_tmp(k)=j;
k=k+1;
end
spm_check_registration(file2show{1,1},temp2show{:,:});
answw='';
for hh=1:length(answ_tmp)
answ=num2str(answ_tmp(hh));
if hh<length(answ_tmp)
answw=[answw,answ,'|'];
else
answw=[answw,answ];
end
end
clear input
m=spm_input('Mask the Ref. image again?','-1','y/n',['N','Y']);m=char(m);
if m=='Y'
continue
else
if i==1
spm_input(['The chosen session-specific template mask should include the entire fetal brain and a minimal amount of the skull (black edges).'],'-1','bd','Yes');
end
GW_tmp=spm_input('Choose the session-specific template mask','-1','m',answw,answ_tmp,30);
% Specific choices
clear input template2show ref2show temp2show
for j=1:3
template2show{j,1}=[path_to_M2_WS_01_Mask,'/',Session_name{1,1},'/',Session_name{1,1},'template_anp/',Session_name{1,1},'banp_',num2str(j),'_',num2str(GW_tmp),'.nii'];
end
file2show=[path_to_M3_WS_01_SegRefVols,'/',Session_name{i,1},'/r1m',Session_name{i,1},'_001.nii'];
temp2show=[path_to_M2_WS_01_Mask,'/',Session_name{1,1},'/',Session_name{1,1},'_template/',Session_name{1,1},'scGW_',num2str(GW_tmp),'.nii'];
spm_check_registration(file2show,temp2show,template2show{:,:});
spm_input('For the mask you have chosen, select the one that covers the whole brain in each direction (es. L/R, A/P; I/S).','-1','bd','Ok!');
template2bused=spm_input('Insert a number between 1 and 4: ','-1','m','1|2|3|4',[1,2,3,4]);
%Applying mask:
cd([path_to_M3_WS_01_SegRefVols,'/',Session_name{i,1}]);
clear matlabbatch list_to_mask
matlabbatch{1}.spm.util.imcalc.input{1,1} = char(strcat(path_to_M3_WS_01_SegRefVols,'/',Session_name{i,1},'/r1m',Session_name{i,1},'_001.nii,1'));
if template2bused==1
matlabbatch{1}.spm.util.imcalc.input{2,1} = char(strcat(path_to_M2_WS_01_Mask,'/',Session_name{i,1},'/',Session_name{i,1},'_template/',Session_name{i,1},'scGW_',num2str(GW_tmp),'.nii,1'));
else
matlabbatch{1}.spm.util.imcalc.input{2,1} = char(strcat(path_to_M2_WS_01_Mask,'/',Session_name{i,1},'/',Session_name{i,1},'template_anp/',Session_name{i,1},'banp_',num2str(template2bused-1),'_',num2str(GW_tmp),'.nii,1'));
end
matlabbatch{1}.spm.util.imcalc.output = char(strcat('r1m',Session_name{i,1},'_001.nii'));
matlabbatch{1}.spm.util.imcalc.outdir = {strcat(path_to_M3_WS_01_SegRefVols,'/',Session_name{i,1})};
matlabbatch{1}.spm.util.imcalc.expression = 'i1.*i2';
matlabbatch{1}.spm.util.imcalc.options.dmtx = 0;
matlabbatch{1}.spm.util.imcalc.options.mask = 0;
matlabbatch{1}.spm.util.imcalc.options.interp = 0;
matlabbatch{1}.spm.util.imcalc.options.dtype = 4;
spm_jobman('run',matlabbatch);
GW_tmp=GW2beused;
end
end
% Segmentation and Gestational Week selection
spm_input(['In this section Session-Specific Functional References Segmentation of each reference image is processed. It is suggested to use the GW that was used in module 2: ',num2str(GW2beused)],'-1','bd','Ok!');
Decision_GW = spm_input(['Do you want to proceed with ',num2str(GW2beused),' GW for classes C1:C7 and inner and outer brain?'],'-1','bd','yes|no',[1,0]);
if Decision_GW==1
GW_winner=GW2beused;
GWin_out_winner=GW2beused;
else
GW_winner=spm_input('Select Gestation Week for classes C1:C7:','-1','m','21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37',[21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37],30);
GWin_out_winner=spm_input('Select Gestation Week for Inner/Outer brain:','-1','m','21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37',[21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37],30);
end
%Creation of path for masked references images:
disp('Creating Session-Specific Functional References masks.');
clear input path3D matlabbatch
input= cell(length(Session_name),1);
for i=1:size(Session_name,1)
input(i,:)=cellstr([path_to_M3_WS_01_SegRefVols,'/',Session_name{i,1},'/r1m',Session_name{i,1},'_001.nii,1']);% input images
end
matlabbatch{1}.spm.spatial.preproc.channel.vols = {
''
};
matlabbatch{1}.spm.spatial.preproc.channel.biasreg = 0.01;
matlabbatch{1}.spm.spatial.preproc.channel.biasfwhm = 60;
matlabbatch{1}.spm.spatial.preproc.channel.write = [1 1];
matlabbatch{1}.spm.spatial.preproc.tissue(1).tpm = {strcat(path_general,'Template/Template_Priors_Seg/34GW/04_Smoothing_Classi/sc1_34GW_CorticalPlate_Cerebellum_Gholipour_2017.nii,1')};
matlabbatch{1}.spm.spatial.preproc.tissue(1).ngaus = 1;
matlabbatch{1}.spm.spatial.preproc.tissue(1).native = [0 0];
matlabbatch{1}.spm.spatial.preproc.tissue(1).warped = [0 0];
matlabbatch{1}.spm.spatial.preproc.tissue(2).tpm = {strcat(path_general,'Template/Template_Priors_Seg/34GW/04_Smoothing_Classi/sc2_34GW_WM_Gholipour_2017.nii,1')};
matlabbatch{1}.spm.spatial.preproc.tissue(2).ngaus = 1;
matlabbatch{1}.spm.spatial.preproc.tissue(2).native = [0 0];
matlabbatch{1}.spm.spatial.preproc.tissue(2).warped = [0 0];
matlabbatch{1}.spm.spatial.preproc.tissue(3).tpm = {strcat(path_general,'Template/Template_Priors_Seg/34GW/04_Smoothing_Classi/sc3_34GW_CSF_Gholipour_2017.nii,1')};
matlabbatch{1}.spm.spatial.preproc.tissue(3).ngaus = 2;
matlabbatch{1}.spm.spatial.preproc.tissue(3).native = [0 0];
matlabbatch{1}.spm.spatial.preproc.tissue(3).warped = [0 0];
matlabbatch{1}.spm.spatial.preproc.tissue(4).tpm = {strcat(path_general,'Template/Template_Priors_Seg/34GW/04_Smoothing_Classi/sc4_34GW_DGM_Gholipour_2017.nii,1')};
matlabbatch{1}.spm.spatial.preproc.tissue(4).ngaus = 1;
matlabbatch{1}.spm.spatial.preproc.tissue(4).native = [0 0];
matlabbatch{1}.spm.spatial.preproc.tissue(4).warped = [0 0];
matlabbatch{1}.spm.spatial.preproc.tissue(5).tpm = {strcat(path_general,'Template/Template_Priors_Seg/34GW/04_Smoothing_Classi/sc5_34GW_Hippocampi_Gholipour_2017.nii,1')};
matlabbatch{1}.spm.spatial.preproc.tissue(5).ngaus = 1;
matlabbatch{1}.spm.spatial.preproc.tissue(5).native = [0 0];
matlabbatch{1}.spm.spatial.preproc.tissue(5).warped = [0 0];
matlabbatch{1}.spm.spatial.preproc.tissue(6).tpm = {strcat(path_general,'Template/Template_Priors_Seg/34GW/04_Smoothing_Classi/sc6_34GW_Amygdala_Gholipour_2017.nii,1')};
matlabbatch{1}.spm.spatial.preproc.tissue(6).ngaus = 1;
matlabbatch{1}.spm.spatial.preproc.tissue(6).native = [0 0];
matlabbatch{1}.spm.spatial.preproc.tissue(6).warped = [0 0];
matlabbatch{1}.spm.spatial.preproc.tissue(7).tpm = {strcat(path_general,'Template/Template_Priors_Seg/34GW/04_Smoothing_Classi/sc7_34GW_Brainstem_Gholipour_2017.nii,1')};
matlabbatch{1}.spm.spatial.preproc.tissue(7).ngaus = 1;
matlabbatch{1}.spm.spatial.preproc.tissue(7).native = [0 0];
matlabbatch{1}.spm.spatial.preproc.tissue(7).warped = [0 0];
matlabbatch{1}.spm.spatial.preproc.tissue(8).tpm = {strcat(path_general,'Template/Template_Priors_Seg/Inbrains_and_Outbrains/34GW/src8_InnerBrain_34GW.nii,1')};
matlabbatch{1}.spm.spatial.preproc.tissue(8).ngaus = 2;
matlabbatch{1}.spm.spatial.preproc.tissue(8).native = [1 1];
matlabbatch{1}.spm.spatial.preproc.tissue(8).warped = [0 0];
matlabbatch{1}.spm.spatial.preproc.tissue(9).tpm = {strcat(path_general,'Template/Template_Priors_Seg/Inbrains_and_Outbrains/34GW/src9_OuterBrain_34GW.nii,1')};
matlabbatch{1}.spm.spatial.preproc.tissue(9).ngaus = 1;
matlabbatch{1}.spm.spatial.preproc.tissue(9).native = [1 1];
matlabbatch{1}.spm.spatial.preproc.tissue(9).warped = [0 0];
matlabbatch{1}.spm.spatial.preproc.warp.mrf = 1;
matlabbatch{1}.spm.spatial.preproc.warp.cleanup = 2;
matlabbatch{1}.spm.spatial.preproc.warp.reg = [0 0.001 0.5 0.05 0.2];
matlabbatch{1}.spm.spatial.preproc.warp.affreg = 'none';
matlabbatch{1}.spm.spatial.preproc.warp.fwhm = 0;
matlabbatch{1}.spm.spatial.preproc.warp.samp = 2;
matlabbatch{1}.spm.spatial.preproc.warp.write = [1 1];
matlabbatch{1}.spm.spatial.preproc.warp.vox = NaN;
matlabbatch{1}.spm.spatial.preproc.warp.bb = [NaN NaN NaN
NaN NaN NaN];