-
Notifications
You must be signed in to change notification settings - Fork 0
/
nuclei_counting_uninfected.ijm
272 lines (200 loc) · 6.7 KB
/
nuclei_counting_uninfected.ijm
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
//Fixed parameters:
brightChannel = 2;
nucleiChannel = 1;
//Preferred thresholding method for the nuclei
nucleiThresholdMethod = "Otsu";
backgroundSubtractionBallPixelRadius = 100;
#@ File (label = "Input directory", style = "directory") input
#@ File (label = "Output directory", style = "directory") output
#@ String (label = "File suffix", value = ".dv") suffix
//How circular can a cell before being considered dead?
#@ Double (label="Maximum allowed circularity", style="slider", min=0, max=1.0, stepSize=0.01, value=0.75) maxCircularity
//Nucleus size parameters
#@ Integer (label="Minimum nucleus size (micron^2)", style="slider", min=0, max=200, value=40) nucleiMin
#@ Integer (label="Maximum nucleus size (micron^2)", style="slider", min=0, max=2000, value=1000) nucleiMax
processFolder(input);
// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(input) {
list = getFileList(input);
list = Array.sort(list);
for (i = 0; i < list.length; i++) {
if(File.isDirectory(input + File.separator + list[i]))
processFolder(input + File.separator + list[i]);
if(endsWith(list[i], suffix))
processFile(input, output, list[i]);
}
finish();
}
function processFile(input, output, file) {
run("Bio-Formats Macro Extensions");
Ext.setId(input + File.separator + file);
Ext.getSeriesCount(seriesCount);
promptLabel = "Starting series for file " + file;
startSeries = getNumber("Start at which series?", 1);
for (i = startSeries; i <= seriesCount; i++)
{
processSeries(input,output,file, i);
}
}
function processSeries(input,output,file, series)
{
importString = "open=[" + input + File.separator + file + "] autoscale color_mode=Composite rois_import=[ROI manager] view=Hyperstack stack_order=XYCZT series_"+series;
run("Bio-Formats Importer", importString);
currentImage = getTitle();
numCells = segmentCells(currentImage);
nucleiSegmentation = segmentNuclei(currentImage);
waitForUser;
countNucleiInCells(nucleiSegmentation, numCells);
roiManager("reset");
close(currentImage);
close(nucleiSegmentation);
stopEarly = getBoolean("Stop now?");
if (stopEarly)
{
finish();
exit;
}
}
function segmentNuclei(imageTitle)
{
duplicateChannelAndProjectStack(imageTitle, nucleiChannel,"Median");
nucleiSegmentation = getTitle();
//Subtract the background
backgroundSubtractionString = "rolling=" + backgroundSubtractionBallPixelRadius;
run("Subtract Background...", backgroundSubtractionString);
waitForUser;
thresholdString = nucleiThresholdMethod + " dark no-reset";
setAutoThreshold(thresholdString);
run("Convert to Mask");
run("Close-");
run("Watershed");
waitForUser;
return nucleiSegmentation;
}
function countNucleiInCells(nuclei, numCells) {
selectWindow(nuclei);
for(i = 0; i < numCells; i++)
{
roiManager("Select",i)
anaPartString = "size=[" + nucleiMin + " - " + nucleiMax + "] show=Nothing summarize";
run("Analyze Particles...", anaPartString);
}
}
function segmentCells(imageTitle)
{
duplicateChannelAndFocus(imageTitle,brightChannel);
brightDuplicate = getTitle();
roiManager("Show All");
setTool("polygon");
roiCounter = 0;
waitForUser("Draw an ROI of your choice, then click OK to add. If finished, click OK again. Click away from a completed ROI to cancel it.");
while(is("area")!=0)
{
roiCounter = roiCounter + 1;
roiManager("add");
count = roiManager("count");
roiManager("select", count-1);
roiManager("Rename", "ROI" + roiCounter);
run("Select None");
waitForUser("Draw an ROI of your choice, then click OK to add. If finished, click OK again. Click away from a completed ROI to cancel it.");
}
numCells=roiManager("count");
close(brightDuplicate);
return numCells;
}
function duplicateChannel(imageTitle, channel)
{
selectWindow(imageTitle);
duplicationString = "duplicate channels=" + channel;
run("Duplicate...", duplicationString);
}
//Duplicate single Z-slice from an image to make new image
function duplicateSlice(imageTitle, slice)
{
selectWindow(imageTitle);
duplicationString = "slices=" + slice;
run("Make Substack...", duplicationString);
}
//Extract a single channel from a multi-channel image and then select most in focus Z-slice
function duplicateChannelAndFocus(imageTitle, channel)
{
//Duplicate specific channel from image to extract it as a new image
duplicateChannel(imageTitle, channel);
getDimensions(width, height, channels, slices, frames);
//Measure standard deviations to select most in focus Z-slice and duplicate this
//as new image
if (slices > 1)
{
stackTitle = getTitle();
//Highest standard deviation observed so far
maxStDev = 0;
//Initial slice assumed to be most in focus before beginning loop
focussedSlice = 1;
//Cycle through slices
for (i = 1; i <= slices; i++)
{
setSlice(i);
stDev = getValue("StdDev");
//If this slice's standard deviation is higher than the current
//maximum, then update the maximum and the most focussed slice
if (stDev > maxStDev)
{
maxStDev = stDev;
focussedSlice = i;
}
}
//Duplicate the most focussed slice
duplicateSlice(stackTitle, focussedSlice);
focussedTitle = getTitle();
close(stackTitle);
selectWindow(focussedTitle);
}
}
//Extract a single channel from a multi-channel image and then do a Z project
function duplicateChannelAndProjectStack(imageTitle, channel, projectPreference)
{
//Duplicate specific channel from image to extract it as a new image
duplicateChannel(imageTitle, channel);
//Do Z project to get single frame if the image is a Z-stack
getDimensions(width, height, channels, slices, frames);
if (slices > 1)
{
projectString = "projection=[" + projectPreference + "]";
stackTitle = getTitle();
run("Z Project...", projectString);
zProjectTitle = getTitle();
close(stackTitle);
//Select the new image before finishing
selectWindow(zProjectTitle);
}
}
function finish()
{
IJ.renameResults("Summary","allResults");
selectWindow("allResults");
Table.deleteColumn("Total Area");
Table.deleteColumn("Average Size");
Table.deleteColumn("%Area");
Table.deleteColumn("Mean");
Table.deleteColumn("IntDen");
Table.deleteColumn("Median");
Table.renameColumn("Slice", "Cell");
Table.renameColumn("Count", "Nuclei_count");
saveResults("allResults", "Results_uninfected", "csv");
}
//Save results as CSV, with image name, appended with "_foci_per_nucleus"
function saveResults(table, name, extension)
{
outCSVName = name + "." + extension;
fullPath = output + "/" + outCSVName;
extendNumber = 1;
while(File.exists(fullPath))
{
outCSVName = name + extendNumber + "." + extension;
fullPath = output + "/" + outCSVName;
extendNumber ++;
}
selectWindow(table);
saveAs(table, fullPath);
close(outCSVName);
}