Skip to contents

Practical recipes. Each one is a short, real task. For the full list of sources and a valid example id for each, see the sources cookbook.

Clean a column of identifiers

Split a vector into the ones that pass and the ones that do not, keeping order.

ids <- c("MONDO:0005148", "mondo:5148", "MONDO:0018076", "banana")
ok <- is_valid_id(ids, source_db = "mondo")

ids[ok] # valid
#> [1] "MONDO:0005148" "MONDO:0018076"
ids[!ok] # invalid
#> [1] "mondo:5148" "banana"

Repair what can be repaired

check_id() returns a suggestion for an input that is invalid but mappable, such as a lowercase prefix. Use it to fix a column instead of dropping it.

res <- check_id(ids, source_db = "mondo")
res$fixed <- ifelse(res$valid, res$normalized, res$suggestion)
res[c("input", "valid", "fixed")]
#> # A tibble: 4 × 3
#>   input         valid fixed        
#>   <chr>         <lgl> <chr>        
#> 1 MONDO:0005148 TRUE  MONDO:0005148
#> 2 mondo:5148    FALSE MONDO:0005148
#> 3 MONDO:0018076 TRUE  MONDO:0018076
#> 4 banana        FALSE NA

banana has no repair, so its fixed value is NA.

Existence, not just shape

pattern mode only checks the shape. To check that an id actually exists, use a pinned snapshot (cache) or the live source (remote).

# offline, against the sample snapshot that ships with the package
is_valid_id("MONDO:9999999", source_db = "mondo", how = "cache", version = "sample")
#> [1] FALSE

MONDO:9999999 is well-formed, so it passes pattern, but it is not in the snapshot. The live check needs a network, so it is shown but not run here:

is_valid_id("ENSG00000139618", source_db = "ensembl", how = "remote")

A failed remote lookup raises an error. It never returns a silent FALSE, so a network problem cannot be mistaken for an absent id.

Species context

For species-aware sources, pass species. For Ensembl the species is encoded in the id, so even pattern mode can reject a well-formed id from the wrong species.

is_valid_id("ENSMUSG00000059552", source_db = "ensembl", species = "mus_musculus")
#> [1] TRUE
is_valid_id("ENSMUSG00000059552", source_db = "ensembl", species = "homo_sapiens")
#> [1] FALSE

Guard a pipeline with checkmate

The checkmate-style adapters check, assert, or test a vector against a source.

# a friendly message instead of TRUE when something is off
check_valid_id(c("MONDO:0005148", "mondo:5148"), "mondo")
#> [1] "Must be valid mondo identifiers (pattern mode), but 1 of 2 failed, for example 'mondo:5148'"

# a single logical, handy in `if` and `stopifnot`
test_valid_id("MONDO:0005148", "mondo")
#> [1] TRUE

Validate a data frame with assertr or validate

id_predicate() returns an elementwise predicate, which is what data-frame validators expect.

is_mondo <- id_predicate("mondo")
ids[is_mondo(ids)]
#> [1] "MONDO:0005148" "MONDO:0018076"

Drop it straight into a pipeline. These need the assertr or validate package, so they are shown but not run here:

# assertr, inside a dplyr pipeline
df |> assertr::assert(id_predicate("mondo"), term)

# validate
rules <- validate::validator(good_terms = id_predicate("mondo")(term))
validate::confront(df, rules)

Validate a data frame with pointblank

pointblank agents run an expression per column. is_valid_id() is exactly the right shape, so it drops into a col_vals_expr() step. This needs the pointblank package, so it is shown but not run here:

library(pointblank)

create_agent(df) |>
  col_vals_expr(~ is_valid_id(term, "mondo")) |>
  interrogate()

Validate a Shiny input

sv_biobouncer() returns a shinyvalidate rule. It returns NULL for a valid input and a message otherwise.

rule <- sv_biobouncer("mondo")
rule("MONDO:0005148") # NULL, valid
#> NULL
rule("mondo:5148") # a message
#> [1] "Not a valid mondo identifier"

Discover sources in code

You never need to hard-code a key or guess an example. source_info() lists every source with a valid example and its supported modes.

source_info()
#> # A tibble: 46 × 6
#>    key           name                  example modes species_aware version_aware
#>    <chr>         <chr>                 <chr>   <chr> <lgl>         <lgl>        
#>  1 bto           BRENDA Tissue Ontolo… BTO:00… patt… FALSE         TRUE         
#>  2 cdd           CDD                   cd00029 patt… FALSE         FALSE        
#>  3 chebi         ChEBI                 CHEBI:… patt… FALSE         TRUE         
#>  4 chembl        ChEMBL                CHEMBL… patt… FALSE         FALSE        
#>  5 cl            Cell Ontology         CL:000… patt… FALSE         TRUE         
#>  6 clinvar       ClinVar               VCV000… patt… FALSE         FALSE        
#>  7 complexportal Complex Portal        CPX-21… patt… FALSE         FALSE        
#>  8 cosmic        COSMIC                COSM476 patt… FALSE         FALSE        
#>  9 dbsnp         dbSNP                 rs7412  patt… FALSE         FALSE        
#> 10 doid          Human Disease Ontolo… DOID:9… patt… FALSE         TRUE         
#> # ℹ 36 more rows