%\VignetteIndexEntry{Plotting with Genominator}
%\VignetteDepends{Genominator, biomaRt}
%\VignettePackage{Genominator}
\documentclass[letterpaper,pdf,english]{article}
<<results=hide,echo=FALSE>>=
options(width = 80)
@ 
\SweaveOpts{prefix.string=plots_Genominator,eps=FALSE,echo=TRUE}
\usepackage{times}
\usepackage{hyperref}
\usepackage{color}
\usepackage{babel}
\usepackage{graphicx}

\newcommand{\Rfunction}[1]{{\texttt{#1}}}
\newcommand{\Robject}[1]{{\texttt{#1}}}
\newcommand{\Rpackage}[1]{{\textit{#1}}}
\newcommand{\Rmethod}[1]{{\texttt{#1}}}
\newcommand{\Rfuncarg}[1]{{\texttt{#1}}}
\newcommand{\Rclass}[1]{{\textit{#1}}}
\newcommand{\Rcode}[1]{{\texttt{#1}}}
\newcommand{\software}[1]{\textsf{#1}}
\newcommand{\R}{\software{R}}

\usepackage[margin=1in]{geometry}
\usepackage{fancyhdr}
\pagestyle{fancy}
\rhead{}
\renewcommand{\footrulewidth}{\headrulewidth}

\title{Plotting using Genominator and GenomeGraphs (Beta)}
\author{James Bullard \and Kasper Daniel Hansen}
\date{Modified: April 18, 2010, Compiled: \today}
\begin{document}
\maketitle
\thispagestyle{fancy}
\textcolor{red}{This vignette is preliminary, and should be viewed as
  subject to change. A number of the functions are not directly
  exported by the package -- there is a reason for that.}

In this vignette we demonstrate how to visualize data using the
\Rpackage{GenomeGraphs} package. The main idea is that we want to
build a plotting function which we can use to plot regions. The
simplest case is the following:

First, we make a database:
<<>>=
require(Genominator)

options(verbose = FALSE)
N <- 100000 # the number of observations. 
K <- 100    # the number of annotation regions, not less than 10

df <- data.frame(chr = sample(1:16, size = N, replace = TRUE),
                 location = sample(1:1000, size = N, replace = TRUE),
                 strand = sample(c(1L,-1L), size = N, replace = TRUE))
eData <- aggregateExpData(importToExpData(df, dbFilename = "pmy.db", overwrite = TRUE, tablename = "ex_tbl"))

annoData <- data.frame(chr = sample(1:16, size = K, replace = TRUE),
                       strand = sample(c(1, -1), size = K, replace = TRUE),
                       start = (st <- sample(1:1000, size = K, replace = TRUE)),
                       end = st + rpois(K, 75),
                       feature = c("gene", "intergenic")[sample(1:2, size = K, replace = TRUE)])
rownames(annoData) <- paste("elt", 1:K, sep = ".")
@ 

<<>>=
rp <- Genominator:::makeRegionPlotter(list("track.1" = list(expData = eData, what = "counts")))
args(rp)
@ 

This constructs a function which can be called to view particular
pieces of data. 
<<fig=TRUE>>=
rp(1, 10, 1000)
@ 

\Rpackage{GenomeGraphs} provides a wealth of customization options and
means of plotting which for the most part are transferable using
the list. 

<<fig=TRUE>>=
rp <- Genominator:::makeRegionPlotter(list("track.1" = list(expData = eData, what = "counts",
                                             dp = DisplayPars(lwd = .45, color = "grey"))))
rp(1, 400, 500)
@ 

Here we can plot our annotation using the annotation factory
construct. This is probably a little advanced. An easier thing 
is to use Ensembl to do the plotting of the annotation. Often, however,
you will want to augment the annotation produced by Ensembl.

<<fig=TRUE>>=
annoFactory <- Genominator:::makeAnnoFactory(annoData, featureColumnName = "feature", 
                                             groupColumnName = NULL, idColumnName = NULL,
                                             dp = DisplayPars("gene" = "blue", 
                                             "intergenic" = "green"))
rp <- Genominator:::makeRegionPlotter(list("track.1" = list(expData = eData, what = "counts",
                                           dp = DisplayPars(lwd=.2, color = "grey")),
                                           "track.2" = list(expData = eData, what = "counts", 
                                           fx = log2, DisplayPars(lwd=.3, color = "black"))),
                                      annoFactory = annoFactory)
rp(annoData[1,"chr"], annoData[1, "start"] - 100, annoData[1, "end"] + 100)
@ 

\Rpackage{GenomeGraphs} also offers a nice way to plot annotation for
a given region using data from Ensembl or other sources of annotation
- in some cases you have to do a little work because of the way that
Biomart indexes the annotation and the way the \Rpackage{Genominator}
package works (in this case yeast annotation is stored with Roman
numerals denoting the chromosomes).

<<fig=TRUE>>=
require("biomaRt")
mart <- useMart("ensembl", dataset = "scerevisiae_gene_ensembl")
annoFactory <- Genominator:::makeAnnoFactory(mart, chrFunction = function(chr) as.roman(chr))

load(system.file("data", "chr1_yeast.rda", package = "Genominator"))
head(chr1_yeast)
yData <- importToExpData(chr1_yeast, dbFilename = "my.db", tablename = "yeast", 
                         overwrite = TRUE)

rp <- Genominator:::makeRegionPlotter(list("track.-" = list(expData = yData, what = c("mRNA_1", "mRNA_2"),
                                             fx = rowMeans, strand = -1,
                                             dp = DisplayPars(lwd=.3, color = "grey"))),
                                      annoFactory = annoFactory)
rp(1, 20000, 50000)
@ 

\section*{SessionInfo}

<<sessionInfo,results=tex,echo=FALSE>>=
toLatex(sessionInfo())
@ 

\end{document}