Skip to content

How to Make Pretty Surface Renderings of Parcellations Using Connectome Workbench

Shi Gu edited this page Feb 4, 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.

function ciftiDLabelUpdateUglyFromGii(wbCommand, ciftiLabelFileRead, ciftiLabelFileWrite, giiLabelVector, matlabLabel, modularColormap)
% this function is superly ugly and only works for the Gordon template
p = 333;
cmdExport = sprintf('%s %s %s %s',wbCommand ,'-cifti-label-export-table',ciftiLabelFileRead,'1 ', 'newTableOutput.txt');
system(cmdExport);
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


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);
system(cmdImport);
% system('rm newTableOutput.txt');
% system('rm newTableInput.txt');
end