library("Seurat") library("ggplot2") library("reshape2") library("tidyr") library(data.table) library(Matrix) options(future.globals.maxSize=100000000000) args=c("") base=basename(args[1]) dir=dirname(args[1]) prefix=sub(".txt","",base) genes=args[2] genes=as.matrix(strsplit(genes,",")[[1]]) color=c(rgb(230/256,70/256,53/256),rgb(77/256,187/256,213/256),rgb(60/256,84/256,136/256),rgb(247/256,155/256,127/256),"#CDD788","#2B5F82","#246B47","#9E6ECF","#8E522F","#A7E2A8","#74C95E","#BBA23E","#297A6A","#85B3D6","#431D58","#4754C2","#9CDED6","#A16836","#85D6B1","#B08D3B","#C65753","#A63BB0","#3DB89F","#329130","#DE9D9C","#C590DA","#CC66AA","#822E2B","#C15D44","#8FC247","#173745","#605320","#5BC899","#D2AD79","#C9C95E","royalblue1","royalblue","olivedrab4","mintcream","turquoise2","saddlebrown","rosybrown2","steelblue4","midnightblue","paleturquoise4","wheat2") read<-function(mat,bin){ data<-fread(mat,header=T) data$cellID<-paste(as.character(round(data$x/bin, digits = 0)),"_",as.character(round(data$y/bin, digits = 0)),sep="") gene=unique(data$geneID) cell=unique(data$cellID) gene_idx=c(1:length(gene)) cell_idx=c(1:length(cell)) names(gene_idx)=gene names(cell_idx)=cell print(head(gene_idx[data$geneID])) mat=sparseMatrix(i=gene_idx[data$geneID],j=cell_idx[data$cellID],x=data$MIDCount) rownames(mat)=gene colnames(mat)=cell return(mat) } analyze <- function(mat,bin){ data=mat data[is.na(data)]=0 SeuObj<-CreateSeuratObject(counts = data,names.delim = "-", project = "SeuObj") SeuObj[["percent.mt"]] <- PercentageFeatureSet(SeuObj, pattern = "^mt-|^MT-|^Mt-") p <- VlnPlot(SeuObj,features=c("nCount_RNA","nFeature_RNA","percent.mt"),group.by="orig.ident") ggsave(file=paste0(prefix,".VlnPlot.pdf"),plot=p) SeuObj <- SCTransform(SeuObj, vars.to.regress = "percent.mt", verbose = FALSE) SeuObj <- RunPCA(SeuObj, verbose = FALSE) SeuObj <- RunUMAP(SeuObj, dims = 1:30, verbose = FALSE) SeuObj <- FindNeighbors(SeuObj, dims = 1:30, verbose = FALSE) SeuObj <- FindClusters(SeuObj, resolution = 0.3, verbose = FALSE) SeuObj@meta.data$coor_x=sub(rownames(SeuObj@meta.data),pattern = "_.*",replacement = "") SeuObj@meta.data$coor_y=sub(rownames(SeuObj@meta.data),pattern = ".*_",replacement = "") SeuObj@meta.data$coor_x=sub(SeuObj@meta.data$coor_x,pattern = "X",replacement = "") SeuObj@meta.data$coor_x=as.integer(SeuObj@meta.data$coor_x) SeuObj@meta.data$coor_y=as.integer(SeuObj@meta.data$coor_y) AllMG <<- FindAllMarkers(SeuObj) SeuObj <<- SeuObj p<-ggplot(SeuObj@meta.data,aes(x=coor_x,y=coor_y,color=nCount_RNA))+geom_point()+coord_fixed()+theme_void()+scale_color_viridis_c(option="B") ggsave(file=paste0(prefix,".nCount_RNA.pdf"),plot=p) p<-ggplot(SeuObj@meta.data,aes(x=coor_x,y=coor_y,color=nFeature_RNA))+geom_point()+coord_fixed()+theme_void()+scale_color_viridis_c(option="B") ggsave(file=paste0(prefix,".nFeature_RNA.pdf"),plot=p) apply( X=genes, MARGIN=1, FUN=function(x){ if(x %in% rownames(SeuObj@assays$SCT@data)){ name=paste0(prefix,"-",x,".pdf") p<-ggplot(SeuObj@meta.data,aes(x=coor_x,y=coor_y,fill=SeuObj@assays$SCT@data[x,]))+geom_tile()+oord_fixed()+ theme_void()+scale_color_viridis_c(option = "B",direction=-1)+labs(title=x,color="") ggsave(file=name, plot=p) } }) filename=paste(dir,"/bin",bin,".MG",sep="") write.table(AllMG,file=filename) save(SeuObj,file=paste(dir,"/bin",bin,".Rdata",sep="")) } bins=c(50) for(bin in bins){ mat<-read(args[1],bin) analyze(mat,bin) }