-
Notifications
You must be signed in to change notification settings - Fork 163
/
utils.py
421 lines (371 loc) · 12.4 KB
/
utils.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
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
from os.path import abspath, dirname, join
import numpy as np
import scipy.sparse as sp
FILE_DIR = dirname(abspath(__file__))
DATA_DIR = join(FILE_DIR, "data")
MACOSKO_COLORS = {
"Amacrine cells": "#A5C93D",
"Astrocytes": "#8B006B",
"Bipolar cells": "#2000D7",
"Cones": "#538CBA",
"Fibroblasts": "#8B006B",
"Horizontal cells": "#B33B19",
"Microglia": "#8B006B",
"Muller glia": "#8B006B",
"Pericytes": "#8B006B",
"Retinal ganglion cells": "#C38A1F",
"Rods": "#538CBA",
"Vascular endothelium": "#8B006B",
}
ZEISEL_COLORS = {
"Astroependymal cells": "#d7abd4",
"Cerebellum neurons": "#2d74bf",
"Cholinergic, monoaminergic and peptidergic neurons": "#9e3d1b",
"Di- and mesencephalon neurons": "#3b1b59",
"Enteric neurons": "#1b5d2f",
"Hindbrain neurons": "#51bc4c",
"Immature neural": "#ffcb9a",
"Immune cells": "#768281",
"Neural crest-like glia": "#a0daaa",
"Oligodendrocytes": "#8c7d2b",
"Peripheral sensory neurons": "#98cc41",
"Spinal cord neurons": "#c52d94",
"Sympathetic neurons": "#11337d",
"Telencephalon interneurons": "#ff9f2b",
"Telencephalon projecting neurons": "#fea7c1",
"Vascular cells": "#3d672d",
}
MOUSE_10X_COLORS = {
0: "#FFFF00",
1: "#1CE6FF",
2: "#FF34FF",
3: "#FF4A46",
4: "#008941",
5: "#006FA6",
6: "#A30059",
7: "#FFDBE5",
8: "#7A4900",
9: "#0000A6",
10: "#63FFAC",
11: "#B79762",
12: "#004D43",
13: "#8FB0FF",
14: "#997D87",
15: "#5A0007",
16: "#809693",
17: "#FEFFE6",
18: "#1B4400",
19: "#4FC601",
20: "#3B5DFF",
21: "#4A3B53",
22: "#FF2F80",
23: "#61615A",
24: "#BA0900",
25: "#6B7900",
26: "#00C2A0",
27: "#FFAA92",
28: "#FF90C9",
29: "#B903AA",
30: "#D16100",
31: "#DDEFFF",
32: "#000035",
33: "#7B4F4B",
34: "#A1C299",
35: "#300018",
36: "#0AA6D8",
37: "#013349",
38: "#00846F",
}
def calculate_cpm(x, axis=1):
"""Calculate counts-per-million on data where the rows are genes.
Parameters
----------
x : array_like
axis : int
Axis accross which to compute CPM. 0 for genes being in rows and 1 for
genes in columns.
"""
normalization = np.sum(x, axis=axis)
# On sparse matrices, the sum will be 2d. We want a 1d array
normalization = np.squeeze(np.asarray(normalization))
# Straight up division is not an option since this will form a full dense
# matrix if `x` is sparse. Divison can be expressed as the dot product with
# a reciprocal diagonal matrix
normalization = sp.diags(1 / normalization, offsets=0)
if axis == 0:
cpm_counts = np.dot(x, normalization)
elif axis == 1:
cpm_counts = np.dot(normalization, x)
return cpm_counts * 1e6
def log_normalize(data):
"""Perform log transform log(x + 1).
Parameters
----------
data : array_like
"""
if sp.issparse(data):
data = data.copy()
data.data = np.log2(data.data + 1)
return data
return np.log2(data.astype(np.float64) + 1)
def pca(x, n_components=50):
if sp.issparse(x):
x = x.toarray()
U, S, V = np.linalg.svd(x, full_matrices=False)
U[:, np.sum(V, axis=1) < 0] *= -1
x_reduced = np.dot(U, np.diag(S))
x_reduced = x_reduced[:, np.argsort(S)[::-1]][:, :n_components]
return x_reduced
def select_genes(
data,
threshold=0,
atleast=10,
yoffset=0.02,
xoffset=5,
decay=1,
n=None,
plot=True,
markers=None,
genes=None,
figsize=(6, 3.5),
markeroffsets=None,
labelsize=10,
alpha=1,
):
if sp.issparse(data):
zeroRate = 1 - np.squeeze(np.array((data > threshold).mean(axis=0)))
A = data.multiply(data > threshold)
A.data = np.log2(A.data)
meanExpr = np.zeros_like(zeroRate) * np.nan
detected = zeroRate < 1
meanExpr[detected] = np.squeeze(np.array(A[:, detected].mean(axis=0))) / (
1 - zeroRate[detected]
)
else:
zeroRate = 1 - np.mean(data > threshold, axis=0)
meanExpr = np.zeros_like(zeroRate) * np.nan
detected = zeroRate < 1
meanExpr[detected] = np.nanmean(
np.where(data[:, detected] > threshold, np.log2(data[:, detected]), np.nan),
axis=0,
)
lowDetection = np.array(np.sum(data > threshold, axis=0)).squeeze() < atleast
# lowDetection = (1 - zeroRate) * data.shape[0] < atleast - .00001
zeroRate[lowDetection] = np.nan
meanExpr[lowDetection] = np.nan
if n is not None:
up = 10
low = 0
for t in range(100):
nonan = ~np.isnan(zeroRate)
selected = np.zeros_like(zeroRate).astype(bool)
selected[nonan] = (
zeroRate[nonan] > np.exp(-decay * (meanExpr[nonan] - xoffset)) + yoffset
)
if np.sum(selected) == n:
break
elif np.sum(selected) < n:
up = xoffset
xoffset = (xoffset + low) / 2
else:
low = xoffset
xoffset = (xoffset + up) / 2
print("Chosen offset: {:.2f}".format(xoffset))
else:
nonan = ~np.isnan(zeroRate)
selected = np.zeros_like(zeroRate).astype(bool)
selected[nonan] = (
zeroRate[nonan] > np.exp(-decay * (meanExpr[nonan] - xoffset)) + yoffset
)
if plot:
import matplotlib.pyplot as plt
if figsize is not None:
plt.figure(figsize=figsize)
plt.ylim([0, 1])
if threshold > 0:
plt.xlim([np.log2(threshold), np.ceil(np.nanmax(meanExpr))])
else:
plt.xlim([0, np.ceil(np.nanmax(meanExpr))])
x = np.arange(plt.xlim()[0], plt.xlim()[1] + 0.1, 0.1)
y = np.exp(-decay * (x - xoffset)) + yoffset
if decay == 1:
plt.text(
0.4,
0.2,
"{} genes selected\ny = exp(-x+{:.2f})+{:.2f}".format(
np.sum(selected), xoffset, yoffset
),
color="k",
fontsize=labelsize,
transform=plt.gca().transAxes,
)
else:
plt.text(
0.4,
0.2,
"{} genes selected\ny = exp(-{:.1f}*(x-{:.2f}))+{:.2f}".format(
np.sum(selected), decay, xoffset, yoffset
),
color="k",
fontsize=labelsize,
transform=plt.gca().transAxes,
)
plt.plot(x, y, linewidth=2)
xy = np.concatenate(
(
np.concatenate((x[:, None], y[:, None]), axis=1),
np.array([[plt.xlim()[1], 1]]),
)
)
t = plt.matplotlib.patches.Polygon(xy, color="r", alpha=0.2)
plt.gca().add_patch(t)
plt.scatter(meanExpr, zeroRate, s=3, alpha=alpha, rasterized=True)
if threshold == 0:
plt.xlabel("Mean log2 nonzero expression")
plt.ylabel("Frequency of zero expression")
else:
plt.xlabel("Mean log2 nonzero expression")
plt.ylabel("Frequency of near-zero expression")
plt.tight_layout()
if markers is not None and genes is not None:
if markeroffsets is None:
markeroffsets = [(0, 0) for g in markers]
for num, g in enumerate(markers):
i = np.where(genes == g)[0]
plt.scatter(meanExpr[i], zeroRate[i], s=10, color="k")
dx, dy = markeroffsets[num]
plt.text(
meanExpr[i] + dx + 0.1,
zeroRate[i] + dy,
g,
color="k",
fontsize=labelsize,
)
return selected
def plot(
x,
y,
ax=None,
title=None,
draw_legend=True,
draw_centers=False,
draw_cluster_labels=False,
colors=None,
legend_kwargs=None,
label_order=None,
**kwargs
):
import matplotlib
if ax is None:
_, ax = matplotlib.pyplot.subplots(figsize=(8, 8))
if title is not None:
ax.set_title(title)
plot_params = {"alpha": kwargs.get("alpha", 0.6), "s": kwargs.get("s", 1)}
# Create main plot
if label_order is not None:
assert all(np.isin(np.unique(y), label_order))
classes = [l for l in label_order if l in np.unique(y)]
else:
classes = np.unique(y)
if colors is None:
default_colors = matplotlib.rcParams["axes.prop_cycle"]
colors = {k: v["color"] for k, v in zip(classes, default_colors())}
point_colors = list(map(colors.get, y))
ax.scatter(x[:, 0], x[:, 1], c=point_colors, rasterized=True, **plot_params)
# Plot mediods
if draw_centers:
centers = []
for yi in classes:
mask = yi == y
centers.append(np.median(x[mask, :2], axis=0))
centers = np.array(centers)
center_colors = list(map(colors.get, classes))
ax.scatter(
centers[:, 0], centers[:, 1], c=center_colors, s=48, alpha=1, edgecolor="k"
)
# Draw mediod labels
if draw_cluster_labels:
for idx, label in enumerate(classes):
ax.text(
centers[idx, 0],
centers[idx, 1] + 2.2,
label,
fontsize=kwargs.get("fontsize", 6),
horizontalalignment="center",
)
# Hide ticks and axis
ax.set_xticks([]), ax.set_yticks([]), ax.axis("off")
if draw_legend:
legend_handles = [
matplotlib.lines.Line2D(
[],
[],
marker="s",
color="w",
markerfacecolor=colors[yi],
ms=10,
alpha=1,
linewidth=0,
label=yi,
markeredgecolor="k",
)
for yi in classes
]
legend_kwargs_ = dict(loc="center left", bbox_to_anchor=(1, 0.5), frameon=False, )
if legend_kwargs is not None:
legend_kwargs_.update(legend_kwargs)
ax.legend(handles=legend_handles, **legend_kwargs_)
def evaluate_embedding(
embedding, labels, projection_embedding=None, projection_labels=None, sample=None
):
"""Evaluate the embedding using Moran's I index.
Parameters
----------
embedding: np.ndarray
The data embedding.
labels: np.ndarray
A 1d numpy array containing the labels of each point.
projection_embedding: Optional[np.ndarray]
If this is given, the score will relate to how well the projection fits
the embedding.
projection_labels: Optional[np.ndarray]
A 1d numpy array containing the labels of each projection point.
sample: Optional[int]
If this is specified, the score will be computed on a sample of points.
Returns
-------
float
Moran's I index.
"""
has_projection = projection_embedding is not None
if projection_embedding is None:
projection_embedding = embedding
if projection_labels is not None:
raise ValueError(
"If `projection_embedding` is None then `projection_labels make no sense`"
)
projection_labels = labels
if embedding.shape[0] != labels.shape[0]:
raise ValueError("The shape of the embedding and labels don't match")
if projection_embedding.shape[0] != projection_labels.shape[0]:
raise ValueError("The shape of the reference embedding and labels don't match")
if sample is not None:
n_samples = embedding.shape[0]
sample_indices = np.random.choice(
n_samples, size=min(sample, n_samples), replace=False
)
embedding = embedding[sample_indices]
labels = labels[sample_indices]
n_samples = projection_embedding.shape[0]
sample_indices = np.random.choice(
n_samples, size=min(sample, n_samples), replace=False
)
projection_embedding = projection_embedding[sample_indices]
projection_labels = projection_labels[sample_indices]
weights = projection_labels[:, None] == labels
if not has_projection:
np.fill_diagonal(weights, 0)
mu = np.asarray(embedding.mean(axis=0)).ravel()
numerator = np.sum(weights * ((projection_embedding - mu) @ (embedding - mu).T))
denominator = np.sum((projection_embedding - mu) ** 2)
return projection_embedding.shape[0] / np.sum(weights) * numerator / denominator