-
Notifications
You must be signed in to change notification settings - Fork 1
/
TKCat-KMR-POK.Rmd
509 lines (420 loc) · 12.4 KB
/
TKCat-KMR-POK.Rmd
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
---
title: "Defining and using Requirements for Knowledge Management (KMR) in TKCat"
author: "Patrice Godard"
date: "`r format(Sys.time(), '%B %d, %Y')`"
package: "TKCat (version `r packageVersion('TKCat')`)"
link-citations: yes
output:
rmarkdown::html_document:
number_sections: yes
self_contained: true
toc: yes
fig_width: 7
fig_height: 5
vignette: >
%\VignetteIndexEntry{TKCat-KMR-POK}
%\VignetteEncoding{UTF-8}
%\VignetteEngine{knitr::rmarkdown}
editor_options:
chunk_output_type: console
---
```{r setup, message=FALSE, echo=FALSE, include=FALSE, cache=FALSE}
library(knitr)
opts_chunk$set(
include=TRUE,
echo=TRUE,
message=TRUE,
warning=TRUE,
cache=FALSE,
cache.lazy=FALSE
)
library(TKCat)
```
In TKCat, knowledge resources are manipulated as modeled
database (MDB) objects. These objects provide access to the data tables along
with a general description of the resource and a detail data model documenting
the tables, their fields and their relationships.
These MDB are then gathered in TKCat catalogs that can be explored an shared.
Beside the relational model, no additional constraints are applied to
an MDB. This allows for high flexibility in the data that can be managed.
However, in some cases, it could be useful to add further constraints to ensure
that the data is compatible with specific analysis or integration workflows.
In TKCat, this feature is supported by KMR (Knowledge Management Requirements).
A KMR object is meant to be shared and centrally managed.
MDBs intended to meet these requirements must contain technical tables
referring to the corresponding KMR. When grouped in the same TKCat catalog,
KMRs and MDBs form a coherent corpus of knowledge that can be leveraged
consistently by KMR-tailored functions.
The first part of this document describes how to create a KMR object.
The second part shows how to refer to these requirements in an MDB object.
Finally, the third part describes how to use KMR and MDB together to leverage
pieces of knowledge (POK).
# Defining KMR
## KMR organization
A KMR object is an MDB object with a predefined data model. It is simply
created by calling the `create_KMR()` function which takes some metadata as
arguments.
```{r}
ebkm <- create_KMR(
name="EBKM",
title="Experimental and Biological Knowledge Management",
description="Requirements for integrating knowledge from biological research activities",
version="0.1",
maintainer="[Patrice Godard](mailto:patrice.godard@gmail.com)"
)
```
The figure below shows the data model of such an object. A KMR object describes
requirements regarding:
- Tables: the features that can be found in the table and which of them
are mandatory.
- Features: the properties composing the feature and which of them are
mandatory. In tables providing the feature, there should be one column
per feature property.
- Feature properties: the type and the kind of measurement (if relevant)
- Units: possible units for measurements
- Possible character or integer values for measurement properties
It's also possible to store helpers in a KMR object. These helpers are typically
functions used to query MDBs fitting KMR requirements.
```{r, eval=FALSE}
data_model(ebkm) %>%
plot()
```
![](KMR-data-model.png)
## Define requirements
### Table and mandatory features
In the example below, we define requirements for tables providing information
about biological samples. Such table must provide at least a sample name and
a sample description.
It is specified by first defining the features,
```{r}
ebkm <- add_feature_def(
kmr = ebkm,
name = "name",
description = "name/identifier of a record",
properties = list(
"value" = list(
type = "character",
mandatory = TRUE
)
)
)
ebkm <- add_feature_def(
kmr = ebkm,
name = "description",
description = "description of a record",
properties = list(
"value" = list(
type = "character",
mandatory = TRUE
)
)
)
```
and then by defining the table.
```{r}
ebkm <- add_table_def(
kmr = ebkm,
name = "samples",
description = "A table listing samples and associated features",
mandatory_features=c("name", "description")
)
```
### Optional features
Non mandatory features can then added to the table definition.
For samples, we can define the sex and tissue features and add them
to the table definition:
```{r}
ebkm <- add_feature_def(
kmr = ebkm,
name = "sex",
description = "Subject sex",
properties = list(
"value" = list(
type = "character",
mandatory = TRUE
)
)
)
ebkm <- add_feature_def(
kmr = ebkm,
name = "tissue",
description = "Name of a biological tissue",
properties = list(
"value" = list(
type = "character",
mandatory = TRUE
),
"identifier" = list(
type = "character",
description = "Identifier in reference database",
mandatory = TRUE
),
"reference" = list(
type="character",
description = "Reference database",
mandatory=TRUE
),
"side" = list(
type = "character",
description = "Sampling side",
mandatory = FALSE
),
"relative side" = list(
type="character",
description = "Sampling relative side",
mandatory = FALSE
),
"side reference" = list(
type="character",
description =
"Reference for relative side (e.g., Handedness, treatment...)",
mandatory = FALSE
)
)
)
ebkm <- add_table_features(
kmr = ebkm,
table = "samples",
features = c("sex", "tissue")
)
```
### Possible values for feature properties
We can also define a list of possible values for some feature property that
we want to standardize as shown below.
- Possible values for sex
```{r}
ebkm <- add_property_values(
kmr = ebkm, feature = "sex", property = "value",
values = c("female", "male")
)
```
- Tissues must be defined using
the [Uberon](http://obofoundry.org/ontology/uberon.html) ontology.
The way to define sampling side, absolutely or relatively to a reference,
is also controlled.
```{r}
ebkm <- add_property_values(
kmr = ebkm, feature = "tissue", property = "reference",
values=c(
"Uberon" = "https://obophenotype.github.io/uberon/"
)
)
ebkm <- add_property_values(
kmr = ebkm, feature = "tissue", property = "side",
values = c("left", "right")
)
ebkm <- add_property_values(
kmr = ebkm, feature = "tissue", property = "relative side",
values = c("ipsilateral", "contralateral")
)
```
### Measurements
Some feature properties could correspond to measurements with units.
To be able to create such property, we first need to define the possible units.
For example, "age" "value" is a "duration" that can be expressed using
different units, such as "m" (months) or "y" (years).
```{r}
ebkm <- add_unit_def(
kmr = ebkm,
measurement = "duration", unit = "s", description = "seconds"
)
ebkm <- add_unit_def(
kmr = ebkm,
measurement = "duration", unit = "min", description = "minutes"
)
ebkm <- add_unit_def(
kmr = ebkm,
measurement = "duration", unit = "h", description = "hours"
)
ebkm <- add_unit_def(
kmr = ebkm,
measurement = "duration", unit = "d", description = "days"
)
ebkm <- add_unit_def(
kmr = ebkm,
measurement = "duration", unit = "m", description = "months"
)
ebkm <- add_unit_def(
kmr = ebkm,
measurement = "duration", unit = "y", description = "years"
)
ebkm <- add_unit_def(
kmr = ebkm,
measurement = "duration", unit = "w", description = "weeks"
)
##
ebkm <- add_feature_def(
kmr = ebkm,
name = "age",
description = "Elapsed time since birth",
properties = list(
"value" = list(
type = "numeric",
mandatory = TRUE,
measurement = "duration"
)
)
)
ebkm <- add_table_features(
kmr = ebkm,
table = "samples",
features = c("age")
)
```
# KM specifications
## Exploring requirements
Before documenting the specifications of an MDB fitting KMR requirements,
those requirements can be explored using different functions.
- Get supported table types:
```{r}
list_table_types(ebkm)
```
- Get supported features:
```{r}
list_table_features(ebkm, "samples")
```
- Details about feature properties:
```{r}
list_feature_properties(ebkm, "tissue")
```
- Get property supported values:
```{r}
list_property_values(ebkm, "sex", "value")
```
- Get measurements and units:
```{r}
list_measurements(ebkm)
list_measurement_units(ebkm, "duration")
```
## Documenting MDB specifications
### Simple example
Let's create a simple MDB with only one table with biological samples
information.
```{r}
samples <- tibble(
name = c("S1", "S2", "S3", "S4"),
description = c(
"Sample from left hippocampus from patient 1",
"Sample from left hippocampus from patient 2",
"Sample from left hippocampus from patient 3",
"Sample from left hippocampus from patient 4"
),
sex = c("male", "female", "female", "male"),
age = c(25, 36, 28, 42),
tissue_name = rep("hippocampus", 4),
tissue_id = rep("UBERON_0002421", 4),
tissue_ref = rep("Uberon", 4),
tissue_side = rep("left", 4),
seizures = c(31, 64, 12, 29)
)
model <- ReDaMoR::df_to_model(samples)
mdb <- memoMDB(
list(samples=samples),
model,
dbInfo=list(
name="Test"
)
)
```
The specifications of the sample table fitting the EBKM requirement defined
above can be document as following. Additional columns not part of the KMR
requirements (e.g. "seizures") can still be used in the MDB but are not part
of specifications.
```{r}
mdb <- add_km_spec(mdb, ebkm)
mdb <- add_km_table(
mdb, ebkm,
name="samples", type="samples",
features=list(
### TBKM mandatory features ###
list(feature="name", fields="name"),
list(feature="description", fields="description"),
### TBKM optional features ###
list(feature="age", fields="age", unit="y"),
list(feature="sex", fields="sex"),
list(
feature="tissue",
fields=list(
"value"=list(field="tissue_name"),
"identifier"=list(field="tissue_id"),
"reference"=list(field="tissue_ref"),
"side"=list(field="tissue_side")
)
)
)
)
```
# Piece Of Knowledge (POK)
## Helpers
Helpers are R functions that are attached to a KMR or an MDB object to
leverage the provided information.
### KMR helpers
Helpers are written in .R script that are then attach to the object of interest.
For example the following function has been saved the "EBKM-helpers.R" file.
```{r, results='asis', echo=FALSE}
ec <- readLines("EBKM-helpers.R")
cat('```r', sep="\n")
cat(ec, sep="\n")
cat('```', sep="\n")
```
It can be attached to the KMR object as follows:
```{r}
ebkm <- add_helpers(
ebkm,
code="EBKM-helpers.R",
name="R-Helpers",
language = "R"
)
```
Then the helpers can be retrieved and used as follows:
```{r}
ebkm_helpers <- get_R_helpers(ebkm)
ebkm_helpers$help()
ebkm_helpers$help("get_tissue_ref_url")
ebkm_helpers$get_tissue_ref_url(c("UBERON_0002421", "UBERON_0001876"))
```
### MDB helpers
Helpers can also be added to MDB with KM specifications. For example the
following function summarizes the number of seizures associated to biological
samples:
```{r, results='asis', echo=FALSE}
ec <- readLines("MDB-helpers.R")
cat('```r', sep="\n")
cat(ec, sep="\n")
cat('```', sep="\n")
```
When associating and retrieving MDB helpers, KMR object need to be provided:
```{r}
mdb <- add_helpers(
mdb,
kmr = ebkm,
code="MDB-helpers.R",
name="R-Helpers",
language = "R"
)
```
MDB helpers can be retrieved and used similarly as KMR helpers:
```{r}
mdb_helpers <- get_R_helpers(mdb, kmr = ebkm)
mdb_helpers$help("summarize_seizures")
mdb_helpers$summarize_seizures()
```
## POK objects
MDB can be combined with relevant KMR to create POK (Piece Of Knowledge)
objects:
```{r}
pok <- create_POK(mdb, ebkm)
pok
```
A POK object is a list with the following slots:
- mdb: the provided MDB object
- kmr: the provided KMR object
- helpers (optional): functions found in mdb and kmr helpers
- tkcat (optional): a TKCat or chTKCat object from mdb or kmr or directly
provided to the constructor.
POK can be retrieved directly from a TKCat or chTKCat object as examplified
below:
```{r}
tkcat <- TKCat(Test=mdb, EBKM=ebkm)
get_POK(tkcat, "Test", "EBKM")
```