-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathscript.R
More file actions
62 lines (51 loc) · 1.77 KB
/
script.R
File metadata and controls
62 lines (51 loc) · 1.77 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
requireNamespace("anndata", quietly = TRUE)
suppressPackageStartupMessages({
library(STACAS)
library(Matrix)
library(SeuratObject)
library(Seurat)
})
## VIASH START
par <- list(
input = "resources_test/task_batch_integration/cxg_immune_cell_atlas/dataset.h5ad",
output = "output.h5ad"
)
meta <- list(
name = "stacas"
)
## VIASH END
cat("Reading input file\n")
adata <- anndata::read_h5ad(par[["input"]])
cat("Create Seurat object\n")
# Only loading normalized values, as raw counts are not needed
# Transpose because Seurat expects genes in rows, cells in columns
normalized <- Matrix::t(adata$layers[["normalized"]])
# Convert to a regular sparse matrix first and then to dgCMatrix
normalized <- as(as(normalized, "CsparseMatrix"), "dgCMatrix")
# Create Seurat object
seurat_obj <- Seurat::CreateSeuratObject(counts = normalized,
meta.data = adata$obs)
# Manually assign pre-normalized values to the "data" slot
seurat_obj@assays$RNA$data <- normalized
seurat_obj@assays$RNA$counts <- NULL # remove counts
# Obtain anchor features from the preprocessing pipeline
anchor.features <- head(adata$var[order(adata$var$hvg_score, decreasing = T), "feature_id"], 2000)
cat("Run STACAS\n")
object_integrated <- seurat_obj |>
Seurat::SplitObject(split.by = "batch") |>
STACAS::Run.STACAS(anchor.features = anchor.features)
cat("Store outputs\n")
output <- anndata::AnnData(
uns = list(
dataset_id = adata$uns[["dataset_id"]],
normalization_id = adata$uns[["normalization_id"]],
method_id = meta$name
),
obs = adata$obs,
var = adata$var,
obsm = list(
X_emb = object_integrated@reductions$pca@cell.embeddings
)
)
cat("Write output AnnData to file\n")
output$write_h5ad(par[["output"]], compression = "gzip")