Skip to content

Commit 3525473

Browse files
committed
working constraint classes
1 parent 29df389 commit 3525473

9 files changed

+157
-74
lines changed

DESCRIPTION

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@ Collate:
3232
'struct_class.R'
3333
'parameter_class.R'
3434
'chart_class.R'
35-
'stato_class.R'
35+
'output_class.R'
3636
'DatasetExperiment_class.R'
37+
'constraint_class.R'
3738
'entity_class.R'
39+
'stato_class.R'
3840
'entity_stato_class.R'
3941
'enum_class.R'
4042
'enum_stato_class.R'
41-
'output_class.R'
4243
'model_class.R'
4344
'example_objects.R'
4445
'model_list_class.R'
@@ -50,7 +51,7 @@ Collate:
5051
'struct-package.R'
5152
'struct_templates.R'
5253
'zzz.R'
53-
RoxygenNote: 7.3.1
54+
RoxygenNote: 7.3.2
5455
Depends: R (>= 4.0)
5556
Suggests:
5657
testthat,

R/DatasetExperiment_class.R

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
#'
33
#' An object for holding raw data and associated meta data
44
#'
5-
#' The DatasetExperiment object is an extension of the SummarizedExperiment object
6-
#' from the SummarizedExperiment package (found on Bioconductor).
7-
#' It incorporates the basic functionality of struct objects, containing fields such as
5+
#' The DatasetExperiment object is an extension of the SummarizedExperiment object
6+
#' from the SummarizedExperiment package (found on Bioconductor).
7+
#' It incorporates the basic functionality of struct objects, containing fields such as
88
#' Description, Name and Type with features of SummarizedExperiment such as subsetting.
9-
#'
9+
#'
1010
#' There are some important differences between DatasetExperiment and SummarizedExperiment:
1111
#' \itemize{
1212
#' \item In DatasetExperiment data is stored as Samples (rows) x Features (columns)
1313
#' \item DatasetExperiment currently only supports a single assay
1414
#' \item length(DatasetExperiment) returns the number of samples
1515
#' }
16-
#'
16+
#'
1717
#' @export
1818
#' @slot name Name of the dataset
1919
#' @slot description Brief description of the dataset
@@ -27,31 +27,31 @@
2727
#' @param ... named slot values to pass through to struct_class
2828
#' @import SummarizedExperiment
2929
#' @import S4Vectors
30-
#' @include generics.R struct_class.R stato_class.R chart_class.R
30+
#' @include generics.R struct_class.R output_class.R parameter_class.R chart_class.R
3131
#' @return DatasetExperiment
3232
#' @rdname struct_DatasetExperiment
3333
DatasetExperiment = function(
3434
data=data.frame(),
3535
sample_meta=data.frame(),
3636
variable_meta=data.frame(),
3737
...){
38-
38+
3939
# convert data set to list
4040
assays=list(data)
41-
41+
4242
# sample_meta
43-
43+
4444
out=.DatasetExperiment(SummarizedExperiment(
4545
assays=assays,
4646
colData=variable_meta,
4747
rowData=sample_meta),
4848
...)
49-
49+
5050
return(out)
5151
}
5252

5353
.DatasetExperiment <- setClass(
54-
"DatasetExperiment",
54+
"DatasetExperiment",
5555
contains = c("struct_class","SummarizedExperiment"),
5656
prototype=list('libraries'='SummarizedExperiment')
5757
)
@@ -61,9 +61,9 @@ DatasetExperiment = function(
6161
setMethod(f = "$",
6262
signature = c("DatasetExperiment"),
6363
definition = function(x,name) {
64-
64+
6565
s = c('data','sample_meta','variable_meta')
66-
66+
6767
if (name %in% s) {
6868
if (name == 'data') {
6969
if (length(assays(x))==0) {
@@ -72,23 +72,23 @@ setMethod(f = "$",
7272
value = assay(x,1)
7373
}
7474
} else if (name == 'sample_meta') {
75-
value = S4Vectors::DataFrame(rowData(x),check.names = FALSE)
75+
value = S4Vectors::DataFrame(rowData(x),check.names = FALSE)
7676
} else if (name == 'variable_meta') {
7777
value = S4Vectors::DataFrame(colData(x),check.names = FALSE)
78-
}
79-
78+
}
79+
8080
if (name %in% s) {
8181
# convert to data.frame if using the original struct definitions
8282
value=as.data.frame(value)
8383
}
84-
84+
8585
return(value)
86-
86+
8787
} else {
8888
# for name,description etc
8989
return(callNextMethod())
9090
}
91-
91+
9292
}
9393
)
9494

@@ -117,10 +117,10 @@ setMethod(f = "$<-",
117117
setMethod(f = 'show',
118118
signature = c('DatasetExperiment'),
119119
definition = function(object) {
120-
120+
121121
# print struct generic info
122122
callNextMethod()
123-
123+
124124
# number of assays
125125
nms <- length(assays(object))
126126
if (is.null(nms)) {
@@ -135,8 +135,8 @@ setMethod(f = 'show',
135135
)
136136

137137
#' Convert a DatasetExperiment to SummarizedExperiment
138-
#'
139-
#' Converts a DatasetExperiment to SummarizedExperiment. The assay data is
138+
#'
139+
#' Converts a DatasetExperiment to SummarizedExperiment. The assay data is
140140
#' transposed, and colData and rowData switched to match. struct specific
141141
#' slots such as "name" and "description" are stored in the metaData.
142142
#' @param obj a DatasetExperiment object
@@ -155,16 +155,16 @@ setMethod (f = 'as.SummarizedExperiment',
155155
'type'=obj$type,
156156
'libraries'=obj$libraries)
157157
)
158-
158+
159159
return(out)
160160
}
161161
)
162162

163163

164164
#' Convert a SummarizedExperiment to DatasetExperiment
165-
#'
166-
#' The assay data is transposed, and colData and rowData switched to match.
167-
#' struct specific slots such as "name" and "description" are extracted from the
165+
#'
166+
#' The assay data is transposed, and colData and rowData switched to match.
167+
#' struct specific slots such as "name" and "description" are extracted from the
168168
#' metaData if available. NB Any additional metadata will be lost during this conversion.
169169
#' @param obj a SummarizedExperiment object
170170
#' @return a DatasetExperiment object
@@ -177,7 +177,7 @@ setMethod (f = 'as.DatasetExperiment',
177177
B = as.data.frame(t(A))
178178
colnames(B) = rownames(A)
179179
rownames(B) = colnames(A)
180-
180+
181181
out=DatasetExperiment(
182182
data=B,
183183
variable_meta=as.data.frame(rowData(obj)),
@@ -187,15 +187,15 @@ setMethod (f = 'as.DatasetExperiment',
187187
type=as.character(metadata(obj)$type),
188188
libraries=as.character(metadata(obj)$libraries)
189189
)
190-
190+
191191
return(out)
192192
}
193193
)
194194

195195

196196

197197
#' Export a dataset to an excel file
198-
#'
198+
#'
199199
#' Exports a dataset object to an excel file with sheets for data, sample_meta and variable_meta
200200
#' @param object a dataset object
201201
#' @param outfile the filename (including path) to write the data to
@@ -211,19 +211,19 @@ setMethod (f = 'as.DatasetExperiment',
211211
setMethod(f = "export_xlsx",
212212
signature = c("DatasetExperiment"),
213213
definition = function(object,outfile,transpose = TRUE) {
214-
214+
215215
# check for openxlsx
216216
if (!requireNamespace('openxlsx', quietly = TRUE)) {
217217
stop('package "openxlsx" was not found. Please install it to use "export.xlsx()".')
218218
}
219-
220-
219+
220+
221221
if (transpose) {
222222
X = as.data.frame(t(object$data))
223223
} else {
224224
X = object$data
225225
}
226-
226+
227227
OUT = list(
228228
'data' = X,
229229
'sample_meta' = object$sample_meta,
@@ -242,7 +242,7 @@ setMethod(f = "export_xlsx",
242242
return(IN)
243243
}
244244

245-
#' @export
245+
#' @export
246246
#' @rdname autocompletion
247247
setMethod('.DollarNames','DatasetExperiment',.DollarNames.DatasetExperiment)
248248

R/constraint_class.R

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#' @include generics.R struct_class.R parameter_class.R output_class.R
2+
3+
# Define the entity_constraint class
4+
setClass("entity_constraint",
5+
contains = 'struct_class',
6+
slots = c(
7+
message = "function",
8+
test = "function"))
9+
10+
# Define the generic function for validation
11+
setGeneric("check_constraints", function(entity_constraint, object) {
12+
standardGeneric("check_constraints")
13+
})
14+
15+
# Define the method for the entity_constraint class
16+
setMethod("check_constraints", "entity_constraint", function(entity_constraint, object) {
17+
# check validity
18+
valid = entity_constraint@test(object)
19+
20+
# return error msg if not valid
21+
if (!valid) {
22+
valid = entity_constraint@message(object)
23+
}
24+
25+
# return TRUE if valid, otherwise returns message
26+
return(valid)
27+
28+
})
29+
30+
# Define specific entity_constraints as instances of the entity_constraint class
31+
constraint.max_len <- function() {
32+
new_struct("entity_constraint",
33+
name = 'Maximum length constraint',
34+
description = paste0('Checks that the value of an entity object not longer than specified.'),
35+
message = function(object){
36+
msg = c(paste0('entity$name = ',object$name),
37+
paste0('entity$value must be at least ', min_value))
38+
return(msg)
39+
},
40+
test = function(object) {
41+
check = length(object@value) <= object@max_length
42+
return(check)
43+
})
44+
}
45+
46+
constraint.type <- function() {
47+
new_struct("entity_constraint",
48+
name = 'Type constraint',
49+
description = 'Checks that the value of an entity object is of the expected type.',
50+
message = function(object){
51+
msg = c(paste0('entity$name = ',object$name),
52+
paste0('entity$value must be of type ', object$type))
53+
return(msg)
54+
},
55+
test = function(object) {
56+
check = any(sapply(object@type, function(t) is(object@value, t)))
57+
return(check)
58+
})
59+
}
60+
61+
62+
# test for minimum
63+
constraint.min_value <- function(min_value) {
64+
new_struct("entity_constraint",
65+
name = 'Minimum value constraint',
66+
description = paste0('Checks that the value of an entity object not less than ',min_value),
67+
message = paste("Minimum value must be at least", min_value),
68+
test = function(object) {
69+
if (any(object@value < min_value)) {
70+
msg = c(paste0('entity$name = ',object$name),
71+
paste0('entity$value must be at least ', min_value))
72+
return(msg)
73+
}
74+
return(TRUE)
75+
})
76+
}

0 commit comments

Comments
 (0)