/
databaseClass-methods.R
157 lines (143 loc) · 4.78 KB
/
databaseClass-methods.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
###S4 class for function metIdentification
#' An S4 class to represent MS1 or MS2 database.
#'
#' @slot database.info Database information.
#' @slot spectra.info Metabolites in database.
#' @slot spectra.data MS2 spectra data.
#' @exportClass databaseClass
setClass(
Class = "databaseClass",
representation(
database.info = "list",
spectra.info = "data.frame",
spectra.data = "list"
),
prototype = list(
database.info = list(),
spectra.info = data.frame(matrix(nrow = 0, ncol = 0), stringsAsFactors = FALSE),
spectra.data = list()
)
)
setMethod(
f = "show",
signature = "databaseClass",
definition = function(object) {
message(crayon::yellow("-----------Base information------------"))
message("Version:", object@database.info$Version)
message("Source:", object@database.info$Source)
message("Link:", object@database.info$Link)
message("Creater:",
object@database.info$Creater,
"(",
object@database.info$Email,
")")
message(
ifelse(
object@database.info$RT,
"With RT information",
"Without RT informtaion"
)
)
message(crayon::yellow("-----------Spectral information------------"))
message(ncol(object@spectra.info),
" items of metabolite information:")
message(paste(paste(head(
colnames(object@spectra.info), 10
),
collapse = "; "),
ifelse(
ncol(object@spectra.info) > 10, "(top10)", ""
)))
message(length(unique(object@spectra.info$Lab.ID)), " metabolites in total.")
message(
length(object@spectra.data$Spectra.positive),
" metabolites with spectra in positive mode."
)
message(
length(object@spectra.data$Spectra.negative),
" metabolites with spectra in negative mode."
)
ce.pos <-
unique(unlist(lapply(
object@spectra.data$Spectra.positive, names
)))
ce.neg <-
unique(unlist(lapply(
object@spectra.data$Spectra.negative, names
)))
message("Collision energy in positive mode (number:):")
message("Total number:", length(ce.pos))
message(paste(head(ce.pos, 10), collapse = "; "), "")
message("Collision energy in negative mode:")
message("Total number:", length(ce.neg))
message(paste(head(ce.neg, 10), collapse = "; "))
}
)
#' @title Get MS2 spectra of peaks from databaseClass object
#' @description Get MS2 spectra of peaks from databaseClass object.
#' @author Xiaotao Shen
#' \email{shenxt1990@@outlook.com}
#' @param lab.id The lab ID of metabolite.
#' @param database Database (databaseClass object).
#' @param polarity positive or negative.
#' @param ce Collision value.
#' @return A MS2 spectrum (data.frame).
#' @export
#' @seealso The example and demo data of this function can be found
#' \url{https://tidymass.github.io/metid/articles/metid.html}
#' @title Get MS2 spectra of peaks from databaseClass object
#' @description Get MS2 spectra of peaks from databaseClass object.
#' @author Xiaotao Shen
#' \email{shenxt1990@@outlook.com}
#' @param lab.id The lab ID of metabolite.
#' @param database Database (databaseClass object).
#' @param polarity positive or negative.
#' @param ce Collision value.
#' @return A MS2 spectrum (data.frame).
#' @export
#' @seealso The example and demo data of this function can be found
#' \url{https://tidymass.github.io/metid/articles/metid.html}
get_ms2_spectrum = function(lab.id,
database,
polarity = c("positive", "negative"),
ce = "30") {
polarity <- match.arg(polarity)
if (!is(database, "databaseClass")) {
stop("The database must be databaseClass object.\n")
}
pol <- ifelse(polarity == "positive", 1, 2)
temp <-
database@spectra.data[[pol]][[match(lab.id, names(database@spectra.data[[pol]]))]]
temp[[match(ce, names(temp))]]
}
#'
#' #' setGeneric(
#' #' name = "get_ms2_spectrum",
#' #' def = function(object,
#' #' lab.id,
#' #' polarity = c("positive", "negative"),
#' #' ce = "30") standardGeneric(f = "get_ms2_spectrum"),
#' #' signature = "object"
#' #'
#' #' )
#' #'
#'
#' #' setMethod(
#' #' f = "get_ms2_spectrum",
#' #' signature = "databaseClass",
#' #' definition = function(object,
#' #' lab.id,
#' #' polarity = c("positive", "negative"),
#' #' ce = "30") {
#' #' polarity <- match.arg(polarity)
#' #' pol <- ifelse(polarity == "positive", 1, 2)
#' #' temp <-
#' #' object@spectra.data[[pol]][[match(lab.id, names(object@spectra.data[[pol]]))]]
#' #' temp <- temp[[match(ce, names(temp))]]
#' #' if(is.null(temp)){
#' #' temp
#' #' }else{
#' #' tibble::as_tibble(temp)
#' #' }
#' #' }
#' #' )