-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscyttools_functions.R
71 lines (64 loc) · 1.61 KB
/
scyttools_functions.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
library(DropletUtils)
library(scater)
library(scran)
library(annotables)
library(tidyverse)
library(glmpca)
library(kohonen)
library(ggraph)
library(tidygraph)
library(igraph)
library(Seurat)
library(edgeR)
library(monocle3)
library(scDblFinder)
library(scry)
library(batchelor)
set.seed(8675309)
cor_dist <- function(som_codes, method){
corr_mat <- cor(t(som_codes),
method = method)
return(1 - corr_mat)
}
tidy_sparse_matrix <- function (x) {
s <- Matrix::summary(x)
row <- s$i
if (!is.null(rownames(x))) {
row <- rownames(x)[row]
}
col <- s$j
if (!is.null(colnames(x))) {
col <- colnames(x)[col]
}
ret <- data.frame(row = row, column = col, value = s$x, stringsAsFactors = FALSE)
ret
}
listCols<-function(m){
#converts a sparse Matrix into a list of its columns
expr <- split(m@x, findInterval(seq_len(nnzero(m)), m@p, left.open=TRUE))
expr
}
score_signature <- function(m, sig_weights){
expr <- split(m@x, findInterval(seq_len(nnzero(m)), m@p, left.open=TRUE))
anno <- split(rownames(m)[summary(m)$i], findInterval(seq_len(nnzero(m)), m@p, left.open=TRUE))
scores <- lapply(seq_along(expr), function(x){
cell_expr <- data.frame(expr = expr[[x]],
ensgene = anno[[x]]) %>%
inner_join(sig_weights,
by = "ensgene")
cell_score <- sum(cell_expr$expr*cell_expr$V1)
return(cell_score)
})
names(scores) <- colnames(m)
return(scores)
}
# baseline3<-function(m,f){
# vapply(listCols(m), f, FUN.VALUE=0.0)
# }
#
# f<-median
#
# baseline3(logcounts(sce_glm_pca),f)
#
#
# expr_values <- listCols(play_with_me)