/
hvg_vs_hs.py
159 lines (118 loc) · 3.84 KB
/
hvg_vs_hs.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
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import loompy
from bio_utils.plots import hover_plot
from matplotlib.colors import LinearSegmentedColormap
import seaborn as sns
plt.rcParams['svg.fonttype'] = 'none'
# %% Load results
hs = pd.read_table("../../CD4_w_protein/hotspot/hotspot_hvg.txt", index_col=0)
hvg = pd.read_table("../../CD4_w_protein/genes/hvg_info.txt", index_col=0)
hvg = hvg.loc[hs.index]
# %%
loom_file = "../../../data/10x_PBMC_w_proteins/cd4/data.loom"
with loompy.connect(loom_file, 'r') as ds:
barcodes = ds.ca['Barcode'][:]
scaled = ds.layers['scaled'][:, :]
counts = ds.layers[''][:, :]
gene_info = ds.ra['EnsID', 'Symbol']
num_umi = ds.ca['NumUmi'][:]
# Have to do this because data_slideseq makes it a numpy array
gene_info = pd.DataFrame(
gene_info, columns=['EnsID', 'Symbol']).set_index('EnsID')
scaled = pd.DataFrame(scaled, index=gene_info.index, columns=barcodes)
counts = pd.DataFrame(counts, index=gene_info.index, columns=barcodes)
umap = pd.read_table(
"../../CD4_w_protein/umap/umap_hvg.txt", index_col=0
)
umap = umap.loc[scaled.columns]
embedding = pd.read_table(
"../../CD4_w_protein/scvi/hvg/latent.txt.gz", index_col=0)
embedding = embedding.loc[scaled.columns]
# %%
gene_cmap = LinearSegmentedColormap.from_list(
"grays", ['#cccccc', '#000000']
)
def plot_gene(gene, gene_info, umap, scaled, ax=None):
if ax is None:
ax = plt.gca()
else:
plt.sca(ax)
gene_ens = gene_info.loc[gene_info.Symbol == gene].index[0]
vals = np.log2(scaled.loc[gene_ens]+1)
vmin = 0
vmax = np.percentile(vals, 80)
plot_data = pd.DataFrame({
'X': umap.umap1,
'Y': umap.umap2,
'Expression': vals,
}).sample(frac=1)
plt.scatter(
x=plot_data.X,
y=plot_data.Y,
c=plot_data.Expression,
s=4,
vmin=vmin, vmax=vmax, cmap=gene_cmap,
alpha=0.7, rasterized=True, edgecolors='none',
)
for sp in ax.spines.values():
sp.set_visible(False)
plt.xticks([])
plt.yticks([])
plt.title(gene)
# %%
plot_data = hs[['Symbol', 'Z']].join(hvg['gene.dispersion.scaled'])
plot_data = plot_data.rename({
'Z': 'Hotspot',
'gene.dispersion.scaled': 'HVG',
}, axis=1)
to_showcase = [
'TCF7', 'TGFB1', 'FOSB', 'SELL', 'CD81', 'ANXA1'
]
plt.figure()
sns.distplot(plot_data['Hotspot'], bins=50,
hist_kws={'range': (-5, 40)}, kde_kws={'bw': .3, 'gridsize': 200})
plt.xlim(-5, 40)
plt.autoscale(False)
ymax = plt.gca().get_ylim()[1]
for gene in to_showcase:
x = plot_data.loc[plot_data['Symbol'] == gene, 'Hotspot'][0]
plt.vlines(x, -1e9, ymax*.5)
plt.annotate(
gene,
(x, ymax*.5), (0, 30),
va='center', ha='center', textcoords='offset pixels',
arrowprops={'arrowstyle': '-'},
)
sns.despine(top=True, right=True, left=True)
plt.yticks([])
plt.xlabel('Hotspot Z-score')
plt.savefig('hs_vs_hvg_hs_score.svg', dpi=300)
# %%
plt.figure()
sns.distplot(plot_data['HVG'], bins=50,
hist_kws={'range': (-3, 15)}, kde_kws={'bw': .3, 'gridsize': 200})
plt.xlim(-3, 15)
plt.autoscale(False)
ymax = plt.gca().get_ylim()[1]
for gene in to_showcase:
x = plot_data.loc[plot_data['Symbol'] == gene, 'HVG'][0]
plt.vlines(x, -1e9, ymax*.5)
plt.annotate(
gene,
(x, ymax*.5), (0, 30),
va='center', ha='center', textcoords='offset pixels',
arrowprops={'arrowstyle': '-'},
)
sns.despine(top=True, right=True, left=True)
plt.yticks([])
plt.xlabel('HVG Scaled Dispersion')
plt.savefig('hs_vs_hvg_hvg_score.svg', dpi=300)
# %%
fig, axs = plt.subplots(3, 2, figsize=(6.2, 9.2))
for gene, ax in zip(to_showcase, axs.ravel()):
plot_gene(gene, gene_info, umap, scaled, ax=ax)
plt.subplots_adjust(top=.9, right=.9, left=.1, bottom=.1)
plt.savefig('hs_vs_hvg_umaps.svg', dpi=300)
# plt.show()