-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMannWhitney_test.m
440 lines (316 loc) · 15.2 KB
/
MannWhitney_test.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
function [] = ...
MannWhitney_test(expInfo,genotypes,clean_omma_centroids,delaunay_neighbors,...
target_genotypes, distance_cutoff, measurement_type, genotype_labels)
%--------------------------------------------------------------------------
% check to make sure we only have two target genotypes
%--------------------------------------------------------------------------
if length(target_genotypes) ~= 2
disp('\n')
disp("Please make sure you have selected two target genotypes for comparison")
return
end
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
% MAKE MEASUREMENTS
% first find COV facter per ommatidia, aggregate by genotype (combine
% ommatidia from all eyes of same genptype), then average
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
num_meas = length(clean_omma_centroids);
disp('\n')
disp("Measuring COV of inter-ommatidial-distance and aggregating across images")
disp("based on genotype. Sample: ")
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
% find ommatidia within distance cutoff
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
% pass to new variable that we will modify based on distance_cutoff
omma_for_analysis = {};
for t = 1:num_meas
omma_count = 0;
% find the center of mass for the segmented ommatidia of the current
% image
center_x = sum(clean_omma_centroids{t}(:,1))/length(clean_omma_centroids{t}(:,1));
center_y = sum(clean_omma_centroids{t}(:,2))/length(clean_omma_centroids{t}(:,2));
for j = 1:length(clean_omma_centroids{t})
term1 = (clean_omma_centroids{t}(j,1) - center_x)^2;
term2 = (clean_omma_centroids{t}(j,2) - center_y)^2;
dist = sqrt(term1 + term2);
if dist < distance_cutoff
omma_count = omma_count + 1;
omma_for_analysis{t}(omma_count) = j;
end
end
end
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
% measurements per ommatidia and then per eye
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
boundary_cent = cell(length(clean_omma_centroids),1);
% initialize cell array for recording COV factor of omma
all_DATA = cell(length(clean_omma_centroids),1);
dist_COV_mean = zeros(length(clean_omma_centroids),1);
% loop thru eyes
for t = 1:length(clean_omma_centroids)
% display counter
if t > 1
for j=0:log10(t-1)
fprintf('\b'); % delete previous counter display
end
fprintf('%d',t)
end
% boundary centroids for current time
boundary_cent{t} = unique(boundary(clean_omma_centroids{t},0.8));
% redefine centroid list as two separate lists, one for x and one for y
x = clean_omma_centroids{t}(:,1);
y = clean_omma_centroids{t}(:,2);
%----------------------------------------------------------------------
% find average inter-ommatidial distance
%----------------------------------------------------------------------
all_distances = [];
dist_count = 0;
for j = 1:size(x,1)
% make sure its not a boundary point and within distance cutoff
if not(ismember(j,boundary_cent{t})) && ismember(j,omma_for_analysis{t})
for jj = 1:length(delaunay_neighbors{t}{j})
% store centroid components for current center position and
% current neighbor
curr_x = x(j);
curr_y = y(j);
neigh_x = x(delaunay_neighbors{t}{j}(jj));
neigh_y = y(delaunay_neighbors{t}{j}(jj));
% euclidean distance b/w two points
temp_D = sqrt( ((neigh_x - curr_x)^2) + ((neigh_y - curr_y)^2) );
dist_count = dist_count + 1;
all_distances(dist_count) = temp_D;
end
end
end
% take mean distance
mean_distance = mean(all_distances);
%----------------------------------------------------------------------
% apply metric for measuring disorder
%----------------------------------------------------------------------
% initialize list for storing COV for omma in current eye
temp_meas = nan(length(x),1);
% loop through ommatidia - the identity of ommatidia is defined by their
% position within 'clean_omma_centroids'
for j = 1:size(x,1)
% make sure its not a boundary point and within distance cutoff
if not(ismember(j,boundary_cent{t})) && ismember(j,omma_for_analysis{t})
%--------------------------------------------------------------
%--------------------------------------------------------------
%
%
% variance in distance
%
%
%--------------------------------------------------------------
%--------------------------------------------------------------
temp_distances = nan(length(delaunay_neighbors{t}{j}),1);
%--------------------------------------------------------------
% loop through current neighbors, calculate distance between
% center point and each neighbor, and collect these
% measurements in a list
%--------------------------------------------------------------
for jj = 1:length(delaunay_neighbors{t}{j})
% store centroid components for current center position and
% current neighbor
curr_x = x(j);
curr_y = y(j);
neigh_x = x(delaunay_neighbors{t}{j}(jj));
neigh_y = y(delaunay_neighbors{t}{j}(jj));
% euclidean distance b/w two points
temp_D = sqrt( ((neigh_x - curr_x)^2) + ((neigh_y - curr_y)^2) );
% store in list of distances for current center point
temp_distances(jj) = temp_D;
end
%--------------------------------------------
% calculate index of dispersion (COV) or min/max
%--------------------------------------------
% calculate variance
if strcmp('COV',measurement_type)
temp_meas(j) = std(temp_distances) / mean(temp_distances);
elseif strcmp('max_min',measurement_type)
if not(isempty(temp_distances))
temp_meas(j) = (max(temp_distances) - min(temp_distances)) ...
/ mean_distance;
end
else
fprintf('Measurement type not properly specified')
end
end
end
%------------------------------------
% record COV of triangle edge length
%------------------------------------
all_DATA{t} = temp_meas;
end
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
% determine genotype for each image
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
Directory = dir(strcat(expInfo.filepath_input,'*.tif'));
%--------------------------------------------------------------------------
% create vectors and cell arrays that record filename, genotype, and
% plotting color for each image
%--------------------------------------------------------------------------
namestr = cell(num_meas,1);
genotype = cell(num_meas,1);
for t = 1:num_meas
% parse out filename
namestr{t} = Directory(t).name;
namestr{t} = namestr{t}(1:end-4); % trim off .tif
namestr{t} = strrep(namestr{t},'_',' '); % remove any '_' characters
% parse out the genotype specifically
genotype{t} = strsplit(namestr{t});
genotype{t} = genotype{t}{1};
end
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
% aggregate measurements based on genotype
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
all_DATA_per_geno = cell(1,length(genotypes));
% loop through images
for t = 1:num_meas
% find genotype of current image
[~,LOCB] = ismember(genotype{t},genotypes);
all_DATA_per_geno{LOCB} = [all_DATA_per_geno{LOCB},all_DATA{t}'];
end
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
% stats and prep for plotting (sort in order of ascending mean
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
% find mean and std of each genotype in order to sort measurements in order
% to ascending mean COV
temp_mean_DATA = [];
for t = 1:length(genotypes)
temp_mean_DATA(t) = mean(all_DATA_per_geno{t},'omitnan');
end
%--------------------------------------------------------------------------
% sort by mean fano factor
%--------------------------------------------------------------------------
sort_order = zeros(length(genotypes),2);
sort_order(:,1) = [1:length(genotypes)];
sort_order(:,2) = temp_mean_DATA;
sort_order = sortrows(sort_order,2);
sorted_aggregate_DATA = cell(length(genotypes),1);
sorted_genotypes = cell(length(genotypes),1);
for t = 1:length(genotypes)
sort_ind = sort_order(t,1);
sorted_genotypes{t} = genotypes{sort_ind};
sorted_genotype_labels(t) = genotype_labels(sort_ind);
sorted_aggregate_DATA{t} = all_DATA_per_geno{sort_ind};
end
% convert list of sorted genotypes to string array
sorted_genotypes = string(sorted_genotypes);
%--------------------------------------------------------------------------
% find mean and std of each genotype
%--------------------------------------------------------------------------
%---------------
% sorted by mean
%---------------
sorted_mean_DATA = [];
sorted_std_DATA = [];
for t = 1:length(genotypes)
sorted_mean_DATA(t) = mean(sorted_aggregate_DATA{t},'omitnan');
sorted_std_DATA(t) = std(sorted_aggregate_DATA{t},'omitnan');
end
%---------
% unsorted
%---------
mean_DATA = [];
std_DATA = [];
for t = 1:length(genotypes)
mean_DATA(t) = mean(all_DATA_per_geno{t},'omitnan');
std_DATA(t) = std(all_DATA_per_geno{t},'omitnan');
end
%--------------------------------------------------------------------------
% transfer cells into matrix
%--------------------------------------------------------------------------
%--------------------------------
% find max length of measurements
%--------------------------------
max_length = 0;
for t = 1:length(genotypes)
% find max length
curr_length = length(all_DATA_per_geno{t});
if curr_length > max_length
max_length = curr_length;
end
end
%---------------
% sorted by mean
%---------------
sorted_DATA_matrix_form = nan(max_length,length(genotypes));
for t = 1:length(genotypes)
sorted_DATA_matrix_form(1:length(sorted_aggregate_DATA{t}),t) = sorted_aggregate_DATA{t};
end
%---------
% unsorted
%---------
DATA_matrix_form = nan(max_length,length(genotypes));
for t = 1:length(genotypes)
DATA_matrix_form(1:length(all_DATA_per_geno{t}),t) = all_DATA_per_geno{t};
end
%--------------------------------------------------------------------------
% pull out target genotypes for comparison
%--------------------------------------------------------------------------
count = 0;
target_meas = {};
for j = 1:length(genotypes)
% check if current genotype in 'sorted_genotypes' is part of our list
% of genotypes to plot
[LIA,~] = ismember(genotypes(j),target_genotypes);
if LIA
count = count + 1;
target_meas{count} = DATA_matrix_form(:,j);
end
end
%--------------------------------------------------------------------------
% check to make sure we have successfully found two genotypes to compare
%--------------------------------------------------------------------------
if length(target_meas) ~= 2
disp('\n')
disp("Please double check you have selected two genotypes for comparison from the variable 'genotype_code'. They should be spelled them the same as they are in 'genotype_code'")
return
end
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
% perform statistical comparison
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
[p,h] = ranksum(target_meas{1},target_meas{2});
if h == 1
decision = "significant";
else
decision = "insignificant";
end
disp('\n')
disp(strcat("The difference between genotypes ",target_genotypes(1)," and ",target_genotypes(2)," is ",decision," with a p-value of ",num2str(p)))
disp(strcat("The -log10 p-value is: ", num2str(-log10(p))))