Skip to content

Instantly share code, notes, and snippets.

@FloWuenne
Last active April 17, 2018 20:03
Show Gist options
  • Select an option

  • Save FloWuenne/df2dca6804f562ed90db26a04508af31 to your computer and use it in GitHub Desktop.

Select an option

Save FloWuenne/df2dca6804f562ed90db26a04508af31 to your computer and use it in GitHub Desktop.
Create .Rds object with components needed for clustering module of heartmat shiny server (for .Rds)
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"))
## Let user enter the time point to process
time_point <- readline(prompt="Enter time point: ")
## Load Seurat object
seurat_object <- readRDS(paste("../Objects/",time_point,".Seurat_object.Rds",sep=""))
## Read marker list
marker_list <- read.table(paste("../Marker/",time_point,".all_markers_identified.tsv",sep=""),
header=T,
sep="\t")
## Only retain top 20 Marker based on avg_logFC
marker_list_subset <- marker_list %>%
group_by(cluster) %>%
top_n(n=20, wt=avg_logFC)
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@[email protected]
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 <- [email protected]
clustering_object <- Clustering_info(tsne=tsne_mappings,
norm_exprs=norm_exprs_df,
metadata = metadata,
marker_list=marker_list_subset)
saveRDS(clustering_object,file=paste("../Shiny_server_data/",time_point,".clustering_module.Rds",sep=""))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment