-
Notifications
You must be signed in to change notification settings - Fork 0
/
metabolyse.R
168 lines (153 loc) · 5.39 KB
/
metabolyse.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#' Perform an analysis
#' @rdname metabolyse
#' @description Perform analyses containing multiple analysis element steps.
#' @param data tibble or data.frame containing data to analyse
#' @param info tibble or data.frame containing data info or meta data
#' @param parameters an object of AnalysisParameters class containing
#' parameters for analysis. Default calls \code{analysisParameters()}
#' @param verbose should output be printed to the console
#' @param analysis an object of class Analysis containing previous
#' analysis results
#' @return An S4 object of class `Analysis`.
#' @details
#' Routine analyses are those that are often made up of numerous steps where parameters have likely already been previously established.
#' The emphasis here is on convenience with as little code as possible required.
#' In these analyses, the necessary analysis elements, order and parameters are first prepared and then the analysis routine subsequently performed in a single step.
#' The `metabolyse` function provides this utility, where the metabolome data, sample meta information and analysis parameters are provided.
#' The `reAnalyse` method can be used to perform further analyses on the results.
#' @examples
#' library(metaboData)
#'
#' ## Generate analysis parameters
#' p <- analysisParameters(c('pre-treatment','modelling'))
#'
#' ## Alter pre-treatment and modelling parameters to use different methods
#' parameters(p,'pre-treatment') <- preTreatmentParameters(
#' list(occupancyFilter = 'maximum',
#' transform = 'TICnorm')
#' )
#' parameters(p,'modelling') <- modellingParameters('anova')
#'
#' ## Change "cls" parameters
#' changeParameter(p,'cls') <- 'day'
#'
#' ## Run analysis using a subset of the abr1 negative mode data set
#' analysis <- metabolyse(abr1$neg[,1:200],
#' abr1$fact,
#' p)
#'
#' ## Re-analyse to include correlation analysis
#' analysis <- reAnalyse(analysis,
#' parameters = analysisParameters('correlations'))
#'
#' print(analysis)
#'
#' @importFrom methods slotNames slot
#' @importFrom tibble tibble as_tibble
#' @importFrom utils packageVersion
#' @importFrom cli console_width
#' @importFrom crayon yellow green
#' @importFrom lubridate seconds_to_period
#' @export
metabolyse <- function(data,
info,
parameters = analysisParameters(),
verbose = TRUE){
version <- packageVersion('metabolyseR') %>% as.character()
analysisStart <- date()
if (verbose == TRUE) {
startTime <- proc.time()
message(
blue('\nmetabolyseR '),
' ',
red(str_c('v',version)),
' ',
analysisStart)
message(str_c(rep('_',console_width()),collapse = ''))
params <- parameters %>%
{capture.output(print(.))} %>%
{
.[1] <- yellow(.[1])
.
} %>%
str_c(collapse = '\n')
message(params)
message(str_c(rep('_',console_width()),collapse = ''))
}
analysis <- new('Analysis',
log = list(
packageVersion = version,
analysis = analysisStart,
verbose = verbose),
parameters = parameters,
raw = analysisData(data,info),
`pre-treated` = new('AnalysisData'),
modelling = list(),
correlations = tibble()
)
elements <- analysisElements()
elements <- elements[map_dbl(elements,
~{length(slot(analysis@parameters,
.))}) > 0]
for (i in elements) {
method <- get(i)
analysis <- analysis %>% method()
}
if (verbose == TRUE) {
endTime <- proc.time()
elapsed <- {endTime - startTime} %>%
.[3] %>%
round(1) %>%
seconds_to_period() %>%
str_c('[',.,']')
message(str_c(rep('_',console_width()),collapse = ''))
message('\n',green('Complete! '),elapsed,'\n')
}
return(analysis)
}
#' @rdname metabolyse
#' @export
setGeneric('reAnalyse',
function(analysis,parameters = analysisParameters(),verbose = TRUE)
standardGeneric('reAnalyse'))
#' @rdname metabolyse
#' @importFrom methods slot slot<-
setMethod('reAnalyse',signature = 'Analysis',function(analysis,
parameters = analysisParameters(),
verbose = TRUE){
version <- packageVersion('metabolyseR') %>% as.character()
analysisStart <- date()
if (verbose == TRUE) {
startTime <- proc.time()
cat('\n',
blue('metabolyseR'),
' ',
red(str_c('v',version)),
' ',
analysisStart,
'\n',
sep = '')
cat(rep('_',console_width()),'\n',sep = '')
print(parameters)
cat(rep('_',console_width()),'\n\n',sep = '')
}
elements <- slotNames(parameters)
elements <- elements[map_dbl(elements,
~{length(slot(parameters,.x))}) > 0]
for (i in elements) {
method <- get(i)
slot(analysis@parameters,i) <- slot(parameters,i)
analysis <- analysis %>% method()
}
if (verbose == TRUE) {
endTime <- proc.time()
elapsed <- {endTime - startTime} %>%
.[3] %>%
round(1) %>%
seconds_to_period() %>%
str_c('[',.,']')
cat(rep('_',console_width()),'\n',sep = '')
cat('\n',green('Complete! '),elapsed,'\n\n',sep = '')
}
return(analysis)
})