-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_publications.R
271 lines (230 loc) · 8.22 KB
/
get_publications.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
get_publication <- function(resource,
limit = 20L,
verbose = FALSE,
warnings = TRUE,
progress_bar = TRUE) {
tbl_json <- get(resource_url = resource,
limit = limit,
verbose = verbose,
warnings = warnings,
progress_bar = progress_bar)
tidy_tbls <- as_tidy_tables_publications(tbl_json)
return(tidy_tbls)
}
get_publication_all <-
function(limit = 20L,
verbose = FALSE,
warnings = TRUE,
progress_bar = TRUE) {
resource <- '/rest/publication/all'
get_publication(resource = resource,
limit = limit,
verbose = verbose,
warnings = warnings,
progress_bar = progress_bar)
}
get_publication_by_pgp_id <-
function(pgp_id,
limit = 20L,
verbose = FALSE,
warnings = TRUE,
progress_bar = TRUE) {
resource <- '/rest/publication'
resource_urls <- sprintf("%s/%s", resource, pgp_id)
purrr::map(
resource_urls,
get_publication,
limit = limit,
warnings = warnings,
verbose = verbose,
progress_bar = progress_bar
) %>%
purrr::pmap(dplyr::bind_rows)
}
get_publication_by_pgs_id <-
function(pgs_id,
limit = 20L,
verbose = FALSE,
warnings = TRUE,
progress_bar = TRUE) {
resource <- '/rest/publication/search'
resource_urls <- sprintf("%s?pgs_id=%s", resource, pgs_id)
purrr::map(
resource_urls,
get_publication,
limit = limit,
warnings = warnings,
verbose = verbose,
progress_bar = progress_bar
) %>%
purrr::pmap(dplyr::bind_rows)
}
get_publication_by_pubmed_id <-
function(pubmed_id,
limit = 20L,
verbose = FALSE,
warnings = TRUE,
progress_bar = TRUE) {
resource <- '/rest/publication/search'
resource_urls <- sprintf("%s?pmid=%s", resource, pubmed_id)
purrr::map(
resource_urls,
get_publication,
limit = limit,
warnings = warnings,
verbose = verbose,
progress_bar = progress_bar
) %>%
purrr::pmap(dplyr::bind_rows)
}
get_publication_by_author <-
function(author,
limit = 20L,
verbose = FALSE,
warnings = TRUE,
progress_bar = TRUE) {
author <- purrr::map_chr(author, utils::URLencode, reserved = TRUE)
resource <- '/rest/publication/search'
resource_urls <- sprintf("%s?author=%s", resource, author)
purrr::map(
resource_urls,
get_publication,
limit = limit,
warnings = warnings,
verbose = verbose,
progress_bar = progress_bar
) %>%
purrr::pmap(dplyr::bind_rows)
}
#' Get PGS Catalog Publications
#'
#' Retrieves PGS publications via the PGS Catalog REST API. The REST
#' API is queried multiple times with the criteria passed as arguments (see
#' below). By default all publications that match the criteria supplied in the
#' arguments are retrieved: this corresponds to the default option
#' \code{set_operation} set to \code{'union'}. If you rather have only the
#' associations that match simultaneously all criteria provided, then set
#' \code{set_operation} to \code{'intersection'}.
#'
#' Please note that all search criteria are vectorised, thus allowing for batch
#' mode search. For more details see the help vignette:
#' \code{vignette("getting-pgs-publications", package = "quincunx")}.
#'
#' @param pgp_id A character vector of PGS Catalog publication accession
#' identifiers.
#' @param pgs_id A \code{character} vector of PGS Catalog score accession
#' identifiers.
#' @param pubmed_id An \code{integer} vector of
#' \href{https://en.wikipedia.org/wiki/PubMed}{PubMed} identifiers.
#' @param author A character vector of author names, any author in the list of
#' authors in a publication, .e.g. \code{'Mavaddat'}.
#' @param set_operation Either \code{'union'} or \code{'intersection'}. This
#' tells how publications retrieved by different criteria should be combined:
#' \code{'union'} binds together all results removing duplicates and
#' \code{'intersection'} only keeps same publications found with different
#' criteria.
#' @param interactive A logical. If all publications are requested, whether to ask
#' interactively if we really want to proceed.
#' @param verbose A \code{logical} indicating whether the function should be
#' verbose about the different queries or not.
#' @param warnings A \code{logical} indicating whether to print warnings, if any.
#' @param progress_bar Whether to show a progress bar as the queries are
#' performed.
#'
#' @return A \linkS4class{publications} object.
#' @examples
#' \dontrun{
#' # Get PGS publications by their identifier
#' get_publications(pgp_id = c('PGP000001', 'PGP000002'))
#'
#' # By polygenic score identifier
#' get_publications(pgs_id = 'PGS000003')
#'
#' # By PubMed identifier
#' get_publications(pubmed_id = '30554720')
#'
#' # By author's last name
#' get_publications(author = 'Natarajan')
#' }
#' @export
get_publications <- function(pgp_id = NULL,
pgs_id = NULL,
pubmed_id = NULL,
author = NULL,
set_operation = 'union',
interactive = TRUE,
verbose = FALSE,
warnings = TRUE,
progress_bar = TRUE) {
if(!(rlang::is_scalar_character(set_operation) &&
set_operation %in% c('union', 'intersection')))
stop("set_operation must be either 'union' or 'intersection'")
if(!(rlang::is_scalar_logical(verbose) && verbose %in% c(TRUE, FALSE)))
stop("verbose must be either TRUE or FALSE")
if(!(rlang::is_scalar_logical(warnings) && warnings %in% c(TRUE, FALSE)))
stop("warnings must be either TRUE or FALSE")
list_of_publications <- list()
if (!rlang::is_null(pgp_id))
list_of_publications[['get_publication_by_pgp_id']] <-
get_publication_by_pgp_id(
pgp_id = pgp_id,
verbose = verbose,
warnings = warnings,
progress_bar = progress_bar
) %>%
coerce_to_s4_publications()
if (!rlang::is_null(pgs_id))
list_of_publications[['get_publication_by_pgs_id']] <-
get_publication_by_pgs_id(pgs_id = pgs_id,
verbose = verbose,
warnings = warnings,
progress_bar = progress_bar) %>%
coerce_to_s4_publications()
if (!rlang::is_null(pubmed_id))
list_of_publications[['get_publication_by_pubmed_id']] <-
get_publication_by_pubmed_id(pubmed_id = pubmed_id,
verbose = verbose,
warnings = warnings,
progress_bar = progress_bar) %>%
coerce_to_s4_publications()
if (!rlang::is_null(author))
list_of_publications[['get_publication_by_author']] <-
get_publication_by_author(author = author,
verbose = verbose,
warnings = warnings,
progress_bar = progress_bar) %>%
coerce_to_s4_publications()
# If no criteria have been passed, i.e. all are NULL then got fetch all
# publications.
if(rlang::is_empty(list_of_publications)) {
msg1 <- "You are about to download all publications from the PGS Catalog.\nThis might take a while."
msg2 <- 'Returning an empty publications object!'
msg3 <- 'OK! Getting all publications then. This is going to take a while...'
if(interactive)
default_answer = NULL # i.e., use interactive mode.
else
default_answer = 'y'
if (sure(
before_question = msg1,
after_saying_no = msg2,
after_saying_yes = msg3,
default_answer = default_answer
))
return(coerce_to_s4_publications(
get_publication_all(
verbose = verbose,
warnings = warnings,
progress_bar = progress_bar
)
))
else
return(coerce_to_s4_publications(NULL))
} else {
if (identical(set_operation, "union")) {
return(purrr::reduce(list_of_publications, union))
}
if (identical(set_operation, "intersection")) {
return(purrr::reduce(list_of_publications, intersect))
}
}
}