-
Notifications
You must be signed in to change notification settings - Fork 9
/
matchFormula.R
160 lines (153 loc) · 5.49 KB
/
matchFormula.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
#' @title Chemical Formula Matching
#'
#' @name matchFormula
#'
#' @description
#'
#' The `matchFormula` method matches chemical formulas from different inputs
#' (parameter `query` and `target`). Before comparison all formulas are
#' normalized using [MetaboCoreUtils::standardizeFormula()]. Inputs can be
#' either a `character` or `data.frame` containing a column with formulas.
#' In case of `data.frame`s parameter `formulaColname` needs to be used to
#' specify the name of the column containing the chemical formulas.
#'
#' @param BPPARAM parallel processing setup. See `BiocParallel::bpparam()` for
#' details.
#'
#' @param formulaColname `character` with the name of the column containing
#' chemical formulas. Can be of length 1 if both `query` and `target` are
#' `data.frame`s and the name of the column with chemical formulas is the
#' same for both. If different columns are used, `formulaColname[1]` can be
#' used to define the column name in `query` and `formulaColname[2]` the
#' one of `target`.
#'
#' @param target `character` or `data.frame` with chemical formulas to compare
#' against.
#'
#' @param query `character` or `data.frame` with chemical formulas to search.
#'
#' @param ... currently ignored
#'
#' @return [Matched] object representing the result.
#'
#' @author Michael Witting
#'
#' @examples
#'
#' ## input formula
#' query <- c("H12C6O6", "C11H12O2", "HN3")
#' target <- c("HCl", "C2H4O", "C6H12O6")
#'
#' query_df <- data.frame(
#' formula = c("H12C6O6", "C11H12O2", "HN3"),
#' name = c("A", "B", "C")
#' )
#' target_df <- data.frame(
#' formula = c("HCl", "C2H4O", "C6H12O6"),
#' name = c("D", "E", "F")
#' )
#'
#' ## character vs character
#' matches <- matchFormula(query, target)
#' matchedData(matches)
#'
#' ## data.frame vs data.frame
#' matches <- matchFormula(query_df, target_df)
#' matchedData(matches)
#' ## data.frame vs character
#' matches <- matchFormula(query_df, target)
#' matchedData(matches)
#' ## character vs data.frame
#' matches <- matchFormula(query, target_df)
#' matchedData(matches)
NULL
#' @rdname matchFormula
#'
#' @export
setGeneric("matchFormula", function(query, target, ...)
standardGeneric("matchFormula"))
#' @rdname matchFormula
#'
#' @importFrom MetaboCoreUtils standardizeFormula
#'
#' @importFrom BiocParallel bpmapply SerialParam
setMethod(
"matchFormula",
signature = c(query = "character",
target = "character"),
function(query, target, BPPARAM = SerialParam()) {
matches <- do.call(
rbind, bpmapply(seq_along(query), standardizeFormula(query),
FUN = .getFormulaMatches,
MoreArgs =list(target = standardizeFormula(target)),
BPPARAM = BPPARAM, SIMPLIFY = FALSE))
Matched(query = query, target = target, matches = matches)
})
#' @rdname matchFormula
setMethod("matchFormula",
signature = c(query = "data.frameOrSimilar",
target = "data.frameOrSimilar"),
function(query, target, formulaColname = c("formula", "formula"),
BPPARAM = SerialParam()) {
if(length(formulaColname) == 1L)
formulaColname <- rep(formulaColname, 2L)
if(!formulaColname[1] %in% colnames(query))
stop("Missing column \"", formulaColname[1L], "\" in query")
if(!formulaColname[2L] %in% colnames(target))
stop("Missing column \"", formulaColname[2L], "\" in target")
res <- matchFormula(query[, formulaColname[1L]],
target[, formulaColname[2L]],
BPPARAM = BPPARAM)
res@query <- query
res@target <- target
res
})
#' @rdname matchFormula
setMethod("matchFormula",
signature = c(query = "character",
target = "data.frameOrSimilar"),
function(query, target, formulaColname = "formula",
BPPARAM = SerialParam()) {
if(!formulaColname %in% colnames(target))
stop("Missing column \"", formulaColname, "\" in target")
res <- matchFormula(query, target[, formulaColname],
BPPARAM = BPPARAM)
res@query <- query
res@target <- target
res
})
#' @rdname matchFormula
setMethod("matchFormula",
signature = c(query = "data.frameOrSimilar",
target = "character"),
function(query, target, formulaColname = "formula",
BPPARAM = SerialParam()) {
if(!formulaColname %in% colnames(query))
stop("Missing column \"", formulaColname, "\" in query")
res <- matchFormula(query[, formulaColname], target,
BPPARAM = BPPARAM)
res@query <- query
res@target <- target
res
})
#' @author Michael Witting
#'
#' @param queryIndex `integer(1)` with the index of the query
#'
#' @param queryFormula `character(1)` with the formula of the query
#'
#' @param target `character` with target formulas
#'
#' @noRd
.getFormulaMatches <- function(queryIndex, queryFormula, target) {
cls <- which(target == queryFormula)
if(length(cls)) {
data.frame(query_idx = queryIndex,
target_idx = cls,
score = 1)
} else {
data.frame(query_idx = integer(),
target_idx = integer(),
score = numeric())
}
}