%\VignetteDepends{method}
%\VignetteIndexEntry{A brief overview of the S4 class system}
%\VignettePackage{AdvancedR2011}

\documentclass[9pt]{beamer}

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

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

\title{A brief overview of the S4 class system}
\author{Herv\'e Pag\`es}
\institute[FHCRC]{Fred Hutchinson Cancer Research Center}
\date{17-18 February, 2011}

\begin{document}

<<setup, echo=FALSE>>=
options(width = 60)
@

\frame{\titlepage}

\section[Outline]{}
\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"                  
  [3] "allGenerics"                     "allNames"                       
  [5] "Arith"                           "as"                             
  [7] "as<-"                            "asMethodDefinition"             
...
[199] "testVirtual"                     "traceOff"                       
[201] "traceOn"                         "tryNew"                         
[203] "trySilent"                       "unRematchDefinition"            
[205] "validObject"                     "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 2.7: 1383 classes and 8397 methods defined
            in 200 packages! (out of 419)

      \item Top 4: 94 classes in \Rpackage{flowCore} and \Rpackage{IRanges}
            (tie), 72 classes in \Rpackage{Biostrings},
            68 classes in \Rpackage{rsbml}, ...

      \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 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}
\begin{Schunk}
\begin{Sinput}
> library(graph)
> data(apopGraph)
> apopGraph
\end{Sinput}
\begin{Soutput}
A graphNEL graph with directed edges
Number of Nodes = 50 
Number of Edges = 59
\end{Soutput}
\end{Schunk}
  \end{block}

  \begin{block}{From using \textit{the} constructor}
\begin{Schunk}
\begin{Sinput}
> library(IRanges)
> IRanges(start=c(101, 25), end=c(110, 80))
\end{Sinput}
\begin{Soutput}
IRanges of length 2
    start end width
[1]   101 110    10
[2]    25  80    56
\end{Soutput}
\end{Schunk}
  \end{block}

\end{frame}

\begin{frame}[fragile]
  \frametitle{}
  \begin{block}{From a coercion}
\begin{Schunk}
\begin{Sinput}
> library(Matrix)
> m <- matrix(3:-4, nrow=2)
> as(m, "Matrix")
\end{Sinput}
\begin{Soutput}
2 x 4 Matrix of class "dgeMatrix"
     [,1] [,2] [,3] [,4]
[1,]    3    1   -1   -3
[2,]    2    0   -2   -4
\end{Soutput}
\end{Schunk}
  \end{block}

  \begin{block}{From using a specialized high-level constructor}
\begin{Schunk}
\begin{Sinput}
> library(GenomicFeatures)
> makeTranscriptDbFromUCSC("sacCer2", tablename="ensGene")
\end{Sinput}
\begin{Soutput}
TranscriptDb object:
| Db type: TranscriptDb
| Data source: UCSC
| Genome: sacCer2
| UCSC Table: ensGene
...
\end{Soutput}
\end{Schunk}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{}
  \begin{block}{From using a high-level I/O function}
\begin{Schunk}
\begin{Sinput}
> library(ShortRead)
> lane1 <- readFastq("path/to/my/data/", pattern="s_1_sequence.txt")
> lane1
\end{Sinput}
\begin{Soutput}
class: ShortReadQ
length: 256 reads; width: 36 cycles
\end{Soutput}
\end{Schunk}
  \end{block}

  \begin{block}{Inside an S4 object}
\begin{Schunk}
\begin{Sinput}
> sread(lane1)
\end{Sinput}
\begin{Soutput}
  A DNAStringSet instance of length 256
      width seq
  [1]    36 GGACTTTGTAGGATACCCTCGCTTTCCTTCTCCTGT
  [2]    36 GATTTCTTACCTATTAGTGGTTGAACAGCATCGGAC
  [3]    36 GCGGTGGTCTATAGTGTTATTAATATCAATTTGGGT
  [4]    36 GTTACCATGATGTTATTTCTTCATTTGGAGGTAAAA
  ...   ... ...
[253]    36 GTTTTACAGACACCTAAAGCTACATCGTCAACGTTA
[254]    36 GATGAACTAAGTCAACCTCAGCACTAACCTTGCGAG
[255]    36 GTTTGGTTCGCTTTGAGTCTTCTTCGGTTCCGACTA
[256]    36 GCAATCTGCCGACCACTCGCGATTCAATCATGACTT
\end{Soutput}
\end{Schunk}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{How to manipulate S4 objects?}
  \begin{block}{Low-level: getters and setters}
\begin{Schunk}
\begin{Sinput}
> ir <- IRanges(start=c(101, 25), end=c(110, 80))
> width(ir)
\end{Sinput}
\begin{Soutput}
[1] 10 56
\end{Soutput}
\begin{Sinput}
> width(ir) <- width(ir) - 5
> ir
\end{Sinput}
\begin{Soutput}
IRanges of length 2
    start end width
[1]   101 105     5
[2]    25  75    51
\end{Soutput}
\end{Schunk}
  \end{block}

  \begin{block}{High-level: plenty of specialized methods}
\begin{Schunk}
\begin{Sinput}
> qa1 <- qa(lane1, lane="lane1")
> class(qa1)
\end{Sinput}
\begin{Soutput}
[1] "ShortReadQQA"
attr(,"package")
[1] "ShortRead"
\end{Soutput}
\end{Schunk}
  \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:
\begin{Schunk}
\begin{Sinput}
> showMethods("qa")
\end{Sinput}
\begin{Soutput}
Function: qa (package ShortRead)
dirPath="character"
dirPath="list"
dirPath="ShortReadQ"
dirPath="SolexaPath"
\end{Soutput}
\end{Schunk}
      \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
\begin{Schunk}
\begin{Sinput}
> class(lane1)
\end{Sinput}
\begin{Soutput}
[1] "ShortReadQ"
attr(,"package")
[1] "ShortRead"
\end{Soutput}
\begin{Sinput}
> showClass("ShortReadQ")
\end{Sinput}
\begin{Soutput}
Class "ShortReadQ" [package "ShortRead"]

Slots:
                                             
Name:       quality        sread           id
Class: QualityScore DNAStringSet   BStringSet

Extends: 
Class "ShortRead", directly
Class ".ShortReadBase", by class "ShortRead", distance 2

Known Subclasses: "AlignedRead"
\end{Soutput}
\end{Schunk}
}
    \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",
    representation(
      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",
    representation(
        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 hard or impossible 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 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}

