-
Notifications
You must be signed in to change notification settings - Fork 19
/
drug_reaction_node_parser.R
92 lines (87 loc) · 2.77 KB
/
drug_reaction_node_parser.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
ReactionsParser <- R6::R6Class(
"ReactionsParser",
inherit = AbstractParser,
private = list(
parse_record = function() {
drugs <- xmlChildren(pkg_env$root)
pb <- progress_bar$new(total = xmlSize(drugs))
map_df(drugs, ~ private$get_reactions_df(., pb))
},
get_reactions_df = function(rec, pb) {
pb$tick()
map_df(
xmlChildren(rec[["reactions"]]),
~ private$get_reactions_rec(., xmlValue(rec[["drugbank-id"]]))
)
},
get_reactions_rec = function(r, drug_key) {
tibble(
sequence = xmlValue(r[["sequence"]]),
left_drugbank_id = xmlValue(r[["left-element"]][["drugbank-id"]]),
left_drugbank_name = xmlValue(r[["left-element"]][["name"]]),
right_drugbank_id = xmlValue(r[["right-element"]][["drugbank-id"]]),
right_drugbank_name = xmlValue(r[["right-element"]][["name"]]),
drugbank_id = drug_key
)
}
)
)
ReactionsEnzymesParser <- R6::R6Class(
"ReactionsEnzymesParser",
inherit = AbstractParser,
private = list(
parse_record = function() {
drugs <- xmlChildren(pkg_env$root)
pb <- progress_bar$new(total = xmlSize(drugs))
map_df(drugs, ~ private$get_reactions_enzymes_df(., pb))
},
get_reactions_enzymes_df = function(rec, pb) {
pb$tick()
map_df(
xmlChildren(rec[["reactions"]]),
~ drug_sub_df(., "enzymes", id = NULL)
)
}
)
)
#' Drug Reactions Parsers
#'
#' Extract the sequential representation of the metabolic reactions that this
#' drug molecule is involved in. Depending on available information, this may
#' include metabolizing enzymes, reaction type, substrates, products,
#' pharmacological activity of metabolites, and a structural representation of
#' the biochemical reactions.
#'
#' @return a tibble with 5 variables:
#' \describe{
#' \item{sequence}{ Reactions are displayed within a numerical sequence}
#' \item{left_drugbank_name}{The substrate of the reaction. Maybe a drug or a
#' metabolite.}
#' \item{rightt_drugbank_name}{ The product of the reaction. Maybe a drug or a
#' metabolite.}
#' \item{left_drugbank_id}{}
#' \item{right_drugbank_id}{}
#' \item{parent_id}{drugbank id}
#' }
#' @keywords internal
drug_reactions <- function() {
ReactionsParser$new("drug_reactions")$parse()
}
#' Drug Reactions Enzymes Parsers
#'
#' Enzymes involved in metabolizing this drug
#'
#' @return a tibble with 3 variables:
#' \describe{
#' \item{name}{}
#' \item{uniprot-id}{}
#' \item{parent_id}{drugbank id}
#' }
#' @keywords internal
drug_reactions_enzymes <- function() {
enzyme <- ReactionsEnzymesParser$new("drug_reactions_enzymes")$parse()
if ("drugbank_id" %in% names(enzyme)) {
enzyme <- rename(enzyme, enzyme_id = "drugbank_id")
}
enzyme
}