R/dataSClust.R

Defines functions dataSClust

Documented in dataSClust

#' Generate cell clusters using Seurat graph based clustering.
#'
#' @param rca.obj RCA object.
#' @param res Resolution parameter (between 0.0 and 1.0) to be used for clustering. Default: 0.5. Check the Seurat documentation for details.
#' @return RCA object.
#' @export
#'
dataSClust <- function(rca.obj,res=0.5) {
	projection.data <- as.matrix(rca.obj$projection.data)
	tempS<-Seurat::CreateSeuratObject(as.matrix(rca.obj$raw.data))
	if (require(HiClimR)){
		projection<-as.dist(1-HiClimR::fastCor(projection.data))
	}else{
		projection<-as.dist(1-cor(projection.data))
	}
	str(projection)
	tempS@reductions[["pca"]]<-new(Class = "DimReduc", cell.embeddings = matrix(0,0,0), assay.used = "RNA")
	tempS@reductions$pca@cell.embeddings<-as.matrix(projection)
	tempS<-Seurat::FindNeighbors(object = tempS)
	tempS<-Seurat::FindClusters(tempS,resolution = res)

	# Convert labels to colours for each tree cut
	if (length(unique(tempS$seurat_clusters))<41){
		dynamicColorsList<-list(WGCNA::labels2colors(tempS$seurat_clusters))
	} else {
		if (require(randomcolorR) & require(plotrix)){
		     clusterColors<-randomcoloR::distinctColorPalette(length(unique(tempS$seurat_clusters)))
		     clusterColors<-sapply(clusterColors,plotrix::color.id)
		     clusterColors<-sapply(clusterColors,function(x){return(x[1])})
		     names(clusterColors)<-unique(clusteringResult$cluster)
		     dynamicColorsList<-list(Colors=clusterColors[as.character(clusteringResult$cluster)])
		    } else{
			dynamicColorsList<-list(WGCNA::labels2colors(tempS$seurat_clusters))
			}
	}
	names(dynamicColorsList)<-c("Clusters")
	# Assign clustering result to RCA object
	rca.obj$clustering.out <- list(
	"cellTree" = tempS$seurat_clusters,
	"dynamicColorsList" = dynamicColorsList
	)
	# Return RCA object
	return(rca.obj)
}
linquynus/RCAv2-beta documentation built on Aug. 9, 2020, 12:34 a.m.