-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cluster_Quant.ijm
746 lines (620 loc) · 23 KB
/
Cluster_Quant.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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
//////////////////////////// PRELIMINARIES ////////////////////////////
main_data_default = "";
nondataprefix = "##### "// printed in lines that are not data, will be ignored by python code
printIMname = 0; // set to 0 or 1 depending on whether you want image name printed to log
time_printing = "time_printing";
file_naming = "file_naming";
starttime = fetchTimeStamp(file_naming);
makeDebugTextWindow = 0;
debugWindow = "Debugging";
start = getTime();
closeWinsWhenDone = true; // turn off for debugging
run ("Close All");
print ("\\Clear");
roiManager("reset");
run("Colors...", "foreground=white background=black");
close("Debug");
if (isOpen(debugWindow)){
selectWindow(debugWindow);
run("Close");
}
while (isOpen("Exception")) {
selectWindow("Exception");
run("Close");
}
//run("Text Window...", "name=" + debugWindow + " width=80 height=24 menu"); setLocation(3200, 140); debugWindow = "[" + debugWindow + "]";
//////////////////////////// DEFAULTS & OPENING DIALOG ////////////////////////////
// load defaults
defaults_dir = getDirectory("imagej") + "defaults" + File.separator;
defaults_file = defaults_dir + "Cluster_Quant.txt";
File.makeDirectory(defaults_dir);
defaults = import_defaults();
// set up dialog
Dialog.createNonBlocking("ClusterQuant settings");
Dialog.setInsets(0,0,0);
Dialog.addMessage(" INPUT/OUTPUT");
Dialog.setInsets(0, 0, 0);
Dialog.addMessage("Base directory should contain one subfolder with data per experimental condition");
Dialog.setInsets(0, 20, 0);
Dialog.addDirectory("Base directory", defaults[0]);
Dialog.addString("Experiment name", defaults[1], 12);
Dialog.addString("Image identifier", defaults[2], 12);
Dialog.setInsets(-35, 255, 0);
Dialog.addMessage("filenames without this identifier are excluded");
Dialog.setInsets(10,0,0);
Dialog.addMessage(" CHANNELS");
Dialog.setInsets(0,0,0);
Dialog.addNumber("Clustering channel", defaults[3],0,3, "measure degree of clustering"); // former: Kinetochore channel
Dialog.addToSameRow(); Dialog.addString("Name", defaults[4], 12);
Dialog.addNumber("Correlation channel", defaults[5],0,3, "correlate clustering with"); // former: Microtubule channel
Dialog.addToSameRow(); Dialog.addString("Name",defaults[6],12);
Dialog.addNumber("Outline (DNA) channel", defaults[7],0,3, "0 to skip; negative value for manual"); // former: DNA channel
Dialog.setInsets(10,0,0);
Dialog.addMessage(" ANALYSIS REGIONS");
Dialog.setInsets(0,0,0);
Dialog.addNumber("Measurement radius", defaults[8],0,5, "pixels");
Dialog.addNumber("Area fraction", defaults[9],0,5, "%; proportion within analysis region");
Dialog.setInsets(10,0,0);
Dialog.addMessage(" MANUAL SELECTION OF REGIONS TO EXCLUDE FROM ANALYSIS");
Dialog.setInsets(0, 20, 0);
Dialog.addCheckbox("Exclude regions", defaults[11]);
Dialog.addCheckbox("Load previously excluded regions", defaults[12]);
Dialog.setInsets(10,0,0);
Dialog.addMessage(" EXTENDED SETTINGS");
Dialog.setInsets(0, 20, 0);
Dialog.addCheckbox("Test extended settings", 0);
Dialog.addCheckbox("Show extended settings", 0);
Dialog.show(); // retrieve input
// input/output
dir = Dialog.getString();
if (!endsWith(dir, File.separator)) dir = dir + File.separator;
expName = Dialog.getString();
expName = replace(expName, " ", "_");
imageIdentifier = Dialog.getString();
imageIdentifier = imageIdentifier.toLowerCase;
// channel order
clusterChannel = Dialog.getNumber(); // former KTchannel
correlChanel = Dialog.getNumber(); // former MTchannelro
dnaChannel = Dialog.getNumber(); // former DNAchannel
clusterName = Dialog.getString(); // for x-axis title
correlName = Dialog.getString(); // for y-axis title
// grid parameters
radius = Dialog.getNumber(); // size of individual windows to measure
minAreaPerc = Dialog.getNumber(); // pixel displacement of grid at each step
// Manual ROI exclusion
excludeRegions = Dialog.getCheckbox();
preloadRegions = Dialog.getCheckbox();
// Extended settings
settingsTester = Dialog.getCheckbox();
extended_settings = Dialog.getCheckbox();
// 2nd dialog
Dialog.create("Extended settings");
Dialog.setInsets(0,0,0);
Dialog.addMessage(" BACKGROUND CORRECTION");
Dialog.setInsets(0,0,2);
background_methods = newArray("None", "Global", "Local");
Dialog.addChoice("Correction method", background_methods, defaults[13]);
Dialog.addNumber("Local background width", defaults[14],0,3, "pixels (only used for local background)");
//Dialog.setInsets(5,0,0);
Dialog.addMessage(" DNA AND SPOT DETECTION");
//Dialog.setInsets(0, 20, 0);
if (dnaChannel < 0) Dialog.addDirectory("Preload DNA outlines", "");
T_options = getList("threshold.methods");
Dialog.addChoice("DNA thresholding", T_options, defaults[15]);
Dialog.addNumber("Dilate cycles", defaults[16],0,3, "");
Dialog.addNumber("Spot prominence", defaults[10],0,5, "(higher is more exclusive)"); // prominence parameter from 'Find Maxima'
//Dialog.setInsets(5,0,0);
Dialog.addMessage(" CROP BORDER");
Dialog.addNumber("Deconvolution border", defaults[17],0,3, "pixels (16 is default for DV; 0 for no cropping)");
if ( extended_settings || dnaChannel < 0 ) Dialog.show();
// Background correction
bgMeth = Dialog.getChoice(); // background method: 0 = no correction; 1 = global background (median of cropped region); 2 = local background
bgBand = Dialog.getNumber(); // width of band around grid window to measure background intensity in (only used for local bg)
// Detection settings
if (dnaChannel < 0){
oldMaskRoiDir = Dialog.getString();
}
else oldMaskRoiDir = "";
threshType = Dialog.getChoice(); // potentially use RenyiEntropy
dilateCycles = Dialog.getNumber(); // number of dilation cycles for DAPI outline
prominence = Dialog.getNumber(); // prominence value of find maxima function
// Crop border
deconvCrop = Dialog.getNumber(); // pixels to crop around each edge (generally 16 for DV Elite). Set to 0 to not crop at all.
//////////////////////////// INPUT/OUTPUT ////////////////////////////
// save defaults
defaults = export_defaults();
// get prev ROI directory
Dialog.create("Choose Directory for preload regions");
Dialog.addDirectory("Main ROI directory", "");
if (preloadRegions && excludeRegions) Dialog.show();
preload_ROIdir = Dialog.getString();
// Create output directories
outdir = dir + "_" + expName + File.separator;
File.makeDirectory(outdir);
roiDir = outdir + "ROIs" + starttime + File.separator;
File.makeDirectory(roiDir);
subdirs = getFileList (dir);
// print initial info
print(nondataprefix, "Main folder:", File.getName(dir));
print(nondataprefix, "Start time:", fetchTimeStamp(time_printing) );
print("****", clusterName, correlName, radius, minAreaPerc);
//////////////////////////// RUN THROUGH FILES ////////////////////////////
// loop through individual conditions within base data folder
for (d = 0; d < subdirs.length; d++) {
subdirname = dir + subdirs [d];
if (File.isDirectory(subdirname) && File.getName(subdirname) != File.getName(outdir) && !startsWith(File.getName(subdirname),"_")) {
filelist = getFileList (subdirname);
subout = roiDir + File.getName(subdirname) + "_ROIs" + File.separator;
File.makeDirectory(subout);
print("***" + File.getName(subdirname));
for (f = 0; f < filelist.length; f++) { // loop through individual images within condition-folder
filename = subdirname + filelist [f];
if ( indexOf(filename.toLowerCase, imageIdentifier) >= 0 ){ // check for identifier
// open image and run macro
run("Bio-Formats Importer", "open=[" + filename + "] autoscale color_mode=Default rois_import=[ROI manager] view=Hyperstack stack_order=XYCZT");
rename(filelist[f]);
if (printIMname == 1) print(nondataprefix, getTitle());
cropEdges(deconvCrop);
clusterQuantification();
// close and dump memory
run ("Close All");
memoryDump(3);
}
}
}
}
//////////////////////////// FINISHING ////////////////////////////
// print end time and save log
print(nondataprefix, "End time:", fetchTimeStamp(time_printing) );
print(nondataprefix, "Total duration:", round((getTime() - start)/100)/10, "seconds");
defaults = Array.concat(nondataprefix + " Parameters used:", defaults);
Array.print(defaults);
print(nondataprefix, "All done");
saveLog();
if(isOpen("Results")){
selectWindow("Results");
run("Close");
roiManager("reset");
}
/////////////////////////////////////////////////////////////////////////
//////////////////////////// MINOR FUNCTIONS ////////////////////////////
/////////////////////////////////////////////////////////////////////////
function fetchTimeStamp(format){
// allows for nice formatting of datetime
getDateAndTime(year, month, dayOfWeek, dayOfMonth, hour, minute, second, msec);
// set to readable output
year = substring(d2s(year,0),2);
DateString = year + IJ.pad(month+1,2) + IJ.pad(dayOfMonth,2);
TimeString = IJ.pad(hour,2) + IJ.pad(minute,2);
DateTime = "_" + DateString + TimeString;
if (format == time_printing) return IJ.pad(hour,2) + ":" + IJ.pad(minute,2);
if (format == file_naming) return DateTime;
}
/////////////////////////////////////////////////////////////////////////
function memoryDump(n){
//print("memory used prior to memory dump: " + IJ.freeMemory());
for (i = 0; i < n; i++) run("Collect Garbage");
//print(nondataprefix, "memory used after " + n + "x memory dump: " + IJ.freeMemory());
}
/////////////////////////////////////////////////////////////////////////
function cropEdges(x){
if (x > 0) {
makeRectangle(deconvCrop, deconvCrop, getWidth-deconvCrop*2, getHeight-deconvCrop*2);
run("Crop");
}
}
/////////////////////////////////////////////////////////////////////////
function saveLog(){
selectWindow("Log");
saveAs("Text", outdir + "_PythonInput" + starttime + "_Radius" + radius + ".csv");
}
/////////////////////////////////////////////////////////////////////////
//////////////////////////// FUNCTIONAL FUNCTIONS ///////////////////////
/////////////////////////////////////////////////////////////////////////
function getRegions(){
roiManager("select", 0);
setThreshold(1, 255);
run("Analyze Particles...", "clear add");
roiManager("Show None");
resetMinAndMax;
for (i = 0; i < roiManager("count"); i++) {
roiManager("select", i);
getSelectionBounds(x, y, width, height);
makeOval(x-radius, y-radius, 2*radius+1, 2*radius+1);
roiManager("update");
}
if (mask != ""){
selectWindow(mask);
nInitialRois = RoiManager.size;
for (i = 0; i < nInitialRois; i++) {
roiManager("select", nInitialRois - 1 - i);
getStatistics(area, mean, min, max, std, histogram);
if (mean < 255 * minAreaPerc / 100){
roiManager("delete");
}
}
}
//waitForUser("Finished getRegions");
}
/////////////////////////////////////////////////////////////////////////
function doLocalBgCorrection(){
// measure (signal + background) MT intensity
getSelectionBounds(x, y, w, h);
makeRectangle(x-bgBand, y-bgBand, w+2*bgBand, h+2*bgBand); // box for measuring bg
getStatistics(largeArea, largeMean);
largeDens = largeArea * largeMean;
// calculate bg signal and final signal
rawDens = rawArea * rawMean;
bgArea = largeArea - rawArea;
bgSignal= (largeDens - rawDens) / bgArea;
corrected_signal = rawMean - bgSignal;
return corrected_signal;
}
/////////////////////////////////////////////////////////////////////////
function import_defaults(){
// set pre-defaults for first time
defaults = newArray();
defaults[0] = "_" ;//dir = Dialog.getString();
defaults[1] = "ClusterQuant" ;//expName = Dialog.getString();
defaults[2] = ".dv" ;//imageIdentifier = Dialog.getString();
defaults[3] = 4 ;//clusterChannel = Dialog.getNumber();
defaults[4] = "Spots" ;//clusterName = Dialog.getString();
defaults[5] = 3 ;//correlChanel = Dialog.getNumber();
defaults[6] = "Intensity" ;//correlName = Dialog.getString();
defaults[7] = 1 ;//dnaChannel = Dialog.getNumber();
defaults[8] = 12 ;//radius = Dialog.getNumber();
defaults[9] = 2 ;//minAreaPerc = Dialog.getNumber();
defaults[10] = 250 ;//prominence = Dialog.getNumber();
defaults[11] = 0 ;//excludeRegions = Dialog.getCheckbox();
defaults[12] = 0 ;//preloadRegions = Dialog.getCheckbox();
defaults[13] = "Global" ;//bgMeth = Dialog.getChoice();
defaults[14] = 2 ;//bgBand = Dialog.getNumber();
defaults[15] = "RenyiEntropy" ;//threshType = Dialog.getChoice();
defaults[16] = 4 ;//dilateCycles = Dialog.getNumber();
defaults[17] = 0 ;//deconvCrop = Dialog.getNumber();
// import previous defaults if they exist
if (File.exists(defaults_file)) {
def_str = File.openAsString(defaults_file);
imp_def = split(def_str, ",");
for (i = 0; i < imp_def.length; i++) {
while(startsWith(imp_def[i], " ")) imp_def[i] = substring(imp_def[i], 1);
}
if (imp_def.length == defaults.length){
defaults = imp_def;
}
else if (imp_def.length > 0){
print(imp_def.length,defaults.length);
Array.print (imp_def);
Array.print (defaults);
min = minOf(imp_def.length, defaults.length);
for (i = 0; i < min; i++) print(i,imp_def[i],defaults[i]);
exit("defaults and imported defaults length doesnt match");
}
}
for (i = 0; i < defaults.length; i++) {
if (defaults[i] == "_") {
defaults[i] = "";
}
}
return defaults;
}
/////////////////////////////////////////////////////////////////////////
function export_defaults(){
defaults = newArray();
defaults[0] = dir;
defaults[1] = expName;
defaults[2] = imageIdentifier;
defaults[3] = clusterChannel;
defaults[4] = clusterName;
defaults[5] = correlChanel;
defaults[6] = correlName;
defaults[7] = dnaChannel;
defaults[8] = radius;
defaults[9] = minAreaPerc;
defaults[10] = prominence;
defaults[11] = excludeRegions;
defaults[12] = preloadRegions;
defaults[13] = bgMeth;
defaults[14] = bgBand;
defaults[15] = threshType;
defaults[16] = dilateCycles;
defaults[17] = deconvCrop;
for (i = 0; i < defaults.length; i++) {
if (defaults[i] == "") {
defaults[i] = "_";
}
}
print("\\Clear");
Array.print(defaults);
selectWindow("Log");
if (getInfo("window.contents") != "") saveAs("Text", defaults_file);
// waitForUser("");
print("\\Clear");
return defaults;
}
/////////////////////////////////////////////////////////////////////////
function test_1(){
selectImage(ori);
setSlice(clusterChannel);
run("Duplicate...", "title=TEMP_thresh duplicate channels=" + dnaChannel);
selectImage(ori);
run("Duplicate...", "title=TEMP_DNA duplicate channels=" + dnaChannel);
run("Tile");
for (id = 1; id <= nImages; id++) {
selectImage(id);
resetMinAndMax;
roiManager("show all without labels");
roiManager("Show None");
roiManager("deselect");
run("Select None");
run("From ROI Manager");
}
selectImage(3);
run("Threshold...");
setAutoThreshold(threshType + " dark");
waitForUser("Check if selection contains all spots: \n \n" +
"- if consistently too large/small: change dilate cycles (higher cycle number = larger area; currently: " + dilateCycles + ").\n" +
"- if completely off, test other threshold method on DNA channel (current default is: " + threshType +").\n" +
" \nYou can change these parameters in the extended settings window when starting this macro,\nand they will be stored as default in ImageJ");
close("TEMP*");
selectImage(ori);
run("Remove Overlay");
}
/////////////////////////////////////////////////////////////////////////
function test_2(){
run("Tile");
for (i = 1; i <= nImages; i++) {
selectImage(i);
roiManager("Combine");
run("Add Selection...");
run("Select None");
roiManager("Show All without labels");
roiManager("Show None");
}
selectImage(ori);
waitForUser("Check whether spot recognition seems OK (spots outside the ROI can be ignored).\n" +
" \nIf this looks wrong, test to find a good 'Prominence' factor using\n" +
"'Process > Find Maxima...' and use 'Preview point selection' to find a good value.\n" +
"Current default prominence: " + prominence + ".\n" +
" \nYou can change this parameter in the extended settings window when starting this macro,\nand it will be stored as default in ImageJ");
roiManager("delete");
}
/////////////////////////////////////////////////////////////////////////
///////////////////////////// MAIN FUNCTIONS ////////////////////////////
/////////////////////////////////////////////////////////////////////////
function clusterQuantification(){
// initialize
ori = getTitle();
setVoxelSize(1, 1, 0, "px"); // unitize pixel size
run("Select None");
resetMinAndMax;
roiManager("reset");
cropEdges(deconvCrop);
ROIfile = subout + ori + ".zip";
// run sequential steps:
// step 1: get DAPI outline
mask = makeMask();
//waitForUser("finished step 1: DAPI outline");
// step 2: exclude regions
if (excludeRegions) setExcludeRegions();
else roiManager("save", ROIfile);
//waitForUser("finished step 2: exclude regions");
// step 3: make grid
//makeGrid();
// step 4: make measurements
before = getTime();
allData = measureClustering();
duration = round((getTime() - before)/100)/10;
//waitForUser("finished step 4: make measurements");
// retrieve separate arrays from allData
clusterList = Array.slice(allData, 0, allData.length/2);
intensList = Array.slice(allData, allData.length/2, allData.length);
// print info
//print("**", ori);
//Array.print(clusterList);
//Array.print(intensList);
//print(nondataprefix, "duration:", duration, "sec");
saveLog();
}
/////////////////////////////////////////////////////////////////////////
// step 1
function makeMask(){
mask = "";
// load old ROIs
if (File.isDirectory(oldMaskRoiDir)){
roiManager("reset");
oldROIfile = oldMaskRoiDir + File.getName(subdirname) + "_ROIs" + File.separator + ori + ".zip";
roiManager("open", oldROIfile)
while (roiManager("count") > 1) {
roiManager("select", 1);
roiManager("delete");
}
}
else if (dnaChannel > 0) {
// prep images
selectImage(ori);
setSlice (dnaChannel);
run("Duplicate...", "duplicate channels=&dnaChannel");
run("Grays");
mask = getTitle();
// make mask
setAutoThreshold(threshType+" dark");
run("Convert to Mask");
run("Fill Holes");
run("Erode");
// find main cell from mask
run("Analyze Particles...", "display exclude clear include add");
while ( roiManager("count") > 1){
roiManager("select", 0);
getStatistics(area_0);
roiManager("select", 1);
getStatistics(area_1);
if (area_0 < area_1) roiManager("select", 0); // else ROI 1 still selected
roiManager("delete");
}
roiManager("select", 0);
run("Invert");
run("Clear Outside");
run("Select None");
run("Invert");
for (i = 0; i < dilateCycles; i++) run("Dilate");
run("Analyze Particles...", "display exclude clear include add");
if (settingsTester) test_1();
}
else if (dnaChannel < 0){ // manual selection of analysis region
setSlice(dnaChannel * -1);
Stack.setDisplayMode("grayscale");
run("Duplicate...", "duplicate channels=&dnaChannel");
mask = getTitle();
run("Tile");
for (id = 1; id <= nImages; id++) {
selectImage(id);
resetMinAndMax;
}
setAutoThreshold("MinError dark");
setTool("wand");
waitForUser("Create analysis region and add to ROI manager (Ctrl+t)");
selectImage(nImages);
// at least 1 ROI added
if (roiManager("count") > 0){
// combine in case >1 ROI was added
roiManager("Combine");
roiManager("add");
while (roiManager("count") > 1) {
roiManager("select", 0);
roiManager("delete");
}
}
// in case selection was made but not added to ROI list --> add to ROI list
else if (is("area")) roiManager("add");
// no selection --> use entire frame
else {
run("Select All");
roiManager("add");
}
run("Convert to Mask");
run("Erode");
for (i = 0; i < dilateCycles; i++) run("Dilate");
roiManager("select", 0);
getSelectionBounds(_x_, _y_, _, _);
doWand(_x_, _y_);
roiManager("update");
}
else { //no mask
run("Select All");
roiManager("add");
}
// save ROI file
selectImage(ori);
roiManager("select", 0);
roiManager("rename", "Analysis_Region");
return mask;
}
/////////////////////////////////////////////////////////////////////////
// step 2
function setExcludeRegions(){
// load existing ROI files if they exist
oldROI_file = preload_ROIdir + File.getName(subdirname) + "_ROIs" + File.separator + ori + ".zip";
if (preloadRegions && File.exists(oldROI_file) ){
roiManager("reset");
roiManager("open", oldROI_file);
}
else {
// select correct visuals
selectImage(ori);
Stack.setChannel(correlChanel);
run("Select None");
run("Set... ", "zoom=150");
//setLocation(2000, 50);
roiManager("Show All without labels");
setTool("oval");
// manually select exclusio regions
roiManager("select", 0);
run("Make Inverse");
roiManager("update");
run("Select None");
waitForUser("Select regions to exclude.\nAdd each region to ROI manager using Ctrl+t.");
roiManager("deselect");
roiManager("combine"); // apparently this overwrites the original. will re-add below
roiManager("select", 0);
run("Make Inverse");
roiManager("update");
// rename ROIs and save
selectImage(mask);
run("Analyze Particles...", "add"); // re-add region of DAPI outline
roiManager("select", roiManager("count")-1)
roiManager("rename", "DAPI_Region");
for (roi = 1; roi < roiManager("count")-1; roi++) {
roiManager("select", roi);
roiManager("rename", "Exclude_Region_"+roi);
fill();
}
roiManager("deselect");
roiManager("save", ROIfile);
}
}
/////////////////////////////////////////////////////////////////////////
// step 4
function measureClustering(){
// get global bg
globalBG = 1; // for no background correction
if (bgMeth == background_methods[1]) { // global bg correction
selectImage(ori);
setSlice(correlChanel);
roiManager("select", 0);
_ = 0;
while (getTitle() != ori) {
selectImage(ori);
_++;
if ( _ > 15) {
exit("crashed on BG measurement of image: " + ori);
}
}
globalBG = getValue("Median");
}
// find kinetochores
selectImage(ori);
resetMinAndMax;
setSlice(clusterChannel);
run("Find Maxima...", "prominence=" + prominence + " strict exclude output=[Single Points]");
run("Divide...", "value=255");
setMinAndMax(0, 1);
roiManager("Show All without labels");
spots_im = getTitle();
getRegions();
close(mask);
setMinAndMax(0,0);
run("Select None");
roiManager("Show All without labels");
saveAs("Tiff", subout + ori + "_Maxima.tif");
spotIM = getTitle();
run("Tile");
// count number of CEN spots
Spots = newArray();
Intensities = newArray();
if (settingsTester) test_2();
// Measure correlation (separate loop from above saves a lot of time!)
selectImage(ori);
setSlice(correlChanel);
for (roi = 0; roi < roiManager("count"); roi++) {
// measure correl channel
roiManager("select",roi);
getStatistics(rawArea, rawMean);
if (bgMeth == background_methods[2]) Intensities[roi] = doLocalBgCorrection(); // local bg correction
else Intensities[roi] = rawMean - globalBG; // global BG
}
close(ori);
selectImage(spotIM);
for (roi = 0; roi < roiManager("count"); roi++) {
roiManager("select",roi);
Spots[roi] = getValue("IntDen");
}
run("Select None");
// print results
print("**", ori);
Array.print(Spots);
Array.print(Intensities);
//waitForUser("test");
data = Array.concat(Spots,Intensities);
return data;
}