-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirst_report.Rmd
293 lines (233 loc) · 7.21 KB
/
first_report.Rmd
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
---
title: "PTM discovery dates"
output: html_notebook
---
Goal is to get information on first report of a PTM. Found this information on PIR. Want to scrape it, then cross-reference and complete with PUBMED queries
```{r setup}
library(tidyverse)
library(rentrez) #https://cran.r-project.org/web/packages/rentrez/vignettes/rentrez_tutorial.html
library(lubridate)
library(here)
library(rvest)
library(janitor)
library(feather)
library(beepr)
library(data.table)
library(vroom)
library(cowplot)
#rm(list=ls()) #clear environment
#how long?
start_time <- Sys.time()
```
#pir scrape
Get resids from PIR master page
http://pir0.georgetown.edu/cgi-bin/resid
```{r}
page <- read_html("http://pir0.georgetown.edu/cgi-bin/resid")
pir_raw <- page %>%
html_node("table") %>%
html_table(fill = TRUE)
pir <- pir_raw %>%
filter(str_detect(X1, "AA\\d{4}")) %>%
slice(-1:-2) %>%
select(X1:X7) %>%
rename(resid = X1,
name = X2,
sequence_spec = X3,
weight = X4,
keyword = X5,
feature = X6,
enzyme = X7)
save(pir, file=here::here("data", "pir.RData"))
```
#page scrape
Get data from a single page
http://pir0.georgetown.edu/cgi-bin/resid?id=AA0601
Then loop through all IDs
```{r eval=FALSE}
load(here::here("data", "pir.RData"))
pir_master <- tibble(
id = character(),
data = list()
)
#id <- "AA0601"
resid <- pir$resid
#resid_short <- resid[1:3]
for (id in resid) {
page <- read_html(paste0("http://pir0.georgetown.edu/cgi-bin/resid?id=", id))
piraa_raw <- page %>%
html_nodes(".annot") %>%
html_text(trim = TRUE)
piraa <- tibble::enframe(piraa_raw) %>%
mutate(id = !!id) %>%
select(id, value) %>%
nest(data = c(value))
pir_master <- pir_master %>%
bind_rows(piraa)
}
save(pir_master, file=here::here("data", "pir_master.RData"))
beep(sound = 8) #because mario is awesome
```
# start here
```{r}
load(file=here::here("data", "pir_master.RData"))
load(file=here::here("data", "pir.RData"))
pir_unnested <- pir_master %>%
unnest(cols = c(data))
pir_clean <- pir_unnested %>%
filter(str_detect(value, "Reference"))
#pir_clean <- pir_unnested %>%
# separate(value, into = c("temp", "authors"), sep = "Authors\\:")
#pir_clean$authors <- str_trim(pir_clean$authors, side = "left")
#%>%
#separate(authors, into = c("authors", "title"), sep = "Title\\:")
pir_clean <- pir_clean %>%
mutate(pmid = str_extract(value, "(?<=PMID\\:)\\d{1,8}")) %>%
mutate(year = str_extract(pir_clean$value, "\\d{4}(?=\\sTitle\\:)")) %>%
arrange(id, year) %>%
distinct(id, .keep_all = TRUE)
```
#join
```{r}
pir <- pir %>%
left_join(pir_clean, by = c("resid" = "id"))
#clean
pir <- map(pir, ~ na_if(., " "))
```
#get systematic names
```{r}
pir_sys <- pir_unnested %>%
filter(str_detect(value, "Systematic"))
pir_sys <- pir_sys %>%
mutate(sys_name = str_extract(pir_sys$value, "(?<=name\\:).*(?=\\sCross)")) #.* matches any number of char
#str_extract(pir_sys$value, "(?<=name\\:).")
#str_extract(pir_sys$value, ".(?=\\sCross)")
```
#PUBMED
Next goal is to query pubmed to fill missing holes and cross reference
#explore
```{r}
entrez_db_searchable("pubmed")
```
#container
```{r eval=FALSE}
first <- tibble(i = character(),
id = numeric(),
year = character())
```
#first search
```{r}
load(here::here("data", "ptm_vec.RData"))
#terms <- c("lysine butyrylation", "lysine acetylation", "lysine succinylation")
#terms <- sample(ptm_vec, 10)
terms <- ptm_vec #from ptm.Rmd
for (i in terms) {
message("Getting entry for ", i)
Sys.sleep(5) #add sleepy time according to https://www.ncbi.nlm.nih.gov/robots.txt
num <- entrez_search(db="pubmed",
term = i,
retmax = 0) %>% #350K phosphorylation entries!!!
purrr::pluck(., 2)
search <- entrez_search(db="pubmed",
term = i,
retmax = num) %>%
purrr::pluck(., 1)
search <- as.double(search)
if(length(search) == 0) {
id <- 0
year <- NA
} else {
id <- min(search) #pubmed ids start small, and count up
year <- entrez_summary(db = "pubmed", id = id) %>%
purrr::pluck(., "pubdate") %>%
str_extract("\\d{4}") #extract first 4 digits from the date string to get year
}
tmp <- tibble(i, id, year)
first <- first %>%
bind_rows(tmp)
}
beep(sound = 8) #because mario is awesome
save(first, file=here::here("data", "first.RData"))
#this will give me a pubmed ID for each of the first entries of a search term
#ac_search
#ac_search$ids
#write_csv(first, here::here("data", "first.csv"))
```
#manually set some dates, NAs, etc.
```{r}
first <- vroom(here::here("data", "first.csv"))
first <- first %>%
rename(ID = i,
pmid = id)
```
#plot
```{r}
first %>%
dplyr::mutate(year = as.numeric(year)) %>%
group_by(year) %>%
summarize(n = n()) %>%
ggplot() +
geom_step(aes(x = year, y = cumsum(n), group = 1))
```
#merge dates
PTM data from https://www.uniprot.org/docs/ptmlist has RESID, so Run ptm.Rmd, then run remaining code below, to merge them all
```{r}
load(file=here::here("data", "ptm.RData"))
first_ptm <- ptm %>%
select(AC, ID, FT, KW, DR) %>%
left_join(pir_clean, by = c("DR" = "id"))
```
#merge with first
```{r}
first_ptm$ID <- str_trim(first_ptm$ID, side = "left")
first_ptm$year <- as.numeric(first_ptm$year)
first_ptm$pmid <- as.numeric(first_ptm$pmid)
first_ptm <- first_ptm %>%
left_join(first, by = "ID")
first_ptm <- first_ptm %>%
mutate(year.x = replace_na(year.x, 2020),
year.y = replace_na(year.y, 2020))
first_ptm <- first_ptm %>%
mutate(year_final = if_else(year.x < year.y, year.x, year.y)) %>%
mutate(pmid_final = if_else(year.x < year.y, pmid.x, pmid.y)) %>%
mutate(year_final = na_if(year_final, 2020))
#clean
first_ptm <- first_ptm %>%
filter(str_detect(ID, "Cyclo", negate = TRUE),
str_detect(ID, "Blocked", negate = TRUE))
#manual curation
#<<- is normally used for global assignments. R might be looking for df in parent environment and not in the function itself.
curate <- function(ac, year, pubmed) {
ac_index <- str_which(first_ptm$AC, as.character(ac))
first_ptm[[ac_index, 11]] <<- as.numeric(year)
first_ptm[[ac_index, 12]] <<- as.numeric(pubmed)
}
curate("PTM-0499", 2016, 27105115)
curate("PTM-0675", 1982, 7115308)
curate("PTM-0487", 2014, 24703693)
curate("PTM-0193", 1975, 1184585)
#curate("PTM-0639", 1925, 25101001)
#get rid of dupes (some in early refs)
write_csv(first_ptm, path = here::here("output", "first_ptm.csv"))
```
#final plot
```{r}
first_ptm %>%
mutate(year_final = as.numeric(year_final)) %>%
group_by(year_final) %>%
summarize(n = n()) %>%
mutate(cumsum = cumsum(n)) %>%
ggplot() +
geom_step(aes(x = year_final, y = cumsum, group = 1)) +
labs(x = "Year of Publication", y = "Cumulative Protein Modifications") +
scale_x_continuous(limits = c(1920, 2020), breaks = seq(1920, 2020, by = 20)) +
theme_half_open()
ggsave(filename = here::here("output", "fig2.pdf"), plot = last_plot(), width = 8, height = 6, units = "in", dpi = 300)
```
```{r}
beep(sound = 8) #because mario is awesome
#how long to scrape?
end_time <- Sys.time()
time_taken <- round(as.duration(start_time %--% end_time)/dminutes(1), digits = 1)
print(time_taken)
```