-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrape_pmids.R
179 lines (132 loc) · 5.32 KB
/
scrape_pmids.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
#------------------------------------------
# Pull Article Metadata from PubMed: |
# (1) publication date |
# (2) MeSH Terms |
# (3) Journal |
# (4) Author Affiliations |
# (5) Publication Types |
# (6) Grant Codes* |
# * no longer using - WOS better |
#------------------------------------------
# Load package for web scraping & cleaning strings
#install.packages("stringr")
#install.packages("rvest")
#install.packages("tidyverse")
#install.packages("xml2")
library(tidyverse)
library(rvest)
library(stringr)
library(xml2)
#setwd("C:/Users/lmostrom/Documents")
setwd("C:/Users/17036/OneDrive/Documents")
################################### FUNCTIONS ###################################
# Pull list of PMIDs to query for individually
pull_pmids = function(query){
search = URLencode(query)
i = 0
# Form URL using the term
url = paste0('https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&retmax=5000&retstart=',
i,
'&term=',
search,
'&tool=my_tool&[email protected]'
)
# Query PubMed and save result
xml = read_xml(url)
# Store total number of papers so you know when to stop looping
N = xml %>%
xml_node('Count') %>%
xml_double()
print(N)
# Return list of article IDs to scrape later
pmid_list = xml %>%
xml_node('IdList')
pmid_list = str_extract_all(pmid_list,"\\(?[0-9]+\\)?")[[1]]
Sys.sleep(0.3)
i = 5000
while (i < N) {
# Form URL using the term
url = paste0('https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&retmax=5000&retstart=',
i,
'&term=',
search,
'&tool=my_tool&[email protected]')
# Query PubMed and save result
xml = read_xml(url)
new_ids = xml %>%
xml_node('IdList')
new_ids = str_extract_all(new_ids,"\\(?[0-9]+\\)?")[[1]]
i = i + 5000
pmid_list = append(pmid_list, new_ids)
Sys.sleep(runif(1,0.6,1))
}
return(pmid_list)
}
################### PULL ARTICLE PMID LISTS ###################################
### TOP 7 JOURNALS, ALL JOURNAL ARTICLES ========================================
queries_sub = read_tsv(file = 'GitHub/pubmed_funding/search_terms_QA.txt')
queries = paste0(queries_sub$Query, ' AND (1980/01/01[PDAT] : 2019/12/31[PDAT])')
query_names = queries_sub$Query_Name
#Run through scraping function to pull out PMIDs
PMIDs = sapply(X = queries, FUN = pull_pmids) %>%
unname()
PMIDs = as.numeric(PMIDs)
PMIDdf = data.frame(pmid=PMIDs)
write_csv(PMIDdf, path = '../../Dropbox/pubmed_funding/Data/PubMed/raw/QA_pmids.csv')
### BASIC, TRANSLATIONAL, AND CLINICAL SCIENCE JOURNAL ARTICLES, ALL JOURNALS ===
years = as.character(1980:2019)
year_queries = paste0('(', years, '/01/01[PDAT] : ', years, '/12/31[PDAT])')
queries_sub = read_tsv(file = 'GitHub/pubmed_funding/search_terms_BTC_notQA.txt')
queries = rep(queries_sub$Query, each=length(year_queries))
query_names = rep(queries_sub$Query_Name, each=length(year_queries))
queries = paste0(year_queries, ' AND ', queries)
query_names = paste0(query_names, '_', years)
#Run through scraping function to pull out PMIDs
PMIDs = sapply(X = queries[521:560], FUN = pull_pmids) %>%
unname()
for (i in 1:40) {
j = i + 520
outfile = paste0('../../Dropbox/pubmed_funding/Data/PubMed/raw/BTC/BTC_',
query_names[j],
'.csv')
subset = data.frame(unlist(PMIDs[i]), rep(query_names[j], length(unlist(PMIDs[i]))))
write_csv(subset, outfile)
}
### DISEASES (GBD LEVEL 2) JOURNAL ARTICLES, ALL JOURNALS =======================
years = as.character(1980:2019)
year_queries = paste0('(', years, '/01/01[PDAT] : ', years, '/12/31[PDAT])')
queries_sub = read_tsv(file = 'GitHub/pubmed_funding/search_terms_GBDlev2_notQA.txt')
queries = rep(queries_sub$Query, each=length(year_queries))
query_names = rep(queries_sub$Query_Name, each=length(year_queries))
queries = paste0(year_queries, ' AND ', queries)
query_names = paste0(query_names, '_', years)
#Run through scraping function to pull out PMIDs
PMIDs = sapply(X = queries[1441:1520], FUN = pull_pmids) %>%
unname()
for (i in 1:80) {
j = i + 1440
outfile = paste0('../../Dropbox/pubmed_funding/Data/PubMed/raw/Diseases/JA_',
query_names[j],
'.csv')
subset = data.frame(unlist(PMIDs[i]), rep(query_names[j], length(unlist(PMIDs[i]))))
write_csv(subset, outfile)
}
### DISEASES (GBD LEVEL 2) CLINICAL TRIALS, ALL JOURNALS ========================
years = as.character(1980:2019)
year_queries = paste0('(', years, '/01/01[PDAT] : ', years, '/12/31[PDAT])')
queries_sub = read_tsv(file = 'GitHub/pubmed_funding/search_terms_GBDlev2_CT_notQA.txt')
queries = rep(queries_sub$Query, each=length(year_queries))
query_names = rep(queries_sub$Query_Name, each=length(year_queries))
queries = paste0(year_queries, ' AND ', queries)
query_names = paste0(query_names, '_', years)
#Run through scraping function to pull out PMIDs
PMIDs = sapply(X = queries[1201:1520], FUN = pull_pmids) %>%
unname()
for (i in 1:320) {
j = i + 1200
outfile = paste0('../../Dropbox/pubmed_funding/Data/PubMed/raw/Diseases/CT_',
query_names[j],
'.csv')
subset = data.frame(unlist(PMIDs[i]), rep(query_names[j], length(unlist(PMIDs[i]))))
write_csv(subset, outfile)
}