-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathontology.R
More file actions
106 lines (90 loc) · 2.94 KB
/
ontology.R
File metadata and controls
106 lines (90 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#' Get an owl file from github
#'
#' @param ontology_name the name of the onotology to read; one of mosaic or ecso
#'
#' @return list
#' @export
#'
#' @examples
#' read_ontology("mosaic")
#' read_ontology("ecso")
read_ontology <- function(ontology_name) {
# get the owl file from github
if (ontology_name == "mosaic") {
mosaic_url <-
"https://raw.githubusercontent.com/DataONEorg/sem-prov-ontologies/main/MOSAiC/MOSAiC.owl"
mosaic <- rdflib::rdf_parse(pins::pin(mosaic_url),
format = "rdfxml")
} else if (ontology_name == "ecso") {
mosaic_url <-
"https://raw.githubusercontent.com/DataONEorg/sem-prov-ontologies/ECSO8-add_non-carbon_measurements/observation/ECSO8.owl"
mosaic <- rdflib::rdf_parse(pins::pin(mosaic_url),
format = "rdfxml")
}
}
#' Gets all the concepts
#'
#' Takes an ontology and returns a dataframe with all the URIs and labels.
#' This is mainly used for MOSAiC because the ontology is modeled differently
#'
#' @param ontology (list) the list form of a OWL file
#'
#' @return dataframe
#' @export
#'
#' @examples
#'
#' mosaic <- read_ontology("mosaic")
#' get_ontology_concepts(mosaic)
get_ontology_concepts <- function(ontology) {
#find the class IRI
query_class <-
'PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select distinct ?label ?Concept
where {
[] a ?Concept .
?Concept rdfs:label ?label .}'
suppressMessages(rdflib::rdf_query(ontology, query_class))
}
#' Given a an annotation from the ECSO ontology, produce the corresponding annotation
#'
#' Reduces the amount of copy pasting needed
#'
#' @param valueLabel (character) the label for the annotation found in
#' [ECSO](https://bioportal.bioontology.org/ontologies/ECSO/?p=classes&conceptid=root)
#'
#' @return list - a formatted EML annotation
#' @export
#'
#' @examples eml_ecso_annotation("latitude coordinate")
eml_ecso_annotation <- function(valueLabel) {
ecso <- read_ontology("ecso")
query <-
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?iri ?label
WHERE {
?iri rdf:type <http://www.w3.org/2002/07/owl#Class> .
?iri rdfs:label ?label .
}"
df <- suppressMessages(rdflib::rdf_query(ecso, query))
stopifnot(valueLabel %in% df$label)
annotations <-
dplyr::filter(df,
stringr::str_to_lower(label) == stringr::str_to_lower(valueLabel))
if (nrow(annotations) > 1) {
warning(
paste0(
"More than one annotation found for ",
valueLabel,
". Please check the ontology on which one would be more appropriate."
)
)
}
list(
propertyURI = list(label = "contains measurements of type",
propertyURI = "http://ecoinformatics.org/oboe/oboe.1.2/oboe-core.owl#containsMeasurementsOfType"),
valueURI = list(label = annotations$label,
valueURI = annotations$iri)
)
}