%\VignetteDepends{GenomicFeatures, BSgenome, BSgenome.Scerevisiae.UCSC.sacCer2,
%                 BSgenome.Hsapiens.UCSC.hg19, SNPlocs.Hsapiens.dbSNP.20090506, hgu95av2probe}
%\VignetteIndexEntry{The GenomicFeatures and BSgenome packages}
%\VignettePackage{CSAMA10}

\documentclass{beamer}

\usepackage{BioconductorSlides}
\SweaveOpts{keep.source=TRUE}

\title{\Biocpkg{GenomicFeatures} and \Biocpkg{BSgenome}}
\author{Patrick Aboyoun}
\institute[FHCRC]{Fred Hutchinson Cancer Research Center}
\date{14-18 June, 2010}

\begin{document}

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

\frame{\titlepage}

\section{Introduction}

\begin{frame}[t]
  \frametitle{\Bioconductor{} Sequence Packages}
\begin{figure}[ht]
 \centering
  \includegraphics[width=4in,height=3in]{packageStack}
\end{figure}
\end{frame}

\begin{frame}[t]
  \frametitle{\Bioconductor{} Sequence Annotation Packages}
  \begin{block}{\Biocpkg{GenomicFeatures}}
    \begin{itemize}
      \item Management of transcript information using
        \Biocpkg{GenomicRanges} infrastructure
      \item Transcripts stored in separate SQLite databases
    \end{itemize}
  \end{block}

  \begin{block}{\Biocpkg{BSgenome}}
    \begin{itemize}
      \item Management of whole genomes using \Biocpkg{Biostrings}
        infrastructure
      \item Tools for operating on those genomes
      \item Genomes stored in separate
        \Rpackage{BSgenome.}\textit{Organism.Provider.BuildVersion}
        packages
      \item Support for pre-build SNP packages for human
    \end{itemize}
  \end{block}
\end{frame}


\section{GenomicFeatures}

\begin{frame}[t,fragile]
  \frametitle{\Biocpkg{GenomicFeatures} transcript sources}
  \begin{block}{Constructors}
    \Rfunction{makeTranscriptDbFromBiomart},
    \Rfunction{makeTranscriptDbFromUCSC}
  \end{block}

{\footnotesize
<<loadGenomicFeatures>>=
library(GenomicFeatures)
nrow(supportedUCSCtables())
head(supportedUCSCtables(), 10)
@
}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{\Rclass{TranscriptDb} basics}
  \begin{block}{Making a \Rclass{TranscriptDb} object}
{\small
<<makeUCSC, eval=FALSE>>=
mm9KG <-
  makeTranscriptDbFromUCSC(genome = "mm9",
                           tablename = "knownGene")
@
}
  \end{block}
  \begin{block}{Saving and Loading}
{\small
<<saveFeatures, eval=FALSE>>=
saveFeatures(mm9KG, file="mm9KG.sqlite")
@
<<loadFeatures>>=
mm9KGChr9 <-
  loadFeatures(system.file("extdata", "mm9KGChr9.sqlite",
                           package = "CSAMA10"))
@
}
  \end{block}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{\Rclass{TranscriptDb} class}
{\small
<<TranscriptDb>>=
mm9KGChr9
@
}
\end{frame}

\begin{frame}{\Rclass{TranscriptDb} schema}
  \begin{figure}
    \includegraphics[width=4.25in]{transcriptschema.png}
  \end{figure}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Ungrouped transcript-related information}
  \begin{block}{Extractors}
    \Rfunction{transcripts}, \Rfunction{exons}, \Rfunction{cds}
  \end{block}

{\footnotesize
<<transcripts>>=
tx <- transcripts(mm9KGChr9)
length(tx)
head(tx, 5)
@
}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Grouped transcript-related information}
  \begin{block}{Extractors}
    \Rfunction{transcriptsBy}, \Rfunction{exonsBy},
    \Rfunction{cdsBy}, \Rfunction{intronsByTranscript},
    \Rfunction{fiveUTRsByTranscript},
    \Rfunction{threeUTRsByTranscript}
  \end{block}

{\footnotesize
<<exonsBy>>=
txExons <- exonsBy(mm9KGChr9)
txIntrons <- intronsByTranscript(mm9KGChr9)
txExons[6]
@
}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Overlapping with transcripts}
  \begin{block}{Methods}
    \Rfunction{findOverlaps}, \Rfunction{countOverlaps},
    \Rfunction{match}, \Rfunction{\%in\%},
    \Rfunction{subsetByOverlaps}
  \end{block}
\vskip12pt
  \begin{block}{Usage}
{\footnotesize
<<findOverlaps-help, eval=FALSE>>=
findOverlaps(query, subject, maxgap = 0L, minoverlap = 1L,
             type = c("any", "start", "end"),
             select = c("all", "first"))
help("findOverlaps,GRanges,GRangesList-method")
@
<<countOverlaps>>=
grngs <- GRanges("chr9", gaps(ranges(txExons[[6]])), "+")
countOverlaps(grngs, tx)
rbind(countOverlaps(grngs, txExons), countOverlaps(grngs, txIntrons))
@
}
  \end{block}
\end{frame}


\section{BSgenome}

\begin{frame}[t,fragile]
  \frametitle{\Biocpkg{BSgenome} packages}

{\scriptsize
<<available.genomes>>=
library(BSgenome)
available.genomes()
@
}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{\Rclass{BSgenome} class decomposition}
  \begin{block}{\Rclass{BSgenome} slots}
{\scriptsize
<<BSgenome-slots>>=
getSlots("BSgenome")
@
}
  \end{block}

  \begin{block}{Notes}
    \begin{itemize}
      \item \Rfunarg{.seqs\_cache} and \Rfunarg{.link\_counts}
        slots manage memory.
      \item \Rfunarg{seqs\_dir} and \Rfunarg{masks\_dir} slots
        specify storage location.
    \end{itemize}
  \end{block}
\end{frame}

\begin{frame}[t]
  \frametitle{\Rclass{BSgenome} methods}
  \begin{block}{Sequence selection}
    \Rfunction{[[}, \Rfunction{\$}
  \end{block}

  \begin{block}{Subsequence selection}
    \Rfunction{getSeq}
  \end{block}

  \begin{block}{Accessors}
    \Rfunction{length}, \Rfunction{names}/\Rfunction{seqnames},
    \Rfunction{mseqnames}, \Rfunction{seqlengths},
    \Rfunction{masknames}, \Rfunction{sourceUrl}
  \end{block}

  \begin{block}{Matching}
    \Rfunction{vmatchPattern}, \Rfunction{vcountPattern},
    \Rfunction{vmatchPDict}, \Rfunction{vcountPDict},    
    \Rfunction{matchPWM}, \Rfunction{countPWM}
  \end{block}

  \begin{block}{SNPs (Human only at this point)}
    \Rfunction{injectSNPs}, \Rfunction{SNPlocs\_pkgname},
    \Rfunction{SNPcount}, \Rfunction{SNPlocs}
  \end{block}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{\Biocpkg{BSgenome} package without masks}

{\footnotesize
<<yeast>>=
library(BSgenome.Scerevisiae.UCSC.sacCer2)
Scerevisiae
Scerevisiae$chrI
@
}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{\Biocpkg{BSgenome} package with masks}

{\footnotesize
<<human>>=
library(BSgenome.Hsapiens.UCSC.hg19)
Hsapiens$chr1
@
}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Sequence information}

  \begin{block}{Operations that don't load sequences}
{\footnotesize
<<yeast-seqnames>>=
head(seqnames(Scerevisiae), 6)
head(seqlengths(Scerevisiae), 8)
@
}
  \end{block}

  \begin{block}{Operation that does}
{\footnotesize
<<yeast-alphabetFrequency>>=
sapply(head(seqnames(Scerevisiae), 8), function(i)
       alphabetFrequency(Scerevisiae[[i]], baseOnly=TRUE))
@
}
  \end{block}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Matches for single pattern across genome}

{\footnotesize
<<vcountPattern>>=
exclude <- setdiff(seqnames(Hsapiens), c("chr1", "chr2"))
vcountPattern("ACYTANCAGT", Hsapiens,
              fixed = c(pattern = FALSE, subject = TRUE),
              exclude = exclude)
patmatch <-
vmatchPattern("ACYTANCAGT", Hsapiens,
              fixed = c(pattern = FALSE, subject = TRUE),
              exclude = exclude, asRangedData = FALSE)
head(patmatch, 3)
@
}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Pattern dictionary (Microarray probes)}

{\footnotesize
<<hgu95av2probe>>=
library("hgu95av2probe")
probes <- DNAStringSet(hgu95av2probe$sequence[1:100])
head(probes, 10)
@
}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Count matches for multiple patterns across genome}

{\scriptsize
<<vcountPDict>>=
counts <- vcountPDict(probes, Hsapiens, exclude = exclude)
head(counts, 5)
dim(counts)
whichMatch <- seqselect(counts$index, counts$count > 0)
length(whichMatch)
whichMatch
matchedProbes <- probes[whichMatch]
@
}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{Find match locations for probes}

{\footnotesize
<<matchPDIct>>=
matchLocs <- matchPDict(PDict(matchedProbes), Hsapiens$chr2)
extractAllMatches(Hsapiens$chr2, matchLocs)
@
}
\end{frame}


\begin{frame}[t,fragile]
  \frametitle{\Rpackage{SNP} packages}

{\footnotesize
<<available.SNPs>>=
available.SNPs()
@
<<injectSNPs>>=
SNPlocs_pkgname(Hsapiens)
HsWithSNPs <-
  injectSNPs(Hsapiens, "SNPlocs.Hsapiens.dbSNP.20090506")
class(HsWithSNPs)
SNPlocs_pkgname(HsWithSNPs)
@
}
\end{frame}

\begin{frame}[t,fragile]
  \frametitle{\Rpackage{SNP} exploration}

{\scriptsize
<<SNPexploration>>=
SNPcount(HsWithSNPs)
alphabetFrequency(Hsapiens$chr1)
alphabetFrequency(HsWithSNPs$chr1)
@
}
\end{frame}


\section{Resources}

\begin{frame}
  \frametitle{Resources}
  \Bioconductor{} Web site
  \begin{itemize}
    \item `\Biocpkg{GenomicFeatures}' and `\Biocpkg{BSgenome}' links.
    \item \url{http://bioconductor.org}
    \item `\href{http://bioconductor.org/docs/install/}{Installation}',
      `\href{http://bioconductor.org/packages/release/Software.html}{Software}',
      and `\href{http://bioconductor.org/docs/mailList.html}{Mailing lists}'
      links.
  \end{itemize}
  Help in \R{}
  \begin{itemize}
    \item \Rcode{help.start()} to view a help browser.
    \item \Rcode{help(package = "BSgenome")}
    \item \Rcode{?transcriptsBy}
    \item \Rcode{browseVignettes("GenomicFeatures")}
  \end{itemize}
\end{frame}


\end{document}
