-
Notifications
You must be signed in to change notification settings - Fork 1
/
FG_AAL_90areas_TC_extract_DPARSFA_based.m
158 lines (127 loc) · 5.4 KB
/
FG_AAL_90areas_TC_extract_DPARSFA_based.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
function FG_AAL_90areas_TC_extract_DPARSFA_based
%%%% based on DPARSFA_run.m
a=which('fmri_grocer.m');
[DPARSF_path,b,c,d]=fileparts(a);
ProgramPath = DPARSF_path;
if strcmp(spm('ver',[],1),'SPM5')|strcmp(spm('ver',[],1),'SPM8')
subjdir = spm_select(1,'dir','Select the subject''s directory where all the img/hdr Img. are:', [],pwd);
else
subjdir = spm_get(1,'dir','Select the subject''s directory:');
end
if isempty(subjdir)
return
end
cd(subjdir);
dirs=spm_str_manip(spm_str_manip(subjdir,'dh'),'dc'); % take use of the "spm_str_manip" function
if size(dirs,1)==1 % in this condition, [spm_str_manip(spm_str_manip(dirs,'dh'),'dc')] can't get the subject dirctories
i=size(dirs,2);
success=0;
for j=i:-1:1
if dirs(j)==filesep
success=1;
break
end
end
if success==1
dirs=dirs(j+1:end);
end
end
if strcmp(spm('ver',[],1),'SPM5')|strcmp(spm('ver',[],1),'SPM8')
files = spm_select(Inf,'any','Select the all the img/hdr Img. you want to draw AAL regions timecourse:', [],pwd,'.img$|.nii$');
end
if isempty(files)
return
end
files=spm_str_manip(files,'dc'); % take use of the "spm_str_manip" function
if size(files,1)==1 % in this condition, [spm_str_manip(spm_str_manip(files,'dh'),'dc')] can't get the group dirctories
i=size(files,2);
success=0;
for j=i:-1:1
if files(j)==filesep
success=1;
break
end
end
if success==1
files=files(j+1:end);
end
end
% dlg_prompt={'How many imgs in the subject''s diretory:'};
% dlg_name='Timepoints';
% TimePoints=inputdlg(dlg_prompt,dlg_name);
% TimePoints=str2num(cell2mat(TimePoints));
%Extract AAL Time Cources (90 areas) for one subject
% mkdir(['..',filesep, dirs,'_AALTC',filesep])
% Check if the mask is appropriate
AMaskFilename=spm_select(1,'any','Suggest to select a right resolution AAL.nii:', [],[ProgramPath,filesep,'Templates',filesep],'^AAL.*nii');
if isempty(AMaskFilename)
return
end
AMaskFilename=deblank(AMaskFilename);
% AMaskFilename=[ProgramPath,filesep,'Templates',filesep,'AAL_61x73x61.nii'];
[MaskData,MaskVox,MaskHeader]=FG_rest_readfile(AMaskFilename);
% DirImg=dir('*.img');
DirImg=files;
RefFile = deblank(DirImg(1,:));
[RefData,RefVox,RefHeader]=FG_rest_readfile(RefFile);
if ~isequal(size(MaskData), size(RefData))
fprintf('\nReslice AAL Mask (%s) for "%s" since the dimension of mask mismatched the dimension of the functional data.\n',AMaskFilename, RefFile);
if ~(7==exist(['..',filesep,'Masks'],'dir')) % judge whether is it a directory
mkdir(['..',filesep,'Masks']);
end
ReslicedMaskName=['..',filesep,'Masks',filesep,'AAL_',dirs,'.img'];
FG_y_Reslice(AMaskFilename,ReslicedMaskName,RefVox,0, RefFile);
AMaskFilename=ReslicedMaskName;
end
% Generate the time courses
[AALData, Vox, Head] = FG_rest_readfile(AMaskFilename);
for iAAL=1:90
AreaName=['0',num2str(iAAL)];
AreaName=AreaName(end-1:end);
% in the standard 'AAL_61x73x61.nii', all the regions are masked from 1~116
eval(['AAL',AreaName,'Index=find(round(AALData)==',num2str(iAAL),');']); % cliff , add round() here to deal with the situation that the AAL template is auto resliced
end
for iAAL=1:90
AreaName=['0',num2str(iAAL)];
AreaName=AreaName(end-1:end);
eval(['AAL',AreaName,'TC=[];']);
end
% DirImg=dir('*.img');
for j=1:size(DirImg,1)
Filename=deblank(DirImg(j,:));
[Data, Vox, Head] = FG_rest_readfile(Filename);
% Data(find(isnan(Data)))=0; % cliff, reset all the nan values into 0
for iAAL=1:90
AreaName=['0',num2str(iAAL)];
AreaName=AreaName(end-1:end);
eval(['Temp=mean(Data(AAL',AreaName,'Index));']);
eval(['AAL',AreaName,'TC=[AAL',AreaName,'TC;Temp];']);
end
end
if 7==exist(['..',filesep,'Masks'],'dir')
rmdir (['..',filesep,'Masks'],'s') % because the resliced BA mask may be wrong, so remove them before exit
end
% save([subjdir,dirs,'_AALTC90.mat'],'-regexp', 'AAL\w\wTC');
clc
% write them into excel row by row
tem=[];
for iAAL=1:90
if iAAL<=9
tem=[tem;{eval(['AAL0' num2str(iAAL) 'TC'])'}];
else
tem=[tem;{eval(['AAL' num2str(iAAL) 'TC'])'}];
end
end
csvwrite('AAL_90TCs_rows.csv',tem);
% write them into excel column by column
tem=[];
for iAAL=1:90
if iAAL<=9
tem=[tem {eval(['AAL0' num2str(iAAL) 'TC'])}];
else
tem=[tem {eval(['AAL' num2str(iAAL) 'TC'])}];
end
end
csvwrite('AAL_90TCs_columns.csv',tem);
cd('..');
fprintf('\n\n==AAL Time Cources extracting: %s''s AAL Time Course has been saved into:\n\n %s%s_AALTC.mat & AAL_90TCs_rows/columns.csv\n\n',dirs,subjdir,dirs);