Skip to content

Instantly share code, notes, and snippets.

View kieranrcampbell's full-sized avatar

Kieran R Campbell kieranrcampbell

View GitHub Profile
library(devtools)
library(SingleCellExperiment)
library(tidyverse)
library(annotables)
library(scater)
library(annotables)
library(scran)
library(pheatmap)
library(matrixStats)
(load-theme 'tango)
(add-to-list 'auto-mode-alist '("\\.smk\\'" . python-mode))
(add-to-list 'auto-mode-alist '("Snakefile\\'" . python-mode))
mito_ensembl_ids = ["ENSG00000198888", "ENSG00000198763", "ENSG00000198804", "ENSG00000198712", "ENSG00000228253",
"ENSG00000198899", "ENSG00000198938", "ENSG00000198840", "ENSG00000212907", "ENSG00000198886",
"ENSG00000198786", "ENSG00000198695", "ENSG00000198727"]
# Add a conda env to jupyter
# Taken from https://stackoverflow.com/questions/39604271/conda-environments-not-showing-up-in-jupyter-notebook
source activate myenv
python -m ipykernel install --user --name myenv --display-name "Python (myenv)"
library(org.Hs.eg.db)
example_ensembl_ids <- c("ENSG00000122180", "ENSG00000080824")
symbols <- mapIds(org.Hs.eg.db, keys = example_ensembl_ids, column = "SYMBOL", keytype = "ENSEMBL", multiVars = "first")
load("../../data/genesets/human_H_v5p2.rdata")
go_gs <- Hs.H
entrezgene_ensembl_map <- as.list(org.Hs.egENSEMBL)
map_ids <- function(entrezgenes) {
x <- unlist(entrezgene_ensembl_map[entrezgenes])
names(x) <- NULL
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]
}
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]))
}))
# 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) {
#' 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)
}