-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-traits.R
233 lines (200 loc) · 7.79 KB
/
class-traits.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
setOldClass(c("tbl_df", "tbl", "data.frame"))
#' An S4 class to represent a set of PGS Catalog Traits
#'
#' The traits object consists of six slots, each a table
#' (\code{\link[tibble]{tibble}}), that combined form a relational database of a
#' subset of PGS Catalog traits. Each trait is an observation (row) in
#' the \code{traits} table --- main table. All tables have the column
#' \code{efo_id} as primary key.
#'
#' @slot traits A table of traits. Columns:
#' \describe{
#' \item{efo_id}{An \href{https://www.ebi.ac.uk/efo/}{EFO} identifier.}
#' \item{parent_efo_id}{An \href{https://www.ebi.ac.uk/efo/}{EFO} identifier of
#' the parent trait.}
#' \item{is_child}{Is this trait obtained because it is a child of other trait?}
#' \item{trait}{Trait name.}
#' \item{description}{Detailed description of the trait from EFO.}
#' \item{url}{External link to the EFO entry.}
#' }
#' @slot pgs_ids A table of associated polygenic score identifiers. Columns:
#' \describe{
#' \item{efo_id}{An \href{https://www.ebi.ac.uk/efo/}{EFO} identifier.}
#' \item{parent_efo_id}{An \href{https://www.ebi.ac.uk/efo/}{EFO} identifier of
#' the parent trait.}
#' \item{is_child}{Is this trait obtained because it is a child of other trait?}
#' \item{pgs_id}{Polygenic Score (PGS) identifier.}
#' }
#' @slot child_pgs_ids A table of polygenic score identifiers associated with
#' the child traits. Columns:
#' \describe{
#' \item{efo_id}{An \href{https://www.ebi.ac.uk/efo/}{EFO} identifier.}
#' \item{parent_efo_id}{An \href{https://www.ebi.ac.uk/efo/}{EFO} identifier of
#' the parent trait.}
#' \item{is_child}{Is this trait obtained because it is a child of other trait?}
#' \item{child_pgs_id}{Polygenic Score (PGS) identifiers associated with child traits.}
#' }
#' @slot trait_categories A table of associated trait categories. Columns:
#' \describe{
#' \item{efo_id}{An \href{https://www.ebi.ac.uk/efo/}{EFO} identifier.}
#' \item{parent_efo_id}{An \href{https://www.ebi.ac.uk/efo/}{EFO} identifier of
#' the parent trait.}
#' \item{is_child}{Is this trait obtained because it is a child of other trait?}
#' \item{trait_category}{Trait category name.}
#' }
#' @slot trait_synonyms A table of associated trait synonyms. Columns:
#' \describe{
#' \item{efo_id}{An \href{https://www.ebi.ac.uk/efo/}{EFO} identifier.}
#' \item{parent_efo_id}{An \href{https://www.ebi.ac.uk/efo/}{EFO} identifier of
#' the parent trait.}
#' \item{is_child}{Is this trait obtained because it is a child of other trait?}
#' \item{trait_synonyms}{Trait synonyms.}
#' }
#' @slot trait_mapped_terms A table of associated external references,
#' identifiers or other terms. Columns:
#' \describe{
#' \item{efo_id}{An \href{https://www.ebi.ac.uk/efo/}{EFO} identifier.}
#' \item{parent_efo_id}{An \href{https://www.ebi.ac.uk/efo/}{EFO} identifier of
#' the parent trait.}
#' \item{is_child}{Is this trait obtained because it is a child of other trait?}
#' \item{trait_mapped_terms}{Trait mapped terms.}
#' }
#' @export
setClass(
"traits",
slots = c(
traits = "tbl_df",
pgs_ids = "tbl_df",
child_pgs_ids = "tbl_df",
trait_categories = "tbl_df",
trait_synonyms = "tbl_df",
trait_mapped_terms = "tbl_df"
)
)
#' Constructor for the S4 traits object.
#'
#' Constructor for the S4 \linkS4class{traits} object.
#'
#' @param traits A \code{s4traits_traits_tbl} tibble.
#' @param pgs_ids A \code{s4traits_pgs_ids_tbl} tibble.
#' @param child_pgs_ids A \code{s4traits_child_pgs_ids_tbl} tibble.
#' @param trait_categories A \code{s4traits_trait_categories_tbl} tibble.
#' @param trait_synonyms A \code{s4traits_trait_synonyms_tbl} tibble.
#' @param trait_mapped_terms A \code{s4traits_trait_mapped_terms_tbl} tibble.
#'
#' @return An object of class \linkS4class{traits}.
#' @keywords internal
traits <-
function(traits = s4traits_traits_tbl(),
pgs_ids = s4traits_pgs_ids_tbl(),
child_pgs_ids = s4traits_child_pgs_ids_tbl(),
trait_categories = s4traits_trait_categories_tbl(),
trait_synonyms = s4traits_trait_synonyms_tbl(),
trait_mapped_terms = s4traits_trait_mapped_terms_tbl()) {
s4_traits <- methods::new(
"traits",
traits = traits,
pgs_ids = pgs_ids,
child_pgs_ids = child_pgs_ids,
trait_categories = trait_categories,
trait_synonyms = trait_synonyms,
trait_mapped_terms = trait_mapped_terms)
return(s4_traits)
}
s4traits_traits_tbl <- function(efo_id = character(),
parent_efo_id = character(),
is_child = logical(),
trait = character(),
description = character(),
url = character()) {
tbl <- tibble::tibble(
efo_id = efo_id,
parent_efo_id = parent_efo_id,
is_child = is_child,
trait = trait,
description = description,
url = url
)
return(tbl)
}
s4traits_pgs_ids_tbl <- function(efo_id = character(),
parent_efo_id = character(),
is_child = logical(),
pgs_id = character()) {
tbl <- tibble::tibble(
efo_id = efo_id,
parent_efo_id = parent_efo_id,
is_child = is_child,
pgs_id = pgs_id
)
return(tbl)
}
s4traits_child_pgs_ids_tbl <- function(efo_id = character(),
parent_efo_id = character(),
is_child = logical(),
child_pgs_id = character()) {
tbl <- tibble::tibble(
efo_id = efo_id,
parent_efo_id = parent_efo_id,
is_child = is_child,
child_pgs_id = child_pgs_id
)
return(tbl)
}
s4traits_trait_categories_tbl <- function(efo_id = character(),
parent_efo_id = character(),
is_child = logical(),
trait_categories = character()) {
tbl <- tibble::tibble(
efo_id = efo_id,
parent_efo_id = parent_efo_id,
is_child = is_child,
trait_categories = trait_categories
)
return(tbl)
}
s4traits_trait_synonyms_tbl <- function(efo_id = character(),
parent_efo_id = character(),
is_child = logical(),
trait_synonyms = character()) {
tbl <- tibble::tibble(
efo_id = efo_id,
parent_efo_id = parent_efo_id,
is_child = is_child,
trait_synonyms = trait_synonyms
)
return(tbl)
}
s4traits_trait_mapped_terms_tbl <- function(efo_id = character(),
parent_efo_id = character(),
is_child = logical(),
trait_mapped_terms = character()) {
tbl <- tibble::tibble(
efo_id = efo_id,
parent_efo_id = parent_efo_id,
is_child = is_child,
trait_mapped_terms = trait_mapped_terms
)
return(tbl)
}
coerce_to_s4_traits <- function(lst_tbl = NULL) {
if (rlang::is_empty(lst_tbl)) {
s4_traits <- traits()
return(s4_traits)
}
s4_traits <- traits(
traits = lst_tbl$traits,
pgs_ids = lst_tbl$pgs_ids,
child_pgs_ids = lst_tbl$child_pgs_ids,
trait_categories = lst_tbl$trait_categories,
trait_synonyms = lst_tbl$trait_synonyms,
trait_mapped_terms = lst_tbl$trait_mapped_terms
)
s4_traits@traits <- drop_metadata_cols(s4_traits@traits)
s4_traits@pgs_ids <- drop_metadata_cols(s4_traits@pgs_ids)
s4_traits@child_pgs_ids <- drop_metadata_cols(s4_traits@child_pgs_ids)
s4_traits@trait_categories <- drop_metadata_cols(s4_traits@trait_categories)
s4_traits@trait_synonyms <- drop_metadata_cols(s4_traits@trait_synonyms)
s4_traits@trait_mapped_terms <- drop_metadata_cols(s4_traits@trait_mapped_terms)
return(s4_traits)
}