devtools::load_all('..')
fit.model <- TRUE
rmarkdown::render('Kouno-DeLorean.Rmd')
library(knitr)
library(knitcitations)
library(rmarkdown)
#
# knitr options
#
opts_chunk$set(
    fig.path = 'figures/Kouno-',
    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")
}
source(system.file("scripts/shared.R", package="DeLorean"))
# 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 date()

r citet(bib[["kouno_temporal_2013"]]) assayed 120 single cells at each of 8 time points.

Data

Kouno et al.'s data is available in the DeLorean R package.

library(DeLorean)
data(KounoDeLorean)

Adjust the observed capture times to distances similar to Kouno et al.'s MDS plot.

kouno.cell.meta.adj.time <- (
    kouno.cell.meta
    %>% mutate(obstime=ifelse(capture== "0h",  0,
                       ifelse(capture== "1h",  6,
                       ifelse(capture== "6h", 10,
                       ifelse(capture=="12h", 11,
                       ifelse(capture=="24h", 12,
                       ifelse(capture=="48h", 13,
                       ifelse(capture=="72h", 14,
                       ifelse(capture=="96h", 15,
                              NA))))))))))

Build the DeLorean object.

dl <- de.lorean(
    kouno.expr,
    kouno.gene.meta,
    kouno.cell.meta.adj.time)

This will load a 45 (genes) by 960 (cells) matrix containing the expression measurements.

dim(dl)

and two data frames of meta data for the genes and cells.

sample_n(dl$gene.meta, 15)
sample_n(dl$cell.meta, 15)

Estimate hyperparameters

Examine data for empirical Bayes estimation of hyperparameters.

dl <- estimate.hyper(dl, sigma.tau=3)

Choose genes and cells

Choose genes.

set.seed(1)
max.genes <- min(getOption("Kouno.num.genes", nrow(dl$gene.meta)))
genes.for.stan <- sample(dl$gene.meta$gene, max.genes)
gene.filter <- function(genes) genes %in% genes.for.stan
dl <- filter_genes(dl, gene.filter)

Choose a few cells from each stage.

num.at.each.stage <- getOption("Kouno.num.each.stage", 7)
sampled.cells <- (
    dl$cell.meta
    %>% group_by(capture)
    %>% do(sample_n(., num.at.each.stage))
)
cell.filter <- function(cells) cells %in% sampled.cells$cell
dl <- filter_cells(dl, cell.filter)

Save expression data and meta data.

saveRDS(list(expr = dl$expr, cell.meta = dl$cell.map, gene.meta=dl$gene.map),
        file='Data/Kouno-input.rds')

Format the data for Stan and fit the model.

dl <- prepare.for.stan(dl)
dl <- compile.model(dl)
dl <- find.best.tau(dl)
system.time(dl <- fit.model(dl, num.cores=getOption("DL.num.cores")))

Examine convergence.

dl <- examine.convergence(dl)

Analyse posterior

Examine posterior.

dl <- process.posterior(dl)
dl <- analyse.noise.levels(dl)

Profiles

Calculate expression profiles.

dl <- make.predictions(dl)

# Save DeLorean object without fit component
saveRDS({dl2 <- dl; dl2$fit <- NULL; dl2}, "Data/Kouno.rds")

R version and packages used:

date()
sessionInfo()


JohnReid/DeLorean documentation built on Sept. 27, 2021, 5:45 a.m.