-
Notifications
You must be signed in to change notification settings - Fork 32
/
MultiAssayExperiment-class.R
926 lines (839 loc) · 31.2 KB
/
MultiAssayExperiment-class.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
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
#' @import BiocGenerics SummarizedExperiment S4Vectors GenomicRanges methods
#' IRanges
NULL
## Helper function for validity checks
.uniqueSortIdentical <- function(charvec1, charvec2) {
listInput <- list(charvec1, charvec2)
listInput <- lapply(listInput, function(x) sort(unique(x)))
return(identical(listInput[[1]], listInput[[2]]))
}
.allIn <- function(charvec1, charvec2) {
return(all(charvec2 %in% charvec1))
}
### ==============================================
### MultiAssayExperiment class
### ----------------------------------------------
#' MultiAssayExperiment - An integrative multi-assay class for experiment data
#'
#' @description
#' The `MultiAssayExperiment` class can be used to manage results of
#' diverse assays on a collection of specimen. Currently, the class can handle
#' assays that are organized instances of
#' \code{\linkS4class{SummarizedExperiment}},
#' \code{\linkS4class{ExpressionSet}}, `matrix`,
#' \code{\link[RaggedExperiment:RaggedExperiment-class]{RaggedExperiment}}
#' (inherits from \code{\linkS4class{GRangesList}}), and `RangedVcfStack`.
#' Create new `MultiAssayExperiment` instances with the homonymous
#' constructor, minimally with the argument \code{\link{ExperimentList}},
#' potentially also with the arguments `colData` (see section below) and
#' \code{\link{sampleMap}}.
#'
#' @details
#' The dots (\code{\ldots}) argument allows the user to specify additional
#' arguments in several instances.
#' \itemize{
#' \item subsetting \strong{[}: additional arguments sent to
#' \link[GenomicRanges:findOverlaps-methods]{findOverlaps}.
#' \item mergeReplicates: used to specify arguments for the \code{simplify}
#' functional argument
#' \item assay: may contain withDimnames, which is forwarded to assays
#' \item combining \strong{c}: compatible \code{MultiAssayExperiment} classes
#' passed on to the \code{\linkS4class{ExperimentList}} constructor,
#' can be a \code{list}, \code{\linkS4class{List}}, or a series of
#' named arguments. See the examples below.
#' }
#'
#' @section colData:
#' The `colData` slot is a collection of primary specimen data valid
#' across all experiments. This slot is strictly of class
#' \code{\linkS4class{DataFrame}} but arguments for the constructor function
#' allow arguments to be of class `data.frame` and subsequently coerced.
#'
#' @section ExperimentList:
#' The \code{\link{ExperimentList}} slot is designed to contain results from
#' each experiment/assay. It contains a \linkS4class{SimpleList}.
#'
#' @section sampleMap:
#' The \code{\link{sampleMap}} contains a `DataFrame` of translatable
#' identifiers of samples and participants or biological units. The standard
#' column names of the `sampleMap` are "assay", "primary", and "colname".
#' Note that the "assay" column is a factor corresponding to the names of each
#' experiment in the `ExperimentList`. In the case where these names do
#' not match between the `sampleMap` and the experiments, the documented
#' experiments in the `sampleMap` take precedence and experiments are
#' dropped by the harmonization procedure. The constructor function will
#' generate a `sampleMap` in the case where it is not provided and this
#' method may be a 'safer' alternative for creating the `MultiAssayExperiment`
#' (so long as the rownames are identical in the `colData`, if provided).
#' An empty `sampleMap` may produce empty experiments if the levels of the
#' "assay" factor in the `sampleMap` do not match the names in the
#' `ExperimentList`.
#'
#' @slot ExperimentList A \code{\link{ExperimentList}} class object for
#' each assay dataset
#'
#' @slot colData A `DataFrame` of all clinical/specimen data available
#' across experiments
#'
#' @slot sampleMap A `DataFrame` of translatable identifiers
#' of samples and participants
#'
#' @slot metadata Additional data describing the
#' `MultiAssayExperiment` object
#'
#' @slot drops A metadata `list` of dropped information
#'
#' @param object,x A `MultiAssayExperiment` object
#'
#' @param ... Additional arguments for supporting functions. See details.
#'
#' @return A `MultiAssayExperiment` object
#'
#' @md
#'
#' @examples
#' example("MultiAssayExperiment")
#'
#' ## Subsetting
#' # Rows (i) Rows/Features in each experiment
#' mae[1, , ]
#' mae[c(TRUE, FALSE), , ]
#'
#' # Columns (j) Rows in colData
#' mae[, rownames(colData(mae))[3:2], ]
#'
#' # Assays (k)
#' mae[, , "Affy"]
#'
#' ## Complete cases (returns logical vector)
#' completes <- complete.cases(mae)
#' compMAE <- mae[, completes, ]
#' compMAE
#' colData(compMAE)
#'
#' @exportClass MultiAssayExperiment
#'
#' @seealso \link{MultiAssayExperiment-methods} for slot modifying methods,
#' \href{https://github.com/waldronlab/MultiAssayExperiment/wiki/MultiAssayExperiment-API}{MultiAssayExperiment API}
#'
#' @include ExperimentList-class.R
setClass(
"MultiAssayExperiment",
contains = "Annotated",
slots = list(
ExperimentList = "ExperimentList",
colData = "DataFrame",
sampleMap = "DataFrame",
drops = "list"
),
prototype = prototype(
colData = new("DFrame"),
sampleMap = new("DFrame")
)
)
### ==============================================
### MultiAssayExperiment constructor
### ----------------------------------------------
.harmonize <- function(experiments, colData, sampleMap) {
harmony <- character()
## sampleMap assays agree with experiment names
assay <- intersect(levels(sampleMap[["assay"]]), names(experiments))
keep_sampleMap_assay <- sampleMap[["assay"]] %in% assay
if (!all(keep_sampleMap_assay)) {
sampleMap <- sampleMap[keep_sampleMap_assay, , drop=FALSE]
sampleMap[["assay"]] <- factor(sampleMap[["assay"]], levels=assay)
harmony <- c(
harmony,
paste("removing", sum(!keep_sampleMap_assay),
"sampleMap rows not in names(experiments)"))
}
## sampleMap colname agrees with experiment colnames
grp <- droplevels(sampleMap[["assay"]])
colnm <- split(sampleMap[["colname"]], grp)
keep <- Map(intersect, colnm, colnames(experiments)[names(colnm)])
keep_sampleMap_colname <- logical(nrow(sampleMap))
split(keep_sampleMap_colname, grp) <- Map("%in%", colnm, keep)
if (!all(keep_sampleMap_colname)) {
sampleMap <- sampleMap[keep_sampleMap_colname, , drop=FALSE]
harmony <- c(
harmony,
paste("removing", sum(!keep_sampleMap_colname),
"sampleMap rows with 'colname'",
"not in colnames of experiments"))
}
## sampleMap primary agrees with primary
primary <- intersect(rownames(colData), sampleMap[["primary"]])
keep_sampleMap_primary <- sampleMap[["primary"]] %in% primary
if (!all(keep_sampleMap_primary)) {
sampleMap <- sampleMap[keep_sampleMap_primary, , drop=FALSE]
harmony <- c(
harmony,
paste("removing", sum(!keep_sampleMap_primary),
"sampleMap rows with 'primary' not in colData"))
}
## update objects
assay <- intersect(names(experiments), levels(sampleMap[["assay"]]))
experiments_columns <- split(sampleMap[["colname"]], sampleMap[["assay"]])
primary <- intersect(rownames(colData), sampleMap[["primary"]])
keep_colData <- rownames(colData) %in% primary
if (!all(keep_colData)) {
colData <- colData[keep_colData, , drop = FALSE]
harmony <- c(
harmony,
paste("removing", sum(!keep_colData),
"colData rownames not in sampleMap 'primary'"))
}
experiments <- mendoapply(function(x, idx) {
colmatch <- colnames(x) %in% idx
if (!all(colmatch))
x <- x[, colmatch, drop=FALSE]
x
}, experiments[assay], experiments_columns[assay])
if (length(harmony))
message("harmonizing input:\n ", paste(harmony, collapse="\n "))
list(experiments=experiments, sampleMap=sampleMap, colData=colData)
}
.smapColumnCoerce <- function(samplemap) {
isfuns <- list(is.factor, is.character, is.character)
asfuns <- list(as.factor, as.character, as.character)
samplemap[] <- Map(
function(cName, isFun, coerceFun) {
smapCol <- samplemap[[cName]]
if (!isFun(smapCol))
warning(
"sampleMap[['", cName, "']] coerced with",
as.character(substitute(coerceFun), "()"),
call. = FALSE
)
samplemap[[cName]] <- coerceFun(samplemap[[cName]])
},
cName = names(samplemap),
isFun = isfuns,
coerceFun = asfuns
)
samplemap
}
#' Construct an integrative representation of multi-omic data with
#' \code{MultiAssayExperiment}
#'
#' The constructor function for the \link{MultiAssayExperiment-class} combines
#' multiple data elements from the different hierarchies of data
#' (study, experiments, and samples). It can create instances where neither
#' a \code{sampleMap} or a \code{colData} set is provided. Please see the
#' MultiAssayExperiment API documentation for more information.
#'
#' @section colData:
#' The `colData` input can be either `DataFrame` or `data.frame` with
#' subsequent coercion to DataFrame. The rownames in the `colData` must match
#' the colnames in the experiments if no sampleMap is provided.
#'
#' @section experiments:
#' The `experiments` input can be of class \linkS4class{SimpleList} or `list`.
#' This input becomes the \code{\link{ExperimentList}}. Each element of the
#' input `list` or `List` must be named, rectangular with two dimensions, and
#' have `dimnames`.
#'
#' @section sampleMap:
#' The \code{\link{sampleMap}} can either be input as `DataFrame` or
#' `data.frame` with eventual coercion to `DataFrame`. The `sampleMap` relates
#' biological units and biological measurements within each assay. Each row in
#' the `sampleMap` is a single such link. The standard column names of the
#' `sampleMap` are "assay", "primary", and "colname". Note that the "assay"
#' column is a factor corresponding to the names of each experiment in the
#' `ExperimentList`. In the case where these names do not match between the
#' `sampleMap` and the experiments, the documented experiments in the
#' `sampleMap` take precedence and experiments are dropped by the harmonization
#' procedure. The constructor function will generate a `sampleMap` in the case
#' where it is not provided and this method may be a 'safer' alternative for
#' creating the `MultiAssayExperiment` (so long as the rownames are identical
#' in the `colData`, if provided). An empty `sampleMap` may produce empty
#' experiments if the levels of the "assay" factor in the `sampleMap` do not
#' match the names in the `ExperimentList`.
#'
#' @param experiments A \code{list} or \link{ExperimentList} of all
#' combined experiments
#'
#' @param colData A \code{\linkS4class{DataFrame}} or \code{data.frame} of
#' characteristics for all biological units
#'
#' @param sampleMap A \code{DataFrame} or \code{data.frame} of assay names,
#' sample identifiers, and colname samples
#'
#' @param metadata An optional argument of "ANY" class (usually list) for
#' content describing the experiments
#'
#' @param drops A \code{list} of unmatched information
#' (included after subsetting)
#'
#' @return A \code{MultiAssayExperiment} object that can store
#' experiment and phenotype data
#'
#' @example inst/scripts/MultiAssayExperiment-Ex.R
#'
#' @export MultiAssayExperiment
#' @seealso \link{MultiAssayExperiment-class}
MultiAssayExperiment <-
function(
experiments = ExperimentList(),
colData = S4Vectors::DataFrame(),
sampleMap = S4Vectors::DataFrame(
assay = factor(),
primary = character(),
colname = character()
),
metadata = list(),
drops = list()
)
{
experiments <- as(experiments, "ExperimentList")
allsamps <- unique(unlist(unname(colnames(experiments))))
if (missing(sampleMap)) {
if (missing(colData))
colData <- S4Vectors::DataFrame(row.names = allsamps)
sampleMap <- .sampleMapFromData(colData, experiments)
} else {
if (missing(colData))
colData <- S4Vectors::DataFrame(
row.names = unique(sampleMap[["primary"]])
)
}
colData <- as(colData, "DataFrame")
sampleMap <- as(sampleMap, "DataFrame")
if (!all(c("assay", "primary", "colname") %in% colnames(sampleMap)))
stop("'sampleMap' does not have required columns")
sampleMap <- .smapColumnCoerce(sampleMap)
bliss <- .harmonize(experiments, colData, sampleMap)
new("MultiAssayExperiment",
ExperimentList = bliss[["experiments"]],
colData = bliss[["colData"]],
sampleMap = bliss[["sampleMap"]],
metadata = metadata)
}
### - - - - - - - - - - - - - - - - - - - - - - - -
### Validity
###
## EXPERIMENTLIST
## 1.i. ExperimentList length must be the same as the unique length of the
## sampleMap "assay" column.
.checkExperimentList <- function(object) {
errors <- character()
assays <- levels(sampleMap(object)[["assay"]])
if (length(experiments(object)) != length(assays)) {
msg <- paste0("ExperimentList must be the same length as",
" the sampleMap assay column")
errors <- c(errors, msg)
}
## 1.ii. Element names of the ExperimentList should be found in the
## sampleMap "assay" column.
if (!all(names(experiments(object)) %in% assays)) {
msg <- paste0("All ExperimentList names were not found in",
" the sampleMap assay column")
errors <- c(errors, msg)
}
## 1.iii. ExperimentList cannot have any non-empty elements when the sampleMap
## is empty.
if (isEmpty(sampleMap(object)) && !isEmpty(experiments(object))) {
msg <- paste0(
"All non-empty elements in the ExperimentList must have",
" names in the sampleMap assay column"
)
errors <- c(errors, msg)
}
if (!length(errors)) NULL else errors
}
## 1.iii. For each ExperimentList element, colnames must be found in the
## "assay" column of the sampleMap
.checkSampleNames <- function(object) {
sampMap <- sampleMap(object)
assayCols <- mapToList(sampMap[, c("assay", "colname")])
colNams <- Filter(function(x) !isEmpty(x), colnames(object))
msg <- NULL
if (length(colNams)) {
logicResult <- mapply(function(x, y) {
identical(sort(x), sort(y))
}, x = colNams, y = assayCols)
if (!all(logicResult))
msg <- "not all ExperimentList samples are found in the sampleMap"
}
msg
}
## COLDATA
## 2.i. See setClass above where colData = "DataFrame"
## SAMPLEMAP
## 3.i. all values in the sampleMap "primary" column must be found in the
## rownames of colData
## 3.i.a sampleMap assay column must be a factor
.checkSampleMapNamesClass <- function(object) {
errors <- character()
if (!(.allIn(
rownames(colData(object)),
sampleMap(object)[["primary"]]
))) {
msg <- "All 'sampleMap[[primary]]' must be in 'rownames(colData)'"
errors <- c(errors, msg)
}
if (!is.factor(sampleMap(object)[["assay"]])) {
msg <- "'sampleMap' assay column not a factor"
errors <- c(errors, msg)
}
if (!length(errors)) NULL else errors
}
## 3.ii. Within rows of "sampleMap" corresponding to a single value in the
## "assay" column, there can be no duplicated values in the "colname" column
.uniqueNamesInAssays <- function(object) {
lcheckdups <- colnames(object)
logchecks <- any(vapply(lcheckdups, FUN = function(x) {
as.logical(anyDuplicated(x))
}, FUN.VALUE = logical(1L)))
if (!logchecks)
NULL
else
"All colname identifiers in assays must be unique"
}
.validMultiAssayExperiment <- function(object) {
if (length(experiments(object)) != 0L) {
c(.checkExperimentList(object),
.checkSampleMapNamesClass(object),
.uniqueNamesInAssays(object),
.checkSampleNames(object)
)
}
}
S4Vectors::setValidity2("MultiAssayExperiment", .validMultiAssayExperiment)
.hasOldAPI <- function(object) {
isTRUE(.hasSlot(object, "Elist")) || isTRUE(.hasSlot(object, "pData"))
}
#' @exportMethod show
#' @describeIn MultiAssayExperiment Show method for a
#' \code{MultiAssayExperiment}
setMethod("show", "MultiAssayExperiment", function(object) {
if (.hasOldAPI(object)) {
stop("MultiAssayExperiment is outdated, please run updateObject()")
}
o_class <- class(object)
o_len <- length(object)
o_names <- names(object)
if (length(o_names) == 0L) {
o_names <- "none"
}
c_elist <- class(experiments(object))
c_mp <- class(colData(object))
c_sm <- class(sampleMap(object))
cat(sprintf("A %s", o_class),
"object of", o_len, "listed\n",
ifelse(o_len == 1L, "experiment", "experiments"),
"with",
ifelse(all(o_names == "none"), "no user-defined names",
ifelse(length(o_names) == 1L, "a user-defined name",
"user-defined names")),
ifelse(length(o_len) == 0L, "or", "and"),
ifelse(length(o_len) == 0L, "classes.",
ifelse(o_len == 1L,
"respective class.\n", "respective classes.\n")),
"Containing an ")
show(experiments(object))
cat("Functionality:\n experiments() - obtain the ",
sprintf("%s", c_elist), " instance",
"\n colData() - the primary/phenotype DataFrame",
"\n sampleMap() - the sample coordination DataFrame",
"\n `$`, `[`, `[[` - extract colData columns, subset, or experiment",
"\n *Format() - convert ", "into a long or wide DataFrame",
"\n assays() - convert ", sprintf("%s", c_elist),
" to a SimpleList of matrices",
"\n exportClass() - save data to flat files\n",
sep = "")
})
#' @name MultiAssayExperiment-methods
#' @title Accessing and modifying information in MultiAssayExperiment
#'
#' @description A set of accessor and setter generic functions to extract
#' either the \code{sampleMap}, the \code{\link{ExperimentList}},
#' \code{colData}, or \code{metadata} slots of a
#' \code{\link{MultiAssayExperiment}} object
#'
#' @section Accessors:
#' Eponymous names for accessing \code{MultiAssayExperiment} slots with the
#' exception of the \link{ExperimentList} accessor named \code{experiments}.
#' \itemize{
#' \item colData: Access the \code{colData} slot
#' \item sampleMap: Access the \code{sampleMap} slot
#' \item experiments: Access the \link{ExperimentList} slot
#' \item `[[`: Access the \link{ExperimentList} slot
#' \item `$`: Access a column in \code{colData}
#' }
#'
#' @section Setters:
#' Setter method values (i.e., '\code{function(x) <- value}'):
#' \itemize{
#' \item experiments<-: An \code{\link{ExperimentList}} object
#' containing experiment data of supported classes
#' \item sampleMap<-: A \code{\link{DataFrame}} object relating
#' samples to biological units and assays
#' \item colData<-: A \code{\link{DataFrame}} object describing the
#' biological units
#' \item metadata<-: A \code{list} object of metadata
#' \item `[[<-`: Equivalent to the \code{experiments<-} setter method for
#' convenience
#' \item `$<-`: A vector to replace the indicated column in \code{colData}
#' }
#'
#' @param object,x A \code{MultiAssayExperiment} object
#'
#' @param name A column in \code{colData}
#'
#' @param value See details.
#'
#' @param ... Argument not in use
#'
#' @return Accessors: Either a \code{sampleMap}, \code{ExperimentList}, or
#' \code{DataFrame} object
#' @return Setters: A \code{MultiAssayExperiment} object
#'
#' @example inst/scripts/MultiAssayExperiment-methods-Ex.R
#'
#' @aliases experiments sampleMap experiments<- sampleMap<-
NULL
### - - - - - - - - - - - - - - - - - - - - - - -
### Accessor methods
###
#' @export
setGeneric("sampleMap", function(x) standardGeneric("sampleMap"))
#' @exportMethod sampleMap
#' @rdname MultiAssayExperiment-methods
setMethod("sampleMap", "MultiAssayExperiment", function(x)
getElement(x, "sampleMap"))
#' @export
setGeneric("experiments", function(x) standardGeneric("experiments"))
#' @exportMethod experiments
#' @rdname MultiAssayExperiment-methods
setMethod("experiments", "MultiAssayExperiment", function(x)
getElement(x, "ExperimentList"))
#' @exportMethod colData
#' @rdname MultiAssayExperiment-methods
setMethod("colData", "MultiAssayExperiment", function(x, ...) {
getElement(x, "colData")
})
#' @exportMethod metadata
#' @rdname MultiAssayExperiment-methods
setMethod("metadata", "MultiAssayExperiment", function(x)
c(getElement(x, "metadata"), drops = getElement(x, "drops"))
)
### - - - - - - - - - - - - - - - - - - - - - - - -
### Getters
###
#' @exportMethod length
#' @describeIn MultiAssayExperiment Get the length of ExperimentList
setMethod("length", "MultiAssayExperiment", function(x)
length(experiments(x))
)
#' @exportMethod names
#' @describeIn MultiAssayExperiment Get the names of the ExperimentList
setMethod("names", "MultiAssayExperiment", function(x)
names(experiments(x))
)
### - - - - - - - - - - - - - - - - - - - - - - - -
### Replacers
###
#' @export
setGeneric("sampleMap<-", function(object, value)
standardGeneric("sampleMap<-"))
#' @exportMethod sampleMap<-
#' @rdname MultiAssayExperiment-methods
setReplaceMethod("sampleMap", c("MultiAssayExperiment", "DataFrame"),
function(object, value) {
if (isEmpty(value))
value <- DataFrame(assay = factor(), primary = character(),
colname = character())
rebliss <- .harmonize(experiments(object),
colData(object),
value)
BiocBaseUtils::setSlots(object,
ExperimentList = rebliss[["experiments"]],
colData = rebliss[["colData"]],
sampleMap = rebliss[["sampleMap"]],
check = FALSE
)
}
)
#' @rdname MultiAssayExperiment-methods
setReplaceMethod("sampleMap", c("MultiAssayExperiment", "ANY"),
function(object, value) {
stopifnot(is.data.frame(value))
value <- as(value, "DataFrame")
`sampleMap<-`(object, value)
}
)
#' @export
setGeneric("experiments<-", function(object, value)
standardGeneric("experiments<-"))
setGeneric("drops<-", function(x, ..., value) standardGeneric("drops<-"))
#' @exportMethod experiments<-
#' @rdname MultiAssayExperiment-methods
setReplaceMethod("experiments", c("MultiAssayExperiment", "ExperimentList"),
function(object, value) {
if (!any(names(object) %in% names(value)) && !isEmpty(object)) {
drops(object) <-
list(experiments = setdiff(names(object), names(value)))
warning("'experiments' dropped; see 'metadata'", call. = FALSE)
}
o_cnames <- colnames(object)
v_cnames <- colnames(value)
if (identical(o_cnames, v_cnames)) {
BiocBaseUtils::setSlots(
object = object,
ExperimentList = value,
check = FALSE
)
} else {
samplemap <- sampleMap(object)
if (all(names(o_cnames) %in% names(v_cnames))) {
levels <- names(v_cnames)
ordernames <- names(Filter(length, v_cnames))
samplemap <- listToMap(mapToList(samplemap)[ordernames])
samplemap[["assay"]] <-
factor(samplemap[["assay"]], levels = levels)
}
rebliss <- .harmonize(value, colData(object), samplemap)
BiocBaseUtils::setSlots(
object = object,
ExperimentList = rebliss[["experiments"]],
colData = rebliss[["colData"]],
sampleMap = rebliss[["sampleMap"]],
check = FALSE
)
}
}
)
#' @exportMethod experiments<-
#' @rdname MultiAssayExperiment-methods
setReplaceMethod("experiments", c("MultiAssayExperiment", "List"),
function(object, value) {
value <- as(value, "ExperimentList")
experiments(object) <- value
object
}
)
#' @exportMethod colData<-
#' @rdname MultiAssayExperiment-methods
setReplaceMethod("colData", c("MultiAssayExperiment", "DataFrame"),
function(x, value) {
x_rnames <- rownames(colData(x))
v_rnames <- rownames(value)
if (!any(v_rnames %in% x_rnames) && !isEmpty(value))
stop("'rownames(value)' have no match in 'rownames(colData)';\n ",
"See '?renamePrimary' for renaming primary units")
if (identical(x_rnames, v_rnames))
BiocBaseUtils::setSlots(
object = x,
colData = value,
check = FALSE
)
else {
rebliss <- .harmonize(experiments(x), value, sampleMap(x))
BiocBaseUtils::setSlots(
object = x,
ExperimentList = rebliss[["experiments"]],
colData = rebliss[["colData"]],
sampleMap = rebliss[["sampleMap"]],
check = FALSE
)
}
}
)
#' @rdname MultiAssayExperiment-methods
setReplaceMethod("colData", c("MultiAssayExperiment", "ANY"),
function(x, value) {
if (!is.data.frame(value))
stop("'colData' can be either 'data.frame' or 'DataFrame'")
value <- as(value, "DataFrame")
`colData<-`(x, value)
}
)
.rearrangeMap <- function(sampMap) {
return(DataFrame(assay = factor(sampMap[["assayname"]]),
primary = sampMap[["primary"]],
colname = sampMap[["assay"]]))
}
#' @exportMethod metadata<-
#' @rdname MultiAssayExperiment-methods
setReplaceMethod("metadata", c("MultiAssayExperiment", "ANY"),
function(x, ..., value) {
slot(x, "metadata") <- value
return(x)
})
setReplaceMethod("drops", c("MultiAssayExperiment", "ANY"),
function(x, ..., value) {
anydrops <- getElement(x, "drops")[["experiments"]]
if (length(anydrops))
value[["experiments"]] <- union(anydrops, value[["experiments"]])
slot(x, "drops") <- value
return(x)
})
#' @exportMethod $<-
#' @rdname MultiAssayExperiment-methods
setReplaceMethod("$", "MultiAssayExperiment", function(x, name, value) {
colData(x)[[name]] <- value
return(x)
})
#' @exportMethod names<-
#' @rdname MultiAssayExperiment-methods
setReplaceMethod("names", c("MultiAssayExperiment", "ANY"),
function(x, value)
{
if (!is.character(value))
stop("'value' must be a character vector",
"in names(x) <- value")
if (length(value) != length(x))
stop("experiment names and experiments not equal in length")
explist <- experiments(x)
oldNames <- names(explist)
names(explist) <- value
sampmap <- sampleMap(x)
map <- .setNames(value, oldNames)
sampmap[, "assay"] <-
factor(unname(map[sampmap[["assay"]]]), levels = value)
BiocBaseUtils::setSlots(x,
ExperimentList = explist,
sampleMap = sampmap,
check = FALSE)
})
#' @exportMethod colnames<-
#' @rdname MultiAssayExperiment-methods
setReplaceMethod("colnames", c("MultiAssayExperiment", "List"),
function(x, value)
{
if (!is(value, "CharacterList"))
stop("'value' must be a 'CharacterList' in 'colnames(x) <- value'")
if (length(value) != length(x))
stop("'colnames(x)' and 'value' not equal in length")
cnames <- colnames(x)
if (!identical(lengths(value), lengths(cnames)))
stop("'value' names and lengths should all be identical to 'names(x)'")
samplemap <- sampleMap(x)
splitmap <- mapToList(samplemap)
splitmap <- Map(function(x, y) {
x[["colname"]] <- y
x
}, x = splitmap, y = value)
exps <- experiments(x)
exps <- S4Vectors::mendoapply(function(x, y) {
colnames(x) <- y
x
}, x = exps, y = value)
BiocBaseUtils::setSlots(
object = x,
ExperimentList = exps,
sampleMap = listToMap(splitmap)
)
})
#' @exportMethod colnames<-
#' @rdname MultiAssayExperiment-methods
setReplaceMethod("colnames", c("MultiAssayExperiment", "list"),
function(x, value)
{
value <- as(value, "CharacterList")
colnames(x) <- value
x
})
#' @exportMethod updateObject
#'
#' @param verbose logical (default FALSE) whether to print extra messages
#'
#' @describeIn MultiAssayExperiment Update old serialized MultiAssayExperiment
#' objects to new API
setMethod("updateObject", "MultiAssayExperiment",
function(object, ..., verbose = FALSE) {
if (verbose)
message("updateObject(object = 'MultiAssayExperiment')")
oldEL <- try(object@ExperimentList, silent = TRUE)
if (is(oldEL, "try-error")) {
explist <- ExperimentList(object@Elist@listData)
samplemap <- .rearrangeMap(object@sampleMap)
} else {
explist <- experiments(object)
samplemap <- sampleMap(object)
}
oldCD <- try(object@colData, silent = TRUE)
if (is(oldCD, "try-error"))
coldata <- object@pData
else
coldata <- colData(object)
explist <- updateObject(explist, ..., verbose = verbose)
coldata <- updateObject(coldata, ..., verbose = verbose)
samplemap <- updateObject(samplemap, ..., verbose = verbose)
BiocBaseUtils::setSlots(
object,
ExperimentList = explist,
colData = coldata,
sampleMap = samplemap,
check=FALSE
)
}
)
.mergeColData <- function(inlist) {
CDbyEXP <- lapply(names(inlist),
function(i, x) {
tryCatch({
S4Vectors::DataFrame(colData(x[[i]]), experiment_name = i)
}, error = function(e) {
S4Vectors::DataFrame(row.names = colnames(x[[i]]))
} )
}, x = inlist
)
colDatas <- Filter(function(y) { !isEmpty(y) }, CDbyEXP)
if (length(colDatas)) {
rnames <- unlist(lapply(colDatas, rownames))
res <- Reduce(function(x, y) {
S4Vectors::merge(
x, y, by = intersect(names(x), names(y)),
all = TRUE, sort = FALSE
)
}, colDatas)
rownames(res) <- rnames
} else {
res <- S4Vectors::DataFrame(
row.names = unlist(lapply(CDbyEXP, rownames))
)
}
res
}
#' @rdname MultiAssayExperiment-class
#'
#' @name coerce-MultiAssayExperiment
#'
#' @aliases coerce,list,MultiAssayExperiment-method
#' coerce,List,MultiAssayExperiment-method
#'
#' @section
#' coercion:
#' Convert a `list` or S4 `List` to a MultiAssayExperiment object using the
#' \link[methods]{as} function.
#'
#' In the following example, `x` is either a `list` or \linkS4class{List}:
#'
#' `as(x, "MultiAssayExperiment")`
#'
#' Convert a `MultiAssayExperiment` to `MAF` class object using the
#' \link[methods]{as} function.
#'
#' In the following example, `x` is a \linkS4class{MultiAssayExperiment}:
#'
#' `MultiAssayExperimentToMAF(x)`
#'
#' @md
#'
#' @exportMethod coerce
setAs("list", "MultiAssayExperiment", function(from) {
newfrom <- as(from, "List")
as(newfrom, "MultiAssayExperiment")
}
)
setAs("List", "MultiAssayExperiment", function(from) {
metaf <- metadata(from)
explist <- as(from, "ExperimentList")
colData <- .mergeColData(from)
MultiAssayExperiment(
experiments = explist, colData = colData, metadata = metaf
)
}
)