-
Notifications
You must be signed in to change notification settings - Fork 0
/
summary.R
68 lines (59 loc) · 2.04 KB
/
summary.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
#' Summarise `m/z` features
#' @rdname featureSummary
#' @description Summarise spectrally processed *m/z* features.
#' @param x an object of S4 class `Binalysis` or `MetaboProfile`
#' @return A tibble containing feature summaries.
#' @examples
#' ## Retrieve file paths and sample information for example data
#' files <- metaboData::filePaths('FIE-HRMS','BdistachyonEcotypes')[1:2]
#'
#' info <- metaboData::runinfo('FIE-HRMS','BdistachyonEcotypes')[1:2,]
#'
#' ## Perform spectral binning
#' analysis <- binneR::binneRlyse(files,
#' info,
#' parameters = binneR::detectParameters(files))
#'
#' featureSummary(analysis)
#' @export
setGeneric('featureSummary',function(x)
standardGeneric('featureSummary'))
#' @rdname featureSummary
setMethod('featureSummary',signature = 'Binalysis',
function(x){
x %>%
binnedData() %>%
featSummary()
})
#' @rdname featureSummary
setMethod('featureSummary',signature = 'MetaboProfile',
function(x){
x %>%
processedData() %>%
featSummary()
})
#' @importFrom dplyr n_distinct
featSummary <- function(x){
if (is.data.frame(x)){
x <- list(x)
}
x %>%
map(~{{
.x %>%
rowid_to_column(var = 'Sample') %>%
gather('Feature','Intensity',-Sample)
}}) %>%
bind_rows() %>%
mutate(Mode = str_sub(Feature,1,1)) %>%
mutate(Mode = replace(Mode,
Mode != 'n' & Mode != 'p',
NA)) %>%
group_by(Mode) %>%
summarise(
`Number of bins` = n_distinct(Feature),
`Missing Data (%)` = round(length(which(Intensity == 0)) /
length(Intensity) * 100,2),
.groups = 'drop') %>%
mutate(Mode = replace(Mode,Mode == 'n','Negative') %>%
replace(Mode == 'p','Positive'))
}