-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01-Organoid4-qc.qmd
132 lines (93 loc) · 2.22 KB
/
01-Organoid4-qc.qmd
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
---
title: "Organoid4 Quality Control"
---
# Introduction
In this document we will perform quality control on the Organoid4 dataset using the thresholds from the original paper.
```{python}
import lamindb as ln
import lnschema_bionty as lb
import scanpy as sc
lb.settings.species = "human"
transform = ln.Transform(name="Organoid4_qc")
ln.track(transform)
```
# Load Data
```{python}
file = ln.File.filter(key="Organoid4_raw.h5ad").one()
adata = file.load()
adata
```
# Prepare data
Normalise, get HVGs, calculate embeddings, calculate QC stats.
```{python}
adata.layers["counts"] = adata.X.copy()
sc.pp.normalize_total(adata, target_sum=1e4)
sc.pp.log1p(adata)
sc.pp.highly_variable_genes(adata)
sc.pp.pca(adata)
sc.pp.neighbors(adata)
sc.tl.umap(adata)
adata.var["MT"] = adata.var["gene_symbols"].str.startswith("MT-")
sc.pp.calculate_qc_metrics(adata, layer="counts", qc_vars=["MT"], inplace=True)
sc.pp.calculate_qc_metrics(adata, expr_type="expr", inplace=True)
```
# Plots
Visualise QC metrics.
## By cell
Cell QC metrics
### Total counts
```{python}
sc.pl.violin(adata, keys="total_counts", groupby="Sample")
```
### Features by total counts
```{python}
sc.pl.scatter(
adata,
x="total_counts",
y="n_genes_by_counts",
color="Sample",
)
```
### PCA
```{python}
sc.pl.pca(adata, color=["Sample", "log1p_total_counts", "pct_counts_MT"])
```
### UMAP
```{python}
sc.pl.umap(adata, color=["Sample", "log1p_total_counts", "pct_counts_MT"])
```
### Mitochondrial genes
```{python}
sc.pl.violin(adata, keys="pct_counts_MT", groupby="Sample")
```
## By gene
Gene QC metrics.
### Dropout by mean
```{python}
sc.pl.scatter(adata, x="mean_expr", y="pct_dropout_by_counts")
```
### Counts by cells
```{python}
sc.pl.scatter(adata, x="n_cells_by_counts", y="log1p_total_counts")
```
# Filtering
## Cells
Filter cells using paper thresholds.
```{python}
filtered = (adata.obs["n_genes_by_counts"] > 9000) | (adata.obs["pct_counts_MT"] > 10)
adata = adata[~ filtered, :].copy()
```
## Genes
Filter genes with less than 1 count.
```{python}
sc.pp.filter_genes(adata, min_counts=1)
```
# Save
```{python}
qc_file = ln.File.from_anndata(
adata,
field=lb.Gene.ensembl_gene_id,
key="Organoid4_qc.h5ad"
)
qc_file.save()
```