diff --git a/R/Name_Clusters_by_Enriched_Cell_Type.R b/R/Name_Clusters_by_Enriched_Cell_Type.R index d769642..eb80c40 100644 --- a/R/Name_Clusters_by_Enriched_Cell_Type.R +++ b/R/Name_Clusters_by_Enriched_Cell_Type.R @@ -4,7 +4,6 @@ #' #' #' @param SO Seurat-class object with cluster a cluster IDs column and "Likely_CellTypes" column present -#' @param metadata a data.frame containing at least a cluster ID column; the column name will be used to retrieve clusters from the input Seurat Object, which then will be matched with the cluster IDs in the Cluster Identities Table #' @param cluster.identities.table a data.frame with 2 columns - one with Cluster IDs (numeric) and the other with Cluster names #' @param cluster.column.from.SO name of the cluster ID column present in the meta.data slot in the Seurat Object #' @param cluster.names column containing cluster labels @@ -22,7 +21,6 @@ NameClusters <- function(SO = seurat.object, - metadata, cluster.identities.table, cluster.column.from.SO, cluster.names, @@ -35,14 +33,14 @@ NameClusters <- colval <- metadata.df[, cluster.column.from.SO] seurat.cluster <- colnames(SO@meta.data)[which(colnames(metadata.df) == cluster.column.from.SO)] - cluster_num <- + cluster.num <- as.data.frame.matrix(table(colval, SO@meta.data$Likely_CellType)) - clusnum.df <- melt(as.matrix(cluster_num)) - sums <- rowSums(cluster_num) - cluster_perc <- (cluster_num / sums) * 100 + clusnum.df <- melt(as.matrix(cluster.num)) + sums <- rowSums(cluster.num) + cluster.perc <- (cluster.num / sums) * 100 # draw plot with Likely cell type numbers per cluster - clus.df <- melt(as.matrix(cluster_perc)) + clus.df <- melt(as.matrix(cluster.perc)) clus.df$num <- clusnum.df$value colnames(clus.df) <- c("cluster", "celltype", "percent", "number") minclus <- min(clus.df$cluster) @@ -100,7 +98,7 @@ NameClusters <- print(table(output$Clusternames, output$Likely_CellType)) } else { - output <- cluster_num + output <- cluster.num cat( sprintf( "\n\nNo update in the Seurat Object - Cluster numbers in the metadata table are not identical with the Seurat Object's %s.\n\n", @@ -110,7 +108,7 @@ NameClusters <- } } else { - output <- cluster_num + output <- cluster.num cat( sprintf( "\n\nNo update in the Seurat Object - The number of clusters in the metadata table is not the same as in the Seurat Object's %s column.\n\n", diff --git a/tests/testthat/test-Name_Clusters_by_Enriched_Cell_Type.R b/tests/testthat/test-Name_Clusters_by_Enriched_Cell_Type.R index 9074216..34c3fa1 100644 --- a/tests/testthat/test-Name_Clusters_by_Enriched_Cell_Type.R +++ b/tests/testthat/test-Name_Clusters_by_Enriched_Cell_Type.R @@ -1,17 +1,15 @@ -# load data -seurat.object <- readRDS(test_path("fixtures", "SO_moduleScore.rds")) -metadata <- read.csv(test_path("fixtures", "Cell_Metadata.csv")) -identities_match <- read.csv(test_path("fixtures", "ClusterNames_match.csv")) -identities_diff <- read.csv(test_path("fixtures", "ClusterNames_diff.csv")) -identities_oneMore <- read.csv(test_path("fixtures", "ClusterNames_oneMore.csv")) + # tests test_that("Function returns a list with specified names", { + # load data + seurat.object <- readRDS(test_path("fixtures", "SO_moduleScore.rds")) + identities_match <- read.csv(test_path("fixtures", "ClusterNames_match.csv")) + output <- NameClusters( SO = seurat.object, - metadata = metadata, cluster.identities.table = identities_match, cluster.column.from.SO = "SCT_snn_res_0_2", cluster.names = "Cluster_Names", @@ -28,10 +26,16 @@ test_that("Function returns a list with specified names", { test_that("Function returns correct class", { + # load data + seurat.object <- readRDS(test_path("fixtures", "SO_moduleScore.rds")) + identities_match <- read.csv(test_path("fixtures", "ClusterNames_match.csv")) + identities_diff <- read.csv(test_path("fixtures", "ClusterNames_diff.csv")) + identities_oneMore <- read.csv(test_path("fixtures", "ClusterNames_oneMore.csv")) + output <- NameClusters( SO = seurat.object, - metadata = metadata, + #metadata = metadata, cluster.identities.table = identities_diff, cluster.column.from.SO = "SCT_snn_res_0_2", cluster.names = "Cluster_Names", @@ -42,7 +46,6 @@ test_that("Function returns correct class", { output <- NameClusters( SO = seurat.object, - metadata = metadata, cluster.identities.table = identities_oneMore, cluster.column.from.SO = "SCT_snn_res_0_2", cluster.names = "Cluster_Names", @@ -53,7 +56,6 @@ test_that("Function returns correct class", { output <- NameClusters( SO = seurat.object, - metadata = metadata, cluster.identities.table = identities_match, cluster.column.from.SO = "SCT_snn_res_0_2", cluster.names = "Cluster_Names", @@ -66,12 +68,15 @@ test_that("Function returns correct class", { test_that("Function returns Clusternames column", { + # load data + seurat.object <- readRDS(test_path("fixtures", "SO_moduleScore.rds")) + identities_match <- read.csv(test_path("fixtures", "ClusterNames_match.csv")) + colname = "Clusternames" output <- NameClusters( SO = seurat.object, - metadata = metadata, cluster.identities.table = identities_match, cluster.column.from.SO = "SCT_snn_res_0_2", cluster.names = "Cluster_Names",