Read an owl file in R as dataframe

59 views Asked by At

How is it possible to read an owl file in R and create a dataframe which will have in one column the value of text and in a second column the type of this text in the ontology for example, class, subclass, data property, object property etc. Here is the ontology I try to use.

Here a random example of the process:

library(rdflib)

# Read the UCO OWL file
file_path <- "C:/Users/User/Desktop/uco_1_5.owl"
graph <- owl_read(file_path)

# Function to extract triples from graph and create dataframe
extract_data <- function(graph) {
  triples <- as.data.frame(as.data.frame(graph))
  colnames(triples) <- c("subject", "predicate", "object")
  return(triples)
}

# Extract triples from graph
triples_df <- extract_data(graph)

Function to classify types
classify_type <- function(x) {
  if (x %in% c("rdf:type", "rdfs:subClassOf")) {
    return("Class")
  }
  else if (x %in% c("rdf:Property", "owl:ObjectProperty", "owl:DatatypeProperty", "owl:AnnotationProperty")) {
    return("Property")
  } else if (x %in% c("owl:Class")) {
    return("Class")
  } else {
    return(NA)
  }
}

# Add a new column to the dataframe indicating the type of property
triples_df$type <- sapply(triples_df$predicate, classify_type)

# Filter out NA values
triples_df <- na.omit(triples_df)

# Keep only the necessary columns
result_df <- triples_df[, c("type")]

# Display the result dataframe
print(result_df)

have a dataframe with the value of the ontology

0

There are 0 answers