3.9: R Assignment
- Page ID
- 281604
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)Standardization
S. Kim, J. Cuadros
November 2nd, 2019
Objectives
- Understand the difference between compounds and substances in PubChem’s terminology.
- Learn how chemical structures are represented in a real world.
- Understand the disambiguity of name-structure associations.
- Learn how to draw chemical structures programmatically.
In this task, we will use some cheminformatics packages to ease some processes. In R, some options are rcdk , ChemmineR and ChemmineOB . In Python, a useful package is RDKit ; in R, we’ll make use of it online version, the Beaker API of ChEMBL (https://chembl.gitbook.io/chembl-interface-documentation/web-services).
Structure Standardization
PubChem contains more than 200 millions chemical records submitted by hundreds of data contributors. These depositor-provided records are archived in a database called “Substance” and each record in this database is called a substance. The records in the Substance database are highly redundant, because different data contributors may submit information on the same chemical, independently of each other. Therefore, PubChem extracts unique chemical structures from the Substance database through a process called standardization (https://doi.org/10.1186/s13321-018-0293-8 ). These unique structures are stored in the Compound database and individaual records in this database is called “compounds”. To learn more about the PubChem compounds and substances, please read this PubChem Blog post (https://go.usa.gov/xVXct (https://go.usa.gov/xVXct)). The code cells below demonstrates the effects of chemical structure standardization.
Step 1. Download a list of the SIDs associated with a given CID First, let’s get a list of SIDs that are associated CID 1174 (uracil).
pugrest <- 'https://pubchem.ncbi.nlm.nih.gov/rest/pug' pugin <- 'compound/cid/1174' pugoper <- 'sids' pugout <- 'txt' url <- paste(pugrest,pugin,pugoper,pugout,sep="/") sids <- readLines(url) length(sids)
The above request returns 300+ substances, all of which are standardized to the same structure (CID 1174).
Step 2. Download the structure data for the SIDs Now retrieve the depositor-provided structures for the returned substances.
chunk_size <- 50 num_chunks <- ceiling(length(sids)/chunk_size) sdf <- character(0) for(i in seq(num_chunks)) { print(paste("Processing chunk", i)) idx1 <- chunk_size * (i - 1) + 1 idx2 <- chunk_size * i str_sids <- paste(sids[idx1:min(idx2,length(sids))], collapse=",") url <- paste("https://pubchem.ncbi.nlm.nih.gov/rest/pug/substance/sid", str_sids, "record/sdf", sep="/") sdf <- c(sdf,readLines(url)) Sys.sleep(0.2) }
writeLines(sdf,"uracil_from_sids.sdf")
Step 3. Convert the structures in the SDF file into the SMILES strings and identify unique SMILES and their
frequencies.
if (!require("BiocManager", quietly=TRUE)) { install.packages("BiocManager", repos="https://cloud.r-project.org/", quiet=TRUE, type="binary") library("BiocManager") } if (!require("ChemmineR", quietly=TRUE)) { BiocManager::install("ChemmineR",ask=FALSE) library("ChemmineR") } if (!require("ChemmineOB", quietly=TRUE)) { BiocManager::install("ChemmineOB",ask=FALSE) library("ChemmineOB") }
convertFormatFile("SDF","CAN","uracil_from_sids.sdf","uracil_from_sids.smi") #convertFormatFile("SDF","CAN","https://chem.libretexts.org/@api/deki/files/346075/uracil_from_sids.sdf?origin=mt-web","uracil_from_sids.smi") smis <- read.table("uracil_from_sids.smi",header=F,sep="\t") tabSMIs <- table(smis[,1]) tabSMIs <- sort(tabSMIs,decreasing = TRUE) tabSMIs
The above output shows that the 300+ SIDs associated with CID 1174 are represented with six different SMILES strings. In addition, some substance records that resulted in an “empty” SMILES strings, implying that the depositors of these substance records did not provide structral information. You may want to what these substances are, but this can be recovered from the SDF file as follows.
uracilSDFset <- read.SDFset("uracil_from_sids.sdf")
nonvalidSDF <- uracilSDFset[!validSDF(uracilSDFset)] unlist(lapply(nonvalidSDF@SDF, function(x) paste(x@datablock["PUBCHEM_SUBSTANCE_ID"], x@datablock["PUBCHEM_SUBS_AUTO_STRUCTURE"], sep=": ")))
Sometimes a data depositor does not provide the structure of a chemical but its chemical synonym(s). In that case, PubChem uses the chemical synonyms to assign a structure to this structure-less record. For example, SID 50608295 (one of the 12 structures without SMILES strings in the above output) did not have a depositorprovided structure, but its depositor-provided synonyms include “CID1174”. Therefore, PubChem assigns SID 50608295 to CID 1174, although the depositor did not provide the structure of SID 50608295. (Please check the structure and synonyms for SID 50608295 stored in the SDF file (“cid2sids-uracil.sdf”) generated in step 2).
Step 4. Generate the structure images from the SMILES
Now we want to see what these SMILES strings look like, by drawing molecular structures from them.
if(!require("httr", quietly=TRUE)) { install.packages("httr", repos="https://cloud.r-project.org/", quiet=TRUE, type="binary") library("httr") } if(!require("png")) { install.packages(("png"), repos="https://cloud.r-project.org/", quiet=TRUE, type="binary") library("png") } if(!require("grid")) { install.packages(("grid"), repos="https://cloud.r-project.org/", quiet=TRUE, type="binary") library("grid") } if(!require("gridExtra")) { install.packages(("gridExtra"), repos="https://cloud.r-project.org/", quiet=TRUE, type="binary") library("gridExtra") }
tabSMIs <- tabSMIs[names(tabSMIs)!=""] names(tabSMIs)
vecSMI <- names(tabSMIs) names(vecSMI) <- names(tabSMIs) sdf <- smiles2sdf(vecSMI) # ChemmineR has a function to print the molecules, but # results are far from acceptable # png("plots.png",width=400,height=2000) # ChemmineR::plot(sdf, griddim=c(ceiling(length((vecSMI))/2),2), # regenCoords=TRUE,atomcex=1 ) # # dev.off() # A better option is to use the ChEMBL Beaker API (RDKit online) write.SDF(sdf,"uracil_unique.sdf") url <- "https://www.ebi.ac.uk/chembl/api/utils/ctab2image?size=300" mydata <- list(sdf=upload_file("uracil_unique.sdf")) res <- POST(url, body = mydata) img <- readPNG(res$content, native=TRUE) grid.arrange(rasterGrob(img)) writePNG(img,"uracil_unique.png")
You may want to write these molecule images in separate files.
dir.create("uracil_unique",showWarnings = FALSE) fileSDF <- write.SDFsplit(sdf,"uracil_unique/",1)$filename url <- "https://www.ebi.ac.uk/chembl/api/utils/ctab2image?size=300" for(i in seq(fileSDF)) { mydata <- list(sdf=upload_file(fileSDF[i])) res <- POST(url, body = mydata) img <- readPNG(res$content, native=TRUE) writePNG(img,paste("uracil_unique/", formatC(i,format="d",width=2,flag="0"),".png",sep="")) Sys.sleep(1) } dir("uracil_unique",pattern=".*[.]png")
As shown these chemical images, the 300+ substances associated with CID 1174 (uracil) correspond to 6 tautomeric forms of uracil, which differ from each other in the position of “movable” hydrogen atoms. Compare these structures with their standardized structure (CID 1174).
img <- readPNG(GET('https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/cid/1174/PNG?image_size=300x300')$content,native=TRUE) grid.arrange(rasterGrob(img))
Exercise 1a: The function used in Step 3 generates the canonical SMILES string by default. OpenBabel supports a second format (“SMI”) that does not use a canonical numbering when creating the SMILES. Other options can be added. Write a code that generates non-canonical SMILES strings for the 300+ substance records associated with uracil (CID 1174).
- Ignore/skip structure-less records using a conditional statement (i.e., an if statement).
- Print the number of unique non-canonical SMILES.
- Print unique non-canonical SMILES, sorted by frequency.
For a given molecule, there may be multiple ways to write SMILES strings: one of them is selected as the “canonical” SMILES and all the others are considered as “non-canonical”. However, for the purpose of this exercise, we want to generate only one non-canonical SMILES for each record (because the function will return only one SMILES string, the canonical SMILES or one of possible non-canonical SMILES).
#Write your code here.
Exercise 1b: NOT INCLUDED IN THE R VERSION
Exercise 1c: Retrieve the substance records associated with guanine (CID 135398634) and display unique structures generated from them, by following these steps:
- Retrieve the SIDs associated CID 135398634
- Download the structure data for the retrieved SIDs (in SDF)
- Generate canonical SMILES strings from the structure data in the SDF file and identify unique canonical SMILES strings
- Draw the structures represented by the unqiue canonical SMILES strings in a single figure.
#Write your code here.
Exercise 1d: Retrieve the substance records whose synonym is “glucose” and display unique structures generated from them, by following these steps: Retrieve the SIDs whose synonym is “glucose”. Download the structure data for the retrieved SIDs (in SDF) Generate canonical SMILES strings from the structure data in the SDF file and identify unique canonical SMILES strings Draw the structures represented by the unique canonical SMILES strings in a single figure.
#Write your code here.
Exercise 1e: Retrieve the compound records associated with the SIDs retrieved in Exercise 1d and display unique structures generated from them, by following these steps: Retrieve the CIDs associated with the SIDs whose name is “glucose”, using a single PUG-REST request
(i.e., using the list conversion covered in the previous activity, “Interconversion between PubChem records”).
- Identify unique CIDs from the returned CIDs.
- Retrieve the isomeric SMILES for the unique CIDs through PUG-REST.
- Draw the structures represented by the returned SMILES strings in a single figure.
#Write your code here.