Skip to content

Instantly share code, notes, and snippets.

@FloWuenne
Last active April 26, 2018 14:12
Show Gist options
  • Save FloWuenne/d48f8f65fc963aeaba065efc3a9334c1 to your computer and use it in GitHub Desktop.
Save FloWuenne/d48f8f65fc963aeaba065efc3a9334c1 to your computer and use it in GitHub Desktop.
Create shiny cluster modules for renamed datasets
library(dplyr)
library(tidyr)
## Define S4 object with required components
Clustering_info <- setClass("clustering_module",slots=c(tsne="data.frame",
metadata="data.frame",
norm_exprs="data.frame",
marker_list="data.frame"))
time_points <- c("E14.5","E16.5","E18.5","P1","P4","P7")
for(time_point in time_points){
## Load Seurat object
seurat_object <- readRDS(paste("../Objects/",time_point,".expression_seurat.renamed_clusters.Rds",sep=""))
## Read marker list
marker_list <- read.table(paste("../Marker/",time_point,".all_markers_identified.renamed_clusters.tsv",sep=""),
header=T,
sep="\t")
## Only retain top 20 Marker based on avg_logFC
marker_list$pct_diff <- marker_list$pct.1 - marker_list$pct.2
marker_list_subset <- marker_list %>%
group_by(cluster) %>%
top_n(n=50, wt=pct_diff)
marker_list_subset <- as.data.frame(marker_list_subset)
## Normalized expression data
norm_exprs_sparse <- seurat_object@data
norm_exprs_matrix <- as.matrix(seurat_object@data)
norm_exprs_df <- as.data.frame(norm_exprs_matrix)
## Create a new data frame that contains tSNE embeddings and cluster identities
tsne_mappings <- seurat_object@dr$tsne@cell.embeddings
cell_identities <- data.frame(seurat_object@ident)
rownames(cell_identities) <- names(seurat_object@ident)
tsne_mappings <- merge(tsne_mappings,cell_identities,by=0,all=TRUE)
rownames(tsne_mappings) <- tsne_mappings$Row.names
tsne_mappings <- tsne_mappings %>%
select(-Row.names)
## Metadata information
metadata <- seurat_object@meta.data
clustering_object <- Clustering_info(tsne=tsne_mappings,
norm_exprs=norm_exprs_df,
metadata = metadata,
marker_list=marker_list_subset)
## With compression, faster reading, bigger file size!
saveRDS(clustering_object,file=paste("../Shiny_server_data/",time_point,".clustering_module.renamed.Rds",sep=""))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment