Skip to content

Instantly share code, notes, and snippets.

View kieranrcampbell's full-sized avatar

Kieran R Campbell kieranrcampbell

View GitHub Profile
## If you get the error
## > Error in `.rowNamesDF<-`(x, value = value) : invalid 'row.names' length
## after singleCellTK::runSeuratPCA(sce)
## then run
sce@metadata$seurat <- NULL
## Going to take non tumour cells as just fibroblast and T/NK
## as these are (i) abundant, and (ii) sufficiently distinct transcriptionally
## from tumour that we're unlikely to confuse them
non_tumour_cells <- c("Fibroblast", "T/NK")
get_baf_df <- function(sce) {
message(paste0("[Running] Sample ", sce$id[1]))
colnames(sce) <- paste0(colData(sce)$id, "-", colData(sce)$barcode)
sce$cell_type <- cell_annots[colnames(sce)]
@kieranrcampbell
kieranrcampbell / mmd_for_michael.py
Created March 24, 2022 23:31
pytorch implementation of mmd
## Can't remember where this comes from unfortunately
def gaussian_kernel(a, b):
dim1_1, dim1_2 = a.shape[0], b.shape[0]
depth = a.shape[1]
a = a.view(dim1_1, 1, depth)
b = b.view(1, dim1_2, depth)
a_core = a.expand(dim1_1, dim1_2, depth)
b_core = b.expand(dim1_1, dim1_2, depth)
numerator = (a_core - b_core).pow(2).mean(2)/depth
return torch.exp(-numerator)
@kieranrcampbell
kieranrcampbell / expand_mask_by_one.py
Last active February 24, 2021 15:45
Expand a cell mask by one
def expand_mask_by_one(m):
"""Takes a cell mask np array as input
and returns with mask expanded by 1
"""
from skimage.morphology import dilation
m2 = m.copy()
unique_cell_ids = np.sort(np.unique(m.reshape(-1)))
unique_cell_ids = unique_cell_ids[unique_cell_ids>0]
for cell_id in unique_cell_ids:
Sys.setenv('R_REMOTES_NO_ERRORS_FROM_WARNINGS' = 'true'); devtools::install_github('...')
#' Sort the results of a gsva() call so that the
#' pathway activation always correlates positively
#' on average with the gene set listed
#' @param g Result of call to gsva() (pathway x cell)
#' @param genesets List of genes corresponding to genesets of g
#' @param sce A SingleCellExperiment with a logcounts assay
fix_gsva_direction <- function(g, genesets, sce) {
stopifnot(nrow(g) == length(genesets))
genesets <- lapply(genesets, intersect, rownames(sce))
seurat_cluster <- function(sce,
resolution = 0.8,
dims = 1:10,
reduction = "mnn",
algorithm = 1,
return_SCE = TRUE) {
seu <- as.Seurat(sce, counts = "counts", data = "logcounts")
seu <- FindNeighbors(seu, dims = dims, reduction = reduction)
seu <- FindClusters(seu, resolution = resolution, algorithm = algorithm)
@kieranrcampbell
kieranrcampbell / uoftproxy.js
Created April 12, 2020 22:02
Chrome bookmark to use UofT journal proxy
javascript:( function(){ f='http://myaccess.library.utoronto.ca/login?url='+window.location.href; if(!window.open(f)) location.href=f; } )()
@kieranrcampbell
kieranrcampbell / jackson-raw-to-sce-server.R
Created March 30, 2020 20:58
parse Jackson 2020 data to SingleCellExperiments
suppressPackageStartupMessages({
library(tidyverse)
library(SingleCellExperiment)
library(argparser)
})
select <- dplyr::select
mutate <- dplyr::mutate
arrange <- dplyr::arrange
rename <- dplyr::rename
+
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))