-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkegg_http_tools.R
71 lines (50 loc) · 1.29 KB
/
kegg_http_tools.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
library(rvest)
library(tidyverse)
library(magrittr)
createKeggHTTPRequest = function(ko_list)
{
base = "https://www.genome.jp/kegg-bin/search_pathway_object?org_name=ko&unclassified="
paste0(base, paste0(ko_list, collapse="%20"))
}
getPathwaysFromKEGGWebpage = function(str)
{
scraped = read_html(kegg_http_request)
li_nodes = scraped %>% html_node('.box2') %>% html_nodes('li')
df = data.frame(KO=character(), Pathway=character())
for (node in li_nodes)
{
# print(node)
ko_id =
node %>%
html_node('a') %>%
html_text() %>%
toupper()
pathway_name =
node %>%
sub(".*/a>(.*?)\\(<a.*", '\\1', .)
df %<>% add_row(KO=ko_id, Pathway=pathway_name)
}
return(df)
}
getPathwaysFromKOList = function(ko_list)
{
kegg_http_request = createKeggHTTPRequest(ko_list)
print(kegg_http_request)
scraped = read_html(kegg_http_request)
li_nodes = scraped %>% html_node('.box2') %>% html_nodes('li')
df = data.frame(KO=character(), Pathway=character())
for (node in li_nodes)
{
# print(node)
ko_id =
node %>%
html_node('a') %>%
html_text() %>%
toupper()
pathway_name =
node %>%
sub(".*/a>(.*?)\\(<a.*", '\\1', .)
df %<>% add_row(KO=ko_id, Pathway=pathway_name)
}
return(df$Pathway)
}