Last active
February 12, 2020 15:35
-
-
Save cplaisier/9d6c2deab96dc496187d30cdd8eb64fc to your computer and use it in GitHub Desktop.
R script to apply cell cycle ccAF classifier to single cell dataset.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(Seurat) | |
| library(ranger) | |
| # Modified function from Seurat V2 | |
| ClassifyCells = function (classifier, training.genes = NULL, training.classes = NULL, | |
| new.data = NULL, ...) | |
| { | |
| features <- classifier$forest$independent.variable.names | |
| genes.to.add <- setdiff(x = features, y = rownames(x = new.data)) | |
| data.to.add <- matrix(data = 0, nrow = length(x = genes.to.add), | |
| ncol = ncol(x = new.data)) | |
| rownames(x = data.to.add) <- genes.to.add | |
| new.data <- rbind(new.data, data.to.add) | |
| new.data <- new.data[features, ] | |
| new.data <- as.matrix(x = t(x = new.data)) | |
| message("Running Classifier ...") | |
| prediction <- predict(classifier, new.data) | |
| new.classes <- prediction$predictions | |
| return(new.classes) | |
| } | |
| ############################################# | |
| ### HEK293T cells from barnyard assay 10X ### | |
| ### http://cf.10xgenomics.com/samples/cell-exp/3.0.2/10k_hgmm_v3_nextgem/10k_hgmm_v3_nextgem_filtered_feature_bc_matrix.tar.gz ### | |
| ############################################# | |
| setwd('/files/hgmm') | |
| exp_mat = Read10X('filtered_feature_bc_matrix') | |
| # Subset to only human genes | |
| exp_mat = exp_mat[grep('hg19_',rownames(exp_mat)),] # Only human genes | |
| rownames(exp_mat) = gsub('hg19_', '', rownames(exp_mat)) # Rename to remove hg19_ | |
| #exp_mat = exp_mat[,colnames(exp_mat)[which(colSums(as.matrix(exp_mat))>10000)]] # Only HEK293T cells | |
| # Make Seruat object | |
| HEK293T = CreateSeuratObject(counts=exp_mat) | |
| HEK293T[["percent.mt"]] = PercentageFeatureSet(HEK293T, pattern = "^MT-") | |
| pdf('HEK293T_vlnPlot.pdf', width=11, height=8.5) | |
| VlnPlot(HEK293T, features = c("nFeature_RNA", "nCount_RNA", "percent.mt"), ncol = 3) | |
| plot1 = FeatureScatter(HEK293T, feature1 = "nCount_RNA", feature2 = "percent.mt") | |
| plot2 = FeatureScatter(HEK293T, feature1 = "nCount_RNA", feature2 = "nFeature_RNA") | |
| CombinePlots(plots = list(plot1, plot2)) | |
| dev.off() | |
| # Subset out good HEK293T cells (ncol = 3468, nrow = 57,905) | |
| HEK293T = subset(HEK293T, subset = nFeature_RNA > 4500 & nFeature_RNA < 100000 & percent.mt < 30) | |
| dim(HEK293T) | |
| # Find variable features, scale data, and do dimensionality reduction | |
| HEK293T = NormalizeData(object = HEK293T) | |
| # Load the classifier | |
| ccAF = ('ccAF.rds') | |
| # Assuming your data is called "d1", a dataframe with genes as rows (labeled as gene symbols), | |
| # and cells are the columns labeled however you like | |
| # Data should be normalized but not scaled | |
| ccAF_HEK293T = ClassifyCells(ccAF, new.data = as.matrix(HEK293T@assays$RNA@data)) | |
| names(ccAF_HEK293T) = colnames(HEK293T@assays$RNA@data) | |
| table(ccAF_HEK293T) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that Seurat is not required for the classification, only to load up the HEK293T data. If you have an alternate data analysis approach and can get a gene expression matrix to input with gene symbols it should work just as well.