-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPostAn_AnalyseStructures_BATCH.m
266 lines (205 loc) · 9.33 KB
/
PostAn_AnalyseStructures_BATCH.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%.
% 2017-09-06 Romain Laine rfl30@cam.ac.uk
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Initialization
clear all;
close all;
clc
warning('off','all');
% User-set parameters -----------------------------------------------------
Save_results = 1;
FileToken = 'recon.mrc'; % to recognize the files to analyse
PixelSize = 32; % nm
% ELM parameters
hough_low = 1;
hough_high = 10;
segmentation = 10;
border = 5;
seed = 7;
hough_sensitivity = 0.85;
% Rod analysis
Resolution = 90; % nm
% -------------------------------------------------------------------------
disp('------------------------------');
Default_path = 'C:\Users\rfl30\DATA raw\SIM data\';
% Read in image file which you would like to predict classes
folder_name = uigetdir(Default_path, 'Please select a folder...');
listing = GetListDataset( folder_name, FileToken );
[Filepath,~,~] = fileparts(listing{1});
disp('File path selected:');
disp(Filepath);
[Filename, Filepath] = uigetfile('*.xlsx','Select a .xlsx file with labels (annotated or predicted)', folder_name);
tic
disp('------------------------------');
disp('Reading data from spreadsheet...');
disp(fullfile(Filepath, Filename));
[Learners_values, annotation, ~] = xlsread(fullfile(Filepath, Filename),1);
Learners_names = annotation(1,1:end-1);
annotation = annotation(2:end,end);
Learners_values = Learners_values(:,1:end);
toc
Class_list = unique(annotation);
n_classes = length(Class_list);
%% START THE BATCH LOOP ---------------------------------------------------
n_virus_inclass = zeros(length(listing), n_classes);
AllFilaLength = [];
AllFilaPL = [];
AllSmallFilaLength = [];
AllLargeSphRadius = [];
AllLargeSphEquiRadius = [];
AllSmallSphRadius = [];
AllUnknownRadius = [];
AllRodWidth = [];
AllRodLength = [];
hFigBoxes = figure('name','Boxed image and mask');
hFigFilamentous = figure('name', 'Filamentous', 'units', 'normalized','outerposition', [0.1 0.1 0.4 0.4]);
hFigSmallFill = figure('name', 'Small filamentous', 'units', 'normalized','outerposition', [0.3 0.1 0.4 0.4]);
hFigLargeSph = figure('name', 'Large spherical', 'units', 'normalized','outerposition', [0.5 0.1 0.4 0.4]);
hFigRod = figure('name', 'Rod', 'units', 'normalized','outerposition', [0.7 0.1 0.4 0.4]);
if Save_results == 1
mkdir(folder_name,'Post-classification analysis');
for i = 1:n_classes
mkdir([folder_name,'\Post-classification analysis'], Class_list{i});
end
end
t0 = tic;
current_label_position = 0;
%%
for f = 1:length(listing)
%%
disp('------------------------------------------------------------------------------------------');
disp(['Opening file (',num2str(f),'/',num2str(length(listing)),')']);
FullFileName = listing{f};
disp(FullFileName);
imvirus = UseBF_openSIMage( FullFileName );
disp('------------------------------');
disp('Loading the labelled image...');
[Filepath, Filename_wo_extension,~] = fileparts(FullFileName);
labelled_image = double(imread( fullfile(Filepath, [Filename_wo_extension,'_labelled image.tif'])));
n_objects = max(labelled_image (:));
%%
for i = 1:n_objects
%%
current_label_position = current_label_position + 1;
current_label = annotation(current_label_position);
mask = ismember(labelled_image, i);
[ Boxed_image, Boxed_mask ] = GetBoxedObject( imvirus, mask, 0);
set(0, 'CurrentFigure', hFigBoxes);
subplot(1,2,1);
imshow(Boxed_image, []);
title(current_label);
subplot(1,2,2);
imshow(Boxed_mask, []);
title(current_label);
disp('---------------------------');
disp(current_label);
if strcmp(current_label, 'Filamentous')
AllFilaLength = cat(1, AllFilaLength, GeodesicDistance( Boxed_mask, hFigFilamentous )*PixelSize);
AllFilaPL = cat(1, AllFilaPL, PersistenceLength(Boxed_mask)*PixelSize);
if Save_results == 1
filenameSave = [folder_name,'\Post-classification analysis\',current_label{1},'\', Filename_wo_extension,'_Object', num2str(i),'.png' ];
saveas(hFigFilamentous, filenameSave, 'png');
end
elseif strcmp(current_label, 'Small filamentous')
AllSmallFilaLength = cat(1, AllSmallFilaLength, GeodesicDistance(Boxed_mask, hFigSmallFill)*PixelSize);
if Save_results == 1
filenameSave = [folder_name,'\Post-classification analysis\',current_label{1},'\', Filename_wo_extension,'_Object', num2str(i),'.png' ];
saveas(hFigSmallFill, filenameSave, 'png');
end
elseif strcmp(current_label, 'Large spherical')
AllLargeSphRadius = cat(1, AllLargeSphRadius, ELM_spherical_analysis(Boxed_image, hough_low, hough_high, segmentation, border, seed, hough_sensitivity, hFigLargeSph)*PixelSize);
if Save_results == 1
filenameSave = [folder_name,'\Post-classification analysis\',current_label{1},'\', Filename_wo_extension,'_Object', num2str(i),'.png' ];
saveas(hFigLargeSph, filenameSave, 'png');
end
AllLargeSphEquiRadius = cat(1, AllLargeSphEquiRadius, sqrt((sum(double(Boxed_mask(:)))/pi))*PixelSize);
elseif strcmp(current_label, 'Small spherical')
AllSmallSphRadius = cat(1, AllSmallSphRadius, sqrt((sum(double(Boxed_mask(:)))/pi))*PixelSize);
elseif strcmp(current_label, 'Unknown')
AllUnknownRadius = cat(1, AllUnknownRadius, sqrt((sum(double(Boxed_mask(:)))/pi))*PixelSize);
elseif strcmp(current_label, 'Rod')
[ DiameterFit, IntensityFit, LengthFit ] = RodAnalysis( Boxed_image, Boxed_mask, PixelSize, Resolution, hFigRod );
AllRodWidth = cat(1, AllRodWidth, DiameterFit);
AllRodLength = cat(1, AllRodLength, LengthFit);
if Save_results == 1
filenameSave = [folder_name,'\Post-classification analysis\',current_label{1},'\', Filename_wo_extension,'_Object', num2str(i),'.png' ];
saveas(hFigRod, filenameSave, 'png');
end
end
end
end
warning('on','all');
%%
figure('Color', 'white', 'name', 'Results: Filamentous');
subplot(1,3,1);
histogram(AllFilaLength,50);
xlabel 'Filamentous length (nm)'
title 'Filamentous length'
subplot(1,3,2);
histogram(AllFilaPL,50);
xlabel 'Persistence length (nm)'
title 'Persistence length'
% subplot(1,3,3);
% scatter(AllFilaLength, AllFilaPL)
% xlabel 'Length (nm)'
% ylabel 'Persistence length (nm)'
figure('Color', 'white', 'name', 'Results: Small filamentous');
histogram(AllSmallFilaLength,50);
xlabel 'Filamentous length (nm)'
title 'Small filamentous'
AllLargeSphRadius_hist = AllLargeSphRadius((AllLargeSphRadius > 0) & (AllLargeSphRadius < 500));
figure('Color', 'white', 'name', 'Results: Spherical');
subplot(1,2,1);
histogram(AllLargeSphRadius_hist,50);
xlabel 'Spherical radius (nm)'
xlim([20 250]);
subplot(1,2,2);
% figure('Color', 'white', 'name', 'Small spherical');
histogram(AllSmallSphRadius,50);
xlabel 'Spherical radius (nm)'
figure('Color', 'white', 'name', 'Results: Unknown');
histogram(AllUnknownRadius,50);
xlabel 'Equivalent radius (nm)'
figure('Color', 'white', 'name', 'Results: Rod');
subplot(1,3,1);
histogram(AllRodWidth,50);
xlabel 'Width (nm)'
subplot(1,3,2);
% figure('Color', 'white', 'name', 'Rod length');
histogram(AllRodLength,50);
xlabel 'Length (nm)'
subplot(1,3,3);
% figure('Color', 'white', 'name', 'Rod length');
scatter(AllRodWidth, AllRodLength)
xlabel 'Width (nm)'
ylabel 'Length (nm)'
if Save_results == 1
ParametersNames = cell(1,8);
ParametersNames{1} = 'Filamentous length';
ParametersNames{2} = 'Filamentous persistence length';
ParametersNames{3} = 'Small filamentous length';
ParametersNames{4} = 'Large spherical radius';
ParametersNames{5} = 'Large spherical equivalent radius';
ParametersNames{6} = 'Small spherical radius';
ParametersNames{7} = 'Unknown equivalent radius';
ParametersNames{8} = 'Rod length';
ParametersNames{9} = 'Rod diameter';
filenameSave = [folder_name,'\Post-classification analysis\','PostClassificationResults.xlsx' ];
xlswrite(filenameSave, ParametersNames,1,'A1');
xlswrite(filenameSave, AllFilaLength,1,'A2');
xlswrite(filenameSave, AllFilaPL,1,'B2');
xlswrite(filenameSave, AllSmallFilaLength,1,'C2');
xlswrite(filenameSave, AllLargeSphRadius,1,'D2');
xlswrite(filenameSave, AllLargeSphEquiRadius,1,'E2');
xlswrite(filenameSave, AllSmallSphRadius,1,'F2');
xlswrite(filenameSave, AllUnknownRadius,1,'G2');
if ~isempty(AllRodLength)
xlswrite(filenameSave, AllRodLength,1,'H2');
end
if ~isempty(AllRodWidth)
xlswrite(filenameSave, AllRodWidth,1,'I2');
end
end
disp('----------------------------------------------------------------------------');
disp('All done.');
toc(t0);