-
Notifications
You must be signed in to change notification settings - Fork 0
/
htiaot.ijm
1843 lines (1732 loc) · 53.2 KB
/
htiaot.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
//Automated image analysis tools for trypanosomes
//=============================================================================================================================
//A set of macros for use in ImageJ
// ImageJ is a free and open source cross platform piece of scientific image analysis software
// For more information and to download ImageJ please visit: http://rsbweb.nih.gov/ij/
// For more information about macros in ImageJ please visit: http://rsbweb.nih.gov/ij/developer/macro/macros.html
//Install these macros via "Plugins>Macros>Install" or "Ctrl+Shift+M"
//
//Copyright 2011 Richard J Wheeler (www.richardwheeler.net)
//This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as
//published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
//This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
//of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//You should have received a copy of the GNU General Public License along with this program; if not, see
//http://www.gnu.org/licenses.
var slice_phase=3;
var slice_dapi=1;
var slice_pi=2;
var image_scale=6.236;
var distort_scale=1;
var distort_xori=0;
var distort_yori=0;
var dapi_nucleus=1000;
var dapi_kinetoplast=1000;
var dapi_background=200;
var pi_nucleus=1000;
var pi_kinetoplast=2000;
var pi_background=200;
var analyseall=true;
//Macro - Image Setup Tool
//=============================================================================================================================
//Global variables changed
//Slice numbers
// slice_phase
// slice_dapi
// slice_pi
//Image scale
// image_scale
macro "Image Setup Action Tool -C888L88fcLfc8fL8f1cL1c88C00fL84f8Lf88bL8b18L1884Cf00L80f4Lf488L8814L1480" {
setBatchMode(true);
Dialog.create("Image Setup");
Dialog.addNumber("Phase slice number:", slice_phase, 0, 5, "");
Dialog.addNumber("DAPI slice number:", slice_dapi, 0, 5, "");
Dialog.addNumber("PI slice number:", slice_pi, 0, 5, "");
Dialog.addNumber("Image scale:", image_scale, 3, 5, "px/um");
Dialog.show();
slice_phase=Dialog.getNumber();
slice_dapi=Dialog.getNumber();
slice_pi=Dialog.getNumber();
image_scale=Dialog.getNumber();
setBatchMode(false);
}
//Macro - Measure Chromatic Aberation Tool
//=============================================================================================================================
//Global variables used
//Slice numbers
// slice_dapi
// slice_pi
//Global variables modified
//PI channel distort variables
// distort_scale
// distort_xori
// distort_yori
var mcattolerance=200;
var mcatmaxdist=5;
macro "Measure Chromatic Aberation Action Tool -Cf88o1022Cf00o8177C00fo2322C88fo9577C000L0fffL0c0fL4d4fL8c8fLcdcfLfcff" {
setBatchMode(true);
//Display options
printresults=false;
drawplot=false;
scalefactor=20;
arrowwidth=8;
Dialog.create("Measure Chromatic Aberation");
Dialog.addCheckbox("Analyse all images currently open", analyseall);
Dialog.addMessage("Analysis options:");
Dialog.addNumber("Noise tolerance:", mcattolerance, 0, 5, "");
Dialog.addNumber("Maximum pairing distance:", mcatmaxdist, 0, 5, "px");
Dialog.addMessage("Output options:");
Dialog.addCheckbox("Print data to log", printresults);
Dialog.addCheckbox("Make image plot of distortion", drawplot);
Dialog.addNumber(" Scale factor:", scalefactor, 0, 5, "px/px");
Dialog.addNumber(" Arrow width:", arrowwidth, 0, 5, "px");
Dialog.show();
analyseall=Dialog.getCheckbox();
mcattolerance=Dialog.getNumber();
mcatmaxdist=Dialog.getNumber();
printresults=Dialog.getCheckbox();
drawplot=Dialog.getCheckbox();
scalefactor=Dialog.getNumber();
arrowwidth=Dialog.getNumber();
if (analyseall==true) {
images=newArray(nImages());
for (n=0; n<nImages; n++) {
selectImage(n+1);
images[n]=getImageID();
}
} else {
images=newArray(1);
images[0]=getImageID();
}
finalxd=newArray(0);
finalyd=newArray(0);
finalxp=newArray(0);
finalyp=newArray(0);
finald=newArray(0);
for (m=0; m<lengthOf(images); m++) {
//Batch mode and select image
setBatchMode(true);
selectImage(images[m]);
//Get points
setSlice(slice_dapi);
run("Find Maxima...", "noise="+mcattolerance+" output=[Point Selection]");
getSelectionCoordinates(dapix, dapiy);
setSlice(slice_pi);
run("Find Maxima...", "noise="+mcattolerance+" output=[Point Selection]");
getSelectionCoordinates(pix, piy);
run("Select None");
//Assign points to pairs
dapip=newArray(lengthOf(dapix));
dapid=newArray(lengthOf(dapix));
for (i=0; i<lengthOf(dapix); i++) {
mindist=getWidth()+getHeight();
for (j=0; j<lengthOf(pix); j++) {
xd=dapix[i]-pix[j];
yd=dapiy[i]-piy[j];
dist=pow(xd*xd+yd*yd, 0.5);
if (dist<mindist) {
mindist=dist;
dapip[i]=j+1;
dapid[i]=dist;
}
}
}
//Tidy pairs arrays
p=0;
for (i=0; i<lengthOf(dapix); i++) {
if (dapip[i]!=0 && dapid[i]<=mcatmaxdist) {
p++;
}
}
tfinalxd=newArray(p);
tfinalyd=newArray(p);
tfinalxp=newArray(p);
tfinalyp=newArray(p);
tfinald=newArray(p);
p=0;
for (i=0; i<lengthOf(dapix); i++) {
if (dapip[i]!=0 && dapid[i]<=mcatmaxdist) {
partner=dapip[i]-1;
tfinalxd[p]=dapix[i];
tfinalyd[p]=dapiy[i];
tfinalxp[p]=pix[partner];
tfinalyp[p]=piy[partner];
tfinald[p]=dapid[i];
p++;
}
}
//append t[final] arrays to [final] arrays
finalxd=appendArrayToArray(finalxd, tfinalxd);
finalyd=appendArrayToArray(finalyd, tfinalyd);
finalxp=appendArrayToArray(finalxp, tfinalxp);
finalyp=appendArrayToArray(finalyp, tfinalyp);
finald=appendArrayToArray(finald, tfinald);
}
if (printresults==true) {
//Print results excluding those over the max dist
print("n dapix dapiy pix piy dist");
n=0;
for (i=0; i<lengthOf(finalxd); i++) {
print(n, finalxd[i], finalyd[i], finalxp[i], finalyp[i], finald[i]);
n++;
}
}
if (drawplot==true) {
setBatchMode(false);
//Draws an exaggerated plot of the distortion
newImage("Distortion", "32-bit Black", getWidth(), getHeight(), 1);
setBatchMode(true);
for (i=0; i<lengthOf(finalxd); i++) {
xd=finalxd[i]-finalxp[i];
yd=finalyd[i]-finalyp[i];
x2=finalxd[i]+scalefactor*xd;
y2=finalyd[i]+scalefactor*yd;
if (xd!=0 || yd!=0) {
drawArrow(finalxd[i], finalyd[i], x2, y2, arrowwidth, 1);
} else {
makeRectangle(finalxd[i]-arrowwidth/2, finalyd[i]-arrowwidth/2, arrowwidth, arrowwidth);
run("Add...", "value=1");
}
}
run("Enhance Contrast", "saturated=0.0");
updateDisplay();
}
//Calculate linear regression parameters for result
//Calculate xdif and ydif arrays
xdif=newArray(lengthOf(finalxd));
ydif=newArray(lengthOf(finalxd));
for (i=0; i<lengthOf(finalxd); i++) {
xdif[i]=finalxd[i]-finalxp[i];
ydif[i]=finalyd[i]-finalyp[i];
}
dxsum=0;
dysum=0;
dixsum=0;
diysum=0;
for (i=0; i<lengthOf(finalxd); i++) {
dxsum=dxsum+finalxd[i];
dysum=dysum+finalyd[i];
dixsum=dixsum+xdif[i];
diysum=diysum+ydif[i];
}
dxmean=dxsum/lengthOf(finalxd);
dymean=dysum/lengthOf(finalxd);
dixmean=dixsum/lengthOf(finalxd);
diymean=diysum/lengthOf(finalxd);
xsxx=0;
xsyy=0;
xsxy=0;
ysxx=0;
ysyy=0;
ysxy=0;
for (i=0; i<lengthOf(finalxd); i++) {
xsxx=xsxx+pow(finalxd[i]-dxmean, 2);
xsyy=xsyy+pow(xdif[i]-dixmean, 2);
xsxy=xsxy+(finalxd[i]-dxmean)*(xdif[i]-dixmean);
ysxx=ysxx+pow(finalyd[i]-dymean, 2);
ysyy=ysyy+pow(ydif[i]-diymean, 2);
ysxy=ysxy+(finalyd[i]-dymean)*(ydif[i]-diymean);
}
xb=xsxy/xsxx;
yb=ysxy/ysxx;
xa=dixmean-dxmean*xb;
ya=diymean-dymean*yb;
if (printresults==true) {
print("Transformation required to make PI signal overlay DAPI:");
print("Scale: "+1+(xb+yb)/2);
print("Origin: ("+-xa/xb+", "+-ya/yb+")");
}
Dialog.create("Distortion Analysis Result");
Dialog.addMessage(""+lengthOf(images)+" images analysed.");
//****I HACKED THIS!!!!!!*****
Dialog.addMessage("Result:\nScale: "+1+((xb+yb)/2)*1.2+"\nOrigin: ("+-xa/xb+", "+-ya/yb+")");
Dialog.show();
//****I HACKED THIS!!!!!!*****
distort_scale=1+((xb+yb)/2)*1.2;
distort_xori=-xa/xb;
distort_yori=-ya/yb;
setBatchMode(false);
}
//Distortion test function - appendArrayToArray
//-----------------------------------------------------------------------------------------------------------------------------
//Joins two arrays together and returns the result
function appendArrayToArray(array1, array2) {
result=newArray(lengthOf(array1)+lengthOf(array2));
for (i=0; i<lengthOf(array1); i++) {
result[i]=array1[i];
}
for (i=0; i<lengthOf(array2); i++) {
result[i+lengthOf(array1)]=array2[i];
}
return result;
}
//Distortion test function - drawArrow
//-----------------------------------------------------------------------------------------------------------------------------
//Draws an arrow between two points with a width w and colour
function drawArrow(x1, y1, x2, y2, w, color) {
xd=x1-x2;
yd=y1-y2;
arrowlength=pow(xd*xd+yd*yd, 0.5);
headwidth=1;
headlength=2;
arrowshapex=newArray(-w/2, -w/2, -w*headwidth, 0, w*headwidth, w/2, w/2);
arrowshapey=newArray(0, arrowlength-headlength*w, arrowlength-headlength*w, arrowlength, arrowlength-headlength*w, arrowlength-headlength*w, 0);
angle=-3.1415926/2+atan2(yd, xd);
arrowtransformx=newArray(lengthOf(arrowshapex));
arrowtransformy=newArray(lengthOf(arrowshapey));
for (i=0; i<lengthOf(arrowshapex); i++) {
x=arrowshapex[i];
y=arrowshapey[i];
xt=x*cos(angle)-y*sin(angle);
yt=x*sin(angle)+y*cos(angle);
arrowtransformx[i]=xt+x1;
arrowtransformy[i]=yt+y1;
}
makeSelection("polygon", arrowtransformx, arrowtransformy);
run("Add...", "value="+color);
}
//Macro - Correct Chromatic Aberation Tool
//=============================================================================================================================
//Global variables used
//Slice numbers
// slice_dapi
// slice_pi
//PI channel distort variables
// distort_scale
// distort_xori
// distort_yori
macro "Correct Chromatic Aberation Action Tool -Cf88o1422Cf00o8577C00fo2722C88fo9977C000L08f8Lf8c5Lf8cb" {
setBatchMode(true);
//Display options
Dialog.create("Correct Chromatic Aberation");
Dialog.addCheckbox("Modify all images currently open", analyseall);
Dialog.addNumber("Scale factor:", distort_scale, 5, 9, "");
Dialog.addNumber("Origin (X):", distort_xori, 5, 9, "px");
Dialog.addNumber("Origin (Y):", distort_yori, 5, 9, "px");
Dialog.show();
analyseall=Dialog.getCheckbox();
distort_scale=Dialog.getNumber();
distort_xori=Dialog.getNumber();
distort_yori=Dialog.getNumber();
//Grab images IDs for analysis
if (analyseall==true) {
images=newArray(nImages());
for (n=0; n<nImages; n++) {
selectImage(n+1);
images[n]=getImageID();
}
} else {
images=newArray(1);
images[0]=getImageID();
}
//Loop through images performing modification to pi channel
for (n=0; n<lengthOf(images); n++) {
run("Select None");
selectImage(images[n]);
setSlice(slice_pi);
modimage=originScale(distort_xori, distort_yori, distort_scale);
run("Select All");
run("Copy");
selectImage(modimage);
close();
selectImage(images[n]);
setSlice(slice_pi);
setPasteMode("Copy");
run("Paste");
run("Select None");
}
exit(""+lengthOf(images)+" images modified.");
setBatchMode(false);
}
//Distortion Adjust Function - originScale
//-------------------------------------------------------------------------------------------------------------------------------
//requires a scalefactor of greater than 1
//x and y represent the origin of scaling
//returns the imageID of the resulting image
function originScale(x, y, scalefactor) {
w=getWidth();
h=getHeight();
run("Duplicate...", "title=originScaleTemp");
originScaleTemp=getImageID();
//Determine anchor position and canvas size
if (x<=w/2) {
xanchor="Right";
wnew=round((w-x)*2);
} else {
xanchor="Left";
wnew=round(x*2);
}
if (y<=h/2) {
yanchor="Bottom";
hnew=round((h-y)*2);
} else {
yanchor="Top";
hnew=round(y*2);
}
//Adjust canvas size to centre image
run("Canvas Size...", "width="+wnew+" height="+hnew+" position="+yanchor+"-"+xanchor);
//Scale the image and crop to original size
run("Size...", "width="+wnew*scalefactor+" height="+hnew*scalefactor+" constrain interpolation=Bicubic");
xcentre=round((wnew*scalefactor)/2);
ycentre=round((hnew*scalefactor)/2);
makeRectangle(xcentre-x, ycentre-y, w, h);
run("Crop");
//return imageID
return getImageID();
}
//Macro - Measure K/N Signal Tool
//=============================================================================================================================
//Global variables used
//Slice numbers
// slice_dapi
// slice_pi
//Global variables modified
//PI channel distort variables
// dapi_nucleus
// dapi_kinetoplast
// dapi_background
// pi_nucleus
// pi_kinetoplast
// pi_background
var mkntolerance=200;
var excludeoutlierstdevs=3;
macro "Measure K/N Signal Action Tool -C80fo1322Cf08o8477C000L0fffL0c0fL4d4fL8c8fLcdcfLfcff" {
setBatchMode(true);
//Display options
printresults=false;
Dialog.create("Measure K/N Signal");
Dialog.addCheckbox("Analyse all images currently open", analyseall);
Dialog.addMessage("Analysis options:");
Dialog.addNumber("Noise tolerance:", mkntolerance, 0, 5, "");
Dialog.addNumber("Exclude outliers:", excludeoutlierstdevs, 0, 5, "standard deviations");
Dialog.addCheckbox("Print data to log", printresults);
Dialog.show();
analyseall=Dialog.getCheckbox();
mkntolerance=Dialog.getNumber();
excludeoutlierstdevs=Dialog.getNumber();
printresults=Dialog.getCheckbox();
//Get image IDs
setBatchMode(true);
if (analyseall==true) {
images=newArray(nImages());
for (n=0; n<nImages; n++) {
selectImage(n+1);
images[n]=getImageID();
}
} else {
images=newArray(1);
images[0]=getImageID();
}
sumdmode=0;
sumpmode=0;
for (i=0; i<lengthOf(images); i++) {
//Get slice modes
run("Select None");
setSlice(slice_dapi);
getRawStatistics(area, mean, min, max, stdev, histogram);
hmax=0;
modepos=0;
for (a=0; a<lengthOf(histogram); a++) {
if(histogram[a]>hmax) {
mode=a;
hmax=histogram[a];
}
}
sumdmode=sumdmode+mode+1;
setSlice(slice_pi);
getRawStatistics(area, mean, min, max, stdev, histogram);
hmax=0;
modepos=0;
for (a=0; a<lengthOf(histogram); a++) {
if(histogram[a]>hmax) {
mode=a;
hmax=histogram[a];
}
}
sumpmode=sumpmode+mode+1;
}
//Calculate average backgrounds
tdapi_background=sumdmode/lengthOf(images);
tpi_background=sumpmode/lengthOf(images);
ratioso=newArray(0);
dvalueso=newArray(0);
pvalueso=newArray(0);
for (i=0; i<lengthOf(images); i++) {
selectImage(images[i]);
//Get values for maxima and record log2 ratios to an array
setSlice(slice_dapi);
run("Find Maxima...", "noise="+mkntolerance+" output=[Point Selection]");
getSelectionCoordinates(x1, y1);
setSlice(slice_pi);
run("Find Maxima...", "noise="+mkntolerance+" output=[Point Selection]");
getSelectionCoordinates(x2, y2);
v1=newArray(lengthOf(x1)+lengthOf(x2));
v2=newArray(lengthOf(x1)+lengthOf(x2));
for (n=0; n<lengthOf(x1); n++) {
setZCoordinate(slice_dapi-1);
v1[n]=getPixel(x1[n], y1[n])-tdapi_background;
setZCoordinate(slice_pi-1);
v2[n]=getPixel(x1[n], y1[n])-tpi_background;
}
for (n=0; n<lengthOf(x2); n++) {
setZCoordinate(slice_dapi-1);
v1[n+lengthOf(x1)]=getPixel(x2[n], y2[n])-tdapi_background;
setZCoordinate(slice_pi-1);
v2[n+lengthOf(x1)]=getPixel(x2[n], y2[n])-tpi_background;
}
r=newArray(lengthOf(v1));
for(n=0; n<lengthOf(v1); n++) {
if (v1[n]<=0) {
v1[n]=1;
}
if (v2[n]<=0) {
v2[n]=1;
}
r[n]=log(v1[n]/v2[n])/log(2);
}
//Append data to final array
ratioso=appendArrayToArray2(ratioso, r);
dvalueso=appendArrayToArray2(dvalueso, v1);
pvalueso=appendArrayToArray2(pvalueso, v2);
}
//Iterate through ratios array and exclude outliers (due to shot noise, dead pixels, fluorescent debris, etc)
nstdev=excludeoutlierstdevs;
ratiosmean=arrayAverage(ratioso);
ratiosstdev=arrayStdDev(ratioso);
includearr=newArray(lengthOf(ratioso));
includecount=0;
for (n=0; n<lengthOf(ratioso); n++) {
if (dvalueso[n]!=0 && pvalueso[n]!=0) {
if (ratioso[n]>ratiosmean-nstdev*ratiosstdev && ratioso[n]<ratiosmean+nstdev*ratiosstdev) {
includearr[n]=1;
includecount++;
}
}
}
ratios=newArray(includecount);
dvalues=newArray(includecount);
pvalues=newArray(includecount);
index=0;
for (n=0; n<lengthOf(ratioso); n++) {
if (includearr[n]==1) {
ratios[index]=ratioso[n];
dvalues[index]=dvalueso[n];
pvalues[index]=pvalueso[n];
index++;
}
}
//1D K means clustering of ratio data
//Calculate mean and stdev of data
sum=0;
for (n=0; n<lengthOf(ratios); n++) {
sum=sum+ratios[n];
}
mean=sum/lengthOf(ratios);
sumsq=0;
for (n=0; n<lengthOf(ratios); n++) {
dif=ratios[n]-mean;
difsq=dif*dif;
sumsq=sumsq+difsq;
}
stdevsq=sumsq/lengthOf(ratios);
stdev=pow(stdevsq, 0.5);
//Starting cluster values
cluster1=mean-stdev;
cluster2=mean+stdev;
//K means clustering
nchanges=lengthOf(ratios);
group=newArray(lengthOf(ratios));
while (nchanges>0) {
nchanges=0;
for (n=0; n<lengthOf(ratios); n++) {
dist1=abs(cluster1-ratios[n]);
dist2=abs(cluster2-ratios[n]);
oldgroup=group[n];
if (dist1<dist2) {
group[n]=1;
} else {
group[n]=2;
}
if (group[n]!=oldgroup) {
nchanges++;
}
}
sum1=0;
sum2=0;
n1=0;
n2=0;
for (n=0; n<lengthOf(ratios); n++) {
if (group[n]==1) {
sum1=sum1+ratios[n];
n1++;
} else {
sum2=sum2+ratios[n];
n2++;
}
}
if (n1!=0) {
cluster1=sum1/n1;
}
if (n2!=0) {
cluster2=sum2/n2;
}
}
//Calculate mean dapi and pi intensities for all points within each cluster
d1sum=0;
p1sum=0;
d2sum=0;
p2sum=0;
for (n=0; n<lengthOf(ratios); n++) {
if (group[n]==1) {
d1sum=d1sum+dvalues[n];
p1sum=p1sum+pvalues[n];
} else if (group[n]==2) {
d2sum=d2sum+dvalues[n];
p2sum=p2sum+pvalues[n];
}
}
d1mean=d1sum/n1;
p1mean=p1sum/n1;
d2mean=d2sum/n1;
p2mean=p2sum/n1;
if (printresults==true) {
print("DAPI value,PI value,Log Ratio,Cluster");
for (n=0; n<lengthOf(ratios); n++) {
print(dvalues[n]+","+pvalues[n]+","+ratios[n]+","+group[n]);
}
}
//Assign the two groups to kin and nuc according to ratio value
if (cluster1<cluster2) {
tdapi_nucleus=d1mean;
tpi_nucleus=p1mean;
tdapi_kinetoplast=d2mean;
tpi_kinetoplast=p2mean;
} else {
tdapi_nucleus=d2mean;
tpi_nucleus=p2mean;
tdapi_kinetoplast=d1mean;
tpi_kinetoplast=p1mean;
}
ratioratio=(tdapi_nucleus/tpi_nucleus)/(tdapi_kinetoplast/tpi_kinetoplast);
if (printresults==true) {
print("DAPI Background", "PI Background");
print(tdapi_background, tpi_background);
print("DAPI Nucleus", "PI Nucleus");
print(tdapi_nucleus, tpi_nucleus);
print("DAPI Kinetoplast", "PI Kinetoplast");
print(tdapi_kinetoplast, tpi_kinetoplast);
print("Points in cluster 1", "Points in cluster 2");
print(n1, n2);
print("Ratio ratio");
print(ratioratio);
}
Dialog.create("Automatic Intensity Sampling Tool");
Dialog.addMessage("Nucleus DAPI: "+tdapi_nucleus);
Dialog.addMessage("Nucleus PI: "+tpi_nucleus);
Dialog.addMessage("Kinetoplast DAPI: "+tdapi_kinetoplast);
Dialog.addMessage("Kinetoplast PI: "+tpi_kinetoplast);
Dialog.addMessage("Background DAPI: "+tdapi_background);
Dialog.addMessage("Background PI: "+tpi_background);
Dialog.addMessage("Points in each cluster: ("+n1+", "+n2+")");
Dialog.addMessage("Ratio ratio: "+ratioratio);
Dialog.show();
dapi_nucleus=tdapi_nucleus;
dapi_kinetoplast=tdapi_kinetoplast;
dapi_background=tdapi_background;
pi_nucleus=tpi_nucleus;
pi_kinetoplast=tpi_kinetoplast;
pi_background=tpi_background;
}
//Intensity test function - appendArrayToArray2
//-----------------------------------------------------------------------------------------------------------------------------
//Joins two arrays together and returns the result
function appendArrayToArray2(array1, array2) {
result=newArray(lengthOf(array1)+lengthOf(array2));
for (i=0; i<lengthOf(array1); i++) {
result[i]=array1[i];
}
for (i=0; i<lengthOf(array2); i++) {
result[i+lengthOf(array1)]=array2[i];
}
return result;
}
//Intensity test function - arrayAverage
//-----------------------------------------------------------------------------------------------------------------------------
//Returns the mean of an array
function arrayAverage(array) {
sum=0;
for (a=0; a<lengthOf(array); a++) {
sum+=array[a];
}
return sum/lengthOf(array);
}
//Intensity test function - arrayStdDev
//-----------------------------------------------------------------------------------------------------------------------------
//Returns the standard deviation of an array
function arrayStdDev(array) {
sumsqd=0;
mean=arrayAverage(array);
for (a=0; a<lengthOf(array); a++) {
sumsqd+=pow(array[a]-mean, 2);
}
return pow(sumsqd/lengthOf(array), 0.5);
}
//Macro - Colour Deconvolution Tool
//=============================================================================================================================
//Global variables used
//Slice numbers
// slice_dapi
// slice_pi
//PI channel distort variables
// dapi_nucleus
// dapi_kinetoplast
// dapi_background
// pi_nucleus
// pi_kinetoplast
// pi_background
var cdsubtractbackground=false;
var cdrollingballradius=15;
macro "Colour Deconvolution Action Tool -C0ffo1622Cf80o8877C000L08f8Lf8c5Lf8cb" {
setBatchMode(true);
//Display options
Dialog.create("Colour Deconvolution");
Dialog.addCheckbox("Modify all images currently open", analyseall);
Dialog.addNumber("Nucleus DAPI:", dapi_nucleus, 3, 9, "");
Dialog.addNumber("Nucleus PI:", pi_nucleus, 3, 9, "");
Dialog.addNumber("Kinetoplast DAPI:", dapi_kinetoplast, 3, 9, "");
Dialog.addNumber("Kinetoplast PI:", pi_kinetoplast, 3, 9, "");
Dialog.addNumber("Background DAPI:", dapi_background, 3, 9, "");
Dialog.addNumber("Background PI:", pi_background, 3, 9, "");
Dialog.addCheckbox("Subtract background following processing", cdsubtractbackground);
Dialog.addNumber("Rolling ball radius:", cdrollingballradius, 0, 9, "px");
Dialog.show();
analyseall=Dialog.getCheckbox();
dapi_nucleus=Dialog.getNumber();
pi_nucleus=Dialog.getNumber();
dapi_kinetoplast=Dialog.getNumber();
pi_kinetoplast=Dialog.getNumber();
dapi_background=Dialog.getNumber();
pi_background=Dialog.getNumber();
cdsubtractbackground=Dialog.getCheckbox();
cdrollingballradius=Dialog.getNumber();
//Grab images IDs for analysis
if (analyseall==true) {
images=newArray(nImages());
for (n=0; n<nImages; n++) {
selectImage(n+1);
images[n]=getImageID();
run("Select None");
}
} else {
images=newArray(1);
images[0]=getImageID();
run("Select None");
}
//Normalise and subtract bg from kinetoplast and nucleus vectors
kinsum=dapi_kinetoplast+pi_kinetoplast;
nucsum=dapi_nucleus+pi_nucleus;
ndapi_kinetoplast=(dapi_kinetoplast)/kinsum;
npi_kinetoplast=(pi_kinetoplast)/kinsum;
ndapi_nucleus=(dapi_nucleus)/nucsum;
npi_nucleus=(pi_nucleus)/nucsum;
mdetinv=1/(ndapi_kinetoplast*npi_nucleus-ndapi_nucleus*npi_kinetoplast);
//Loop through all images and perform deconvolution
for (i=0; i<lengthOf(images); i++) {
selectImage(images[i]);
run("Select None");
setSlice(slice_dapi);
run("Duplicate...", "dapi1");
rename("dapi1");
dapi1=getImageID();
run("Duplicate...", "dapi2");
rename("dapi2");
dapi2=getImageID();
selectImage(images[i]);
setSlice(slice_pi);
run("Duplicate...", "pi1");
rename("pi1");
pi1=getImageID();
run("Duplicate...", "pi2");
rename("pi2");
pi2=getImageID();
selectImage(dapi1);
run("32-bit");
run("Multiply...", "value="+npi_nucleus);
selectImage(dapi2);
run("32-bit");
run("Multiply...", "value="+-npi_kinetoplast);
selectImage(pi1);
run("32-bit");
run("Multiply...", "value="+-ndapi_nucleus);
selectImage(pi2);
run("32-bit");
run("Multiply...", "value="+ndapi_kinetoplast);
imageCalculator("Add create 16-bit", "dapi1","pi1");
rename("pi");
pi=getImageID();
run("Multiply...", "value="+mdetinv);
selectImage(dapi1);
close();
selectImage(pi1);
close();
imageCalculator("Add create 16-bit", "dapi2","pi2");
rename("dapi");
dapi=getImageID();
run("Multiply...", "value="+mdetinv);
selectImage(dapi2);
close();
selectImage(pi2);
close();
run("Options...", "iterations=1 black edm=Overwrite count=1");
selectImage(pi);
run("Select None");
if (cdsubtractbackground==true) {
run("Subtract Background...", "rolling="+cdrollingballradius);
}
getRawStatistics(area, mean, min, max, stdev, histogram);
maxh=0;
for (m=0; m<lengthOf(histogram); m++) {
if (histogram[m]>maxh) {
medv=m;
maxh=histogram[m];
}
}
median=(max-min)*(medv/255)+min;
run("Subtract...", "value="+median);
selectImage(dapi);
run("Select None");
if (cdsubtractbackground==true) {
run("Subtract Background...", "rolling="+cdrollingballradius);
}
getRawStatistics(area, mean, min, max, stdev, histogram);
maxh=0;
for (m=0; m<lengthOf(histogram); m++) {
if (histogram[m]>maxh) {
medv=m;
maxh=histogram[m];
}
}
median=(max-min)*(medv/255)+min;
run("Subtract...", "value="+median);
//Copy images into pi and dapi channels of stack
selectImage(dapi);
run("Select All");
run("Copy");
close();
selectImage(images[i]);
setSlice(slice_dapi);
setPasteMode("Copy");
run("Paste");
selectImage(pi);
run("Select All");
run("Copy");
close();
selectImage(images[i]);
setSlice(slice_pi);
run("Paste");
}
exit(""+lengthOf(images)+" images modified.");
setBatchMode(false);
}
//Macro - Cell Analysis Tool
//=============================================================================================================================
//Global variables used
//Slice numbers
// slice_phase
// slice_dapi
// slice_pi
//Image scale
// image_scale
var displaymasks=true;
var rollingballradius=10;
var cellautomanual="Automatic";
var cellthresholdvalue=6000;
var cellautothreshtype="Triangle";
var cellthresholdvalue=6000;
var cellminarea=200;
var trimbranches=true;
var minbranch=10;
var threshtype="Manual";
var autothreshtype="MaxEntropy";
var nthresholdvalue=600;
var kthresholdvalue=500;
var nucminarea=50;
var kinminarea=5;
macro "Cell Analysis Action Tool -C0ffo1322Cf80o8477C000T0f081T4f082T8f083" {
setBatchMode(true);
getDateAndTime(year, month, dayOfWeek, dayOfMonth, hour, minute, second, msec);
starttime=second+100*minute+100*100*hour+100*100*100*dayOfMonth+100*100*100*100*month+100*100*100*100*100*year;
starttime=""+year+"_"+month+"_"+dayOfMonth+"_"+hour+"_"+minute+"_"+second;
//User interface
threshchoice=newArray("Default", "Huang", "Intermodes", "IsoData", "Li", "MaxEntropy", "Minimum", "Moments", "Otsu", "Percentile", "RenyiEntropy", "Shanbhag", "Triangle", "Yen");
automanual=newArray("Manual", "Automatic");
Dialog.create("K/N Count")
Dialog.addCheckbox("Process all images currently open", analyseall);
//Dialog.addCheckbox("Give a visual display of results", displaymasks);
Dialog.addMessage("Cell thresholding options:");
Dialog.addNumber("Rolling ball radius:", rollingballradius, 0, 5, "px");
Dialog.addChoice("Thresholding type:", automanual, automanual[getArrayIndex(automanual, cellautomanual)]);
Dialog.addNumber("Manual cell threshold value:", cellthresholdvalue, 0, 5, "");
Dialog.addChoice("Automatic threshold type:", threshchoice, threshchoice[getArrayIndex(threshchoice, cellautothreshtype)]);
Dialog.addNumber("Minimum cell area:", cellminarea, 2, 5, "px^2");
Dialog.addCheckbox("Trim short skeleton branches", trimbranches);
Dialog.addNumber("Minimum skeleton branch length:", minbranch, 2, 5, "px");
Dialog.addMessage("Nucleus and Kintoplast thresholding options:");
Dialog.addChoice("Thresholding type:", automanual, automanual[getArrayIndex(automanual, threshtype)]);
Dialog.addNumber("Manual nucleus threshold value:", nthresholdvalue, 0, 5, "");
Dialog.addNumber("Manual kinetoplast threshold value:", kthresholdvalue, 0, 5, "");
Dialog.addChoice("Automatic threshold type:", threshchoice, threshchoice[getArrayIndex(threshchoice, autothreshtype)]);
Dialog.addNumber("Minimum nucleus area:", nucminarea, 2, 5, "px^2");
Dialog.addNumber("Minimum kinetoplast area:", kinminarea, 2, 5, "px^2");
Dialog.show();
analyseall=Dialog.getCheckbox();
//displaymasks=Dialog.getCheckbox();
rollingballradius=Dialog.getNumber();
cellautomanual=Dialog.getChoice();
cellthresholdvalue=Dialog.getNumber();
cellautothreshtype=Dialog.getChoice();
cellminarea=Dialog.getNumber();
trimbranches=Dialog.getCheckbox();
minbranch=Dialog.getNumber();
threshtype=Dialog.getChoice();
nthresholdvalue=Dialog.getNumber();
kthresholdvalue=Dialog.getNumber();
autothreshtype=Dialog.getChoice();
nucminarea=Dialog.getNumber();
kinminarea=Dialog.getNumber();
//Grab images IDs for analysis
if (analyseall==true) {
images=newArray(nImages());
for (n=0; n<nImages; n++) {
selectImage(n+1);
images[n]=getImageID();
}
} else {
images=newArray(1);
images[0]=getImageID();
}
run("Options...", "iterations=1 black edm=Overwrite count=1");
cellno=0;
for (i=0; i<lengthOf(images); i++) {
selectImage(images[i]);
rename("Analysed_"+starttime+"_"+i);
run("Select None");
w=getWidth();
h=getHeight();
//Detect and size filter cell masks
selectImage(images[i]);
setSlice(slice_phase);
run("Duplicate...", "title=phase");