This file is indexed.

/usr/lib/R/site-library/S4Vectors/doc/S4QuickOverview.Rnw is in r-bioc-s4vectors 0.16.0-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
%\VignetteIndexEntry{A quick overview of the S4 class system}
%\VignetteDepends{methods,Matrix,IRanges,ShortRead,graph}

\SweaveOpts{keep.source=TRUE, eps=FALSE, width=9, height=3}

\documentclass[9pt]{beamer}

\usepackage{slides}

\AtBeginSection[]
{
  \begin{frame}<beamer>{Outline}
    \tableofcontents[currentsection,currentsubsection]
  \end{frame}
}

\title{A quick overview of the S4 class system}

\author{Herv\'e Pag\`es\\
        \href{mailto:hpages@fredhutch.org}{hpages@fredhutch.org}}

%\institute[FHCRC]{Fred Hutchinson Cancer Research Center\\
%                  Seattle, WA}

\date{June 2016}

\begin{document}

<<setup,echo=FALSE>>=
options(width=60)
library(Matrix)
library(IRanges)
library(ShortRead)
library(graph)
@

\maketitle

\frame{\tableofcontents}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{What is S4?}

\begin{frame}[fragile]
  \frametitle{The S4 class system}
  \begin{block}{}
    \begin{itemize}
      \item The \textit{S4 class system} is a set of facilities
            provided in R for OO programming.

      \item Implemented in the \Rpackage{methods} package.
      \item On a fresh \R{} session:
\begin{Schunk}
\begin{Sinput}
> sessionInfo()
\end{Sinput}
\begin{Soutput}
...
attached base packages:
[1] stats     graphics  grDevices utils     datasets 
[6] methods   base
\end{Soutput}
\end{Schunk}
      \item R also supports an older class system: the \textit{S3 class
            system}.
    \end{itemize}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{A different world}
  \begin{block}{The syntax}
\begin{Schunk}
\begin{Sinput}
> foo(x, ...)
\end{Sinput}
\end{Schunk}
    not:
\begin{Schunk}
\begin{Sinput}
> x.foo(...)
\end{Sinput}
\end{Schunk}
    like in other OO programming languages.
  \end{block}

  \begin{block}{The central concepts}
    \begin{itemize}
      \item The core components: \emph{classes}\footnote{also called
            \emph{formal classes}, to distinguish them from the S3
            classes aka \emph{old style classes}}, \emph{generic functions}
            and \emph{methods}

      \item The glue: \emph{method dispatch} (supports \emph{simple} and
            \emph{multiple} dispatch)
    \end{itemize}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{}
  \begin{block}{The result}
\begin{Schunk}
\begin{Sinput}
> ls('package:methods')
\end{Sinput}
\begin{Soutput}
  [1] "addNextMethod"                   "allGenerics"                    
  [3] "allNames"                        "Arith"                          
  [5] "as"                              "as<-"                           
  [7] "asMethodDefinition"              "assignClassDef"              
...
[211] "testVirtual"                     "traceOff"                       
[213] "traceOn"                         "tryNew"                         
[215] "unRematchDefinition"             "validObject"                    
[217] "validSlotNames"                 
\end{Soutput}
\end{Schunk}
    \begin{itemize}
      \item Rich, complex, can be intimidating

      \item The classes and methods we implement in our packages can be
            hard to document, especially when the class hierarchy is
            complicated and multiple dispatch is used
    \end{itemize}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{S4 in Bioconductor}
  \begin{block}{}
    \begin{itemize}
      \item Heavily used. In BioC 3.3: 3158 classes and 22511 methods defined
            in 609 packages! (out of 1211 software packages)

      \item Top 10: 128 classes in \Rpackage{ChemmineOB},
            98 in \Rpackage{flowCore}, 79 in \Rpackage{IRanges},
            68 in \Rpackage{rsbml}, 61 in \Rpackage{ShortRead},
            58 in \Rpackage{Biostrings}, 51 in \Rpackage{rtracklayer},
            50 in \Rpackage{oligoClasses}, 45 in \Rpackage{flowUtils},
            and 40 in \Rpackage{BaseSpaceR}.

      \item For the end-user: it's mostly transparent. But when something
            goes wrong, error messages issued by the S4 class system can
            be hard to understand. Also it can be hard to find the
            documentation for a specific method.

      \item Most Bioconductor packages use only a small subset of the S4
            capabilities (covers 99.99\% of our needs)
    \end{itemize}
  \end{block}
\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{S4 from an end-user point of view}

\begin{frame}[fragile]
  \frametitle{Where do S4 objects come from?}
  \begin{block}{From a dataset}
<<S4_object_in_dataset>>=
library(graph)
data(apopGraph)
apopGraph
@
  \end{block}

  \begin{block}{From using an object constructor function}
<<S4_object_from_constructor>>=
library(IRanges)
IRanges(start=c(101, 25), end=c(110, 80))
@
  \end{block}

\end{frame}

\begin{frame}[fragile]
  \frametitle{}
  \begin{block}{From a coercion}
<<S4_object_from_ceorcion>>=
library(Matrix)
m <- matrix(3:-4, nrow=2)
as(m, "Matrix")
@
  \end{block}

  \begin{block}{From using a specialized high-level constructor}
\begin{Schunk}
\begin{Sinput}
> library(GenomicFeatures)
> makeTxDbFromUCSC("sacCer2", tablename="ensGene")
\end{Sinput}
\begin{Soutput}
TxDb object:
# Db type: TxDb
# Supporting package: GenomicFeatures
# Data source: UCSC
# Genome: sacCer2
# Organism: Saccharomyces cerevisiae
# Taxonomy ID: 4932
# UCSC Table: ensGene
# UCSC Track: Ensembl Genes
...
\end{Soutput}
\end{Schunk}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{}
  \begin{block}{From using a high-level I/O function}
<<S4_object_from_high_level_IO_function>>=
library(ShortRead)
path_to_my_data <- system.file(
    package="ShortRead",
    "extdata", "Data", "C1-36Firecrest", "Bustard", "GERALD")
lane1 <- readFastq(path_to_my_data, pattern="s_1_sequence.txt")
lane1
@
  \end{block}

  \begin{block}{Inside another object}
<<S4_object_inside_another_object>>=
sread(lane1)
@
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{How to manipulate S4 objects?}
  \begin{block}{Low-level: getters and setters}
<<getters_and_setters>>=
ir <- IRanges(start=c(101, 25), end=c(110, 80))
width(ir)
width(ir) <- width(ir) - 5
ir
@
  \end{block}

  \begin{block}{High-level: plenty of specialized methods}
<<specialized_methods>>=
qa1 <- qa(lane1, lane="lane1")
class(qa1)
@
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{How to find the right man page?}
    \begin{itemize}
      \item \Rcode{class?graphNEL} or equivalently \Rcode{?\`{}graphNEL-class\`}
            for accessing the man page of a class
      \item \Rcode{?qa} for accessing the man page of a generic
            function
      \item The man page for a generic might also document some or all of
            the methods for this generic. The \textit{See Also:} section
            might give a clue. Also using \Rcode{showMethods()} can be
            useful:
<<showMethods>>=
showMethods("qa")
@
      \item \Rcode{?\`{}qa,ShortReadQ-method\`} to access the man page for a
            particular method (might be the same man page as for the generic)
      \item In doubt: \Rcode{??qa} will search the man pages of all the
            installed packages and return the list of man pages that contain
            the string \Rcode{qa}
    \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Inspecting objects and discovering methods}
  \begin{itemize}
    \item \Rcode{class()} and \Rcode{showClass()}
{\footnotesize
<<showClass>>=
class(lane1)
showClass("ShortReadQ")
@
}
    \item \Rcode{str()} for compact display of the content of an object
    \item \Rcode{showMethods()} to discover methods
    \item \Rcode{selectMethod()} to see the code
  \end{itemize}
\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Implementing an S4 class (in 4 slides)}

\begin{frame}[fragile]
  \frametitle{Class definition and constructor}
  \begin{block}{Class definition}
{\footnotesize
<<setClass>>=
setClass("SNPLocations",
    slots=c(
      genome="character",  # a single string
      snpid="character",   # a character vector of length N
      chrom="character",   # a character vector of length N
      pos="integer"        # an integer vector of length N
    )
)
@
}
  \end{block}

  \begin{block}{Constructor}
{\footnotesize
<<SNPLocations>>=
SNPLocations <- function(genome, snpid, chrom, pos)
    new("SNPLocations", genome=genome, snpid=snpid, chrom=chrom, pos=pos)
@
<<test_SNPLocations>>=
snplocs <- SNPLocations("hg19",
             c("rs0001", "rs0002"),
             c("chr1", "chrX"),
             c(224033L, 1266886L))
@
}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Getters}
\begin{block}{Defining the \Rfunction{length} method}
{\footnotesize
<<length,results=hide>>=
setMethod("length", "SNPLocations", function(x) length(x@snpid))
@
<<test_length>>=
length(snplocs)  # just testing
@
}
\end{block}

\begin{block}{Defining the slot getters}
{\footnotesize
<<genome,results=hide>>=
setGeneric("genome", function(x) standardGeneric("genome"))
setMethod("genome", "SNPLocations", function(x) x@genome)
@
<<snpid,results=hide>>=
setGeneric("snpid", function(x) standardGeneric("snpid"))
setMethod("snpid", "SNPLocations", function(x) x@snpid)
@
<<chrom,results=hide>>=
setGeneric("chrom", function(x) standardGeneric("chrom"))
setMethod("chrom", "SNPLocations", function(x) x@chrom)
@
<<pos,results=hide>>=
setGeneric("pos", function(x) standardGeneric("pos"))
setMethod("pos", "SNPLocations", function(x) x@pos)
@
<<test_slot_getters>>=
genome(snplocs)  # just testing
snpid(snplocs)   # just testing
@
}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{}
  \begin{block}{Defining the \Rfunction{show} method}
{\footnotesize
<<show,results=hide>>=
setMethod("show", "SNPLocations",
    function(object)
        cat(class(object), "instance with", length(object),
            "SNPs on genome", genome(object), "\n")
)
@
<<>>=
snplocs  # just testing
@
}
  \end{block}

  \begin{block}{Defining the \textit{validity method}}
{\footnotesize
<<validity,results=hide>>=
setValidity("SNPLocations",
    function(object) {
        if (!is.character(genome(object)) ||
            length(genome(object)) != 1 || is.na(genome(object)))
            return("'genome' slot must be a single string")
        slot_lengths <- c(length(snpid(object)),
                          length(chrom(object)),
                          length(pos(object)))
        if (length(unique(slot_lengths)) != 1)
            return("lengths of slots 'snpid', 'chrom' and 'pos' differ")
        TRUE
    }
)
@
\begin{Schunk}
\begin{Sinput}
> snplocs@chrom <- LETTERS[1:3]  # a very bad idea!
> validObject(snplocs)
\end{Sinput}
\begin{Soutput}
Error in validObject(snplocs) : 
  invalid class "SNPLocations" object: lengths of slots 'snpid', 'chrom'
  and 'pos' differ
\end{Soutput}
\end{Schunk}
}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{}
  \begin{block}{Defining slot setters}
{\footnotesize
<<set_chrom,results=hide>>=
setGeneric("chrom<-", function(x, value) standardGeneric("chrom<-"))
setReplaceMethod("chrom", "SNPLocations",
    function(x, value) {x@chrom <- value; validObject(x); x})
@
<<test_slot_setters>>=
chrom(snplocs) <- LETTERS[1:2]  # repair currently broken object
@
\begin{Schunk}
\begin{Sinput}
> chrom(snplocs) <- LETTERS[1:3]  # try to break it again
\end{Sinput}
\begin{Soutput}
Error in validObject(x) : 
  invalid class "SNPLocations" object: lengths of slots 'snpid', 'chrom'
  and 'pos' differ
\end{Soutput}
\end{Schunk}
}
  \end{block}

  \begin{block}{Defining a coercion method}
{\footnotesize
<<setAs,results=hide>>=
setAs("SNPLocations", "data.frame",
    function(from)
        data.frame(snpid=snpid(from), chrom=chrom(from), pos=pos(from))
)
@
<<test_coercion>>=
as(snplocs, "data.frame")  # testing
@
}
  \end{block}
\end{frame}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Extending an existing class}

\begin{frame}[fragile]
  \frametitle{Slot inheritance}
  \begin{itemize}
    \item Most of the time (but not always), the child class will have
          additional slots:
{\footnotesize
<<AnnotatedSNPs>>=
setClass("AnnotatedSNPs",
    contains="SNPLocations",
    slots=c(
        geneid="character"  # a character vector of length N
    )
)
@
}
    \item The slots from the parent class are inherited:
{\footnotesize
<<slot_inheritance>>=
showClass("AnnotatedSNPs")
@
}
    \item Constructor:
{\footnotesize
<<AnnotatedSNPs>>=
AnnotatedSNPs <- function(genome, snpid, chrom, pos, geneid)
{
    new("AnnotatedSNPs",
        SNPLocations(genome, snpid, chrom, pos),
        geneid=geneid)
}
@
}
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Method inheritance}
  \begin{itemize}
    \item Let's create an AnnotatedSNPs object:
{\footnotesize
<<method_inheritance>>=
snps <- AnnotatedSNPs("hg19",
             c("rs0001", "rs0002"),
             c("chr1", "chrX"),
             c(224033L, 1266886L),
             c("AAU1", "SXW-23"))
@
}
    \item All the methods defined for SNPLocations objects
          work out-of-the-box:
{\footnotesize
<<method_inheritance>>=
snps
@
}
    \item But sometimes they don't do the right thing:
{\footnotesize
<<as_data_frame_is_not_right>>=
as(snps, "data.frame")  # the 'geneid' slot is ignored
@
}
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{}
  \begin{itemize}
    \item Being a SNPLocations \emph{object} vs being a SNPLocations
          \emph{instance}:
{\footnotesize
<<>>=
is(snps, "AnnotatedSNPs")     # 'snps' is an AnnotatedSNPs object
is(snps, "SNPLocations")      # and is also a SNPLocations object
class(snps)                   # but is *not* a SNPLocations *instance*
@
}
    \item Method overriding: for example we could define a \Rfunction{show}
          method for AnnotatedSNPs objects. \Rfunction{callNextMethod}
          can be used in that context to call the method defined for the
          parent class from within the method for the child class.

    \item Automatic coercion method:
{\footnotesize
<<automatic_coercion_method>>=
as(snps, "SNPLocations")
@
}
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Incremental validity method}
  \begin{itemize}
    \item The \textit{validity method} for AnnotatedSNPs objects only needs
          to validate what's not already validated by the \textit{validity
          method} for SNPLocations objects:
{\footnotesize
<<incremental_validity_method,results=hide>>=
setValidity("AnnotatedSNPs",
    function(object) {
        if (length(object@geneid) != length(object))
            return("'geneid' slot must have the length of the object")
        TRUE
    }
)
@
}
    \item In other words: before an AnnotatedSNPs object can be considered
          valid, it must first be a valid SNPLocations object.
  \end{itemize}
\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{What else?}

\begin{frame}[fragile]
  \frametitle{}
  \begin{block}{Other important S4 features}
    \begin{itemize}
      \item \textit{Virtual} classes: equivalent to \textit{abstract}
            classes in Java

      \item Class unions (see \Rcode{?setClassUnion})

      \item Multiple inheritance: a powerful feature that should be used with
            caution. If used inappropriately, can lead to a class hierarchy
            that is very hard to maintain
    \end{itemize}
  \end{block}

  \begin{block}{Resources}
    \begin{itemize}
      \item Man pages in the \Rpackage{methods} package: \Rcode{?setClass},
            \Rcode{?showMethods}, \Rcode{?selectMethod}, \Rcode{?getMethod},
            \Rcode{?is}, \Rcode{?setValidity}, \Rcode{?as}

      \item The \textit{Extending RangedSummarizedExperiment} section of
            the \textit{SummarizedExperiment} vignette in the
            \Rpackage{SummarizedExperiment} package.

      \item Note: S4 is \emph{not} covered in the \textit{An Introduction
            to R} or \textit{The R language definition}
            manuals\footnote{http://cran.fhcrc.org/manuals.html}

      \item The \emph{Writing R Extensions} manual for details about
            integrating S4 classes to a package

      \item The \textit{R Programming for Bioinformatics} book by Robert
            Gentleman\footnote{http://bioconductor.org/help/publications/books/r-programming-for-bioinformatics/}
    \end{itemize}
  \end{block}
\end{frame}

\end{document}