## Monday, November 27, 2017

### assigning arbitrary values to surface parcels: method 1

This tutorial describes a method for plotting arbitrary per-parcel values (such as from an analysis) on the surface. For example, let's say I want to display MMP parcels 1, 10, and 15 (only) in red, or (more usefully) to assign continuous numbers to each parcel, and then display parcels with larger numbers in hotter colors.

This post describes a method using matlab and creating GIfTI files; see the next post for a method using R, wb_command functions, and creating a CIFTI file. Both methods work, but one or the other might be more convenient in particular situations.

I assume that you have a surface version of the parcellation to use as a template. For example, the MMP parcellation was released in CIFTI format as part of the HCP 1200 release, and the Gordon (2014) parcellation can be downloaded here.

I'll be using the MMP in this example; if you want to follow along, download a copy of the S1200 Group Average Data Release; I put mine at d:/Workbench/HCP_S1200_GroupAvg_v1/. The MMP template is named Q1-Q6_RelatedValidation210.CorticalAreas_dil_Final_Final_Areas_Group_Colors.32k_fs_LR.dlabel.nii. (If you're not sure how to use the files in the S1200 release, try this tutorial to get started.)

I have the values to assign to the parcels in a text file with 180 lines (one line for each MMP parcel). For this tutorial, let's do the simple example of assigning a color to parcels 1, 10, and 15 only. An easy way to do this is to make a text file with 1s on these rows and 0s on all the other rows. I prefer R, but since the GIfTI library is in matlab, here's matlab code for making the little text file:
`````` out = repelem(0, 180);
out(1) = 1;
out(10) = 1;
out(15) = 1;
csvwrite('D:\temp\parcelNums.csv', out')
``````

The MMP template is in CIFTI format, but we can extract GIfTI files for each hemisphere using wb_command cifti-separate:
`````` wb_command -cifti-separate D:\Workbench\HCP_S1200_GroupAvg_v1\Q1-Q6_RelatedValidation210.CorticalAreas_dil_Final_Final_Areas_Group_Colors.32k_fs_LR.dlabel.nii COLUMN -metric CORTEX_LEFT D:\temp\mmpL.func.gii
wb_command -cifti-separate D:\Workbench\HCP_S1200_GroupAvg_v1\Q1-Q6_RelatedValidation210.CorticalAreas_dil_Final_Final_Areas_Group_Colors.32k_fs_LR.dlabel.nii COLUMN -metric CORTEX_RIGHT D:\temp\mmpR.func.gii
``````

This matlab code reads in the text file with the new parcel values and the GIfTI templates, then writes GIfTI files with the new parcel values substituted for the parcel label numbers:
`````` addpath 'C:/Program Files/MATLAB/gifti-1.6';  % so matlab can find the library
mmpR = gifti('D:\temp\mmpR.func.gii');  % and the right side MMP atlas
newvals = csvread('D:\temp\parcelNums.csv');  % 180 integers; new value for each parcel
Lout = mmpL;  % output gifti
Lout.cdata(:,1) = repelem(0, size(mmpL.cdata,1));  % replace the values with zeros
Rout = mmpR;
Rout.cdata(:,1) = repelem(0, size(mmpR.cdata,1));
for i=1:180  % i = 1;
inds = find(mmpR.cdata == i);  % find vertices for parcel i
Rout.cdata(inds,1) = newvals(i);  % put the new value in for this parcel's vertices
inds = find(mmpL.cdata == (i+180)); % in MMP, left hemisphere vertices are 181:360
Lout.cdata(inds,1) = newvals(i);
end
save(Rout,'D:\temp\plotR.func.gii','Base64Binary');
``````

You can now to plot these GIfTIs in Workbench (see this post if you're not sure how); I plotted them on the S1200 Group Average (MNI) anatomy:
I clicked to put a marker in the left visual parcel. The value at this vertex is 1, as assigned (green lines). I loaded in the MMP atlas as well (blue lines), so it tells me (correctly!) that the marker is in the L_V1_ROI.

1. You can also do this without going to dense representation (all vertices). However, dense files are easier to combine with other data.

To do this the parcellated way, first take any 91282 dscalar file (single column would be best), and use "wb_command -cifti-parcellate" to make a template .pscalar.nii file with the dlabel file of the parcellation of your choice. You can then use "wb_command -cifti-convert" with "-from-text" to replace the values in the .pscalar.nii file with values from a text file.

1. It took a few tries, but I got it to work via -cifti-parcellate; I'll post that version when I get a chance. Thanks!