%\VignetteDepends{IRanges,GRanges,Biostrings,Rsamtools,ShortRead,
%  rtracklayer,biomaRt,GenomicFeatures}
%\VignetteIndexEntry{Four exercises using \Bioconductor{} Sequence Infrastructure}
%\VignettePackage{HTSandGeneCentricLabs}

\documentclass{beamer}

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

\title{Four exercises using \Bioconductor{} Sequence Infrastructure}
\author{Patrick Aboyoun \& Martin Morgan}
\institute[FHCRC]{Fred Hutchinson Cancer Research Center}
\date{29-30 July, 2010}

\begin{document}

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

\frame{\titlepage}

\section[Outline]{}
\frame{\tableofcontents}


\section{Objective}

\begin{frame}[t]
  \frametitle{Objective}

  \begin{block}{}
    The objective of this lab is to learn about the \Bioconductor{}
    sequence infrastructure by walking through exercises on:
  \end{block}

  \begin{block}{Exercises}
    \begin{itemize}
      \item Reading and manipulating short reads
      \item An introduction to \Biocpkg{Rsamtools}
      \item A simple ChIP-seq workflow
      \item A simple RNA-seq use case
    \end{itemize}
  \end{block}
\end{frame}


\section{Genomic ranges}

\begin{frame}[t,fragile]
  \frametitle{Important classes}

  \begin{block}{Classes}
    \begin{itemize}
      \item \Biocpkg{IRanges}'s \Rclass{IRanges} - generic interval/range
      \item \Biocpkg{GenomicRanges}'s \Rclass{GRanges} - genomic interval/range
    \end{itemize}
  \end{block}

  \begin{block}{Example}
{\footnotesize
<<GRanges-construction>>=
library(GenomicRanges)
grngs <-
  GRanges(seqnames=c("seq1", "seq2", "seq2"),
          ranges=IRanges(c(1000, 100, 1000),
                         c(2000, 1000, 2000)),
          strand=c("+", "+", "-"))
grngs
@
}
  \end{block}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Accessors}

  \begin{block}{Methods}
    \begin{itemize}
      \item \Rfunction{seqnames}
      \item \Rfunction{ranges}
      \item \Rfunction{strand}
      \item \Rfunction{start}, \Rfunction{end}, \Rfunction{width}
      \item \Rfunction{seqlengths}
    \end{itemize}
  \end{block}

  \begin{block}{Example}
{\small
<<GRanges-accessors>>=
seqnames(grngs)
@
}
  \end{block}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Basic interval manipulations}

  \begin{block}{Methods}
    \begin{itemize}
      \item \Rfunction{shift} - moves the intervals to the left or right
      \item \Rfunction{resize} - anchors the 5' end of the interval and
        moves the 3' end
    \end{itemize}
  \end{block}

  \begin{block}{Example}
{\footnotesize
<<GRanges-manipulation>>=
resize(grngs, 1)
@
}
  \end{block}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Interval overlap operations}

  \begin{block}{Methods}
    \begin{itemize}
      \item \Rfunction{findOverlaps} - maps query set to target set
      \item \Rfunction{countOverlaps} - counts query set overlaps
      \item \Rfunction{subsetByOverlaps} - subsets based on overlaps
    \end{itemize}
  \end{block}

  \begin{block}{Example}
{\footnotesize
<<GRanges-overlap>>=
findOverlaps(grngs, grngs)
@
}
  \end{block}
\end{frame}


\section{Short reads}

\begin{frame}[t,fragile]
  \frametitle{Importing short read data via \Biocpkg{ShortRead}}

  \begin{block}{Methods}
    \begin{itemize}
      \item \Rfunction{readAligned} - alignment files
      \item \Rfunction{readFastq} - fastq files
      \item \Rfunction{readXStringColumns} - collection of
        \Rclass{XStringSet} objects
    \end{itemize}
  \end{block}

  \begin{block}{Example}
{\scriptsize
<<readAligned>>=
library(ShortRead)
extdataDir <- system.file("extdata", "slx",
                          package="HTSandGeneCentricLabs")
pattern <- "s_1_1_export_head.txt"
aln <- readAligned(extdataDir, pattern, "SolexaExport")
aln
@
}
  \end{block}
\end{frame}

\begin{frame}[t]
  \frametitle{Short read related classes}

  \begin{block}{Classes}
    \begin{itemize}
      \item \Biocpkg{ShortRead}'s \Rclass{AlignedRead} - general container
        for alignments
      \item \Biocpkg{ShortRead}'s \Rclass{SolexaExportQA} - a quality
        assurance processing of Solexa export files
      \item \Biocpkg{Biostrings}'s \Rclass{DNAStringSet} - a collection of
        short reads
      \item \Biocpkg{Biostrings}'s \Rclass{BStringSet} - a collection of
        base call qualities
    \end{itemize}
  \end{block}
\end{frame}

\begin{frame}[t]
  \frametitle{Accessors}

  \begin{block}{Methods}
    \begin{itemize}
      \item \Rfunction{length} - number of alignments
      \item \Rfunction{id} - read identifier
      \item \Rfunction{sread} - read base calls
      \item \Rfunction{quality} - quality base calls
      \item \Rfunction{chromosome} - aligned chromosome
      \item \Rfunction{position} - aligned leftmost position
      \item \Rfunction{strand} - aligned strand
      \item \Rfunction{alignQuality} - overall alignment quality
      \item \Rfunction{alignData} - data associated with alignment
    \end{itemize}
  \end{block}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Counting letters in strings}

  \begin{block}{Methods}
    \begin{itemize}
      \item \Rfunction{alphabet} - available letters for string
      \item \Rfunction{alphabetFrequency} - letter frequency per string
      \item \Rfunction{alphabetByCycle}/\Rfunction{consensusMatrix} - letter
        frequency per position
    \end{itemize}
  \end{block}

  \begin{block}{Example}
{\small
<<alphabetByCycle>>=
alphabetByCycle(sread(aln))[1:4, 1:6]
@
}
  \end{block}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Filtering alignments}

  \begin{block}{Methods}
    \begin{itemize}
      \item \Rfunction{chromosomeFilter} - filter on chromosome
      \item \Rfunction{alignDataFilter} - filter on alignment data (e.g.
        Illumina's chastity determination)
      \item \Rfunction{alignQualityFilter} - filter on overall quality
      \item \Rfunction{compose} - functional composition of filters 
      \item \Rfunction{[} - square-bracket operator
    \end{itemize}
  \end{block}

  \begin{block}{Example}
{\small
<<filter>>=
filt1 <- alignDataFilter(expression(filtering=="Y"))
filt2 <- chromosomeFilter("chr[0-9XYM]+.fa")
filt <- compose(filt1, filt2)
caln <- aln[filt(aln)]
length(caln) / length(aln)
@
}
  \end{block}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Quality assurance of sequencing}

  \begin{block}{Methods}
    \begin{itemize}
      \item \Rfunction{qa} - generate QA measure
      \item \Rfunction{report} - create QA report
    \end{itemize}
  \end{block}

  \begin{block}{Example (Not Run)}
{\small
<<workflow-qa, eval=FALSE>>=
rpt <- report(qa(extdataDir, "_export.txt"),
              dest="reports/my_report.pdf")
@
}
  \end{block}
\end{frame}


\section{SAM-based alignments}

\begin{frame}[t]
  \frametitle{Importing from BAM files}

  \begin{block}{Read parameter class}
    \Rfunction{ScanBamParam} - specifies read filter
  \end{block}
    
  \begin{block}{Minimalist container class}
    \Rfunction{GappedAlignments} - ``shape" (CIGAR) and location of alignments
  \end{block}

  \begin{block}{Methods}
    \begin{itemize}
      \item \Rfunction{scanBam} - creates list of specified columns
      \item \Rfunction{readBamGappedAlignments} - create a
        \Rclass{GappedAlignments} instance
    \end{itemize}
  \end{block}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{\Rclass{GappedAlignments} class}

  \begin{block}{Example}
{\small
<<readBamGappedAlignments>>=
library(Rsamtools)
bamFile <- 
  system.file("extdata", "ex1.bam", package="Rsamtools")
readBamGappedAlignments(bamFile)[1:10]
@
}
  \end{block}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{\Rfunction{scanBam} method}

  \begin{block}{Example}
{\small
<<scanBam>>=
which <- GRanges(seqnames=c("seq1", "seq2", "seq2"),
                 ranges=IRanges(c(1000, 100, 1000),
                                c(2000, 1000, 2000)))
what <- c("rname", "strand", "pos", "qwidth", "seq")
param <- ScanBamParam(which=which, what=what)
bam <- scanBam(bamFile, param=param)
class(bam)
names(bam)
@
}
  \end{block}
\end{frame}

\begin{frame}[t]
  \frametitle{References to BAM files}

  \begin{block}{BAM files reference class}
    \Rclass{BamViews} - defines views of interest
  \end{block}
    
  \begin{block}{Accessors}
    \begin{itemize}
      \item \Rclass{bamPaths} - BAM file paths
      \item \Rclass{bamSamples} - sample information
      \item \Rclass{bamRanges} - \Rclass{GRanges} specifying genomic
        intervals of interest
      \item \Rclass{bamExperiment} - high-level information for the
        experiment
    \end{itemize}
  \end{block}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{\Rclass{BamViews} class}

  \begin{block}{Example}
{\small
<<BamViews>>=
fls <- list.files(system.file("extdata", package="Rsamtools"),
                  "\\.bam$", full=TRUE)
rngs <-
  GRanges(seqnames = Rle(c("chr1", "chr2"), c(9, 9)),
          ranges =
          c(IRanges(seq(10000, 90000, 10000), width=500),
            IRanges(seq(100000, 900000, 100000), width=5000)),
          Count = seq_len(18L))
bv <- BamViews(fls, bamRanges=rngs)
bv
basename(bamPaths(bv))
@
}
  \end{block}
\end{frame}


\section{Coverage vectors}

\begin{frame}[t,fragile]
  \frametitle{Coverage vectors class}

  \begin{block}{Class}
    \Biocpkg{IRanges}'s \Rclass{RleList} - list of run-length encoded vectors
  \end{block}

  \begin{block}{Constructor}
    \Rfunction{coverage} - generates coverage vectors
  \end{block}

  \begin{block}{Example}
{\small
<<coverage>>=
cover <- coverage(caln)
cover[1]
@
}
  \end{block}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Coverage vector operations}

  \begin{block}{Methods}
    \begin{itemize}
      \item \Rfunction{runwtsum} - running window weighted sum smoother
      \item \Rfunction{pmin} - parallel (elementwise) minimum of two or
        more vectors
      \item \Rfunction{quantile} - quantiles for vectors
    \end{itemize}
  \end{block}

  \begin{block}{Example}
{\small
<<coverage-quantile>>=
quantile(cover, c(0.99, 0.9999, 0.999999))[,1:6]
@
}
  \end{block}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Views on coverage vectors}

  \begin{block}{Class}
    \Biocpkg{IRanges}'s \Rclass{RleViewsList} - contiguous regions within
      the coverage vectors
  \end{block}

  \begin{block}{Constructor}
    \Rfunction{slice} - views based on fixed cutoffs (peak thresholds)
  \end{block}

  \begin{block}{Method}
    \Rfunction{viewMaxs} - maximum for each view
  \end{block}

  \begin{block}{Example}
{\small
<<coverage-Views>>=
islands <- slice(cover, 1)
viewMaxs(islands)[1:3]
@
}
  \end{block}
\end{frame}


\section{Genomic annotations}

\begin{frame}[t,fragile]
  \frametitle{Importing/exporting genomic features via \Biocpkg{rtacklayer}}

  \begin{block}{Methods}
    \begin{itemize}
      \item \Rfunction{import} - gff, bed, wig, etc.
      \item \Rfunction{export} - gff, bed, wig, etc.
    \end{itemize}
  \end{block}

  \begin{block}{Example}
{\small
<<import>>=
library(rtracklayer)
import(system.file("tests", "bed.wig",
                   package="rtracklayer"))[1:3,]
@
}
  \end{block}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Transcript databases via \Biocpkg{GenomicFeatures}}

  \begin{block}{Functions}
    \begin{itemize}
      \item \Rclass{makeTranscriptDbFromBiomart} - create transcript
        database using BioMart
      \item \Rclass{loadFeatures} - reference serialized database
      \item \Rclass{transcripts} - extract transcript bounds
    \end{itemize}
  \end{block}

  \begin{block}{Example (Not Run)}
{\small
<<makeTranscriptDbFromBiomart, eval=FALSE>>=
dmTxDb <-
  makeTranscriptDbFromBiomart(biomart = "ensembl",
                              dataset =
                              "dmelanogaster_gene_ensembl")
@
}
  \end{block}
\end{frame}


\section{Exercises}

\begin{frame}[t,fragile]
  \frametitle{Exercises}

  \begin{block}{Steps}
    \begin{enumerate}
      \item Install HTSandGeneCentricLabs and its suggested packages
      \item Locate labs withing \R{} using \\
        \Rcode{browseVignettes("HTSandGeneCentricLabs")}
      \item Look over vignettes with a prefix of \texttt{Examples:}
\begin{verbatim}
Exercises: A Simple ChIP-Seq Workflow
Exercises: A Simple RNA-seq Use Case
Exercises: An introduction to Rsamtools
Exercises: Reading and Manipulating Short Reads
\end{verbatim}
      \item Choose vignettes that are of most interest since they
        would involve more than two hours of work to complete.
    \end{enumerate}
  \end{block}

\end{frame}


\end{document}
