Skip to content

Commit

Permalink
update NameClusters function and test
Browse files Browse the repository at this point in the history
  • Loading branch information
ammichalowski committed Dec 10, 2022
1 parent 7b2f599 commit c88161e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
16 changes: 7 additions & 9 deletions R/Name_Clusters_by_Enriched_Cell_Type.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,7 +21,6 @@

NameClusters <-
function(SO = seurat.object,
metadata,
cluster.identities.table,
cluster.column.from.SO,
cluster.names,
Expand All @@ -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)
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down
27 changes: 16 additions & 11 deletions tests/testthat/test-Name_Clusters_by_Enriched_Cell_Type.R
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down

0 comments on commit c88161e

Please sign in to comment.