-
Notifications
You must be signed in to change notification settings - Fork 1
/
mn2.bsh
324 lines (275 loc) · 9.12 KB
/
mn2.bsh
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
/**
* Beanshell script to find micronuclei in a field of cells stained with a DNA dye
* This script prefilters the image, automatically segments it, and find objects
* of a certain size (size limits are set just under here)
*
* Nico Stuurman, 2015, Copyright regents of the University of California
*
*
*/
import java.util.ArrayList;
import java.util.prefs.Preferences;
import java.util.HashMap;
import java.awt.geom.Point2D;
import java.awt.geom.Point2D.Double;
import java.awt.Polygon;
import ij.IJ;
import ij.plugin.Duplicator;
import ij.plugin.filter.GaussianBlur;
import ij.plugin.frame.RoiManager;
import ij.plugin.ImageCalculator;
import ij.gui.Roi;
import ij.gui.PolygonRoi;
import org.micromanager.MMStudio;
import org.micromanager.projector.ProjectorControlForm;
import edu.valelab.gaussianfit.FitAllThread;
import edu.valelab.gaussianfit.algorithm.FindLocalMaxima;
import edu.valelab.gaussianfit.data.GaussianInfo;
// whether or not to photoactivate
zap = false;
// whether or not to show binary masks, set to false when using the IA plugin!
showMasks = false;
// microNuclei allowed sizes
mnMinSize = 3.0;
mnMaxSize = 800.0;
// nuclei allowed sized
nMinSize = 80;
nMaxSize = 900;
// max distance a micronucleus can be separated from a nucleus
maxDistance = 20;
// min distance a micronucleus should be from the edge of the image
minEdgeDistance = 10.0; // in microns
// minimum number of "micronuclei" we want per nucleus to score as a hit
minNumMNperNucleus = 3;
// do not analyze images whose stdev is above this value
// Use this to remove images showing well edges
maxStdDev = 7000;
// name of the faltfield image in ImageJ. Open this image first
flatfieldName = "flatfield.tif";
pixelSize = 0.644; // not sure why, but imp.getCalibration is unreliable
// functions used in the main code
/**
* calculates distance between two points
*/
distance(Point2D.Double p1, Point2D.Double p2) {
x = p1.x - p2.x;
y = p1.y - p2.y;
total = x * x + y * y;
return Math.sqrt(total);
}
/**
* Find the closest point in the HashMap
* for now, uses brute force search
*/
closest(Point2D.Double p, HashMap l) {
if (l.size() == 0) {
return null;
}
pointList = l.keySet().toArray();
closestNucleus = pointList[0];
d = distance(p, closestNucleus);
for (p2 : pointList) {
dNew = distance(p, p2);
if (dNew < d) {
d = dNew;
closestNucleus = p2;
}
}
return closestNucleus;
}
/**
* Calculate the size of an ImageJ ROI
*/
roiSize(Roi r) {
return r.getBounds().width * r.getBounds().height;
}
// start of the main code
microNuclei = new ArrayList();
microNucleiROIs = new HashMap();
nuclei = new HashMap();
nucleiContents = new ArrayList();
nucleiRois = new HashMap();
zapNuclei = new ArrayList();
// check if there is a flatfield image
flatField = ij.WindowManager.getImage(flatfieldName);
if (flatField == null)
gui.message("No flatfield found");
// clean results table
res = ij.measure.ResultsTable.getResultsTable();
res.reset();
imp = IJ.getImage();
cal = imp.getCalibration();
// remove images that have the well edge in them
stdDev = imp.getStatistics().stdDev;
if (stdDev > maxStdDev)
return;
width = imp.getProcessor().getWidth();
height = imp.getProcessor().getHeight();
widthUm = cal.getX(width);
heightUm = cal.getY(height);
pixelSize = cal.getX(1.0);
gui = MMStudio.getInstance();
//gui.message("PixelSize: " + cal.getX(1));
// maintain some form of persistence using prefs
prefs = Preferences.userNodeForPackage(this.getClass());
imp2 = (new Duplicator()).run(imp, 1, 1);
ic = new ImageCalculator();
if (flatfield != null)
imp2 = ic.run("Divide, float, 32", imp2, flatField);
// find micronuclei by sharpening, segmentation using Otsu, and Watershed
microNucleiImp = imp2.duplicate();
IJ.run(microNucleiImp, "16-bit", "");
IJ.run(microNucleiImp, "Sharpen", "");
//IJ.run(microNucleiImp, "Thresholded Blur", "radius=5 threshold=800 softness=0.50 strength=3");
//IJ.run(microNucleiImp, "Unsharp Mask...", "radius=3 mask=0.60");
//IJ.run(microNucleiImp, "Kuwahara Filter", "sampling=3");
IJ.setAutoThreshold(microNucleiImp, "Otsu dark");
Prefs.blackBackground = true;
IJ.run(microNucleiImp, "Convert to Mask", "");
IJ.run(microNucleiImp, "Close-", "");
//IJ.run(microNucleiImp, "Erode", "");
IJ.run(microNucleiImp, "Watershed", "");
//IJ.run("Set Measurements...", "area centroid center bounding fit shape redirect=None decimal=2");
IJ.run("Set Measurements...", "area center decimal=2");
IJ.run(microNucleiImp, "Analyze Particles...", "size=" + mnMinSize + "-" + mnMaxSize +
" show clear add");
// Build up a list of potential micronuclei
rm = RoiManager.getInstance2();
if (rm == null)
rm = new RoiManager();
for (roi : rm.getRoisAsArray()) {
// approximate microNuclear positions as the center of the bounding box
rc = roi.getBounds();
double xc = rc.x + 0.5 * rc.width;
double yc = rc.y + 0.5 * rc.height;
xc *= pixelSize;
yc *= pixelSize;
// gui.message("pt: " + xc + ", " + yc);
pt = new java.awt.geom.Point2D.Double ( xc, yc );
microNuclei.add(pt);
microNucleiROIs.put(pt, roi);
// gui.message("spot " + x + ", " + y + " was not in an ROI");
}
// find nuclei by smoothing and gaussian filtering, followed by Otsu segmentation and watershed
nucleiImp = imp2.duplicate();
IJ.run(nucleiImp, "Smooth", "");
IJ.run(nucleiImp, "Gaussian Blur...", "sigma=5.0");
IJ.setAutoThreshold(nucleiImp, "Otsu dark");
Prefs.blackBackground = true;
IJ.run(nucleiImp, "Convert to Mask", "");
IJ.run(nucleiImp, "Dilate", "");
IJ.run(nucleiImp, "Erode", "");
IJ.run(nucleiImp, "Watershed", "");
//IJ.run("Set Measurements...", "area centroid center bounding fit shape redirect=None decimal=2");
IJ.run("Set Measurements...", "area center decimal=2");
IJ.run(nucleiImp, "Analyze Particles...", "size=" + nMinSize + "-" + nMaxSize +
" clear add");
// add nuclei to our list of nuclei:
rm = RoiManager.getInstance2();
for (roi : rm.getRoisAsArray()) {
// approximate nuclear positions as the center of the bounding box
rc = roi.getBounds();
double xc = rc.x + 0.5 * rc.width;
double yc = rc.y + 0.5 * rc.height;
xc *= pixelSize;
yc *= pixelSize;
// gui.message("pt: " + xc + ", " + yc);
pt = new java.awt.geom.Point2D.Double ( xc, yc );
nucleiRois.put(pt, roi);
ArrayList containedMNs = new ArrayList();
nuclei.put(pt, containedMNs);
}
// close the ImagePlus as we no longer need it (we could leave this to the GC)
nucleiImp.changes = false;
if (showMasks)
nucleiImp.show();
else
nucleiImp.close();
// no longer need the microNuclei imp
microNucleiImp.changes = false;
if (showMasks)
microNucleiImp.show();
else
microNucleiImp.close();
imp2.changes = false;
if (showMasks)
imp2.show();
else
imp2.close();
// cycle through the list of micronuclei
// assign each to the nearest by nucleus (not more than maxdistance away)
for (mn : microNuclei) {
cn = closest(mn, nuclei);
if (cn != null && maxDistance > distance(mn, cn) ) {
nuclei.get(cn).add(mn);
}
}
// report what we found
res.reset();
// this is a bit funky, but seems to work
double roiMinSize = pixelSize * pixelSize * nMinSize * 10;
for (p : nuclei.keySet() ) {
res.incrementCounter();
res.addValue("X", p.x);
res.addValue("Y", p.y);
mnList = nuclei.get(p);
res.addValue("# mN", mnList.size());
zapit = 0;
if (nuclei.get(p).size() >= minNumMNperNucleus) {
// add to our target nuclei, except if these happen to be two nuclei that were
// lying close together.
if ( mnList.size() == 2) {
Roi r0 = microNucleiROIs.get(mnList.get(0));
Roi r1 = microNucleiROIs.get(mnList.get(1));
if ( (r0 != null && roiSize (r0 ) < roiMinSize) ||
(r1 != null && roiSize (r1) < roiMinSize) ) {
zapNuclei.add(p);
zapit = 1;
}
} else {
zapNuclei.add(p);
zapit = 1;
}
}
res.addValue("Zap", zapit);
}
res.show("Results");
prefs.getInt("nuclei", 0);
prefs.putInt("nuclei", prefs.getInt("nuclei", 0) + nuclei.size());
// prefs.putInt("microNuclei", prefs.getInt("microNuclei", 0) + nMn);
prefs.putInt("zappedNuclei", prefs.getInt("zappedNuclei", 0) + zapNuclei.size());
if (zap) {
// get a list with rois that we want to zap
zapRois = new ArrayList();
for (p : zapNuclei) {
//for (p : nuclei) { // use this to zap all nuclei
roi = nucleiRois.get(p);
//gui.message("Zap the roi: " + roi.getBounds());
zapRois.add(roi);
}
gui.message("mn: " + microNuclei.size() + ", n: " + nuclei.size() + ", zap: " + zapRois.size());
//gui.message("ZapRoi x: " + zapRois.get(0).x + ", y: " + zapRois.get(0).y);
// convert zapRois in a Roi[] of Polygon Rois
pcf = ProjectorControlForm.showSingleton(gui.getMMCore(), gui);
Roi[] rois = new Roi[zapRois.size()];
for (i=0; i < zapRois.size(); i++) {
rois[i] = (Roi) zapRois.get(i);
poly = rois[i].getConvexHull();
rois[i] = new PolygonRoi(poly, Roi.POLYGON);
}
prefs.putInt("zappedNow", rois.length);
// send to the galvo device and zap them for real
pcf.setNrRepetitions(5);
for (i=0; i < rois.length; i++) {
gui.message("Zapping " + (i + 1) + " of " + rois.length);
Roi[] theRois = {rois[i]};
pcf.setROIs(theRois);
pcf.updateROISettings();
pcf.getDevice().waitForDevice();
pcf.runRois();
pcf.getDevice().waitForDevice();
}
// display result and return focus to our window
//gui.runAcquisition();
//imp.getWindow().toFront();
}