%% \VignetteIndexEntry{Motifs and Position Weight Matricies}

\documentclass[a4paper, notitlepage]{beamer}

\usepackage{BioconductorSlides}
\title{Motifs and Position Weight Matrices}
\author{
  Martin
  Morgan\footnote{\href{mailto:mtmorgan@fhcrc.org}{mtmorgan@fhcrc.org}}
}
\date{June 23 -- 28, 2013}

<<setup, echo=FALSE>>=
options(showHeadLines=2, showTailLines=1, max.print=1000, width=50)
library(Morgan2013)
library(Biostrings)
library(BSgenome.Dmelanogaster.UCSC.dm3)
library(MotifDb)
library(seqLogo)
@ 

\begin{document}

\begin{frame}
  \maketitle
\end{frame}

\begin{frame}{Position weight matrix (PWM)}
  \begin{itemize}
  \item A probabilistic description of (short) sequences
  \item Useful for describing, e.g., transcription factor binding sites
  \item Easy to construct and use, e.g., scanning genomic sequences
    for occurence of binding motifs
  \item A quick case study
  \end{itemize}
\end{frame}

\begin{frame}[fragile]{Position weight matrix}
  \begin{itemize}
  \item HNF4alpha transcription factor binding sites
<<HNF4alpha, echo=FALSE>>=
library(Biostrings)
data(HNF4alpha)
HNF4alpha
@ 
\item Consensus matrix / position frequency matrix (first 7 columns)
<<HNF4alpha-consensusMatrix, echo=FALSE>>=
consensusMatrix(HNF4alpha)[1:4,1:7]
@ 
  \end{itemize}
\end{frame}

\begin{frame}[fragile]{Position weight matrix}
  \begin{itemize}
  \item Approximately: log probability $p_{ij}$ of symbol $i$ at
    position $j$, given background (prior) probability $b_i$,
    $log_2(p_{ij} / b_i)$
  \item Scaled so possible score ranges from 0 to 1
<<HNF4alpha-PFM, echo=FALSE>>=
pwm <- PWM(consensusMatrix(HNF4alpha)[1:4,])
round(pwm, 3)[,1:7]
@ 
\item Score a `subject' sequence by adding elements of corresponding
  entries in PWM
  \end{itemize}
\end{frame}

\begin{frame}[fragile]{Use}
<<pwm-example>>=
library(Biostrings)
data(HNF4alpha)
pfm <- consensusMatrix(HNF4alpha)
pwm <- PWM(consensusMatrix(HNF4alpha))
@ 
\Biocpkg{seqLogo}:\par
\begin{center}
  \includegraphics[width=\textwidth]{seqlogo.pdf}
\end{center}
<<seqlogo, echo=FALSE>>=
pdf("seqlogo.pdf", width=7, height=3)
pfm1 <- sweep(pfm, 2, colSums(pfm), "/")[1:4,]
seqLogo(pfm1)
x <- dev.off()
@ 
\end{frame}

\begin{frame}[fragile]{Use}
<<hits>>=
library(BSgenome.Dmelanogaster.UCSC.dm3)
hits <- matchPWM(pwm, Dmelanogaster$chr3R)
length(hits)
head(hits)
@ 
\end{frame}

\begin{frame}[fragile]{Use}
  \begin{columns}
    \column{.5\textwidth}
    \begin{itemize}
    \item Scores
<<pwm-score>>=
scores <- 
  PWMscoreStartingAt(
    pwm, subject(hits), 
    start(hits))
hist(scores)
@   
%% 
\item Minus strand -- take the \Rfunction{reverseComplement} of the
  PWM!
    \end{itemize}
<<pwm-scores, echo=FALSE>>=
pdf("scores.pdf")
hist(scores, cex.axis=2)
x <- dev.off()
@ 
    \column{.5\textwidth}
    \includegraphics{scores.pdf}
  \end{columns}
\end{frame}

\begin{frame}{Software}
  \begin{itemize}
  \item \Biocpkg{Biostrings} for PWM matching
  \item \Biocpkg{MotifDb} for a catalog of known motifs
  \item \Biocpkg{seqLogo} for visualization
  \end{itemize}
\end{frame}

\begin{frame}{Case study}
  \begin{itemize}
  \item Huang et al., 2013, Highly Recurrent TERT
    Promoter Mutations in Human Melanoma, Science 339:957
  \item Prior whole-genome sequencing of malignant melanomas; two
    somatic telomerase reverse transcriptase (TERT) promoter mutations
    in 17 of 19 cases: C228T and C250T
  \item Validate \& extend to additional tumor cell lines
  \item C228T and C250T introduce E-twenty-six (ETS) transcription
    factor binding sites
  \item Suggests plausible MAP kinase pathway; recurrent somatic
    mutations in regulatory regions as driver events in cancer
  \end{itemize}
\end{frame}

\begin{frame}{Case study}
  Lab workflow:
  \begin{itemize}
  \item (Call variants in TERT promoter region)
  \item Score catalog of known PWMs for match to reference versus
    variant sequence
  \end{itemize}
\end{frame}

\begin{frame}{Acknowledgments}
  \begin{itemize}
  \item Paul Shannon (\Biocpkg{MotifDb}; lab workflow)
  \item Herv\'e Pag\`es (\Biocpkg{Biostrings})
  \item Patrick Aboyoun (\Rfunction{PWM} \& friends)
  \item Valerie Obenchain (lab workflow)
  \end{itemize}
\end{frame}

\end{document}
