Skip to content

Instantly share code, notes, and snippets.

View kieranrcampbell's full-sized avatar

Kieran R Campbell kieranrcampbell

View GitHub Profile
select <- dplyr::select
mutate <- dplyr::mutate
arrange <- dplyr::arrange
rename <- dplyr::rename
#' Plot a CNV heatmap
#' \code{cnv_data} must have the following columns:
#'
#' - start (start position of each region)
#' - chr (chromosome)
#' - single_cell_id (id of each cell)
#' - clone (clone to which each cell is assigned)
#' - copy_number (copy number of each clone in region)
#'
#' @export
export PATH=$PATH:/Applications/RStudio.app/Contents/MacOS/pandoc
@kieranrcampbell
kieranrcampbell / voom_de.R
Created March 20, 2018 22:40
example differential expression with limma voom
library(limma)
library(tidyverse)
dge <- DGEList(counts(sce_de))
dge <- calcNormFactors(dge)
design <- model.matrix(~ (dbz_cluster_str == "Unknown"), colData(sce_de)) # Your design matrix here
v <- voom(dge, design, plot = TRUE)
library(biomaRt)
ensembl <- useMart("ensembl")
ensembl <- useDataset("hsapiens_gene_ensembl", mart=ensembl)
bm <- getBM(attributes = c("ensembl_gene_id", "hgnc_symbol"),
filters = c("ensembl_gene_id"),
values = ensembl_gene_ids,
mart = ensembl) %>%
as_data_frame()
#' These functions round an input string 'x' to a desired
#' number of decimal places
round1 <- function(x) format(round(x, 1), nsmall = 1)
round2 <- function(x) format(round(x, 2), nsmall = 2)
roundn <- function(x, n = 2) format(round(x, n), nsmall = n)
# Nice colour palette for visualising copy number profiles
# inspired by DLP paper
cnv_cols <- c("0" = "#deebf7",
"1" = "#9ecae1",
"2" = "grey80",
"3" = "#fdae6b",
"4" = "#e6550d")
# Factors for chromosomes
chr_levels <- c(as.character(1:23), "X", "Y")
@kieranrcampbell
kieranrcampbell / overdispersion-analysis.R
Created June 15, 2017 09:55
Brief example of scran's method for "Accounting for technical noise in single-cell RNA-seq experiments"
library(stringr)
library(ggplot2)
library(scran)
library(scater)
library(dplyr)
# Step 1: do the overdispersion analysis
sc2 <- sce[rowMeans(round(exprs(sce)) > 0) > 0, ] # Keep only genes that are expressed
is_ercc <- grepl("NA_ERCC", featureNames(sc2)) # Your ERCCs might be named differently to mine!
## You can show that the estimate of fg is wrong *only*
## when m_beta = 1 and m_c = 1 and xx = 1 and that in
## such a case it appears to overcount by 2, implying term 11 in the derivation is wrong
m_alpha <- 0
s_alpha <- 1
m_beta <- 1
s_beta <- 1
m_t <- 0
s_t <- 1
@kieranrcampbell
kieranrcampbell / goplot.R
Created May 15, 2017 09:06
Plot the p-values of a GO analysis
library(stringr)
library(ggplot)
library(dplyr)
## Assuming "go" has columns "term" and "q_value"
go_top <- head(go, n = 10)
go_top$term <- str_to_title(go_top$term)
terms_sorted <- arrange(go_top, desc(q_value)) %>% .$term
go_top$term <- factor(go_top$term, levels = terms_sorted)