From 4de8cb261dfb8c72d2c15f2a0f3d378fbd578c9c Mon Sep 17 00:00:00 2001 From: bbimber Date: Thu, 19 Dec 2024 14:31:06 -0800 Subject: [PATCH] Store the RIRA model names in the seurat object misc slot (#123) * Store the RIRA model names in the seurat object misc slot * Add test --- R/CellTypist.R | 15 ++++++++++++--- tests/testthat/test-celltypist.R | 3 +++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/R/CellTypist.R b/R/CellTypist.R index c85cf37..80a3f19 100644 --- a/R/CellTypist.R +++ b/R/CellTypist.R @@ -436,8 +436,9 @@ TrainCellTypist <- function(seuratObj, labelField, modelFile, minCellsPerClass = #' #' @export Classify_TNK <- function(seuratObj, assayName = Seurat::DefaultAssay(seuratObj), columnPrefix = 'RIRA_TNK_v2.', maxAllowableClasses = 6, minFractionToInclude = 0.01, minCellsToRun = 200, maxBatchSize = 600000, retainProbabilityMatrix = FALSE) { + modelName <- "RIRA_TNK_v2" seuratObj <- RunCellTypist(seuratObj = seuratObj, - modelName = "RIRA_TNK_v2", + modelName = modelName, # These are optimized for this model: pThreshold = 0.5, minProp = 0, useMajorityVoting = FALSE, mode = "prob_match", @@ -455,6 +456,8 @@ Classify_TNK <- function(seuratObj, assayName = Seurat::DefaultAssay(seuratObj), seuratObj$RIRA_TNK_v2.cellclass[seuratObj$RIRA_Immune_v2.majority_voting != 'T_NK'] <- 'Other' } + seuratObj@misc$RIRA_TNK_Model <- modelName + return(seuratObj) } @@ -473,8 +476,9 @@ Classify_TNK <- function(seuratObj, assayName = Seurat::DefaultAssay(seuratObj), #' #' @export Classify_Myeloid <- function(seuratObj, assayName = Seurat::DefaultAssay(seuratObj), columnPrefix = 'RIRA_Myeloid_v3.', maxAllowableClasses = 6, minFractionToInclude = 0.01, minCellsToRun = 200, maxBatchSize = 600000, retainProbabilityMatrix = FALSE) { + modelName <- "RIRA_FineScope_Myeloid_v3" seuratObj <- RunCellTypist(seuratObj = seuratObj, - modelName = "RIRA_FineScope_Myeloid_v3", + modelName = modelName, # These are optimized for this model: pThreshold = 0.5, minProp = 0, useMajorityVoting = FALSE, mode = "prob_match", @@ -498,6 +502,8 @@ Classify_Myeloid <- function(seuratObj, assayName = Seurat::DefaultAssay(seuratO vect[seuratObj@meta.data[[fn2]] %in% c('DC', 'Mature DC')] <- 'DC' seuratObj[[fn]] <- as.factor(vect) + seuratObj@misc$RIRA_Myeloid_Model <- modelName + return(seuratObj) } @@ -517,6 +523,7 @@ Classify_Myeloid <- function(seuratObj, assayName = Seurat::DefaultAssay(seuratO #' #' @export Classify_ImmuneCells <- function(seuratObj, assayName = Seurat::DefaultAssay(seuratObj), columnPrefix = 'RIRA_Immune_v2.', maxAllowableClasses = 6, minFractionToInclude = 0.01, minCellsToRun = 200, maxBatchSize = 600000, retainProbabilityMatrix = FALSE, filterDisallowedClasses = TRUE) { + modelName <- 'RIRA_Immune_v2' if ('RIRA_Immune_v1.cellclass' %in% names(seuratObj@meta.data)) { print('Dropping legacy RIRA_Immune_v1 columns') toDrop <- grep(names(seuratObj@meta.data), pattern = 'RIRA_Immune_v1', value = TRUE) @@ -526,7 +533,7 @@ Classify_ImmuneCells <- function(seuratObj, assayName = Seurat::DefaultAssay(seu } seuratObj <- RunCellTypist(seuratObj = seuratObj, - modelName = 'RIRA_Immune_v2', + modelName = modelName, # These are optimized for this model: minProp = 0.5, useMajorityVoting = TRUE, mode = "prob_match", @@ -569,6 +576,8 @@ Classify_ImmuneCells <- function(seuratObj, assayName = Seurat::DefaultAssay(seu ) ) + seuratObj@misc$RIRA_Immune_Model <- modelName + return(seuratObj) } diff --git a/tests/testthat/test-celltypist.R b/tests/testthat/test-celltypist.R index 5cb1177..d590ae9 100644 --- a/tests/testthat/test-celltypist.R +++ b/tests/testthat/test-celltypist.R @@ -80,6 +80,7 @@ test_that("celltypist runs for RIRA models", { seuratObj <- Classify_TNK(seuratObj, retainProbabilityMatrix = TRUE) print(table(seuratObj$RIRA_TNK_v2.cellclass)) + expect_equal('RIRA_TNK_v2', seuratObj@misc$RIRA_TNK_Model) expect_equal(4, length(unique(seuratObj$RIRA_TNK_v2.cellclass)), info = 'using RIRA T_NK', tolerance = 1) expect_equal(221, unname(table(seuratObj$RIRA_TNK_v2.cellclass)['CD4+ T Cells']), tolerance = 1) expect_equal(1028, unname(table(seuratObj$RIRA_TNK_v2.cellclass)['CD8+ T Cells']), tolerance = 1) @@ -93,6 +94,7 @@ test_that("celltypist runs for RIRA models", { print(table(seuratObj$RIRA_Myeloid_v3.cellclass)) print(table(seuratObj$RIRA_Myeloid_v3.coarseclass)) + expect_equal('RIRA_FineScope_Myeloid_v3', seuratObj@misc$RIRA_Myeloid_Model) expect_equal(5, length(unique(seuratObj$RIRA_Myeloid_v3.cellclass)), info = 'using RIRA Myeloid') expect_equal(32, unname(table(seuratObj$RIRA_Myeloid_v3.cellclass)['DC']), tolerance = 1) expect_equal(32, unname(table(seuratObj$RIRA_Myeloid_v3.coarseclass)['DC']), tolerance = 1) @@ -108,6 +110,7 @@ test_that("FilterDisallowedClasses works as expected", { print('RIRA_Immune_v2.cellclass:') print(table(seuratObj$RIRA_Immune_v2.cellclass)) + expect_equal('RIRA_Immune_v2', seuratObj@misc$RIRA_Immune_Model) expect_equal(256, sum(seuratObj$RIRA_Immune_v2.cellclass == 'Bcell', na.rm = T), tolerance = 1) expect_equal(571, sum(seuratObj$RIRA_Immune_v2.cellclass == 'Myeloid', na.rm = T)) expect_equal(1303, sum(seuratObj$RIRA_Immune_v2.cellclass == 'T_NK', na.rm = T))