-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.R
executable file
·109 lines (93 loc) · 2.64 KB
/
run.R
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
#!/usr/local/bin/Rscript
task <- dyncli::main()
# load libraries
library(dyncli, warn.conflicts = FALSE)
library(dynwrap, warn.conflicts = FALSE)
library(dplyr, warn.conflicts = FALSE)
library(purrr, warn.conflicts = FALSE)
library(cellTree, warn.conflicts = FALSE)
#####################################
### LOAD DATA ###
#####################################
expression <- task$expression
parameters <- task$parameters
priors <- task$priors
start_cell <-
if (!is.null(priors$start_id)) {
sample(priors$start_id, 1)
} else {
NULL
}
if (parameters$rooting_method == "null") {
parameters$rooting_method <- NULL
}
# TIMING: done with preproc
timings <- list(method_afterpreproc = Sys.time())
#####################################
### INFER TRAJECTORY ###
#####################################
# infer the LDA model
lda_out <- cellTree::compute.lda(
t(expression) + min(expression) + 1,
k.topics = parameters$num_topics,
method = parameters$method,
log.scale = FALSE,
sd.filter = parameters$sd_filter,
tot.iter = parameters$tot_iter,
tol = parameters$tolerance
)
# check whether there is prior information available
start.group.label <- NULL
grouping <- NULL
if (!is.null(priors$groups_id)) {
grouping <-
priors$groups_id %>%
dplyr::slice(match(cell_id, rownames(expression))) %>%
pull(group_id)
if (!is.null(priors$start_id)) {
start.group.label <-
priors$groups_id %>%
filter(cell_id == priors$start_id) %>%
pull(group_id)
}
}
# construct the backbone tree
mst_tree <- cellTree::compute.backbone.tree(
lda.results = lda_out,
grouping = grouping,
start.group.label = start.group.label,
width.scale.factor = parameters$width_scale_factor,
outlier.tolerance.factor = parameters$outlier_tolerance_factor,
rooting.method = parameters$rooting_method,
only.mst = FALSE,
merge.sequential.backbone = FALSE
)
# TIMING: done with trajectory inference
timings$method_aftermethod <- Sys.time()
# simplify sample graph to just its backbone
cell_graph <-
igraph::as_data_frame(mst_tree, "edges") %>%
dplyr::select(from, to, length = weight) %>%
mutate(
from = rownames(expression)[from],
to = rownames(expression)[to],
directed = FALSE
)
to_keep <-
igraph::V(mst_tree)$is.backbone %>%
setNames(rownames(expression))
#####################################
### SAVE OUTPUT TRAJECTORY ###
#####################################
output <-
wrap_data(
cell_ids = rownames(expression)
) %>%
add_cell_graph(
cell_graph = cell_graph,
to_keep = to_keep
) %>%
add_timings(
timings = timings
)
dyncli::write_output(output, task$output)