From f43f7f9dec2337c8db62f0ddb167986af59a033e Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Fri, 13 Mar 2026 13:53:10 +0100 Subject: Explain consequence of chent objects being R6 objects --- R/chent.R | 1 - README.md | 76 +++++++++++++++++++-- README.rmd | 39 ++++++++++- docs/index.html | 61 +++++++++++++++-- docs/index.md | 76 +++++++++++++++++++-- docs/llms.txt | 76 +++++++++++++++++++-- .../reference/figures/README-unnamed-chunk-4-1.png | Bin 15109 -> 6180 bytes .../reference/figures/README-unnamed-chunk-5-1.png | Bin 39123 -> 16259 bytes docs/search.json | 2 +- man/figures/README-unnamed-chunk-4-1.png | Bin 15109 -> 6180 bytes man/figures/README-unnamed-chunk-5-1.png | Bin 39123 -> 16259 bytes 11 files changed, 306 insertions(+), 25 deletions(-) diff --git a/R/chent.R b/R/chent.R index c118558..7b47f41 100644 --- a/R/chent.R +++ b/R/chent.R @@ -196,7 +196,6 @@ chent <- R6Class("chent", attr(self$inchikey, "source") <- "pubchem" } else { if (is.na(self$inchikey)) { - warning("Overwriting uninitialized InChIKey") self$inchikey <- self$pubchem$InChIKey attr(self$inchikey, "source") <- "pubchem" } else { diff --git a/README.md b/README.md index bbf4ae9..0bdc255 100644 --- a/README.md +++ b/README.md @@ -22,8 +22,8 @@ using the [webchem](https://docs.ropensci.org/webchem/) package. ``` r library(chents) -caffeine <- chent$new("caffeine") -#> Querying PubChem for name caffeine ... +caffeine <- chent$new("Caffeine") +#> Querying PubChem for name Caffeine ... #> Get chemical information from RDKit using PubChem SMILES #> CN1C=NC2=C1C(=O)N(C(=O)N2C)C ``` @@ -39,7 +39,7 @@ collected. ``` r print(caffeine) #> -#> Identifier $identifier caffeine +#> Identifier $identifier Caffeine #> InChI Key $inchikey RYYVLZVUVIJVGH-UHFFFAOYSA-N #> SMILES string $smiles: #> PubChem @@ -67,8 +67,8 @@ which starts with querying the [BCPC compendium](http://www.bcpcpesticidecompendium.org/) first. ``` r -delta <- pai$new("deltamethrin") -#> Querying BCPC for deltamethrin ... +delta <- pai$new("Deltamethrin") +#> Querying BCPC for Deltamethrin ... #> Querying PubChem for inchikey OWZREIFADZCYQD-NSHGMRRFSA-N ... #> Get chemical information from RDKit using PubChem SMILES #> CC1([C@H]([C@H]1C(=O)O[C@H](C#N)C2=CC(=CC=C2)OC3=CC=CC=C3)C=C(Br)Br)C @@ -116,3 +116,69 @@ RDKit installed in the system location. ``` r Sys.setenv(RETICULATE_PYTHON="/usr/bin/python3") ``` + +## Using R6 classes + +Note that the `chent` objects defined by this package are +[R6](https://r6.r-lib.org/articles/Introduction.html) classes. +Therefore, if you think you make a copy by assigning them to a new name, +the objects will still be connected, because only the reference is +copied. For example, you can create a molecule without retrieving data +from PubChem + +``` r +but <- chent$new("Butane", smiles = "CCCC", pubchem = FALSE) +#> Get chemical information from RDKit using user SMILES +#> CCCC +print(but) +#> +#> Identifier $identifier Butane +#> InChI Key $inchikey NA +#> SMILES string $smiles: +#> user +#> "CCCC" +#> Molecular weight $mw: 58.1 +``` + +If you then assign a new name and add PubChem information to the object +with the new name, the information will also be added to the original +`chent` object: + +``` r +but_pubchem <- but +but_pubchem$try_pubchem() +#> Querying PubChem for name Butane ... +print(but) +#> +#> Identifier $identifier Butane +#> InChI Key $inchikey IJDNQMDRQITEOD-UHFFFAOYSA-N +#> SMILES string $smiles: +#> user PubChem +#> "CCCC" "CCCC" +#> Molecular weight $mw: 58.1 +#> PubChem synonyms (up to 10): +#> [1] "BUTANE" "n-Butane" "106-97-8" +#> [4] "Diethyl" "Methylethylmethane" "Butanen" +#> [7] "Butani" "Butyl hydride" "HC 600" +#> [10] "A 21 (lowing agent)" +``` + +You can create a derived, independent object using the `clone()` method +that will not be affectd by operations on the original object: + +``` r +but_new <- chent$new("Butane", smiles = "CCCC", pubchem = FALSE) +#> Get chemical information from RDKit using user SMILES +#> CCCC +but_clone <- but_new$clone() +but_new$try_pubchem() +#> Querying PubChem for name Butane ... +but_clone +#> +#> Identifier $identifier Butane +#> InChI Key $inchikey NA +#> SMILES string $smiles: +#> user +#> "CCCC" +#> Molecular weight $mw: 58.1 +``` diff --git a/README.rmd b/README.rmd index b88c22f..ffee832 100644 --- a/README.rmd +++ b/README.rmd @@ -20,14 +20,13 @@ provides a way to define and check the identity of chemically defined substances ("chemical entities") and to collect related information. - When first defining a chemical entity, some chemical information is retrieved from the [PubChem](https://pubchem.ncbi.nlm.nih.gov/) website using the [webchem](https://docs.ropensci.org/webchem/) package. ```{r} library(chents) -caffeine <- chent$new("caffeine") +caffeine <- chent$new("Caffeine") ``` If Python and [RDKit](https://rdkit.org) (> 2015.03) are installed and @@ -52,7 +51,7 @@ can use the 'pai' class derived from the 'chent' class, which starts with queryi the [BCPC compendium](http://www.bcpcpesticidecompendium.org/) first. ```{r fig.height = 3.5} -delta <- pai$new("deltamethrin") +delta <- pai$new("Deltamethrin") plot(delta) ``` @@ -96,3 +95,37 @@ location. Sys.setenv(RETICULATE_PYTHON="/usr/bin/python3") ``` +## Using R6 classes + +Note that the `chent` objects defined by this package are [R6](https://r6.r-lib.org/articles/Introduction.html) +classes. Therefore, if you think you make a copy by assigning them to a new name, the +objects will still be connected, because only the reference is copied. For +example, you can create a molecule without retrieving data from PubChem + +```{r} +but <- chent$new("Butane", smiles = "CCCC", pubchem = FALSE) +print(but) +``` + +If you then assign a new name and add PubChem information to the object with +the new name, the information will also be added to the original `chent` +object: + + +```{r} +but_pubchem <- but +but_pubchem$try_pubchem() +print(but) +``` + +You can create a derived, independent object using the `clone()` method +that will not be affectd by operations on the original object: + +```{r} +but_new <- chent$new("Butane", smiles = "CCCC", pubchem = FALSE) +but_clone <- but_new$clone() +but_new$try_pubchem() +but_clone +``` + + diff --git a/docs/index.html b/docs/index.html index ec6ba42..aa6d358 100644 --- a/docs/index.html +++ b/docs/index.html @@ -54,8 +54,8 @@

When first defining a chemical entity, some chemical information is retrieved from the PubChem website using the webchem package.

 library(chents)
-caffeine <- chent$new("caffeine")
-#> Querying PubChem for name caffeine ...
+caffeine <- chent$new("Caffeine")
+#> Querying PubChem for name Caffeine ...
 #> Get chemical information from RDKit using PubChem SMILES
 #> CN1C=NC2=C1C(=O)N(C(=O)N2C)C

If Python and RDKit (> 2015.03) are installed and configured for use with the reticulate package, some additional chemical information including a 2D graph are computed.

@@ -63,7 +63,7 @@
 print(caffeine)
 #> <chent>
-#> Identifier $identifier caffeine 
+#> Identifier $identifier Caffeine 
 #> InChI Key $inchikey RYYVLZVUVIJVGH-UHFFFAOYSA-N 
 #> SMILES string $smiles:
 #>                        PubChem 
@@ -81,8 +81,8 @@
 

If you have a so-called ISO common name of a pesticide active ingredient, you can use the ‘pai’ class derived from the ‘chent’ class, which starts with querying the BCPC compendium first.

-delta <- pai$new("deltamethrin")
-#> Querying BCPC for deltamethrin ...
+delta <- pai$new("Deltamethrin")
+#> Querying BCPC for Deltamethrin ...
 #> Querying PubChem for inchikey OWZREIFADZCYQD-NSHGMRRFSA-N ...
 #> Get chemical information from RDKit using PubChem SMILES
 #> CC1([C@H]([C@H]1C(=O)O[C@H](C#N)C2=CC(=CC=C2)OC3=CC=CC=C3)C=C(Br)Br)C
@@ -107,6 +107,57 @@
 
 Sys.setenv(RETICULATE_PYTHON="/usr/bin/python3")
+
+

Using R6 classes +

+

Note that the chent objects defined by this package are R6 classes. Therefore, if you think you make a copy by assigning them to a new name, the objects will still be connected, because only the reference is copied. For example, you can create a molecule without retrieving data from PubChem

+
+but <- chent$new("Butane", smiles = "CCCC", pubchem = FALSE)
+#> Get chemical information from RDKit using user SMILES
+#> CCCC
+print(but)
+#> <chent>
+#> Identifier $identifier Butane 
+#> InChI Key $inchikey NA 
+#> SMILES string $smiles:
+#>   user 
+#> "CCCC" 
+#> Molecular weight $mw: 58.1
+

If you then assign a new name and add PubChem information to the object with the new name, the information will also be added to the original chent object:

+
+but_pubchem <- but
+but_pubchem$try_pubchem()
+#> Querying PubChem for name Butane ...
+print(but)
+#> <chent>
+#> Identifier $identifier Butane 
+#> InChI Key $inchikey IJDNQMDRQITEOD-UHFFFAOYSA-N 
+#> SMILES string $smiles:
+#>    user PubChem 
+#>  "CCCC"  "CCCC" 
+#> Molecular weight $mw: 58.1 
+#> PubChem synonyms (up to 10):
+#>  [1] "BUTANE"              "n-Butane"            "106-97-8"           
+#>  [4] "Diethyl"             "Methylethylmethane"  "Butanen"            
+#>  [7] "Butani"              "Butyl hydride"       "HC 600"             
+#> [10] "A 21 (lowing agent)"
+

You can create a derived, independent object using the clone() method that will not be affectd by operations on the original object:

+
+but_new <- chent$new("Butane", smiles = "CCCC", pubchem = FALSE)
+#> Get chemical information from RDKit using user SMILES
+#> CCCC
+but_clone <- but_new$clone()
+but_new$try_pubchem()
+#> Querying PubChem for name Butane ...
+but_clone
+#> <chent>
+#> Identifier $identifier Butane 
+#> InChI Key $inchikey NA 
+#> SMILES string $smiles:
+#>   user 
+#> "CCCC" 
+#> Molecular weight $mw: 58.1
+