Skip to content
Snippets Groups Projects
Commit 24cb6e2f authored by Zhiyu Zhao's avatar Zhiyu Zhao
Browse files

05162024: SeuratObjectFunctions.R changed.

parent bcd4b134
No related merge requests found
...@@ -213,18 +213,22 @@ getAvgExp=function(seuratObject,cells,colID,geneDescriptions,features) { ...@@ -213,18 +213,22 @@ getAvgExp=function(seuratObject,cells,colID,geneDescriptions,features) {
colID='Altogether' colID='Altogether'
cells$Altogether='all' cells$Altogether='all'
} }
Idents(seuratObject)=cells[,colID]
features=unique(features) features=unique(features)
expressions=AverageExpression(seuratObject,assays='RNA',features=features) #By default this uses non-scaled values.
if (length(features)==1)
rownames(expressions[['RNA']])=features
expressions[['RNA']]=expressions[['RNA']][,mixedsort(colnames(expressions[['RNA']])),drop=F]
stats=cells%>%count(!!as.name(colID)) stats=cells%>%count(!!as.name(colID))
rownames(stats)=stats[,1] rownames(stats)=stats[,1]
stats=stats[colnames(expressions[['RNA']]),] ids=1:(dim(stats)[1])
colnames(expressions[['RNA']])=paste0(colnames(expressions[['RNA']]),'(n=',stats$n,')') names(ids)=rownames(stats)
if (colID=='Altogether')
Idents(seuratObject)='all'
else
Idents(seuratObject)=paste0('ID',ids[cells[,colID]],'@',cells[,colID])
expressions=AverageExpression(seuratObject,assays='RNA',features=features) #By default this uses non-scaled values. #This function doesn't preserve column names of expressions. a 'g' can be added as prefix.
if (length(features)==1)
rownames(expressions[['RNA']])=features
expressions[['RNA']]=data.frame(expressions[['RNA']][,mixedsort(colnames(expressions[['RNA']])),drop=F],check.names=F)
colnames(expressions[['RNA']])=paste0(rownames(stats),'(n=',stats$n,')')
if (!is.null(geneDescriptions)) if (!is.null(geneDescriptions))
expressions[['RNA']]=cbind(name=paste0('"',left_join(data.frame(gene=rownames(expressions[['RNA']])),geneDescriptions,by=c('gene'='SYMBOL'))$name,'"'),expressions[['RNA']]) expressions[['RNA']]=cbind2(name=paste0('"',left_join(data.frame(gene=rownames(expressions[['RNA']])),geneDescriptions,by=c('gene'='SYMBOL'))$name,'"'),expressions[['RNA']])
return(expressions) return(expressions)
} }
...@@ -238,11 +242,10 @@ getPercentExp=function(seuratObject,cells,colID,geneDescriptions,features) { ...@@ -238,11 +242,10 @@ getPercentExp=function(seuratObject,cells,colID,geneDescriptions,features) {
else else
data=t(GetAssayData(seuratObject,assay='RNA')[features,,drop=F]>0) data=t(GetAssayData(seuratObject,assay='RNA')[features,,drop=F]>0)
percent=aggregate.Matrix(data,cells[,colID]) percent=aggregate.Matrix(data,cells[,colID])
tempNames=rownames(percent)
stats=cells%>%count(!!as.name(colID)) stats=cells%>%count(!!as.name(colID))
percent=data.frame(t(percent*100/stats$n)) rownames(stats)=stats[,1]
colnames(percent)=paste0(tempNames,'(n=',stats$n,')') percent=data.frame(t(percent[rownames(stats),,drop=F]*100/stats$n),check.names=F)
percent=percent[,mixedsort(colnames(percent)),drop=F] colnames(percent)=paste0(rownames(stats),'(n=',stats$n,')')
if (!is.null(geneDescriptions)) if (!is.null(geneDescriptions))
percent=cbind(name=paste0('"',left_join(data.frame(gene=rownames(percent)),geneDescriptions,by=c('gene'='SYMBOL'))$name,'"'),percent) percent=cbind(name=paste0('"',left_join(data.frame(gene=rownames(percent)),geneDescriptions,by=c('gene'='SYMBOL'))$name,'"'),percent)
return(percent) return(percent)
...@@ -442,7 +445,7 @@ saveDimPlots=function(seuratObject,fileName,format='png',split.by=NULL,shape.by= ...@@ -442,7 +445,7 @@ saveDimPlots=function(seuratObject,fileName,format='png',split.by=NULL,shape.by=
if (is.null(split.by)) if (is.null(split.by))
ggsave(paste0(fileName,'.',i),plot=p,height=20,width=20) ggsave(paste0(fileName,'.',i),plot=p,height=20,width=20)
else else
ggsave(paste0(fileName,'.',i),plot=p,height=10,width=30) # ggsave(paste0(fileName,'.',i),plot=p,height=10,width=30)
ggsave(paste0(fileName,'.',i),plot=p,height=min(48,max(4,ceiling(infoList$nClusters/12)*4)),width=min(48,max(4,infoList$nClusters*4))) ggsave(paste0(fileName,'.',i),plot=p,height=min(48,max(4,ceiling(infoList$nClusters/12)*4)),width=min(48,max(4,infoList$nClusters*4)))
} }
} }
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment