-
Notifications
You must be signed in to change notification settings - Fork 2
/
_DKR_AlexsKinetochoreMeasurer_Batch.ijm
448 lines (360 loc) · 13.7 KB
/
_DKR_AlexsKinetochoreMeasurer_Batch.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
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
440
441
442
443
444
445
446
447
448
//*********************************************************
//
//
//
//*********************************************************
function reduceHyperStack();
function printStackMaxPixel();
function blackout();
function appendMax();
function saveDocumentation();
macro "Alex's Kinetochore Measurer" {
//Dialog to define inputs
if(isOpen("Results")){
selectWindow("Results");
run("Close");
}
if(isOpen("Log")){
selectWindow("Log");
run("Close");
}
Dialog.create("Define Channels");
filter="D3D.dv";
Dialog.addNumber("Subunit",1);
Dialog.addNumber("MTs",2);
Dialog.addNumber("DAPI",3);
Dialog.addNumber("ACA",4);
Dialog.addString("Suffix Filter :", filter);
Dialog.addNumber("# of Objects: ", 20);
Dialog.addCheckbox("Batch Mode?", true)
Dialog.show();
subunit=Dialog.getNumber();
MTs=Dialog.getNumber();
DAPI=Dialog.getNumber();
aca=Dialog.getNumber();
filter=Dialog.getString();
objects=Dialog.getNumber();
batcher=Dialog.getCheckbox();
print(batcher);
if(batcher){
//get directory information
dir1 = getDirectory("Choose source directory");
if (dir1=="")
exit("No directory available");
//get a list of all the files in this folder
list = getFileList(dir1);
// Create a results directory in dir1
dir2 = dir1+"results"+File.separator;
File.makeDirectory(dir2);
if (!File.exists(dir2))
exit("Unable to create directory");
length=list.length;
}
else{
//get info for the open image
list=getTitle;
subdir=getDirectory("Choose where to save results");
length=1;
}
//loop for each image
resultNumber=0;
for(i=0; i<length; i++) {
//process each image, only with "*.filter" suffix
if (endsWith(list[i],filter)) {
if(batcher){
// Create an image directory within dir2(results)
subdir = dir2+list[i]+File.separator;
File.makeDirectory(subdir);
if (!File.exists(subdir))
exit("Unable to create movie directory");
//import image, get stack dimensions, make composite projection for identifying mitotic and interphase cells
run("Bio-Formats Importer", "open="+dir1+list[i] +" view=Hyperstack stack_order=XYCZT");
}
setLocation(50,50);
Stack.getDimensions(width, height, channels, slices, frames);
run("Z Project...", "start=1 stop=" +slices+" projection=[Max Intensity]");
rename("Image Map of "+list[i]);
setLocation(60,60);
run("Make Composite");
selectWindow("Image Map of "+list[i]);
setSlice(subunit);
run("Enhance Contrast", "saturated=0.4");
setSlice(DAPI);
run("Enhance Contrast", "saturated=0.4");
selectWindow("Image Map of "+list[i]);
//identify cells to crop and count
setTool("elliptical");
roiManager("reset");
run("ROI Manager...");
selectWindow("ROI Manager");
waitForUser("circle and \"Add (t)\" each nuclei to ROI Manager");
//show all cell selections and show dialog to identify celltype for each, continue by OK
selectWindow("Image Map of "+list[i]);
celltype=newArray("mitotic","interphase");
selectWindow("Image Map of "+list[i]);
setOption("Show All",true);
Dialog.create("Identify Cell Type");
//get cell types from dialog box by iterating for each cellROI, close projection image
for(count=0;count<roiManager("count");count++){
Dialog.addChoice("Cell #"+count+1, celltype);
}
Dialog.show();
selectWindow("Image Map of "+list[i]);
close();
//place celltype value in each cellID array position, rename each cellROI, and save ROI manager for this HyperStackImage
cellID=newArray(roiManager("count"));
for(count=0;count < roiManager("count");count++){
cellID[count]=Dialog.getChoice();
roiManager("Select", count);
roiManager("Rename","cell "+count+1+" "+cellID[count]);
}
roiManager("Save",subdir+"CellMap.zip");
maxCell=roiManager("count");
//reopen ROI file if closed, select image, select cell by ROI, duplicate this cells' hyperstack (all channels), duplicate subunit channel to regular stack (single channel)
//loop this for each cell in the image
//count = keep track of the cells ROI in ROI manager
for(count=0;count < maxCell;count++){
//duplicate each cropped cell hyperstack
roiManager("reset");
roiManager("Open",subdir+"CellMap.zip");
selectWindow(list[i]);
roiManager("Select", count);
run("Duplicate...", "title=Image:"+i+"_Cell:"+count+1+"_CellType:"+cellID[count]+" duplicate");
//define this cropped cell hyperstack as "cellHyperStack"
cellHyperStack="Image:"+i+"_Cell:"+count+1+"_CellType:"+cellID[count];
//enlarge the hyperstack to prevent errors when measuring edge kinetochores
//save the image for documentation later
selectWindow(cellHyperStack);
cellHyperStack="Cell#"+count+1+".tif";
rename(cellHyperStack);
//duplicate subunit channel for kinetochore identification
Stack.setChannel(subunit);
reduceHyperStack(subunit);
Stack.getDimensions(width, height, channels, slices, frames);
run("Canvas Size...", "width="+width+10+" height="+height+10+" position=Center");
//find brightest pixel for this channel, place its value in the redults table, then change the pixel value to black, repeat for 'objects' times, then close the blacked out image
kt=1;
max=resultNumber+objects;
//resultNumber is initialized=0, then recalculated after adding all objects to results table before next cell iteration
for(resultNumber;resultNumber<max;resultNumber++){
//find the highest intensity pixel and print its location and value to results table
printStackMaxPixel(i+1,kt,count+1,resultNumber);
//change this max value and a region () around it to black, preventing re-counting
blackout(resultNumber);
kt++;
}
close();
//go back to hyperstack of this cell, duplicate subunit channel stack, subtract background and gather pixel values
selectWindow(cellHyperStack);
Stack.setChannel(subunit);
reduceHyperStack(subunit);
run("Subtract Background...", "rolling=50 stack");
Stack.getDimensions(width, height, channels, slices, frames);
run("Canvas Size...", "width="+width+10+" height="+height+10+" position=Center");
resultNumber=resultNumber-objects;
for(resultNumber;resultNumber<max;resultNumber++){
appendMax(getResult("x", resultNumber),getResult("y", resultNumber),getResult("z", resultNumber),resultNumber,"subtracted subunit");
}
close();
//go back to hyperstack of this cell, duplicate the aca channel stack and gather pixel values
selectWindow(cellHyperStack);
Stack.setChannel(aca);
reduceHyperStack(aca);
Stack.getDimensions(width, height, channels, slices, frames);
run("Canvas Size...", "width="+width+10+" height="+height+10+" position=Center");
resultNumber=resultNumber-objects;
for(resultNumber;resultNumber<max;resultNumber++){
appendMax(getResult("x", resultNumber),getResult("y", resultNumber),getResult("z", resultNumber),resultNumber,"raw aca");
}
close();
//go back to hyperstack of this cell, duplicate the aca channel stack and gather pixel values
selectWindow(cellHyperStack);
Stack.setChannel(aca);
reduceHyperStack(aca);
run("Subtract Background...", "rolling=50 stack");
Stack.getDimensions(width, height, channels, slices, frames);
run("Canvas Size...", "width="+width+10+" height="+height+10+" position=Center");
resultNumber=resultNumber-objects;
for(resultNumber;resultNumber<max;resultNumber++){
appendMax(getResult("x", resultNumber),getResult("y", resultNumber),getResult("z", resultNumber),resultNumber,"subtracted aca");
}
//before closing, save an ROI for each kinetochore using coordinates on the Results table
resultNumber=resultNumber-objects;
roiManager("reset");
for(resultNumber;resultNumber<max;resultNumber++){
saveDocumentation(getResult("x", resultNumber),getResult("y", resultNumber),getResult("z", resultNumber),resultNumber);
}
roiManager("Save",subdir+"Cell#"+count+1+"KinetochoreROIs.zip");
close();
if(batcher){
selectWindow("Results");
saveAs("Measurements", dir2+"Results of Analysis.txt");
}
//set resultNumber as the next available line in Results for next cell iteration
resultNumber=nResults;
//close cellHyperStack and prepare to open another
selectWindow(cellHyperStack);
Stack.getDimensions(width, height, channels, slices, frames);
run("Canvas Size...", "width="+width+10+" height="+height+10+" position=Center");
saveAs("Tiff",subdir+"Cell#"+count+1+".tif");
close();
} //loops each cell for one image
selectWindow(list[i]);
close();
} //end of IF(filter) statement
else
print(list[i]+" was not included");
} //loops each movie in source folder
if(batcher){
selectWindow("Results");
saveAs("Measurements", dir2+"Results of Analysis.txt");
selectWindow("ROI Manager");
run("Close");
}
print("program ended properly");
} //end main macro
// Reduce HyperStack
//
// This macro function reduces the dimensionality of an hyperstack. It will create
// a new hyperstack with all the channels and time points at a given
// z position, or all the z slices for the current channel and time point, etc.
function reduceHyperStack(channel) {
requires("1.40a");
if (!Stack.isHyperStack) exit ("HyperStack required");
setBatchMode(true);
Stack.getDimensions(width, height, channels, slices, frames);
Stack.getPosition(c1, z1, t1);
Stack.getDisplayMode(mode);
title = getTitle();
b = bitDepth();
id = getImageID();
channels = 1;
run("New HyperStack...", "title="+title+" channel-"+channel+" type="+b+"-bit display=Color width="+width+" height="+height+" channels="+channels+" slices="+slices+" frames="+frames+" ");
id2 = getImageID();
n = channels*slices*frames;
i = 0;
for (c=1; c<=channels; c++) {
if (channels==1) c=c1;
selectImage(id);
Stack.setPosition(c,1,1);
getVoxelSize(vw, vh, vd, unit);
if (b!=24) getLut(reds, greens, blues);
getMinAndMax(min, max);
for (z=1; z<=slices; z++) {
if (slices==1) z=z1;
for (t=1; t<=frames; t++) {
showProgress(i++, n);
if (frames==1) t=t1;
selectImage(id);
Stack.setPosition(c,z,t);
run("Copy");
selectImage(id2);
Stack.setPosition(c,z,t);
run("Paste");
}
}
selectImage(id2);
Stack.setPosition(c,1,1);
setVoxelSize(vw, vh, vd, unit);
if (b!=24) setLut(reds, greens, blues);
setMinAndMax(min, max);
}
setBatchMode(false);
selectImage(id);
Stack.setPosition(c1, z1, t1);
selectImage(id2);
Stack.setPosition(1, 1, 1);
if (is("composite")) Stack.setDisplayMode(mode);
run("Select None");
}
//this macro function searches through a stack to find the highest intensity pixel,
//once found it adds the location to the roi manager,
//changes the selection to a 3x3 square, changes pixel value to 0,
//and then repeats for +/- slice
function printStackMaxPixel(movie,kt,cell,resultNumber) {
//determine which slice contains the hightest intensity pixel
Stack.getDimensions(width, height, channels, slices, frames);
currentmax=0;
goodslice=0;
for(i=1;i<=slices;i++){
setSlice(i);
getRawStatistics(nPixels, mean, min, slicemax, std, histogram);
if(slicemax > currentmax){
currentmax=slicemax;
goodslice=i;
}
}
//go to that slice and find the coordinates of the highest intensity pixel
//append the intensity value and coordinates in results table
setSlice(goodslice);
for(y=1;y<=height;y++){
for(x=1;x<=width;x++){
if (currentmax==getPixel(x,y)){
setResult("Image#",resultNumber,movie);
setResult("Cell #",resultNumber,cell);
setResult("kt #",resultNumber,kt);
setResult("x",resultNumber,x);
setResult("y",resultNumber,y);
setResult("z",resultNumber,goodslice);
setResult("raw subunit value",resultNumber,getPixel(x,y));
updateResults();
}
}
}
}
//ImageJ macro written to be used in Alex's Kinetochore Counter macro
//Dan Rozelle, 20090123
//
//draws a sphere with specified redius centered around
//pointx,y,z found in results table
//
function blackout(resultNumber) {
x=getResult("x", resultNumber);
y=getResult("y", resultNumber);
z=getResult("z", resultNumber);
slice=z-2;
run("Colors...", "foreground=black background=black");
//fill 5x5 square black on 5 slices, centered on ROI
for(slice;slice<=z+2;slice++){
if(slice > 0 && slice <= nSlices){
setSlice(slice);
fillRect(x-2,y-2,5,5);
}
}
}
//this macro function scans through a specified ROI and appends the
//coordinates and value of the highest intensity pixel to the results table
function appendMax(x,y,slice,line,category) {
//scan each pixel in a 5x5 ROI specified by given center point for the highest intensity pixel
//append its value to the given line in the results table under a new specified category
Stack.getDimensions(width, height, channels, slices, frames);
currentmax=0;
setSlice(slice);
y=y-2;
x=x-2;
ymax=y+5;
xmax=x+5;
if(y>=0 && x>=0){
if(ymax<=height && xmax<=width){
for(y;y<=ymax;y++){
for(x;x<=xmax;x++){
if(currentmax < getPixel(x,y)){
currentmax=getPixel(x,y);
}
}
}
}
}
else{
currentmax=getPixel(y+2,x+2);
}
setResult(category,line,currentmax);
updateResults();
}
function saveDocumentation(x,y,z,line){
foo=getResult("x", line);
bar=getResult("y", line);
baz=getResult("z", line);
run("Specify...", "width=3 height=3 x="+foo+" y="+bar+" slice="+baz);
roiManager("add");
}