-
Notifications
You must be signed in to change notification settings - Fork 25
/
AllClasses.R
7751 lines (7472 loc) · 297 KB
/
AllClasses.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
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
## =========================================================================##
## =========================================================================##
## Class definitions and contructors ##
## =========================================================================##
## =========================================================================##
## ===========================================================================
## Some helpers
## ---------------------------------------------------------------------------
## Check for the class of object x and its length and cast error if wrong
checkClass <- function(x, class, length=NULL, verbose=FALSE,
mandatory=TRUE)
{
if(mandatory && missing(x))
stop("Argument '", substitute(x), "' missing with no default",
call.=verbose)
msg <- paste("'", substitute(x), "' must be object of class ",
paste("'", class, "'", sep="", collapse=" or "), sep="")
fail <- !any(sapply(class, function(c, y) is(y, c), x))
if(!is.null(length) && length(x) != length)
{
if(!is.null(x))
{
fail <- TRUE
msg <- paste(msg, "of length", length)
}
}
if(fail) stop(msg, call.=verbose) else invisible(NULL)
}
## ===========================================================================
## flowFrame
## ---------------------------------------------------------------------------
## A container for flow cytometry measurements with slots exprs, parameters
## and description. exprs contains measurement values, description contains
## information from file headers of FCS file and parameters contains
## information about the FCS measurement parameters (i.e. channels) available.
## Exprs is a matrix (values are stored in internal memory)
## ---------------------------------------------------------------------------
#' 'flowFrame': a class for storing observed quantitative properties for a
#' population of cells from a FACS run
#'
#' This class represents the data contained in a \acronym{FCS} file or similar
#' data structure. There are three parts of the data: \enumerate{
#' \item a numeric matrix of the raw measurement values with \kbd{rows=events}
#' and \kbd{columns=parameters}
#' \item annotation for the parameters (e.g., the measurement channels, stains,
#' dynamic range)
#' \item additional annotation provided through keywords in the \acronym{FCS}
#' file
#' }
#'
#'
#'
#' Objects of class \code{flowFrame} can be used to hold arbitrary data of cell
#' populations, acquired in flow-cytometry.
#'
#' \acronym{FCS} is the Data File Standard for Flow Cytometry, the current
#' version is FCS 3.0. See the vignette of this package for additional
#' information on using the object system for handling of flow-cytometry data.
#'
#' @name flowFrame-class
#' @aliases flowFrame-class flowFrame [,flowFrame,ANY-method
#' [,flowFrame,filter-method [,flowFrame,filterResult-method $.flowFrame exprs
#' exprs<- exprs,flowFrame-method exprs<-,flowFrame,matrix-method
#' exprs<-,flowFrame,ANY-method initialize,flowFrame-method
#' head,flowFrame-method tail,flowFrame-method description
#' description,flowFrame-method description<-,flowFrame,list-method
#' description<-,flowFrame,ANY-method show,flowFrame-method
#' plot,flowFrame,ANY-method plot,flowFrame-method summary,flowFrame-method
#' ncol,flowFrame-method nrow,flowFrame-method dim dim,flowFrame-method
#' featureNames featureNames,flowFrame-method colnames,flowFrame-method
#' colnames<- colnames<-,flowFrame-method names names,flowFrame-method range
#' range,flowFrame-method cbind2,flowFrame,matrix-method
#' cbind2,flowFrame,numeric-method
#' compensate,flowFrame,matrix-method compensate,flowFrame,data.frame-method
#' compensate,flowFrame,compensation-method ==,flowFrame,filterResult-method
#' ==,flowFrame,flowFrame-method <,flowFrame,ANY-method <=,flowFrame,ANY-method
#' >,flowFrame,ANY-method >=,flowFrame,ANY-method spillover,flowFrame-method
#' spillover
#' @docType class
#'
#' @slot exprs {Object of class \code{matrix} containing the
#' measured intensities. Rows correspond to cells, columns to the
#' different measurement channels. The \code{colnames} attribute of
#' the matrix is supposed to hold the names or identifiers for the
#' channels. The \code{rownames} attribute would usually not be set.
#' }
#' @slot parameters {An
#' \code{\link[Biobase:class.AnnotatedDataFrame]{AnnotatedDataFrame}}
#' containing information about each column of the
#' \code{flowFrame}. This will generally be filled in by
#' \code{read.FCS} or similar functions using data from the
#' \acronym{FCS} keywords describing the parameters.}
#' @slot description {A list containing the meta data included
#' in the FCS file.}
#'
#' @section Creating Objects:
#' Objects can be created using\cr \code{
#' new("flowFrame",}\cr \code{ exprs = ...., Object of class matrix}\cr \code{
#' parameters = ...., Object of class AnnotatedDataFrame}\cr \code{ description
#' = ...., Object of class list}\cr \code{ )}\cr
#'
#' or the constructor \code{flowFrame}, with mandatory arguments \code{exprs}
#' and optional arguments \code{parameters} and \code{description}.
#'
#' \code{flowFrame(exprs, parameters, description=list())}
#'
#' To create a \code{flowFrame} directly from an \acronym{FCS} file, use
#' function \code{\link[flowCore]{read.FCS}}. This is the recommended and
#' safest way of object creation, since \code{read.FCS} will perform basic data
#' quality checks upon import. Unless you know exactly what you are doing,
#' creating objects using \code{new} or the constructor is discouraged.
#'
#' @section Methods:
#' There are separate documentation pages for most of the methods
#' listed here which should be consulted for more details.
#' \describe{
#' \item{[}{Subsetting. Returns an object of class \code{flowFrame}.
#' The subsetting is applied to the \code{exprs} slot, while the
#' \code{description} slot is unchanged. The syntax for subsetting is
#' similar to that of \code{\link[=data.frame]{data.frames}}. In
#' addition to the usual index vectors (integer and logical by
#' position, character by parameter names), \code{flowFrames} can be
#' subset via \code{\link{filterResult}} and
#' \code{\linkS4class{filter}} objects.
#'
#' \emph{Usage:}
#'
#' \code{ flowFrame[i,j]}
#'
#' \code{ flowFrame[filter,]}
#'
#' \code{ flowFrame[filterResult,]}
#'
#' Note that the value of argument \code{drop} is ignored when
#' subsetting \code{flowFrames}.
#'
#' }
#' \item{$}{Subsetting by channel name. This is similar to subsetting
#' of columns of \code{\link[=data.frame]{data.frames}}, i.e.,
#' \code{frame$FSC.H} is equivalent to \code{frame[, "FSC.H"]}. Note
#' that column names may have to be quoted if they are no valid R
#' symbols (e.g. \code{frame$"FSC-H"}).
#'
#' }
#' \item{exprs, exprs<-}{Extract or replace the raw data
#' intensities. The replacement value must be a numeric matrix with
#' colnames matching the parameter definitions. Implicit subsetting
#' is allowed (i.e. less columns in the replacement value compared to
#' the original \code{flowFrame}, but all have to be defined there).
#'
#' \emph{Usage:}
#'
#' \code{ exprs(flowFrame)}
#'
#' \code{ exprs(flowFrame) <- value}
#'
#' }
#' \item{head, tail}{Show first/last elements of the raw data matrix
#'
#' \emph{Usage:}
#'
#' \code{ head(flowFrame)}
#'
#' \code{ tail(flowFrame)}
#'
#' }
#' \item{description, description<-}{Extract the whole list
#' of annotation keywords and their corresponding values or replace values by keyword
#' (\code{description<-} is equivalent to \code{keyword<-}). Usually one would only be
#' interested in a subset of keywords, in which case the \code{keyword} method is
#' more appropriate. The optional \code{hideInternal} parameter can
#' be used to exclude internal FCS parameters starting
#' with \code{$}.
#'
#' \emph{Usage:}
#'
#' \code{ description(flowFrame)}
#'
#' \code{ description(flowFrame) <- value}
#'
#' }
#' \item{keyword, keyword<-}{Extract ore replace one or more entries
#' from the \code{description} slot by keyword. Methods are defined
#' for character vectors (select a keyword by name), functions
#' (select a keyword by evaluating a function on their content) and
#' for lists (a combination of the above). See \code{\link{keyword}}
#' for details.
#'
#' \emph{Usage:}
#'
#' \code{ keyword(flowFrame)}
#'
#' \code{ keyword(flowFrame, character)}
#'
#' \code{ keyword(flowFrame, list)}
#'
#' \code{ keyword(flowFrame) <- list(value) }
#'
#' }
#' \item{parameters, parameters<-}{Extract parameters and return an
#' object of class
#' \code{\link[Biobase:class.AnnotatedDataFrame]{AnnotatedDataFrame}},
#' or replace such an object. To access the actual parameter
#' annotation, use \code{pData(parameters(frame))}. Replacement is
#' only valid with
#' \code{\link[Biobase:class.AnnotatedDataFrame]{AnnotatedDataFrames}}
#' containing all varLabels \code{name}, \code{desc}, \code{range},
#' \code{minRange} and \code{maxRange}, and matching entries in the
#' \code{name} column to the colnames of the \code{exprs} matrix. See
#' \code{\link{parameters}} for more details.
#'
#' \emph{Usage:}
#'
#' \code{ parameters(flowFrame)}
#'
#' \code{ parameters(flowFrame) <- value}
#'
#' }
#' \item{show}{
#'
#' Display details about the \code{flowFrame} object.
#'
#' }
#' \item{summary}{Return descriptive statistical summary (min, max,
#' mean and quantile) for each channel
#'
#' \emph{Usage:}
#'
#' \code{ summary(flowFrame)}
#'
#' }
#' \item{plot}{Basic plots for \code{flowFrame} objects. If the object
#' has only a single parameter this produces a
#' \code{\link[graphics:hist]{histogram}}. For exactly two parameters
#' we plot a bivariate density map (see
#' \code{\link[graphics]{smoothScatter}}
#' and for more than two parameters we produce a simple
#' \code{\link[lattice]{splom}} plot. To select specific parameters
#' from a \code{flowFrame} for plotting, either subset the object or
#' specify the parameters as a character vector in the second
#' argument to \code{plot}. The smooth parameters lets you toggle
#' between density-type
#' \code{\link[graphics]{smoothScatter}}
#' plots and regular scatterplots. This simple method still uses the legacy
#' \code{\link[flowViz:flowViz-package]{flowViz}} package. For far more sophisticated
#' plotting of flow cytometry data, see the
#' \code{\link[ggcyto:ggcyto]{ggcyto}} package.
#'
#' \emph{Usage:}
#'
#' \code{ plot(flowFrame, ...)}
#'
#' \code{ plot(flowFrame, character, ...)}
#'
#' \code{ plot(flowFrame, smooth=FALSE, ...)}
#'
#' }
#' \item{ncol, nrow, dim}{Extract the dimensions of the data matrix.
#'
#' \emph{Usage:}
#'
#' \code{ ncol(flowFrame)}
#'
#' \code{ nrow(flowFrame)}
#'
#' \code{ dim(flowFrame)}
#'
#' }
#' \item{featureNames, colnames, colnames<-}{. \code{colnames} and
#' \code{featureNames} are synonyms, they extract parameter names (i.e., the
#' colnames of the data matrix) .
#' For \code{colnames} there is
#' also a replacement method. This will update the \code{name} column
#' in the \code{parameters} slot as well.
#'
#' \emph{Usage:}
#'
#' \code{ featureNames(flowFrame)}
#'
#' \code{ colnames(flowFrame)}
#'
#' \code{ colnames(flowFrame) <- value}
#'
#' }
#' \item{names}{Extract pretty formated names of the parameters
#' including parameter descriptions.
#'
#' \emph{Usage:}
#'
#' \code{ names(flowFrame)}
#'
#' }
#' \item{identifier}{Extract GUID of a \code{flowFrame}. Returns the
#' file name if no GUID is available. See \code{\link{identifier}}
#' for details.
#'
#' \emph{Usage:}
#'
#' \code{ identifier(flowFrame)}
#' }
#' \item{range}{Get instrument or actual data range of the \code{flowFame}. Note that
#' instrument dynamic range is not necessarily the same as the range of the actual data values, but
#' the theoretical range of values the measurement instrument was
#' able to capture. The values of the dynamic range will be
#' transformed when using the transformation methods for\code{flowFrames}.
#'
#' parameters:
#'
#' x: flowFrame object.
#'
#' type: Range type. either "instrument" or "data". Default is "instrument"
#'
#' \emph{Usage:}
#'
#' \code{ range(x, type = "data")}
#'
#' }
#' \item{each_row, each_col}{Apply functions over rows or columns of
#' the data matrix. These are convenience methods. See
#' \code{\link{each_col}} for details.
#'
#' \emph{Usage:}
#'
#' \code{ each_row(flowFrame, function, ...)}
#'
#' \code{ each_col(flowFrame, function, ...)}
#' }
#' \item{transform}{Apply a transformation function on a
#' \code{flowFrame} object. This uses R's
#' \code{\link[base]{transform}} function by treating the
#' \code{flowFrame} like a regular \code{data.frame}. \code{flowCore}
#' provides an additional inline mechanism for transformations (see
#' \code{\link{\%on\%}}) which is strictly more limited
#' than the out-of-line transformation described here.
#'
#' \emph{Usage:}
#'
#' \code{ transform(flowFrame, translist, ...)}
#'
#' }
#' \item{filter}{Apply a \code{\linkS4class{filter}} object on a
#' \code{flowFrame} object. This returns an object of class
#' \code{\link{filterResult}}, which could then be used for
#' subsetting of the data or to calculate summary statistics. See
#' \code{\link{filter}} for details.
#'
#' \emph{Usage:}
#'
#' \code{ filter(flowFrame, filter)}
#'
#' }
#' \item{split}{Split \code{flowFrame} object according to a
#' \code{\link{filter}}, a \code{\link{filterResult}} or a
#' \code{factor}. For most types of filters, an optional
#' \code{flowSet=TRUE} parameter will create a
#' \code{\linkS4class{flowSet}} rather than a simple list. See
#' \code{\link{split}} for details.
#'
#' \emph{Usage:}
#'
#' \code{ split(flowFrame, filter, flowSet=FALSE, ...)}
#'
#' \code{ split(flowFrame, filterResult, flowSet=FALSE, ...)}
#'
#' \code{ split(flowFrame, factor, flowSet=FALSE, ...)}
#'
#' }
#' \item{Subset}{Subset a \code{flowFrame} according to a \code{filter}
#' or a logical vector. The same can be done using the standard
#' subsetting operator with a \code{filter}, \code{filterResult}, or
#' a logical vector as first argument.
#'
#' \emph{Usage:}
#'
#' \code{ Subset(flowFrame, filter)}
#'
#' \code{ Subset(flowFrame, logical)}
#'
#' }
#' \item{cbind2}{Expand a \code{flowFrame} by the data in a
#' \code{numeric matrix} of the same length. The \code{matrix} must
#' have column names different from those of the
#' \code{flowFrame}. The additional method for \code{numerics} only
#' raises a useful error message.
#'
#' \emph{Usage:}
#'
#' \code{ cbind2(flowFrame, matrix)}
#'
#' \code{ cbind2(flowFrame, numeric)}
#'
#' }
#' \item{compensate}{Apply a compensation matrix (or a
#' \code{\linkS4class{compensation}} object) on a \code{flowFrame}
#' object. This returns a compensated \code{flowFrame}.
#'
#' \emph{Usage:}
#'
#' \code{ compensate(flowFrame, matrix)}
#' \code{ compensate(flowFrame, data.frame)}
#'
#' }
#' \item{decompensate}{Reverse the application of a compensation matrix (or a
#' \code{\linkS4class{compensation}} object) on a \code{flowFrame}
#' object. This returns a decompensated \code{flowFrame}.
#'
#' \emph{Usage:}
#'
#' \code{ decompensate(flowFrame, matrix)}
#' \code{ decompensate(flowFrame, data.frame)}
#'
#' }
#' \item{spillover}{Extract spillover matrix from description slot if
#' present. It is equivalent to
#' \code{keyword(x, c("spillover", "SPILL", "$SPILLOVER"))}
#' Thus will simply return a list of keywords value for "spillover", "SPILL" and "$SPILLOVER".
#'
#' \emph{Usage:}
#'
#' \code{ spillover(flowFrame)}
#'
#' }
#' \item{==}{Test equality between two \code{flowFrames}}
#' \item{<, >, <=, >=}{These operators basically treat the
#' \code{flowFrame} as a numeric matrix.}
#' \item{\code{initialize(flowFrame)}:}{Object instantiation, used
#' by \code{new}; not to be called directly by the user.}
#' }
#'
#' @author
#'
#' F. Hahne, B. Ellis, P. Haaland and N. Le Meur
#' @seealso
#'
#' \code{\linkS4class{flowSet}}, \code{\link{read.FCS}}
#' @keywords classes
#' @examples
#'
#' ## load example data
#' data(GvHD)
#' frame <- GvHD[[1]]
#'
#' ## subsetting
#' frame[1:4,]
#' frame[,3]
#' frame[,"FSC-H"]
#' frame$"SSC-H"
#'
#' ## accessing and replacing raw values
#' head(exprs(frame))
#' exprs(frame) <- exprs(frame)[1:3000,]
#' frame
#' exprs(frame) <- exprs(frame)[,1:6]
#' frame
#'
#' ## access FCS keywords
#' head(keyword(frame))
#' keyword(frame, c("FILENAME", "$FIL"))
#'
#' ## parameter annotation
#' parameters(frame)
#' pData(parameters(frame))
#'
#' ## summarize frame data
#' summary(frame)
#'
#' ## plotting
#' plot(frame)
#' if(require(flowViz)){
#' plot(frame)
#' plot(frame, c("FSC-H", "SSC-H"))
#' plot(frame[,1])
#' plot(frame, c("FSC-H", "SSC-H"), smooth=FALSE)
#' }
#'
#' ## frame dimensions
#' ncol(frame)
#' nrow(frame)
#' dim(frame)
#'
#' ## accessing and replacing parameter names
#' featureNames(frame)
#' all(featureNames(frame) == colnames(frame))
#' colnames(frame) <- make.names(colnames(frame))
#' colnames(frame)
#' parameters(frame)$name
#' names(frame)
#'
#' ## accessing a GUID
#' identifier(frame)
#' identifier(frame) <- "test"
#'
#' ## range of a frame
#' range(frame) #instrument range
#' range(frame, type = "data") #actual data range
#' range(frame)$FSC.H
#'
#' ## iterators
#' head(each_row(frame, mean))
#' head(each_col(frame, mean))
#'
#' ## transformation
#' opar <- par(mfcol=c(1:2))
#' if(require(flowViz))
#' plot(frame, c("FL1.H", "FL2.H"))
#' frame <- transform(frame, transformList(c("FL1.H", "FL2.H"), log))
#' if(require(flowViz))
#' plot(frame, c("FL1.H", "FL2.H"))
#' par(opar)
#' range(frame)
#'
#' ## filtering of flowFrames
#' rectGate <- rectangleGate(filterId="nonDebris","FSC.H"=c(200,Inf))
#' fres <- filter(frame, rectGate)
#' summary(fres)
#'
#' ## splitting of flowFrames
#' split(frame, rectGate)
#' split(frame, rectGate, flowSet=TRUE)
#' split(frame, fres)
#' f <- cut(exprs(frame$FSC.H), 3)
#' split(frame, f)
#'
#' ## subsetting according to filters and filter results
#' Subset(frame, rectGate)
#' Subset(frame, fres)
#' Subset(frame, as.logical(exprs(frame$FSC.H) < 300))
#' frame[rectGate,]
#' frame[fres,]
#'
#' ## accessing the spillover matrix
#' try(spillover(frame))
#'
#' ## check equality
#' frame2 <- frame
#' frame == frame2
#' exprs(frame2) <- exprs(frame)*2
#' frame == frame2
#'
#'
#' @export
setClass("flowFrame",
representation=representation(exprs="matrix",
parameters="AnnotatedDataFrame",
description="list"),
prototype=list(exprs=matrix(numeric(0),
nrow=0,
ncol=0),
parameters=new("AnnotatedDataFrame"),
description=list(note="empty")))
## helper function to create empty AnnotatedDataFrame for the parameters slot
parDefault <- function(exp)
{
vm <- data.frame(labelDescription=c(name="Name of Parameter",
desc="Description of Parameter",
range="Range of Parameter",
minRange="Minimum Parameter Value after Transformation",
maxRange="Maximum Parameter Value after Transformation"))
cols <- colnames(exp)
pd <- data.frame(name=cols, desc=cols,
range=apply(exp, 2, max, na.rm=TRUE),
minRange=apply(exp, 2, min, na.rm=TRUE),
maxRange=apply(exp, 2, max, na.rm=TRUE)
, row.names = paste0("$P", seq_along(cols)))
new("AnnotatedDataFrame", pd, vm)
}
## check parameter AnnotatedDataFrame for validity
isValidParameters <- function(parameters, exprs)
{
checkClass(parameters, "AnnotatedDataFrame")
if(!all(c("name", "desc", "range", "minRange", "maxRange")
%in% varLabels(parameters)))
stop("The following columns are mandatory:\n 'name', 'desc',",
"'range', 'minRange', 'maxRange'", call.=FALSE)
if(!missing(exprs))
if(!all(colnames(exprs) %in% parameters$name))
stop("parameter description doesn't match colnames of the ",
"data matrix", call.=FALSE)
return(TRUE)
}
## constructor
#' @export
flowFrame <- function(exprs, parameters, description=list())
{
if(!is.matrix(exprs) || !is.numeric(exprs) || is.null(colnames(exprs)))
stop("Argument 'exprs' must be numeric matrix with colnames ",
"attribute set", call.=FALSE)
if(missing(parameters))
parameters <- parDefault(exprs)
else
isValidParameters(parameters, exprs)
checkClass(description, "list")
fr <- new("flowFrame", exprs=exprs, parameters=parameters,description=description)
tmp <- tempfile()
on.exit(unlink(tmp))
suppressMessages(write.FCS(fr, tmp))
suppressMessages(read.FCS(tmp))
}
## ===========================================================================
## flowSet
## ---------------------------------------------------------------------------
## A collection of several cytoFrames making up one experiment. Slots
## frames, phenoData, colnames. Frames contains the cytoFrame objects,
## phenoData the experiment meta data and colnames the channel names.
## ---------------------------------------------------------------------------
#' 'flowSet': a class for storing flow cytometry raw data from quantitative
#' cell-based assays
#'
#' This class is a container for a set of \code{\linkS4class{flowFrame}}
#' objects
#'
#'
#' @name flowSet-class
#' @aliases flowSet-class flowSet [,flowSet-method [,flowSet,ANY-method
#' $,flowSet-method [[,flowSet-method [[,flowSet,ANY-method [[<-,flowSet-method
#' [[<-,flowSet,ANY,ANY,flowFrame-method [[<-,flowFrame-method
#' fsApply,flowSet-method show,flowSet-method length,flowSet-method
#' colnames,flowSet-method colnames<-,flowSet-method identifier,flowSet-method
#' identifier<-,flowSet,ANY-method sampleNames,flowSet-method
#' sampleNames<-,flowSet,ANY-method phenoData,flowSet-method
#' phenoData<-,flowSet,ANY-method phenoData<-,flowSet,phenoData-method
#' pData,flowSet-method pData<-,flowSet,data.frame-method
#' plot,flowSet,ANY-method plot,flowSet-method varLabels,flowSet-method
#' varLabels<-,flowSet-method varLabels<-,flowSet,ANY-method
#' varMetadata,flowSet-method varMetadata<-,flowSet,ANY-method
#' compensate,flowSet,ANY-method compensate,flowSet,list-method
#' compensate,flowSet,data.frame-method
#' rbind2,flowSet,missing rbind2,flowSet,flowSet-method
#' rbind2,flowSet,flowSet,missing-method rbind2,flowSet,flowFrame-method
#' rbind2,flowFrame,flowSet-method rbind2,flowSet,missing-method
#' summary,flowSet-method
#' @docType class
#'
#' @slot frames An \code{\link[base:environment]{environment}}
#' containing one or more \code{\linkS4class{flowFrame}} objects.
#' @slot phenoData An
#' \code{\link[Biobase:class.AnnotatedDataFrame]{AnnotatedDataFrame}}
#' containing the phenotypic data for the whole data set. Each row
#' corresponds to one of the \code{\linkS4class{flowFrame}}s in the
#' \code{frames} slot. The \code{sampleNames} of \code{phenoData}
#' (see below) must match the names of the
#' \code{\linkS4class{flowFrame}} in the \code{frames} environment.
#'
#' @section Creating Objects:
#'
#' Objects can be created using\cr \code{ new('flowSet',}\cr \code{ frames =
#' ...., # environment with flowFrames}\cr \code{ phenoData = .... # object of
#' class AnnotatedDataFrame}\cr \code{ colnames = .... # object of class
#' character}\cr \code{ )}\cr
#'
#' or via the constructor \code{flowSet}, which takes arbitrary numbers of
#' flowFrames, either as a list or directly as arguments, along with an
#' optional \code{\link[Biobase:class.AnnotatedDataFrame]{AnnotatedDataFrame}}
#' for the \code{phenoData} slot and a \code{character} scalar for the
#' \code{name} by which the object can be referenced.
#'
#' \code{flowSet(..., phenoData)}
#'
#' Alternatively, \code{flowSets} can be coerced from \code{list} and
#' \code{environment} objects.
#'
#' \code{as(list("A"=frameA,"B"=frameB),"flowSet")}
#'
#' The safest and easiest way to create \code{flowSet}s directly from
#' \acronym{FCS} files is via the \code{\link{read.flowSet}} function, and
#' there are alternative ways to specify the files to read. See the separate
#' documentation for details.
#'
#' @section Methods:
#' \describe{
#'
#' \item{[, [[}{Subsetting. \code{x[i]} where \code{i} is a scalar,
#' returns a \code{flowSet} object, and \code{x[[i]]} a
#' \code{\linkS4class{flowFrame}} object. In this respect the
#' semantics are similar to the behavior of the subsetting operators
#' for lists. \code{x[i, j]} returns a \code{flowSet} for which the
#' parameters of each \code{\linkS4class{flowFrame}} have been subset
#' according to \code{j}, \code{x[[i,j]]} returns the subset of a
#' single \code{\linkS4class{flowFrame}} for all parameters in
#' \code{j}. Similar to data frames, valid values for \code{i} and
#' \code{j} are logicals, integers and characters.
#'
#' \emph{Usage:}
#'
#' \code{ flowSet[i]}
#'
#' \code{ flowSet[i,j]}
#'
#' \code{ flowSet[[i]]}
#'
#' }
#'
#' \item{$}{Subsetting by frame name. This will return a single
#' \code{\linkS4class{flowFrame}} object. Note that names may have to
#' be quoted if they are no valid R symbols
#' (e.g. \code{flowSet$"sample 1"}}
#'
#' \item{colnames, colnames<-}{Extract or replace the \code{colnames}
#' slot.
#'
#' \emph{Usage:}
#'
#' \code{ colnames(flowSet)}
#'
#' \code{ colnames(flowSet) <- value}
#'
#' }
#'
#' \item{identifier, identifier<-}{Extract or replace the \code{name}
#' item from the environment.
#'
#' \emph{Usage:}
#'
#' \code{ identifier(flowSet)}
#'
#' \code{ identifier(flowSet) <- value}
#'
#' }
#'
#'
#' \item{phenoData, phenoData<-}{Extract or replace the
#' \code{\link[Biobase:class.AnnotatedDataFrame]{AnnotatedDataFrame}}
#' from the \code{phenoData} slot.
#'
#' \emph{Usage:}
#'
#' \code{ phenoData(flowSet)}
#'
#' \code{ phenoData(flowSet) <- value}
#'
#' }
#'
#' \item{pData, pData<-}{Extract or replace the data frame (or columns
#' thereof) containing actual phenotypic information from the
#' \code{phenoData} slot.
#'
#' \emph{Usage:}
#'
#' \code{ pData(flowSet)}
#'
#' \code{ pData(flowSet)$someColumn <- value}
#'
#' }
#'
#' \item{varLabels, varLabels<-}{ Extract and set varLabels in the
#' \code{\link[Biobase:class.AnnotatedDataFrame]{AnnotatedDataFrame}}
#' of the \code{phenoData} slot.
#'
#' \emph{Usage:}
#'
#' \code{ varLabels(flowSet)}
#'
#' \code{ varLabels(flowSet) <- value}
#'
#' }
#'
#' \item{sampleNames}{Extract and replace sample names from the
#' \code{phenoData} object. Sample names correspond to frame
#' identifiers, and replacing them will also replace the \code{GUID}
#' slot for each frame. Note that \code{sampleName} need to be
#' unique.
#'
#' \emph{Usage:}
#'
#' \code{ sampleNames(flowSet)}
#'
#' \code{ sampleNames(flowSet) <- value}
#'
#' }
#'
#' \item{keyword}{Extract or replace keywords specified in a character
#' vector or a list from the \code{description} slot of each
#' frame. See \code{\link{keyword}} for details.
#'
#' \emph{Usage:}
#'
#' \code{ keyword(flowSet, list(keywords))}
#'
#' \code{ keyword(flowSet, keywords)}
#'
#' \code{ keyword(flowSet) <- list(foo="bar") }
#'
#' }
#'
#' \item{length}{number of \code{\linkS4class{flowFrame}} objects in
#' the set.
#'
#' \emph{Usage:}
#'
#' \code{ length(flowSet)}
#'
#' }
#'
#' \item{show}{display object summary.}
#'
#' \item{summary}{Return descriptive statistical summary (min, max,
#' mean and quantile) for each channel of each
#' \code{\linkS4class{flowFrame}}
#'
#' \emph{Usage:}
#'
#' \code{ summary(flowSet)}
#'
#' }
#'
#'
#' \item{fsApply}{Apply a function on all frames in a \code{flowSet}
#' object. Similar to \code{\link{sapply}}, but with additional
#' parameters. See separate documentation for details.
#'
#' \emph{Usage:}
#'
#' \code{ fsApply(flowSet, function, ...)}
#'
#' \code{ fsApply(flowSet, function, use.exprs=TRUE, ...)}
#'
#' }
#'
#' \item{compensate}{Apply a compensation matrix on all frames in a
#' \code{flowSet} object. See separate documentation for details.
#'
#' \emph{Usage:}
#'
#' \code{ compensate(flowSet, matrix)}
#'
#' }
#'
#' \item{transform}{Apply a transformation function on all frames of a
#' \code{flowSet} object. See separate documentation for details.
#'
#' \emph{Usage:}
#'
#' \code{ transform(flowSet, ...)}
#'
#' }
#'
#' \item{filter}{Apply a filter object on a \code{flowSet}
#' object. There are methods for \code{\linkS4class{filter}}s
#' and lists of filters. The latter has to
#' be a named list, where names of the list items are matching
#' sampleNames of the \code{flowSet}. See \code{\linkS4class{filter}}
#' for details.
#'
#' \emph{Usage:}
#'
#' \code{ filter(flowSet, filter)}
#'
#' \code{ filter(flowSet, list(filters))}
#'
#' }
#'
#' \item{split}{Split all \code{flowSet} objects according to a
#' \code{\link{filter}}, \code{\link{filterResult}} or a list of such
#' objects, where the length of the list has to be the same as the
#' length of the \code{flowSet}. This returns a list of
#' \code{\linkS4class{flowFrame}}s or an object of class
#' \code{flowSet} if the \code{flowSet} argument is set to
#' \code{TRUE}. Alternatively, a \code{flowSet} can be split into
#' separate subsets according to a factor (or any vector that can be
#' coerced into factors), similar to the behaviour of
#' \code{\link[base]{split}} for lists. This will return a list of
#' \code{flowSet}s. See \code{\link{split}} for details.
#'
#' \emph{Usage:}
#'
#' \code{ split(flowSet, filter)}
#'
#' \code{ split(flowSet, filterResult)}
#'
#' \code{ split(flowSet, list(filters))}
#'
#' \code{ split(flowSet, factor)}
#'
#' }
#'
#' \item{Subset}{Returns a \code{flowSet} of
#' \code{\linkS4class{flowFrame}}s that have been subset according
#' to a \code{\linkS4class{filter}} or
#' \code{\linkS4class{filterResult}}, or according to a list of such
#' items of equal length as the \code{flowSet}.
#'
#' \emph{Usage:}
#'
#' \code{ Subset(flowSet, filter)}
#'
#' \code{ Subset(flowSet, filterResult)}
#'
#' \code{ Subset(flowSet, list(filters))}
#'
#' }
#'
#'
#' \item{rbind2}{Combine two \code{flowSet} objects, or one
#' \code{flowSet} and one \code{\linkS4class{flowFrame}} object.
#'
#' \emph{Usage:}
#'
#' \code{ rbind2(flowSet, flowSet)}
#'
#' \code{ rbind2(flowSet, flowFrame)}
#'
#' }
#'
#' \item{spillover}{Compute spillover matrix from a compensation
#' set. See separate documentation for details.
#' }
#' }
#'
#' @section Important note on storage and performance:
#' The bulk of the data in a \code{flowSet} object is stored in an
#' \code{\link[base:environment]{environment}}, and is therefore not
#' automatically copied when the \code{flowSet} object is copied. If
#' \code{x} is an object of class \code{flowSet}, then the code
#' \preformatted{y <- x} will create an object \code{y} that contains
#' copies of the \code{phenoData} and administrative data in \code{x},
#' but refers to the \emph{same} environment with the actual fluorescence
#' data. See below for how to create proper copies.
#'
#' The reason for this is performance. The pass-by-value semantics of
#' function calls in \code{R} can result in numerous copies of the same
#' data object being made in the course of a series of nested function
#' calls. If the data object is large, this can result in considerable
#' cost of memory and performance. \code{flowSet} objects are intended to
#' contain experimental data in the order of hundreds of Megabytes, which
#' can effectively be treated as read-only: typical tasks are the
#' extraction of subsets and the calculation of summary statistics. This
#' is afforded by the design of the \code{flowSet} class: an object of
#' that class contains a \code{phenoData} slot, some administrative
#' information, and a \emph{reference} to an environment with the
#' fluorescence data; when it is copied, only the reference is copied,
#' but not the potentially large set of fluorescence data themselves.
#'
#' However, note that subsetting operations, such as \code{y <- x[i]} do
#' create proper copies, including a copy of the appropriate part of the
#' fluorescence data, as it should be expected. Thus, to make a proper
#' copy of a \code{flowSet} \code{x}, use \code{y <- x[seq(along=x)]}
#'
#' @author
#'
#' F. Hahne, B. Ellis, P. Haaland and N. Le Meur
#' @seealso
#'
#' \code{\linkS4class{flowFrame}}, \code{\link{read.flowSet}}
#' @keywords classes
#' @examples
#'
#' ## load example data and object creation
#' data(GvHD)
#'
#' ## subsetting to flowSet
#' set <- GvHD[1:4]
#' GvHD[1:4,1:2]
#' sel <- sampleNames(GvHD)[1:2]
#' GvHD[sel, "FSC-H"]
#' GvHD[sampleNames(GvHD) == sel[1], colnames(GvHD[1]) == "SSC-H"]
#'
#' ## subsetting to flowFrame
#' GvHD[[1]]
#' GvHD[[1, 1:3]]
#' GvHD[[1, "FSC-H"]]
#' GvHD[[1, colnames(GvHD[1]) == "SSC-H"]]
#' GvHD$s5a02
#'
#' ## constructor
#' flowSet(GvHD[[1]], GvHD[[2]])
#' pd <- phenoData(GvHD)[1:2,]
#' flowSet(s5a01=GvHD[[1]], s5a02=GvHD[[2]],phenoData=pd)
#'
#' ## colnames
#' colnames(set)
#' colnames(set) <- make.names(colnames(set))
#'
#' ## object name
#' identifier(set)
#' identifier(set) <- "test"
#'
#' ## phenoData
#' pd <- phenoData(set)
#' pd
#' pd$test <- "test"
#' phenoData(set) <- pd
#' pData(set)
#' varLabels(set)
#' varLabels(set)[6] <- "Foo"
#' varLabels(set)