-
Notifications
You must be signed in to change notification settings - Fork 0
/
BIL_SynapseDetection_v2.ijm
1402 lines (1296 loc) · 51 KB
/
BIL_SynapseDetection_v2.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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* 210719_BIL_MonicaVandenberg_Neurotransmitters_v2
***********************
ImageJ/Fiji macro set for to detection of neurotransmitter markers
Buidling blocks used from CellBlocks.ijm written by Winnok H. De Vos - [email protected]
Author: Marlies Verschuuren - [email protected]
Date Created: 2021 - 07 - 19
Date Last Modified: 2022 - 01 - 20
*/
/*
Plugins needed (Help > Update > Manage update sites):
Bio-Formats: https://sites.imagej.net/Bio-Formats/
ImageScience: https://sites.imagej.net/ImageScience/
*/
/*
VERSION SUPPORT:
V1: - Spot detection in different channels
- Max projection of substacks
V2: - Add max scale for multi-scale analysis
- Exclude cholinergic bodies
*/
// Variables ---------------------------------------------------------------------
// String variables
var dir = ""; // directory
var log_path = ""; // path for the log file
var micron = getInfo("micrometer.abbreviation"); // micro symbol
var order = "xyczt(default)"; // hyperstack dimension order
var output_dir = ""; // dir for analysis output
var results = ""; // summarized results
var spots_ch1_results = ""; // spot results ch 1
var spots_ch1_roi_set = ""; // spot ROI sets ch 1
var spots_ch1_segmentation_method = "Multi-Scale"; // spots segmentation method ch 1
var spots_ch1_threshold = "Fixed"; // threshold method for spot segmentation ch 1
var spots_ch1_exclusion_threshold = "Fixed"; // threshold method for exclusion ch 1
var spots_ch2_results = ""; // spot results ch 2
var spots_ch2_roi_set = ""; // spot ROI sets ch 2
var spots_ch2_segmentation_method = "Multi-Scale"; // spots segmentation method ch 2
var spots_ch2_threshold = "Fixed"; // threshold method for spot segmentation ch 2
var spots_ch2_exclusion_threshold = "Fixed"; // threshold method for exclusion ch 2
var spots_ch3_results = ""; // spot results ch 2
var spots_ch3_roi_set = ""; // spot ROI sets ch 3
var spots_ch3_segmentation_method = "Multi-Scale"; // spots segmentation method ch 3
var spots_ch3_threshold = "Fixed"; // threshold method for spot segmentation ch 3
var spots_ch3_exclusion_threshold = "Fixed"; // threshold method for exclusion ch 3
var spots_ch4_results = ""; // spot results ch 3
var spots_ch4_roi_set = ""; // spot ROI sets ch 4
var spots_ch4_segmentation_method = "Multi-Scale"; // spots segmentation method ch 4
var spots_ch4_threshold = "Fixed"; // threshold method for spot segmentation ch 4
var spots_ch4_exclusion_threshold = "Fixed"; // threshold method for exclusion ch 4
var suffix = ".tif"; // suffix for specifying the file type
// Number variables
var channels = 4; // number of channels
var image_height = 1000; // image height
var image_width = 1000; // image width
var pixel_size = 0.1785703; // pixel size (µm)
var spots_ch1_filter_scale = 2; // scale for Laplacian spots ch 1
var spots_ch1_fixed_threshold_value = 1.5; // fixed maximum threshold for spot segmentation (if auto doesn't work well); ch 1
var spots_ch1_max_scale_multi = 3; // max scale multi-scale enhancement ch 1
var spots_ch1_min_area = 0.2; // min spot size in micrometer ch 1
var spots_ch1_max_area = 3; // max spot size in micrometer ch 1
var spots_ch1_exclusion_scale = 15; // scale medium filter to exclude cell bodies ch 1
var spots_ch1_exclusion_fixed_threshold = 25; // fixed threshold to exclude cell bodies ch 1
var spots_ch1_exclusion_min_area = 20; // min area of cell bodies to exclude in ch 1
var spots_ch2_filter_scale = 2; // scale for Laplacian spots ch 2
var spots_ch2_fixed_threshold_value = 3; // fixed maximum threshold for spot segmentation (if auto doesn't work well); ch 2
var spots_ch2_max_scale_multi = 3; // max scale multi-scale enhancement ch 2
var spots_ch2_max_area = 3; // max spot size in micrometer ch 2
var spots_ch2_min_area = 0.2; // min spot size in micrometer ch 2
var spots_ch2_exclusion_scale = 15; // scale medium filter to exclude cell bodies ch 2
var spots_ch2_exclusion_fixed_threshold = 25; // fixed threshold to exclude cell bodies ch 2
var spots_ch2_exclusion_min_area = 20; // min area of cell bodies to exclude in ch 2
var spots_ch3_filter_scale = 1; // scale for Laplacian spots ch 3
var spots_ch3_fixed_threshold_value = 2; // fixed maximum threshold for spot segmentation (if auto doesn't work well); ch 3
var spots_ch3_max_scale_multi = 3; // max scale multi-scale enhancement ch3
var spots_ch3_max_area = 3; // max spot size in micrometer ch 3
var spots_ch3_min_area = 0.2; // min spot size in micrometer ch 3
var spots_ch3_exclusion_scale = 15; // scale medium filter to exclude cell bodies ch 3
var spots_ch3_exclusion_fixed_threshold = 25; // fixed threshold to exclude cell bodies ch 3
var spots_ch3_exclusion_min_area = 20; // min area of cell bodies to exclude in ch 3
var spots_ch4_filter_scale = 1; // scale for Laplacian spots ch 4
var spots_ch4_fixed_threshold_value = 2; // fixed maximum threshold for spot segmentation (if auto doesn't work well); ch 4
var spots_ch4_max_scale_multi = 3; // max scale multi-scale enhancement ch4
var spots_ch4_max_area = 3; // max spot size in micrometer ch 4
var spots_ch4_min_area = 0.2; // min spot size in micrometer ch 4
var spots_ch4_exclusion_scale = 15; // scale medium filter to exclude cell bodies ch 4
var spots_ch4_exclusion_fixed_threshold = 25; // fixed threshold to exclude cell bodies ch 4
var spots_ch4_exclusion_min_area = 20; // min area of cell bodies to exclude in ch 4
var z_substack = 3;
var z_step = 2;
// Boolean Parameters
var spots_ch1 = false;
var spots_ch2 = true;
var spots_ch3 = true;
var spots_ch4 = true;
var spots_ch1_exclusion = false;
var spots_ch2_exclusion = false;
var spots_ch3_exclusion = false;
var spots_ch4_exclusion = true;
// Arrays
var dimensions = newArray("xyczt(default)","xyctz","xytcz","xytzc","xyztc","xyzct");
var file_list = newArray(0);
var file_types = newArray(".tif",".tiff",".nd2",".ids",".jpg",".mvd2",".czi");
var objects = newArray("spots_ch1","spots_ch2","spots_ch3","spots_ch4");
var prefixes = newArray(0);
var spot_segmentation_methods = newArray("Gauss","Laplace","Multi-Scale");
var threshold_methods = getList("threshold.methods");
var threshold_methods = Array.concat(threshold_methods,"Fixed");
//---------------------------------------------------------------------Macros---------------------------------------------------------------------
macro "[I] Install Macro"{
// Only works on my personal drive
run("Install...", "install=[/data/CBH/mverschuuren/ACAM/2021_BIL_MonicavandenBerg_Neurotransmitters/BIL_SynapseDetection_v2.ijm]");
}
macro "Autorun"{
erase(1);
}
macro "Split Stack Into Fields Action Tool - C888 R0077 R9077 R9977 R0977"{
setBatchMode(true);
splitRegions();
setBatchMode("exit and display");
}
macro "Z Project Action Tool - C888 R0099 R3399 R6699 R9999"{
setBatchMode(true);
projectImages();
setBatchMode("exit and display");
}
macro "Setup Action Tool - C888 T5f16S"{
setup();
}
macro "Analyse Single Image Action Tool - C888 T5f161"{
setBatchMode(true);
//Set Directory
dir = getInfo("image.directory");
output_dir = dir+"Output"+File.separator;
if(!File.exists(output_dir)){
print(output_dir);
File.makeDirectory(output_dir);
}
start = getTime();
//Image properties
title = getTitle;
prefix = substring(title,0,lastIndexOf(title,suffix));
setFileNames(prefix);
id = getImageID;
//Spot detection
roiManager("reset");
if(spots_ch1>0){
args = newArray(1,spots_ch1_segmentation_method,spots_ch1_threshold,spots_ch1_fixed_threshold_value,spots_ch1_filter_scale,spots_ch1_max_scale_multi,spots_ch1_min_area,spots_ch1_max_area,spots_ch1_exclusion,spots_ch1_exclusion_threshold,spots_ch1_exclusion_fixed_threshold,spots_ch1_exclusion_scale,spots_ch1_exclusion_min_area);
snr = segmentSpots(id,args,false);
if(snr>0){
roiManager("Save",spots_ch1_roi_set);
roiManager("reset");
}
}
if(spots_ch2>0){
args = newArray(2,spots_ch2_segmentation_method,spots_ch2_threshold,spots_ch2_fixed_threshold_value,spots_ch2_filter_scale,spots_ch2_max_scale_multi,spots_ch2_min_area,spots_ch2_max_area,spots_ch2_exclusion,spots_ch2_exclusion_threshold,spots_ch2_exclusion_fixed_threshold,spots_ch2_exclusion_scale,spots_ch2_exclusion_min_area);
snr = segmentSpots(id,args,false);
if(snr>0)
{
roiManager("Save",spots_ch2_roi_set);
roiManager("reset");
}
}
if(spots_ch3>0){
args = newArray(3,spots_ch3_segmentation_method,spots_ch3_threshold,spots_ch3_fixed_threshold_value,spots_ch3_filter_scale,spots_ch3_max_scale_multi,spots_ch3_min_area,spots_ch3_max_area,spots_ch3_exclusion,spots_ch3_exclusion_threshold,spots_ch3_exclusion_fixed_threshold,spots_ch3_exclusion_scale,spots_ch3_exclusion_min_area);
snr = segmentSpots(id,args,false);
if(snr>0)
{
roiManager("Save",spots_ch3_roi_set);
roiManager("reset");
}
}
if(spots_ch4>0){
args = newArray(4,spots_ch4_segmentation_method,spots_ch4_threshold,spots_ch4_fixed_threshold_value,spots_ch4_filter_scale,spots_ch4_max_scale_multi,spots_ch4_min_area,spots_ch4_max_area,spots_ch4_exclusion,spots_ch4_exclusion_threshold,spots_ch4_exclusion_fixed_threshold,spots_ch4_exclusion_scale,spots_ch4_exclusion_min_area);
snr = segmentSpots(id,args,false);
if(snr>0)
{
roiManager("Save",spots_ch4_roi_set);
roiManager("reset");
}
}
//Spot analysis
readout = analyzeRegions(id);
if(readout){
summaryResults();
}
print((getTime()-start)/1000,"sec");
print("Analysis Done");
setBatchMode("exit and display");
}
macro "Batch Analysis Action Tool - C888 T5f16#"{
erase(1);
setBatchMode(true);
//Set Directory
setDirectory();
prefixes = scanFiles();
fields = prefixes.length;
setup();
start = getTime();
//Loop over images
for(i=0;i<fields;i++){
//Open image
prefix = prefixes[i];
file = prefix+suffix;
setFileNames(prefix);
print(i+1,"/",fields,":",prefix);
path = dir+file;
run("Bio-Formats Importer", "open=["+path+"] color_mode=Default open_files view=Hyperstack stack_order=XYCZT");
id = getImageID;
//Spot detection
roiManager("reset");
if(spots_ch1>0){
args = newArray(1,spots_ch1_segmentation_method,spots_ch1_threshold,spots_ch1_fixed_threshold_value,spots_ch1_filter_scale,spots_ch1_max_scale_multi,spots_ch1_min_area,spots_ch1_max_area,spots_ch1_exclusion,spots_ch1_exclusion_threshold,spots_ch1_exclusion_fixed_threshold,spots_ch1_exclusion_scale,spots_ch1_exclusion_min_area);
snr = segmentSpots(id,args,false);
if(snr>0){
roiManager("Save",spots_ch1_roi_set);
roiManager("reset");
}
}
if(spots_ch2>0){
args = newArray(2,spots_ch2_segmentation_method,spots_ch2_threshold,spots_ch2_fixed_threshold_value,spots_ch2_filter_scale,spots_ch2_max_scale_multi,spots_ch2_min_area,spots_ch2_max_area,spots_ch2_exclusion,spots_ch2_exclusion_threshold,spots_ch2_exclusion_fixed_threshold,spots_ch2_exclusion_scale,spots_ch2_exclusion_min_area);
snr = segmentSpots(id,args,false);
if(snr>0)
{
roiManager("Save",spots_ch2_roi_set);
roiManager("reset");
}
}
if(spots_ch3>0){
args = newArray(3,spots_ch3_segmentation_method,spots_ch3_threshold,spots_ch3_fixed_threshold_value,spots_ch3_filter_scale,spots_ch3_max_scale_multi,spots_ch3_min_area,spots_ch3_max_area,spots_ch3_exclusion,spots_ch3_exclusion_threshold,spots_ch3_exclusion_fixed_threshold,spots_ch3_exclusion_scale,spots_ch3_exclusion_min_area);
snr = segmentSpots(id,args,false);
if(snr>0)
{
roiManager("Save",spots_ch3_roi_set);
roiManager("reset");
}
}
if(spots_ch4>0){
args = newArray(4,spots_ch4_segmentation_method,spots_ch4_threshold,spots_ch4_fixed_threshold_value,spots_ch4_filter_scale,spots_ch4_max_scale_multi,spots_ch4_min_area,spots_ch4_max_area,spots_ch4_exclusion,spots_ch4_exclusion_threshold,spots_ch4_exclusion_fixed_threshold,spots_ch4_exclusion_scale,spots_ch4_exclusion_min_area);
snr = segmentSpots(id,args,false);
if(snr>0)
{
roiManager("Save",spots_ch4_roi_set);
roiManager("reset");
}
}
//Spot analysis
readout = analyzeRegions(id);
selectImage(id); close;
if(readout){
summaryResults();
}
erase(0);
}
//Concatenate results
concatenateResults();
saveAs(".txt",output_dir+"ConcatenatedResults.txt");
print((getTime()-start)/1000,"sec");
if(isOpen("Log")){
selectWindow("Log");
saveAs("txt",log_path);
}
print("Complete Analysis Done");
setBatchMode("exit and display");
}
macro "Segment Spots Action Tool - C888 H00f5f8cf3f0800 Cf88 o3222 o4b22 o5822 o7522 oa822"
{
erase(0);
setBatchMode(true);
id = getImageID;
c = getNumber("Spot Channel",1);
if(c == 1){
args = newArray(1,spots_ch1_segmentation_method,spots_ch1_threshold,spots_ch1_fixed_threshold_value,spots_ch1_filter_scale,spots_ch1_max_scale_multi,spots_ch1_min_area,spots_ch1_max_area,spots_ch1_exclusion,spots_ch1_exclusion_threshold,spots_ch1_exclusion_fixed_threshold,spots_ch1_exclusion_scale,spots_ch1_exclusion_min_area);
}
if(c == 2){
args = newArray(2,spots_ch2_segmentation_method,spots_ch2_threshold,spots_ch2_fixed_threshold_value,spots_ch2_filter_scale,spots_ch2_max_scale_multi,spots_ch2_min_area,spots_ch2_max_area,spots_ch2_exclusion,spots_ch2_exclusion_threshold,spots_ch2_exclusion_fixed_threshold,spots_ch2_exclusion_scale,spots_ch2_exclusion_min_area);
}
if(c == 3){
args = newArray(3,spots_ch3_segmentation_method,spots_ch3_threshold,spots_ch3_fixed_threshold_value,spots_ch3_filter_scale,spots_ch3_max_scale_multi,spots_ch3_min_area,spots_ch3_max_area,spots_ch3_exclusion,spots_ch3_exclusion_threshold,spots_ch3_exclusion_fixed_threshold,spots_ch3_exclusion_scale,spots_ch3_exclusion_min_area);
}
if(c == 4){
args = newArray(4,spots_ch4_segmentation_method,spots_ch4_threshold,spots_ch4_fixed_threshold_value,spots_ch4_filter_scale,spots_ch4_max_scale_multi,spots_ch4_min_area,spots_ch4_max_area,spots_ch4_exclusion,spots_ch4_exclusion_threshold,spots_ch4_exclusion_fixed_threshold,spots_ch4_exclusion_scale,spots_ch4_exclusion_min_area);
}
snr = segmentSpots(id,args,true);
selectImage(id);
toggleOverlay();
setBatchMode("exit and display");
run("Tile");
}
macro "Toggle Overlay Action Tool - Caaa O11ee"{
toggleOverlay();
}
macro "[t] Toggle Overlay"{
toggleOverlay();
}
macro "Verification Stack Action Tool - C888 T5f16V"{
erase(1);
setBatchMode(true);
setDirectory();
prefixes = scanFiles();
names = prefixes;
createOverlay(names);
setBatchMode("exit and display");
run("Channels Tool... ");
}
macro "Concatenate Results Action Tool - C888 T5f16C"
{
setBatchMode(true);
setDirectory();
concatenateResults();
saveAs(".txt",output_dir+"ConcatenatedResults.txt");
setBatchMode("exit and display");
}
//---------------------------------------------------------------------Functions---------------------------------------------------------------------
function splitRegions(){
erase(1);
//GUI
Dialog.create("Split Fields...");
Dialog.addString("Destination Directory Name","Export",25);
Dialog.addString("Add a prefix","",25);
Dialog.addChoice("Import format",file_types,".mvd2");
Dialog.addChoice("Export format",file_types,suffix);
Dialog.addNumber("Channels",4);
Dialog.addChoice("Dimension order",dimensions, order);
Dialog.show;
dest = Dialog.getString;
pre = Dialog.getString;
ext = Dialog.getChoice;
suffix = Dialog.getChoice;
channels = Dialog.getNumber;
order = Dialog.getChoice;
//Set directory
dir = getDirectory("");
file_list = getFileList(dir);
destination_dir = dir+dest+File.separator;
File.makeDirectory(destination_dir);
//Loop over images: open and save individual files
for(i=0;i<file_list.length;i++){
path = dir+file_list[i];
if(endsWith(path,ext)){
run("Bio-Formats Importer", "open=["+path+"] color_mode=Default open_all_series view=Hyperstack ");
while(nImages>0){
selectImage(nImages);
id=getImageID();
selectImage(id);
run("Stack to Hyperstack...", "order="+order+" channels="+channels+" slices="+(nSlices/channels)+" frames=1 display=Color");
id=getImageID();
title = getTitle;
saveAs(suffix,destination_dir+title+suffix);
selectImage(id); close;
}
}
}
print("Done");
}
function projectImages(){
erase(1);
//GUI
Dialog.create("Project Images...");
Dialog.addChoice("Import format",file_types,".tif");
Dialog.addChoice("Export format",file_types,suffix);
Dialog.addNumber("Z step", z_step, 0, 2, micron);
Dialog.addNumber("# Slices of Substack (0=all)", z_substack, 0, 2, "");
Dialog.show;
ext = Dialog.getChoice;
suffix = Dialog.getChoice;
z_step = Dialog.getNumber();
z_substack = Dialog.getNumber();
//Set directory
dir = getDirectory("");
file_list = getFileList(dir);
folder_subSample = "SubSample";
dest_subSample = dir+folder_subSample+File.separator;
File.makeDirectory(dest_subSample);
folder_max = "MaxProj";
dest_max = dest_subSample+folder_max+File.separator;
File.makeDirectory(dest_max);
//Loop over images: substack if needed+ max projection
for(i=0;i<file_list.length;i++){
path = dir+file_list[i];
if(endsWith(path,ext)){
print(i+1);
//Open image + check properties
run("Bio-Formats Importer", "open=["+path+"] color_mode=Default concatenate_series open_all_series view=Hyperstack ");
ns = nSlices;
run("Stack to Hyperstack...", "order=xyczt(default) channels="+channels+" slices="+ns/channels+" frames=1 display=Color");
id = getImageID;
title = getTitle;
print(title);
getDimensions(width, height, channels, slices, frames);
getVoxelSize(width, height, depth, unit);
print("Res. z: " + depth + " " + unit);
//Subsample if z-resolution < z_step
run("Duplicate...", "duplicate");
idDup=getImageID();
if(unit!="microns"){
showMessageWithCancel("Maximum intensity projection","Add image calibration in microns");
selectImage(id); close;
selectImage(idDup); close;
break;
}else{
if(z_step>2){
showMessageWithCancel("Maximum intensity projection","Z-range not included in script");
selectImage(id); close;
selectImage(idDup); close;
break;
}
if(depth>z_step){
showMessageWithCancel("Maximum intensity projection","Voxel depth larger than z_step");
selectImage(id); close;
selectImage(idDup); close;
break;
}
if(depth<z_step){
factor=z_step/depth;
s=2;
selectImage(idDup);
getDimensions(width, height, channels, slices, frames);
finalSlices=Math.ceil(slices/factor);
print("Number of slices before subsampling: "+ slices);
while(slices>finalSlices){
selectImage(idDup);
Stack.setSlice(s);
run("Delete Slice", "delete=slice");
selectImage(idDup);
getDimensions(width, height, channels, slices, frames);
s=s+1;
}
selectImage(idDup);
getVoxelSize(width, height, depth, unit);
setVoxelSize(width, height, z_step, unit);
selectImage(idDup);
getDimensions(width, height, channels, slices, frames);
print("Number of slices after subsampling: "+ slices);
}
}
selectImage(idDup);
saveAs(suffix,dest_subSample+"SubSample_"+title+suffix);
//Addition substack if needed
if(z_substack!=0){
selectImage(idDup);
getDimensions(width, height, channels, slices, frames);
print("Slices in stack: " + slices );
if(slices>z_substack){
zMin=round((slices-z_substack)/2)+1;
zMax=zMin+z_substack-1;
selectImage(idDup);
run("Duplicate...", "duplicate slices="+zMin+"-"+zMax);
idDupSub=getImageID();
getDimensions(width, height, channels, slices, frames);
print(slices +" slices in substack: "+zMin+" - "+zMax);
run("Z Project...", "projection=[Max Intensity]");
zid = getImageID;
selectImage(zid); saveAs(suffix,dest_max+"Max_"+title+suffix);
selectImage(idDupSub); close;
selectImage(zid); close;
}else{
run("Z Project...", "projection=[Max Intensity]");
zid = getImageID;
selectImage(zid); saveAs(suffix,dest_max+"Max_"+title+suffix);
selectImage(zid); close;
}
}else{
run("Z Project...", "projection=[Max Intensity]");
zid = getImageID;
selectImage(zid); saveAs(suffix,dest_max+"Max_"+title+suffix);
selectImage(zid); close;
}
selectImage(id); close;
selectImage(idDup); close;
}
}
print("Done");
}
function setOptions(){
run("Options...", "iterations=1 count=1");
run("Colors...", "foreground=white correct_background=black selection=yellow");
run("Overlay Options...", "stroke=red width=1 fill=none");
setBackgroundColor(0, 0, 0);
setForegroundColor(255,255,255);
}
function getMoment(){
MonthNames = newArray("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
DayNames = newArray("Sun", "Mon","Tue","Wed","Thu","Fri","Sat");
getDateAndTime(year, month, dayOfWeek, dayOfMonth, hour, minute, second, msec);
TimeString ="Date: "+DayNames[dayOfWeek]+" ";
if (dayOfMonth<10) {TimeString = TimeString+"0";}
TimeString = TimeString+dayOfMonth+"-"+MonthNames[month]+"-"+year+"\nTime: ";
if (hour<10) {TimeString = TimeString+"0";}
TimeString = TimeString+hour+":";
if (minute<10) {TimeString = TimeString+"0";}
TimeString = TimeString+minute+":";
if (second<10) {TimeString = TimeString+"0";}
TimeString = TimeString+second;
return TimeString;
}
function erase(all){
if(all){print("\\Clear");run("Close All");}
run("Clear Results");
roiManager("reset");
run("Collect Garbage");
}
function setDirectory(){
dir = getDirectory("Choose a Source Directory");
file_list = getFileList(dir);
output_dir = dir+"Output"+File.separator;
if(!File.exists(output_dir))File.makeDirectory(output_dir);
log_path = output_dir+"Log.txt";
}
function setFileNames(prefix){
spots_ch1_roi_set = output_dir+prefix+"_spots_ch1_roi_set.zip";
spots_ch2_roi_set = output_dir+prefix+"_spots_ch2_roi_set.zip";
spots_ch3_roi_set = output_dir+prefix+"_spots_ch3_roi_set.zip";
spots_ch4_roi_set = output_dir+prefix+"_spots_ch4_roi_set.zip";
spots_ch1_results = output_dir+prefix+"_spots_ch1_results.txt";
spots_ch2_results = output_dir+prefix+"_spots_ch2_results.txt";
spots_ch3_results = output_dir+prefix+"_spots_ch3_results.txt";
spots_ch4_results = output_dir+prefix+"_spots_ch4_results.txt";
results = output_dir+prefix+"_summary.txt";
}
function scanFiles(){
prefixes = newArray(0);
for(i=0;i<file_list.length;i++){
path = dir+file_list[i];
if(endsWith(path,suffix) && indexOf(path,"flatfield")<0){
print(path);
prefixes = Array.concat(prefixes,substring(file_list[i],0,lastIndexOf(file_list[i],suffix)));
}
}
return prefixes;
}
function setup(){
setOptions();
Dialog.create("Colocalisation neurotransmitters");
Dialog.setInsets(0,0,0);
Dialog.addChoice("Image Type", file_types, suffix);
Dialog.setInsets(0,0,0);
Dialog.addNumber("Pixel Size", pixel_size, 3, 5, micron+" (only if not calibrated)");
Dialog.setInsets(0,0,0);
Dialog.addNumber("Number of Channels", channels, 0, 5, " ");
Dialog.setInsets(0,0,0);
Dialog.addMessage("Spot channels:");
Dialog.setInsets(0,0,0);
labels = newArray(4);defaults = newArray(4);
labels[0] = "Channel 1"; defaults[0] = spots_ch1;
labels[1] = "Channel 2"; defaults[1] = spots_ch2;
labels[2] = "Channel 3"; defaults[2] = spots_ch3;
labels[3] = "Channel 4"; defaults[3] = spots_ch4;
Dialog.setInsets(0,0,0);
Dialog.addCheckboxGroup(1,4,labels,defaults);
Dialog.setInsets(0,0,0);
Dialog.show();
print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
suffix = Dialog.getChoice(); print("Image type:",suffix);
pixel_size = Dialog.getNumber(); print("Pixel size:",pixel_size);
channels = Dialog.getNumber(); print("Channels:",channels);
spots_ch1 = Dialog.getCheckbox(); print("Spots channel 1",spots_ch1);
spots_ch2 = Dialog.getCheckbox(); print("Spots channel 2",spots_ch2);
spots_ch3 = Dialog.getCheckbox(); print("Spots channel 3:",spots_ch3);
spots_ch4 = Dialog.getCheckbox(); print("Spots channel 4:",spots_ch4);
Dialog.create("Spot Analysis");
if(spots_ch1){
Dialog.setInsets(0,0,0);
Dialog.addMessage("----------------------------------------------------------------------------------------------- Channel 1 -----------------------------------------------------------------------------------------------", 14, "#ff0000");
Dialog.setInsets(0,0,0);
Dialog.addChoice("Spot Segmentation Method", spot_segmentation_methods, spots_ch1_segmentation_method);
Dialog.addToSameRow();
Dialog.addNumber("Gauss/Laplace Scale", spots_ch1_filter_scale, 2, 4, "");
Dialog.addToSameRow();
Dialog.addNumber("Max Scale Multi", spots_ch1_max_scale_multi, 2, 4, "");
Dialog.setInsets(0,0,0);
Dialog.addChoice("Threshold Algorithm", threshold_methods, spots_ch1_threshold);
Dialog.addToSameRow();
Dialog.addNumber("Fixed Threshold", spots_ch1_fixed_threshold_value, 2, 4, "");
Dialog.setInsets(0,0,0);
Dialog.addNumber("Minimum Spot Size", spots_ch1_min_area, 2, 4, micron);
Dialog.addToSameRow();
Dialog.addNumber("Maximum Spot Size", spots_ch1_max_area, 2, 4, micron);
Dialog.setInsets(0,0,0);
Dialog.addMessage("");
Dialog.setInsets(0,0,0);
Dialog.addCheckbox("Exclusion cell bodies", spots_ch1_exclusion);
Dialog.setInsets(0,0,0);
Dialog.addChoice("Exclusion: Threshold Algorithm", threshold_methods, spots_ch1_exclusion_threshold);
Dialog.addToSameRow();
Dialog.addNumber("Exclusion: Fixed Threshold", spots_ch1_exclusion_fixed_threshold, 2, 4, "");
Dialog.addToSameRow();
Dialog.addNumber("Exclusion: Median filter Scale", spots_ch1_exclusion_scale, 2, 4, "");
Dialog.addToSameRow();
Dialog.addNumber("Exclusion: Minimum area", spots_ch1_exclusion_min_area, 2, 4, micron);
}
if(spots_ch2){
Dialog.setInsets(0,0,0);
Dialog.addMessage("----------------------------------------------------------------------------------------------- Channel 2 -----------------------------------------------------------------------------------------------", 14, "#ff0000");
Dialog.setInsets(0,0,0);
Dialog.addChoice("Spot Segmentation Method", spot_segmentation_methods, spots_ch2_segmentation_method);
Dialog.addToSameRow();
Dialog.addNumber("Gauss/Laplace Scale", spots_ch2_filter_scale, 2, 4, "");
Dialog.addToSameRow();
Dialog.addNumber("Max Scale Multi", spots_ch2_max_scale_multi, 2, 4, "");
Dialog.setInsets(0,0,0);
Dialog.addChoice("Threshold Algorithm", threshold_methods, spots_ch2_threshold);
Dialog.addToSameRow();
Dialog.addNumber("Fixed Threshold", spots_ch2_fixed_threshold_value, 2, 4, "");
Dialog.setInsets(0,0,0);
Dialog.addNumber("Minimum Spot Size", spots_ch2_min_area, 2, 4, micron);
Dialog.addToSameRow();
Dialog.addNumber("Maximum Spot Size", spots_ch2_max_area, 2, 4, micron);
Dialog.setInsets(0,0,0);
Dialog.addMessage("");
Dialog.setInsets(0,0,0);
Dialog.addCheckbox("Exclusion cell bodies", spots_ch2_exclusion);
Dialog.setInsets(0,0,0);
Dialog.addChoice("Exclusion: Threshold Algorithm", threshold_methods, spots_ch2_exclusion_threshold);
Dialog.addToSameRow();
Dialog.addNumber("Exclusion: Fixed Threshold", spots_ch2_exclusion_fixed_threshold, 2, 4, "");
Dialog.addToSameRow();
Dialog.addNumber("Exclusion: Median filter Scale", spots_ch2_exclusion_scale, 2, 4, "");
Dialog.addToSameRow();
Dialog.addNumber("Exclusion: Minimum area", spots_ch2_exclusion_min_area, 2, 4, micron);
}
if(spots_ch3){
Dialog.setInsets(0,0,0);
Dialog.addMessage("----------------------------------------------------------------------------------------------- Channel 3 -----------------------------------------------------------------------------------------------", 14, "#ff0000");
Dialog.setInsets(0,0,0);
Dialog.addChoice("Spot Segmentation Method", spot_segmentation_methods, spots_ch3_segmentation_method);
Dialog.addToSameRow();
Dialog.addNumber("Gauss/Laplace Scale", spots_ch3_filter_scale, 2, 4, "");
Dialog.addToSameRow();
Dialog.addNumber("Max Scale Multi", spots_ch3_max_scale_multi, 2, 4, "");
Dialog.setInsets(0,0,0);
Dialog.addChoice("Threshold Algorithm", threshold_methods, spots_ch3_threshold);
Dialog.addToSameRow();
Dialog.addNumber("Fixed Threshold", spots_ch3_fixed_threshold_value, 2, 4, "");
Dialog.setInsets(0,0,0);
Dialog.addNumber("Minimum Spot Size", spots_ch3_min_area, 2, 4, micron);
Dialog.addToSameRow();
Dialog.addNumber("Maximum Spot Size", spots_ch3_max_area, 2, 4, micron);
Dialog.setInsets(0,0,0);
Dialog.addMessage("");
Dialog.setInsets(0,0,0);
Dialog.addCheckbox("Exclusion cell bodies", spots_ch3_exclusion);
Dialog.setInsets(0,0,0);
Dialog.addChoice("Exclusion: Threshold Algorithm", threshold_methods, spots_ch3_exclusion_threshold);
Dialog.addToSameRow();
Dialog.addNumber("Exclusion: Fixed Threshold", spots_ch3_exclusion_fixed_threshold, 2, 4, "");
Dialog.addToSameRow();
Dialog.addNumber("Exclusion: Median filter Scale", spots_ch3_exclusion_scale, 2, 4, "");
Dialog.addToSameRow();
Dialog.addNumber("Exclusion: Minimum area", spots_ch3_exclusion_min_area, 2, 4, micron);
}
if(spots_ch4){
Dialog.setInsets(0,0,0);
Dialog.addMessage("----------------------------------------------------------------------------------------------- Channel 4 -----------------------------------------------------------------------------------------------", 14, "#ff0000");
Dialog.setInsets(0,0,0);
Dialog.addChoice("Spot Segmentation Method", spot_segmentation_methods, spots_ch4_segmentation_method);
Dialog.addToSameRow();
Dialog.addNumber("Gauss/Laplace Scale", spots_ch4_filter_scale, 2, 4, "");
Dialog.addToSameRow();
Dialog.addNumber("Max Scale Multi", spots_ch4_max_scale_multi, 2, 4, "");
Dialog.setInsets(0,0,0);
Dialog.addChoice("Threshold Algorithm", threshold_methods, spots_ch4_threshold);
Dialog.addToSameRow();
Dialog.addNumber("Fixed Threshold", spots_ch4_fixed_threshold_value, 2, 4, "");
Dialog.setInsets(0,0,0);
Dialog.addNumber("Minimum Spot Size", spots_ch4_min_area, 2, 4, micron);
Dialog.addToSameRow();
Dialog.addNumber("Maximum Spot Size", spots_ch4_max_area, 2, 4, micron);
Dialog.setInsets(0,0,0);
Dialog.addMessage("");
Dialog.setInsets(0,0,0);
Dialog.addCheckbox("Exclusion cell bodies", spots_ch4_exclusion);
Dialog.setInsets(0,0,0);
Dialog.addChoice("Exclusion: Threshold Algorithm", threshold_methods, spots_ch4_exclusion_threshold);
Dialog.addToSameRow();
Dialog.addNumber("Exclusion: Fixed Threshold", spots_ch4_exclusion_fixed_threshold, 2, 4, "");
Dialog.addToSameRow();
Dialog.addNumber("Exclusion: Median filter Scale", spots_ch4_exclusion_scale, 2, 4, "");
Dialog.addToSameRow();
Dialog.addNumber("Exclusion: Minimum area", spots_ch4_exclusion_min_area, 2, 4, micron);
}
Dialog.show();
if(spots_ch1){
spots_ch1_segmentation_method = Dialog.getChoice(); print("CH1 - Spot Segmentation Method:",spots_ch1_segmentation_method);
spots_ch1_filter_scale = Dialog.getNumber(); print("CH1 - Laplace scale:",spots_ch1_filter_scale);
spots_ch1_max_scale_multi = Dialog.getNumber(); print("CH1 - Max multi scale:",spots_ch1_max_scale_multi);
spots_ch1_threshold = Dialog.getChoice(); print("CH1 - Spot autoThreshold:",spots_ch1_threshold);
spots_ch1_fixed_threshold_value = Dialog.getNumber(); print("CH1 - Fixed threshold:",spots_ch1_fixed_threshold_value);
spots_ch1_min_area = Dialog.getNumber(); print("CH1 - Min. spot size:", spots_ch1_min_area);
spots_ch1_max_area = Dialog.getNumber(); print("CH1 - Max. spot size:",spots_ch1_max_area);
spots_ch1_exclusion = Dialog.getCheckbox(); print("CH1 - Exclusion cell bodies:",spots_ch1_exclusion);
spots_ch1_exclusion_threshold = Dialog.getChoice(); print("CH1 - Exclusion auto threshold:",spots_ch1_exclusion_threshold);
spots_ch1_exclusion_fixed_threshold = Dialog.getNumber(); print("CH1 - Exclusion fixed threshold:",spots_ch1_exclusion_fixed_threshold);
spots_ch1_exclusion_scale = Dialog.getNumber(); print("CH1 - Exclusion median filter scale:", spots_ch1_exclusion_scale);
spots_ch1_exclusion_min_area = Dialog.getNumber(); print("CH1 - Exclusion min area:", spots_ch1_exclusion_min_area);
}
if(spots_ch2){
spots_ch2_segmentation_method = Dialog.getChoice(); print("CH2 - Spot Segmentation Method:",spots_ch2_segmentation_method);
spots_ch2_filter_scale = Dialog.getNumber(); print("CH2 - Laplace scale:",spots_ch2_filter_scale);
spots_ch2_max_scale_multi = Dialog.getNumber(); print("CH2 - Max multi scale:",spots_ch2_max_scale_multi);
spots_ch2_threshold = Dialog.getChoice(); print("CH2 - Spot autoThreshold:",spots_ch2_threshold);
spots_ch2_fixed_threshold_value = Dialog.getNumber(); print("CH2 - Fixed threshold:",spots_ch2_fixed_threshold_value);
spots_ch2_min_area = Dialog.getNumber(); print("CH2 - Min. spot size:", spots_ch2_min_area);
spots_ch2_max_area = Dialog.getNumber(); print("CH2 - Max. spot size:",spots_ch2_max_area);
spots_ch2_exclusion = Dialog.getCheckbox(); print("CH2 - Exclusion cell bodies:",spots_ch2_exclusion);
spots_ch2_exclusion_threshold = Dialog.getChoice(); print("CH2 - Exclusion auto threshold:",spots_ch2_exclusion_threshold);
spots_ch2_exclusion_fixed_threshold = Dialog.getNumber(); print("CH2 - Exclusion fixed threshold:",spots_ch2_exclusion_fixed_threshold);
spots_ch2_exclusion_scale = Dialog.getNumber(); print("CH2 - Exclusion median filter scale:", spots_ch2_exclusion_scale);
spots_ch2_exclusion_min_area = Dialog.getNumber(); print("CH2 - Exclusion min area:", spots_ch2_exclusion_min_area);
}
if(spots_ch3){
spots_ch3_segmentation_method = Dialog.getChoice(); print("CH3 - Spot Segmentation Method:",spots_ch3_segmentation_method);
spots_ch3_filter_scale = Dialog.getNumber(); print("CH3 - Laplace scale:",spots_ch3_filter_scale);
spots_ch3_max_scale_multi = Dialog.getNumber(); print("CH3 - Max multi scale:",spots_ch3_max_scale_multi);
spots_ch3_threshold = Dialog.getChoice(); print("CH3 - Spot autoThreshold:",spots_ch3_threshold);
spots_ch3_fixed_threshold_value = Dialog.getNumber(); print("CH3 - Fixed threshold:",spots_ch3_fixed_threshold_value);
spots_ch3_min_area = Dialog.getNumber(); print("CH3 - Min. spot size:", spots_ch3_min_area);
spots_ch3_max_area = Dialog.getNumber(); print("CH3 - Max. spot size:",spots_ch3_max_area);
spots_ch3_exclusion = Dialog.getCheckbox(); print("CH3 - Exclusion cell bodies:",spots_ch3_exclusion);
spots_ch3_exclusion_threshold = Dialog.getChoice(); print("CH3 - Exclusion auto threshold:",spots_ch3_exclusion_threshold);
spots_ch3_exclusion_fixed_threshold = Dialog.getNumber(); print("CH3 - Exclusion fixed threshold:",spots_ch3_exclusion_fixed_threshold);
spots_ch3_exclusion_scale = Dialog.getNumber(); print("CH3 - Exclusion median filter scale:", spots_ch3_exclusion_scale);
spots_ch3_exclusion_min_area = Dialog.getNumber(); print("CH3 - Exclusion min area:", spots_ch3_exclusion_min_area);
}
if(spots_ch4){
spots_ch4_segmentation_method = Dialog.getChoice(); print("CH4 - Spot Segmentation Method:",spots_ch4_segmentation_method);
spots_ch4_filter_scale = Dialog.getNumber(); print("CH4 - Laplace scale:",spots_ch4_filter_scale);
spots_ch4_max_scale_multi = Dialog.getNumber(); print("CH4 - Max multi scale:",spots_ch4_max_scale_multi);
spots_ch4_threshold = Dialog.getChoice(); print("CH4 - Spot autoThreshold:",spots_ch4_threshold);
spots_ch4_fixed_threshold_value = Dialog.getNumber(); print("CH4 - Fixed threshold:",spots_ch4_fixed_threshold_value);
spots_ch4_min_area = Dialog.getNumber(); print("CH4 - Min. spot size:", spots_ch4_min_area);
spots_ch4_max_area = Dialog.getNumber(); print("CH4 - Max. spot size:",spots_ch4_max_area);
spots_ch4_exclusion = Dialog.getCheckbox(); print("CH4 - Exclusion cell bodies:",spots_ch4_exclusion);
spots_ch4_exclusion_threshold = Dialog.getChoice(); print("CH4 - Exclusion auto threshold:",spots_ch4_exclusion_threshold);
spots_ch4_exclusion_fixed_threshold = Dialog.getNumber(); print("CH4 - Exclusion fixed threshold:",spots_ch4_exclusion_fixed_threshold);
spots_ch4_exclusion_scale = Dialog.getNumber(); print("CH4 - Exclusion median filter scale:", spots_ch4_exclusion_scale);
spots_ch4_exclusion_min_area = Dialog.getNumber(); print("CH4 - Exclusion min area:", spots_ch4_exclusion_min_area);
}
print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
}
function calibrateImage(id)
{
getPixelSize(unit, pixelWidth, pixelHeight);
if(unit!=micron)run("Properties...", " unit="+micron+" pixel_width="+pixel_size+" pixel_height="+pixel_size);
else pixel_size = pixelWidth;
}
function decalibrateImage(id)
{
getPixelSize(unit, pixelWidth, pixelHeight);
if(unit!="pixel")run("Properties...", " unit=pixel pixel_width=1 pixel_height=1");
}
function segmentSpots(id,args,booleanDebug){
//Extract features
spot_channel = args[0];
spot_segmentation_method = args[1];
spot_threshold_method = args[2];
spot_fixed_threshold_value = args[3];
scale = args[4];
max_scale_multi = args[5];
spot_min_area = args[6];
spot_max_area = args[7];
exclusion = args[8];
exclusion_segmentation_method = args[9];
exclusion_fixed_threshold = args[10];
exclusion_scale = args[11];
exclusion_min_area = args[12];
//Image properties
selectImage(id);
run("Select None");
if(Stack.isHyperstack){
run("Duplicate...", "title=copy duplicate channels="+spot_channel);
}
else{
setSlice(spot_channel);
run("Duplicate...","title=copy ");
}
cid = getImageID;
decalibrateImage(cid);
selectImage(cid);
title = getTitle;
//Preprocessing
if(spot_segmentation_method=="Gauss"){
run("Duplicate...","title=Gauss");
run("Gaussian Blur...", "sigma="+scale);
run("Invert");
idLap = getImageID;
}
else if(spot_segmentation_method=="Laplace"){
run("FeatureJ Laplacian", "compute smoothing="+scale);
idLap = getImageID;
}
else if(spot_segmentation_method=="Multi-Scale") {
cid=getImageID;
title=getTitle;
scale=max_scale_multi;
e=0;
while(e<scale){
e++;
selectImage(cid);
run("FeatureJ Laplacian", "compute smoothing="+e);
selectWindow(title+" Laplacian");
run("Multiply...","value="+e*e);
rename("scale "+e);
eid = getImageID;
if(e>1)
{
selectImage(eid);run("Select All");run("Copy");close;
selectWindow("scale 1");run("Add Slice");run("Paste");
}
}
selectWindow("scale 1");
nlid = getImageID;
selectImage(nlid);
run("Z Project...", "start=1 projection=[Sum Slices]");
idLap = getImageID;
selectImage(nlid); close;
}
//Thresholding
selectImage(idLap);
run("Duplicate...","title=lapBinary");
idLapBinary=getImageID();
selectImage(idLapBinary);
setOption("BlackBackground",false);
if(spot_threshold_method=="Fixed"){
setAutoThreshold("Default ");
getThreshold(mit,mat);
print("Original threshold settings:",mit,mat);
setThreshold(minOf(mit,-spot_fixed_threshold_value),-spot_fixed_threshold_value);
print("Fixed threshold settings:",minOf(mit,-spot_fixed_threshold_value),-spot_fixed_threshold_value);
}
else{
setAutoThreshold(spot_threshold_method+" ");
}
getThreshold(mit,mat);
print("Threshold:",mit,mat);
run("Convert to Mask");
//Max finding
selectImage(idLap);
diameter=sqrt(spot_min_area/PI)*2;
run("Find Maxima...", "prominence="+diameter+" light output=[Segmented Particles]");
idRegions=getImageID();
rename("Regions");
imageCalculator("AND","lapBinary", "Regions");
//Exclude cell bodies
if(exclusion){
selectImage(cid);
run("Duplicate...", " ");
idExcl=getImageID();
run("Median...", "radius="+exclusion_scale);
if(exclusion_segmentation_method=="Fixed"){
getStatistics(area, mean, min, max, std, histogram);
setAutoThreshold("Default ");
setThreshold(exclusion_fixed_threshold,max);
}
else{
setAutoThreshold(exclusion_segmentation_method+" ");
}
run("Convert to Mask");
calibrateImage(idExcl);
run("Analyze Particles...", "size="+exclusion_min_area+"-Infinity show=Masks");
idMaskExcl=getImageID();
rename("Exclude-CH"+spot_channel);
run("Invert");
imageCalculator("AND","lapBinary", "Exclude-CH"+spot_channel);
selectImage(idExcl); close();
if(!booleanDebug){
selectImage(idMaskExcl); close();
}
}
//Measurements
selectImage(idLapBinary);
calibrateImage(idLapBinary);
run("Set Measurements...", " area min mean redirect=None decimal=4");
run("Analyze Particles...", "size="+spot_min_area+"-"+spot_max_area+" circularity=0.00-1.00 show=Nothing display clear include add");
snr = roiManager("count");
print(snr,"spots");
//To avoid excessive spot finding when there are no true spots
if(snr>10000){
print("excessive number, hence reset");
snr=0;
roiManager("reset");
}
selectImage(idLap); close;
selectImage(cid); close;
selectImage(idLapBinary); close;
selectImage(idRegions); close;
return snr;
}
function analyzeRegions(id)
{
erase(0);
mask = 0;
readout = 1;
selectImage(id);
calibrateImage(id);
// analyze spot rois
if(File.exists(spots_ch1_roi_set)){
ms = nSlices;
run("Set Measurements...", " area mean min redirect=None decimal=4");
roiManager("Open",spots_ch1_roi_set);
rmc = roiManager("count");
selectImage(id);
for(c=1;c<=channels;c++)
{
setSlice(c);
roiManager("deselect");
roiManager("Measure");
}
sortResults();
IJ.renameResults("Results","Temp");
run("Clear Results");
IJ.renameResults("Temp","Results");
updateResults;
saveAs("Measurements",spots_ch1_results);
erase(0);
}
if(File.exists(spots_ch2_roi_set)){