-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColocalization_Finder.java
More file actions
2079 lines (1890 loc) · 78.4 KB
/
Copy pathColocalization_Finder.java
File metadata and controls
2079 lines (1890 loc) · 78.4 KB
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
/*
* Colocalization_Finder.java
*
* Created on 20/01/2005 Copyright (C) 2005 IBMP
* ImageJ plugin
*
* Version : 1.1
* Authors : C. Laummonerie & J. Mutterer
* written for the IBMP-CNRS Strasbourg(France)
* Email : jerome.mutterer at ibmp-ulp.u-strasbg.fr
* Description : This plugin displays a correlation diagram for two
* images (8bits, same size). Drawing a rectangular selection
* on this diagram allows you to highlight corresponding pixels on a
* RGB overlap of the original images, and if selected, on a 3rd image.
* Analysis can be restricted to pixels having values with a minimum ratio.
* Selection settings are logged to a results window. Large parts of this
* code were taken from Wayne Rasband, Pierre Bourdoncle.
* and Gary Chinga.
*
* Version 1.2 : JM
* - Rewrote the mask overlay part, now faster
* - Made the scatterplot selection possible with any kind of closed selections after several requests.
* - The ratio bars are now overlaid on a separte layer, so that you stillcan read the pixel info behind these bars
* - Fixed the Fire LUT issue (LUT was not always applied)
*
* Version 1.3 : Philippe Carl
* Email : philippe.carl at unistra.fr
* Date : 4/30/2016
* - Replacement of the deprecated functions (getBoundingRect, IJ.write) by the new ones
* - Extension of the plugin for whatever picture dynamics
* - Addition of a plot (with legends, ticks (minor and major), labels) within the scatter plot
* - The selected points within the overlay picture are updated as soon as the ROI in the scatter plot is modified or dragged over
* - Possibility to move the ROI position (within the scatter plot) from the mouse position within the overlay picture
* - Possibility to set ROIs with given colors with a mouse double click
* - Possibility to generate the x or y histogram with a Gaussian fit in order to extract the histogram maximum position by using the numeric pad 4/6 or 2/8 keys
*
* Version 1.4 : Philippe Carl
* Date : 10/20/2019
* - Addition of scripting possibilities through plugin or macro programming
* - The colocalization calculations are performed using double parameters instead of float
* - Possibility to reduce the analysis to a ROI within the composite picture
*
* Version 1.5: Philippe Carl
* Date : 12/15/2019
* - Possibility to add a selection within the Composite picture to restric the analysis a the given selection
* - Addition of synchronized background thread for smoothly updating the calculations on the fly
*
* Version 1.6: Philippe Carl
* Date : 06/12/2022
* - Possibility to choose the size of the scatter plot upon start of the plugin
* - Addition of a label panel at the bottom of the scatterPlot picture displaying the limits of the scatterPlot Roi selection (or other parameters upon selection)
* - Addition of a "Set" button at the bottom left of the scatterPlot picture allowing so set the limits of the scatterPlot graph and/or of the scatterPlot Roi and/or choosing the displayed parameters within the label panel at the bottom of the scatterPlot (the 'g' key gives the same features)
* - Addition of the Manders coefficients (M1, M2 and M1_norm, M2_norm) calculation
* - The possibility to set ROIs with given colors with a mouse double click has been erased (due to the ImageJ 1.53c 26 June 2020 update) and replaced by a Ctrl + mouse click user action
*
* Version 1.7: Philippe Carl
* Date : 18/03/2023
* - Addition of a ScatterPlot_ROI_name column within the Colocalization Finder Results window
*
* Version 1.8: Philippe Carl
* Date : 01/05/2023
* - The Colocalization_Finder plugin allows the analysis of image stacks
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
import ij.IJ;
import ij.Prefs;
import ij.ImageListener;
import ij.ImagePlus;
import ij.WindowManager;
import ij.gui.GenericDialog;
import ij.gui.ImageCanvas;
import ij.gui.ImageWindow;
import ij.gui.Overlay;
import ij.gui.Line;
import ij.gui.Roi;
import ij.gui.RoiListener;
import ij.gui.ShapeRoi;
import ij.gui.TextRoi;
import ij.gui.Toolbar;
import ij.measure.CurveFitter;
import ij.plugin.Colors;
import ij.plugin.PlugIn;
import ij.plugin.RGBStackMerge;
import ij.plugin.filter.ThresholdToSelection;
import ij.plugin.frame.Fitter;
import ij.plugin.frame.RoiManager;
import ij.process.ByteProcessor;
import ij.process.ImageConverter;
import ij.process.ImageProcessor;
import ij.process.ImageStatistics;
import ij.process.ShortProcessor;
import ij.text.TextWindow;
import ij.util.ArrayUtil;
import ij.util.Tools;
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Image;
import java.awt.Label;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.Panel;
import java.awt.Point;
import java.net.URL;
import java.util.Arrays;
import javax.swing.Timer;
public class Colocalization_Finder implements PlugIn, ActionListener, ItemListener, ImageListener, RoiListener, KeyListener, MouseListener, MouseMotionListener, Runnable
{
private static int multiClickInterval;
private Thread bgThread; // thread for launching the calculation (in the background)
Toolkit toolkit;
Integer interval;
Timer timer;
ImageCanvas canvas, canvasResu, ccr, cc3, icc;
ImageConverter image1Converter , image2Converter;
ImagePlus imp , insertImp, insertImp2, insertImp3;
ImageStatistics image1Statistics, image2Statistics;
boolean previousblackBackgroundState;
boolean mouseInsideResultImage = false;
boolean mouseInsideScatterPlot = false;
boolean scatterPlotModified = false;
boolean resultImageModified = false;
String [] selectedItemsLabels = {"Pearson's_Rr" , "Overlap_R" , "k1" , "k2" , "M1" , "M2" , "M1_norm" , "M2_norm" , "Slope" , "Intercept" , "nb_pixels" , "%pixels" , "min_I1" , "max_I1" , "min_I2" , "max_I2" };
// boolean [] selectedItemsValues = {show_Pearson_s_Rr, show_Overlap_R, show_k1 , show_k2 , show_M1 , show_M2 , show_M1_norm , show_M2_norm , show_Slope, show_Intercept, show_nb_pixels, show_percentage_pixels, show_min_I1 , show_max_I1 , show_min_I2 , show_max_I2 };
int [] wList;
String str;
int scatterPlotSizeIndex;
String [] scatterPlotSizeText = {"_256 x 256_", "_512 x 512_", "1024 x 1024"};
// String [] scatterPlotSizeText = {"_256 \u00D7 256_", "_512 \u00D7 512_", "1024 \u00D7 1024"};
Button set;
Checkbox [] checkboxes;
Component [] dlgItems;
Image insert = null;
static Image icon = null;
static Colocalization_Finder instance;
static GenericDialog gd;
static ImagePlus image1 , image2 , scatterPlot , resultImage;
static ImageProcessor image1Processor , image2Processor, scatterPlotProcessor;
static ImageProcessor mask, colocMask;
static ImageWindow scatterPlotWindow;
static ThresholdToSelection ts;
static Overlay scatterplotOverlay , resultImageOverlay;
static CurveFitter cf;
static TextWindow ResultsWindow;
static String resultImageRoiName;
static Roi colocMaskRoi, scatterPlotRoi , resultImageRoi;
static ShapeRoi sr1, sr2;
static RoiManager rm;
static Rectangle coord, rect;
static Label statusLabel;
static String title = "Colocalization Finder";
static String ResultsTitle = "Colocalization Finder Results";
static String ResultsHeadings, spaceString, sstr;
static boolean pearson = true;
static boolean comparisonRunning = false;
// static boolean doubleClick;
static int resultImageSliceNumbers, resultImageSlicePosition;
static final int show_Pearson = 0x1;
static final int show_Overlap = 0x2;
static final int show_k1 = 0x4;
static final int show_k2 = 0x8;
static final int show_M1 = 0x10;
static final int show_M2 = 0x20;
static final int show_M1_norm = 0x40;
static final int show_M2_norm = 0x80;
static final int show_Slope = 0x100;
static final int show_Intercept = 0x200;
static final int show_nb_pixels = 0x400;
static final int show_percentage_pixels = 0x800;
static final int show_min_I1 = 0x1000;
static final int show_max_I1 = 0x2000;
static final int show_min_I2 = 0x4000;
static final int show_max_I2 = 0x8000;
static final int default_checked = show_Pearson + show_min_I1 + show_max_I1 + show_min_I2 + show_max_I2;
static int show_checked = default_checked;
static int nbChecked = 5;
static int precision = 8;
// static int i, scatterPlotSize, npixels, clickCount, counter, i1Index, i2Index, i3Index, x, y, z1, z2, count;
static int i, scatterPlotSize, npixels, counter, i1Index, i2Index, i3Index, x, y, z1, z2, count;
static int windowOffset, xOffset, yOffset, w1, w2, h1, h2, roiWidth, roiHeight;
static int vi1, vi2, pos, color;
static double val, percentPixels, min1, min2, max1, max2, minI1, maxI1, maxI2, minI2, depth1, depth2;
static double scatterPlotMin1, scatterPlotMax1, scatterPlotMin2, scatterPlotMax2;
static double PearsonValue, xMean, yMean, xStd, yStd; // Variables from getR(double[] d1, double[] d2) that are made global to be able to be outputed
static String PearsonValueAsString;
static byte [] maskPixels;
static double [] intx, inty, lesx, lesy, cfParams;
static String [] titles;
static Point [] pointsInsideRoi;
static ColorDefinition [] colors;
public Colocalization_Finder()
{
toolkit = Toolkit.getDefaultToolkit();
interval = (Integer) toolkit.getDesktopProperty("awt.multiClickInterval");
if (interval == null)
multiClickInterval = 200;
else
multiClickInterval = interval.intValue();
}
public void run(String arg)
{
if (IJ.versionLessThan("1.52r"))
return;
IJ.register (Colocalization_Finder.class);
previousblackBackgroundState = Prefs.blackBackground;
Prefs.blackBackground = false;
instance = this;
try
{
URL url = getClass().getResource("/image/coloc_icon.png");
icon = Toolkit.getDefaultToolkit().getImage(url);
}
catch (Exception e)
{
IJ.showMessage("Loading the icon picture", "The icon picture \"/image/coloc_icon.png\" could not be found!");
}
try
{
URL url1 = getClass().getResource("/image/coloc_insert.png");
insert = Toolkit.getDefaultToolkit().getImage(url1);
}
catch (Exception e)
{
IJ.showMessage("Loading the insert picture", "The insert picture \"/image/coloc_insert.png\" could not be found!");
}
insertImp = new ImagePlus ("Error message", insert);
try
{
URL url2 = getClass().getResource("/image/area_chart.png");
insert = Toolkit.getDefaultToolkit().getImage(url2);
}
catch (Exception e)
{
IJ.showMessage("Loading the insert picture", "The insert picture \"/image/area_chart.png\" could not be found!");
}
insertImp2 = new ImagePlus ("Error message", insert);
try
{
URL url3 = getClass().getResource("/image/select_points.png");
insert = Toolkit.getDefaultToolkit().getImage(url3);
}
catch (Exception e)
{
IJ.showMessage("Loading the insert picture", "The insert picture \"/image/select_points.png\" could not be found!");
}
insertImp3 = new ImagePlus ("Error message", insert);
if (arg.equals("about"))
{
showAbout();
return;
}
wList = WindowManager.getIDList();
if (wList == null || wList.length < 2)
{
IJ.showMessage(title, "There must be at least two windows open");
return;
}
titles = new String[wList.length];
for (i = 0; i < wList.length; i++)
{
imp = WindowManager.getImage(wList[i]);
if (imp != null)
titles[i] = imp.getTitle();
else
titles[i] = "";
}
scatterPlotSizeIndex = WindowManager.getImage(wList[0]).getBitDepth() / 16;
if (!showDialog())
return;
ResultsHeadings = resultImageSliceNumbers > 1 ?
"picture1_name\tpicture2_name\tSlice_number\tROI_name\tPearson's_Rr\tAverage_a\tAverage_b\tSigma_a\tSigma_b\tOverlap_R\tk1\tk2\tM1\tM2\tM1_norm\tM2_norm\tSlope\tIntercept\tnb_pixels\t%pixels\tmin_I1\tmax_I1\tmin_I2\tmax_I2\t<picture1>\t<picture2>\tROI_color" :
"picture1_name\tpicture2_name\tROI_name\tPearson's_Rr\tAverage_a\tAverage_b\tSigma_a\tSigma_b\tOverlap_R\tk1\tk2\tM1\tM2\tM1_norm\tM2_norm\tSlope\tIntercept\tnb_pixels\t%pixels\tmin_I1\tmax_I1\tmin_I2\tmax_I2\t<picture1>\t<picture2>\tROI_color";
defineColors();
build_scatter_plot();
IJ.run(scatterPlot, "Fire", "");
IJ.run(scatterPlot, "Enhance Contrast", "saturated=0.5");
// resultImage.show();
comparison(false, false);
}
public void run()
{
while (true)
{
// IJ.wait(50); // delay to make sure the roi has been updated
if (scatterPlotModified && !comparisonRunning)
{
comparison(false, false);
scatterPlot.draw();
}
else if (resultImageModified && !comparisonRunning)
{
rebuild_scatter_plot();
comparison(false, false);
}
synchronized (this)
{
if (scatterPlotModified)
{
scatterPlotModified = false; // and loop again
}
else if (resultImageModified)
{
resultImageModified = false; // and loop again
}
else
{
try
{
wait(); // notify wakes up the thread
}
catch(InterruptedException e)
{
return; // interrupted tells the thread to exit
}
}
}
}
}
public boolean showDialog()
{
gd = new GenericDialog(title);
gd .addChoice ("Image_1 (will be shown in red): " , titles , titles[0] );
gd .addChoice ("Image_2 (will be shown in green):" , titles , titles[1] );
gd .addChoice ("ScatterPlot_Size: ", scatterPlotSizeText, scatterPlotSizeText[scatterPlotSizeIndex]);
gd .setIconImage (icon);
Choice theChoice = (Choice) (gd.getChoices().lastElement());
gd .pack ();
theChoice .requestFocusInWindow ();
gd.showDialog();
if (gd.wasCanceled())
return false;
i1Index = gd.getNextChoiceIndex ();
i2Index = gd.getNextChoiceIndex ();
scatterPlotSizeIndex = gd.getNextChoiceIndex ();
image1 = WindowManager.getImage(wList[i1Index]).duplicate();
image2 = WindowManager.getImage(wList[i2Index]).duplicate();
WindowManager .getImage(wList[i1Index]).getWindow().setIconImage(icon);
WindowManager .getImage(wList[i2Index]).getWindow().setIconImage(icon);
switch(scatterPlotSizeIndex)
{
case 0:
scatterPlotSize = 256;
break;
case 1:
scatterPlotSize = 512;
break;
case 2:
scatterPlotSize = 1024;
break;
default:
scatterPlotSize = 512;
break;
}
w1 = image1.getWidth();
w2 = image2.getWidth();
h1 = image1.getHeight();
h2 = image2.getHeight();
if (w1 != w2 || h1 != h2)
{
IJ.showMessage(title, "Images 1 and 2 must be at the same height and width");
return false;
}
image1Statistics = image1.getStatistics();
image2Statistics = image2.getStatistics();
scatterPlotMin1 = min1 = image1Statistics.min;
scatterPlotMax1 = max1 = image1Statistics.max;
scatterPlotMin2 = min2 = image2Statistics.min;
scatterPlotMax2 = max2 = image2Statistics.max;
depth1 = Math.pow(2, image1.getBitDepth());
depth2 = Math.pow(2, image2.getBitDepth());
// create the overlay and mask image.
ImagePlus[] images = { image1, image2 };
resultImage = RGBStackMerge.mergeChannels (images, true);
resultImage .show ();
resultImage .getWindow().setIconImage (icon);
resultImage .getCanvas().addMouseListener (this);
resultImage .getCanvas().addMouseMotionListener (this);
resultImageSliceNumbers = resultImage.getNSlices ();
resultImageSlicePosition= resultImage.getSlice ();
maskPixels = new byte[w1 * h1];
Arrays .fill (maskPixels, (byte) 0);
if(titles[i1Index].lastIndexOf(".") > 0)
resultImage .getImageStack().setSliceLabel (titles[i1Index].substring(0, titles[i1Index].lastIndexOf(".")) , 1);
else
resultImage .getImageStack().setSliceLabel (titles[i1Index] , 1);
if(titles[i2Index].lastIndexOf(".") > 0)
resultImage .getImageStack().setSliceLabel (titles[i2Index].substring(0, titles[i2Index].lastIndexOf(".")) , 2);
else
resultImage .getImageStack().setSliceLabel (titles[i2Index] , 2);
windowOffset = 80;
scatterPlot = new ImagePlus ("ScatterPlot", new ByteProcessor (scatterPlotSize + windowOffset, scatterPlotSize + windowOffset));
scatterPlot .addImageListener (this);
scatterPlot .show ();
scatterPlotWindow = scatterPlot.getWindow ();
scatterPlotWindow .addKeyListener (this);
scatterPlotWindow .setIconImage (icon);
canvas = scatterPlotWindow.getCanvas ();
canvas .addKeyListener (this);
canvas .addMouseListener (this);
Panel bottomPanel = new Panel ();
int hgap = IJ.isMacOSX ()?1:5;
set = new Button (" Set ");
set .addActionListener (this);
set .addKeyListener (this);
bottomPanel .add (set);
bottomPanel .setLayout (new FlowLayout(FlowLayout.RIGHT,hgap,0));
statusLabel = new Label ();
statusLabel .setFont (new Font("Monospaced", Font.PLAIN, 12));
statusLabel .setBackground (new Color(220, 220, 220));
bottomPanel .add (statusLabel);
scatterPlotWindow .add (bottomPanel);
// spaceString = String.format ("%1$" + Math.round(0.047 * scatterPlotWindow.getWidth() - 20) + "s", " ");
spaceString = String.format ("%1$" + Math.round(0.039 * scatterPlotWindow.getWidth() - 21) + "s", " ");
if(scatterPlotSize == 256)
statusLabel .setText ( "min1: " + Math.round(minI1) + " max1: " + Math.round(maxI1) + " min2: " + Math.round(minI2) + " max2: " + Math.round(maxI2));
else
// statusLabel .setText (" minI1: " + Math.round(minI1) + spaceString + "maxI1: " + Math.round(maxI1) + spaceString + "minI2: " + Math.round(minI2) + spaceString + "maxI2: " + Math.round(maxI2));
statusLabel .setText (" Pearson: " + IJ.d2s(PearsonValue, precision) + spaceString + "minI1: " + Math.round(minI1) + spaceString + "maxI1: " + Math.round(maxI1) + spaceString + "minI2: " + Math.round(minI2) + spaceString + "maxI2: " + Math.round(maxI2));
statusLabel .setPreferredSize (new Dimension(scatterPlotWindow.getWidth() - 73, statusLabel.getPreferredSize().height));
scatterPlotWindow .pack();
return true;
}
public void build_scatter_plot()
{
xOffset = 60;
yOffset = windowOffset - xOffset;
image1Processor = image1.getProcessor();
image2Processor = image2.getProcessor();
scatterPlotProcessor = scatterPlot.getProcessor();
for (y = 0; y < h1; y++)
{
for (x = 0; x < w1; x++)
{
z1 = (int) (( image1Processor.getPixelValue(x, y) - scatterPlotMin1) * scatterPlotSize / (scatterPlotMax1 - scatterPlotMin1));
z2 = scatterPlotSize - (int) (( image2Processor.getPixelValue(x, y) - scatterPlotMin2) * scatterPlotSize / (scatterPlotMax2 - scatterPlotMin2));
count = (int) scatterPlotProcessor.getPixelValue(z1 + xOffset, z2 + yOffset);
count++;
scatterPlotProcessor.putPixelValue(z1 + xOffset, z2 + yOffset, count);
}
}
// scatterPlot .setRoi(new Roi(xOffset + scatterPlotSize + 1 - 150, yOffset, 150, 150));
scatterPlot .setRoi(new Roi(xOffset, yOffset, scatterPlotSize + 1, scatterPlotSize + 1));
// scatterPlot .setRoi(new Roi(xOffset + 20, yOffset, 237, 237));
scatterPlotRoi = scatterPlot.getRoi();
scatterPlotRoi .addRoiListener(this);
// resultImageRoi = resultImage.getRoi();
// resultImageRoi .addRoiListener(this);
build_plot_for_scatter_plot();
}
static public void rebuild_scatter_plot()
{
for (y = 0; y <= scatterPlotSize; y++)
for (x = 0; x <= scatterPlotSize; x++)
scatterPlotProcessor.putPixelValue(x + xOffset, y + yOffset, 0);
resultImageRoi = resultImage.getRoi();
if (resultImageRoi != null)
{
rect = resultImageRoi.getBounds();
if (rect.width == 0 || rect.height == 0)
resultImageRoi = null;
}
if(resultImageRoi == null)
{
// There is no ROI within the result image, thus I make the analysis within the whole picture
for (y = 0; y < h1; y++)
{
for (x = 0; x < w1; x++)
{
if(image1Processor.getPixelValue(x, y) > scatterPlotMin1 && image2Processor.getPixelValue(x, y) > scatterPlotMin2)
{
z1 = (int) (( image1Processor.getPixelValue(x, y) - scatterPlotMin1) * scatterPlotSize / (scatterPlotMax1 - scatterPlotMin1));
z2 = scatterPlotSize - (int) (( image2Processor.getPixelValue(x, y) - scatterPlotMin2) * scatterPlotSize / (scatterPlotMax2 - scatterPlotMin2));
count = (int) scatterPlotProcessor.getPixelValue(z1 + xOffset, z2 + yOffset);
count++;
scatterPlotProcessor.putPixelValue(z1 + xOffset, z2 + yOffset, count);
}
}
}
}
else
{ // There is no ROI within the result image, thus I make the analysis only within the ROI elements
pointsInsideRoi = resultImageRoi.getContainedPoints();
for (i = 0; i != pointsInsideRoi.length; i++)
{
if(pointsInsideRoi[i].x >= 0 && pointsInsideRoi[i].x < w1 && pointsInsideRoi[i].y >= 0 && pointsInsideRoi[i].y < h1)
{
if(image1Processor.getPixelValue(pointsInsideRoi[i].x, pointsInsideRoi[i].y) > scatterPlotMin1 && image2Processor.getPixelValue(pointsInsideRoi[i].x, pointsInsideRoi[i].y) > scatterPlotMin2)
{
z1 = (int) ((( image1Processor.getPixelValue(pointsInsideRoi[i].x, pointsInsideRoi[i].y)) - scatterPlotMin1) * scatterPlotSize / (scatterPlotMax1 - scatterPlotMin1));
z2 = scatterPlotSize - (int) ((( image2Processor.getPixelValue(pointsInsideRoi[i].x, pointsInsideRoi[i].y)) - scatterPlotMin2) * scatterPlotSize / (scatterPlotMax2 - scatterPlotMin2));
count = (int) scatterPlotProcessor.getPixelValue(z1 + xOffset, z2 + yOffset);
count++;
scatterPlotProcessor.putPixelValue(z1 + xOffset, z2 + yOffset, count);
}
}
}
}
// scatterPlot.updateAndDraw();
scatterPlot.draw();
}
public static String analyze(boolean _write_results, boolean _set_roi, String separator)
{
rebuild_scatter_plot();
return comparison(_write_results, _set_roi);
}
public static String analyze(boolean _write_results, boolean _set_roi)
{
return analyze(_write_results, _set_roi, ";");
}
public static String analyze(boolean _write_results, boolean _set_roi, int[] _outputIDs, String separator)
{
int i;
String output;
String [] outputs;
rebuild_scatter_plot();
output = comparison(_write_results, _set_roi);
Arrays.sort(_outputIDs);
outputs = Tools.split(output, ";");
output = _outputIDs[0] < outputs.length ? outputs[_outputIDs[0]] : "outOfBoundsOfChosenIndex";
for(i = 1; i != _outputIDs.length; i++)
if(_outputIDs[i] < outputs.length)
output += separator + outputs[_outputIDs[i]];
else
output += separator + "outOfBoundsOfChosenIndex";
return output;
}
public static String analyze(boolean _write_results, boolean _set_roi, int[] _outputIDs)
{
return analyze (_write_results, _set_roi, _outputIDs, ";");
}
// call("Colocalization_Finder.analyzeByMacro", _write_results);
public static String analyzeByMacro(String _write_results)
{
return analyze (Boolean.valueOf(_write_results), false);
}
// call("Colocalization_Finder.analyzeByMacro", _write_results, _set_roi);
public static String analyzeByMacro(String _write_results, String _set_roi)
{
return analyze (Boolean.valueOf(_write_results), Boolean.valueOf(_set_roi));
}
public static String analyzeByMacro(String _write_results, String _set_roi, String _outputIDs)
{
return analyzeByMacro (_write_results, _set_roi, _outputIDs, ";");
}
// call("Colocalization_Finder.getResultsLinesCount");
public static String getResultsLinesCount()
{
ResultsWindow = (TextWindow) WindowManager.getWindow(ResultsTitle);
if(ResultsWindow == null)
return String.valueOf(0);
return String.valueOf(ResultsWindow.getTextPanel().getLineCount());
}
public static String analyzeByMacro(String _write_results, String _set_roi, String _outputIDs, String separator)
{
int i, j;
int [] outputIDs , outputIDsSub;
String [] outputIDSplitted, outputIDSplittedSub, outputIDSplittedFull;
outputIDSplitted = Tools.split(_outputIDs, ",");
for(i = 0; i != outputIDSplitted.length; i++)
{
if(outputIDSplitted[i].indexOf("-") != -1)
{
outputIDSplittedSub = Tools.split(outputIDSplitted[i], "-");
outputIDsSub = new int[2];
outputIDsSub[0] = Integer.valueOf(outputIDSplittedSub[0]);
outputIDsSub[1] = Integer.valueOf(outputIDSplittedSub[1]);
Arrays.sort (outputIDsSub);
for(j = i; j != outputIDSplitted.length - 1; j++)
outputIDSplitted[j] = outputIDSplitted[j + 1];
outputIDSplitted[outputIDSplitted.length - 1] = String.valueOf(outputIDsSub[0]);
outputIDSplittedSub = new String[outputIDsSub[1] - outputIDsSub[0]];
for(j = 0; j != outputIDSplittedSub.length; j++)
outputIDSplittedSub[j] = String.valueOf(outputIDsSub[0] + 1 + j);
outputIDSplittedFull = Arrays.copyOf(outputIDSplitted, outputIDSplitted.length + outputIDSplittedSub.length);
System.arraycopy (outputIDSplittedSub, 0, outputIDSplittedFull, outputIDSplitted.length, outputIDSplittedSub.length);
outputIDSplitted = outputIDSplittedFull;
i--;
}
}
outputIDs = new int[outputIDSplitted.length];
for(i = 0; i != outputIDSplitted.length; i++)
outputIDs[i] = Integer.valueOf(outputIDSplitted[i]);
return analyze(Boolean.valueOf(_write_results), Boolean.valueOf(_set_roi), outputIDs, separator);
}
// call("Colocalization_Finder.setScatterPlotRoi", 0, 500, 100, 200);
public static void setScatterPlotRoi(String _minI1, String _maxI1, String _minI2, String _maxI2)
{
try { minI1 = Double.valueOf(_minI1) ;}
catch(NumberFormatException e) { minI1 = scatterPlotMin1 ;}
try { maxI1 = Double.valueOf(_maxI1) ;}
catch(NumberFormatException e) { maxI1 = scatterPlotMax1 ;}
try { minI2 = Double.valueOf(_minI2) ;}
catch(NumberFormatException e) { minI2 = scatterPlotMin2 ;}
try { maxI2 = Double.valueOf(_maxI2) ;}
catch(NumberFormatException e) { maxI2 = scatterPlotMax2 ;}
setScatterPlotRoi(minI1, maxI1, minI2, maxI2);
}
// call("Colocalization_Finder.setScatterPlotLimits", 0, 500, 100, 200);
public static void setScatterPlotLimits(String _scatterPlotMin1, String _scatterPlotMax1, String _scatterPlotMin2, String _scatterPlotMax2)
{
try { scatterPlotMin1 = Double.valueOf(_scatterPlotMin1) ;}
catch(NumberFormatException e) { scatterPlotMin1 = min1 ;}
try { scatterPlotMax1 = Double.valueOf(_scatterPlotMax1) ;}
catch(NumberFormatException e) { scatterPlotMax1 = max1 ;}
try { scatterPlotMin2 = Double.valueOf(_scatterPlotMin2) ;}
catch(NumberFormatException e) { scatterPlotMin2 = min2 ;}
try { scatterPlotMax2 = Double.valueOf(_scatterPlotMax2) ;}
catch(NumberFormatException e) { scatterPlotMax2 = max2 ;}
scatterPlotProcessor .setColor(Color.black);
scatterPlotProcessor .resetRoi();
scatterPlotProcessor .fill();
setScatterPlotGraphLimits ();
build_plot_for_scatter_plot ();
rebuild_scatter_plot ();
setScatterPlotRoi (minI1, maxI1, minI2, maxI2);
}
private static void setScatterPlotGraphLimits()
{
scatterPlotMin1 = Double.isNaN(scatterPlotMin1) || scatterPlotMin1 < 0 ? 0 : scatterPlotMin1;
scatterPlotMax1 = Double.isNaN(scatterPlotMax1) || scatterPlotMax1 > depth1 ? depth1 : scatterPlotMax1;
scatterPlotMin2 = Double.isNaN(scatterPlotMin2) || scatterPlotMin2 < 0 ? 0 : scatterPlotMin2;
scatterPlotMax2 = Double.isNaN(scatterPlotMax2) || scatterPlotMax2 > depth2 ? depth2 : scatterPlotMax2;
}
private static void setScatterPlotRoi(double minI1, double maxI1, double minI2, double maxI2)
{
double xtemp, ytemp, wtemp, htemp;
minI1 = Double.isNaN(minI1) || minI1 < scatterPlotMin1 ? scatterPlotMin1 : minI1;
maxI1 = Double.isNaN(maxI1) || maxI1 > scatterPlotMax1 ? scatterPlotMax1 : maxI1;
minI2 = Double.isNaN(minI2) || minI2 < scatterPlotMin2 ? scatterPlotMin2 : minI2;
maxI2 = Double.isNaN(maxI2) || maxI2 > scatterPlotMax2 ? scatterPlotMax2 : maxI2;
xtemp = scatterPlotSize / (scatterPlotMax1 - scatterPlotMin1) * (minI1 - scatterPlotMin1) + xOffset;
wtemp = scatterPlotSize / (scatterPlotMax1 - scatterPlotMin1) * (maxI1 - scatterPlotMin1) + xOffset + 1 - xtemp;
ytemp = scatterPlotSize / (scatterPlotMin2 - scatterPlotMax2) * (maxI2 - scatterPlotMin2) + yOffset + scatterPlotSize;
htemp = scatterPlotSize / (scatterPlotMin2 - scatterPlotMax2) * (minI2 - scatterPlotMin2) + yOffset + scatterPlotSize + 1 - ytemp;
scatterPlotRoi = new Roi(xtemp, ytemp, wtemp, htemp);
scatterPlot .setRoi(scatterPlotRoi);
}
private static boolean setScatterPlotRoiLimits()
{
boolean changed = false;
scatterPlotRoi = scatterPlot .getRoi();
if(scatterPlotRoi == null)
{
scatterPlotRoi = new Roi(xOffset, yOffset, scatterPlotSize + 1, scatterPlotSize + 1);
// scatterPlotRoi = new Roi(xOffset + 20, yOffset, 237, 237);
scatterPlot.setRoi(scatterPlotRoi);
}
else
{
rect = scatterPlotRoi .getBounds();
if (rect.width == 0 || rect.height == 0)
{
scatterPlotRoi = new Roi(xOffset, yOffset, scatterPlotSize + 1, scatterPlotSize + 1);
// scatterPlotRoi = new Roi(xOffset + 20, yOffset, 237, 237);
scatterPlot.setRoi(scatterPlotRoi);
}
}
coord = scatterPlotRoi.getBounds();
minI1 = (int) ( scatterPlotMin1 + ( coord.x - xOffset ) * (scatterPlotMax1 - scatterPlotMin1) / scatterPlotSize );
maxI1 = (int) ( scatterPlotMin1 + ( coord.x - xOffset + coord.width - 1) * (scatterPlotMax1 - scatterPlotMin1) / scatterPlotSize );
minI2 = (int) ( scatterPlotMin2 + (scatterPlotSize - coord.y + yOffset - coord.height + 1) * (scatterPlotMax2 - scatterPlotMin2) / scatterPlotSize );
maxI2 = (int) ( scatterPlotMin2 + (scatterPlotSize - coord.y + yOffset ) * (scatterPlotMax2 - scatterPlotMin2) / scatterPlotSize );
if(!IJ.shiftKeyDown())
{
if(minI1 < scatterPlotMin1 || minI1 > scatterPlotMax1)
{
minI1 = scatterPlotMin1;
changed = true;
}
if(maxI1 < scatterPlotMin1 || maxI1 > scatterPlotMax1)
{
maxI1 = scatterPlotMax1;
changed = true;
}
if(minI2 < scatterPlotMin2 || minI2 > scatterPlotMax2)
{
minI2 = scatterPlotMin2;
changed = true;
}
if(maxI2 < scatterPlotMin2 || maxI2 > scatterPlotMax2)
{
maxI2 = scatterPlotMax2;
changed = true;
}
}
return changed;
}
static String comparison(boolean write_results, boolean set_roi)
{
if(!comparisonRunning)
comparisonRunning = true;
else
{
comparisonRunning = false;
// IJ.log("comparison already running!");
return "comparison already running!";
}
if (Toolbar.getInstance().getToolId() > 4)
{
gd = new GenericDialog("Setting analysis ROI");
gd.setIconImage (icon);
gd.addMessage ("A Selection Tool needs to be chosen in order to modify the analysis ROI.\r\n\r\n Do you want the Rectangular_Selection_Tool to be set?");
gd.setOKLabel ("Yes");
gd.setCancelLabel ("No");
gd.showDialog();
if (gd.wasCanceled())
return "";
Toolbar.getInstance().setTool(Toolbar.RECTANGLE);
}
counter = 0;
if(setScatterPlotRoiLimits())
setScatterPlotRoi(minI1, maxI1, minI2, maxI2);
resultImageRoi = resultImage.getRoi();
if(resultImageRoi != null)
{
rect = resultImageRoi.getBounds();
if (rect.width == 0 || rect.height == 0)
resultImageRoi = null;
}
if(resultImageRoi == null)
{ // There is no ROI within the result image, thus I make the analysis within the whole picture
intx = new double[w1 * h1];
inty = new double[w1 * h1];
lesx = new double[w1 * h1];
lesy = new double[w1 * h1];
for (y = 0; y < h1; y++)
{
for (x = 0; x < w1; x++)
{
pos = y * w1 + x;
// vi1 = (int) ( image1Processor.getPixelValue(x, y) * scatterPlotSize / depth1);
// vi2 = (int) ( image2Processor.getPixelValue(x, y) * scatterPlotSize / depth2);
vi1 = (int) ((image1Processor.getPixelValue(x, y) - scatterPlotMin1) * scatterPlotSize / scatterPlotMax1);
vi2 = (int) ((image2Processor.getPixelValue(x, y) - scatterPlotMin2) * scatterPlotSize / scatterPlotMax2);
intx[y * w1 + x] = image1Processor.getPixelValue(x, y);
inty[y * w1 + x] = image2Processor.getPixelValue(x, y);
setMaskPixels();
}
}
}
else
{ // There is a ROI within the result image, thus I make the analysis only within the ROI elements
pointsInsideRoi = resultImageRoi.getContainedPoints();
intx = new double[pointsInsideRoi.length];
inty = new double[pointsInsideRoi.length];
lesx = new double[pointsInsideRoi.length];
lesy = new double[pointsInsideRoi.length];
for (i = 0; i != pointsInsideRoi.length; i++)
{
if(pointsInsideRoi[i].x >= 0 && pointsInsideRoi[i].x < w1 && pointsInsideRoi[i].y >= 0 && pointsInsideRoi[i].y < h1)
{
pos = pointsInsideRoi[i].y * w1 + pointsInsideRoi[i].x;
// vi1 = (int) ( image1Processor.getPixelValue(pointsInsideRoi[i].x, pointsInsideRoi[i].y) * scatterPlotSize / depth1);
// vi2 = (int) ( image2Processor.getPixelValue(pointsInsideRoi[i].x, pointsInsideRoi[i].y) * scatterPlotSize / depth2);
vi1 = (int) ((image1Processor.getPixelValue(pointsInsideRoi[i].x, pointsInsideRoi[i].y) - scatterPlotMin1) * scatterPlotSize / scatterPlotMax1);
vi2 = (int) ((image2Processor.getPixelValue(pointsInsideRoi[i].x, pointsInsideRoi[i].y) - scatterPlotMin2) * scatterPlotSize / scatterPlotMax2);
intx[i] = image1Processor.getPixelValue(pointsInsideRoi[i].x, pointsInsideRoi[i].y);
inty[i] = image2Processor.getPixelValue(pointsInsideRoi[i].x, pointsInsideRoi[i].y);
setMaskPixels();
}
}
}
lesx = Arrays.copyOf(lesx, counter);
lesy = Arrays.copyOf(lesy, counter);
colocMask = new ByteProcessor(w1, h1, maskPixels);
colocMask .setBinaryThreshold();
ts = new ThresholdToSelection();
colocMaskRoi = ts.convert(colocMask);
if (resultImageRoi != null)
{
if (resultImageRoi instanceof ShapeRoi)
sr1 = (ShapeRoi)resultImageRoi.clone();
else
sr1 = new ShapeRoi(resultImageRoi);
if (colocMaskRoi != null)
{
if (colocMaskRoi instanceof ShapeRoi)
sr2 = (ShapeRoi)colocMaskRoi.clone();
else
sr2 = new ShapeRoi(colocMaskRoi);
colocMaskRoi = sr1.and(sr2);
}
}
if(colocMaskRoi != null)
colocMaskRoi .setFillColor(Colors.decode("#EEFFFFFF", null));
// resultImage .setRoi(colocMaskRoi);
resultImageOverlay = resultImage.getOverlay();
if (resultImageOverlay == null)
{
resultImageOverlay = new Overlay();
resultImageOverlay .addElement(colocMaskRoi);
}
else
resultImageOverlay .set(colocMaskRoi, 0);
// resultImageOverlay .set(colocMaskRoi, resultImageOverlay.size() - 1); Generated some bugs thus replaced the 'resultImageOverlay.size() - 1' by '0'
resultImage .setOverlay(resultImageOverlay);
percentPixels = ((double) counter / (w1 * h1)) * 100.0;
cf = new CurveFitter(lesx, lesy);
cf.doFit(CurveFitter.STRAIGHT_LINE);
cfParams = cf.getParams();
String output = set_roi ? getResultsAsString(";") + ";" + colors[color].name : getResultsAsString(";");
// if (write_results && IJ.getToolName() != "polygon")
// if (write_results && (IJ.getToolName() == "rectangle" || IJ.getToolName() == "roundrect" || IJ.getToolName() == "rotrect" || IJ.getToolName() == "oval" || IJ.getToolName() == "ellipse" || IJ.getToolName() == "brush" || IJ.getToolName() == "freehand" || IJ.getToolName() == "polygon"))
if (write_results && Toolbar.getInstance().getToolId() < 4)
{
ResultsWindow = (TextWindow) WindowManager.getWindow(ResultsTitle);
if(ResultsWindow == null)
{
ResultsWindow = new TextWindow(ResultsTitle, ResultsHeadings, "", 1040, 300);
ResultsWindow .setIconImage (icon);
}
ResultsWindow.append(output.replace(";", "\t"));
}
// if (set_roi && IJ.getToolName() != "polygon")
// if (set_roi && (IJ.getToolName() == "rectangle" || IJ.getToolName() == "roundrect" || IJ.getToolName() == "rotrect" || IJ.getToolName() == "oval" || IJ.getToolName() == "ellipse" || IJ.getToolName() == "brush" || IJ.getToolName() == "freehand" || IJ.getToolName() == "polygon"))
if (set_roi && Toolbar.getInstance().getToolId() < 4)
{
scatterPlotRoi = scatterPlot.getRoi();
scatterPlotRoi .setStrokeColor(colors[color].color);
rm = RoiManager.getInstance();
if (rm == null)
{
rm = new RoiManager();
rm .setIconImage (icon);
}
rm .addRoi(scatterPlotRoi);
rm .rename(rm.getCount() - 1, colors[color].name);
rm .runCommand(scatterPlot, "Show All without labels");
rm .runCommand(resultImage, "Show None"); // Line added to get rid of the Rois within the RoiManager which are added by the line "rm.runCommand(i3,"Show All without labels");" in the case imResu is the selected window - And I don't know why!!!
scatterPlot .restoreRoi();
scatterPlotRoi = scatterPlot.getRoi();
scatterPlotRoi .setStrokeColor(Color.yellow);
resultImageOverlay = resultImage.getOverlay();
// if (resultImageOverlay == null) resultImageOverlay = new Overlay();
colocMaskRoi .setFillColor(colors[color].color);
// resultImage .setRoi(colocMaskRoi);
resultImageOverlay .addElement(colocMaskRoi);
resultImage .setOverlay(resultImageOverlay);
// resultImage .killRoi();
color = color < 4 ? color + 1 : 0;
}
comparisonRunning = false;
// statusLabel.setPreferredSize(new Dimension(scatterPlotWindow.getWidth() - 73, statusLabel.getPreferredSize().height));
// spaceString = String.format("%1$" + Math.round(0.047 * scatterPlotWindow.getWidth() - 20) + "s", " ");
if(show_checked == default_checked)
{
if(scatterPlotSize == 256)
statusLabel.setText( "min1: " + Math.round(minI1) + " max1: " + Math.round(maxI1) + " min2: " + Math.round(minI2) + " max2: " + Math.round(maxI2));
else
// statusLabel.setText(" minI1: " + Math.round(minI1) + spaceString + "maxI1: " + Math.round(maxI1) + spaceString + "minI2: " + Math.round(minI2) + spaceString + "maxI2: " + Math.round(maxI2));
statusLabel.setText(" Pearson: " + IJ.d2s(PearsonValue, precision) + spaceString + "minI1: " + Math.round(minI1) + spaceString + "maxI1: " + Math.round(maxI1) + spaceString + "minI2: " + Math.round(minI2) + spaceString + "maxI2: " + Math.round(maxI2));
}
else
{
String str = getStatusLabelString(1, nbChecked);
int size = statusLabel.getPreferredSize().width - scatterPlotProcessor.getStringWidth(str) - 100;
if (size / (5 * nbChecked) > 0)
str = getStatusLabelString(size / (5 * nbChecked), nbChecked);
statusLabel.setText(str);
}
return output;
}