Created
September 13, 2013 20:13
-
-
Save vsoch/6555488 to your computer and use it in GitHub Desktop.
Functions for exporting graph data (a list of connected nodes and corresponding attribute values) for functional neuroimaging data. Currently includes cytoscape and gephi. You must provide a list of anatomical labels (the variable AAL_labels).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function cytoscapeExport(links,weights,threshold,exptype) | |
% This function will take a list of links and weights, and create a file to | |
% import into Cytoscape for further graph analysis | |
% exptype is the export type - 'all' means voxelwise above a threshold, | |
% 'region' means creating graph of average regional values | |
% links is an n by 2 list of connections, each a voxel id | |
% weights is a correlation value corresponding to each link set | |
% You must load AAL_labels, a cell of length 116 with region names, and | |
% aalflat is a flattened AAL atlas, the same size as the original data | |
% First write sif file - this is a list of connections between nodes based | |
% on voxel index | |
if strcmp(exptype,'all') | |
voxels = unique(links(weights>threshold,:),'rows'); | |
nodes = unique([ voxels(:,1) voxels(:,2) ]); | |
weightpt9 = weights(weights>threshold); | |
% Write each to file | |
filey = fopen('brain.sif','w'); | |
for v=1:length(voxels) | |
fprintf(filey,'%s\n',[ num2str(voxels(v,1)) ' fc ' num2str(voxels(v,2)) ]); | |
end | |
fclose(filey); | |
% Now create data file that includes region names | |
filey = fopen('brain_features.csv','w'); | |
fprintf(filey,'%s\n','VOXEL,REGION_AAL'); | |
for n=1:length(nodes) | |
fprintf(filey,'%s\n',[ num2str(nodes(n)) ',' AAL_labels{aalflat(nodes(n))} ]); | |
end | |
fclose(filey); | |
% Write edges to file | |
filey = fopen('brain_edges.csv','w'); | |
for v=1:length(voxels) | |
fprintf(filey,'%s\n',[ num2str(voxels(v,1)) ' (fc) ' num2str(voxels(v,2)) ' = ' num2str(weightpt9(v)) ]); | |
end | |
fclose(filey); | |
elseif strcmp(exptype,'region') | |
% Get unique links and weights | |
linksA = links(weights>threshold,:); | |
weightsA = weights(weights>threshold); | |
[linksA,ia,~] = unique(linksA,'rows'); | |
weightsA = weightsA(ia); | |
% Now we need to get rid of duplicates, and also when i==j | |
linksAinv = [ linksA(:,2) linksA(:,1) ]; | |
dups = (linksA == linksAinv); | |
linksA(dups(:,1),:) = []; | |
weightsA(dups(:,1)) = []; | |
% For each region, calculate mean weight | |
region_weights = cell(116,116); | |
region_vox = zeros(116,116); | |
for i=1:116 | |
for j=1:116 | |
region_weights{i,j} = []; | |
end | |
end | |
for l=1:length(linksA) | |
nodes = sort(aalflat(linksA(l,:))); | |
% If we have the same region | |
region_vox(nodes(1),nodes(2)) = region_vox(nodes(1),nodes(2)) + 1; | |
region_weights{nodes(1),nodes(2)} = [ region_weights{nodes(1),nodes(2)}; weightsA(l) ]; | |
end | |
% Calculate mean inter and intra region weights | |
mean_weights = zeros(116,116); | |
for i=1:116 | |
for j=1:116 | |
weightset = region_weights{i,j}; | |
mean_weights(i,j) = mean(weightset(~isnan(weightset))); | |
end | |
end | |
mean_weights(isnan(mean_weights)) = 0; | |
% Now write to cytoscape export file | |
filey = fopen('brain_region.sif','w'); | |
for r1=1:116 | |
for r2=1:116 | |
region1 = AAL_labels{r1}; | |
region2 = AAL_labels{r2}; | |
fprintf(filey,'%s\n',[ region1 ' fc ' region2 ]); | |
end | |
end | |
fclose(filey); | |
% Now create data file that includes intra region weights | |
filey = fopen('brain_region_features.csv','w'); | |
for w1=1:size(mean_weights,1) | |
for w2=1:size(mean_weights,2) | |
if w1==w2 | |
region1 = AAL_labels{w1}; | |
fprintf(filey,'%s\n',[ region1 ',' num2str(mean_weights(w1,w2)) ]); | |
end | |
end | |
end | |
fclose(filey); | |
% Write iter region weights (edges) to file | |
filey = fopen('brain_region_edges.csv','w'); | |
for w1=1:size(mean_weights,1) | |
for w2=1:size(mean_weights,2) | |
if w1~=w2 | |
region1 = AAL_labels{w1}; | |
region2 = AAL_labels{w2}; | |
if mean_weights(w1,w2) ~= 0 | |
fprintf(filey,'%s\n',[ region1 ' (fc) ' region2 ' = ' num2str(mean_weights(w1,w2)) ]); | |
end | |
end | |
end | |
end | |
fclose(filey); | |
else | |
error('Invalid export type - must specify all or region for fourth argument!'); | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function gephiExport(links,weights,threshold) | |
% This function will take a list of links and weights, and create a file to | |
% import into gephi for further graph analysis | |
% links is an n by 2 list of connections, each a voxel id | |
% weights is a correlation value corresponding to each link set | |
% You must load AAL_labels, a cell of length 116 with region names, and | |
% aalflat is a flattened AAL atlas, the same size as the original data | |
% Get unique links and weights | |
linksA = links(weights>threshold,:); | |
weightsA = weights(weights>threshold); | |
[linksA,ia,~] = unique(linksA,'rows'); | |
weightsA = weightsA(ia); | |
% Now we need to get rid of duplicates, and also when i==j | |
linksAinv = [ linksA(:,2) linksA(:,1) ]; | |
dups = (linksA == linksAinv); | |
linksA(dups(:,1),:) = []; | |
weightsA(dups(:,1)) = []; | |
% For each region, calculate mean weight | |
region_weights = cell(116,116); | |
region_vox = zeros(116,116); | |
for i=1:116 | |
for j=1:116 | |
region_weights{i,j} = []; | |
end | |
end | |
for l=1:length(linksA) | |
nodes = sort(aalflat(linksA(l,:))); | |
% If we have the same region | |
region_vox(nodes(1),nodes(2)) = region_vox(nodes(1),nodes(2)) + 1; | |
region_weights{nodes(1),nodes(2)} = [ region_weights{nodes(1),nodes(2)}; weightsA(l) ]; | |
end | |
% Calculate mean inter and intra region weights | |
mean_weights = zeros(116,116); | |
for i=1:116 | |
for j=1:116 | |
weightset = region_weights{i,j}; | |
mean_weights(i,j) = mean(weightset(~isnan(weightset))); | |
end | |
end | |
mean_weights(isnan(mean_weights)) = 0; | |
% Now write to graph wiz export file | |
filey = fopen('brain_region.gexf','w'); | |
fprintf(filey,'%s\n%s\n%s\n%s\n','<gexf xmlns:viz="http:///www.gexf.net/1.1draft/viz" xmlns="http://www.gexf.net/1.1draft" version="1.1">','<meta lastmodifieddate="2010-03-03+23:44">','<creator>Gephi 0.7</creator>','</meta>','<graph defaultedgetype="undirected" idtype="string" type="static">'); | |
fprintf(filey,'%s\n',[ '<nodes count="' num2str(length(AAL_labels)) '">' ]); | |
for r1=1:116 | |
fprintf(filey,'%s\n',[ 'node id="' num2str(i) '" label="' AAL_labels{r1} '">' ]); | |
end | |
fprintf(filey,'%s\n%s\n','</nodes>',[ '<edges count="' num2str(length(links)) '">' ]); | |
edge_count = 0; | |
for r1=1:116 | |
for r2=1:116 | |
region1 = AAL_labels{r1}; | |
region2 = AAL_labels{r2}; | |
if mean_weights(r1,r2) ~= 0 | |
fprintf(filey,'%s\n',[ '<edge id="' num2str(edge_count) '" source="' num2str(r1) '" target="' num2str(r2) '" weight="' num2str(mean_weights(r1,r2)) '"/>' ]); | |
edge_count = edge_count + 1; | |
end | |
end | |
end | |
fprintf(filey,'%s\n%s\n%s\n','</edges>','</graph>','</gexf>'); | |
fclose(filey); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment