-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXTFISH_Regenerate_Stats.m
154 lines (135 loc) · 5.26 KB
/
XTFISH_Regenerate_Stats.m
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
% XT2DArea_regenerate for Imaris 7.6.4
%
%
%
% Installation:
%
% - Copy this file into the XTensions folder in the Imaris installation directory
% - You will find this function in the Image Processing menu
%
% <CustomTools>
% <Menu>
% <Submenu name="FISH">
% <Item name="Regenerate the stats of the FISH model" icon="Matlab" tooltip="Regenerate the stats of the FISH model">
% <Command>MatlabXT::XTFISH_Regenerate_Stats(%i)</Command>
% </Item>
% </Submenu>
% </Menu>
% </CustomTools>
%
%
% Description:
%
% This XTension regenerates the area of 2D ovary cells.
% It requires intersect_line.m and normals.m from FieldTrip project
% https://code.google.com/p/fieldtrip/
%
function XTFISH_Regenerate_Stats(aImarisApplicationID) % ID corresponds to Imaris instance
if isa(aImarisApplicationID, 'Imaris.IApplicationPrxHelper')
vImarisApplication = aImarisApplicationID;
else
% Connect to Imaris interface
if exist('ImarisLib','class') == 0
javaaddpath ImarisLib.jar
end
vImarisLib = ImarisLib;
if ischar(aImarisApplicationID)
aImarisApplicationID = round(str2double(aImarisApplicationID));
end
vImarisApplication = vImarisLib.GetApplication(aImarisApplicationID);
end
%DataSet
aDataSet = vImarisApplication.GetDataSet;
% Size variables
sizeC = aDataSet.GetSizeC;
%% Data access
aSurpassScene = vImarisApplication.GetSurpassScene;
nb_objects = aSurpassScene.GetNumberOfChildren;
surfaces = zeros(nb_objects,1);
spots = zeros(nb_objects,1);
nucleus = -1;
for i=0:nb_objects-1
if(vImarisApplication.GetFactory.IsSurfaces(aSurpassScene.GetChild(i)))
surfaces(i+1) = 1;
else
if(vImarisApplication.GetFactory.IsSpots(aSurpassScene.GetChild(i)))
spots(i+1) = 1;
end
end
end
surfidx = find(surfaces==1)-1;
spotsidx = find(spots==1)-1;
for i=1:size(surfidx)
if(strcmp(char(aSurpassScene.GetChild(surfidx(i)).GetName),'Nucleus')==1)
nucleus = surfidx(i);
end
end
%% Generate stats if nucleus was detected
if nucleus >= 0
aNucleus = vImarisApplication.GetFactory.ToSurfaces(aSurpassScene.GetChild(nucleus));
n = aNucleus.GetNumberOfSurfaces;
nspots = size(spotsidx,1);
if nspots > 0
aSpots = javaArray('Imaris.ISpotsPrxHelper', nspots);
for i=1:size(spotsidx)
aSpots(i) = vImarisApplication.GetFactory.ToSpots(aSurpassScene.GetChild(spotsidx(i)));
end
for i=1:n
CMN = aNucleus.GetCenterOfMass(i-1);
vertices = aNucleus.GetVertices(i-1);
triangles = aNucleus.GetTriangles(i-1);
faces = triangles + 1; % Index correction for Matlab
for j=1:nspots
positions = aSpots(j).GetPositionsXYZ;
stats = aSpots(j).GetStatistics;
aNames = cell(stats.mNames);
aUnits = cell(stats.mUnits);
aUnit = char(aUnits(find(ismember(aNames, 'Position')==1, 1)));
aFactornames = cell(stats.mFactorNames);
aFactors = transpose(cell(stats.mFactors));
values = stats.mValues;
ids = stats.mIds;
ch = strcmp(aFactornames,'Channel');
I = strcmp(aNames,'Intensity Center') & strcmp(aFactors(:,ch),num2str(sizeC));
intensities = values(I);
corrids = ids(I);
spotcenters = positions(corrids(intensities == i)+1,:);
% Compute border distance from nucleus center (in spot direction)
ns = size(spotcenters,1);
if ns > 0
ids = corrids(intensities == i);
dn = zeros(ns,1);
ds = zeros(ns,1);
names_dn = cell(1,ns);
names_ds = cell(1,ns);
units = cell(1,ns);
factors = cell(4,ns);
[names_dn{1:ns}] = deal('Distance from nucleus centroid to border');
[names_ds{1:ns}] = deal('Distance from spot to nucleus centroid');
[units{1:ns}] = deal(aUnit);
[factors{1,1:ns}] = deal('Spot');
[factors{2,1:ns}] = deal('');
[factors{3,1:ns}] = deal('');
[factors{4,1:ns}] = deal('');
% For each spot in the selected nucleus
for k=1:ns;
% Intersections computation
CMS = spotcenters(k,:);
[inters pos] = intersect_line(vertices, faces, CMS, CMN);
% Closest intersection on the outer layer of the nucleus
[~, idx] = min(abs(pos(pos<0)));
points = inters(pos<0,:);
point = points(idx,:);
% Distances computation
dn(k) = norm(point - CMN);
ds(k) = norm(CMS - CMN);
end
% Save statistics
aSpots(j).AddStatistics(names_dn,dn,units,factors,aFactornames,ids);
aSpots(j).AddStatistics(names_ds,ds,units,factors,aFactornames,ids);
end
end
end
end
end
end