Skip to content

Commit 0fbe44f

Browse files
committed
Update vignette and precompile
1 parent d3e5752 commit 0fbe44f

3 files changed

Lines changed: 320 additions & 44 deletions

File tree

vignettes/precompile.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
# More info here: https://ropensci.org/technotes/2019/12/08/precompute-vignettes/
33
library(knitr)
44
knit("vignettes/webchem.Rmd.orig", "vignettes/webchem.Rmd") #Get Started
5+
knit("vignettes/pubchem-pages.Rmd.orig", "vignettes/pubchem-pages.Rmd") #PubChem pages

vignettes/pubchem-pages.Rmd

Lines changed: 171 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,10 @@ vignette: >
77
%\VignetteEncoding{UTF-8}
88
---
99

10-
```{r, include = FALSE}
11-
knitr::opts_chunk$set(
12-
collapse = TRUE,
13-
comment = "#>"
14-
)
15-
```
1610

17-
```{r setup}
11+
12+
13+
``` r
1814
library(webchem)
1915
```
2016

@@ -26,58 +22,120 @@ When we manually search for a compound on PubChem (https://pubchem.ncbi.nlm.nih.
2622

2723
First, we need to get the PubChem ID (CID) of the compound. This can be done using the `get_cid()` function. Throughout this vignette, we will mostly work with paracetamol, so let's get its CID:
2824

29-
```{r}
25+
26+
``` r
3027
get_cid("paracetamol")
28+
#> # A tibble: 1 × 2
29+
#> query cid
30+
#> <chr> <chr>
31+
#> 1 paracetamol 1983
3132
```
33+
3234
Now that we know the CID, we can retrieve any section of the PubChem web page using `pc_sect()` (short for PubChem Section). Here is an example:
3335

34-
```{r}
35-
pc_sect(1983, section = "smiles")
36+
37+
``` r
38+
pc_sect(1983, section = "smiles") |> dplyr::select(1:6)
39+
#> # A tibble: 1 × 6
40+
#> Section Domain ID Name Result SourceName
41+
#> <chr> <chr> <chr> <chr> <chr> <chr>
42+
#> 1 smiles compound 1983 Acetaminophen CC(=O)NC1=CC=C(C=C1)O PubChem
3643
```
3744

38-
Notice that the function returns a data frame rather than a string. `pc_sect()` always returns a data frame but the variables it contains depends on the section. The first four variable names sown above are very common because they follow a standard schema for sections where the data consists of a single string, number, or similar value: the queried CID and section, the compound name, and the result.
45+
Notice that the function returns a data frame rather than a string. `pc_sect()` always returns a data frame but the variables it contains depends on the section. The first five variable names shown above are common because they follow a standard schema for sections whose data consists of a single string, number, or similar value: the queried section, domain, ID (e.g., CID or SID), name, and result. For simplicity, this vignette only shows the first six variables of each tibble. Check the full output if you want to see all available variables.
3946

4047
Returning a data frame offers several advantages over returning only the result string. For example, we can query multiple compounds at the same time and simply get a data frame with additional rows:
4148

42-
```{r}
43-
pc_sect(c(1983, 3672), section = "smiles")
49+
50+
``` r
51+
pc_sect(c(1983, 3672), section = "smiles") |> dplyr::select(1:6)
52+
#> # A tibble: 2 × 6
53+
#> Section Domain ID Name Result SourceName
54+
#> <chr> <chr> <chr> <chr> <chr> <chr>
55+
#> 1 smiles compound 1983 Acetaminophen CC(=O)NC1=CC=C(C=C1)O PubChem
56+
#> 2 smiles compound 3672 Ibuprofen, (+-)- CC(C)CC1=CC=C(C=C1)C(C)C(=O)O PubChem
4457
```
58+
4559
Another advantage of the data frame structure is that it also includes any reference data stored in PubChem for each data element.
4660

47-
The `section` argument is not case sensitive but it is sensitive to typing errors and requires the full name of the section as it is printed on the content page. The PubChem Table of Contents Tree can also be found at https://pubchem.ncbi.nlm.nih.gov/classification/#hid=72. As a general rule, `pc_sect()` works with the lowest level section before the data. For example, it works with "IUPAC Name" but not with "Names and Identifiers".
61+
The `section` argument is not case sensitive but it is sensitive to typing errors and requires the full name of the section as it is printed on the content page. The PubChem Table of Contents Tree can also be found at https://pubchem.ncbi.nlm.nih.gov/classification/#hid=72. As a general rule, `pc_sect()` works with the lowest level section before the data. For example, it works with "IUPAC Name" but not with "Names and Identifiers".
4862

4963
Let's look at some of the sections in more detail.
5064

5165
## Names and Identifiers
5266

5367
Here are a few more examples from the "Names and Identifiers" section:
5468

55-
```{r}
56-
pc_sect(1983, "IUPAC Name")
69+
70+
``` r
71+
pc_sect(1983, "IUPAC Name") |> dplyr::select(1:6)
72+
#> # A tibble: 1 × 6
73+
#> Section Domain ID Name Result SourceName
74+
#> <chr> <chr> <chr> <chr> <chr> <chr>
75+
#> 1 iupac name compound 1983 Acetaminophen N-(4-hydroxyphenyl)acetamide PubChem
5776
```
5877

59-
```{r}
60-
pc_sect(1983, "CAS")
78+
79+
``` r
80+
pc_sect(1983, "CAS") |> dplyr::select(1:6)
81+
#> # A tibble: 19 × 6
82+
#> Section Domain ID Name Result SourceName
83+
#> <chr> <chr> <chr> <chr> <chr> <chr>
84+
#> 1 cas compound 1983 Acetaminophen 103-90-2 Australian Industrial Chemicals Introduction Scheme (AICIS)
85+
#> 2 cas compound 1983 Acetaminophen 103-90-2 CAMEO Chemicals
86+
#> 3 cas compound 1983 Acetaminophen 103-90-2 CAS Common Chemistry
87+
#> 4 cas compound 1983 Acetaminophen 103-90-2 ChemIDplus
88+
#> 5 cas compound 1983 Acetaminophen 103-90-2 DrugBank
89+
#> 6 cas compound 1983 Acetaminophen 103-90-2 DTP/NCI
90+
#> 7 cas compound 1983 Acetaminophen 103-90-2 DTP/NCI
91+
#> 8 cas compound 1983 Acetaminophen 103-90-2 DTP/NCI
92+
#> 9 cas compound 1983 Acetaminophen 103-90-2 EFSA OpenFoodTox
93+
#> 10 cas compound 1983 Acetaminophen 103-90-2 EPA Chemicals under the TSCA
94+
#> 11 cas compound 1983 Acetaminophen 103-90-2 EPA DSSTox
95+
#> 12 cas compound 1983 Acetaminophen 103-90-2 European Chemicals Agency (ECHA)
96+
#> 13 cas compound 1983 Acetaminophen 103-90-2 FDA Global Substance Registration System (GSRS)
97+
#> 14 cas compound 1983 Acetaminophen 103-90-2 Hazardous Substances Data Bank (HSDB)
98+
#> 15 cas compound 1983 Acetaminophen 103-90-2 Human Metabolome Database (HMDB)
99+
#> 16 cas compound 1983 Acetaminophen 103-90-2 ILO-WHO International Chemical Safety Cards (ICSCs)
100+
#> 17 cas compound 1983 Acetaminophen 103-90-2 New Zealand Environmental Protection Authority (EPA)
101+
#> 18 cas compound 1983 Acetaminophen 103-90-2 NIAID ChemDB
102+
#> 19 cas compound 1983 Acetaminophen 103-90-2 Risk Assessment Information System (RAIS)
61103
```
62104

63-
```{r}
64-
pc_sect(1983, "ChEMBL ID")
105+
106+
``` r
107+
pc_sect(1983, "ChEMBL ID") |> dplyr::select(1:6)
108+
#> # A tibble: 2 × 6
109+
#> Section Domain ID Name Result SourceName
110+
#> <chr> <chr> <chr> <chr> <chr> <chr>
111+
#> 1 chembl id compound 1983 Acetaminophen CHEMBL112 ChEMBL
112+
#> 2 chembl id compound 1983 Acetaminophen CHEMBL112 Open Targets
65113
```
66114

67115
## Synonyms
68116

69117
Let's look at "Depositor-Supplied Synonyms":
70118

71-
```{r}
72-
pc_sect(1983, "depositor-supplied synonyms") |> head(5)
119+
120+
``` r
121+
pc_sect(1983, "depositor-supplied synonyms") |> head(5) |> dplyr::select(1:6)
122+
#> # A tibble: 5 × 6
123+
#> Section Domain ID Name Result SourceName
124+
#> <chr> <chr> <chr> <chr> <chr> <chr>
125+
#> 1 depositor-supplied synonyms compound 1983 Acetaminophen acetaminophen PubChem
126+
#> 2 depositor-supplied synonyms compound 1983 Acetaminophen Paracetamol PubChem
127+
#> 3 depositor-supplied synonyms compound 1983 Acetaminophen 4-Acetamidophenol PubChem
128+
#> 4 depositor-supplied synonyms compound 1983 Acetaminophen 103-90-2 PubChem
129+
#> 5 depositor-supplied synonyms compound 1983 Acetaminophen N-(4-Hydroxyphenyl)acetamide PubChem
73130
```
74131

75132
Notice that the dash is needed for the section to work.
76133

77134
Now this one is a little trickier:
78135

79-
```{r}
80-
A <- pc_sect(1983, "MeSH Entry Terms", form = "long")
136+
137+
``` r
138+
pc_sect(1983, "MeSH Entry Terms", form = "long") |> dplyr::select(1:6)
81139
```
82140

83141
Notice that we need to use `form = "long"` to get the desired result. So far "MeSH Entry Terms" appear to be the only section where where `form` needs to be set manually, but let us know if you find other sections.
@@ -86,61 +144,130 @@ Notice that we need to use `form = "long"` to get the desired result. So far "Me
86144

87145
In the "Computed Properties" section, the lowest-level section names that can be retrieved are the property names. For example:
88146

89-
```{r}
90-
pc_sect(1983, "xlogp3")
147+
148+
``` r
149+
pc_sect(1983, "xlogp3") |> dplyr::select(1:6)
150+
#> # A tibble: 1 × 6
151+
#> Section Domain ID Name Result SourceName
152+
#> <chr> <chr> <chr> <chr> <chr> <chr>
153+
#> 1 xlogp3 compound 1983 Acetaminophen 0.5 PubChem
91154
```
92155

93156
There are also sections that contain textual data, such as "Color/Form":
94157

95-
```{r}
96-
pc_sect(1983, "color/form")
158+
159+
``` r
160+
pc_sect(1983, "color/form") |> dplyr::select(1:6)
161+
#> # A tibble: 1 × 6
162+
#> Section Domain ID Name Result SourceName
163+
#> <chr> <chr> <chr> <chr> <chr> <chr>
164+
#> 1 color/form compound 1983 Acetaminophen Large monoclinic prisms from water Hazardous Substances Data Bank (HSDB)
97165
```
98166
Notice that the forward slash is needed for the section name to work.
99167

100168
## Spectral Information
101169

102170
Here is how we can retrieve "1H NMR Spectra":
103171

104-
```{r}
105-
pc_sect(1983, "1H NMR Spectra")
172+
173+
``` r
174+
pc_sect(1983, "1H NMR Spectra") |> dplyr::select(1:6)
175+
#> # A tibble: 3 × 6
176+
#> Section Domain ID Name `Spectra ID` `Instrument Type`
177+
#> <chr> <chr> <chr> <chr> <chr> <chr>
178+
#> 1 1h nmr spectra compound 1983 Acetaminophen 1761 Varian
179+
#> 2 1h nmr spectra compound 1983 Acetaminophen 2079 JEOL
180+
#> 3 1h nmr spectra compound 1983 Acetaminophen <NA> <NA>
106181
```
107182

108-
The PubChem web pagealso displays a figure, which can be retrieved from the URL in the `Thumbnail` variable. However, if we want to generate the figure ourselves, the peaks are available in the `Shifts [ppm]:Intensity` variable. Notice that this section returns several data variables instead of a single `Result` variable. This is because the retrieved data set is a data frame, so `form = "auto"` detects this and converts the output to wide format.
183+
The PubChem web page also displays a figure, which can be retrieved from the URL in the `Thumbnail` variable. However, if we want to generate the figure ourselves, the peaks are available in the `Shifts [ppm]:Intensity` variable. Notice that this section returns several data variables instead of a single `Result` variable. This is because the retrieved data set is a data frame, so `form = "auto"` detects this and converts the output to wide format.
109184

110185
## Drug and Medical Information
111186

112187
I included this section in the vignette because its output is a little more complicated. When we look at this section of the PubChem web page (https://pubchem.ncbi.nlm.nih.gov/compound/1983#section=Drug-Indication) we see that it contains both a table and textual data. When we retrieve the data, `pc_sect()` returns the table in wide format while using the standard schema with the `Result` column for the textual data. Downstream processing of this data frame may require some extra attention.
113188

114-
```{r}
115-
pc_sect(1983, "drug indication") |> head(5)
189+
190+
``` r
191+
pc_sect(1983, "drug indication") |> head(5) |> dplyr::select(1:6)
192+
#> # A tibble: 5 × 6
193+
#> Section Domain ID Name gid refchemids
194+
#> <chr> <chr> <chr> <chr> <chr> <chr>
195+
#> 1 drug indication compound 1983 Acetaminophen <NA> 37
196+
#> 2 drug indication compound 1983 Acetaminophen <NA> 37
197+
#> 3 drug indication compound 1983 Acetaminophen <NA> 37
198+
#> 4 drug indication compound 1983 Acetaminophen <NA> 37
199+
#> 5 drug indication compound 1983 Acetaminophen <NA> 37
116200
```
117201
## Clinical Trials
118202

119203
We can retrieve clinical trial information from "EU Clinical Trials Register".
120204

121-
```{r}
122-
pc_sect(1983, "EU Clinical Trials Register") |> head(5)
205+
206+
``` r
207+
pc_sect(1983, "EU Clinical Trials Register") |> head(5) |> dplyr::select(1:6)
208+
#> # A tibble: 5 × 6
209+
#> Section Domain ID Name gid EudraCT
210+
#> <chr> <chr> <chr> <chr> <chr> <chr>
211+
#> 1 eu clinical trials register compound 1983 Acetaminophen <NA> 2013-004955-19
212+
#> 2 eu clinical trials register compound 1983 Acetaminophen <NA> 2016-001596-75
213+
#> 3 eu clinical trials register compound 1983 Acetaminophen <NA> 2017-001014-28
214+
#> 4 eu clinical trials register compound 1983 Acetaminophen <NA> 2020-002908-39
215+
#> 5 eu clinical trials register compound 1983 Acetaminophen <NA> 2022-003559-32
123216
```
124217
## Toxicity
125218

126219
Let's look at some toxicity data:
127220

128-
```{r}
129-
pc_sect(1983, "Acute Effects")
221+
222+
``` r
223+
pc_sect(1983, "Acute Effects") |> dplyr::select(1:6)
224+
#> # A tibble: 31 × 6
225+
#> Section Domain ID Name gid Compound_CID
226+
#> <chr> <chr> <chr> <chr> <chr> <chr>
227+
#> 1 acute effects compound 1983 Acetaminophen <NA> 1983
228+
#> 2 acute effects compound 1983 Acetaminophen <NA> 1983
229+
#> 3 acute effects compound 1983 Acetaminophen <NA> 1983
230+
#> 4 acute effects compound 1983 Acetaminophen <NA> 1983
231+
#> 5 acute effects compound 1983 Acetaminophen <NA> 1983
232+
#> 6 acute effects compound 1983 Acetaminophen <NA> 1983
233+
#> 7 acute effects compound 1983 Acetaminophen <NA> 1983
234+
#> 8 acute effects compound 1983 Acetaminophen <NA> 1983
235+
#> 9 acute effects compound 1983 Acetaminophen <NA> 1983
236+
#> 10 acute effects compound 1983 Acetaminophen <NA> 1983
237+
#> # ℹ 21 more rows
130238
```
131239

132-
```{r}
133-
pc_sect(1983, "Lethal Concentration")
240+
241+
``` r
242+
pc_sect(1983, "Lethal Concentration") |> dplyr::select(1:6)
243+
#> # A tibble: 1 × 6
244+
#> Section Domain ID Name gid refchemids
245+
#> <chr> <chr> <chr> <chr> <chr> <chr>
246+
#> 1 lethal concentration compound 1983 Acetaminophen <NA> 37
134247
```
135248

136-
```{r}
137-
pc_sect(1983, "Lethal Dose")
249+
250+
``` r
251+
pc_sect(1983, "Lethal Dose") |> dplyr::select(1:6)
252+
#> # A tibble: 5 × 6
253+
#> Section Domain ID Name gid refchemids
254+
#> <chr> <chr> <chr> <chr> <chr> <chr>
255+
#> 1 lethal dose compound 1983 Acetaminophen <NA> 37
256+
#> 2 lethal dose compound 1983 Acetaminophen <NA> 37
257+
#> 3 lethal dose compound 1983 Acetaminophen <NA> 37
258+
#> 4 lethal dose compound 1983 Acetaminophen <NA> 37
259+
#> 5 lethal dose compound 1983 Acetaminophen <NA> 37
138260
```
139261

140-
```{r}
141-
pc_sect(1983, "Other Toxicity Values")
262+
263+
``` r
264+
pc_sect(1983, "Other Toxicity Values") |> dplyr::select(1:6)
265+
#> # A tibble: 1 × 6
266+
#> Section Domain ID Name gid refchemids
267+
#> <chr> <chr> <chr> <chr> <chr> <chr>
268+
#> 1 other toxicity values compound 1983 Acetaminophen <NA> 37
142269
```
143270

144271
## Help Us Improve PubChem Access
145272

146-
PubChem web pages contain a huge amount of information about chemicals. Most of this data can be retrieved in `webchem` using `pc_sect()`. However, there may still be cases that are not handled properly. If you find any examples where you are not satisfied with the output from `pc_sect()`, please open an issue and we will look into it.
273+
PubChem web pages contain a huge amount of information about chemicals. Most of this data can be retrieved in `webchem` using `pc_sect()`. However, there may still be cases that are not handled properly. If you find any examples where you are not satisfied with the output from `pc_sect()`, please open an issue and we will look into it.

0 commit comments

Comments
 (0)