-
Notifications
You must be signed in to change notification settings - Fork 0
/
calcCorrelations-method.R
71 lines (61 loc) · 2.3 KB
/
calcCorrelations-method.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
#' @importFrom metabolyseR analysisParameters metabolyse analysisResults keepFeatures analysisData dat sinfo
#' @importFrom magrittr set_rownames
#' @importFrom stats cutree dist hclust
setMethod('calcCorrelations',signature = 'Assignment',function(assignment){
if (assignment@log$verbose == T) {
startTime <- proc.time()
message(blue('Calculating correlations '),cli::symbol$continue,'\r',appendLF = FALSE)
}
p <- analysisParameters('correlations')
p@correlations <- assignment@parameters@correlations
if (str_detect(assignment@parameters@technique,'LC')) {
feat <- tibble(Feature = colnames(assignment@data)) %>%
mutate(RT = str_split_fixed(Feature,'@',2)[,2] %>%
as.numeric())
RTgroups <- feat %>%
data.frame() %>%
set_rownames(.$Feature) %>%
select(-Feature) %>%
dist() %>%
hclust() %>%
cutree(h = assignment@parameters@RTwindow) %>%
{tibble(Feature = names(.),Group = .)}
RTsum <- RTgroups %>%
group_by(Group) %>%
summarise(N = n())
RTgroups <- RTgroups %>%
filter(Group %in% {RTsum %>%
filter(N > 1) %>%
.$Group})
clus <- makeCluster(assignment@parameters@nCores,type = assignment@parameters@clusterType)
cors <- RTgroups %>%
split(.$Group) %>%
parLapply(cl = clus,function(f){
analysisData(assignment@data,tibble(ID = 1:nrow(assignment@data))) %>%
keepFeatures(features = f$Feature) %>%
{metabolyse(dat(.),sinfo(.),p,verbose = FALSE)} %>%
analysisResults(element = 'correlations')
}) %>%
bind_rows(.id = 'RT Group')
stopCluster(clus)
} else {
cors <- metabolyse(assignment@data,
tibble(ID = 1:nrow(assignment@data)),
p,
verbose = FALSE) %>%
analysisResults(element = 'correlations')
}
assignment@correlations <- cors
if (assignment@log$verbose == T) {
endTime <- proc.time()
elapsed <- {endTime - startTime} %>%
.[3] %>%
round(1) %>%
seconds_to_period() %>%
str_c('[',.,']')
ncors <- nrow(assignment@correlations) %>%
str_c('[',.,' correlations',']')
message(blue('Calculating correlations '),'\t',green(cli::symbol$tick),' ',ncors,' ',elapsed)
}
return(assignment)
})