forked from adamltyson/foci2D
-
Notifications
You must be signed in to change notification settings - Fork 1
/
foci2D.m
172 lines (140 loc) · 5.24 KB
/
foci2D.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
function foci2D
%% Adam Tyson | 2018-05-03 | adam.tyson@icr.ac.uk
% loads an .lsm file, segments the first channel (nuclear), and estimates
% cell boundaries
% measures channel 2 on a per cell basis
%% TO DO
% normalise output
vars=getVars;
tic
cd(vars.directory)
files=dir('*.lsm'); % all tif's in this folder
numImages=length(files);
imCount=0;
f = waitbar(0,'1','Name','Analysing images...');
% prep for .csv export
if strcmp(vars.save, 'Yes')
valMeans{1,1}='Image/variable';
valMeans{1,2}='Mean foci number per cell';
valMeans{1,3}='Mean total foci area per cell';
valMeans{1,4}='Mean total foci intensity per cell';
valMeans{1,5}='Number of cells';
end
for file=files' % go through all images
imCount=imCount+1;
waitbar((imCount-1)/numImages,f,strcat("Analysing Image: ",...
num2str(imCount)))
rawFile{imCount}=file.name;
disp(['Processing - ' rawFile{imCount}])
% load - evalc to supress bf output
evalc('[data, ~, ~]=lsmPrep2chan(rawFile{imCount})');
ch1Max=max(data.channel1,[],3);
ch2Max=sum(data.channel2,3);
% segment nuclei and find edges
[labelDAPI, labelCell] = nucSegBorders(ch1Max, vars);
% measure other channel
[areaColoc{imCount}, intenColoc{imCount}, numFoci{imCount}] = ...
fociPerCell(ch2Max, labelCell, labelDAPI, vars, rawFile{imCount});
if strcmp(vars.save, 'Yes')
valMeans{imCount+1,1}=rawFile{imCount};
valMeans{imCount+1,2}=mean(numFoci{imCount});
valMeans{imCount+1,3}=mean(areaColoc{imCount});
valMeans{imCount+1,4}=mean(intenColoc{imCount});
valMeans{imCount+1,5}=max(labelDAPI(:));
end
end
% save results
if strcmp(vars.save, 'Yes')
disp('Saving Results')
save_raw_res(rawFile, numFoci, intenColoc, areaColoc, imCount, vars)
save_summary_res(valMeans, vars)
end
delete(f)
toc
end
%% Internal functions
function save_summary_res(valMeans, vars)
valMeans_Table=cell2table(valMeans);
writetable(valMeans_Table, ['summaryResults_' vars.stamp '.csv'],...
'WriteVariableNames', 0)
end
function save_raw_res(rawFile, numFoci, intenColoc,...
areaColoc, imCount, vars)
% tidy up
for i=1:imCount
lengths(i)=length(numFoci{i});
end
numFociReshape=NaN(imCount+1, max(lengths)+1);
intenColocReshape=numFociReshape;
areaColocReshape=numFociReshape;
for i=1:imCount
numFociReshape(i+1,2:length(numFoci{i})+1)=numFoci{i};
intenColocReshape(i+1,2:length(intenColoc{i})+1)=intenColoc{i};
areaColocReshape(i+1,2:length(areaColoc{i})+1)=areaColoc{i};
end
numFociCell=num2cell(numFociReshape);
intenColocCell=num2cell(intenColocReshape);
areaColocCell=num2cell(areaColocReshape);
numFociCell{1,1}='Image';
intenColocCell{1,1}='Image';
areaColocCell{1,1}='Image';
for cellnum=1:max(lengths)
numFociCell{1, cellnum+1}=strcat("Cell_", num2str(cellnum));
intenColocCell{1, cellnum+1}=strcat("Cell_", num2str(cellnum));
areaColocCell{1, cellnum+1}=strcat("Cell_", num2str(cellnum));
end
for image=1:imCount
numFociCell{image+1,1}=rawFile{image};
intenColocCell{image+1,1}=rawFile{image};
areaColocCell{image+1,1}=rawFile{image};
end
fociNum_Table=cell2table(numFociCell);
fociInten_Table=cell2table(intenColocCell);
fociArea_Table=cell2table(areaColocCell);
writetable(fociNum_Table, ['fociNumbers_' vars.stamp '.csv'],...
'WriteVariableNames', 0)
writetable(fociInten_Table, ['fociTotalInten_' vars.stamp '.csv'],...
'WriteVariableNames', 0)
writetable(fociArea_Table, ['fociTotalArea_' vars.stamp '.csv'],...
'WriteVariableNames', 0)
end
function vars=getVars
vars.directory = uigetdir('', 'Choose directory containing images');
vars.plot = questdlg('Display segmentation? ', ...
'Plotting', ...
'Yes', 'No', 'No');
vars.save = questdlg('Save results as .csv? ', ...
'Saving', ...
'Yes', 'No', 'Yes');
vars.threshQ = questdlg('Specify foci threshold? ', ...
'Saving', ...
'Yes', 'No', 'No');
if strcmp(vars.threshQ, 'Yes')
prompt = {'Foci threshold:'};
dlg_title = 'Analysis variables';
num_lines = 1;
defaultans = {'0.2'};
answer = inputdlg(prompt,dlg_title,num_lines,defaultans);
vars.hardCodeFociThresh=str2double(answer{1});% specificy threshold
else
vars.hardCodeFociThresh=[];
end
prompt = {'Nuclear segmentation threshold (a.u.):',...
'Foci segmentation threshold (a.u.)::',...
'Maximum hole size:',...
'Largest object to remove:',...
'Smoothing sigma (nucleus):',...
'Smoothing sigma (foci):'};
dlg_title = 'Analysis variables';
num_lines = 1;
defaultans = {'1','1','500','1000', '5', '1'};
answer = inputdlg(prompt,dlg_title,num_lines,defaultans);
vars.threshScaleCh1=str2double(answer{1});%change sensitivity of threshold
vars.threshScaleCh2=str2double(answer{2});%change sensitivity of threshold
vars.holeFill=str2double(answer{3});% largest hole to fill
vars.noiseRem=str2double(answer{4}); % smallest obj to remove
vars.filtSigmaCh1=str2double(answer{5});% smoothing kernel
vars.filtSigmaCh2=str2double(answer{6});% smoothing kernel
vars.stamp=num2str(fix(clock)); % date and time
vars.stamp(vars.stamp==' ') = '';%remove spaces
end