Creating a new field for entries. (original) (raw)

Introduction

In this vignette we will focus on creating a new biodb field to be used inside an existing connector.biodb fields are defined for all database connectors. They are definitions of what types of data may be set inside biodb entry objects. Since they are shared by all connectors, they need to be defined without any reference to a particular database. However many of them are linked to a particular science or technological domain (genetics, metabolomics, mass spectrometry, …).

An entry field is like a type definition. The definition is done at the top-level of biodb, and thus it not related to any particular connector. The definition includes: a name, a description, a class (integer, double, character, logical), a cardinality (single value or vector), a list of allowed values, a class (to group similar fields like “mass”), etc.

For a particular connector, when an entry object is created in memory, a file containing the values is obtained from the database and a parsing is run in order to extract those values and affect them to associated biodb entry fields inside the biodb entry object. Thus the parsing of the value of a biodb entry field is different for each connector, while the biodb entry field is used by several different connectors.

No biodb connector use all available biodb entry fields. However it can happen that a connector does not implement the parsing of some available data inside a database. The reason is that, in most cases, the amount of available data, and the diversity of it, inside a single entry would require an excessive amount of coding. As a consequence, we often restrict our development onto a subset of the available data, in which we are interested.

When one particular data from the database is not present inside the entries of the corresponding biodb connector, this means that no parsing has been written for it inside the connector. Moreover it could also mean that no biodb entry field is defined to handled this particular type of data. Fortunately, biodb offers you a way to correct dynamically, inside your code, this shortage, creating a new biodb entry field if necessary and creating the corresponding parsing of the data for the connector.

Follow the subsequent explanations in order to learn how to define a new parsing of a value for a connector and assign it to an existing entry field, and how to define a new entry field.

First we instantiate the package:

mybiodb <- biodb::BiodbMain$new()
## INFO  [17:32:39.455] Loading definitions from package biodb version 1.16.0.

Defining a new parsing of a field

Before going with the creation of a new field, we will look at different ways of parsing a value for an existing biodb field that is not handled by a connector. Two connector cases will be used as examples: the ChebiExConn connector defined for theCreating a new connector.vignette and the CompCsvFileConn connector from the biodb package.

Defining a parsing expression for a remote database connector (ChebiExConn)

The ChebiExConn class implements an example connector to the ChEBI (Hastings et al. 2012) remote database. See vignetteCreating a new connector.for the creation of this connector.

We load dynamically the definition of this connector inside biodb as explained in theCreating a new connector.vignette:

chebiexDefFile <- system.file("extdata", "chebi_ex.yml", package='biodb')
connClass <- system.file("extdata", "ChebiExConn.R", package='biodb')
entryClass <- system.file("extdata", "ChebiExEntry.R", package='biodb')
source(connClass)
source(entryClass)
mybiodb$loadDefinitions(chebiexDefFile)

For our demonstration we will suppose this connector has been created by somebody else, and we have no access to the implementation code.

We create a connector to this database:

conn <- mybiodb$getFactory()$createConn('chebi.ex')

And get one entry:

entryIds <- c('17001', '40304', '64679')
entriesDf <- mybiodb$entriesToDataframe(conn$getEntry(entryIds))

That you can see in table 1.

You will notice that no electrical charge is mentioned for the molecules in the table, while it is present inside ChEBI database. Let us choose one of the entries:

id <- entryIds[[1]]
id
## [1] "17001"

And get the ChEBI web page of this entry:

conn$getEntryPageUrl(id)
##                                                   17001 
## "https://www.ebi.ac.uk/chebi/searchId.do?chebiId=17001"

Go on this page (https://www.ebi.ac.uk/chebi/searchId.do?chebiId=17001) to check that the electrical charge information is indeed given by ChEBI (Net Charge 0). To integrate this data inside the biodb entry, we need to extract it from the file returned by ChEBI. When asked for an entry on its web service interface, ChEBI returns an XML file that biodb stores in its cache. By calling the following method on your connector, you can get the path to the biodb cache file:

conn$getCacheFile(id)
## [1] "/home/biocbuild/.cache/R/biodb/chebi.ex-0c5076ac2a43d16dbce503a44b09f649/17001.xml"

If you take a look to this file with your favourite editor, you will see the following text:

<charge>0</charge>

This the XML tag that stores the value of the electrical charge. To extract values from XML, biodb uses the XPath query language. In XPath language, the expression //chebi:charge means to get the value inside the charge tag wherever it is (//) inside the tree structure of the XML. See XPath Tutorial for an introduction to XPath. We need to give this XPath expression to the biodb instance, and explain to which entry field the extracted value must be affected. This is done by defining a small YAML file:

chargeParsingDefFile <- system.file("extdata", "chebi_ex_charge_parsing.yml", package='biodb')

Whose content is as follow:

databases:
  chebi.ex:
    parsing.expr:
      charge: //chebi:charge

In this file we define a new parsing expression inside the parsing.expr section for the chebi.ex database connector. The definition of the parsing expression consists of two values: the targeted biodb entry field (charge) and the XPath expression (//chebi:charge).

Now we just have to load this new definition:

mybiodb$loadDefinitions(chargeParsingDefFile)

Delete the existing connector:

mybiodb$getFactory()$deleteConn(conn)
## INFO  [17:32:42.001] Connector "chebi.ex" deleted.

Recreate the connector and reload the same entries:

conn <- mybiodb$getFactory()$createConn('chebi.ex')
entriesDf <- mybiodb$entriesToDataframe(conn$getEntry(entryIds))

You can see in 2 that the electrical charge is now indicated for each entry.

Defining a parsing expression for a local database connector (CompCsvFileConn)

The CompCsvFileConn class implements a connector to a local CSV file database of chemical compounds, as explained inside vignette

. For a database stored inside a CSV file, the data parsing is very simple. It consists in associating each biodb entry field with a column name. By default biodb will define associations for each entry field whose name is used for a column. The columns whose names are not the names of existing biodb entry fields are not associated and thus you cannot access their values from biodb.

If you want to access those values, you have the define manually the associations, using the setField() method.

For our example we use an extract from ChEBI database as the input CSV database file:

fileUrl <- system.file("extdata", "chebi_extract_with_unknown_column.tsv", package='biodb')

See table 3 for the content of this file.

In this file, the column name elecCharge will not be associated to any_biodb_ entry field. Indeed, the biodb entry field the electrical charge of a molecule ischarge, not elecCharge. Let us verify that.

We first create the connector to this CSV file:

conn <- mybiodb$getFactory()$createConn('comp.csv.file', url=fileUrl)

And get the content of some of the entries:

entriesDf <- mybiodb$entriesToDataframe(conn$getEntry(conn$getEntryIds()))
## INFO  [17:32:42.358] Loading file database "/tmp/RtmppkRxLT/Rinst14543835500b25/biodb/extdata/chebi_extract_with_unknown_column.tsv".
## WARN  [17:32:42.361] Column "elecCharge" does not match any biodb field.
## Warning in warn("Column \"%s\" does not match any biodb field.", colname):
## Column "elecCharge" does not match any biodb field.

See table 4 for the content of this entry. As you can see, no charge field is listed.

Now we call the method to define the new association:

conn$setField('charge', 'elecCharge')

The first parameter is the name of the biodb entry field, the second the name of the column inside the CSV file

The new column will now be parsed when getting the entry. But before we must remove all entries from memory:

conn$deleteAllEntriesFromVolatileCache()

And then reload the same entries again:

entries2Df <- mybiodb$entriesToDataframe(conn$getEntry(conn$getEntryIds()))

See table 5 for the content of this entry. A new data frame column is present, named charge.

Creating a new field and parsing its value

Sometimes you just do not need to parse some value for setting an existing_biodb_ field, but you need to get a value that does not correspond to any defined biodb field. In this case, you need to define a new field alongside defining your parsing.

For this demonstration we will use again the ChebiExConn connector example from theCreating a new connector.vignette.

In the ChEBI database, each entry (i.e.: molecule) gets a score (a number of stars) reflecting its curation status. This field is not present inside the current ChebiExConn connector example. Let us see that by displaying the content of some entries:

conn <- mybiodb$getFactory()$getConn('chebi.ex')
entryIds <- c('17001', '40304', '64679')
entriesDf <- mybiodb$entriesToDataframe(conn$getEntry(entryIds))

See table 6.

In the XML entry content returned by the ChEBI server, this field is stored inside the entityStar element as shown here:

<entityStar>3</entityStar>

You can check that directly inside the XML content of one of the entries, as explained earlier.

To get this number of stars we define the new field and its parsing expression inside the following YAML file:

nStarsDefFile <- system.file("extdata", "chebi_ex_stars_field.yml", package='biodb')

Here is its content:

databases:
  chebi.ex:
    parsing.expr:
      n_stars: //chebi:return/chebi:entityStar

fields:
  n_stars:
    description: The ChEBI example stars indicator.
    class: integer

You already know how to define the parsing expression inside the YAML file The value of the XPath expression is a bit longer than for the electrical charge, but the principle is the same.

What is new, is the fields section, in which we define the new fields. The name of the field (n_stars) is used as a key inside the section. Then several keys are used to define the field, see table7 for a description of those keys.

We can now load the new definition:

mybiodb$loadDefinitions(nStarsDefFile)

Delete the existing connector:

mybiodb$getFactory()$deleteConn(conn)
## INFO  [17:32:43.989] Connector "chebi.ex" deleted.

Recreate the connector and reload the same entries:

conn <- mybiodb$getFactory()$createConn('chebi.ex')
entriesDf <- mybiodb$entriesToDataframe(conn$getEntry(entryIds))

See table 8. Now a column named n_stars indicates the number of stars for each entry in the data frame.

Closing biodb instance

Do not forget to terminate your biodb instance once you are done with it:

mybiodb$terminate()
## INFO  [17:32:44.231] Closing BiodbMain instance...
## INFO  [17:32:44.232] Connector "comp.csv.file" deleted.
## INFO  [17:32:44.234] Connector "chebi.ex" deleted.

Session information

sessionInfo()
## R version 4.5.0 RC (2025-04-04 r88126)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 24.04.2 LTS
## 
## Matrix products: default
## BLAS:   /home/biocbuild/bbs-3.21-bioc/R/lib/libRblas.so 
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.12.0  LAPACK version 3.12.0
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_GB              LC_COLLATE=C              
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## time zone: America/New_York
## tzcode source: system (glibc)
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] biodb_1.16.0     BiocStyle_2.36.0
## 
## loaded via a namespace (and not attached):
##  [1] rappdirs_0.3.3       sass_0.4.10          generics_0.1.3      
##  [4] bitops_1.0-9         stringi_1.8.7        RSQLite_2.3.9       
##  [7] hms_1.1.3            digest_0.6.37        magrittr_2.0.3      
## [10] evaluate_1.0.3       bookdown_0.43        fastmap_1.2.0       
## [13] blob_1.2.4           plyr_1.8.9           jsonlite_2.0.0      
## [16] progress_1.2.3       DBI_1.2.3            BiocManager_1.30.25 
## [19] httr_1.4.7           XML_3.99-0.18        jquerylib_0.1.4     
## [22] cli_3.6.4            rlang_1.1.6          chk_0.10.0          
## [25] crayon_1.5.3         dbplyr_2.5.0         bit64_4.6.0-1       
## [28] withr_3.0.2          cachem_1.1.0         yaml_2.3.10         
## [31] tools_4.5.0          memoise_2.0.1        dplyr_1.1.4         
## [34] filelock_1.0.3       curl_6.2.2           vctrs_0.6.5         
## [37] R6_2.6.1             BiocFileCache_2.16.0 lifecycle_1.0.4     
## [40] stringr_1.5.1        bit_4.6.0            pkgconfig_2.0.3     
## [43] pillar_1.10.2        bslib_0.9.0          glue_1.8.0          
## [46] Rcpp_1.0.14          lgr_0.4.4            xfun_0.52           
## [49] tibble_3.2.1         tidyselect_1.2.1     knitr_1.50          
## [52] htmltools_0.5.8.1    rmarkdown_2.29       compiler_4.5.0      
## [55] prettyunits_1.2.0    askpass_1.2.1        RCurl_1.98-1.17     
## [58] openssl_2.3.2