---
title: DeLorean analysis of Guo et al. mouse embryo single cell data
author: John Reid
bibliography: DeLorean.bib
output: html_document
---

<!--
%\VignetteEngine{knitr::rmarkdown}
%\VignetteIndexEntry{DeLorean analysis of Guo et al. mouse embryo data}
-->

```{r build, echo=FALSE, eval=FALSE}
devtools::load_all('..')
rmarkdown::render('DeLorean-Guo.Rmd')


```{r config, echo=FALSE, message=FALSE}
library(knitr)
library(knitcitations)
library(rmarkdown)
#
# knitr options
#
opts_chunk$set(
    fig.path = 'figures/Guo-',
    stop_on_error = TRUE,
    fig.width = 12.5,
    fig.height = 8)
#
# Citations
#
cleanbib()
cite_options(
    # hyperlink = 'to.doc',
    hyperlink = TRUE,
    # style = 'html',
    # citation_format = 'text',
    citation_format = "pandoc",
    cite.style = "numeric",
    check.entries = TRUE)
    # hyperlink = TRUE)
bib <- read.bibtex("DeLorean.bib")
if (file.exists("config.R")) {
    source("config.R")
}

```

```{r loadLibs, echo=FALSE, message=FALSE}
# suppressMessages(loadfonts())
library(DeLorean)
#
# Stylesheet
#
options(markdown.HTML.stylesheet = system.file(file.path('Rmd', 'foghorn.css'),
                                               package="DeLorean"))
font.family <- "Verdana"
font.theme <- theme_update(text=element_text(family=font.family))
theme_set(font.theme)

```


`r citet(bib[["guo_resolution_2010"]])` performed qPCR on mouse embryonic
cells at various stages.


# Data

Guo et al.'s data is available in the `DeLorean` R package.
```{r loadLib}
library(DeLorean)
data(GuoDeLorean)
dl <- de.lorean(
    guo.expr,
    guo.gene.meta,
    guo.cell.meta)

```

```{r child=report.file("data")}
```


# Estimate hyperparameters

Examine data for empirical Bayes estimation of hyperparameters.
```{r empiricalBayes}
dl <- estimate.hyper(dl, sigma.tau=0.5)

```

```{r child=report.file("hyper-parameters")}
```


# Choose genes and cells

Choose a few genes.
```{r filterGenes}
set.seed(1)
num.genes <- nrow(dl$gene.meta)  # All genes
sampled.genes <- sample_n(dl$gene.meta, num.genes)$gene
gene.filter <- function(genes) genes %in% sampled.genes
dl <- filter_genes(dl, gene.filter)

```

Choose a few cells from each stage.
```{r filterCells}
num.at.each.stage <- 9
te.sampled.cells <- (
    dl$cell.meta
    %>% filter(capture < "32C" | "TE" == cell.type)
    %>% group_by(capture)
    %>% do(sample_n(., num.at.each.stage))
)
pe.sampled.cells <- (
    dl$cell.meta
    %>% filter(capture < "32C" | "PE" == cell.type | "ICM" == cell.type)
    %>% group_by(capture)
    %>% do(sample_n(., num.at.each.stage))
)
epi.sampled.cells <- (
    dl$cell.meta
    %>% filter(capture < "32C" | "EPI" == cell.type | "ICM" == cell.type)
    %>% group_by(capture)
    %>% do(sample_n(., num.at.each.stage))
)
run.model <- function(dl, cells.sampled) {
    cell.filter <- function(cells) cells %in% cells.sampled
    dl <- filter_cells(dl, cell.filter)
    dl <- prepare.for.stan(dl)
    dl <- compile.model(dl)
    dl <- find.best.tau(dl)
    system.time(dl <- fit.model(dl, num.cores=20))
    dl <- examine.convergence(dl)
    dl <- process.posterior(dl)
    dl <- analyse.noise.levels(dl)
    dl <- make.predictions(dl)
    dl
}
dl.te  <- run.model(dl,  te.sampled.cells$cell)
dl.pe  <- run.model(dl,  pe.sampled.cells$cell)
dl.epi <- run.model(dl, epi.sampled.cells$cell)

```

Compare the profiles in the three fitted models for the TE, PE and ICM.

```{r compareProfiles}
gp <- cmp.profiles.plot(TE=dl.te, PE=dl.pe, EPI=dl.epi,
                        genes=dl.te$gene.map$gene)
print(gp)
png('Guo-fates.png', width=1400, height=900)
print(gp)
dev.off()

```


```{r echo=FALSE}
# Save DeLorean object without fit component
saveRDS({dl2 <- dl; dl2$fit <- NULL; dl2}, "Data/Guo.rds")
```


R version and packages used:
```{r Rversion}
sessionInfo()
```
