-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_full_kme_fxns.R
87 lines (52 loc) · 2.1 KB
/
make_full_kme_fxns.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
make_full_kme <- function(analyte_data, module_eig, kme_partial, parallel=F, n_threads=NULL){
module_eig$Index <- as.character(module_eig$Index)
idx <- match(colnames(analyte_data)[-c(1,2)], module_eig$Index)
module_eig <- module_eig[idx,]
if(!identical(colnames(analyte_data)[-c(1,2)], module_eig$Index)){
stop("Expression samples do not match module eigengene samples.")
}
idx <- match(analyte_data$INDEX[-c(1)], kme_partial$Index)
kme_partial <- kme_partial[idx,]
if(!identical(analyte_data$INDEX[-c(1)], kme_partial$Index)){
stop("Expression features do match kME table features.")
}
analyte_data <- remove_dataset_indices(analyte_data)
analyte_data <- analyte_data[,-c(1)]
module_eig <- module_eig[-c(1,2)]
eig_vecs <- lapply(1:ncol(module_eig), function(i) module_eig[,i])
if(parallel){
kme_stats_list <- par_cor(analyte_data, eig_vecs, n_threads)
} else {
kme_stats_list <- lapply(eig_vecs, function(eigengene){
t(apply(analyte_data, 1, cor_fxn, eigengene))
})
}
kme_stats <- do.call(cbind, kme_stats_list)
colnames(kme_stats) <- paste0("kME", mapply(function(x) paste0(x, c(".cor", ".pval")), colnames(module_eig)))
return(cbind(kme_partial, kme_stats))
}
par_cor <- function(analyte_data, eig_vecs, n_threads){
require("future.apply")
options(future.globals.maxSize=Inf)
if(is.null(n_threads)){
n_threads <- detectCores()
}
plan(multisession, workers=n_threads)
kme_stats_list <- future_lapply(eig_vecs, FUN=function(eigengene){
t(apply(analyte_data, 1, cor_fxn, eigengene))
})
return(kme_stats_list)
} ## par_cor <- function(
remove_dataset_indices <- function(analyte_data){
analyte_data <- analyte_data[,-c(1)]
colnames(analyte_data) <- analyte_data[1,]
analyte_data <- analyte_data[-c(1),]
for(i in 2:ncol(analyte_data)){
analyte_data[,i] <- as.numeric(analyte_data[,i])
}
return(analyte_data)
}
cor_fxn <- function(x, y){
cor.stats <- cor.test(x, y)
return(c(cor.stats$estimate, cor.stats$p.value))
}