-
Notifications
You must be signed in to change notification settings - Fork 1
/
07_query_ROR.r
131 lines (96 loc) · 2.79 KB
/
07_query_ROR.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
# library(tidyverse)
library(readr)
library(dplyr)
library(stringr)
library(stringi)
library(fs)
library(httr)
library(jsonlite)
library(magrittr)
library(purrr)
a <- readr::read_tsv("data-net/edges-2015-2019.tsv", col_types = cols(.default = "c")) %>%
select(institution) %>%
filter(!is.na(institution)) %>%
distinct()
cat("Querying ROR for", nrow(a), "affiliations")
d <- a %>%
mutate(
url = "https://api.ror.org/organizations?affiliation=" %>%
# replace spaces
str_c(str_replace_all(institution, "\\s", "+")) %>%
# sanitize: remove non-ASCII characters (otherwise error 400)
stringi::stri_trans_general(id = "Latin-ASCII"),
ror = list(NA)
)
# remove affiliations that have already been parsed
f <- "data-net/ror-results.rds"
if (fs::file_exists(f)) {
d <- anti_join(d, readr::read_rds(f), by = "institution")
cat(" (skipping", nrow(a) - nrow(d), "already queried)...\n")
} else {
cat("...\n")
}
cat("\n")
for (i in nrow(d):1) {
cat(
str_pad(i, 4),
str_pad(str_trunc(d$institution[ i ], 50), 50, side = "right"),
"..."
)
r <- try(httr::GET(d$url[ i ]), silent = TRUE)
if ("try-error" %in% class(r)) {
cat("ERROR\n")
next
}
Sys.sleep(0.75)
if (httr::status_code(r) != 200) {
cat(" error", httr::status_code(r), "\n")
next
} else {
cat(" OK\n")
d$ror[ i ] <- httr::content(r, as = "text", encoding = "UTF-8")
}
}
# subset to valid results
d <- mutate(d, ror_results = map_lgl(ror, is.character)) %>%
filter(ror_results) %>%
mutate(
ror_results = map_chr(ror,
~ jsonlite::fromJSON(.x, flatten = TRUE) %>%
magrittr::extract2("number_of_results")
),
ror_results = as.integer(ror_results)
)
# export full results -----------------------------------------------------
if (fs::file_exists(f)) {
# append
readr::write_rds(bind_rows(d, readr::read_rds(f)), f, compress = "gz")
} else {
# initialize
readr::write_rds(d, f, compress = "gz")
}
# export results overview -------------------------------------------------
d <- readr::read_rds(f)
cat("\nExporting overview of", nrow(d), "results on disk...")
r <- map_dfr(d$ror[ d$ror_results > 0 ],
~ jsonlite::fromJSON(.x, flatten = TRUE) %>%
magrittr::extract2("items") %>%
slice(1) %>%
select(
# confidence
chosen, score, matching_type,
# information
organization.name, organization.id, organization.country.country_name
)
)
# queried, successfully
bind_cols(select(filter(d, ror_results > 0), -ror), r) %>%
# queried, not successfully
bind_rows(select(filter(d, ror_results == 0), -ror)) %>%
# not queried yet
full_join(a, ., by = "institution") %>%
# stable order
arrange(institution) %>%
readr::write_tsv("data-net/ror-overview.tsv", na = "")
cat(" done.\n")
# kthxbye