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 8 done (3.9/s) | 2.1s
#> ⠹ iterating 14 done (4.9/s) | 2.8s
#> Importing ontology via GitHub.
#> Loading required namespace: piggyback
#> Identifying latest release for: obophenotype/human-phenotype-ontology
#> Preparing ontology_index object from: https://github.com/obophenotype/human-phenotype-ontology/releases/download/v2025-05-06/hp.obo
#> Parsing [Typedef] sections in the obo file [3/3]
#> Parsing [Term] sections in the obo file [1000/19650]
#> Parsing [Term] sections in the obo file [2000/19650]
#> Parsing [Term] sections in the obo file [3000/19650]
#> Parsing [Term] sections in the obo file [4000/19650]
#> Parsing [Term] sections in the obo file [5000/19650]
#> Parsing [Term] sections in the obo file [6000/19650]
#> Parsing [Term] sections in the obo file [7000/19650]
#> Parsing [Term] sections in the obo file [8000/19650]
#> Parsing [Term] sections in the obo file [9000/19650]
#> Parsing [Term] sections in the obo file [10000/19650]
#> Parsing [Term] sections in the obo file [11000/19650]
#> Parsing [Term] sections in the obo file [12000/19650]
#> Parsing [Term] sections in the obo file [13000/19650]
#> Parsing [Term] sections in the obo file [14000/19650]
#> Parsing [Term] sections in the obo file [15000/19650]
#> Parsing [Term] sections in the obo file [16000/19650]
#> Parsing [Term] sections in the obo file [17000/19650]
#> Parsing [Term] sections in the obo file [18000/19650]
#> Parsing [Term] sections in the obo file [19000/19650]
#> Parsing [Term] sections in the obo file [19650/19650]
#> remove 473 obsolete terms
#> Adding term metadata.
#> IC_method: IC_offspring
#> Adding ancestor metadata.
#> Getting absolute ontology level for 19,177 IDs.
#> 82 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,177 IDs.
#> Caching file --> /github/home/.cache/R/KGExplorer/ontologies/github/hp_v2025-05-06.rds
ont2 <- add_ancestors(ont, lvl=4)
#> Adding ancestor metadata.
#> Ancestor metadata already present. Use force_new=TRUE to overwrite.