目的
计算某些基因,在每一个亚群中基因表达的细胞占每个亚群中所有细胞的比例。
特定基因集为:c('Cd3d','Cd3e','Cd3g','Cd4','Cd8a','Cd8b1')
准备
前提条件是有rds文件,seurat进行分析一定要保存rds文件,后期很多个性化分析都可以直接用rds文件,可以节约很多时间。
require(Seurat)
require(dplyr)
require(Matrix)
require(magrittr)
mkdirs <- function(outdir,fp) {
if(!file.exists(file.path(outdir,fp))) {
# mkdirs(dirname(fp))
dir.create(file.path(outdir,fp))
}else{
print(paste(fp,"Dir already exists!",sep=" "))
unlink(file.path(outdir,fp), recursive=TRUE)
dir.create(file.path(outdir,fp))
}
}
rds_file<-'immune_combined.rds'
outdir<-outdir
prefix<-'test'
immune.combined <- readRDS(rds_file)
cluster_num<-length(unique(immune.combined@ident))
cluster<-unique(immune.combined@ident)
计算
data<-as.matrix(immune.combined@data)
data<-data[c('Cd3d','Cd3e','Cd3g','Cd4','Cd8a','Cd8b1'),]
# data[,immune.combined@ident[immune.combined@ident==1]]
# apply(a,1,function(x){length(x[x==0])})
# a<-data[c('Cd3d','Cd3e','Cd3g','Cd4','Cd8a','Cd8b1'),names(immune.combined@ident[immune.combined@ident==1])]
# as.matrix(apply(a,1,function(x){round(length(x[x!=0])/ncol(a),3)}))
cluster<-unique(immune.combined@ident)
yao<-data.frame()
yao<-data.frame(Gene=c('Cd3d','Cd3e','Cd3g','Cd4','Cd8a','Cd8b1'))
for (i in sort(cluster)){
# a<-data[c('Cd3d','Cd3e','Cd3g','Cd4','Cd8a','Cd8b1'),names(immune.combined@ident[immune.combined@ident==i])]
a<-data[,names(immune.combined@ident[immune.combined@ident==i])]
b<-as.matrix(apply(a,1,function(x){round(length(x[x!=0])/ncol(a),digit = 3)}))
yao<-cbind(yao,b[,1])
}
colnames(yao)<-c('Gene',sort(cluster))
write.csv(yao,paste(outdir,paste(prefix,'Gene.pct1.csv',sep=''),sep='/'),quote=F,row.names=FALSE)
网友评论