-
Notifications
You must be signed in to change notification settings - Fork 0
/
nucleus.py
executable file
·266 lines (232 loc) · 10 KB
/
nucleus.py
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
from __future__ import division
import re
import pyimod
import numpy as np
from .utils import transform_coords, is_outlier
from .readfile import scalar_field, surface
def get_adjacent_vertices(seed, indices):
"""
Takes a vertex index, or seed, as input, then finds all triangles that
contain this seed vertex. The vertex indices of all these neighboring
triangles are then returned.
"""
# Find all triangles that contain the seed vertex
tris_with_seed = np.where(indices == seed)[0]
# Find all vertices of these triangles. Keep only the unique ones
idx = []
for i in tris_with_seed:
idx = np.hstack([idx, indices[i]])
idx = np.unique(idx).tolist()
idx = [int(x) for x in idx]
idx.remove(seed)
return idx, tris_with_seed
def get_triangle_area(a, b, c):
"""
Computes the area of a triangle from three arbitrary 3D points.
"""
ab = b - a
ac = c - a
ab_norm = np.linalg.norm(ab)
ac_norm = np.linalg.norm(ac)
theta = np.arccos(np.dot(ab, ac) / (ab_norm * ac_norm))
A = 0.5 * ab_norm * ac_norm * np.sin(theta)
return A
def get_cluster_area(idx, indices, vertices):
"""
Computes the surface area of a given list of triangles.
"""
Acluster = 0
for i in idx:
vert1 = vertices[indices[i][0] - 1,:]
vert2 = vertices[indices[i][1] - 1,:]
vert3 = vertices[indices[i][2] - 1,:]
Ai = get_triangle_area(vert1, vert2, vert3)
Acluster += Ai
return Acluster
def willmore_energy(fname_mean, fname_gauss, fname_surf):
"""
Computes the Willmore energy of a surface. The Willmore energy is a unit-
less measure of how much a given surface deviates from a perfect sphere.
The Willmore energy of a perfect sphere is zero.
(https://en.wikipedia.org/wiki/Willmore_energy)
"""
# Import data from Amira files
meanCurv = scalar_field(fname_mean)
gaussCurv = scalar_field(fname_gauss)
vertices, indices = surface(fname_surf)
# Check the Mean curvature and Gaussian curvature scalar fields for extreme
# outliers, i.e. values for which the z-score is > 100.
meanCurvOutl = is_outlier(meanCurv, thresh = 100)
meanCurvOutl = [i for i, x in enumerate(meanCurvOutl) if x]
gaussCurvOutl = is_outlier(gaussCurv, thresh = 100)
gaussCurvOutl = [i for i, x in enumerate(gaussCurvOutl) if x]
# Get mean values of Mean and Gaussian Curvature
meanCurvMean = np.mean(meanCurv)
gaussCurvMean = np.mean(gaussCurv)
# Compute the integrals
nTri = indices.shape[0]
integral_mean = 0
integral_gauss = 0
for i in range(nTri):
# Get the area of the corresponding triangle
idx = indices[i,:]
vert1 = vertices[idx[0] - 1,:]
vert2 = vertices[idx[1] - 1,:]
vert3 = vertices[idx[2] - 1,:]
area = get_triangle_area(vert1, vert2, vert3)
# Compute the mean curvature product for the i-th triangle. First, check
# if the value for the i-th triangle is an outlier. If so, replace it
# with the average value of all surrounding triangles.
if i in meanCurvOutl:
_, tris1 = get_adjacent_vertices(idx[0], indices)
_, tris2 = get_adjacent_vertices(idx[1], indices)
_, tris3 = get_adjacent_vertices(idx[2], indices)
tris = np.concatenate([tris1, tris2, tris3])
tris = np.unique(tris).tolist()
tris.remove(i)
for j in range(len(tris)-1, -1, -1):
if tris[j] in meanCurvOutl:
tris.remove(tris[j])
vals = [meanCurv[x] for x in tris]
# If ALL surrounding triangles are outliers, replace the triangle's
# mean curvature with the mean of the whole surface.
if not vals:
meanCurv[i] = meanCurvMean
else:
meanCurv[i] = np.mean(vals)
integral_mean += (meanCurv[i] ** 2) * area
# Do the same with the Gaussian curvature
if i in gaussCurvOutl:
_, tris1 = get_adjacent_vertices(idx[0], indices)
_, tris2 = get_adjacent_vertices(idx[1], indices)
_, tris3 = get_adjacent_vertices(idx[2], indices)
tris = np.concatenate([tris1, tris2, tris3])
tris = np.unique(tris).tolist()
tris.remove(i)
for j in range(len(tris)-1, -1, -1):
if tris[j] in gaussCurvOutl:
tris.remove(tris[j])
vals = [gaussCurv[x] for x in tris]
# If ALL surrounding triangles are outliers, replace the triangle's
# Gaussian curvature with the mean of the whole surface.
if not vals:
gaussCurv[i] = gaussCurvMean
else:
gaussCurv[i] = np.mean(vals)
integral_gauss += gaussCurv[i] * area
willmore_energy = integral_mean - integral_gauss
return willmore_energy[0]
def quantify_nuclear_folds(model, vertices, indices, shapeIndex, grad):
"""
Computes metrics relating to nuclear folding.
"""
coordList = []
nTri = indices.shape[0]
nVert = vertices.shape[0]
idx_keep = []
# Find all vertices with Shape Index > 0
vert_idx_pos = np.where(shapeIndex > 0)[0].tolist()
vert_idx_pos = [int(x) + 1 for x in vert_idx_pos]
nCluster = 0
saClusterSum = 0
while vert_idx_pos: # Set the seed vertex as the first entry
vert_seed = vert_idx_pos[0]
vert_check = [vert_seed]
vert_keep = []
while vert_check:
# Find all vertices in triangles adjacent to the seed vertex
vert_adj, _ = get_adjacent_vertices(vert_check[0], indices)
vert_keep.append(vert_check[0])
# If none of the adjacent vertices have a positive Shape Index,
# then the seed vertex is an island. Break the loop and remove it.
# If there is at least one adjacent vertex with a positive Shape
# Index, add the seed vertex to the keep list and remove it from
# the seed position of the check list.
if not sum([x in vert_idx_pos for x in vert_adj]):
break
else:
vert_check = vert_check[1:]
# Append all adjacent vertices with positive Shape Indices to the
# check list.
for i in vert_adj:
if ((i in vert_idx_pos) and (i not in vert_check) and
(i not in vert_keep)):
vert_check.append(i)
for i in vert_keep:
vert_idx_pos.remove(i)
# Process the relevantly sized clusters
if len(vert_keep) > 20:
nCluster+=1
nVertCluster = len(vert_keep)
# Get a list of Shape Index values from the vertices
K = 0
sCluster = np.zeros([nVertCluster, 1])
for i in vert_keep:
sCluster[K] = shapeIndex[i - 1]
K+=1
# Get the triangles from vertices
tri_keep = []
for i in vert_keep:
tris_with_vert = np.where(indices == i)[0]
for j in tris_with_vert:
idx = indices[j]
if (sum([x in vert_keep for x in idx]) == 3
and j not in tri_keep):
tri_keep.append(j)
nTriCluster = len(tri_keep)
# Get the gradients from the triangles. Take the magnitude of the
# Shape Index gradient at each triangle face.
magMinCluster = 1
for i in tri_keep:
gradi = grad[i,:]
magClusteri = np.linalg.norm(gradi)
if magClusteri < magMinCluster:
magMinCluster = magClusteri
imc = indices[i,:]
# Calculate the coordinates of the triangle centroid
coordx = (vertices[imc[0]-1,0] + vertices[imc[1]-1,0] +
vertices[imc[2]-1,0]) / 3
coordy = (vertices[imc[0]-1,1] + vertices[imc[1]-1,1] +
vertices[imc[2]-1,1]) / 3
coordz = (vertices[imc[0]-1,2] + vertices[imc[1]-1,2] +
vertices[imc[2]-1,2]) / 3
# Scale back to IMOD coordinate system
coordtrx = transform_coords(model, coordx, coordy, coordz)
# Compute cluster surface area
saCluster = get_cluster_area(tri_keep, indices, vertices)
saCluster = saCluster / (10000 ** 2)
saClusterSum += saCluster
# Print metrics
print "Cluster {0}".format(nCluster)
print "==========="
print "# Vertices : {0}".format(nVertCluster)
print "# Triangles: {0}".format(nTriCluster)
print "Surface Area (um2): {0}".format(saCluster)
print "S_min : {0}".format(np.amin(sCluster))
print "S_max : {0}".format(np.amax(sCluster))
print "S_median: {0}".format(np.median(sCluster))
print "S_mean : {0}".format(np.mean(sCluster))
print "S_std : {0}".format(np.std(sCluster))
print "Min. gradient magnitude: {0}".format(magMinCluster)
print "Coords: {0}, {1}, {2}".format(coordtrx[0], coordtrx[1],
coordtrx[2])
print ""
coordList.append(coordtrx[0])
coordList.append(coordtrx[1])
coordList.append(int(coordtrx[2]))
# Create a new object in the input model that has scattered points
# corresponding to the location of minimum gradient magnitude in each
# positive shape index cluster.
nFolds = int(len(coordList) / 3)
model.addObject()
model.Objects[-1].setObjectType('scattered')
model.Objects[-1].setSymbolType('circle')
model.Objects[-1].setSymbolSize(6)
model.Objects[-1].setSymbolFillOn()
model.Objects[-1].setMeshOn()
model.Objects[-1].addContour()
model.Objects[-1].Contours[-1].points = coordList
model.Objects[-1].Contours[-1].nPoints = nFolds
model.Objects[-1].Views[-1].pdrawsize = 8
model.Objects[-1].Views[-1].quality = 4
return nFolds, saClusterSum