Skip to content

Instantly share code, notes, and snippets.

@vjcitn
Created September 6, 2023 13:55
Show Gist options
  • Save vjcitn/cb316b89d08be63ebc9e3c9194e17b36 to your computer and use it in GitHub Desktop.
Save vjcitn/cb316b89d08be63ebc9e3c9194e17b36 to your computer and use it in GitHub Desktop.
import semantic SQL content to ontology_index
#' produce an ontology_index instance from semantic sql sqlite connection
#' @param con DBI::dbConnect value for sqlite table
#' @return result of ontologyIndex::ontology_index evaluated for the labels and
#' parent-child relations in tables statements and edge of the semantic sql resource
#' @export
semsql_to_oi = function(con) {
# sqlite> select * from statements where predicate = 'rdfs:label' limit 40;
alltabs = DBI::dbListTables(con)
stopifnot(all(c("edge", "statements") %in% alltabs))
labdf = dplyr::tbl(con, "statements") |> dplyr::filter(predicate == "rdfs:label") |>
as.data.frame()
edgdf = dplyr::tbl(con, "edge") |> dplyr::filter(predicate == "rdfs:subClassOf") |>
as.data.frame()
nn = split(labdf$value, labdf$subject) # value!!
pl = split(edgdf$object, edgdf$subject)
okn = intersect(names(nn), names(pl))
pl = pl[okn]
nn = nn[okn]
ontologyIndex::ontology_index(name=nn, parents=pl)
}
@vjcitn
Copy link
Author

vjcitn commented Sep 6, 2023

https://github.com/INCATools/semantic-sql indicates how to acquire, e.g., hp.db

get efo.db by the same means

in R, use the semsql_to_oi function on dbConnect(SQLite(), "efo.db") to produce efoo

verify that this plot looks right:

onto_plot2(efoo, c(unlist(efoo$ancestors[c("EFO:0004997", "HP:0012594", "EFO:0004518")])))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment