-
Notifications
You must be signed in to change notification settings - Fork 5
/
get_molecule.R
73 lines (70 loc) · 2.43 KB
/
get_molecule.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
#' Get Molecule
#'
#' Loads the structure of a molecule by fetching an SDF file from Pubchem, which can be piped to generate_full_scene
#'
#' @param molecule A character variable of a compound name or a numeric variable of an official compound ID
#'
#' @return List giving the atom locations and the connections between atoms.
#' @export
#'
#' @examples
#' if(run_documentation()) {
#' get_molecule("caffeine") %>%
#' generate_full_scene() %>%
#' render_model()
#' }
#' if(run_documentation()) {
#' #estradiol (aka estrogen)
#' get_molecule(5757) %>%
#' generate_full_scene() %>%
#' render_model()
#' }
#' if(run_documentation()) {
#' get_molecule("testosterone") %>%
#' generate_full_scene() %>%
#' render_model()
#' }
#' if(run_documentation()) {
#' get_molecule("aspirin") %>%
#' generate_full_scene() %>%
#' render_model()
#' }
#' if(run_documentation()) {
#' get_molecule("rutoside") %>%
#' generate_full_scene() %>%
#' render_model()
#' }
#' if(run_documentation()) {
#' #If the 3D SDF doesn't exist, this function will pull the 2D SDF and inform the user
#' get_molecule("cyanocobalamin") %>%
#' generate_full_scene() %>%
#' render_model()
#' }
get_molecule = function(molecule) {
#Check for 3D model first
if (is.numeric(molecule)) { #assume it is a CID
url = sprintf("https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/cid/%s/SDF/?record_type=3d",molecule)
} else if (is.character(molecule)) { #assume it is a compound name
url = sprintf("https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/%s/SDF/?record_type=3d",molecule)
} else {
stop("Provide a quoted compound name (character) or an unquoted compound ID (numeric)")
}
status = httr::status_code(httr::GET(url))
#Check for 2D model if 3D doesn't return SDF
if(status != 200) {
if (is.numeric(molecule)) { #assume it is a CID
url = sprintf("https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/cid/%s/SDF",molecule)
} else if (is.character(molecule)) { #assume it is a compound name
url = sprintf("https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/%s/SDF",molecule)
} else {
stop("Provide a quoted compound name (character) or an unquoted compound ID (numeric)")
}
}
status = httr::status_code(httr::GET(url))
if (status == 200) {
molecule_sdf = read_sdf(url)
} else {
stop(sprintf("Cannot find compound '%s'. Check the compound name or compound ID (CID)", molecule))
}
return(molecule_sdf)
}