This repository has been archived by the owner on Aug 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.m
178 lines (139 loc) · 4.63 KB
/
main.m
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
function [] = main()
if ~isdeployed
disp('adding paths');
addpath(genpath('/N/soft/rhel7/spm/8')) %spm needs to be loaded before vistasoft as vistasoft provides nanmean that works
addpath(genpath('/N/u/brlife/git/encode'))
addpath(genpath('/N/u/brlife/git/jsonlab'))
addpath(genpath('/N/u/brlife/git/vistasoft'))
addpath(genpath('/N/u/brlife/git/afq'))
end
config = loadjson('config.json')
% Load the track file
wbfg = dtiImportFibersMrtrix(config.track, .5);
disp('Classify the major tracts from all the fascicles');
[fg_classified,~,classification]= AFQ_SegmentFiberGroups(fullfile(config.dtiinit, 'dti/dt6.mat'), wbfg, [], [], config.useinterhemisphericsplit);
disp('done segmenting. outputting tracts.json');
tracts = fg2Array(fg_classified);
clear fg
mkdir('tracts');
% Make colors for the tracts
cm = parula(length(tracts));
for it = 1:length(tracts)
tract.name = tracts(it).name;
tract.color = cm(it,:);
%pick randomly up to 1000 fibers (pick all if there are less than 1000)
fiber_count = min(1000, numel(tracts(it).fibers));
tract.coords = tracts(it).fibers(randperm(fiber_count));
all_tracts(it).name = tracts(it).name;
all_tracts(it).color = cm(it,:);
savejson('', tract, fullfile('tracts',sprintf('%i.json',it)));
all_tracts(it).filename = sprintf('%i.json',it);
clear tract
end
savejson('', all_tracts, fullfile('tracts/tracts.json'));
% Save the results to disk
save('output.mat','fg_classified','classification');
% save product.json information
tract_info = cell(length(fg_classified), 2);
fibercounts = zeros(1, length(fg_classified));
possible_error = 0;
num_left_tracts = 0;
num_right_tracts = 0;
for i = 1 : length(fg_classified)
name = fg_classified(i).name;
num_fibers = length(fg_classified(i).fibers);
fibercounts(i) = num_fibers;
tract_info{i,1} = name;
tract_info{i,2} = num_fibers;
if startsWith(name, 'Right ') || endsWith(name, ' R')
num_right_tracts = num_right_tracts + 1;
else
num_left_tracts = num_left_tracts + 1;
end
if num_fibers < 20
possible_error = 1;
end
end
left_tract_xs = cell(1, num_left_tracts);
right_tract_xs = cell(1, num_right_tracts);
left_tract_ys = zeros([1, num_left_tracts]);
right_tract_ys = zeros([1, num_right_tracts]);
left_tract_idx = 1;
right_tract_idx = 1;
for i = 1 : length(fg_classified)
name = fg_classified(i).name;
num_fibers = length(fg_classified(i).fibers);
basename = name;
if startsWith(basename, 'Right ')
basename = extractAfter(basename, 6);
end
if endsWith(basename, ' R')
basename = extractBefore(basename, length(basename) - 1);
end
if startsWith(basename, 'Left ')
basename = extractAfter(basename, 5);
end
if endsWith(basename, ' L')
basename = extractBefore(basename, length(basename) - 1);
end
if startsWith(name, 'Right ') || endsWith(name, ' R')
right_tract_xs{right_tract_idx} = basename;
right_tract_ys(right_tract_idx) = num_fibers;
right_tract_idx = right_tract_idx + 1;
else
left_tract_xs{left_tract_idx} = basename;
left_tract_ys(left_tract_idx) = num_fibers;
left_tract_idx = left_tract_idx + 1;
end
end
bar1 = struct;
bar2 = struct;
bar1.x = left_tract_xs;
bar1.y = left_tract_ys;
bar1.type = 'bar';
bar1.name = 'Left Tracts';
bar1.marker = struct;
bar1.marker.color = 'rgb(49,130,189)';
bar2.x = right_tract_xs;
bar2.y = right_tract_ys;
bar2.type = 'bar';
bar2.name = 'Right Tracts';
bar2.marker = struct;
bar2.marker.color = 'rgb(204, 204, 204)';
bardata = {bar1, bar2};
barlayout = struct;
barlayout.xaxis = struct;
barlayout.xaxis.tickfont = struct;
barlayout.xaxis.tickfont.size = 8;
barlayout.barmode = 'group';
barplot = struct;
barplot.data = bardata;
barplot.layout = barlayout;
barplot.type = 'plotly';
barplot.name = 'Number of Fibers';
T = cell2table(tract_info);
T.Properties.VariableNames = {'Tracts', 'FiberCount'};
writetable(T, 'output_fibercounts.txt');
% bar graph
% box plot
boxplot = make_plotly_data(fibercounts, 'Fiber Counts', 'Number of Fibers');
product = {barplot, boxplot};
if possible_error == 1
message = struct;
message.type = 'error';
message.msg = 'ERROR: Some tracts have less than 20 streamlines. Check quality of data!';
product = {barplot, boxplot, message};
end
savejson('brainlife', product, 'product.json');
end
%% make plotly plot data
function out = make_plotly_data(values, plotTitle, axisTitle)
out = struct;
out.data = struct;
out.type = 'plotly';
out.name = plotTitle;
out.data.x = values;
out.data.type = 'box';
out.data.name = axisTitle;
out.data = {out.data};
end