For each term, get its ancestor at a given level and add the ID and name of the ancestor to the ontology metadata. By default, lower numbers are higher in the ontology (e.g. level 0 includes the most broad ontology term). When a terms does not have an ancestor at a given level (e.g. lvl=3 but the term itself is at level 1) the term is assigned to itself as its own ancestor. When a term has multiple ancestors at a given level, the first ancestor is arbitrarily chosen.

add_ancestors(
  ont,
  lvl = 2,
  include_self = TRUE,
  prefix = "ancestor",
  fill_na = TRUE,
  i = 1,
  force_new = FALSE
)

Arguments

ont

An ontology of class ontology_DAG.

lvl

Depth of the ancestor terms to add. Will get the closest ancestor to this level if none have this exact level.

include_self

For dag_offspring() and dag_ancestors(), this controls whether to also include the query term itself.

prefix

A prefix for the ancestor column names.

fill_na

If TRUE, fill ancestor columns with NA values using the term ID and name.

i

Index of ancestor to use when more than one is available.

force_new

Add the data again even if the associated column already exists.

Examples

ont <- get_ontology("hp")
#> ⠙ Iterating 9 done (4.2/s) | 2.2s
#> ⠹ Iterating 14 done (4.6/s) | 3s
#> Loading required namespace: piggyback
#> Preparing ontology_index object from: https://github.com/obophenotype/human-phenotype-ontology/releases/download/v2024-12-12/hp.obo
#> Parsing [Typedef] sections in the obo file [3/3]
#> Parsing [Term] sections in the obo file [1000/19475]
#> Parsing [Term] sections in the obo file [2000/19475]
#> Parsing [Term] sections in the obo file [3000/19475]
#> Parsing [Term] sections in the obo file [4000/19475]
#> Parsing [Term] sections in the obo file [5000/19475]
#> Parsing [Term] sections in the obo file [6000/19475]
#> Parsing [Term] sections in the obo file [7000/19475]
#> Parsing [Term] sections in the obo file [8000/19475]
#> Parsing [Term] sections in the obo file [9000/19475]
#> Parsing [Term] sections in the obo file [10000/19475]
#> Parsing [Term] sections in the obo file [11000/19475]
#> Parsing [Term] sections in the obo file [12000/19475]
#> Parsing [Term] sections in the obo file [13000/19475]
#> Parsing [Term] sections in the obo file [14000/19475]
#> Parsing [Term] sections in the obo file [15000/19475]
#> Parsing [Term] sections in the obo file [16000/19475]
#> Parsing [Term] sections in the obo file [17000/19475]
#> Parsing [Term] sections in the obo file [18000/19475]
#> Parsing [Term] sections in the obo file [19000/19475]
#> Parsing [Term] sections in the obo file [19475/19475]
#> remove 450 obsolete terms
#> Adding term metadata.
#> IC_method: IC_offspring
#> Adding ancestor metadata.
#> Getting absolute ontology level for 19,025 IDs.
#> 79 ancestors found at level 2
#> Translating ontology terms to names.
#> Translating ontology terms to ids.
#> Converted ontology to: adjacency 
#> Getting absolute ontology level for 19,025 IDs.
#> Saving ontology --> /github/home/.cache/R/KGExplorer/hp.rds
ont2 <- add_ancestors(ont, lvl=4)
#> Adding ancestor metadata.
#> Ancestor metadata already present. Use force_new=TRUE to overwrite.