Skip to content

How to Make Pretty Surface Renderings of Parcellations Using Connectome Workbench

Shi Gu edited this page Feb 8, 2017 · 7 revisions

This page describes how to map the volume measure on to the surface by modifying an existing ".dlabel.nii" file in Workbench.

First, you need to download the matlab-cifti package from https://github.com/robertoostenveld/cifti-matlab, which allows you to read and save cifti file. However, the support for .dlabel.nii file is not satisfying. For a tutorial of updating scalar and time series datafile, you can refer to https://mandymejia.wordpress.com/2015/08/10/a-laymans-guide-to-working-with-cifti-files/.

Theoretically there should be no problem here and you can export the ".dlabel.nii" file to a table, modify it and load it back (credit to Jaredz). However, sometimes the labels for your ".gii" or ".nii" template are not exactly aligned with the ".dlabel.nii" file. You need to read in the ".dlabel.nii" and ".xii" first and pair the label first. Sometimes the ".dlabel.nii file contains the subcortical area and please take care of where they are located.

Below attached is a matlab script for the Gordon parcellation. You can modify it for Glasser's as long as you got the related templates.

This is not the easiest way to do this. An alternative is to take the label as scalar measure and save it to ".dscalar.nii" instead.

Or if you want to visualize the .gii file directly. You could also plot it with the method stated in http://www.artefact.tk/software/matlab/gifti/ as long as you have ".surf.gii" files.

function ciftiDLabelUpdateUglyFromGii(wbCommand, ciftiLabelFileRead, ciftiLabelFileWrite, giiLabelVector, matlabLabel, modularColormap)
% this function is superly ugly and only works for the Gordon
% template
% Parameter: 
%                    wbCommand: path to "wb_command"
%           ciftiLabelFileRead: path to .dlabel.nii file
%          ciftiLabelFileWrite: fileName to save
%               giiLabelVector: matlab vector saving the voxel label
%                               information from .gii template
%                  matlabLabel: new label to update
%              modularColormap: color information 
%------------------------------------------------------------------------
% p is the number of regions
p = 333;
% export the existing label in .dlabel.nii to a txt file
cmdExport = sprintf('%s %s %s %s',wbCommand ,'-cifti-label-export-table',ciftiLabelFileRead,'1 ', 'newTableOutput.txt');
system(cmdExport);

% read in the cifti file and align the .cii and .gii label
source = ft_read_cifti(ciftiLabelFileRead);
ciiLabelVector = source.x333cort_subcort;
cii2giiMap = zeros(p,1);
gii2ciiMap = zeros(p,1);
for i = 1:p
    idxcii2gii = (ciiLabelVector == i);
    idxgii2cii = (giiLabelVector == i);
    cii2giiMap(i) = round(nanmean(giiLabelVector(idxcii2gii)));
    gii2ciiMap(i) = round(nanmean(ciiLabelVector(idxgii2cii)));
end

fileID = fopen('newTableOutput.txt','r');
formatSpec = '%s\n%d%d%d%d%d';
C = textscan(fileID,formatSpec);
fclose(fileID);

if max(modularColormap(:)) < 2
    modularColormap = round(255*modularColormap);
end

% align and update labels
matlabLabelMin = min(matlabLabel);
matlabLabelMax = max(matlabLabel);
cminIdx = 1;
cmaxIdx = size(modularColormap,1);
for i = 1:p
    matlabIdx = matlabLabel(cii2giiMap(i));
    scaleIdx = round((matlabIdx-matlabLabelMin)/(matlabLabelMax-matlabLabelMin)*(cmaxIdx - cminIdx) + cminIdx);
    for cl = 4:6
        C{cl}(i) = modularColormap(scaleIdx,cl-3);
    end
    C{3}(i) = 255;

end
for i = p+1:numel(C{1})
    C{3}(i) = 0;
    for cl = 4:6
        C{cl}(i) = 40;
    end
end

fileIDE = fopen('newTableInput.txt','w');
for i = 1:numel(C{1})
    fprintf(fileIDE,'%s\n%d %d %d %d %d\n',C{1}{i},C{2}(i),C{3}(i),C{4}(i),C{5}(i),C{6}(i));
end
fclose(fileIDE);


cmdImport = sprintf('%s %s %s %s %s',wbCommand ,'-cifti-label-import',ciftiLabelFileRead,'newTableInput.txt',ciftiLabelFileWrite);
% update and delete the tmp files
system(cmdImport);
system('rm newTableOutput.txt');
system('rm newTableInput.txt');
end