-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathreport.py
More file actions
executable file
·281 lines (238 loc) · 7.56 KB
/
report.py
File metadata and controls
executable file
·281 lines (238 loc) · 7.56 KB
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
import os
from typing import Any, Dict, List, Optional, Tuple
import nbformat
import numpy as np
import plotly.graph_objects as go
from jinja2 import Template
from nbconvert import HTMLExporter
from nbconvert.preprocessors import ExecutePreprocessor
from . import __version__
from .config import PACKAGE_PATH
from .dry import dryable
from .utils import get_temporary_filename
REPORT_DIR = os.path.join(PACKAGE_PATH, 'report')
BASIC_TEMPLATE_PATH = os.path.join(REPORT_DIR, 'report_basic.ipynb')
MATRIX_TEMPLATE_PATH = os.path.join(REPORT_DIR, 'report_matrix.ipynb')
MARGIN = go.layout.Margin(t=5, r=5, b=0, l=5) # noqa: E741
def dict_to_table(
d: Dict[str, Any],
column_ratio: List[int] = [3, 7],
column_align: List[str] = ['right', 'left']
) -> go.Figure:
"""Convert a dictionary to a Plot.ly table of key-value pairs.
Args:
d: Dictionary to convert
column_ratio: Relative column widths, represented as a ratio,
defaults to `[3, 7]`
column_align: Column text alignments, defaults to `['right', 'left']`
Returns:
Figure
"""
keys = []
values = []
for key, value in d.items():
if isinstance(value, list):
keys.append(key)
values.append(value[0])
for val in value[1:]:
keys.append('')
values.append(val)
else:
keys.append(key)
values.append(value)
table = go.Table(
columnwidth=column_ratio,
header={'values': ['key', 'value']},
cells={
'values': [keys, values],
'align': column_align
}
)
figure = go.Figure(data=table)
figure.update_layout(
margin=MARGIN,
xaxis_automargin=True,
yaxis_automargin=True,
autosize=True
)
return figure
def knee_plot(n_counts: List[int]) -> go.Figure:
"""Generate knee plot card.
Args:
n_counts: List of UMI counts
Returns:
Figure
"""
knee = np.sort(n_counts)[::-1]
scatter = go.Scattergl(x=knee, y=np.arange(len(knee)), mode='lines')
figure = go.Figure(data=scatter)
figure.update_layout(
margin=MARGIN,
xaxis_title='UMI counts',
yaxis_title='Number of barcodes',
xaxis_type='log',
yaxis_type='log',
xaxis_automargin=True,
yaxis_automargin=True,
autosize=True
)
return figure
def genes_detected_plot(n_counts: List[int], n_genes: List[int]) -> go.Figure:
"""Generate genes detected plot card.
Args:
n_counts: List of UMI counts
n_genes: List of gene counts
Returns:
Figure
"""
scatter = go.Scattergl(x=n_counts, y=n_genes, mode='markers')
figure = go.Figure(data=scatter)
figure.update_layout(
margin=MARGIN,
xaxis_title='UMI counts',
yaxis_title='Genes detected',
xaxis_type='log',
yaxis_type='log',
xaxis_automargin=True,
yaxis_automargin=True,
autosize=True
)
return figure
def elbow_plot(pca_variance_ratio: List[float]) -> go.Figure:
"""Generate elbow plot card.
Args:
pca_variance_ratio: List PCA variance ratios
Returns:
Figure
"""
scatter = go.Scattergl(
x=np.arange(1,
len(pca_variance_ratio) + 1),
y=pca_variance_ratio,
mode='markers'
)
figure = go.Figure(data=scatter)
figure.update_layout(
margin=MARGIN,
xaxis_title='PC',
yaxis_title='Explained variance ratio',
xaxis_automargin=True,
yaxis_automargin=True,
autosize=True
)
return figure
def pca_plot(pc: np.ndarray) -> go.Figure:
"""Generate PCA plot card.
Args:
pc: Embeddings
Returns:
Figure
"""
scatter = go.Scattergl(x=pc[:, 0], y=pc[:, 1], mode='markers')
figure = go.Figure(data=scatter)
figure.update_layout(
margin=MARGIN,
xaxis_title='PC 1',
yaxis_title='PC 2',
xaxis_automargin=True,
yaxis_automargin=True,
autosize=True
)
return figure
def write_report(
stats_path: str,
info_path: str,
inspect_path: str,
out_path: str,
matrix_path: Optional[str] = None,
barcodes_path: Optional[str] = None,
genes_path: Optional[str] = None,
t2g_path: Optional[str] = None,
) -> str:
"""Render the Jupyter notebook report with Jinja2.
Args:
stats_path: Path to kb stats JSON
info_path: Path to run_info.json
inspect_path: Path to inspect.json
out_path: Path to Jupyter notebook to generate
matrix_path: Path to matrix
barcodes_path: List of paths to barcodes.txt
genes_path: Path to genes.txt, defaults to `None`
t2g_path: Path to transcript-to-gene mapping
Returns:
Path to notebook generated
"""
template_path = MATRIX_TEMPLATE_PATH if all(
p is not None
for p in [matrix_path, barcodes_path, genes_path, t2g_path]
) else BASIC_TEMPLATE_PATH
with open(template_path, 'r') as f, open(out_path, 'w') as out:
template = Template(f.read())
out.write(
template.render(
packages=f'#!pip install kb-python>={__version__}',
stats_path=stats_path,
info_path=info_path,
inspect_path=inspect_path,
matrix_path=matrix_path,
barcodes_path=barcodes_path,
genes_path=genes_path,
t2g_path=t2g_path
)
)
return out_path
def execute_report(execute_path: str, nb_path: str,
html_path: str) -> Tuple[str, str]:
"""Execute the report and write the results as a Jupyter notebook and HTML.
Args:
execute_path: Path to Jupyter notebook to execute
nb_path: Path to Jupyter notebook to generate
html_path: Path to HTML to generate
Returns:
Tuple containing executed notebook and HTML
"""
with open(execute_path, 'r') as f:
nb = nbformat.read(f, as_version=4)
ep = ExecutePreprocessor(timeout=600)
ep.preprocess(nb)
with open(nb_path, 'w') as f:
nbformat.write(nb, f)
with open(html_path, 'w') as f:
html_exporter = HTMLExporter()
html, resources = html_exporter.from_notebook_node(nb)
f.write(html)
return nb_path, html_path
@dryable(lambda *args, **kwargs: {})
def render_report(
stats_path: str,
info_path: str,
inspect_path: str,
nb_path: str,
html_path: str,
matrix_path: Optional[str] = None,
barcodes_path: Optional[str] = None,
genes_path: Optional[str] = None,
t2g_path: Optional[str] = None,
temp_dir: str = 'tmp'
) -> Dict[str, str]:
"""Render and execute the report.
Args:
stats_path: Path to kb stats JSON
info_path: Path to run_info.json
inspect_path: Path to inspect.json
nb_path: Path to Jupyter notebook to generate
html_path: Path to HTML to generate
matrix_path: Path to matrix
barcodes_path: List of paths to barcodes.txt
genes_path: Path to genes.txt, defaults to `None`
t2g_path: Path to transcript-to-gene mapping
temp_dir: Path to temporary directory, defaults to `tmp`
Returns:
Dictionary containing notebook and HTML paths
"""
temp_path = write_report(
stats_path, info_path, inspect_path, get_temporary_filename(temp_dir),
matrix_path, barcodes_path, genes_path, t2g_path
)
execute_report(temp_path, nb_path, html_path)
return {'report_notebook': nb_path, 'report_html': html_path}