-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPairCorr.m
More file actions
3549 lines (3153 loc) · 116 KB
/
PairCorr.m
File metadata and controls
3549 lines (3153 loc) · 116 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
classdef PairCorr < handle
% PairCorr class written by Michael Wester, Keith Lidke, Carolyn Pehlke and
% others as noted internally (5/16/2017) <wester@math.unm.edu>
% The New Mexico Center for the Spatiotemporal Modeling of Cell Signaling
% University of New Mexico Health Sciences Center
% Albuquerque, New Mexico, USA 87131
% Copyright (c) 2015-2017 by Michael J. Wester and Keith A. Lidke
%
% Example main program:
%
% pc = PairCorr();
% pc.Results = 'results';
%
% [x, y] = textread('../data/9021_5.txt', '%*u %u %u %*u', ...
% 'headerlines', 1);
% XY_5 = [x, y];
% [x, y] = textread('../data/9021_10.txt', '%*u %u %u %*u', ...
% 'headerlines', 1);
% XY_10 = [x, y];
% pc.ROI = [0, 0, 7400, 6000];
% results_pcc = pc.pair_correlation(2.7559, '9021', XY_5, XY_10)
% results_pac1 = pc.pair_correlation(2.7559, '9021_5', XY_5)
% results_pac2 = pc.pair_correlation(2.7559, '9021_10', XY_10)
%
% results_Vpcc = ...
% pc.pair_correlation_Veatch(XY_5, XY_10, 2.7559, '9021', 'cross');
% results_Vpac1 = ...
% pc.pair_correlation_Veatch(XY_5, [], 2.7559, '9021_5', 'auto');
% results_Vpac2 = ...
% pc.pair_correlation_Veatch(XY_10, [], 2.7559, '9021_10','auto');
% =============================================================================
properties
% =============================================================================
% If ROI is provided, it will be used, otherwise if ROI_size is defined,
% then the (x_min, y_min) computed from the data will be used, otherwise if
% neither is provided, then the xy_size will be calculated using the
% (x_min, y_min, x_max, y_max) computed from the data as
% xy_size = min(x_max - x_min, y_max - y_min)
ROI = []; % [x_min, y_min, x_size, y_size]
ROI_size = []; % Set x_size = y_size = ROI_size
Font_props = {'FontSize', 15, 'FontWeight', 'bold'};
%Line_props = {'LineWidth', 3};
% If Fig_ext is empty, plot to the screen and save as .fig; otherwise, do
% not plot to the screen, but save as .fig AND .Fig_ext .
Fig_ext = 'pdf';
Lines = true; % If true, plot lines rather than points for g(r) vs. r
Results = '.'; % Directory to store results.
Rmax_axis = -1; % Sets plotting limit if > 0
% Default fit model for pair_correlation_Veatch. This can also be supplied
% as the optional last (6th) argument to this function.
Veatch_fit = 'exponential_and_gaussian';
%Veatch_fit = 'exponential_and_cosine';
%Veatch_fit = 'exponential';
% Note that pair_correlation and pair_correlation_ROIcombined use a 2D
% Gaussian fit model.
% Properties for {PC,PCC,PAC}_SR:
Verbose = false; % verbose output and extra saved .mat files
% Perform auto-correlations on each ROI as well as cross-correlations.
Auto = false; % only applicable to PC_SR
% Use the original Veatch code for pairwise correlation as well as code
% adapted from SuperCluster (originally written by Carolyn Pehlke and Keith
% Lidke).
Veatch = false;
% If true, correlate on ROIs rather than the entire image. This is usually
% preferred except for small datasets.
ROIs = true;
Pixel2nm = 104; % conversion from pixels to nm
% Histogram bin size for pairwise correlation---this is the number of pixels
% per bin over which correlation statistics are collected.
Hist_bin_size = 104 / 20;
% H-SET pass 2 parameters (in which clustering is performed):
% If true, perform H-SET on observations. H-SET (Hierarchical Single
% Emitter Hypothesis Test), is a top-down hierarchical clustering algorithm
% that collapses clusters of observations of blinking fluorophores into
% single estimates of the true locations (localizations) of the
% fluorophores.
HSET = false;
Algorithm = 'DBSCAN_Daszykowski';
E = 30; % epsilon for clustering (nm)
MinPts = 3; % minimum number of points for clustering
% The shrink factor is in the interval [0, 1].
% ShrinkFactor = 0 produces convex hulls around clustered points.
% ShrinkFactor = 1 produces compact boundaries around clustered points,
% which may be very concave.
% The MATLAB default value for the shrink factor is 0.5
ShrinkFactor = 0.5;
% =============================================================================
end % properties
methods
% =============================================================================
function PC_SR(obj, SRDR1, SRDR2, basefile)
% Perform pair correlation before and after H-SET collapse on the given input
% SRD.Results datasets.
%
% Cross-correlation and optionally auto-correlation are performed.
%
% Use the mouse to select ROIs (regions of interest):
% left click chooses the center of a fixed size (x_size x y_size) region
% right click chooses an adjustable rectangular size region
% key press:
% backspace or delete deletes the previous region
% anything else terminates selection
% Results directory needs to exist beforehand.
base = fullfile(obj.Results, basefile);
% ==========================================================================
if obj.HSET
SRc = SRcluster();
SRc.Algorithm = obj.Algorithm;
SRc.E = obj.E;
SRc.minPts = obj.MinPts;
SRc.PlotFigures = false;
SRc.ShrinkFactor = obj.ShrinkFactor;
end
% --------------------------------------------------------------------------
obj.Lines = true;
% ==========================================================================
RT = ROITools();
if obj.ROIs
% Choose some ROIs to analyze the data over.
if ~isempty(obj.ROI)
RT.ROI_sizes = [obj.ROI(3), obj.ROI(4)];
elseif ~isempty(obj.ROI_size)
RT.ROI_sizes = [obj.ROI_size, obj.ROI_size];
else
error('ROI sizes not defined!');
end
RT.pixel2nm = obj.Pixel2nm;
file_display = regexprep(basefile, '_', '\\_');
[n_ROIs, RoI, Sigma_Reg] = RT.getROI({SRDR1, SRDR2}, file_display);
saveas(gcf, sprintf('%s_ROIs.fig', base));
if ~isempty(obj.Fig_ext)
print(sprintf('%s_ROIs.%s', base, obj.Fig_ext), ['-d', obj.Fig_ext]);
end
close
else
% Analyze the data over the entire region.
n_ROIs = 1;
Sigma_Reg{1} = [10, 10];
Sigma_Reg{2} = [10, 10];
[XY1, XY_STD1, Sigma_Reg1] = RT.import_XY(SRDR1, obj.Pixel2nm);
[XY2, XY_STD2, Sigma_Reg2] = RT.import_XY(SRDR2, obj.Pixel2nm);
RoI{1}.X{1} = XY1(:, 1);
RoI{1}.Y{1} = XY1(:, 2);
RoI{1}.X{2} = XY2(:, 1);
RoI{1}.Y{2} = XY2(:, 2);
if ~isempty(XY_STD1)
RoI{1}.X_STD{1} = XY_STD1(:, 1);
RoI{1}.Y_STD{1} = XY_STD1(:, 2);
else
RoI{1}.X_STD{1} = zeros(size(XY1, 1), 1);
RoI{1}.Y_STD{1} = zeros(size(XY1, 1), 1);
end
if ~isempty(XY_STD2)
RoI{1}.X_STD{2} = XY_STD2(:, 1);
RoI{1}.Y_STD{2} = XY_STD2(:, 2);
else
RoI{1}.X_STD{2} = zeros(size(XY2, 1), 1);
RoI{1}.Y_STD{2} = zeros(size(XY2, 1), 1);
end
RoI{1}.ROI = [min([RoI{1}.X{1}; RoI{1}.X{2}]), ...
max([RoI{1}.X{1}; RoI{1}.X{2}]), ...
min([RoI{1}.Y{1}; RoI{1}.Y{2}]), ...
max([RoI{1}.Y{1}; RoI{1}.Y{2}])];
end
save(sprintf('%s_ROIs.mat', base), 'n_ROIs', 'RoI', 'Sigma_Reg');
% Perform H-SET (optionally) and pair correlation over each ROI in turn,
% combining the results at the end.
ROIs_pre = cell(n_ROIs, 1);
ROIs_post = cell(n_ROIs, 1);
for i = 1 : n_ROIs
fprintf('ROI %d\n\n', i);
obj.ROI = [RoI{i}.ROI(1), RoI{i}.ROI(3), ...
RoI{i}.ROI(2) - RoI{i}.ROI(1), RoI{i}.ROI(4) - RoI{i}.ROI(3)];
XY1 = [ RoI{i}.X{1}, RoI{i}.Y{1} ];
XY2 = [ RoI{i}.X{2}, RoI{i}.Y{2} ];
sigma1 = [ RoI{i}.X_STD{1}, RoI{i}.Y_STD{1} ];
sigma2 = [ RoI{i}.X_STD{2}, RoI{i}.Y_STD{2} ];
base_file_pre = sprintf('%s_ROI%d_pre', basefile, i);
% --- BEGIN Auto --------------------------------------------------------
if obj.Auto
base_file_pre1 = sprintf('%s_L1_ROI%d_pre', basefile, i);
base_file_pre2 = sprintf('%s_L2_ROI%d_pre', basefile, i);
end
% --- END Auto ----------------------------------------------------------
if obj.HSET
base_file_post = sprintf('%s_ROI%d_post', basefile, i);
% --- BEGIN Auto -----------------------------------------------------
if obj.Auto
base_file_post1 = sprintf('%s_L1_ROI%d_post', basefile, i);
base_file_post2 = sprintf('%s_L2_ROI%d_post', basefile, i);
end
% --- END Auto -------------------------------------------------------
base1 = sprintf('%s_L1_ROI%d', base, i);
base2 = sprintf('%s_L2_ROI%d', base, i);
end
ROIs_pre{i}.ROI = RoI{i}.ROI;
ROIs_pre{i}.XY1 = XY1;
ROIs_pre{i}.XY2 = XY2;
results_pccA = obj.pair_correlation(obj.Hist_bin_size, base_file_pre, ...
XY1, XY2)
% --- BEGIN Auto --------------------------------------------------------
if obj.Auto
results_pac1A = obj.pair_correlation(obj.Hist_bin_size, ...
base_file_pre1, XY1)
results_pac2A = obj.pair_correlation(obj.Hist_bin_size, ...
base_file_pre2, XY2)
end
% --- END Auto ----------------------------------------------------------
if obj.Veatch
results_VpccA = obj.pair_correlation_Veatch(XY1, XY2, ...
obj.Hist_bin_size, base_file_pre, 'cross');
% --- BEGIN Auto -----------------------------------------------------
if obj.Auto
results_Vpac1A = obj.pair_correlation_Veatch(XY1, [], ...
obj.Hist_bin_size, base_file_pre1, 'auto');
results_Vpac2A = obj.pair_correlation_Veatch(XY2, [], ...
obj.Hist_bin_size, base_file_pre2, 'auto');
end
% --- END Auto -------------------------------------------------------
end
if obj.HSET
[XY1_SR, sigma1_SR, combined1] = ...
SRc.clusterSR(XY1, sigma1, Sigma_Reg{1});
% --- BEGIN Verbose --------------------------------------------------
if obj.Verbose
SRclusterFig1 = SRc.plotSRclusters();
SRclusterFig1.Visible = 'on';
saveas(SRclusterFig1, sprintf('%s_%s.fig', base1, 'SRcluster'));
close(SRclusterFig1);
SRcollapseFig1 = SRc.plotSRcollapse();
SRcollapseFig1.Visible = 'on';
saveas(SRcollapseFig1, sprintf('%s_%s.fig', base1, 'SRcollapse'));
close(SRcollapseFig1);
[results1, analysisFigs1] = SRc.analyzeSRclusters();
for j = 1 : length(analysisFigs1)
analysisFigs1{j}.Visible = 'on';
saveas(analysisFigs1{j}, ...
sprintf('%s_%s%1d.fig', base1, 'SR', j));
close(analysisFigs1{j});
end
end
% --- END Verbose ----------------------------------------------------
[XY2_SR, sigma2_SR, combined2] = ...
SRc.clusterSR(XY2, sigma2, Sigma_Reg{2});
% --- BEGIN Verbose --------------------------------------------------
if obj.Verbose
SRclusterFig2 = SRc.plotSRclusters();
SRclusterFig2.Visible = 'on';
saveas(SRclusterFig2, sprintf('%s_%s.fig', base2, 'SRcluster'));
close(SRclusterFig2);
SRcollapseFig2 = SRc.plotSRcollapse();
SRcollapseFig2.Visible = 'on';
saveas(SRcollapseFig2, sprintf('%s_%s.fig', base2, 'SRcollapse'));
close(SRcollapseFig2);
[results2, analysisFigs2] = SRc.analyzeSRclusters();
for j = 1 : length(analysisFigs2)
analysisFigs2{j}.Visible = 'on';
saveas(analysisFigs2{j}, ...
sprintf('%s_%s%1d.fig', base2, 'SR', j));
close(analysisFigs2{j});
end
end
% --- END Verbose ----------------------------------------------------
ROIs_post{i}.ROI = RoI{i}.ROI;
ROIs_post{i}.XY1 = XY1_SR;
ROIs_post{i}.XY2 = XY2_SR;
% --- BEGIN Verbose --------------------------------------------------
if obj.Verbose
save(sprintf('%s_ROI%d_XYsigma.mat', base, i), ...
'XY1', 'sigma1', 'XY2', 'sigma2', ...
'XY1_SR', 'sigma1_SR', 'XY2_SR', 'sigma2_SR');
end
% --- END Verbose ----------------------------------------------------
results_pccB = obj.pair_correlation(obj.Hist_bin_size, ...
base_file_post, XY1_SR, XY2_SR)
% --- BEGIN Auto -----------------------------------------------------
if obj.Auto
results_pac1B = obj.pair_correlation(obj.Hist_bin_size, ...
base_file_post1, XY1_SR)
results_pac2B = obj.pair_correlation(obj.Hist_bin_size, ...
base_file_post2, XY2_SR)
end
% --- END Auto -------------------------------------------------------
if obj.Veatch
results_VpccB = obj.pair_correlation_Veatch(XY1_SR, XY2_SR, ...
obj.Hist_bin_size, base_file_post, 'cross');
% --- BEGIN Auto --------------------------------------------------
if obj.Auto
results_Vpac1B = obj.pair_correlation_Veatch(XY1_SR, [], ...
obj.Hist_bin_size, base_file_post1, 'auto');
results_Vpac2B = obj.pair_correlation_Veatch(XY2_SR, [], ...
obj.Hist_bin_size, base_file_post2, 'auto');
end
% --- END Auto ----------------------------------------------------
end
figure();
hold on
plot(XY1_SR(:, 1), XY1_SR(:, 2), 'k.', 'MarkerSize', 10);
plot(XY2_SR(:, 1), XY2_SR(:, 2), 'b.', 'MarkerSize', 10);
title(sprintf('Collapsed Two Color Plot ROI %d', i));
xlabel('x (nm)');
ylabel('y (nm)');
hold off
if ~isempty(obj.Fig_ext)
print(sprintf('%s_ROI%d_collapsed.%s', base, i, obj.Fig_ext), ...
['-d', obj.Fig_ext]);
else
print(sprintf('%s_ROI%d_collapsed.pdf', base, i), '-dpdf');
end
close(gcf);
% --- BEGIN Verbose --------------------------------------------------
if obj.Verbose
if obj.Auto && obj.Veatch
save(sprintf('%s_ROI%d_results.mat', base, i), ...
'results_pccA', 'results_pac1A', 'results_pac2A', ...
'results_VpccA', 'results_Vpac1A', 'results_Vpac2A', ...
'results_pccB', 'results_pac1B', 'results_pac2B', ...
'results_VpccB', 'results_Vpac1B', 'results_Vpac2B', ...
'results1', 'results2', 'obj', 'SRc');
else
save(sprintf('%s_ROI%d_results.mat', base, i), ...
'results_pccA', 'results_pccB', ...
'results1', 'results2', 'obj', 'SRc');
end
end
% --- END Verbose ----------------------------------------------------
end
end
if obj.ROIs
fprintf('ROI combined\n\n');
base_pre = sprintf('%s_pre', basefile);
resultsRC_pccA = ...
obj.pair_correlation_ROIcombined(obj.Hist_bin_size, base_pre, ...
2, n_ROIs, ROIs_pre)
% --- BEGIN Auto --------------------------------------------------------
if obj.Auto
base_pre1 = sprintf('%s_L1_pre', basefile);
base_pre2 = sprintf('%s_L2_pre', basefile);
resultsRC_pac1A = ...
obj.pair_correlation_ROIcombined(obj.Hist_bin_size, base_pre1, ...
1, n_ROIs, ROIs_pre, 1)
resultsRC_pac2A = ...
obj.pair_correlation_ROIcombined(obj.Hist_bin_size, base_pre2, ...
1, n_ROIs, ROIs_pre, 2)
end
% --- END Auto ----------------------------------------------------------
if obj.HSET
base_post = sprintf('%s_post', basefile);
resultsRC_pccB = ...
obj.pair_correlation_ROIcombined(obj.Hist_bin_size, base_post, ...
2, n_ROIs, ROIs_post)
% --- BEGIN Auto -----------------------------------------------------
if obj.Auto
base_post1 = sprintf('%s_L1_post', basefile);
base_post2 = sprintf('%s_L2_post', basefile);
resultsRC_pac1B = ...
obj.pair_correlation_ROIcombined(obj.Hist_bin_size, ...
base_post1, 1, n_ROIs, ...
ROIs_post, 1)
resultsRC_pac2B = ...
obj.pair_correlation_ROIcombined(obj.Hist_bin_size, ...
base_post2, 1, n_ROIs, ...
ROIs_post, 2)
save(sprintf('%s_results.mat', base), ...
'resultsRC_pccA', 'resultsRC_pccB', ...
'resultsRC_pac1A', 'resultsRC_pac2A', ...
'resultsRC_pac1B', 'resultsRC_pac2B');
% --- END Auto -------------------------------------------------------
else
save(sprintf('%s_results.mat', base), ...
'resultsRC_pccA', 'resultsRC_pccB');
end
else
% --- BEGIN Auto -----------------------------------------------------
if obj.Auto
save(sprintf('%s_results.mat', base), 'resultsRC_pccA', ...
'resultsRC_pac1A', 'resultsRC_pac2A');
% --- END Auto -------------------------------------------------------
else
save(sprintf('%s_results.mat', base), 'resultsRC_pccA');
end
end
end
end
% -----------------------------------------------------------------------------
function PCC_SR(obj, SRDR1, SRDR2, basefile)
% Perform pair correlation before and after H-SET collapse on the given input
% SRD.Results datasets.
%
% Cross-correlation is performed.
%
% Use the mouse to select ROIs (regions of interest):
% left click chooses the center of a fixed size (x_size x y_size) region
% right click chooses an adjustable rectangular size region
% key press:
% backspace or delete deletes the previous region
% anything else terminates selection
% Results directory needs to exist beforehand.
base = fullfile(obj.Results, basefile);
% ==========================================================================
if obj.HSET
SRc = SRcluster();
SRc.Algorithm = obj.Algorithm;
SRc.E = obj.E;
SRc.minPts = obj.MinPts;
SRc.PlotFigures = false;
SRc.ShrinkFactor = obj.ShrinkFactor;
end
% --------------------------------------------------------------------------
obj.Lines = true;
% ==========================================================================
RT = ROITools();
if obj.ROIs
% Choose some ROIs to analyze the data over.
if ~isempty(obj.ROI)
RT.ROI_sizes = [obj.ROI(3), obj.ROI(4)];
elseif ~isempty(obj.ROI_size)
RT.ROI_sizes = [obj.ROI_size, obj.ROI_size];
else
error('ROI sizes not defined!');
end
RT.pixel2nm = obj.Pixel2nm;
file_display = regexprep(basefile, '_', '\\_');
[n_ROIs, RoI, Sigma_Reg] = RT.getROI({SRDR1, SRDR2}, file_display);
saveas(gcf, sprintf('%s_ROIs.fig', base));
if ~isempty(obj.Fig_ext)
print(sprintf('%s_ROIs.%s', base, obj.Fig_ext), ['-d', obj.Fig_ext]);
end
close
else
% Analyze the data over the entire region.
n_ROIs = 1;
Sigma_Reg{1} = [10, 10];
Sigma_Reg{2} = [10, 10];
[XY1, XY_STD1, Sigma_Reg1] = RT.import_XY(SRDR1, obj.Pixel2nm);
[XY2, XY_STD2, Sigma_Reg2] = RT.import_XY(SRDR2, obj.Pixel2nm);
RoI{1}.X{1} = XY1(:, 1);
RoI{1}.Y{1} = XY1(:, 2);
RoI{1}.X{2} = XY2(:, 1);
RoI{1}.Y{2} = XY2(:, 2);
if ~isempty(XY_STD1)
RoI{1}.X_STD{1} = XY_STD1(:, 1);
RoI{1}.Y_STD{1} = XY_STD1(:, 2);
else
RoI{1}.X_STD{1} = zeros(size(XY1, 1), 1);
RoI{1}.Y_STD{1} = zeros(size(XY1, 1), 1);
end
if ~isempty(XY_STD2)
RoI{1}.X_STD{2} = XY_STD2(:, 1);
RoI{1}.Y_STD{2} = XY_STD2(:, 2);
else
RoI{1}.X_STD{2} = zeros(size(XY2, 1), 1);
RoI{1}.Y_STD{2} = zeros(size(XY2, 1), 1);
end
RoI{1}.ROI = [min([RoI{1}.X{1}; RoI{1}.X{2}]), ...
max([RoI{1}.X{1}; RoI{1}.X{2}]), ...
min([RoI{1}.Y{1}; RoI{1}.Y{2}]), ...
max([RoI{1}.Y{1}; RoI{1}.Y{2}])];
end
save(sprintf('%s_ROIs.mat', base), 'n_ROIs', 'RoI', 'Sigma_Reg');
% Perform H-SET (optionally) and pair correlation over each ROI in turn,
% combining the results at the end.
ROIs_pre = cell(n_ROIs, 1);
ROIs_post = cell(n_ROIs, 1);
for i = 1 : n_ROIs
fprintf('ROI %d\n\n', i);
obj.ROI = [RoI{i}.ROI(1), RoI{i}.ROI(3), ...
RoI{i}.ROI(2) - RoI{i}.ROI(1), RoI{i}.ROI(4) - RoI{i}.ROI(3)];
XY1 = [ RoI{i}.X{1}, RoI{i}.Y{1} ];
XY2 = [ RoI{i}.X{2}, RoI{i}.Y{2} ];
sigma1 = [ RoI{i}.X_STD{1}, RoI{i}.Y_STD{1} ];
sigma2 = [ RoI{i}.X_STD{2}, RoI{i}.Y_STD{2} ];
base_file_pre = sprintf('%s_ROI%d_pre', basefile, i);
if obj.HSET
base_file_post = sprintf('%s_ROI%d_post', basefile, i);
base1 = sprintf('%s_L1_ROI%d', base, i);
base2 = sprintf('%s_L2_ROI%d', base, i);
end
ROIs_pre{i}.ROI = RoI{i}.ROI;
ROIs_pre{i}.XY1 = XY1;
ROIs_pre{i}.XY2 = XY2;
results_pccA = obj.pair_correlation(obj.Hist_bin_size, base_file_pre, ...
XY1, XY2)
if obj.Veatch
results_VpccA = obj.pair_correlation_Veatch(XY1, XY2, ...
obj.Hist_bin_size, base_file_pre, 'cross');
end
if obj.HSET
[XY1_SR, sigma1_SR, combined1] = ...
SRc.clusterSR(XY1, sigma1, Sigma_Reg{1});
% --- BEGIN Verbose --------------------------------------------------
if obj.Verbose
SRclusterFig1 = SRc.plotSRclusters();
SRclusterFig1.Visible = 'on';
saveas(SRclusterFig1, sprintf('%s_%s.fig', base1, 'SRcluster'));
close(SRclusterFig1);
SRcollapseFig1 = SRc.plotSRcollapse();
SRcollapseFig1.Visible = 'on';
saveas(SRcollapseFig1, sprintf('%s_%s.fig', base1, 'SRcollapse'));
close(SRcollapseFig1);
[results1, analysisFigs1] = SRc.analyzeSRclusters();
for j = 1 : length(analysisFigs1)
analysisFigs1{j}.Visible = 'on';
saveas(analysisFigs1{j}, ...
sprintf('%s_%s%1d.fig', base1, 'SR', j));
close(analysisFigs1{j});
end
end
% --- END Verbose ----------------------------------------------------
[XY2_SR, sigma2_SR, combined2] = ...
SRc.clusterSR(XY2, sigma2, Sigma_Reg{2});
% --- BEGIN Verbose --------------------------------------------------
if obj.Verbose
SRclusterFig2 = SRc.plotSRclusters();
SRclusterFig2.Visible = 'on';
saveas(SRclusterFig2, sprintf('%s_%s.fig', base2, 'SRcluster'));
close(SRclusterFig2);
SRcollapseFig2 = SRc.plotSRcollapse();
SRcollapseFig2.Visible = 'on';
saveas(SRcollapseFig2, sprintf('%s_%s.fig', base2, 'SRcollapse'));
close(SRcollapseFig2);
[results2, analysisFigs2] = SRc.analyzeSRclusters();
for j = 1 : length(analysisFigs2)
analysisFigs2{j}.Visible = 'on';
saveas(analysisFigs2{j}, ...
sprintf('%s_%s%1d.fig', base2, 'SR', j));
close(analysisFigs2{j});
end
end
% --- END Verbose ----------------------------------------------------
ROIs_post{i}.ROI = RoI{i}.ROI;
ROIs_post{i}.XY1 = XY1_SR;
ROIs_post{i}.XY2 = XY2_SR;
% --- BEGIN Verbose --------------------------------------------------
if obj.Verbose
save(sprintf('%s_ROI%d_XYsigma.mat', base, i), ...
'XY1', 'sigma1', 'XY2', 'sigma2', ...
'XY1_SR', 'sigma1_SR', 'XY2_SR', 'sigma2_SR');
end
% --- END Verbose ----------------------------------------------------
results_pccB = obj.pair_correlation(obj.Hist_bin_size, ...
base_file_post, XY1_SR, XY2_SR)
if obj.Veatch
results_VpccB = obj.pair_correlation_Veatch(XY1_SR, XY2_SR, ...
obj.Hist_bin_size, base_file_post, 'cross');
end
figure();
hold on
plot(XY1_SR(:, 1), XY1_SR(:, 2), 'k.', 'MarkerSize', 10);
plot(XY2_SR(:, 1), XY2_SR(:, 2), 'b.', 'MarkerSize', 10);
title(sprintf('Collapsed Two Color Plot ROI %d', i));
xlabel('x (nm)');
ylabel('y (nm)');
hold off
if ~isempty(obj.Fig_ext)
print(sprintf('%s_ROI%d_collapsed.%s', base, i, obj.Fig_ext), ...
['-d', obj.Fig_ext]);
else
print(sprintf('%s_ROI%d_collapsed.pdf', base, i), '-dpdf');
end
close(gcf);
% --- BEGIN Verbose --------------------------------------------------
if obj.Verbose
if obj.Veatch
save(sprintf('%s_ROI%d_results.mat', base, i), ...
'results_pccA', 'results_VpccA', ...
'results_pccB', 'results_VpccB', ...
'results1', 'results2', 'obj', 'SRc');
else
save(sprintf('%s_ROI%d_results.mat', base, i), ...
'results_pccA', 'results_pccB', ...
'results1', 'results2', 'obj', 'SRc');
end
end
% --- END Verbose ----------------------------------------------------
end
end
if obj.ROIs
fprintf('ROI combined\n\n');
base_pre = sprintf('%s_pre', basefile);
resultsRC_pccA = ...
obj.pair_correlation_ROIcombined(obj.Hist_bin_size, base_pre, ...
2, n_ROIs, ROIs_pre)
if obj.HSET
base_post = sprintf('%s_post', basefile);
resultsRC_pccB = ...
obj.pair_correlation_ROIcombined(obj.Hist_bin_size, base_post, ...
2, n_ROIs, ROIs_post)
save(sprintf('%s_results.mat', base), ...
'resultsRC_pccA', 'resultsRC_pccB');
else
save(sprintf('%s_results.mat', base), 'resultsRC_pccA');
end
end
end
% -----------------------------------------------------------------------------
function PAC_SR(obj, SRDR1, basefile)
% Perform pair correlation before and after H-SET collapse on the given input
% SRD.Results dataset.
%
% Auto-correlation is performed.
%
% Use the mouse to select ROIs (regions of interest):
% left click chooses the center of a fixed size (x_size x y_size) region
% right click chooses an adjustable rectangular size region
% key press:
% backspace or delete deletes the previous region
% anything else terminates selection
% Results directory needs to exist beforehand.
base = fullfile(obj.Results, basefile);
% ==========================================================================
if obj.HSET
SRc = SRcluster();
SRc.Algorithm = obj.Algorithm;
SRc.E = obj.E;
SRc.minPts = obj.MinPts;
SRc.PlotFigures = false;
SRc.ShrinkFactor = obj.ShrinkFactor;
end
% --------------------------------------------------------------------------
obj.Lines = true;
% ==========================================================================
RT = ROITools();
if obj.ROIs
% Choose some ROIs to analyze the data over.
if ~isempty(obj.ROI)
RT.ROI_sizes = [obj.ROI(3), obj.ROI(4)];
elseif ~isempty(obj.ROI_size)
RT.ROI_sizes = [obj.ROI_size, obj.ROI_size];
else
error('ROI sizes not defined!');
end
RT.pixel2nm = obj.Pixel2nm;
file_display = regexprep(basefile, '_', '\\_');
[n_ROIs, RoI, Sigma_Reg] = RT.getROI({SRDR1}, file_display);
saveas(gcf, sprintf('%s_ROIs.fig', base));
if ~isempty(obj.Fig_ext)
print(sprintf('%s_ROIs.%s', base, obj.Fig_ext), ['-d', obj.Fig_ext]);
end
close
else
% Analyze the data over the entire region.
n_ROIs = 1;
Sigma_Reg{1} = [10, 10];
[XY1, XY_STD1, Sigma_Reg1] = RT.import_XY(SRDR1, obj.Pixel2nm);
RoI{1}.X{1} = XY1(:, 1);
RoI{1}.Y{1} = XY1(:, 2);
if ~isempty(XY_STD1)
RoI{1}.X_STD{1} = XY_STD1(:, 1);
RoI{1}.Y_STD{1} = XY_STD1(:, 2);
else
RoI{1}.X_STD{1} = zeros(size(XY1, 1), 1);
RoI{1}.Y_STD{1} = zeros(size(XY1, 1), 1);
end
RoI{1}.ROI = [min(RoI{1}.X{1}), max(RoI{1}.X{1}), ...
min(RoI{1}.Y{1}), max(RoI{1}.Y{1})];
end
save(sprintf('%s_ROIs.mat', base), 'n_ROIs', 'RoI', 'Sigma_Reg');
% Perform H-SET (optionally) and pair correlation over each ROI in turn,
% combining the results at the end.
ROIs_pre = cell(n_ROIs, 1);
ROIs_post = cell(n_ROIs, 1);
for i = 1 : n_ROIs
fprintf('ROI %d\n\n', i);
obj.ROI = [RoI{i}.ROI(1), RoI{i}.ROI(3), ...
RoI{i}.ROI(2) - RoI{i}.ROI(1), RoI{i}.ROI(4) - RoI{i}.ROI(3)];
XY1 = [ RoI{i}.X{1}, RoI{i}.Y{1} ];
sigma1 = [ RoI{i}.X_STD{1}, RoI{i}.Y_STD{1} ];
base_file_pre1 = sprintf('%s_ROI%d_pre', basefile, i);
if obj.HSET
base_file_post = sprintf('%s_ROI%d_post', basefile, i);
base_file_post1 = sprintf('%s_ROI%d_post', basefile, i);
base1 = sprintf('%s_ROI%d', base, i);
end
ROIs_pre{i}.ROI = RoI{i}.ROI;
ROIs_pre{i}.XY1 = XY1;
results_pac1A = obj.pair_correlation(obj.Hist_bin_size, ...
base_file_pre1, XY1)
if obj.Veatch
results_Vpac1A = obj.pair_correlation_Veatch(XY1, [], ...
obj.Hist_bin_size, base_file_pre1, 'auto');
end
if obj.HSET
[XY1_SR, sigma1_SR, combined1] = ...
SRc.clusterSR(XY1, sigma1, Sigma_Reg{1});
% --- BEGIN Verbose --------------------------------------------------
if obj.Verbose
SRclusterFig1 = SRc.plotSRclusters();
SRclusterFig1.Visible = 'on';
saveas(SRclusterFig1, sprintf('%s_%s.fig', base1, 'SRcluster'));
close(SRclusterFig1);
SRcollapseFig1 = SRc.plotSRcollapse();
SRcollapseFig1.Visible = 'on';
saveas(SRcollapseFig1, sprintf('%s_%s.fig', base1, 'SRcollapse'));
close(SRcollapseFig1);
[results1, analysisFigs1] = SRc.analyzeSRclusters();
for j = 1 : length(analysisFigs1)
analysisFigs1{j}.Visible = 'on';
saveas(analysisFigs1{j}, ...
sprintf('%s_%s%1d.fig', base1, 'SR', j));
close(analysisFigs1{j});
end
end
% --- END Verbose ----------------------------------------------------
ROIs_post{i}.ROI = RoI{i}.ROI;
ROIs_post{i}.XY1 = XY1_SR;
% --- BEGIN Verbose --------------------------------------------------
if obj.Verbose
save(sprintf('%s_ROI%d_XYsigma.mat', base, i), ...
'XY1', 'sigma1', 'XY1_SR', 'sigma1_SR');
end
% --- END Verbose ----------------------------------------------------
results_pac1B = obj.pair_correlation(obj.Hist_bin_size, ...
base_file_post1, XY1_SR)
if obj.Veatch
results_Vpac1B = ...
obj.pair_correlation_Veatch(XY1_SR, [], obj.Hist_bin_size, ...
base_file_post1, 'auto');
end
figure();
hold on
plot(XY1_SR(:, 1), XY1_SR(:, 2), 'k.', 'MarkerSize', 10);
title(sprintf('Collapsed Two Color Plot ROI %d', i));
xlabel('x (nm)');
ylabel('y (nm)');
hold off
if ~isempty(obj.Fig_ext)
print(sprintf('%s_ROI%d_collapsed.%s', base, i, obj.Fig_ext), ...
['-d', obj.Fig_ext]);
else
print(sprintf('%s_ROI%d_collapsed.pdf', base, i), '-dpdf');
end
close(gcf);
% --- BEGIN Verbose --------------------------------------------------
if obj.Verbose
if obj.Veatch
save(sprintf('%s_ROI%d_results.mat', base, i), ...
'results_pac1A', 'results_Vpac1A', ...
'results_pac1B', 'results_Vpac1B', ...
'results1', 'obj', 'SRc');
else
save(sprintf('%s_ROI%d_results.mat', base, i), ...
'results1', 'obj', 'SRc');
end
end
% --- END Verbose ----------------------------------------------------
end
end
if obj.ROIs
fprintf('ROI combined\n\n');
base_pre = sprintf('%s_pre', basefile);
resultsRC_pacA = ...
obj.pair_correlation_ROIcombined(obj.Hist_bin_size, base_pre, ...
1, n_ROIs, ROIs_pre)
if obj.HSET
base_post = sprintf('%s_post', basefile);
resultsRC_pacB = ...
obj.pair_correlation_ROIcombined(obj.Hist_bin_size, base_post, ...
1, n_ROIs, ROIs_post)
save(sprintf('%s_results.mat', base), ...
'resultsRC_pacA', 'resultsRC_pacB');
else
save(sprintf('%s_results.mat', base), 'resultsRC_pacA');
end
end
end
% =============================================================================
function results = pair_correlation_ROIcombined(obj, hist_bin_size, ...
base_name, n_labels, ...
n_ROIs, ROIs, label_num)
% Combine ROIs while performing pair correlation.
% Modified from code originally written by Carolyn Pehlke.
%
% hist_bin_size histogram bin size sometims known as pixel size (nm)
% base_name descriptive name for the results files
% n_labels number of different labeled particles:
% 1 -> auto-correlation, 2 -> cross_correlation
% n_ROIs number of ROIs to combine
% ROIs contains (x, y) coordinates of the two datasets (nm)
% label_num (optional) if n_labels = 1, then whether XY1 or XY2 should be
% used for the coordinates contained in ROIs
if n_labels == 2
corr_type = 'C'; % cross-correlation
else
corr_type = 'A'; % auto-correlation
end
% Add in the DERIVESTsuite path.
%addpath('DERIVESTsuite', filesep);
ROI_size = zeros(n_ROIs, 1);
IM1 = cell(n_ROIs, 1);
if corr_type == 'C'
IM2 = cell(n_ROIs, 1);
end
for j = 1 : n_ROIs
x_min = ROIs{j}.ROI(1);
x_max = ROIs{j}.ROI(2);
y_min = ROIs{j}.ROI(3);
y_max = ROIs{j}.ROI(4);
ROI_size(j) = min(x_max - x_min, y_max - y_min);
% Compute the number of pixels in x and y.
imszX = round((x_max - x_min) / hist_bin_size);
imszY = round((y_max - y_min) / hist_bin_size);
% Create a blank image.
im1 = zeros(imszX, imszY);
if corr_type == 'C'
im2 = zeros(imszX, imszY);
end
% Convert (x, y) coordinates into pixel units.
if ~exist('label_num', 'var')
label_num = 1;
end
if label_num == 1
x1 = round((ROIs{j}.XY1(:, 1) - x_min) / hist_bin_size) + 1;
y1 = round((ROIs{j}.XY1(:, 2) - y_min) / hist_bin_size) + 1;
elseif label_num == 2
x1 = round((ROIs{j}.XY2(:, 1) - x_min) / hist_bin_size) + 1;
y1 = round((ROIs{j}.XY2(:, 2) - y_min) / hist_bin_size) + 1;
else
error('Invalid label_num: %d!', label_num);
end
if corr_type == 'C'
x2 = round((ROIs{j}.XY2(:, 1) - x_min) / hist_bin_size) + 1;
y2 = round((ROIs{j}.XY2(:, 2) - y_min) / hist_bin_size) + 1;
end
% Get the pixels within the image size.
mask1 = (x1 > 0) & (y1 > 0) & (x1 <= imszX) & (y1 <= imszY);
x1 = x1(mask1);
y1 = y1(mask1);
if corr_type == 'C'
mask2 = (x2 > 0) & (y2 > 0) & (x2 <= imszX) & (y2 <= imszY);
x2 = x2(mask2);
y2 = y2(mask2);
end
% Make a histogram image.
for i = 1 : size(x1, 1)
im1(x1(i), y1(i)) = im1(x1(i), y1(i)) + 1;
end
if corr_type == 'C'
for i = 1 : size(x2, 1)
im2(x2(i), y2(i)) = im2(x2(i), y2(i)) + 1;
end
end
IM1{j} = im1;
if corr_type == 'C'
IM2{j} = im2;
end
end
% Establish rmax as half the size of the ROI in pixels.
rmax = round(min(ROI_size) / (2 * hist_bin_size));
if corr_type == 'C'
% Pair crosscorrelation using Veatch method.
[G, r, g, dg, rmax] = PairCorr.get_corr(n_ROIs, rmax, IM1, IM2);
else
% Pair autocorrelation using Veatch method.
[G, r, g, dg, rmax] = PairCorr.get_corr(n_ROIs, rmax, IM1);
end
% Convert back to nm
r_nm = r * hist_bin_size;
if corr_type == 'C'
i1 = 0; i2 = 0; % intensity sums
p1 = 0; p2 = 0; % pixel sums
for i = 1 : n_ROIs
i1 = i1 + sum(sum(IM1{i}));
i2 = i2 + sum(sum(IM2{i}));
p1 = p1 + prod(size(IM1{i}));
p2 = p2 + prod(size(IM2{i}));
end
rho1 = i1 / p1;
rho2 = i2 / p2;
%rho1 = mean(mean(im1));
%rho2 = mean(mean(im2));
rho = sqrt(rho1 * rho2);
%paircorr = [{[im1, im2]}, {[r', g', dg']}, {rho}, {G}];
else
i1 = 0; % intensity sum
p1 = 0; % pixel sum
for i = 1 : n_ROIs
i1 = i1 + sum(sum(IM1{i}));
p1 = p1 + prod(size(IM1{i}));
end
rho = i1 / p1;
%rho = mean(mean(im1));
%paircorr = [{im1}, {[r', g', dg']}, {rho}, {G}];
end
[estimates, errors, model] = PairCorr.PC_GaussFit(r', g', rmax, rho);
% PairCorr.PC_GaussFit(paircorr{2}(:,1), paircorr{2}(:,2), rmax, ...
% paircorr{3});
estimates = abs(estimates);
if ~isempty(obj.Fig_ext)
figure('Visible', 'off');
else
figure;
end
axes(obj.Font_props{:});
hold on
if corr_type == 'C'