-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path112_scrna_cellxgene.Rmd
342 lines (293 loc) · 8.59 KB
/
112_scrna_cellxgene.Rmd
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
---
title: "scRNA data conversion for cellxgene"
subtitle: "MSK SPECTRUM"
author:
- "Ignacio Vazquez-Garcia"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output:
html_document:
highlight: tango
df_print: paged
code_folding: hide
fig_align: center
toc: true
toc_float: true
toc_depth: 3
number_sections: yes
params:
rmd: "112_scrna_cellxgene.Rmd"
---
```{r setup, include=FALSE}
# Global knit options
knitr::opts_knit$set(root.dir = rprojroot::find_rstudio_root_file())
# Global chunk options
knitr::opts_chunk$set(echo=FALSE, tidy=TRUE, warning=FALSE, message=FALSE)
```
```{r}
library(tidyverse)
library(Seurat)
library(SeuratData)
library(SeuratDisk)
library(sceasy)
library(googlesheets4)
library(cowplot)
library(reticulate)
use_condaenv('cellxgene')
source("src/global_vars.R")
source("src/metadata.R")
```
## Load data
```{r}
seu_obj <- readr::read_rds("/work/shah/users/uhlitzf/data/SPECTRUM/freeze/v7/outs_pre/integrated_seurat.rds")
```
## Load metadata
```{r}
gids <- list(
"organism_ontology"="910670315",
"tissue_ontology"="1819990913",
"assay_ontology"="150407398",
"suspension_ontology"="533715780",
"disease_ontology"="1550965376",
"cell_type_super_ontology"="2137427075",
"cell_type_sub_ontology"="120791258",
"ethnicity_ontology"="1904472753",
"development_stage_ontology"="722315336",
"sex_ontology"="1100516779"
)
url_template <- "https://docs.google.com/spreadsheets/d/15RWlyM8EfB7CgFGc22nIw0yBErkdHz0EFf0DYBMae2M/export?format=tsv&id=15RWlyM8EfB7CgFGc22nIw0yBErkdHz0EFf0DYBMae2M&gid="
ontology_list <- gids %>%
imap(~ str_c(url_template,.x)) %>%
map(~read_tsv(.))
```
## Map cellxgene metadata
```{r}
source("src/metadata.R")
# Duplicate Seurat object to add annotations
seu_obj_annotated <- seu_obj
scrna_meta_cell_tbl <- [email protected]
# Add patient metadata
scrna_meta_cell_tbl <- add_cellxgene_patient_metadata(scrna_meta_cell_tbl, db, ontology_list)
# Add sample metadata
scrna_meta_cell_tbl <- add_cellxgene_sample_metadata(scrna_meta_cell_tbl, db, ontology_list)
# Add cell metadata
scrna_meta_cell_tbl <- add_cellxgene_cell_metadata(scrna_meta_cell_tbl, db, ontology_list)
#
# # Add columns with constant values for all samples based on ontology table
#
# ## Organism ontology
# scrna_meta_cell_tbl <- scrna_meta_cell_tbl %>%
# # organism & organism_ontology_term_id (Example: Homo sapiens, NCBITaxon:9606)
# add_column(
# ontology_list$organism_ontology %>%
# select(c("organism", "organism_ontology_term_id")),
# ) %>%
# select(-c("organism"))
#
# # Add columns with sample-dependent values by mapping to ontology table
#
# ## Cell type ontology
# scrna_meta_cell_tbl <- scrna_meta_cell_tbl %>%
# # Remap monocytes to myeloid cells to follow convention used in paper
# mutate(cell_type = ifelse(cell_type == "Monocyte", "Myeloid.cell", cell_type)) %>%
# # cell_type & cell_type_ontology_term_id (Epithelial Cell, CL:0000066)
# left_join(
# ontology_list$cell_type_super_ontology %>%
# select(c("cell_type", "cell_type_ontology_term_id")),
# by = "cell_type"
# ) %>%
# rename(c("author_cell_type" = "cell_type")) %>%
# select(-c("cell_type_super","seurat_clusters"))
#
# # Tissue ontology
# scrna_meta_cell_tbl <- scrna_meta_cell_tbl %>%
# # Add sample assay
# left_join(
# db$sequencing_scrna %>%
# select(c("isabl_id","tumor_subsite")),
# by = c("sample"="isabl_id")
# ) %>%
# # tissue & tissue_ontology_term_id
# left_join(
# ontology_list$tissue_ontology %>%
# select(c("tumor_subsite", "tissue", "tissue_ontology_term_id")),
# by = "tumor_subsite"
# ) %>%
# select(-c("tumor_subsite","tissue"))
#
# ## Assay ontology
# scrna_meta_cell_tbl <- scrna_meta_cell_tbl %>%
# # Add sample assay
# left_join(
# db$sequencing_scrna %>%
# select(c("isabl_id","platform")),
# by = c("sample"="isabl_id")
# ) %>%
# # assay & assay_ontology_term_id (Example: 10xV2 assay, EFO:0009899)
# left_join(
# ontology_list$assay_ontology %>%
# select(c("platform", "assay", "assay_ontology_term_id")),
# by = "platform"
# ) %>%
# select(-c("platform","assay"))
#
# ## Suspension type ontology
# scrna_meta_cell_tbl <- scrna_meta_cell_tbl %>%
# # Add suspension_type
# mutate(
# suspension_type = "cell"
# )
#
# ## Disease ontology
# scrna_meta_cell_tbl <- scrna_meta_cell_tbl %>%
# # Add sample assay
# left_join(
# db$gyn_diagnosis %>%
# select(c("patient_id","gyn_diagnosis_histology")),
# by = "patient_id"
# ) %>%
# # disease & disease_ontology_term_id (MONDO or normal/PATO:0000461 if healthy)
# left_join(
# ontology_list$disease_ontology %>%
# select(c("gyn_diagnosis_histology", "disease", "disease_ontology_term_id")),
# by = "gyn_diagnosis_histology"
# ) %>%
# select(-c("gyn_diagnosis_histology","disease"))
#
# ## Ethnicity ontology
# scrna_meta_cell_tbl <- scrna_meta_cell_tbl %>%
# # Add patient ethnicity
# left_join(
# db$patients %>%
# select(c("patient_id","patient_race")),
# by = "patient_id"
# ) %>%
# # ethnicity & ethnicity_ontology_term_id (Example: Asian, HANCESTRO:0008, ‘unknown’ if info unavailable)
# left_join(
# ontology_list$ethnicity_ontology %>%
# select(c("patient_race", "ethnicity", "ethnicity_ontology_term_id")),
# by = "patient_race"
# ) %>%
# select(-c("patient_race","ethnicity"))
#
# ## Age ontology
# scrna_meta_cell_tbl <- scrna_meta_cell_tbl %>%
# # Add patient age
# left_join(
# db$patients %>%
# select(c("patient_id","patient_age")),
# by = "patient_id"
# ) %>%
# # development_stage & development_stage_ontology_term_id field (20-year-old human stage,HsapDv:0000114, ‘unknown’ if info unavailable)
# left_join(
# ontology_list$development_stage_ontology %>%
# select(c("patient_age", "development_stage", "development_stage_ontology_term_id")),
# by = "patient_age"
# ) %>%
# select(-c("patient_age","development_stage"))
#
# ## Sex ontology
# scrna_meta_cell_tbl <- scrna_meta_cell_tbl %>%
# # Add patient sex
# left_join(
# db$patients %>%
# select(c("patient_id","patient_gender")),
# by = "patient_id"
# ) %>%
# # sex & sex_ontology_term_id field – male (PATO:0000384) or female (PATO:0000383) ('unknown' if info unavailable)
# left_join(
# ontology_list$sex_ontology %>%
# select(c("patient_gender", "sex", "sex_ontology_term_id")),
# by = "patient_gender"
# ) %>%
# select(-c("patient_gender","sex"))
#
# ## Donor identifier
# scrna_meta_cell_tbl <- scrna_meta_cell_tbl %>%
# # Add patient identifier
# mutate(
# donor_id = patient_id
# ) %>%
# select(-c("patient_id","batch"))
#
# ## Sample identifier
# scrna_meta_cell_tbl <- scrna_meta_cell_tbl %>%
# rename(
# c("author_sample_id" = "sample")
# ) %>%
# # Add sample assay
# left_join(
# db$sequencing_scrna %>%
# select(c("isabl_id","tumor_supersite","tumor_site","tumor_subsite","sort_parameters","therapy","procedure","procedure_type")) %>%
# rename(
# c("sample_id" = "isabl_id")
# ) %>%
# rename_with( ~ paste("author", .x, sep = "_")),
# by = "author_sample_id"
# )
scrna_meta_cell_tbl
[email protected] <- scrna_meta_cell_tbl
```
## Export data
### Metadata
```{r}
write_tsv(scrna_meta_cell_tbl, "release/cellxgene/scrna_cell_metadata.tsv")
```
### Seurat
```{r}
seu_obj_annotated_lite <-
Seurat::DietSeurat(
seu_obj_annotated,
counts = TRUE,
data = TRUE,
scale.data = FALSE,
assays = "RNA",
dimreducs = c("pca","umap50")
)
```
```{r}
# Save Seurat object as h5Seurat
SeuratDisk::SaveH5Seurat(
seu_obj_annotated,
filename = "release/cellxgene/seu_obj_annotated_seuratdisk.h5Seurat",
overwrite = TRUE,
verbose = TRUE
)
# Convert h5Seurat file to h5ad file
SeuratDisk::Convert(
"release/cellxgene/seu_obj_annotated_seuratdisk.h5Seurat",
dest = "h5ad",
assay = "RNA",
overwrite = TRUE,
verbose = TRUE
)
```
```{r}
# Save Seurat object as h5Seurat
SeuratDisk::SaveH5Seurat(
seu_obj_annotated_lite,
filename = "release/cellxgene/seu_obj_annotated_lite_seuratdisk.h5Seurat",
overwrite = TRUE,
verbose = TRUE
)
# Convert h5Seurat file to h5ad file
SeuratDisk::Convert(
"release/cellxgene/seu_obj_annotated_lite_seuratdisk.h5Seurat",
dest = "h5ad",
assay = "RNA",
overwrite = TRUE,
verbose = TRUE
)
```
### sceasy
```{r}
# sceasy::convertFormat(
# seu_obj_annotated_lite,
# assay='RNA',
# from="seurat",
# to="anndata",
# main_layer='data',
# transfer_layers='counts',
# drop_single_values=FALSE,
# outFile='seu_obj_annotated_lite_scgene.h5ad'
# )
```