## ----setup, echo=FALSE, results="hide"----------------------------------------
knitr::opts_chunk$set(tidy=FALSE, cache=TRUE,
dev="png",
message=TRUE, error=FALSE, warning=TRUE)

## ----vignettes, eval=FALSE----------------------------------------------------
#  browseVignettes("distinct")

## ----citation, eval=FALSE-----------------------------------------------------
#  citation("distinct")

## ----Bioconductor_installation, eval=FALSE------------------------------------
#  if (!requireNamespace("BiocManager", quietly=TRUE))
#    install.packages("BiocManager")
#  BiocManager::install("distinct")

## ----github_installation, eval=FALSE------------------------------------------
#  devtools::install_github("SimoneTiberi/distinct")

## ----github_installation_2, eval=FALSE----------------------------------------
#  devtools::install_github("SimoneTiberi/distinct",
#                           build_opts = c("--no-resave-data", "--no-manual"))

## ----load-example-data, message = FALSE---------------------------------------
data("Kang_subset", package = "distinct")
Kang_subset

## ----visualize colData--------------------------------------------------------
colData(Kang_subset)

## ----visualize experimental design--------------------------------------------
metadata(Kang_subset)$experiment_info

## ----t-SNE plot, message = FALSE----------------------------------------------
library(scater)
plotTSNE(Kang_subset, colour_by = "cell")

## ----load_distinct, message=FALSE---------------------------------------------
library(distinct)

## ----differential-analyses----------------------------------------------------
set.seed(61217)
res = distinct_test(
  x = Kang_subset, 
  name_assays_expression = "counts",
  name_cluster = "cell",
  name_group = "stim",
  name_sample = "sample_id",
  P = 10^3, 
  min_non_zero_cells = 20)

## ----visualize-results--------------------------------------------------------
head(res)

## ----UpsetR plot--------------------------------------------------------------
library(UpSetR)
res_by_cluster = split( ifelse(res$p_adj.glb < 0.01, 1, 0), res$cluster_id)
upset(data.frame(do.call(cbind, res_by_cluster)))

## ----plotExpression-----------------------------------------------------------
# select cluster of cells:
cluster = "Dendritic cells"
sel_cluster = res$cluster_id == cluster
sel_column = Kang_subset$cell == cluster

# select significant genes:
sel_genes = res$p_adj.glb < 0.01
genes = as.character(res$gene[sel_cluster & sel_genes])

# make violin plots:
library(scater)
plotExpression(Kang_subset[,sel_column],
                     features = genes, exprs_values = "logcounts",
                     log2_values = FALSE,
                     x = "sample_id", colour_by = "stim", ncol = 3) +
        guides(fill = guide_legend(override.aes = list(size = 5, alpha = 1))) +
        theme(axis.text.x = element_text(angle = 45, hjust = 1))

## ----sessionInfo--------------------------------------------------------------
sessionInfo()

