-
Notifications
You must be signed in to change notification settings - Fork 6
/
SPOTlight_human_LN.Rmd
143 lines (117 loc) · 5.39 KB
/
SPOTlight_human_LN.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
---
title: "SPOTlight"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
rmarkdown::render(input = paste0("./SPOTlight_human_LN.Rmd"),
output_format = "html_document",
output_file=paste0("./SPOTlight_human_LN.html"))
## SPOTlight
```{r }
library(Matrix)
library(data.table)
library(Seurat)
library(SeuratDisk)
library(dplyr)
library(SPOTlight)
library(Seurat)
```
```{r}
# read cell2location model results
results_folder = '/nfs/team205/vk7/sanger_projects/cell2location_paper/notebooks/selected_results/lymph_nodes_analysis/'
reg_mod_name = 'RegressionNBV4Torch_57covariates_73260cells_10237genes'
reg_path = paste0(results_folder, 'regression_model/', reg_mod_name, '/')
Convert(paste0(reg_path,'sc.h5ad'), dest = "h5seurat", overwrite = TRUE)
sc_seu = LoadH5Seurat(paste0(reg_path,'sc.h5seurat'))
Idents(sc_seu) = sc_seu$Subset
sc_seu = sc_seu[!is.na(sc_seu@assays$RNA@meta.features$GeneID.2),]
sc_seu = sc_seu[!duplicated(sc_seu@assays$RNA@meta.features$GeneID.2),]
rownames(sc_seu@assays$RNA@meta.features) = sc_seu@assays$RNA@meta.features$GeneID.2
rownames(sc_seu@assays$RNA@counts) = sc_seu@assays$RNA@meta.features$GeneID.2
rownames(sc_seu@assays$RNA@data) = sc_seu@assays$RNA@meta.features$GeneID.2
```
```{r}
run_name = 'CoLocationModelNB4V2_34clusters_4039locations_10241genes_input_inferred_V4_batch1024_l2_0001_n_comb50_5_cps5_fpc3_alpha001'
sp_data_path = paste0(results_folder, run_name, '/')
Convert(paste0(sp_data_path, 'sp_with_clusters_for_seurat.h5ad'), dest = "h5seurat", overwrite = TRUE)
sp_seu = LoadH5Seurat(paste0(sp_data_path, 'sp_with_clusters_for_seurat.h5seurat'))
sp_seu = sp_seu[!is.na(sp_seu@assays$RNA@meta.features$gene_ids),]
sp_seu = sp_seu[!duplicated(sp_seu@assays$RNA@meta.features$gene_ids),]
rownames(sp_seu@assays$RNA@meta.features) = sp_seu@assays$RNA@meta.features$gene_ids
rownames(sp_seu@assays$RNA@counts) = sp_seu@assays$RNA@meta.features$gene_ids
rownames(sp_seu@assays$RNA@data) = sp_seu@assays$RNA@meta.features$gene_ids
```
```{r}
# the column for annotations
sc_seu@meta.data$Subset = factor(as.character(sc_seu@meta.data$Subset))
```
#when sc_seu contains column named SPOTlight code breaks
name <- function(se_obj, clust_vr, cl_n=5000) {
lapply(split(se_obj@meta.data, se_obj@meta.data[, clust_vr]),
function(subdf) {
n_sample <- if_else(nrow(subdf) < cl_n, as.numeric(nrow(subdf)),
as.numeric(cl_n))
tmp_ds <- subdf[sample(seq_len(nrow(subdf)), n_sample),
] %>% tibble::rownames_to_column("ID") %>% dplyr::pull(ID)
return(tmp_ds)
}) %>% purrr::flatten_chr()
}
> name(se_obj=sc_seu, clust_vr='Subset')
Error: Column name `ID` must not be duplicated.
Run `rlang::last_error()` to see where the error occurred.
> rlang::last_error()
█
├─<error/tibble_error_column_names_must_be_unique>
│ Column name `ID` must not be duplicated.
└─<error/vctrs_error_names_must_be_unique>
Names must be unique.
Backtrace:
1. global::name(se_obj = sc_seu, clust_vr = "Subset")
8. tibble::rownames_to_column(., "ID")
9. tibble:::repaired_names(c(unique(names2(df)), var))
12. vctrs::vec_as_names(...)
14. vctrs:::validate_unique(names = names, arg = arg)
15. vctrs:::stop_names_must_be_unique(names, arg)
16. vctrs:::stop_names(...)
17. vctrs:::stop_vctrs(class = c(class, "vctrs_error_names"), ...)
Run `rlang::last_trace()` to see the full context.
## running SPOTlight
```{r SPOTlight}
sc_seu@meta.data = rename(sc_seu@meta.data, sample='ID')
sc_seu <- SCTransform(sc_seu, verbose = FALSE) %>% RunPCA(verbose = FALSE)
sp_seu <- SCTransform(sp_seu, verbose = FALSE) %>% RunPCA(verbose = FALSE)
start_time <- Sys.time()
#### Extract the top marker genes from each cluster ####
Seurat::Idents(object = sc_seu) <- sc_seu@meta.data$Subset
cluster_markers_all <- Seurat::FindAllMarkers(object = sc_seu,
assay = "RNA",
slot = "data",
verbose = TRUE,
only.pos = TRUE,
logfc.threshold = 1,
min.pct = 0.9)
set.seed(123)
spotlight_ls <- spotlight_deconvolution(se_sc = sc_seu,
counts_spatial = sp_seu@assays$RNA@counts,
clust_vr = "Subset",
cluster_markers = cluster_markers_all,
cl_n = 100, # 100 by default
hvg = 5000,
ntop = NULL,
transf = "uv",
method = "nsNMF",
min_cont = 0.09)
end_time <- Sys.time()
end_time - start_time
```
```{r}
saveRDS(object = spotlight_ls,
file = "/nfs/team205/vk7/sanger_projects/cell2location_paper/notebooks/selected_results/benchmarking/lymph_nodes_analysis/SPOTlight/results_hvg5k.RDS")
decon_mtrx <- spotlight_ls[[2]]
rownames(decon_mtrx) = colnames(sp_seu)
library(data.table)
decon_df = as.data.table(decon_mtrx, keep.rownames=TRUE)
fwrite(decon_df, "/nfs/team205/vk7/sanger_projects/cell2location_paper/notebooks/selected_results/benchmarking/lymph_nodes_analysis/SPOTlight/results_hvg5k.csv")
```