-
Notifications
You must be signed in to change notification settings - Fork 0
/
_app1.py
182 lines (153 loc) · 4.98 KB
/
_app1.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
#%%
if __name__=='__main__':
__package__ = 'covid19_cell_atlas'
import pandas as pd
import numpy as np
from ._analysis3 import analysis3
from ._helpers import round_float
from .common.caching import compose, lazy
class _app1:
analysis = analysis3
@compose(property, lazy)
def genes(self):
return self.analysis.pseudobulk.gene.data
def plot2_data(self, gene):
x1 = self.analysis.pseudobulk.sel(gene=gene)
x1 = x1.sel(sample=x1.dsm_severity_score_group!='')
x1 = x1.sum(dim='cell_type')
x1 = x1.to_dataframe().reset_index()
x3 = pd.Categorical(
x1.status,
categories=[
'Control', 'Moderate-alive', 'Severe-alive',
'Critical-alive', 'Critical-deceased'
]
)
x1['status'] = x3
return x1
def plot3_data(self, gene):
x1 = self.analysis.pseudobulk.sel(gene=gene)
x1 = x1.sel(sample=x1.dsm_severity_score_group!='')
x1 = x1.to_dataframe().reset_index()
x3 = pd.Categorical(
x1.status,
categories=[
'Control', 'Moderate-alive', 'Severe-alive',
'Critical-alive', 'Critical-deceased'
]
)
x1['status'] = x3
return x1
@compose(property, lazy)
def genes_table(self):
x = self.analysis.fit1.to_dataframe().reset_index()
x = x[~x['Pr(>F)'].isna()]
x = x.sort_values('q')
x = x[[
'subset', 'gene',
'Pr(>F)', 'q', 'F',
'Intercept',
'days_since_onset',
'dsm_severity_score_group[T.DSM_low]',
'days_since_onset:dsm_severity_score_group[T.DSM_low]'
]]
for c in ['Pr(>F)', 'q']:
x[c] = round_float(x[c], 1)
for c in x:
if c in ['q', 'Pr(>F)', 'subset', 'gene']:
continue
x[c] = round(x[c], 2)
return x
@compose(property, lazy)
def enrich1_table(self):
x = self.analysis.enrich1
x = x.to_dataframe().reset_index()
x = x[x.coef>0]
x = x.sort_values('p')
for c in ['p']:
x[c] = round_float(x[c], 1)
for c in ['coef', 'r2', 'se']:
x[c] = round(x[c], 2)
return x
@compose(property, lazy)
def enrich2_table(self):
x = self.analysis.enrich2
x = x.drop_dims('gene')
x = x.to_dataframe().reset_index()
x = x[x.ES>0]
x = x[~x.pval.isna()]
x = x.sort_values('pval')
for c in ['pval', 'padj']:
x[c] = round_float(x[c], 1)
for c in ['ES', 'NES']:
x[c] = round(x[c], 2)
return x
@compose(property, lazy)
def enrich2_leading_edge(self):
g = self.analysis.symbol_entrez
g = g.rename(
Entrez_Gene_ID='entrez',
symbol='gene'
)
g = g.to_series_sparse()
g = g.reset_index()
x = self.analysis.enrich2
x = x.leadingEdge
x = x.rename(gene='entrez')
x = x.to_series_sparse()
x = x.reset_index()
r = x.merge(g, on='entrez')
r = r[['subset', 'gene', 'sig']].drop_duplicates()
return r
@property
def sigs(self):
g = self.analysis.symbol_entrez
g = g.rename(
Entrez_Gene_ID='entrez',
symbol='gene'
)
g = g.to_series_sparse()
g = g.reset_index()
s = self.analysis.sigs
s = s.rename(gene='entrez')
s = s.to_series_sparse()
s = s.reset_index()
r = s.merge(g, on='entrez')
r = r[['gene', 'sig']].drop_duplicates()
return r
app1 = _app1()
#%%
if __name__ == '__main__':
from plotnine import *
self = app1
#%%
x2 = self.plot2_data('IL6')
print(
ggplot(x2)+aes('days_since_onset', 'np.log1p(X)/np.log(2)')+
geom_point(aes(fill='dsm_severity_score_group'), alpha=0.5)+
geom_line(aes(group='donor'), alpha=0.1)+
geom_smooth(aes(color='dsm_severity_score_group'), alpha=0.5)+
facet_grid('subset+gene~.', scales='free')+
theme(figure_size=(6, 4))+
labs(y='log2RPM')
)
#%%
x2 = self.plot3_data('IL6')
print(
ggplot(x2)+aes('days_since_onset', 'np.log1p(X)/np.log(2)')+
geom_point(aes(fill='dsm_severity_score_group'), alpha=0.5)+
geom_line(aes(group='donor'), alpha=0.1)+
geom_smooth(aes(color='dsm_severity_score_group'), alpha=0.5)+
facet_grid('subset+cell_type+gene~.', scales='free_x')+
theme(figure_size=(4, 30))+
labs(y='log2RPM')
)
#%%
x2 = self.plot2_data('IL6')
print(
ggplot(x2)+aes('status', 'dsm_severity_score')+
geom_violin(aes(fill='status'))+
geom_boxplot(width=0.05)+
geom_point(aes(color='dsm_severity_score_group'))+
theme(axis_text_x=element_text(angle=45, ha='right'))
)