Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export(CalculateUCellScores)
export(Classify_ImmuneCells)
export(Classify_Myeloid)
export(Classify_TNK)
export(CombineTcellActivationClasses)
export(DownloadFromGEO)
export(ExpandGeneList)
export(ExtractGeneWeights)
Expand All @@ -25,6 +24,7 @@ export(PlotMarkerSeries)
export(PlotUcellCorrelation)
export(PredictCellTypeProbability)
export(PredictTcellActivation)
export(PredictTcellActivationUsingCustomModel)
export(RunCellTypist)
export(RunScGate)
export(RunScGateForModels)
Expand Down
414 changes: 144 additions & 270 deletions R/Classification.R

Large diffs are not rendered by default.

108 changes: 0 additions & 108 deletions man/CombineTcellActivationClasses.Rd

This file was deleted.

11 changes: 4 additions & 7 deletions man/PredictTcellActivation.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions man/PredictTcellActivationUsingCustomModel.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 32 additions & 49 deletions tests/testthat/test-classification.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ SimulateSeuratData <- function() {

#grab General component files (9 components)
for (i in 1:9) {
savedFile <- system.file(paste0("components/General_Activation_sPLSDA_component", i, ".tsv"), package = "RIRA")
savedFile <- system.file(paste0("components/GeneralizedTCR_Activation_sPLSDA_component", i, ".tsv"), package = "RIRA")
component <- utils::read.table(savedFile, header = T, sep = '\t')
features <- c(features, component$feature)
}
Expand Down Expand Up @@ -143,67 +143,51 @@ test_that("Predict TCell Activation works ", {

SimulateSeuratObj <- SimulateSeuratData()
SimulateSeuratObj <- PredictTcellActivation(SimulateSeuratObj)
SimulateSeuratObj <- PredictTcellActivation(SimulateSeuratObj, modelName = 'CD4')
SimulateSeuratObj <- PredictTcellActivation(SimulateSeuratObj, modelName = 'CD8')

#exhaustive check on component scores, since all are necessary for the default parameterization of the function
for (i in 1:6) {
pattern <- paste0("^CD8_Activation_sPLSDA_Score_", i, "_v\\d+$")
matchingCols <- grep(pattern, colnames(SimulateSeuratObj@meta.data), value = TRUE)
testthat::expect_true(length(matchingCols) > 0,
label = paste0("CD8_Activation_sPLSDA_Score_", i, " (with version) should be present"))
colName <- paste0("CD8_sPLSDA_Score_", i)
testthat::expect_true(colName %in% names(SimulateSeuratObj@meta.data))
}

for (i in 1:6) {
pattern <- paste0("^CD4_Activation_sPLSDA_Score_", i, "_v\\d+$")
matchingCols <- grep(pattern, colnames(SimulateSeuratObj@meta.data), value = TRUE)
testthat::expect_true(length(matchingCols) > 0,
label = paste0("CD4_Activation_sPLSDA_Score_", i, " (with version) should be present"))
colName <- paste0("CD4_sPLSDA_Score_", i)
testthat::expect_true(colName %in% names(SimulateSeuratObj@meta.data), label = paste0('Missing: colName'))
}

for (i in 1:9) {
pattern <- paste0("^General_Activation_sPLSDA_Score_", i, "_v\\d+$")
matchingCols <- grep(pattern, colnames(SimulateSeuratObj@meta.data), value = TRUE)
testthat::expect_true(length(matchingCols) > 0,
label = paste0("General_Activation_sPLSDA_Score_", i, " (with version) should be present"))
colName <- paste0("GeneralizedTCR_sPLSDA_Score_", i)
testthat::expect_true(colName %in% names(SimulateSeuratObj@meta.data))
}

#check that the scores are numeric
cd8_score_2 <- grep("^CD8_Activation_sPLSDA_Score_2_v\\d+$", colnames(SimulateSeuratObj@meta.data), value = TRUE)[1]
cd4_score_3 <- grep("^CD4_Activation_sPLSDA_Score_3_v\\d+$", colnames(SimulateSeuratObj@meta.data), value = TRUE)[1]
general_score_7 <- grep("^General_Activation_sPLSDA_Score_7_v\\d+$", colnames(SimulateSeuratObj@meta.data), value = TRUE)[1]

testthat::expect_type(SimulateSeuratObj@meta.data[5, cd8_score_2], "double")
testthat::expect_type(SimulateSeuratObj@meta.data[5, cd4_score_3], "double")
testthat::expect_type(SimulateSeuratObj@meta.data[5, general_score_7], "double")
testthat::expect_type(SimulateSeuratObj@meta.data[5, 'CD8_sPLSDA_Score_2'], "double")
testthat::expect_type(SimulateSeuratObj@meta.data[5, 'CD4_sPLSDA_Score_3'], "double")
testthat::expect_type(SimulateSeuratObj@meta.data[5, 'GeneralizedTCR_sPLSDA_Score_7'], "double")

#check that prediction portion of the function worked

#probabilities
testthat::expect_true(any(grepl("^CD8_sPLS_prob.*_v\\d+$", colnames(SimulateSeuratObj@meta.data))),
label = "CD8 probability columns (with version) should be present")
testthat::expect_true(any(grepl("^CD4_sPLS_prob.*_v\\d+$", colnames(SimulateSeuratObj@meta.data))),
label = "CD4 probability columns (with version) should be present")
testthat::expect_true(any(grepl("^General_sPLS_prob.*_v\\d+$", colnames(SimulateSeuratObj@meta.data))),
label = "General probability columns (with version) should be present")
testthat::expect_equal(4, sum(grepl("^CD8_sPLSDA_prob_", colnames(SimulateSeuratObj@meta.data))))
testthat::expect_equal(5, sum(grepl("^CD4_sPLSDA_prob_", colnames(SimulateSeuratObj@meta.data))))
testthat::expect_equal(14, sum(grepl("^GeneralizedTCR_sPLSDA_prob_", colnames(SimulateSeuratObj@meta.data))))

#classes
testthat::expect_true(any(grepl("^CD8_sPLS_class_v\\d+$", colnames(SimulateSeuratObj@meta.data))),
label = "CD8 class column (with version) should be present")
testthat::expect_true(any(grepl("^CD4_sPLS_class_v\\d+$", colnames(SimulateSeuratObj@meta.data))),
label = "CD4 class column (with version) should be present")
testthat::expect_true(any(grepl("^General_sPLS_class_v\\d+$", colnames(SimulateSeuratObj@meta.data))),
label = "General class column (with version) should be present")

testthat::expect_equal(1, sum(grepl("CD8_sPLSDA_class", colnames(SimulateSeuratObj@meta.data))))
testthat::expect_equal(1, sum(grepl("CD4_sPLSDA_class", colnames(SimulateSeuratObj@meta.data))))
testthat::expect_equal(1, sum(grepl("GeneralizedTCR_sPLSDA_class", colnames(SimulateSeuratObj@meta.data))))

#verify specific probability value
cd4_prob_resting1 <- grep("^CD4_sPLS_prob_Resting1_v\\d+$", colnames(SimulateSeuratObj@meta.data), value = TRUE)[1]
testthat::expect_equal(SimulateSeuratObj@meta.data[5, cd4_prob_resting1],
expected = 0.0086,
tolerance = 0.001)
testthat::expect_equal(SimulateSeuratObj@meta.data[5, 'CD4_sPLSDA_prob_Resting1'], expected = 0.0086, tolerance = 0.001)
})


test_that("CombineTcellActivationClasses combines classes and writes versioned columns", {
seuratObj <- SimulateSeuratData()
seuratObj <- PredictTcellActivation(seuratObj)
seuratObj <- PredictTcellActivation(seuratObj, combineClasses = FALSE)

#use the example mapping from roxygen docs
classMapping <- list(
"Th1" = c("Th1_MIP1B.neg_CD137.neg", "Th1_MIP1B.neg_CD137.pos", "Th1_MIP1B.pos"),
"Th17" = c("Th17"),
Expand All @@ -212,15 +196,15 @@ test_that("CombineTcellActivationClasses combines classes and writes versioned c
"Naive T cell" = c("Cultured_Bystander_NoBFA", "Cultured_Bystander_BFA")
)

seuratObj <- CombineTcellActivationClasses(seuratObj, classMapping = classMapping)
seuratObj <- .CombineTcellActivationClasses(seuratObj, classMapping = classMapping, sourceFieldPrefix = 'GeneralizedTCR_sPLSDA', outputFieldPrefix = 'CustomizedClasses')

#confirm combined class column exists with model/version naming
combinedClassCol <- grep("^General_Combined_Class_v\\d+$", colnames(seuratObj@meta.data), value = TRUE)
combinedClassCol <- grep("^CustomizedClasses_ConsensusClass$", colnames(seuratObj@meta.data), value = TRUE)
testthat::expect_true(length(combinedClassCol) == 1)

#confirm combined probability columns exist for each mapping
for (newClassName in names(classMapping)) {
probCol <- grep(paste0("^General_Combined_prob_", newClassName, "_v\\d+$"), colnames(seuratObj@meta.data), value = TRUE)
probCol <- grep(paste0("^CustomizedClasses_prob_", newClassName, "$"), colnames(seuratObj@meta.data), value = TRUE)
testthat::expect_true(length(probCol) == 1, label = paste0("Missing combined prob column for: ", newClassName))
}

Expand All @@ -231,19 +215,19 @@ test_that("CombineTcellActivationClasses combines classes and writes versioned c

#empty names not allowed
badMapping1 <- list(NULL = c("Th17"))
expect_error(CombineTcellActivationClasses(seuratObj, classMapping = badMapping1))
expect_error(.CombineTcellActivationClasses(seuratObj, classMapping = badMapping1, sourceFieldPrefix = 'GeneralizedTCR_sPLSDA', outputFieldPrefix = 'CustomizedClasses'))
#non-character values not allowed
badMapping2 <- list("Th17" = 1)
expect_error(CombineTcellActivationClasses(seuratObj, classMapping = badMapping2))
expect_error(.CombineTcellActivationClasses(seuratObj, classMapping = badMapping2, sourceFieldPrefix = 'GeneralizedTCR_sPLSDA', outputFieldPrefix = 'CustomizedClasses'))
#empty vector not allowed
badMapping3 <- list("Th17" = character(0))
expect_error(CombineTcellActivationClasses(seuratObj, classMapping = badMapping3))
expect_error(.CombineTcellActivationClasses(seuratObj, classMapping = badMapping3, sourceFieldPrefix = 'GeneralizedTCR_sPLSDA', outputFieldPrefix = 'CustomizedClasses'))
#duplicate classes across mappings not allowed
badMapping4 <- list("A" = c("Th17"), "B" = c("Th17"))
expect_error(CombineTcellActivationClasses(seuratObj, classMapping = badMapping4))
expect_error(.CombineTcellActivationClasses(seuratObj, classMapping = badMapping4, sourceFieldPrefix = 'GeneralizedTCR_sPLSDA', outputFieldPrefix = 'CustomizedClasses'))
#classes not present in predictions should error
badMapping5 <- list("X" = c("NotAClass"))
expect_warning(CombineTcellActivationClasses(seuratObj, classMapping = badMapping5))
expect_warning(.CombineTcellActivationClasses(seuratObj, classMapping = badMapping5, sourceFieldPrefix = 'GeneralizedTCR_sPLSDA', outputFieldPrefix = 'CustomizedClasses'))
})


Expand All @@ -264,7 +248,6 @@ test_that("Activation class mapping registry works and validates structure", {
}

# Unknown key should warn and return NULL
expect_warning(x <- GetActivationClassMapping('This.Key.Does.Not.Exist'))
expect_null(x)
expect_error(x <- GetActivationClassMapping('This.Key.Does.Not.Exist'))
})