forked from qinqian/SingleCellOpenProblems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnormalize.py
49 lines (37 loc) · 1.11 KB
/
normalize.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
from . import decorators
import scanpy as sc
import scprep
_scran = scprep.run.RFunction(
setup="library('scran')",
args="sce, min.mean=0.1",
body="""
sce <- computeSumFactors(sce, min.mean=min.mean, assay.type="X")
sizeFactors(sce)
""",
)
@decorators.normalizer
def log_scran_pooling(adata):
"""Normalize data with scran via rpy2."""
scprep.run.install_bioconductor("scran")
adata.obs["size_factors"] = _scran(adata)
adata.X = scprep.utils.matrix_vector_elementwise_multiply(
adata.X, adata.obs["size_factors"], axis=0
)
sc.pp.log1p(adata)
def _cpm(adata):
adata.layers["counts"] = adata.X.copy()
sc.pp.normalize_total(adata, target_sum=1e6, key_added="size_factors")
@decorators.normalizer
def cpm(adata):
"""Normalize data to counts per million."""
_cpm(adata)
@decorators.normalizer
def log_cpm(adata):
"""Normalize data to log counts per million."""
_cpm(adata)
sc.pp.log1p(adata)
@decorators.normalizer
def sqrt_cpm(adata):
"""Normalize data to sqrt counts per million."""
_cpm(adata)
adata.X = scprep.transform.sqrt(adata.X)