Browse code

various changes to condition on availability of owlready2

vjcitn authored on 10/10/2024 09:32:35
Showing 11 changed files

... ...
@@ -19,10 +19,13 @@ get_classes = function(owlfile) {
19 19
 #' the entity.)
20 20
 #' @examples
21 21
 #' pa = get_ordo_owl_path()
22
-#' orde = setup_entities(pa)
23
-#' orde
24
-#' ancestors(orde[1000:1001])
25
-#' labels(orde[1000:1001])
22
+#' o2 = try(reticulate::import("owlready2"), silent=TRUE)
23
+#' if (!inherits(o2, "try-error")) {
24
+#'  orde = setup_entities(pa)
25
+#'  orde
26
+#'  ancestors(orde[1000:1001])
27
+#'  labels(orde[1000:1001])
28
+#' }
26 29
 #' @export 
27 30
 setup_entities = function(owlfn) {
28 31
   thecall = match.call()
... ...
@@ -71,13 +74,17 @@ print.owlents = function(x, ...) {
71 74
 #' @return a list of sets
72 75
 #' @examples
73 76
 #' pa = get_ordo_owl_path()
74
-#' orde = setup_entities(pa)
75
-#' orde
76
-#' ancestors(orde[1:5])
77
-#' labels(orde[1:5])
77
+#' o2 = try(reticulate::import("owlready2"), silent=TRUE)
78
+#' if (!inherits(o2, "try-error")) {
79
+#'  orde = setup_entities(pa)
80
+#'  orde
81
+#'  ancestors(orde[1:5])
82
+#'  labels(orde[1:5])
83
+#' }
78 84
 #' @export
79 85
 ancestors = function(oe) {
80
-  o2 = reticulate::import("owlready2")
86
+  o2 = try(reticulate::import("owlready2"))
87
+  if (inherits(o2, "try-error")) stop("ensure that reticulate can find owlready2")
81 88
   ans = lapply(oe$allents, o2$EntityClass$ancestors)
82 89
   names(ans) = oe$clnames
83 90
   ans
... ...
@@ -89,9 +96,12 @@ ancestors = function(oe) {
89 96
 #' @return list of vectors of character()
90 97
 #' @examples
91 98
 #' pa = get_ordo_owl_path()
92
-#' orde = setup_entities(pa)
93
-#' al = ancestors(orde[1001:1002])
94
-#' ancestors_names(al)
99
+#' o2 = try(reticulate::import("owlready2"), silent=TRUE)
100
+#' if (!inherits(o2, "try-error")) {
101
+#'  orde = setup_entities(pa)
102
+#'  al = ancestors(orde[1001:1002])
103
+#'  ancestors_names(al)
104
+#' }
95 105
 #' @export
96 106
 ancestors_names = function(anclist) {
97 107
   lapply(anclist, .ancestor_element_names)
... ...
@@ -103,9 +113,12 @@ ancestors_names = function(anclist) {
103 113
 #' @return list of vectors of character()
104 114
 #' @examples
105 115
 #' pa = get_ordo_owl_path()
106
-#' orde = setup_entities(pa)
107
-#' al = subclasses(orde[100:120])
108
-#' children_names(al)
116
+#' o2 = try(reticulate::import("owlready2"), silent=TRUE)
117
+#' if (!inherits(o2, "try-error")) {
118
+#'  orde = setup_entities(pa)
119
+#'  al = subclasses(orde[100:120])
120
+#'  children_names(al)
121
+#' }
109 122
 #' @export
110 123
 children_names = function(sclist) {
111 124
   lapply(sclist, .subclass_element_names)
... ...
@@ -131,10 +144,13 @@ children_names = function(sclist) {
131 144
 #' @return list of vectors of tags of parents
132 145
 #' @examples
133 146
 #' pa = get_ordo_owl_path()
134
-#' orde = setup_entities(pa)
135
-#' orde
136
-#' parents(orde[1000:1001])
137
-#' labels(orde[1000:1001])
147
+#' o2 = try(reticulate::import("owlready2"), silent=TRUE)
148
+#' if (!inherits(o2, "try-error")) {
149
+#'  orde = setup_entities(pa)
150
+#'  orde
151
+#'  parents(orde[1000:1001])
152
+#'  labels(orde[1000:1001])
153
+#' }
138 154
 #' @export
139 155
 parents = function(oe) {
140 156
     pts = lapply(oe$allents, function(x) x$is_a)
... ...
@@ -154,14 +170,17 @@ parents = function(oe) {
154 170
 #' @param oe owlents instance
155 171
 #' @examples
156 172
 #' pa = get_ordo_owl_path()
157
-#' orde = setup_entities(pa)
158
-#' orde
159
-#' sc <- subclasses(orde[1:5])
160
-#' labels(orde[3])
161
-#' o3 = reticulate::iterate(sc[[3]])
162
-#' print(length(o3))
163
-#' o3[[2]]
164
-#' labels(orde["Orphanet_100011"])
173
+#' o2 = try(reticulate::import("owlready2"), silent=TRUE)
174
+#' if (!inherits(o2, "try-error")) {
175
+#'  orde = setup_entities(pa)
176
+#'  orde
177
+#'  sc <- subclasses(orde[1:5])
178
+#'  labels(orde[3])
179
+#'  o3 = reticulate::iterate(sc[[3]])
180
+#'  print(length(o3))
181
+#'  o3[[2]]
182
+#'  labels(orde["Orphanet_100011"])
183
+#' }
165 184
 #' @export
166 185
 subclasses = function(oe) {
167 186
   o2 = reticulate::import("owlready2")
... ...
@@ -181,9 +200,12 @@ subclasses = function(oe) {
181 200
 #' The previous functionality which failed is available, not exported, as labelsOLD.owlents.
182 201
 #' @examples
183 202
 #' clont_path = owl2cache(url="http://purl.obolibrary.org/obo/cl.owl")
184
-#' clont = setup_entities(clont_path)
185
-#' labels(clont[1:5])
186
-#' labels(clont[51:55])
203
+#' o2 = try(reticulate::import("owlready2"), silent=TRUE)
204
+#' if (!inherits(o2, "try-error")) {
205
+#'  clont = setup_entities(clont_path)
206
+#'  labels(clont[1:5])
207
+#'  labels(clont[51:55])
208
+#' }
187 209
 #' @export
188 210
 labels.owlents = function (object, ...) 
189 211
 {
... ...
@@ -205,32 +227,33 @@ labelsOLD.owlents = function(object, ...) {
205 227
   
206 228
 
207 229
 #' use owlready2 ontology search facility on term labels
208
-#' @param oents owlents instance
230
+#' @param ontopath character(1) path to owl file
209 231
 #' @param regexp character(1) simple regular expression
210 232
 #' @param case_sensitive logical(1) should case be respected in search?
211 233
 #' @return A named list: term labels are elements, tags are names of elements.
212 234
 #' Will return NULL if nothing is found.
213 235
 #' @examples
214 236
 #' pa = get_ordo_owl_path()
215
-#' orde = setup_entities(pa)
216
-#' ol = search_labels(orde, "*Immunog*")
217
-#' plot(orde, names(ol))
237
+#' ol = search_labels(pa, "*Immunog*")
238
+#' orde = setup_entities2(pa)
239
+#' onto_plot2(orde, names(ol))
218 240
 #' @export
219
-search_labels = function (oents, regexp, case_sensitive=TRUE) 
241
+search_labels = function (ontopath, regexp, case_sensitive=TRUE) 
220 242
 {
221
-#
222
-#search(_use_str_as_loc_str=True, _case_sensitive=True, _bm25=False, **kargs)
223
-#
224
-    stopifnot(inherits(oents, "owlents"))
243
+    stopifnot(inherits(ontopath, "character"))
225 244
     thecall = match.call()
226
-    o2 = reticulate::import("owlready2")
227
-    ont = o2$get_ontology(oents$owlfn)$load()
228
-    ans = ont$search(`_case_sensitive`=case_sensitive, label = regexp)
229
-    lans = reticulate::iterate(ans, force)
230
-    if (length(lans)<=0) return(NULL)
231
-    allv = lapply(lans, function(x) x$label[0])
232
-    alln = lapply(lans, function(x) x$name)
233
-    names(allv) = unlist(alln)
234
-    allv
245
+    proc = basilisk::basiliskStart(bsklenv, testload="owlready2") # avoid package-specific import
246
+    on.exit(basilisk::basiliskStop(proc))
247
+    basilisk::basiliskRun(proc, function(owlfn, thecall) {
248
+     o2 = reticulate::import("owlready2")
249
+     ont = o2$get_ontology(owlfn)$load()
250
+     ans = ont$search(`_case_sensitive`=case_sensitive, label = regexp)
251
+     lans = reticulate::iterate(ans, force)
252
+     if (length(lans)<=0) return(NULL)
253
+     allv = lapply(lans, function(x) x$label[0])
254
+     alln = lapply(lans, function(x) x$name)
255
+     names(allv) = unlist(alln)
256
+     allv
257
+     }, ontopath, thecall)
235 258
 }
236 259
 
... ...
@@ -11,8 +11,11 @@
11 11
 #'   "CL:0000623", "CL:0000451", "CL:0000556")
12 12
 #' cl3k = gsub(":", "_", cl3k)
13 13
 #' clont_path = owl2cache(url="http://purl.obolibrary.org/obo/cl.owl")
14
-#' clont = setup_entities(clont_path)
15
-#' plot(clont,cl3k)
14
+#' o2 = try(reticulate::import("owlready2"), silent=TRUE)
15
+#' if (!inherits(o2, "try-error")) {
16
+#'  clont = setup_entities(clont_path)
17
+#'  plot(clont,cl3k)
18
+#' }
16 19
 #' @export
17 20
 plot.owlents = function(x, y, ..., dropThing=TRUE) {
18 21
   if (missing(y)) stop("must provide classes for plotting as 'y'")
... ...
@@ -17,8 +17,11 @@ retrieve ancestor 'sets'
17 17
 }
18 18
 \examples{
19 19
 pa = get_ordo_owl_path()
20
-orde = setup_entities(pa)
21
-orde
22
-ancestors(orde[1:5])
23
-labels(orde[1:5])
20
+o2 = try(reticulate::import("owlready2"), silent=TRUE)
21
+if (!inherits(o2, "try-error")) {
22
+ orde = setup_entities(pa)
23
+ orde
24
+ ancestors(orde[1:5])
25
+ labels(orde[1:5])
26
+}
24 27
 }
... ...
@@ -20,7 +20,10 @@ non-entities are removed and names are extracted
20 20
 }
21 21
 \examples{
22 22
 pa = get_ordo_owl_path()
23
-orde = setup_entities(pa)
24
-al = ancestors(orde[1001:1002])
25
-ancestors_names(al)
23
+o2 = try(reticulate::import("owlready2"), silent=TRUE)
24
+if (!inherits(o2, "try-error")) {
25
+ orde = setup_entities(pa)
26
+ al = ancestors(orde[1001:1002])
27
+ ancestors_names(al)
28
+}
26 29
 }
... ...
@@ -20,7 +20,10 @@ non-entities are removed and names are extracted
20 20
 }
21 21
 \examples{
22 22
 pa = get_ordo_owl_path()
23
-orde = setup_entities(pa)
24
-al = subclasses(orde[100:120])
25
-children_names(al)
23
+o2 = try(reticulate::import("owlready2"), silent=TRUE)
24
+if (!inherits(o2, "try-error")) {
25
+ orde = setup_entities(pa)
26
+ al = subclasses(orde[100:120])
27
+ children_names(al)
28
+}
26 29
 }
... ...
@@ -24,7 +24,10 @@ The previous functionality which failed is available, not exported, as labelsOLD
24 24
 }
25 25
 \examples{
26 26
 clont_path = owl2cache(url="http://purl.obolibrary.org/obo/cl.owl")
27
-clont = setup_entities(clont_path)
28
-labels(clont[1:5])
29
-labels(clont[51:55])
27
+o2 = try(reticulate::import("owlready2"), silent=TRUE)
28
+if (!inherits(o2, "try-error")) {
29
+ clont = setup_entities(clont_path)
30
+ labels(clont[1:5])
31
+ labels(clont[51:55])
32
+}
30 33
 }
... ...
@@ -17,8 +17,11 @@ retrieve is_a
17 17
 }
18 18
 \examples{
19 19
 pa = get_ordo_owl_path()
20
-orde = setup_entities(pa)
21
-orde
22
-parents(orde[1000:1001])
23
-labels(orde[1000:1001])
20
+o2 = try(reticulate::import("owlready2"), silent=TRUE)
21
+if (!inherits(o2, "try-error")) {
22
+ orde = setup_entities(pa)
23
+ orde
24
+ parents(orde[1000:1001])
25
+ labels(orde[1000:1001])
26
+}
24 27
 }
... ...
@@ -25,6 +25,9 @@ cl3k = c("CL:0000492", "CL:0001054", "CL:0000236",
25 25
   "CL:0000623", "CL:0000451", "CL:0000556")
26 26
 cl3k = gsub(":", "_", cl3k)
27 27
 clont_path = owl2cache(url="http://purl.obolibrary.org/obo/cl.owl")
28
-clont = setup_entities(clont_path)
29
-plot(clont,cl3k)
28
+o2 = try(reticulate::import("owlready2"), silent=TRUE)
29
+if (!inherits(o2, "try-error")) {
30
+ clont = setup_entities(clont_path)
31
+ plot(clont,cl3k)
32
+}
30 33
 }
... ...
@@ -4,10 +4,10 @@
4 4
 \alias{search_labels}
5 5
 \title{use owlready2 ontology search facility on term labels}
6 6
 \usage{
7
-search_labels(oents, regexp, case_sensitive = TRUE)
7
+search_labels(ontopath, regexp, case_sensitive = TRUE)
8 8
 }
9 9
 \arguments{
10
-\item{oents}{owlents instance}
10
+\item{ontopath}{character(1) path to owl file}
11 11
 
12 12
 \item{regexp}{character(1) simple regular expression}
13 13
 
... ...
@@ -22,7 +22,7 @@ use owlready2 ontology search facility on term labels
22 22
 }
23 23
 \examples{
24 24
 pa = get_ordo_owl_path()
25
-orde = setup_entities(pa)
26
-ol = search_labels(orde, "*Immunog*")
27
-plot(orde, names(ol))
25
+ol = search_labels(pa, "*Immunog*")
26
+orde = setup_entities2(pa)
27
+onto_plot2(orde, names(ol))
28 28
 }
... ...
@@ -22,8 +22,11 @@ construct owlents instance from an owl file
22 22
 }
23 23
 \examples{
24 24
 pa = get_ordo_owl_path()
25
-orde = setup_entities(pa)
26
-orde
27
-ancestors(orde[1000:1001])
28
-labels(orde[1000:1001])
25
+o2 = try(reticulate::import("owlready2"), silent=TRUE)
26
+if (!inherits(o2, "try-error")) {
27
+ orde = setup_entities(pa)
28
+ orde
29
+ ancestors(orde[1000:1001])
30
+ labels(orde[1000:1001])
31
+}
29 32
 }
... ...
@@ -14,12 +14,15 @@ retrieve subclass entities
14 14
 }
15 15
 \examples{
16 16
 pa = get_ordo_owl_path()
17
-orde = setup_entities(pa)
18
-orde
19
-sc <- subclasses(orde[1:5])
20
-labels(orde[3])
21
-o3 = reticulate::iterate(sc[[3]])
22
-print(length(o3))
23
-o3[[2]]
24
-labels(orde["Orphanet_100011"])
17
+o2 = try(reticulate::import("owlready2"), silent=TRUE)
18
+if (!inherits(o2, "try-error")) {
19
+ orde = setup_entities(pa)
20
+ orde
21
+ sc <- subclasses(orde[1:5])
22
+ labels(orde[3])
23
+ o3 = reticulate::iterate(sc[[3]])
24
+ print(length(o3))
25
+ o3[[2]]
26
+ labels(orde["Orphanet_100011"])
27
+}
25 28
 }