#' [CuffDiff](http://cufflinks.cbcb.umd.edu/manual.html) and [cummeRbund](http://compbio.mit.edu/cummeRbund/) were used to perform this analysis. For details about how the test for differntial expression works, refer to [Differential analysis of gene regulation at transcript resolution with RNA-seq](http://www.nature.com/nbt/journal/v31/n1/full/nbt.2450.html).
#' Create some global summary statistic plots in order to get an idea about the general quaniltiy and distribution of the expression data. Descriptions are copied from [Cummerbund Manual](http://compbio.mit.edu/cummeRbund/manual_2_0.html)
#' Several plotting methods are available that allow for quality-control or global analysis of cufflinks data. A good place to begin is to evaluate the quality of the model fitting. Overdispersion is a common problem in RNA-Seq data. As of cufflinks v2.0 mean counts, variance, and dispersion are all emitted, allowing you to visualize the estimated overdispersion for each sample as a quality control measure.
dispersionPlot(genes(cuff))#+ aes(alpha=0.01)
#' The coefficient of variation (CV) is defined as the ratio of the standard deviation the mean. The squared coefficient of variation is a normalized measure of cross-replicate variability that can be useful for evaluating the quality your RNA-seq data. Differences in CV2 can result in lower numbers of differentially expressed genes due to a higher degree of variability between replicate fpkm estimates.
fpkmSCVPlot(genes(cuff))
#fpkmSCVPlot(genes(cuff), showPool=T)
#' To assess the distributions of FPKM scores across samples, we plot their densities
#' For the volcano plot matrix we calculate fold-changes as x/y (ie column/row). For details see [here](http://rpackages.ianhowson.com/bioc/cummeRbund/man/csVolcano.html). Signifcance coloring is based on cuffdiff's internal score an not our hit criterion.
csVolcanoMatrix(genes(cuff))
#' We also plot more detailed scatter plots for just the contrasts for interest. Signifcance coloring is based on cuffdiff's internal score an not our hit criterion.
#' For most type of experiments we expect replicates to cluster together. Second similar biological conditions should cluster together. As clustering results depend on the method being used, we try a few here to check if we get consistent results.
#' Map replicates into 2-dimensional space to highlight replicate clusters (or the lack of them)
#' Here we are using [cuffdiff](http://cole-trapnell-lab.github.io/cufflinks/cuffdiff) to test for differential expression. Estimate variance-mean dependence in count data from high-throughput sequencing assays and test for differential expression based on a model using the negative binomial distribution.
allDiff<-diffData(genes(cuff))%>%
## just keep pairs of interest
merge(contrasts)%>%
## discard all genes that are not expressed in any of the cell types (>1)
ggplot(degs,aes(paste(sample_1,"vs",sample_2),fill=sample_1_overex))+geom_bar()+xlab(NULL)+ylab("# DGEs")+ggtitle("DEGs by contrast")+coord_flip()
#' DEGs can be browsed in Excel using the exported table or via the embedded table browser. To enable the IGV links, you need to set the port in the IGV preferences to 3334.
kableDegs<-degs
if(nrow(degs>2000)){
kableDegs<-degs%>%arrange(-q_value)%>%head(1000)
print("just showing first 1000 most significant hits in interactive hit table!!!! Use Excel to browser to browse the full set")
#' To assess the relation of fold-changes and p-value we plot the data as volcano plots for each contrasts including the number of DEGS according our hit criterion.
##' For the volcano plot matrix we calculate fold-changes as x/y (ie column/row). For details see [here](http://rpackages.ianhowson.com/bioc/cummeRbund/man/csVolcano.html)