Skip to content

Instantly share code, notes, and snippets.

View kieranrcampbell's full-sized avatar

Kieran R Campbell kieranrcampbell

View GitHub Profile
#' 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)
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()
@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)
export PATH=$PATH:/Applications/RStudio.app/Contents/MacOS/pandoc
#' 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
select <- dplyr::select
mutate <- dplyr::mutate
arrange <- dplyr::arrange
rename <- dplyr::rename
#' Source an HDF5 file (ignoring all groups) where each
#' entry in the HDF5 is read and assigned to a variable
#' in the current environment
#' @importFrom rhdf5 h5ls
source_hdf5 <- function(filename, e) {
ls <- h5ls(filename)
vars <- ls$name
for(var in vars) {
assign(var, h5read(filename, var), envir = e)
}
# Some common functions for manipulating bioconductor SingleCellExperiment objects
#' Return the corresponding ensembl gene id for a symbol in a given SCE
get_ensembl_id <- function(symbol, sce) {
stopifnot(symbol %in% rowData(sce)$Symbol)
rownames(sce)[rowData(sce)$Symbol == symbol]
}
#' Prepare an SCE read using read10XUtils for SC3
prepare_for_sc3 <- function(sce) {
clusterMap <- function(sce1, sce2) {
clusters1 <- sort(unique(sce1$cluster)) # get unique clusters
clusters2 <- sort(unique(sce2$cluster))
# Check this is gene (row) by cluster (cell) - if not needs transposed
cluster_means_1 <- sapply(clusters1, function(x) {
rowMeans(as.matrix(logcounts(sce1[,sce1$cluster == x]))
}))
get_ensembl_id <- function(symbol, sce) {
if(!(symbol %in% rowData(sce)$Symbol)) {
stop("Symbol not in SCE genes")
}
rownames(sce)[rowData(sce)$Symbol == symbol]
}