-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.R
More file actions
65 lines (54 loc) · 1.81 KB
/
Copy pathscript.R
File metadata and controls
65 lines (54 loc) · 1.81 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
63
64
65
library(flowCore)
library(anndata)
library(flowStats)
## VIASH START
par <- list(
input = "resources_test/task_cyto_batch_integration/cyto_spleen_subset/unintegrated_censored.h5ad",
output = "output.h5ad"
)
meta <- list(
name = "gaussNorm",
temp_dir: '/tmp'
)
## VIASH END
source(paste0(meta$resources_dir, "/helper_functions.R"))
source(paste0(meta$resources_dir, "/anndata_to_fcs.R"))
tmp_path <- get_temp_dir(meta)
print(paste0("Using temp dir: ", tmp_path))
on.exit(clean_temp_dir(tmp_path))
cat("Reading input files\n")
adata <- anndata::read_h5ad(par[["input"]]) |>
subset_nocontrols()
markers_to_correct <- as.vector(adata$var$channel[adata$var$to_correct])
cat("Creating FlowSet from Anndata\n")
fset <- anndata_to_fcs(adata)
print(fset)
cat("Run gaussNorm\n")
for(marker in markers_to_correct){
print(marker)
pass<- tryCatch({
fset <- gaussNorm(fset, channel.names = marker)$flowset},
error = function(e) {FALSE})
if(isFALSE(pass)){
print(paste0("Reducing n. of landmarks for ",marker))
fset <- gaussNorm(fset, channel.names = marker, max.lms = 1)$flowset
}
}
# Concatenate all flowFrames in a FlowSet
integrated_matrix <- fsApply(fset,exprs)
# fsApply returns the cells grouped per sample, so restore the original
# cell order before attaching the obs of the input anndata
integrated_matrix <- integrated_matrix[order(fcs_cell_order(adata, fset)), ]
cat("Write output AnnData to file\n")
output <- anndata::AnnData(
obs = adata$obs[,integer(0)],
var = adata$var[adata$var_names, integer(0)],
layers = list(integrated = integrated_matrix),
uns = list(
dataset_id = adata$uns$dataset_id,
method_id = meta$name,
parameters = list()
)
)
output$write_h5ad(par[["output"]], compression = "gzip")
cat("Written anndata of shape ", dim(output), " to file: ", par[["output"]], "\n")