-
Notifications
You must be signed in to change notification settings - Fork 0
/
genViewerFolder.asv
198 lines (182 loc) · 6.89 KB
/
genViewerFolder.asv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
function genViewerFolder(inputFile,varargin)
%% Parse input.
p = inputParser;
p.addOptional('inputFile',[],@ischar);
p.addParameter('OutputFolder',[],@ischar);
p.addParameter('Resample',[],@isnumeric);
p.parse(varargin{:});
Inputs = p.Results;
%% Check if output/ input file provided.
if isempty(Inputs.inputFile)
[file,path] = uigetfile('.mat','Open Reconstruction Viewer file');
if path==0
return
end
Inputs.inputFile = fullfile(path,file);
end
%% Get output Folder
if isempty(Inputs.OutputFolder)
[path,~,~] = fileparts(Inputs.inputFile);
[path] = uigetdir(path,'Save as..');
if path==0
return
end
Inputs.OutputFolder = fullfile(path);
end
%% Make output folders.
swcFolder = fullfile(Inputs.OutputFolder,'swcs');
meshFolder = fullfile(Inputs.OutputFolder,'meshes');
renderFolder = fullfile(Inputs.OutputFolder,'renders');
if ~isfolder(swcFolder), mkdir(swcFolder); end
if ~isfolder(meshFolder), mkdir(meshFolder); end
if ~isfolder(renderFolder), mkdir(renderFolder); end
%% Load session.
fprintf('\nLoading Session..');
load(Inputs.inputFile);
%% Get anatomy Info.
fprintf('\nLoading Anatomy Info..');
%Folder location
[mainFolder,~,~] = fileparts(which('reconstructionViewer'));
load(fullfile(mainFolder,'+allen','allenMesh.mat'));
%% get visible Neurons.
indVis = strcmpi({Session.Neurons.Visible},'on');
Session.Neurons = Session.Neurons(indVis);
names = {Session.Neurons.Name};
names = cellfun(@(x) x(1:6),names,'UniformOutput',false);
[names,ind,~] = unique(names);
neurons = struct('id','','color',[]);
if size(names,2)>50
poolobj = gcp('nocreate');
if (isempty(poolobj))
parpool('local',3)
end
parfor i =1:size(names,2)
processNeuron(Session,names,ind,i,Inputs,swcFolder)
end
else
for i =1:size(names,2)
processNeuron(Session,names,ind,i,Inputs,swcFolder)
end
end
%% neuron info.
for i =1:size(names,2)
cNeuron = ind(i);
neurons(i).id = names{i};
neurons(i).color = Session.Neurons(cNeuron).Color;
end
%% get Area's
ind = find(Session.visibleStructures);
actStruct = find(Session.activeStructures);
indAct = ismember(actStruct,ind);
colors = cat(1,Session.structProps.FaceColor);
colors = colors(indAct,:);
ind = [ind;{allenMesh.a ]; %add whole brain just in case.
anatomy = struct();
counter = 0;
for iArea = 1:length(ind)
fprintf('\nWriting Area %i\\%i',iArea,length(ind));
cArea = ind(iArea);
if cArea~=712
counter = counter+1;
anatomy(counter).acronym = allenMesh(cArea).acronym;
anatomy(counter).color = colors(iArea,:);
end
% write obj.
dimOrder = [1,2,3];
% get Mesh Info.
v = allenMesh(cArea).v(:,dimOrder);
vn = allenMesh(cArea).vn(:,dimOrder);
f = allenMesh(cArea).f;
% Create result file.
nameFile = sprintf('%s_%i',allenMesh(cArea).acronym,allenMesh(cArea).id);
nameFile = strrep(nameFile,'\','-');
nameFile = strrep(nameFile,'/','-');
nameFile = strrep(nameFile,',','.');
fOut = fopen(fullfile(meshFolder,[nameFile,'.obj']),'w');
% Write obj.
outputStr = '';
outputStr = [outputStr, sprintf('# Brain compartment 3D surface mesh\n')];
outputStr = [outputStr,sprintf('# Compartment name: ''%s'' \n', allenMesh(cArea).name)];
outputStr = [outputStr,sprintf('# Compartment acronym: ''%s'' \n', allenMesh(cArea).acronym)];
outputStr = [outputStr,sprintf('# Compartment id: %i \n', allenMesh(cArea).id)];
outputStr = [outputStr,sprintf('# parent id: %i \n', allenMesh(cArea).parent_id)];
outputStr = [outputStr,sprintf('# hierarchy path: %s \n', allenMesh(cArea).hierarchy_path)];
outputStr = [outputStr,sprintf('# Compartment color: 0x%s \n', allenMesh(cArea).color)];
% outputStr = [outputStr,sprintf('# graph order: %i \n', allenMesh(cArea).graph_order)];
% outputStr = [outputStr,sprintf('# atlas id: %i \n', allenMesh(cArea).atlas_id)];
outputStr = [outputStr,sprintf('# Im fine!\n')];
outputStr = [outputStr,sprintf('# How are you?\n')];
outputStr = [outputStr,sprintf('# List of [x, y, z, w] geometric vertices:\n')];
%v
outputStr = [outputStr,sprintf('v %.6f %.6f %.6f 1.0\n',v')];
%vn
outputStr = [outputStr,sprintf('vn %.6f %.6f %.6f\n',vn')];
%f
f = repmat(f,1,2);
f = f(:,[1,4,2,5,3,6]);
outputStr = [outputStr,sprintf('f %i//%i %i//%i %i//%i\n',f')];
fprintf(fOut,outputStr);
fclose(fOut);
end
%% Join
data = struct('neurons',neurons,...
'anatomy',anatomy);
%% Write session info file.
text = jsonencode(data);
fid = fopen(fullfile(Inputs.OutputFolder,'session_info.json'),'w');
fprintf(fid,text);
fclose(fid);
%% Copy staging script.
[mainFolder,~,~] = fileparts(which('genViewerFolder'));
copyfile(fullfile(mainFolder,'Stage Brain.py'),fullfile(Inputs.OutputFolder));
copyfile(fullfile(mainFolder,'renderMacro.py'),fullfile(Inputs.OutputFolder));
end
function processNeuron(Session,names,ind,i,Inputs,swcFolder)
fprintf('\nWriting Neuron %i\\%i',i,size(names,2));
% Settings for neuron.
cNeuron = ind(i);
name = names{i};
color = Session.Neurons(cNeuron).Color;
% Write swcs.
allFields = {Session.Neurons.Name};
indNeuron = cellfun(@(x) strcmpi(x(1:6),name),allFields,'UniformOutput',false);
indNeuron = find([indNeuron{:}]);
% go through matching neurons
for iNeuron = 1:size(indNeuron,2)
cNeuron = indNeuron(iNeuron);
cName = allFields{cNeuron}(1:6);
type = allFields{cNeuron}(end);
% make swc.
swc = Session.Neurons(cNeuron).Nodes;
swc = [ swc(:,5),zeros(size(swc,1),1),swc(:,1:3),ones(size(swc,1),1),swc(:,4)];
% Resample if requested.
if ~isempty(Inputs.Resample)
[tree] = quickLoadTreeSwc(swc);
tree = resample_tree(tree,Inputs.Resample);
idpar0 = idpar_tree (tree, '-0'); % vector containing index to direct parent
idpar0 (idpar0 == 0) = -1;
swc = [(1 : length(tree.X))' zeros(length(tree.X),1) tree.X tree.Y tree.Z tree.D idpar0];
end
% make node type list.
swc(1,2) = 1;
[N,~] = histcounts(swc(:,7),[1:size(swc,1)]);
swc(N>1,2) = 5;
swc(N==0,2) = 6;
% generate output name
if strcmpi(type,'a')
typeName = 'axon';
else
typeName = 'dendrite';
end
outputFile = fullfile(swcFolder,...
sprintf('%s_%s.swc',cName,typeName));
fid = fopen(outputFile,'w');
% Header.
fprintf(fid,'# ORIGINAL_SOURCE MouseLight Database');
fprintf(fid,'\n# OFFSET 0 0 0');
fprintf(fid,'\n# COLOR %.4f,%.4f,%.4f',color);
fprintf(fid,'\n# GENERATED ON %s',datestr(now,'yy/mm/dd HH:MM'));
fprintf(fid,'\n%i %i %.6f %.6f %.6f %.6f %i',swc');
fclose(fid);
end
end