%\VignetteIndexEntry{DEGreport}
%\VignetteKeywords{DifferentialExpression, Visualization, RNASeq, ReportWriting}
%\VignetteEngine{knitr::knitr}

\documentclass{article}
\usepackage[utf8]{inputenc}

<<knitr, echo=FALSE, results="hide">>=
library("knitr")
opts_chunk$set(tidy=FALSE,
               fig.width=9,fig.height=5,
               message=FALSE)
@ 


<<style, eval=TRUE, echo=FALSE, results="asis">>=
BiocStyle::latex()
@

\title{DEGreport }
\author{Lorena Pantano}
\date{Modified: 2 July, 2016. Compiled: \today}

\begin{document}
maketitle
\tableofcontents
\newpage

<<package-load,message=FALSE>>=
library(DEGreport)
data(humanSexDEedgeR)
library(edgeR)
@

\section{QC figures from DE analysis}

We are going to do a differential expression analysis with edgeR. We have an 
object that is comming from the edgeR package. It countains a gene count matrix
for 85 TSI HapMap individuals, and the gender information. With that, we are 
going to apply the `glmFit` function to get genes differentially expressed 
between males and females.

<<chunk-1>>=
des<-humanSexDEedgeR$design
fit <- glmFit(humanSexDEedgeR,des)
lrt <- glmLRT(fit)
tab<-cbind(lrt$table,p.adjust(lrt$table$PValue,method="BH"))
detags <- rownames(tab[tab[,5]<=0.1,])
plotSmear(humanSexDEedgeR, de.tags=detags)
@

We need to extract the experiment design data.frame where the condition is 
Male or Female.

<<chunk-2>>=
counts<-cpm(humanSexDEedgeR,log=FALSE)
g1<-colnames(counts)[1:41]
g2<-colnames(counts)[42:85]
design<-data.frame(condition=sub("1","Male",sub("0","Female",des[,2])))
@

We are getting the chromosome information for each gene. This way we can colour
genes according autosomic,X or Y chromosomes.

<<chunk-3>>=
data(geneInfo)
@

Create the report. The main parameters are the column names in group1, and 
group2. Then, the count matrix, gene names that are DE, p-values, fold changes 
and path to create the report. As optional, you can give colours for each gene, 
and the number of permutation.

<<chunk-4>>=
detag10<-detags[1:10]
pval<-tab[,4]
fc<-tab[detag10,1]
@

Run the fowlling lines if you want to visualize your expression values by 
condition:

<<chunk-6, eval=FALSE>>=
degObj(counts,design,"degObj.rda")
library(shiny)
runGist(9930881)
@

You can use individual functions, like degRank or degMean. This will create 
specific figures and tables that are included in the report.

<<chunk-7>>=
degMean(pval,counts)
degVar(pval,counts)
degMV(humanSexDEedgeR$samples$group,pval,counts)
degMB(detags,g1,g2,counts)
degVB(detags,g1,g2,counts)
# require(rjags)
# rank<-degRank(g1,g2,counts[detag10,],fc,400,500)
# degPR(rank)
@


\section{Report from DESeq2 analysis}

In this section, we show how to use DESeq2 output to create a full report,
including figures and tbale with top de-regulated genes, GO enrichment
analysis and heatmaps and PCA plots. If you set \Rcode{path\_results},
different files will be saved there.

<<deseq2>>=
data(humanSexDEedgeR)
library(DESeq2)
idx <- c(1:10, 75:85)
dse <- DESeqDataSetFromMatrix(humanSexDEedgeR$counts[1:1000, idx], 
humanSexDEedgeR$samples[idx,], design=~group)
dse <- DESeq(dse)
res <- degResults(dds=dse, name="test", org=NULL, 
do_go=FALSE, group="group", xs="group", path_results = NULL)
@

\section{Detect patterns of expression}

In this section, we show how to detect pattern of expression. Mainly useful when
data is a time course experiment. \Rfunction{degPatterns} needs a expression
matrix, the design experiment and the column used to group samples.

<<pattern>>=
data(humanSexDEedgeR)
ma <- humanSexDEedgeR$counts[1:100,]
des <- data.frame(row.names=colnames(ma), 
sex=as.factor(humanSexDEedgeR$samples$group))
res <- degPatterns(ma, des, time="sex", col=NULL)
@

\end{document}
