0001 function [ResultMaps] = fc(ADataDir,AMaskFilename, AROIDef,AResultFilename, ACovariablesDef)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024 if nargin~=5,
0025 error(' Error using ==> fc. 4 arguments wanted.');
0026 end
0027
0028 theElapsedTime =cputime;
0029 fprintf('\nComputing functional connectivity with:\t"%s"', ADataDir);
0030 [AllVolume,VoxelSize,theImgFileList, Header] =rest_to4d(ADataDir);
0031
0032 nDim1 = size(AllVolume,1); nDim2 = size(AllVolume,2); nDim3 = size(AllVolume,3);
0033 BrainSize = [nDim1 nDim2 nDim3];
0034 sampleLength = size(theImgFileList,1);
0035
0036
0037
0038 theTempDatasetDirName =sprintf('fc_%d', fix((1e4) *rem(now, 1) ));
0039 theTempDatasetDir =[tempdir theTempDatasetDirName] ;
0040 ans=rmdir(theTempDatasetDir, 's');
0041 mkdir(tempdir, theTempDatasetDirName);
0042
0043 AROIList =AROIDef;
0044 if iscell(AROIDef),
0045
0046 theROITimeCourses =zeros(sampleLength, size(AROIDef,1));
0047 for x=1:size(AROIDef,1),
0048 fprintf('\n\t ROI time courses retrieving through "%s".', AROIDef{x});
0049 IsDefinedROITimeCourse =0;
0050 if rest_SphereROI( 'IsBallDefinition', AROIDef{x})
0051
0052 maskROI =rest_SphereROI( 'BallDefinition2Mask' , AROIDef{x}, BrainSize, VoxelSize, Header);
0053 elseif exist(AROIDef{x},'file')==2
0054 [pathstr, name, ext, versn] = fileparts(AROIDef{x});
0055 if strcmpi(ext, '.txt'),
0056 tmpX=load(AROIDef{x});
0057 if size(tmpX,2)>1,
0058
0059 tmpX = mean(tmpX')';
0060 end
0061 theROITimeCourses(:, x) =tmpX;
0062 IsDefinedROITimeCourse =1;
0063 elseif strcmpi(ext, '.img')
0064
0065 maskROI =rest_loadmask(nDim1, nDim2, nDim3, AROIDef{x});
0066 else
0067 error(sprintf('REST doesn''t support the selected ROI definition now, Please check: \n%s', AROIDef{x}));
0068 end
0069 else
0070 error(sprintf('Wrong ROI definition, Please check: \n%s', AROIDef{x}));
0071 end
0072
0073 if ~IsDefinedROITimeCourse,
0074 maskROI =find(maskROI);
0075
0076
0077 for t=1:sampleLength,
0078 theTimePoint = squeeze(AllVolume(:,:,:, t));
0079 theTimePoint = theTimePoint(maskROI);
0080 if ~isempty(theTimePoint),
0081 theROITimeCourses(t, x) =mean(theTimePoint);
0082 end
0083 end
0084 end
0085 end
0086
0087 [pathstr, name, ext, versn] = fileparts(AResultFilename);
0088 theROITimeCourseLogfile =[fullfile(pathstr,['ROI_', name]), '.txt'];
0089 save(theROITimeCourseLogfile, 'theROITimeCourses', '-ASCII', '-DOUBLE','-TABS')
0090
0091
0092 theCovariables =[];
0093 if exist(ACovariablesDef.ort_file, 'file')==2,
0094 theCovariables =load(ACovariablesDef.ort_file);
0095
0096 thePolOrt=[];
0097 if ACovariablesDef.polort>=0,
0098 thePolOrt =(1:sampleLength)';
0099 thePolOrt =repmat(thePolOrt, [1, (1+ACovariablesDef.polort)]);
0100 for x=1:(ACovariablesDef.polort+1),
0101 thePolOrt(:, x) =thePolOrt(:, x).^(x-1) ;
0102 end
0103 end
0104
0105
0106 theCovariables =[thePolOrt, theCovariables];
0107 theROITimeCourses =Brain1D_RegressOutCovariables(theROITimeCourses, theCovariables);
0108
0109 elseif ~isempty(ACovariablesDef.ort_file) && ~all(isspace(ACovariablesDef.ort_file)),
0110 warning(sprintf('\n\nCovariables definition text file "%s" doesn''t exist, please check! I won''t regress out the covariables this time.', ACovariablesDef.ort_file));
0111 end
0112
0113
0114
0115 ResultMaps =corrcoef(theROITimeCourses);
0116 save([AResultFilename, '.txt'], 'ResultMaps', '-ASCII', '-DOUBLE','-TABS')
0117
0118
0119 else
0120
0121 fprintf('\n\t ROI time course retrieving through "%s".', AROIDef);
0122 AROITimeCourse = zeros(sampleLength, 1);
0123 IsDefinedROITimeCourse =0;
0124 if rest_SphereROI( 'IsBallDefinition', AROIDef)
0125
0126 maskROI =rest_SphereROI( 'BallDefinition2Mask' , AROIDef, BrainSize, VoxelSize, Header);
0127 elseif exist(AROIDef,'file')==2
0128 [pathstr, name, ext, versn] = fileparts(AROIDef);
0129 if strcmpi(ext, '.txt'),
0130 tmpX=load(AROIDef);
0131 if size(tmpX,2)>1,
0132
0133 tmpX = mean(tmpX')';
0134 end
0135 AROITimeCourse =tmpX;
0136 IsDefinedROITimeCourse =1;
0137 elseif strcmpi(ext, '.img')
0138
0139 maskROI =rest_loadmask(nDim1, nDim2, nDim3, AROIDef);
0140 else
0141 error(sprintf('REST doesn''t support the selected ROI definition now, Please check: \n%s', AROIDef));
0142 end
0143 else
0144 error(sprintf('Wrong ROI definition, Please check: \n%s', AROIDef));
0145 end
0146 if ~IsDefinedROITimeCourse,
0147 maskROI =find(maskROI);
0148
0149
0150 for t=1:sampleLength,
0151 theTimePoint = squeeze(AllVolume(:,:,:, t));
0152 theTimePoint = theTimePoint(maskROI);
0153 AROITimeCourse(t) =mean(theTimePoint);
0154 end
0155
0156 AROITimeCourse =reshape(AROITimeCourse, sampleLength,1);
0157 end
0158
0159 [pathstr, name, ext, versn] = fileparts(AResultFilename);
0160 theROITimeCourseLogfile =[fullfile(pathstr,['ROI_', name]), '.txt'];
0161 save(theROITimeCourseLogfile, 'AROITimeCourse', '-ASCII', '-DOUBLE','-TABS')
0162
0163
0164 Save1stDimPieces(theTempDatasetDir, AllVolume, 'dim1_');
0165 clear AllVolume;
0166
0167
0168 fprintf('\n\t Load mask "%s".', AMaskFilename);
0169 mask=rest_loadmask(nDim1, nDim2, nDim3, AMaskFilename);
0170
0171 fprintf('\n\t Build functional connectivity mask.\tWait...');
0172 mask =logical(mask);
0173 mask = repmat(mask, [1, 1, 1, sampleLength]);
0174
0175 Save1stDimPieces(theTempDatasetDir, mask, 'mask_');
0176
0177 fprintf('\n\t Build Covariables time course.\tWait...');
0178
0179 theCovariables =[];
0180 if exist(ACovariablesDef.ort_file, 'file')==2,
0181 theCovariables =load(ACovariablesDef.ort_file);
0182
0183 thePolOrt=[];
0184 if ACovariablesDef.polort>=0,
0185 thePolOrt =(1:sampleLength)';
0186 thePolOrt =repmat(thePolOrt, [1, (1+ACovariablesDef.polort)]);
0187 for x=1:(ACovariablesDef.polort+1),
0188 thePolOrt(:, x) =thePolOrt(:, x).^(x-1) ;
0189 end
0190 end
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201 theCovariables =[thePolOrt, theCovariables];
0202
0203 elseif ~isempty(ACovariablesDef.ort_file) && ~all(isspace(ACovariablesDef.ort_file)),
0204 warning(sprintf('\n\nCovariables definition text file "%s" doesn''t exist, please check! I won''t regress out the covariables this time.', ACovariablesDef.ort_file));
0205 end
0206
0207 fprintf('\n\t Functional connectivity is computing.\tWait...');
0208 NumPieces_Dim1 =4;
0209 NumComputingCount =floor(nDim1/NumPieces_Dim1);
0210 if NumComputingCount< (nDim1/NumPieces_Dim1),
0211 NumComputingCount =NumComputingCount +1;
0212 else
0213 end
0214 for x=1:(NumComputingCount),
0215
0216 rest_waitbar(x/(floor(nDim1/NumPieces_Dim1) +1), ...
0217 'Computing Functional connectivity. Please wait...', ...
0218 'REST working','Child','NeedCancelBtn');
0219
0220
0221 theFilename =fullfile(theTempDatasetDir, sprintf('dim1_%.8d', x));
0222 theDim1Volume4D =Load1stDimVolume(theFilename);
0223 theDim1Volume4D =double(theDim1Volume4D);
0224
0225
0226 theFilename =fullfile(theTempDatasetDir, sprintf('mask_%.8d', x));
0227 theDim1Mask4D =Load1stDimVolume(theFilename);
0228 theDim1Volume4D(~theDim1Mask4D)=0;
0229
0230
0231
0232 for y=1:size(AROITimeCourse, 2),
0233
0234 if ~isempty(theCovariables),
0235 AROITimeCourse(:, y) =Brain1D_RegressOutCovariables(AROITimeCourse(:, y), theCovariables);
0236 theDim1Volume4D =Brain4D_RegressOutCovariables(theDim1Volume4D, theCovariables);
0237 end
0238
0239
0240 theCorrResult =PearsonCorrWithROI(AROITimeCourse(:, y), theDim1Volume4D);
0241
0242
0243 theFilename =fullfile(theTempDatasetDir, sprintf('result%.2d_%.8d', y, x));
0244 save(theFilename, 'theCorrResult');
0245 end
0246 fprintf('.');
0247 end
0248 clear theDim1Volume4D theDim1Mask4D theCorrResult;
0249
0250
0251 fprintf('\n\t ReConstructing 3D Dataset Functional Connectivity.\tWait...');
0252
0253 [pathstr, name, ext, versn] = fileparts(AResultFilename);
0254 ResultMaps =[];
0255 for y=1:size(AROITimeCourse, 2),
0256 ResultMaps =[ResultMaps;{[pathstr, filesep ,name, sprintf('%.2d',y), ext]}];
0257 end
0258
0259 for y=1:size(AROITimeCourse, 2),
0260 theDataset3D=zeros(nDim1, nDim2, nDim3);
0261 for x=1:(NumComputingCount)
0262 rest_waitbar(x/(floor(nDim1/NumPieces_Dim1)+1), ...
0263 'Functional Connectivity 3D Brain reconstructing. Please wait...', ...
0264 'REST working','Child','NeedCancelBtn');
0265
0266 theFilename =fullfile(theTempDatasetDir, sprintf('result%.2d_%.8d', y, x));
0267
0268 if x~=(floor(nDim1/NumPieces_Dim1)+1)
0269 theDataset3D(((x-1)*NumPieces_Dim1+1):(x*NumPieces_Dim1),:,:)=Load1stDimVolume(theFilename);
0270 else
0271 theDataset3D(((x-1)*NumPieces_Dim1+1):end,:,:)=Load1stDimVolume(theFilename);
0272 end
0273 fprintf('.');
0274 end
0275
0276 if size(AROITimeCourse, 2)>1,
0277
0278 fprintf('\n\t Saving Functional Connectivity maps: %s\tWait...', ResultMaps{y, 1});
0279 rest_writefile(single(theDataset3D), ...
0280 ResultMaps{y, 1}, ...
0281 BrainSize,VoxelSize,Header, 'single');
0282 elseif size(AROITimeCourse, 2)==1,
0283
0284
0285 fprintf('\n\t Saving Functional Connectivity map.\tWait...');
0286 rest_writefile(single(theDataset3D), ...
0287 AResultFilename, ...
0288 BrainSize,VoxelSize,Header, 'single');
0289 end
0290 end
0291 end
0292
0293
0294 theElapsedTime =cputime - theElapsedTime;
0295 fprintf('\n\t Functional Connectivity compution over, elapsed time: %g seconds.\n', theElapsedTime);
0296
0297
0298
0299 ans=rmdir(theTempDatasetDir, 's');
0300
0301
0302
0303 function Save1stDimPieces(ATempDir, A4DVolume, AFilenamePrefix)
0304 NumPieces_Dim1 =4;
0305 NumComputingCount =floor(size(A4DVolume,1)/NumPieces_Dim1);
0306 if NumComputingCount< (size(A4DVolume,1)/NumPieces_Dim1),
0307 NumComputingCount =NumComputingCount +1;
0308 else
0309 end
0310 for x = 1:(NumComputingCount),
0311
0312 rest_waitbar(x/(floor(size(A4DVolume,1)/NumPieces_Dim1)+1), ...
0313 'Cut one Big 3D+time Dataset into pieces of 3D+time Dataset Before Functional Connectivity. Please wait...', ...
0314 'REST working','Child','NeedCancelBtn');
0315
0316 theFilename =fullfile(ATempDir, sprintf('%s%.8d',AFilenamePrefix, x));
0317 if x~=(floor(size(A4DVolume,1)/NumPieces_Dim1)+1)
0318 the1stDim = A4DVolume(((x-1)*NumPieces_Dim1+1):(x*NumPieces_Dim1), :,:,:);
0319 else
0320 the1stDim = A4DVolume(((x-1)*NumPieces_Dim1+1):end, :,:,:);
0321 end
0322 save(theFilename, 'the1stDim');
0323 end
0324
0325
0326 function Result=Load1stDimVolume(AFilename)
0327 Result =load(AFilename);
0328 theFieldnames=fieldnames(Result);
0329
0330 Result = Result.(theFieldnames{1});
0331
0332
0333
0334 function ResultCorrCoef3DBrain=PearsonCorrWithROI(AROITimeCourse, ABrain4D);
0335 [nDim1, nDim2, nDim3, nDim4]=size(ABrain4D);
0336
0337 ABrain4D = ABrain4D - repmat(sum(ABrain4D,4)/nDim4,[1, 1, 1, nDim4]);
0338 AROITimeCourse =AROITimeCourse -repmat(sum(AROITimeCourse)/nDim4, [size(AROITimeCourse,1), size(AROITimeCourse,2)]);
0339
0340 ABrainSTD= squeeze(std(ABrain4D, 0, 4));
0341 ABrainSTD=ABrainSTD * std(AROITimeCourse);
0342
0343
0344 ABrain4D =reshape(ABrain4D, nDim1*nDim2*nDim3, nDim4)';
0345 AROITimeCourse =reshape(AROITimeCourse, 1, nDim4);
0346 ABrain4D = AROITimeCourse * ABrain4D /(nDim4 -1);
0347 ABrain4D =squeeze(reshape(ABrain4D', nDim1, nDim2, nDim3, 1));
0348
0349
0350
0351 ABrainSTD(find(ABrainSTD==0))=inf;
0352 ResultCorrCoef3DBrain =ABrain4D ./ABrainSTD;
0353
0354
0355
0356 function Result =Brain4D_RegressOutCovariables(ABrain4D, ABasisFunc)
0357
0358
0359 [nDim1, nDim2, nDim3, nDim4]=size(ABrain4D);
0360
0361
0362 if size(ABasisFunc,1)~=nDim4, error('The length of Covariables don''t match with the volume.'); end
0363
0364
0365 ABrain4D =reshape(ABrain4D, nDim1*nDim2*nDim3, nDim4)';
0366 Result =(eye(nDim4) - ABasisFunc * inv(ABasisFunc' * ABasisFunc)* ABasisFunc') * ABrain4D;
0367
0368
0369 Result =reshape(Result', nDim1, nDim2, nDim3, nDim4);
0370
0371 function Result =Brain1D_RegressOutCovariables(ABrain1D, ABasisFunc)
0372
0373
0374
0375
0376
0377
0378 if size(ABasisFunc,1)~=length(ABrain1D), error('The length of Covariables don''t match with the volume.'); end
0379
0380
0381 Result =(eye(size(ABrain1D, 1)) - ABasisFunc * inv(ABasisFunc' * ABasisFunc)* ABasisFunc') * ABrain1D;
0382