-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRMGWebScraping.R
242 lines (199 loc) · 10.8 KB
/
RMGWebScraping.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
# Belinda Slakman
# DSCS 6020 Term Project - Web Scraping
# This file will scrape the RMG website at http://rmg.coe.neu.edu for chemical data.
# Load required packages
library('RCurl')
library('XML')
# This function scrapes thermodynamic data from the RMG database according to sourceType (either 'libraries' or 'groups')
# and name (of the library or group).
ScrapeRMGThermo <- function(sourceType, name){
# form the URL by joining the rmg thermo database website with the source type and source name
url <- paste0("http://rmg.coe.neu.edu/database/thermo/", sourceType, "/", name)
webpage <- getURL(url, followlocation=TRUE)
# convert the page into a line-by-line format
tc <- textConnection(webpage)
webpage <- readLines(tc)
close(tc)
# get webpage in tree format
pagetree_parent <- htmlTreeParse(webpage, useInternalNodes = TRUE)
# Figure out how many species there are
lastSpecLabel <- unlist(xpathApply(pagetree_parent,"//*/div[@id='contents']/table[@class='thermoData']/tr[position()=last()]/td[1]/a", xmlValue))
lastSpecSplit <- strsplit(lastSpecLabel, ". ")
numSpecies <- as.numeric(lastSpecSplit[[1]][1])
# create empty vectors
label <- rep(NA, numSpecies)
adj_list <- rep(NA, numSpecies)
Hf <- rep(NA, numSpecies)
Sf <- rep(NA, numSpecies)
Cp_300 <- rep(NA, numSpecies)
Cp_1000 <- rep(NA, numSpecies)
for(index in 1:numSpecies){
# create specific URL for species
url_specific <- paste0(url, "/", index)
# Make sure it exists
if (!url.exists(url_specific)) {next}
webpage <- getURL(url_specific, followlocation=TRUE)
# convert the page into a line-by-line format
tc <- textConnection(webpage)
webpage <- readLines(tc)
close(tc)
# get webpage in tree format
pagetree <- htmlTreeParse(webpage, useInternalNodes = TRUE)
# get the label for the molecule, and add just its name to the list
whole_label <- unlist(xpathApply(pagetree,"//*/h1",xmlValue))
split_label <- strsplit(whole_label, ". ", fixed=TRUE)
label[index] <- split_label[[1]][[2]]
# get the "alt" attribute of the molecule's image, which corresponds to its adjacency list
adj_list_attr <- unlist(xpathApply(pagetree,"//*/div[@id='contents']/p/img/@alt"))
adj_list[index] <- adj_list_attr[[1]]
# Check the thermo format. We'll only process if Group additivity
number_label <- paste0(index, ". ", label[index])
thermo_type <- unlist(xpathApply(pagetree_parent, paste0("//*/table[@class='thermoData']/tr[td[a='", number_label, "']]/td[3]"), xmlValue))
if (!thermo_type=='Group additivity') {next}
# get enthalpy of formation, entropy of formation, and heat capcity at 300K and 1000K
Hf_value <- unlist(xpathApply(pagetree, "//*/table[@class='thermoEntryData']/tr[1]/td[@class='value']/span", xmlValue))
Hf_processing <- strsplit(Hf_value, " ")
Hf[index] <- paste0(Hf_processing[[1]][[1]], " ", Hf_processing[[1]][[4]])
Sf_value <- unlist(xpathApply(pagetree, "//*/table[@class='thermoEntryData']/tr[2]/td[@class='value']/span", xmlValue))
Sf_processing <- strsplit(Sf_value, " ")
Sf[index] <- paste0(Sf_processing[[1]][[1]], " ", Sf_processing[[1]][[4]])
Cp_300_value <- unlist(xpathApply(pagetree, "//*/table[@class='thermoEntryData']/tr[3]/td[@class='value']/span", xmlValue))
Cp_300_processing <- strsplit(Cp_300_value, " ")
Cp_300[index] <- paste0(Cp_300_processing[[1]][[1]], " ", Cp_300_processing[[1]][[4]])
Cp_1000_value <- unlist(xpathApply(pagetree, "//*/table[@class='thermoEntryData']/tr[8]/td[@class='value']/span", xmlValue))
Cp_1000_processing <- strsplit(Cp_1000_value, " ")
Cp_1000[index] <- paste0(Hf_processing[[1]][[1]], " ", Cp_1000_processing[[1]][[4]])
}
# Create data frame of these 10 species
thermo <- data.frame(label, adj_list, Hf, Sf, Cp_300, Cp_1000, stringsAsFactors = FALSE)
thermo <- thermo[complete.cases(thermo[,1]),]
return(thermo)
}
ScrapeRMGKineticsFromLibrary <- function(libraryName){
# form the URL by joining the rmg kinetics library website with the library name
url <- paste0("http://rmg.coe.neu.edu/database/kinetics/libraries/", libraryName)
webpage <- getURL(url, followlocation=TRUE)
# convert the page into a line-by-line format
tc <- textConnection(webpage)
webpage <- readLines(tc)
close(tc)
# get webpage in tree format
pagetree_parent <- htmlTreeParse(webpage, useInternalNodes = TRUE)
# Figure out how many species there are
lastSpecLabel <- unlist(xpathApply(pagetree_parent,"//*/div[@id='contents']/table[@class='kineticsData']/tr[position()=last()]/td[1]/a", xmlValue))
lastSpecSplit <- strsplit(lastSpecLabel, ". ")
numSpecies <- as.numeric(lastSpecSplit[[1]][1])
# create empty vectors for reactants and kinetics data
reactant_1 <- rep(NA, numSpecies)
reactant_2 <- rep(NA, numSpecies)
reactant_3 <- rep(NA, numSpecies)
product_1 <- rep(NA, numSpecies)
product_2 <- rep(NA, numSpecies)
product_3 <- rep(NA, numSpecies)
A <- rep(NA, numSpecies)
n <- rep(NA, numSpecies)
E_A <- rep(NA, numSpecies)
for(index in 1:numSpecies){
# create specific URL for reaction
url_specific <- paste0(url, "/", index)
if (!url.exists(url_specific)) {next}
webpage <- getURL(url_specific, followlocation=TRUE)
# convert the page into a line-by-line format
tc <- textConnection(webpage)
webpage <- readLines(tc)
close(tc)
# get webpage in tree format
pagetree <- htmlTreeParse(webpage, useInternalNodes = TRUE)
reactants <- unlist(xpathApply(pagetree,"//*/div[@id='contents']/table[@class='reaction']/tr/td[1]/a/img/@alt"))
reactant_1[index] <- reactants[[1]]
if(length(reactants) > 1) {reactant_2[index] <- reactants[[2]]}
if(length(reactants) > 2) {reactant_3[index] <- reactants[[3]]}
products <- unlist(xpathApply(pagetree,"//*/div[@id='contents']/table[@class='reaction']/tr/td[3]/a/img/@alt"))
product_1[index] <- products[[1]]
if(length(products) > 1) {product_2[index] <- products[[2]]}
if(length(products) > 2) {product_3[index] <- products[[3]]}
# Check the kinetics format. We'll only process if Arrhenius
number_label <- paste0(index, ". ")
kinetics_type <- unlist(xpathApply(pagetree_parent, paste0("//*/table[@class='kineticsData']/tr[td[a='", number_label, "']]/td[5]"), xmlValue))
if (!kinetics_type=='Arrhenius') {next}
kinetics <- unlist(xpathApply(pagetree,"//*/div[@id='contents']/div[@class='math']", xmlValue))
kinetics_split1 <- strsplit(kinetics, "= ")
if(length(kinetics_split1[[1]]) < 2) {next}
kinetics_split2 <- strsplit(kinetics_split1[[1]][[2]], "T")
A[index] <- kinetics_split2[[1]][[1]]
if(length(kinetics_split2[[1]]) < 2) {next}
kinetics_split3 <- strsplit(kinetics_split2[[1]][[2]], " ")
if(length(kinetics_split3[[1]]) < 9) {next}
n[index] <- kinetics_split3[[1]][[2]]
E_A[index] <- kinetics_split3[[1]][[9]]
}
kinetics <- data.frame(reactant_1, reactant_2, reactant_3, product_1, product_2, product_3, A, n, E_A, stringsAsFactors = FALSE)
kinetics <- kinetics[complete.cases(kinetics[,1]),]
return(kinetics)
}
ScrapeRMGSolvation <- function(){
url <- "http://rmg.mit.edu/database/solvation/libraries/solute"
webpage <- getURL(url, followlocation=TRUE)
# convert the page into a line-by-line format
tc <- textConnection(webpage)
webpage <- readLines(tc)
close(tc)
# get webpage in tree format
pagetree_parent <- htmlTreeParse(webpage, useInternalNodes = TRUE)
# Figure out how many species there are
lastSpecLabel <- unlist(xpathApply(pagetree_parent,"//*/div[@id='contents']/table[@class='solvationData']/tr[position()=last()]/td[1]/a", xmlValue))
lastSpecSplit <- strsplit(lastSpecLabel, ". ")
numSpecies <- as.numeric(lastSpecSplit[[1]][1])
# create empty lists for 10 solutes
label <- rep(NA, numSpecies)
adj_list <- rep(NA, numSpecies)
S <- rep(NA, numSpecies)
B <- rep(NA, numSpecies)
E <- rep(NA, numSpecies)
L <- rep(NA, numSpecies)
A <- rep(NA, numSpecies)
V <- rep(NA, numSpecies)
for(index in 1:numSpecies){
url_solvation <- paste0("http://rmg.mit.edu/database/solvation/libraries/solute/", index)
if (!url.exists(url_solvation)) {next}
webpage <- getURL(url_solvation, followlocation=TRUE)
# convert the page into a line-by-line format
tc <- textConnection(webpage)
webpage <- readLines(tc)
close(tc)
# get webpage in tree format
pagetree <- htmlTreeParse(webpage, useInternalNodes = TRUE)
# get the label for the molecule, and add just its name to the list
whole_label <- unlist(xpathApply(pagetree,"//*/h1",xmlValue))
split_label <- strsplit(whole_label, ". ", fixed=TRUE)
label[index] <- split_label[[1]][[2]]
# get the "alt" attribute of the molecule's image, which corresponds to its adjacency list
adj_list_attr <- unlist(xpathApply(pagetree,"//*/div[@id='contents']/p/a/img/@alt"))
adj_list[index] <- adj_list_attr[[1]]
# Retrieve solvation data
S_full <- unlist(xpathApply(pagetree,"//*/div[@id='contents']/table[@class='solvationEntryData']/table[@class='solvationEntryData']/tr[1]/td[@class='value']/span"), xmlValue)
splitS <- strsplit(xmlValue(S_full[[1]]), " ")
S[index] <- splitS[[1]][1]
B_full <- unlist(xpathApply(pagetree,"//*/div[@id='contents']/table[@class='solvationEntryData']/table[@class='solvationEntryData']/tr[2]/td[@class='value']/span"), xmlValue)
splitB <- strsplit(xmlValue(B_full[[1]]), " ")
B[index] <- splitB[[1]][1]
E_full <- unlist(xpathApply(pagetree,"//*/div[@id='contents']/table[@class='solvationEntryData']/table[@class='solvationEntryData']/tr[3]/td[@class='value']/span"), xmlValue)
splitE <- strsplit(xmlValue(E_full[[1]]), " ")
E[index] <- splitE[[1]][1]
L_full <- unlist(xpathApply(pagetree,"//*/div[@id='contents']/table[@class='solvationEntryData']/table[@class='solvationEntryData']/tr[4]/td[@class='value']/span"), xmlValue)
splitL <- strsplit(xmlValue(L_full[[1]]), " ")
L[index] <- splitL[[1]][1]
A_full <- unlist(xpathApply(pagetree,"//*/div[@id='contents']/table[@class='solvationEntryData']/table[@class='solvationEntryData']/tr[5]/td[@class='value']/span"), xmlValue)
splitA <- strsplit(xmlValue(A_full[[1]]), " ")
A[index] <- splitA[[1]][1]
V_full <- unlist(xpathApply(pagetree,"//*/div[@id='contents']/table[@class='solvationEntryData']/table[@class='solvationEntryData']/tr[6]/td[@class='value']/span"), xmlValue)
splitV <- strsplit(xmlValue(V_full[[1]]), " ")
V[index] <- splitV[[1]][1]
}
solvation <- data.frame(label, adj_list, S, B, E, L, A, V, stringsAsFactors = FALSE)
solvation <- solvation[complete.cases(solvation[,1]),]
return(solvation)
}
#prim_thermo <- ScrapeRMGThermo('libraries', 'primaryThermoLibrary')
#Glarborg_C3_kinetics <- ScrapeRMGKineticsFromLibrary('Glarborg/C3')
#solvation <- ScrapeRMGSolvation()