display_vox2surf_corrmap.m

% Displays surface of whole-brain voxel-wise fMRI timeseries correlations
% with source voxel timeseries on surface mesh.
%
% Usage: [M R H] = display_vox2surf_corrmap(P,MNI,Ps)
%
% P   – Filename for fMRI image.
% MNI – MNI coordinates for source voxel.
% Ps  – Filename for cortical segmentation mesh.
% M   – Mesh structure.
% R   – Facevertex CData variable.
% H   – Mesh handle.
%
% Created by Joshua Goh 11 May 2013.
 
function [M R H] = display_vox2surf_corrmap(P,MNI,Ps)
 
% Setup environment
global O;
 
if nargin < 3
    [P p]   = uigetfile({‘*.nii’,’*.nii’;’*.img’,’*.img’},’Select fMRI image file’);
    P = [p P];
    MNI     = str2num(cell2mat(inputdlg(‘Enter source voxel timeseries (e.g. 0 0 0, default)’,…
        ”,1,{‘0 0 0’})));
    [Ps ps] = uigetfile({‘*.gii’,’*.gii’},’Select surface mesh file’);
    Ps = [ps Ps];
end
 
% Setup fMRI timeseries data
[p n e]  = fileparts(P);
V        = spm_vol(P);
Y        = spm_read_vols(V);
dim      = size(Y);
Yr       = reshape(Y,prod(dim(1:3)),dim(4));
Vr       = V(1);
Vr.dt    = [16 0];
Vr.pinfo = [1;0;0];
xyz      = mni2vox(V(1).mat,MNI’);
 
% Correlate source voxel timeseries with whole-brain voxel timeseries.
S = squeeze(Y(xyz(1),xyz(2),xyz(3),:));
r = reshape(corr(S,Yr’),dim(1),dim(2),dim(3));
 
% Save correlation image
Vr.fname   = sprintf(‘%s/corrmap.nii’,p);
Vr.descrip = sprintf(‘Source voxel %d %d %d’,MNI(1),MNI(2),MNI(3));
spm_write_vol(Vr,r);
 
% Overlay on surface
FV = gifti(Ps);
M  = spm_mesh_inflate(FV,Inf); % Inflated surface.
R  = spm_mesh_project(M,Vr.fname,{‘nn’});
H  = spm_mesh_render(M);
H  = spm_mesh_render(‘Overlay’,H,R);
H  = spm_mesh_render(‘ColourMap’,H,jet);
H  = spm_mesh_render(‘ColourBar’,H,{‘on’});
 
% Figure properties
title(sprintf(‘Correlations with source voxel %d %d %d’,MNI(1),MNI(2),MNI(3)));
 
% Menu for voxel-based correlation update setup
uimenu(H.figure,’Label’,’Update Correlation’, ‘Callback’, {@updatecorr,H});
uimenu(H.figure,’Label’,’Smooth Overlay’, ‘Callback’, {@smooth,H});
 
% Declare global values
O.Yr  = Yr;
O.Y   = Y;
O.dim = dim;
O.Vr  = Vr;
O.M   = M;
 
 
%Sub-functions
function H = updatecorr(obj,evt,H)
dcmObj = datacursormode(H.figure);
point1 = getCursorInfo(dcmObj);
MNI    = round(point1.Position);
disp(sprintf(‘Updating correlations based on MNI: %d %d %d’,MNI(1),MNI(2),MNI(3)));
global O
xyz     = mni2vox(O.Vr.mat,MNI’);
S       = squeeze(O.Y(xyz(1),xyz(2),xyz(3),:));
dat.dat = reshape(corr(S,O.Yr’),O.dim(1),O.dim(2),O.dim(3)); % Use eval to include Spearman, Kendall etc..
dat.mat = O.Vr.mat;
R       = spm_mesh_project(O.M,dat,{‘nn’});
H       = spm_mesh_render(‘Overlay’,H,R);
title(sprintf(‘Correlations with source voxel %d %d %d’,MNI(1),MNI(2),MNI(3)));
 
function H = smooth(obj,evt,H)
global O
smth = str2num(cell2mat(inputdlg(‘Enter smooth iterations (e.g. default 4)’,…
        ”,1,{‘4’})));
R = spm_mesh_smooth(O.M,getappdata(H.patch,’data’)’,smth);
H = spm_mesh_render(‘Overlay’,H,R’);
 
function VOX=mni2vox(M,MNI)
 T=M(1:3,4);
 M=M(1:3,1:3);
 for i=1:3
MNI(i,:)=MNI(i,:)-T(i);
 end
 VOX=round(inv(M)*MNI);
return