-
Notifications
You must be signed in to change notification settings - Fork 0
/
classSampleAnnotation.R
222 lines (190 loc) · 6.5 KB
/
classSampleAnnotation.R
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
#'
#' Class that stores the annotation for samples
#'
library(shiny)
SampleAnnotationNames <- c("Conditions" = "conditions",
"Mean fragment lengths" = "meanfragmentlength")
#' Contains the annotation of various samples
#'
#' @slot conditions Table that determines if a sample (row) is in a condition condition (column)
#' @slot fragment.lengths Table that contains annotations for each sample (row)
#'
#' @return
#' @export
#'
#' @examples
SampleAnnotation <- setClass(
"SampleAnnotation",
slots = signature(
conditions = "data.frame",
sample.info = "data.frame"
),
prototype = list(
conditions = data.frame(),
sample.info = data.frame()
),
validity = function(object) {
if(nrow(object@conditions) > 0) {
if(!all(is.logical(as.matrix(object@conditions)))) {
return(F)
}
}
if(nrow([email protected]) > 0 && colnames([email protected]) != c("meanfragmentlength")) {
return("Invalid sample info object")
}
return(T)
}
)
#' Checks if a sample annotation has a conditions table
#'
#' @param object SampleAnnotation object
#'
#' @return
#' @export
#' @rdname sampleAnnotationHasConditions
#'
#' @examples
setGeneric(name = "sampleAnnotationHasConditions",
def = function(object) {
standardGeneric("sampleAnnotationHasConditions")
})
#' @rdname sampleAnnotationHasConditions
setMethod(f = "sampleAnnotationHasConditions",
signature = signature(object = "SampleAnnotation"),
definition = function(object) {
return(nrow(object@conditions) > 0)
})
#' Checks if a sample annotation has a sample info table
#'
#' @param object SampleAnnotation object
#'
#' @return
#' @export
#' @rdname sampleAnnotationHasSampleInfo
#'
#' @examples
setGeneric(name = "sampleAnnotationHasSampleInfo",
def = function(object) {
standardGeneric("sampleAnnotationHasSampleInfo")
})
#' @rdname sampleAnnotationHasSampleInfo
setMethod(f = "sampleAnnotationHasSampleInfo",
signature = signature(object = "SampleAnnotation"),
definition = function(object) {
return(nrow([email protected]) > 0)
})
#' Checks if a sample annotation and a list of sample names are matching
#'
#' @param object SampleAnnotation object
#' @param samples vector of samples
#'
#' @return
#' @export
#' @rdname sampleAnnotationMatchesSamples
#'
#' @examples
setGeneric(name = "sampleAnnotationMatchesSamples",
def = function(object, samples) {
standardGeneric("sampleAnnotationMatchesSamples")
})
#' @rdname sampleAnnotationMatchesSamples
setMethod(f = "sampleAnnotationMatchesSamples",
signature = signature(object = "SampleAnnotation", samples = "character"),
definition = function(object, samples) {
if(sampleAnnotationHasConditions(object)) {
if(!setequal(rownames(object@conditions), samples)) {
return(F)
}
}
if(sampleAnnotationHasSampleInfo(object)) {
if(!setequal(rownames([email protected]), samples)) {
return(F)
}
}
return(T)
})
#' Merges two SampleAnnotation objects
#'
#' @param object1 SampleAnnotation object
#' @param object2 SampleAnnotation object
#'
#' @return
#' @export
#' @rdname mergeSampleAnnotation
#'
#' @examples
setGeneric(name = "mergeSampleAnnotation",
def = function(object1, object2) {
standardGeneric("mergeSampleAnnotation")
})
#' @rdname mergeSampleAnnotation
setMethod(f = "mergeSampleAnnotation",
signature = signature(object1 = "SampleAnnotation", object2 = "SampleAnnotation"),
definition = function(object1, object2) {
# Conditions are just overwritten
if(sampleAnnotationHasConditions(object2)) {
object1@conditions <- object2@conditions
}
# Merge sample info
if(sampleAnnotationHasSampleInfo(object2)) {
if(sampleAnnotationHasSampleInfo(object1)) {
}
else {
}
}
return(object1)
})
#' Returns list of samples that are annotatated with given annotation type
#'
#' @param object SampleAnnotation object
#' @param annotation conditions, meanfragmentlengths
#'
#' @return
#' @export
#' @rdname sampleAnnotationAnnotatedSamples
#'
#' @examples
setGeneric(name = "sampleAnnotationAnnotatedSamples",
def = function(object, annotation) {
standardGeneric("sampleAnnotationAnnotatedSamples")
})
#' @rdname sampleAnnotationAnnotatedSamples
setMethod(f = "sampleAnnotationAnnotatedSamples",
signature = signature(object = "SampleAnnotation", annotation = "character"),
definition = function(object, annotation) {
if(annotation %in% c("meanfragmentlength")) {
return(if(sampleAnnotationHasSampleInfo(object)) rownames([email protected])[!is.na([email protected][[annotation]])] else c())
}
else if(annotation == "conditions") {
return(rownames(object@conditions))
}
else {
stop(paste("Unknown annotation type ", annotation))
}
})
#' Returns a data frame that contains the annotation data
#'
#' @param object SampleAnnotation object
#'
#' @return
#' @export
#' @rdname sampleAnnotationToTable
#'
#' @examples
setGeneric(name = "sampleAnnotationToTable",
def = function(object) {
standardGeneric("sampleAnnotationToTable")
})
#' @rdname sampleAnnotationToTable
setMethod(f = "sampleAnnotationToTable",
signature = signature(object = "SampleAnnotation"),
definition = function(object) {
samples <- unique(c(rownames([email protected])))
table <- data.frame(row.names = samples,
"meanfragmentlength" = rep(NA, length(samples)),
check.names = F)
table[rownames([email protected]), "meanfragmentlength"] <- [email protected]$meanfragmentlength
return(table)
})