tests/testthat/test-mts.R

# Converted from mts_tests() All mode (single sequential file: tests share fixtures
# created between blocks, e.g. multisepOutputFull, mixModelClusters, mixturemodelClusters).

test_that("mts_mixModelCluster returns an output for negative gene expression values", {
  skip_on_cran()
  numeric_cols <- sapply(depMapXPR_subset, is.numeric)
  negative_eDF <- depMapXPR_subset
  negative_eDF[, numeric_cols] <- -negative_eDF[, numeric_cols]
  expect_no_error(mts_mixModelCluster(dataMatrix = negative_eDF[1:5,], cores = cores))
}) 

test_that("the mts_mixModelCluster() returns the desired output", {
  skip_on_cran()
  multisepOutput <- mts_mixModelCluster(dataMatrix = depMapXPR_subset[1:2,], cores = cores)
  expect_type(multisepOutput[[1]][,1], "character")
  expect_type(multisepOutput[[1]][,2], "double")
  expect_type(multisepOutput[[1]][,3], "integer")
})


test_that("mts_mixModelCluster returns an error when 0 is passed to the cores argument", {
  expect_error(mts_mixModelCluster(dataMatrix = depMapXPR_subset[1:5,], cores = 0), "cores should be >= 1")
})


test_that("mts_mixModelCluster returns an object inherited from the expected base type, list", {
  multisepOutput <- mts_mixModelCluster(dataMatrix = depMapXPR_subset[1:1,], cores = cores)
  expect_type(multisepOutput, "list")
})

test_that("mts_mixModelCluster gives an error when a gene expression matrix is not provided", {
  expect_error(mts_mixModelCluster())
})


test_that("mts_mixModelCluster with NA values in gene expression matrix gives an output", {
  skip_on_cran()
  df = depMapXPR_subset[1:10,]
  df[df ==0] <- NA
  expect_no_error(mts_mixModelCluster(dataMatrix = df, cores = cores))
})

test_that("0 values as the gene expression matrix gives an output with mts_mixModelCluster", {
  skip_on_cran()
  df2 <- replace(depMapXPR_subset[1:10,], 1:50, "0") 
  expect_no_error(mts_mixModelCluster(dataMatrix = df2, cores = cores))
})

test_that("mts_mixModelCluster can produce clusters for various data types", {
  expect_no_error(mts_mixModelCluster(dataMatrix = depMapCRISPRscores_subset[1:2,], cores = cores)) # check it can default
  expect_no_error(mts_mixModelCluster(dataMatrix = depMapMUT_subset[1:2,], cores = cores)) # check it can default
  expect_no_error(mts_mixModelCluster(dataMatrix = depMapXPR_subset[1:2,], cores = cores)) # check it can default
})

# mts_mixModelCluster_XPR() ------
test_that("mts_mixModelCluster_XPR with NA values in gene expression matrix gives an output", {
  skip_on_cran()
  df = depMapXPR_subset[1:10,]
  expect_no_error(mts_mixModelCluster_XPR(dataMatrix = df,
                                          GeneXPRthresh = 3.321928, 
                                          NumSampleThresh = 20,
                                          cores = cores))
})

test_that("mts_mixModelCluster_XPR returns an object inherited from the expected base type, list", {
  skip_on_cran()
  multisepOutput <- mts_mixModelCluster_XPR(dataMatrix = depMapXPR_subset[1:1,], 
                                            GeneXPRthresh = 3.321928, 
                                            NumSampleThresh = 20,
                                            cores = cores)
  expect_type(multisepOutput, "list")
})

test_that("mts_mixModelCluster_XPR with NA values in gene expression matrix gives an output", {
  skip_on_cran()
  df = depMapXPR_subset[1:10,]
  df[df ==0] <- NA
  expect_no_error(mts_mixModelCluster_XPR(dataMatrix = df,
                                          GeneXPRthresh = 3.321928, 
                                          NumSampleThresh = 20,
                                          cores = cores))
})

test_that("mts_mixModelCluster_XPR returns a vector with the specified length", {
  skip_on_cran()
  multisepOutput <- mts_mixModelCluster_XPR(dataMatrix = depMapXPR_subset[1:2,], cores = cores)
  expect_length(multisepOutput, 2)
})

# mts_clusterAvg() ------
multisepOutputFull <- mts_clusterAvg(exprsMatrix = depMapXPR_subset[1:4,], crisprMatrix = depMapCRISPRscores_subset[1:15,], cores = cores)

test_that("the code returns an object inherited from the expected base type, list", {
  expect_type(multisepOutputFull, (class = "list"))
})



test_that("an output is given for a crispr score matrix with 0 values", {
  skip_on_cran()
  crispr_0value = replace(depMapCRISPRscores_subset, 1:50, 0)
  expect_no_error(mts_clusterAvg(exprsMatrix = depMapXPR_subset[1:10,], crisprMatrix = crispr_0value, cores = cores))
})


test_that("the code returns an error when a crispr score matrix is not passed to the crisprMatrix argument and matches the error message", {
  skip_on_cran()
  expect_error(mts_clusterAvg(exprsMatrix = depMapXPR_subset[1:10,], crisprMatrix = , cores = cores), "No CRISPR Matrix Provided")
})


test_that("the code returns an error when an invalid value is given to cores argument", {
  skip_on_cran()
  expect_error(mts_clusterAvg(exprsMatrix = depMapXPR_subset[1:5,], crisprMatrix = depMapCRISPRscores_subset, cores=0), "cores should be >= 1")
})

test_that("the code returns an error when an expression matrix is not provided and matches the error message", {
  skip_on_cran()
  expect_error(mts_clusterAvg(crisprMatrix = depMapCRISPRscores_subset), "No Expression Matrix Provided: A gene by sample matrix is required to invoke this function")
})

test_that("mts_clusterAvg gives expected output", {
  expect_no_error(mts_clusterAvg(exprsMatrix = depMapXPR_subset[1:5,], crisprMatrix = depMapCRISPRscores_subset, cores = cores))
})

# mts_crisprPartition() ------
test_that("function can handle missing values", {
  skip_on_cran()
  df <- depMapCRISPRscores_subset[1:5, ]
  df[1, 1] <- NA
  expect_no_error(mts_crisprPartition(dataMatrix = df, cores = cores))
})

test_that("function can handle zero values", {
  skip_on_cran()
  df <- depMapCRISPRscores_subset[1:5, ]
  df[,] <- 0
  expect_no_error(mts_crisprPartition(dataMatrix = df, cores = cores))
})


test_that("invalid cores argument gives informative error", {
  skip_on_cran()
  expect_error(mts_crisprPartition(dataMatrix = depMapCRISPRscores_subset[1:5, ], cores = 0))
})

test_that("output contains expected column names for a known gene", {
  skip_on_cran()
  result <- mts_crisprPartition(dataMatrix = depMapCRISPRscores_subset[1:2, ], cores = 1)
  expect_named(result$EIF1AX, c("Sample", "Values", "Cluster_Assignment"))
})

test_that("missing dataMatrix argument returns an error", {
  skip_on_cran()
  expect_error(mts_crisprPartition(), "No Matrix Provided")
})

test_that("mts_crisprPartition returns a list with character and integer types in expected columns", {
  result <- mts_crisprPartition(dataMatrix = depMapCRISPRscores_subset[1:2, ], cores = 1)
  expect_type(result[[1]]$Sample, "character")
  expect_type(result[[1]]$Values, "double")
  expect_type(result[[1]]$Cluster_Assignment, "double")
})

# mts_plotCRISPRGeneCluster ------
clusters_depMapXPR <- mts_mixModelCluster(
  dataMatrix = depMapXPR_subset[1:15,],
  cores = cores
)
test_that("mts_plotCRISPRGeneCluster returns an error when a crispr score matrix is not provided and matches the error message", {
  skip_on_cran()
  expect_error(mts_plotCRISPRGeneCluster(resultList =, plotType = "mrna_only"))
  expect_error(mts_plotCRISPRGeneCluster(resultList = multisepOutputFull, mrna_gene="EIF1A",   crispr_gene = "NMT1",
                                         crisprMatrix= depMapCRISPRscores_subset, tissueMatrix=depMapTissue_subset,
                                         plotType = "Integrated"), "EIF1A is not in supplied result list")
  expect_error(mts_plotCRISPRGeneCluster(resultList = multisepOutputFull, mrna_gene="EIF1AY",   crispr_gene = "NM1",
                                         crisprMatrix= depMapCRISPRscores_subset, tissueMatrix=depMapTissue_subset,
                                         plotType = "Integrated"), "NM1 is not in supplied crispr matrix")
  expect_error(mts_plotCRISPRGeneCluster(resultList = multisepOutputFull, mrna_gene="EIF1AY",   crispr_gene = "NMT1",
                                         crisprMatrix= , tissueMatrix=depMapTissue_subset,
                                         plotType = "Integrated"), "No CRISPR Matrix Provided")
  expect_error(mts_plotCRISPRGeneCluster(resultList = multisepOutputFull, mrna_gene="EIF1AY",   crispr_gene = "NMT1",
                                         crisprMatrix=depMapCRISPRscores_subset,
                                         plotType = "Integrated"))
  expect_error(mts_plotCRISPRGeneCluster(mrna_gene="EIF1AY",   crispr_gene = "NMT1",
                                         crisprMatrix=depMapCRISPRscores_subset, tissueMatrix=depMapTissue_subset,
                                         mixModelClustersXPR = clusters_depMapXPR,
                                         plotType = "integrated"), "Plot type is not recognised, please use 'Integrated', 'mrna_only' or 'crispr_only'")
})


test_that("mts_plotCRISPRGeneCluster output follows no errors for running as intended", {
  expect_no_error(mts_plotCRISPRGeneCluster(resultList = multisepOutputFull, mrna_gene = "EIF1AY", crispr_gene = "NMT1",
                                            crisprMatrix= depMapCRISPRscores_subset, tissueMatrix=depMapTissue_subset,
                                            plotType = "Integrated"))
  expect_no_error(mts_plotCRISPRGeneCluster(resultList = multisepOutputFull, mrna_gene = "EIF1AY", crispr_gene = "NMT1",
                                            crisprMatrix= depMapCRISPRscores_subset, tissueMatrix=depMapTissue_subset,
                                            plotType = "mrna_only"))
  expect_no_error(mts_plotCRISPRGeneCluster(resultList = multisepOutputFull, mrna_gene = "EIF1AY", crispr_gene = "NMT1",
                                            crisprMatrix= depMapCRISPRscores_subset, tissueMatrix=depMapTissue_subset,
                                            plotType = "crispr_only"))
  expect_no_error(mts_plotCRISPRGeneCluster(resultList = multisepOutputFull, mrna_gene = "RPP25", crispr_gene = "DNAJC19",
                                            crisprMatrix= depMapCRISPRscores_subset, tissueMatrix=depMapTissue_subset,
                                            plotType = "crispr_only"))
  expect_no_error(mts_plotCRISPRGeneCluster(resultList = multisepOutputFull, mrna_gene = "EIF1AY", crispr_gene = "EIF1AX",
                                            crisprMatrix= depMapCRISPRscores_subset, tissueMatrix=depMapTissue_subset,
                                            plotType = "mrna_only"))
  expect_no_error(mts_plotCRISPRGeneCluster(resultList = multisepOutputFull, mrna_gene = "EIF1AY", crispr_gene = "EIF1AX",
                                            crisprMatrix= depMapCRISPRscores_subset, tissueMatrix=depMapTissue_subset,
                                            plotType = "Integrated"))
  expect_no_error(mts_plotCRISPRGeneCluster(mrna_gene = "DNAJC15", crispr_gene = "DNAJC19", crisprMatrix = depMapCRISPRscores_subset, 
                                            tissueMatrix = depMapTissue_subset, mixModelClustersXPR = clusters_depMapXPR, plotType = "Integrated"
  ))
})


test_that("the object returned by mts_plotCRISPRGeneCluster matches expected plot attributes", {
  skip_on_cran()
  crispr_plot = mts_plotCRISPRGeneCluster(resultList = multisepOutputFull, mrna_gene = "RPP25", crispr_gene = "DNAJC19",
                                          crisprMatrix= depMapCRISPRscores_subset, tissueMatrix=depMapTissue_subset,
                                          plotType = "crispr_only")
  expect_equal(crispr_plot$labels$title, c("DNAJC19 CRISPR score coloured by RPP25 expression cluster"))
  expect_equal(crispr_plot$labels$x, c("Sample"))
  expect_equal(crispr_plot$labels$y, c("DNAJC19 CRISPR Score"))
 # expect_equal(crispr_plot$labels$fill, c("mode"))
  
  mrna_plot = mts_plotCRISPRGeneCluster(resultList = multisepOutputFull, mrna_gene = "RPP25", crispr_gene = "DNAJC19",
                                        crisprMatrix= depMapCRISPRscores_subset, tissueMatrix=depMapTissue_subset,
                                        plotType = "mrna_only")
  
  expect_equal(mrna_plot$labels$title, c("RPP25 density split by MultiSEp cluster"))
  expect_equal(mrna_plot$labels$x, c("RPP25 log2 mRNA Expression"))
  expect_equal(mrna_plot$labels$y, c("Density"))
 # expect_equal(mrna_plot$labels$fill, c("mode"))
  
})


test_that("mts_plotCRISPRGeneCluster returns an object inherited from the expected base type, list", {
  skip_on_cran()
  crispr_plot2 = mts_plotCRISPRGeneCluster(resultList = multisepOutputFull, mrna_gene = "RPP25", crispr_gene = "DNAJC19",
                                           crisprMatrix= depMapCRISPRscores_subset, tissueMatrix=depMapTissue_subset,
                                           plotType = "crispr_only")
  expect_s3_class(crispr_plot2, "ggplot")
})

# mts_Crispr ------
test_that("mts_Crispr returns an object inherited from the expected base type, list", {
  skip_on_cran()
  mCrisprOut <- mts_Crispr(resultList = multisepOutputFull,
                           exprsMatrix = depMapXPR_subset[1:5,], crisprMatrix = depMapCRISPRscores_subset, cores = cores)
  expect_type(mCrisprOut, (class = "list"))
})


test_that("mts_Crispr returns informative errors for incorrect input", {
  skip_on_cran()
  expect_error(mts_Crispr(resultList = multisepOutputFull,
                          exprsMatrix = depMapXPR_subset[1:10,]), "No CRISPR Matrix Provided")
  expect_error(mts_Crispr(exprsMatrix = depMapXPR_subset[1:10,], crisprMatrix = depMapCRISPRscores_subset))
  expect_error(mts_Crispr(resultList = multisepOutputFull,
                          exprsMatrix = , crisprMatrix = depMapCRISPRscores_subset), "No Expression Matrix Provided")
  expect_error(mts_Crispr(resultList = multisepOutputFull,
                          exprsMatrix = depMapXPR_subset[1:10,], crisprMatrix = depMapCRISPRscores_subset, cores = 0), "cores should be >= 1")
})


test_that("mts_Crispr returns an output for a crispr score matrix of 0 values", {
  skip_on_cran()
  crispr_0value = replace(depMapCRISPRscores_subset, 1:50, 0)
  expect_no_error(mts_Crispr(resultList = multisepOutputFull,
                           exprsMatrix = depMapXPR_subset[1:10,], crisprMatrix = crispr_0value, cores = cores))
})


test_that("by either specifying the fcVal and pVal or leaving as default, will return equal result", {
  skip_on_cran()
  mCrisprOut_t5 <- mts_Crispr(resultList = multisepOutputFull,
                              exprsMatrix = depMapXPR_subset[1:5,], crisprMatrix = depMapCRISPRscores_subset, fcVal= -0.1, pVal = 0.1, cores = cores)
  mCrisprOut <- mts_Crispr(resultList = multisepOutputFull,
                           exprsMatrix = depMapXPR_subset[1:5,], crisprMatrix = depMapCRISPRscores_subset, cores = cores)
  expect_equal(mCrisprOut_t5, mCrisprOut)
})


test_that("changing the gene number will match the expression gene result", {
  skip_on_cran()
  resultList1 <- mts_clusterAvg(exprsMatrix = depMapXPR_subset[1:2,], crisprMatrix = depMapCRISPRscores_subset, cores = cores) # to specify two expression gene results
  mCrisprOut_t6 <- mts_Crispr(resultList = resultList1,
                              exprsMatrix = depMapXPR_subset[1:2,], crisprMatrix = depMapCRISPRscores_subset, fcVal= -0.1, pVal = 0.1, cores = cores)
  mCrisprOut_t7 <- mts_Crispr(resultList = resultList1,
                              exprsMatrix = depMapXPR_subset[1:2,], crisprMatrix = depMapCRISPRscores_subset, fcVal= -0.1, pVal = 0.1, cores = cores)
  expect_equal(mCrisprOut_t6, mCrisprOut_t7)
})

test_that("mts_Crispr output follows no errors for running as intended", {
  rl_small <- lapply(multisepList[1:3], function(g) {
      keep <- intersect(rownames(g[[1]]), rownames(depMapCRISPRscores_subset[1:15,]))
      g[[1]] <- g[[1]][keep, , drop = FALSE]
      g
    })

  expect_no_error(mts_Crispr(resultList  = rl_small,
                             exprsMatrix = depMapXPR_subset[1:15,],
                             crisprMatrix = depMapCRISPRscores_subset[1:15,], cores = cores))
  expect_no_error(mts_Crispr(resultList = rl_small,
                             exprsMatrix = depMapXPR_subset[1:15,],
                             crisprMatrix = depMapCRISPRscores_subset[1:15,], fcVal = 0.1, pVal = 0.25, cores = cores))
})

test_that("mts_Crispr correctly handles numeric effect size threshold for bidirectional analysis", {
  skip_on_cran()
    rl_small <- lapply(multisepList[1:3], function(g) {
      keep <- intersect(rownames(g[[1]]), rownames(depMapCRISPRscores_subset[1:15,]))
      g[[1]] <- g[[1]][keep, , drop = FALSE]
      g
    })
  result <- mCrisprOut <- mts_Crispr(
    resultList = rl_small,
    exprsMatrix = depMapXPR_subset[1:15,],
    crisprMatrix = depMapCRISPRscores_subset[1:15,],
    fcVal = -0.1,
    pVal = 0.1
  )
  expect_s3_class(result, "data.frame")
})


### mts_CrisprTS(): Tissue data ------
mCrisprOut <- mts_Crispr(resultList = multisepOutputFull,
                         exprsMatrix = depMapXPR_subset[1:5,], crisprMatrix = depMapCRISPRscores_subset[1:15,], cores = cores)
test_that("the two objects match using the default fcVal and pVal values in the ?mts_CrisprTS() documentation", {
  # mts_clusterAvg ->  mts_Crispr -> mts_CrisprTS
  mCrisprTS <- mts_CrisprTS(resultList = multisepOutputFull,
                            crisprMatrix = depMapCRISPRscores_subset[1:15,], tissueMatrix = depMapTissue_subset, fcVal = -0.1,
                            pVal = 0.25, allDisRes = mCrisprOut, cores = cores) # allDisRes = mts_Crispr
  
  mCrisprTS_test <- mts_CrisprTS(resultList = multisepOutputFull,
                                 crisprMatrix = depMapCRISPRscores_subset[1:15,], tissueMatrix = depMapTissue_subset, allDisRes = mCrisprOut, cores = cores)
  expect_equal(mCrisprTS, mCrisprTS_test)
})

test_that("mts_CrisprTS output follows no errors for running as intended", {
  expect_no_error(mts_CrisprTS(resultList = multisepList, crisprMatrix = depMapCRISPRscores_subset[1:10,],
                               tissueMatrix = depMapTissue_subset, fcVal = -0.1, pVal = 0.25, allDisRes = multisepCrisprList[1:5,], cores = cores))
  expect_no_error(mts_CrisprTS(resultList = multisepList,
                               crisprMatrix = depMapCRISPRscores_subset[1:10,], tissueMatrix = depMapTissue_subset,
                               allDisRes = multisepCrisprList[4:10,], cores = cores))
})


test_that("mts_CrisprTS code returns informative error messages when invalid arguments are passed to the function", {
  skip_on_cran()
  expect_error(mts_CrisprTS(crisprMatrix = depMapCRISPRscores_subset, tissueMatrix = depMapTissue_subset, fcVal = -0.1,
                            pVal = 0.25, allDisRes = mCrisprOut), "No MultiSEp results list provided - please run mts_clusterAvg to generate")
  expect_error(mts_CrisprTS(resultList = multisepOutputFull,
                            crisprMatrix = , tissueMatrix = depMapTissue_subset, fcVal = -0.1,
                            pVal = 0.25, allDisRes = mCrisprOut), "No CRISPR Matrix Provided")
  expect_error(mts_CrisprTS(resultList = multisepOutputFull,
                            crisprMatrix = depMapCRISPRscores_subset, tissueMatrix =, fcVal = -0.1,
                            pVal = 0.25, allDisRes = mCrisprOut), "No tissue matrix provided")
  expect_error(mts_CrisprTS(resultList = multisepOutputFull,
                            crisprMatrix = depMapCRISPRscores_subset, tissueMatrix =depMapTissue_subset, fcVal = -0.1,
                            pVal = 0.25, allDisRes = ), "No multisep-crispr result table provided - please run mts_Crispr to generate")
})

copyT = depMapTissue_subset
colnames(depMapTissue_subset) = c("samples", "diseaseType") 

test_that("mts_CrisprTS can handle a tissueMatrix with different column names", {
  skip_on_cran()
  mCrisprTS <- mts_CrisprTS(resultList = multisepList,crisprMatrix = depMapCRISPRscores_subset,
                            tissueMatrix = copyT,fcVal = -0.1, pVal = 0.25, allDisRes = multisepCrisprList[1:5,], cores = cores)
  mCrisprTS <- mts_CrisprTS(resultList = multisepList, crisprMatrix = depMapCRISPRscores_subset, 
                            tissueMatrix = depMapTissue_subset,fcVal = -0.1, pVal = 0.25, allDisRes = multisepCrisprList[1:5,], cores = cores)
  expect_equal(mCrisprTS, mCrisprTS)
})

### mts_Mutation ------
mixModelClusters <- mts_mixModelCluster(dataMatrix = depMapXPR_subset[1:5,])

test_that("mts_Mutation() returns informative error messages when invalid arguments are passed to the function", {
  skip_on_cran()
  expect_error(mts_Mutation(resultList = ,mutMatrix = depMapMUT_subset, tissueMatrix = depMapTissue_subset, pVal = 0.01))
  expect_error(mts_Mutation(resultList = mixModelClusters_depMapXPR, mutMatrix = ,  tissueMatrix = depMapTissue_subset,  pVal = 0.01), "No Mutation Matrix Provided")
  expect_error(mts_Mutation(resultList = NULL, mutMatrix = depMapMUT_subset, pVal = 0.01))
})

test_that("mts_Mutation gives expected output", {
  expect_no_error(mts_Mutation(resultList = mixModelClusters_depMapXPR, mutMatrix = depMapMUT_subset[1:10,], tissueMatrix = depMapTissue_subset, pVal = 0.25, cores = cores))
  expect_no_error(mts_Mutation(resultList = mixModelClusters_depMapXPR, mutMatrix = depMapMUT_subset[1:10,], tissueMatrix = depMapTissue_subset, cores = cores))
  expect_no_error(mts_Mutation(resultList = mixModelClusters_depMapXPR, mutMatrix = depMapMUT_subset[1:10,], cores = cores)) # can run without a tissueMatrix
})

test_that("When the tissue matrix does not contain tissue and cell_line as the colnames then mts_Mutation() wont throw an error", {
  skip_on_cran()
  colnames(depMapTissue_subset) = c("sample", "disease") 
  multisepMut_test <- mts_Mutation(resultList = mixModelClusters_depMapXPR,
                                   mutMatrix = depMapMUT_subset[1:10,], tissueMatrix = depMapTissue_subset,
                                   pVal = 0.25, cores = cores)
  expect_no_error(multisepMut_test)
})

test_that("the two objects match using the default pVal value in the ?multisepMutation() documentation and using different colnames in the tissue matrix ", {
  skip_on_cran()
  multisepMut_test1 = mts_Mutation(mixModelClusters_depMapXPR, depMapMUT_subset[1:10,], depMapTissue_subset, pVal = 0.01, cores = cores)
  multisepMut_test2 = mts_Mutation(mixModelClusters_depMapXPR, depMapMUT_subset[1:10,], copyT, cores = cores)
  expect_equal(multisepMut_test1, multisepMut_test2)
})

test_that("the object returned by mts_Mutation() matches the expected column attributes and returns a vector with the specified length", {
  skip_on_cran()
  multisepMut_test1 = mts_Mutation(mixModelClusters_depMapXPR, depMapMUT_subset[1:10,], copyT, pVal = 0.01, cores = cores)
  expect_named(multisepMut_test1, c('mRNA_gene', 'mutation_gene', 'tissue', 'chiSqPvalue', 'mutation_per_mode'))
  expect_length(multisepMut_test1, 5)
})


test_that("an error is expected when a mutation score matrix with the columns and rows in different order is given to the function", {
  skip_on_cran()
  mDT_t = as.data.frame(t(depMapMUT_subset))
  expect_error(mts_Mutation(resultList = mixModelClusters,
                            mutMatrix = mDT_t,
                            pVal = 0.25))
})


test_that("values are returned when using different pvalue threshold from default ", {
  skip_on_cran()
  expect_no_error(mts_Mutation(resultList = mixModelClusters,
                             mutMatrix = depMapMUT_subset,
                             pVal = 0.05, cores = cores))
})


## plotMutation(): Mutation plots ------
test_that("mts_plotMutation code returns informative error messages when invalid arguments are passed to the function", {
  skip_on_cran()
  expect_error(mts_plotMutation(resultList = , mrna_gene = "PSMB8", mut_gene = "TP53", 
                                mutMatrix = depMapMUT_subset, tissueMatrix = depMapTissue_subset, 
                                diseaseFilter = "Pancreatic Cancer"), "No result list provided: A mts_clusterAvg result object is required to run this function")
  expect_error(mts_plotMutation(resultList = mixModelClusters, mrna_gene = "PSM8", mut_gene = "TP53", 
                                mutMatrix = depMapMUT_subset, tissueMatrix = depMapTissue_subset, 
                                diseaseFilter = "Pancreatic Cancer"), "PSM8 is not in supplied result list")
  
  expect_error(mts_plotMutation(resultList = mixModelClusters, mrna_gene = "EIF1AY", mut_gene = "T53", 
                                mutMatrix = depMapMUT_subset, tissueMatrix = depMapTissue_subset, 
                                diseaseFilter = "Pancreatic Cancer"), "T53 is not in supplied mutation matrix")
  expect_error(mts_plotMutation(resultList = mixModelClusters, mrna_gene = "EIF1AY", mut_gene = "TP53", 
                                mutMatrix = , tissueMatrix = depMapTissue_subset, 
                                diseaseFilter = "Pancreatic Cancer"), "No Mutation Matrix Provided")
  expect_error(mts_plotMutation(resultList = mixModelClusters, mrna_gene = "EIF1AY", mut_gene = "TP53", 
                                mutMatrix = depMapMUT_subset, tissueMatrix = , 
                                diseaseFilter = "Pancreatic Cancer"), "No Tissue Matrix Provided")
  expect_error(mts_plotMutation(resultList = mixModelClusters, mrna_gene = "EIF1AY", mut_gene = "TP53", 
                                mutMatrix = depMapMUT_subset, tissueMatrix = depMapTissue_subset, 
                                diseaseFilter = " Cancer"), "Disease Filter not recognised: please check")
})


test_that("mts_plotMutation gives expected output", {
  expect_no_error(mts_plotMutation(resultList = mixModelClusters, mrna_gene = "RPP25", mut_gene = "TP53", 
                                   mutMatrix = depMapMUT_subset, tissueMatrix = depMapTissue_subset, 
                                   diseaseFilter = "Pancreatic Cancer")) 
  expect_no_error(mts_plotMutation(resultList = mixModelClusters, mrna_gene = "RPP25", mut_gene = "TP53", 
                                   mutMatrix = depMapMUT_subset, tissueMatrix = depMapTissue_subset))
  expect_no_error(mts_plotMutation(resultList = mixModelClusters, mrna_gene = "DNAJC15", mut_gene = "TP53", 
                                   mutMatrix = depMapMUT_subset, tissueMatrix = depMapTissue_subset))
  expect_no_error(mts_plotMutation(resultList = mixModelClusters, mrna_gene = "DNAJC15", mut_gene = "TP53", 
                                   mutMatrix = depMapMUT_subset, tissueMatrix = copyT)) # using different colnames
})

test_that("mts_plotMutation returns desired object", {
  skip_on_cran()
  expect_visible(mts_plotMutation(resultList = mixModelClusters, mrna_gene = "RPP25", mut_gene = "TP53", 
                                  mutMatrix = depMapMUT_subset, tissueMatrix = depMapTissue_subset, 
                                  diseaseFilter = "Pancreatic Cancer")) 
})

test_that("mts_plotMutation returns desired plot object with expected attributes", {
  skip_on_cran()
  plotM = mts_plotMutation(resultList = mixModelClusters, mrna_gene = "RPP25", mut_gene = "TP53", 
                           mutMatrix = depMapMUT_subset, tissueMatrix = depMapTissue_subset)
  expect_equal(plotM$labels$colour, c('Mutation Class'))
  expect_equal(plotM$labels$title, c('RPP25 Expression Cluster TP53 Enrichment'))
  expect_equal(plotM$labels$x, c('RPP25 mRNA Expression Cluster'))
  expect_equal(plotM$labels$y, c('RPP25 log2 mRNA Expression'))
})

test_that("plot layers match expectations",{
  skip_on_cran()
  plotM = mts_plotMutation(resultList = mixModelClusters, mrna_gene = "RPP25", mut_gene = "TP53", 
                           mutMatrix = depMapMUT_subset, tissueMatrix = depMapTissue_subset)
  expect_equal(class(plotM$layers[[1]]$stat), c("StatIdentity","Stat", "ggproto", "gg") )
  expect_equal(class(plotM$layers[[1]]$geom), c("GeomPoint","Geom", "ggproto", "gg") )
})

test_that("Scale range is NULL",{
  skip_on_cran()
  plotM = mts_plotMutation(resultList = mixModelClusters, mrna_gene = "RPP25", mut_gene = "TP53", 
                           mutMatrix = depMapMUT_subset, tissueMatrix = depMapTissue_subset)
  expect_null(plotM$scales$scales[[1]]$range$range)
})

test_that("mts_plotMutation returns an object from the expected base type, object", {
  skip_on_cran()
  expect_s3_class(mts_plotMutation(resultList = mixModelClusters, mrna_gene = "RPP25", mut_gene = "TP53",
                               mutMatrix = depMapMUT_subset, tissueMatrix = depMapTissue_subset), "ggplot")
  expect_s3_class(mts_plotMutation(resultList = mixModelClusters, mrna_gene = "RPP25", mut_gene = "TP53",
                               mutMatrix = depMapMUT_subset, tissueMatrix = depMapTissue_subset, diseaseFilter = "Bone Cancer"), "ggplot")
})

## mts_targetCoverageFromMutations ------
mDT <- mts_Mutation(resultList = mixModelClusters_depMapXPR,
                    mutMatrix = depMapMUT_subset, tissueMatrix = depMapTissue_subset,
                    pVal = 0.05, cores = cores)
TpopCoverage_test <- mts_targetCoverageFromMutations(
  mutData=depMapMUT_subset, 
  edgeData=mRes,
  rowNum=100, 
  cores = cores
)

test_that("mts_targetCoverageFromMutations returns the desired object with the expected column attributes", {
  expect_type(TpopCoverage_test$Gene, (class = "character"))
  expect_type(TpopCoverage_test$NeighbourCount, (class = "double"))
  expect_type(TpopCoverage_test$NumSamplesWithMutation, (class = "double"))
  expect_type(TpopCoverage_test$NeighbourMutationFrequency, (class = "double"))
  expect_type(TpopCoverage_test$NeighbourGenes, (class = "character"))
  expect_type(TpopCoverage_test$MutatedSamples, (class = "character"))
})

test_that("mts_targetCoverageFromMutations code returns informative error messages when invalid arguments are passed to the function", {
  skip_on_cran()
  expect_error(mts_targetCoverageFromMutations(mutData=, edgeData=mRes, rowNum=10),"Please provide a mutation score matrix")
  expect_error(mts_targetCoverageFromMutations(mutData=mDT, edgeData=, rowNum=10),"Please provide edgeData")
})

test_that("mts_targetCoverageFromMutations returns a data frame with expected length", {
  skip_on_cran()
  expect_s3_class(TpopCoverage_test, "data.frame")
  expect_equal(nrow(TpopCoverage_test), 80)
  expect_equal(ncol(TpopCoverage_test), 6)
})

## mts_formatMatrix ------
test_that("mts_formatMatrix returns informative error messages when invalid arguments are passed to the function", {
  skip_on_cran()
  expect_error(mts_formatMatrix(matrix = NULL,  cores =1), "matrix must be a data frame")
  expect_error(mts_formatMatrix(matrix = MUT_impactMatrix,  cores =0), "cores should be >= 1")
})

test_that("mts_formatMatrix gives expected output", {
  expect_no_error(mts_formatMatrix(matrix = MUT_impactMatrix, cores = cores)) 
})

## mts_genepairsChunkGeneration ------
test_that("mts_genepairsChunkGeneration gives expected output", {
  expect_no_error(mts_genepairsChunkGeneration(mixModelClusters1 = mixModelClusters_depMapCRISPR, num_tasks = 1)) # check it can default
  expect_no_error(mts_genepairsChunkGeneration(mixModelClusters1 = mixModelClusters_depMapCRISPR, mixModelClusters2 = mixModelClusters_depMapXPR, num_tasks=1))
})

test_that("mts_genepairsChunkGeneration returns informative error messages when invalid arguments are passed to the function", {
  skip_on_cran()
  expect_error(mts_genepairsChunkGeneration(mixModelClusters1 = , num_tasks = 1)) 
  expect_error(mts_genepairsChunkGeneration(mixModelClusters1 = NULL, mixModelClusters2 = mixModelClusters_depMapXPR, num_tasks=1))
  expect_error(mts_genepairsChunkGeneration(mixModelClusters1 = mixModelClusters, num_tasks="1"), 'Error: num_tasks value must be numeric and greater than 0.')
  expect_error(mts_genepairsChunkGeneration(mixModelClusters1 = mixModelClusters))
})

## mts_plotClusterDistribution ------
mixturemodelClusters <- mts_mixModelCluster_XPR(dataMatrix = depMapXPR_subset[c(1,4,9,14,20),], cores = cores)
CRISPRmixModelClusters = mts_crisprPartition(dataMatrix = depMapCRISPRscores_subset[1:15,], cores = cores)

test_that("mts_plotClusterDistribution function can produce a plot for both log2 and inverse values cluster function can plot log and non values", {
  skip_on_cran()
  logT= lapply(mixModelClusters, function(dfG) {
    dfG[,2]=log(as.numeric(dfG[,2]))
    return(dfG)
  })
  expect_visible(mts_plotClusterDistribution(mixModelClusters = logT, gene1 = "NMT2",gene2 = "STX2"))
  expect_visible(mts_plotClusterDistribution(mixModelClusters = mixModelClusters, gene1 = "DNAJC15", gene2 = "NMT2"))
})

test_that("mts_plotClusterDistribution gives expected output", {
  expect_no_error(mts_plotClusterDistribution(mixModelClusters = mixModelClusters_depMapXPR, gene1 = "EIF1AY", gene2="RPP25"))
  expect_no_error(mts_plotClusterDistribution(mixModelClusters = mixModelClusters_depMapCRISPR, gene1="RPP25L", gene2="NMT1"))
})

test_that("mts_plotClusterDistribution returns informative error messages when invalid arguments are passed to the function", {
  skip_on_cran()
  expect_error(mts_plotClusterDistribution(mixModelClusters = NULL,gene1 = "EIF1AY",gene2 = "RPP25"))
  expect_error(mts_plotClusterDistribution(mixModelClusters = mixModelClusters_depMapXPR, gene1 = "EIF1AX", gene2 = "RPP25"), "EIF1AX is not in supplied in the mixModelClusters list")
  expect_error(mts_plotClusterDistribution(mixModelClusters = mixModelClusters_depMapCRISPR, gene1 = "EIF1AY", gene2 = "NMT1"))
})

test_that("mts_plotClusterDistribution gives expected output for bidirectional analysis", {
  skip_on_cran()
  expect_visible(mts_plotClusterDistribution(mixModelClusters = mixturemodelClusters,mixModelClusters2 = CRISPRmixModelClusters,
                                             gene1 = "ACSL1",gene2 = "PSMB5"))
})

test_that("mts_plotClusterDistribution gives expected output for genes with differing cell line counts, applicable for CRISPR", {
  skip_on_cran()
  expect_visible(mts_plotClusterDistribution(mixModelClusters = CRISPRmixModelClusters,
                                             gene1 = "EIF1AX",gene2 = "NMT1"))
})

Lung_Cancer_cellLines <- depMapTissue_subset$cell_line[
  depMapTissue_subset$tissue == "Lung Cancer"]
Lung_Cancer_cellLines

# 2. subset XPR mixture model result list 
LungCancer_XPRmixturemodelClusters <- lapply(mixturemodelClusters, function(GMM) {
  GMM[GMM$Sample %in% Lung_Cancer_cellLines, ]})
length(LungCancer_XPRmixturemodelClusters[[1]][,3])  

# 3. subset CRISPR mixture model result list 
LungCancer_CRISPRmixModelClusters <- lapply(CRISPRmixModelClusters, function(GMM) {
  GMM[GMM$Sample %in% Lung_Cancer_cellLines, ]})
length(LungCancer_CRISPRmixModelClusters[[1]][,3])  

test_that("mts_plotClusterDistribution gives expected output for tissue specific analysis", {
  expect_visible(mts_plotClusterDistribution(mixModelClusters = mixturemodelClusters,mixModelClusters2 = CRISPRmixModelClusters,
                                             gene1 = "ACSL1",gene2 = "PSMB5", TScluster1 = LungCancer_XPRmixturemodelClusters, 
                                             TScluster2 = LungCancer_CRISPRmixModelClusters))
})

## mts_GeneDepCrispr ------
test_that("mts_GeneDepCrispr returns informative error messages when invalid arguments are passed to the function", {
  skip_on_cran()
  x = 1
  expect_error(mts_GeneDepCrispr(exprsMatrix = x, crisprMatrix =depMapCRISPRscores_subset[1:2,]),"exprsMatrix must be a data frame")
  expect_error(mts_GeneDepCrispr(exprsMatrix = depMapXPR_subset[1:2,], crisprMatrix =depMapCRISPRscores_subset[1:2,], tissueMatrix = 1), "tissueMatrix must be a data frame")
  expect_error(mts_GeneDepCrispr(exprsMatrix = ), "No Expression Matrix Provided")
  expect_error(mts_GeneDepCrispr(exprsMatrix = depMapXPR_subset[1:2,], crisprMatrix = ), "No CRISPR Matrix Provide")
})

test_that("mts_GeneDepCrispr returns desired object and attributes", {
  skip_on_cran()
  y2= mts_GeneDepCrispr(exprsMatrix = depMapXPR_subset[1:5,], crisprMatrix =depMapCRISPRscores_subset[1:5,], tissueMatrix = depMapTissue_subset, cores = cores)
  expect_s3_class(y2$CRISPR_Results, "data.frame")
  expect_s3_class(y2$CRISPR_TS_Results, "data.frame")
  expect_type(y2$CRISPR_Results$mRNA_gene, "character")
  expect_length(y2$CRISPR_TS_Results, 26)
  expect_equal(nrow(y2$CRISPR_Results), 7)
})

test_that("mts_GeneDepCrispr gives expected output", {
  skip_on_cran()
  expect_no_error(mts_GeneDepCrispr(exprsMatrix = depMapXPR_subset[1:5,], crisprMatrix = depMapCRISPRscores_subset[1:5,], cores = cores))
  expect_no_error(mts_GeneDepCrispr(exprsMatrix = depMapXPR_subset[1:5,], crisprMatrix =depMapCRISPRscores_subset[1:5,], tissueMatrix = depMapTissue_subset, cores = cores))
  expect_no_error(mts_GeneDepCrispr(exprsMatrix = depMapXPR_subset[1:5,], crisprMatrix = depMapCRISPRscores_subset[1:5,], tissueMatrix=copyT, cores = cores))
})

## mts_GeneDepID ------
test_that("mts_GeneDepID gives expected output", {
  skip_on_cran()
  expect_no_error(mts_GeneDepID(exprsMatrix = depMapXPR_subset[1:5,], crisprMatrix = depMapCRISPRscores_subset[1:5,], cores = cores))
  expect_no_error(mts_GeneDepID(exprsMatrix = depMapXPR_subset[1:5,], crisprMatrix =depMapCRISPRscores_subset[1:5,], tissueMatrix = depMapTissue_subset, cores = cores))
  expect_no_error(mts_GeneDepID(exprsMatrix = depMapXPR_subset[1:5,], crisprMatrix =depMapCRISPRscores_subset[1:5,], tissueMatrix = copyT, cores = cores))
})

test_that("mts_GeneDepID returns informative error messages when invalid arguments are passed to the function", {
  skip_on_cran()
  x = 1
  expect_error(mts_GeneDepID(exprsMatrix = x, crisprMatrix =depMapCRISPRscores_subset[1:2,]),"exprsMatrix must be a data frame")
  expect_error(mts_GeneDepID(exprsMatrix = depMapXPR_subset[1:2,], crisprMatrix =depMapCRISPRscores_subset[1:2,], tissueMatrix = 1), "tissueMatrix must be a data frame")
  expect_error(mts_GeneDepID(exprsMatrix = ), "No Expression Matrix Provided")
  expect_error(mts_GeneDepID(exprsMatrix = depMapXPR_subset[1:2,], crisprMatrix = ), "No CRISPR Matrix Provide")
})

test_that("mts_GeneDepID returns desired object and expected attributes", {
  skip_on_cran()
  y= mts_GeneDepID(exprsMatrix = depMapXPR_subset[1:5,], crisprMatrix =depMapCRISPRscores_subset[1:5,], tissueMatrix = depMapTissue_subset, cores = cores)
  expect_s3_class(y$CRISPR_Results, "data.frame")
  expect_s3_class(y$CRISPR_TS_Results, "data.frame")
  expect_length(y$CRISPR_Results, 25)
  expect_length(y$CRISPR_TS_Results, 26)
})

## mts_GeneDepMutation ------

test_that("mts_GeneDepMutation gives expected output", {
  skip_on_cran()
  expect_no_error(mts_GeneDepMutation(exprsMatrix = depMapXPR_subset[1:5,], mutMatrix = depMapMUT_subset[1:10,], tissueMatrix=depMapTissue_subset, cores = cores))
  expect_no_error(mts_GeneDepMutation(exprsMatrix = depMapXPR_subset[1:5,], mutMatrix = depMapMUT_subset[1:10,], tissueMatrix=copyT, cores = cores))
  expect_no_error(mts_GeneDepMutation(exprsMatrix = depMapXPR_subset[1:5,], mutMatrix = depMapMUT_subset[1:10,], tissueMatrix=depMapTissue_subset, pVal = 0.01, cores = cores))
})

test_that("mts_GeneDepMutation returns informative error messages when invalid arguments are passed to the function", {
  skip_on_cran()
  x = 1
  expect_error(mts_GeneDepMutation(exprsMatrix = x, mutMatrix =depMapMUT_subset[1:2,]),"exprsMatrix must be a data frame")
  expect_error(mts_GeneDepMutation(exprsMatrix = depMapXPR_subset[1:2,], mutMatrix =depMapMUT_subset[1:2,], tissueMatrix = 1), "tissueMatrix must be a data frame")
  expect_error(mts_GeneDepMutation(exprsMatrix = depMapXPR_subset[1:2,], mutMatrix =depMapMUT_subset[1:2,]), "No Tissue Matrix Provided")
})

test_that("mts_GeneDepMutation returns desired object and expected attributes", {
  skip_on_cran()
  x= mts_GeneDepMutation(exprsMatrix = depMapXPR_subset[1:5,], mutMatrix =depMapMUT_subset[1:5,], tissueMatrix = depMapTissue_subset, cores = cores)
  expect_s3_class(x, "data.frame")
  expect_named(x, c("mRNA_Gene","mutation_gene","tissue","pvalue" , "mMode"))
  expect_type(x$mRNA_Gene, "character")
  expect_type(x$pvalue, "double")
})

# mts_patternDetection ------
test_that("mts_patternDetection returns informative error messages when invalid arguments are passed to the function", {
  skip_on_cran()
  x = 1
  # genepairs 
  expect_error(mts_patternDetection(genepairs = x, mixModelClusters1 =mixModelClusters_depMapXPR))
  # GMM
  expect_error(mts_patternDetection(mixModelClusters1 = NULL))
  # SL
  expect_error(mts_patternDetection(mixModelClusters1 = mixModelClusters_depMapXPR, SyntheticLethalityPrediction = x ))
  # cores 
  expect_error(mts_patternDetection(mixModelClusters1 = mixModelClusters_depMapXPR, cores = 0))
  # directionality 
  expect_error(mts_patternDetection(mixModelClusters1 = mixModelClusters_depMapXPR, directionality = "none"))
  # effect size 
  expect_error(mts_patternDetection(mixModelClusters1 = mixModelClusters_depMapXPR, effectsize_threshold = "Yes"))
})

test_that("mts_patternDetection correctly handles numeric effect size threshold", {
  skip_on_cran()
  result <- mts_patternDetection(
    genepairs = data.frame(Gene1="PSMB8",Gene2="TTC7B"),
    mixModelClusters1 = mixturemodelClusters,
    effectsize_threshold = 0.5,
    p_adjustMethod = "BY",
    SyntheticLethalityPrediction = TRUE,
    cores = 1,
    verbose = FALSE
  )
  expect_s3_class(result, "data.frame")
})

test_that("mts_patternDetection correctly handles numeric effect size threshold for bidirectional analysis", {
  skip_on_cran()
  result <- mts_patternDetection(
    mixModelClusters1 = mixturemodelClusters,
    mixModelClusters2 = CRISPRmixModelClusters[1:10],
    effectsize_threshold = 0.5,
    p_adjustMethod = "BY",
    SyntheticLethalityPrediction = TRUE,
    cores = cores,
    verbose = FALSE
  )
  expect_s3_class(result, "data.frame")
})

test_that("mts_patternDetection correctly handles tissue specific analysis for both one directional and bidirectional analysis", {
  skip_on_cran()
  TS1 <- mts_patternDetection(mixModelClusters1 = LungCancer_CRISPRmixModelClusters,
                              mixModelClusters2 = LungCancer_XPRmixturemodelClusters, 
                              include_reverse_pairs = T, verbose=F,
                              effectsize_threshold = 0, SyntheticLethalityPrediction = T,
                              directionality = "enrichment",
                              qVal = 1) 
  expect_s3_class(TS1, "data.frame")
  
  TS2 <- mts_patternDetection(mixModelClusters1 = LungCancer_XPRmixturemodelClusters,
                              include_reverse_pairs = F, verbose=F,
                              effectsize_threshold = 0, SyntheticLethalityPrediction = T,
                              directionality = "enrichment",
                              qVal = 1) 
  expect_s3_class(TS2, "data.frame")
})


test_that("mts_patternDetection gives expected output for genes with differing cell line counts, applicable for CRISPR", {
  skip_on_cran()
  x = mts_patternDetection(genepairs = data.frame(Gene1 = "EIF1AX", Gene2 = "NMT1"),
                           mixModelClusters1 = mixModelClusters_depMapCRISPR,
                           cores = 1, qVal = 1, effectsize = F,
                           verbose = T)
  expect_s3_class(x, "data.frame")
  
})

test_that("mts_patternDetection throws error for invalid p_adjustMethod", {
  skip_on_cran()
  expect_error(
    mts_patternDetection(
      genepairs = data.frame(Gene1 = "G1", Gene2 = "G2"),
      mixModelClusters1 = mixModelClusters_depMapXPR,
      p_adjustMethod = "INVALID",
      cores = 1
    ),
    "Invalid p-value correction method"
  )
})

test_that("mts_patternDetection returns empty result when no gene pairs are valid", {
  result <- mts_patternDetection(
    genepairs = data.frame(Gene1 = "Gene1", Gene2 = "Gene2"),
    mixModelClusters1 = mixModelClusters_depMapXPR,
    cores = 1,
    verbose = FALSE
  )
  expect_equal(nrow(result), 0)
})

## mts_pairwiseExpressionPlot ------
test_that("mts_pairwiseExpressionPlot produces a plot for valid genes", {
  pdf(tempfile()); on.exit(dev.off())      # 7x7 device with room for the margins
  expect_no_error(mts_pairwiseExpressionPlot(exprsMatrix = depMapXPR_subset, gene1 = "STX2", gene2 = "VRK2"))
})

test_that("mts_pairwiseExpressionPlot returns informative errors for invalid input", {
    skip_on_cran()
  expect_error(mts_pairwiseExpressionPlot(gene1 = "STX2", gene2 = "VRK2"), "No Expression Matrix Provided")
  expect_error(mts_pairwiseExpressionPlot(exprsMatrix = as.matrix(depMapXPR_subset), gene1 = "STX2", gene2 = "VRK2"), "must be a data frame")
  expect_error(mts_pairwiseExpressionPlot(exprsMatrix = depMapXPR_subset, gene2 = "VRK2"), "No Gene1 provided")
})

## mts_omics ------
test_that("mts_omics returns a data frame for unidirectional XPR vs XPR (synthetic lethality)", {
  out <- mts_omics(dataMatrix = depMapXPR_subset[1:2,], qVal = 1, cores = cores)
  expect_s3_class(out, "data.frame")
})

test_that("mts_omics runs for bidirectional enrichment across data types (CRISPR vs XPR)", {
  expect_no_error(mts_omics(dataMatrix = depMapCRISPRscores_subset[1:2,],
                            dataMatrix2 = depMapXPR_subset[1:2,],
                            directionality = "enrichment", qVal = 1, cores = cores))
})

test_that("mts_omics runs in GDR pattern-analysis mode (SyntheticLethalityPrediction = FALSE)", {
  expect_no_error(mts_omics(dataMatrix = depMapXPR_subset[1:2,],
                            SyntheticLethalityPrediction = FALSE,
                            effectsize = FALSE, qVal = 1, cores = cores))
})

test_that("mts_omics handles categorical (mutation) data against expression", {
  mutImpact <- as.data.frame(ifelse(as.matrix(depMapMUT_small) == "WT", 2L, 1L),
                             stringsAsFactors = FALSE, check.names = FALSE)
  rownames(mutImpact) <- rownames(depMapMUT_small)
  expect_no_error(mts_omics(dataMatrix = mutImpact, dataMatrix2 = depMapXPR_small[1:2,],
                            categorical1 = TRUE, directionality = "depletion",
                            qVal = 1, effectsize = FALSE, cores = cores))
})

test_that("mts_omics accepts pre-computed cluster lists via mixModelClusters1", {
    skip_on_cran()
  mutImpact <- as.data.frame(ifelse(as.matrix(depMapMUT_small) == "WT", 2L, 1L),
                             stringsAsFactors = FALSE, check.names = FALSE)
  rownames(mutImpact) <- rownames(depMapMUT_small)
  mutClusters <- mts_formatMatrix(matrix = mutImpact, cores = cores)
  expect_no_error(mts_omics(mixModelClusters1 = mutClusters, directionality = "depletion",
                            qVal = 1, effectsize = FALSE, cores = cores))
})

## mts_InducedDependency ------
test_that("mts_InducedDependency returns a result for induced dependency analysis", {
  skip_on_cran()
  out <- mts_InducedDependency(resultList = multisepList[1:3],
                               exprsMatrix = depMapXPR_subset[1:15,],
                               crisprMatrix = depMapCRISPRscores_subset[c(16,27),],
                               cores = cores)
  expect_true(is.data.frame(out) || is.list(out))
})


test_that("mts_InducedDependency respects user fold-change and p-value thresholds", {
    skip_on_cran()
  expect_no_error(mts_InducedDependency(resultList = multisepList[1:3],
                                        exprsMatrix = depMapXPR_subset[1:15,],
                                        crisprMatrix = depMapCRISPRscores_subset[c(16,27),],
                                        fcVal = 0.1, pVal = 0.1, cores = cores))
})

test_that("mts_Crispr tolerates a CRISPR gene with no usable values (t.test regression)", {
  skip_on_cran()
  sparseCrispr <- depMapCRISPRscores_subset[1:10,]
  sparseCrispr[1, ] <- NA_real_
  expect_no_error(
    mCrispr_sparse <- mts_Crispr(resultList = multisepOutputFull,
                                 exprsMatrix = depMapXPR_subset[1:4, ],
                                 crisprMatrix = sparseCrispr, cores = cores)
  )
  expect_s3_class(mCrispr_sparse, "data.frame")
})

test_that("mts_InducedDependency tolerates a CRISPR gene with no usable values (t.test regression)", {
   skip_on_cran()
  sparseCrispr <- depMapCRISPRscores_subset[c(16,27),]
  sparseCrispr[1, ] <- NA_real_
  expect_no_error(
    id_sparse <- mts_InducedDependency(resultList = multisepList[1:3],
                                       exprsMatrix = depMapXPR_subset[1:15,],
                                       crisprMatrix = sparseCrispr, cores = cores)
  )
  expect_true(is.data.frame(id_sparse) || is.list(id_sparse))
})

test_that("mts_CrisprTS tolerates a CRISPR gene with no usable values (t.test regression)", {
  skip_on_cran()
  sparseCrispr <- depMapCRISPRscores_subset[1:15,]
  sparseCrispr[1, ] <- NA_real_
  allDis <- mts_Crispr(resultList = multisepOutputFull,
                       exprsMatrix = depMapXPR_subset[1:4, ],
                       crisprMatrix = sparseCrispr, cores = cores)
  expect_no_error(
    ts_sparse <- mts_CrisprTS(resultList = multisepOutputFull,
                              crisprMatrix = sparseCrispr,
                              tissueMatrix = depMapTissue_subset,
                              allDisRes = allDis, cores = cores)
  )
  expect_s3_class(ts_sparse, "data.frame")
})

test_that("mts_mixModelCluster reproduces known cluster sizes for EIF1AY", {
    skip_on_cran()
  gmm <- mts_mixModelCluster(dataMatrix = depMapXPR_subset[1:3, ], cores = cores)
  expect_named(gmm, c("EIF1AY", "RPP25", "DNAJC15"))
  expect_equal(nrow(gmm[["EIF1AY"]]), 1304L)
  expect_equal(as.integer(table(gmm[["EIF1AY"]][, 3])), c(544L, 312L, 50L, 398L))
})

test_that("mts_crisprPartition reproduces known cluster sizes for EIF1AX", {
    skip_on_cran()
  cp <- mts_crisprPartition(dataMatrix = depMapCRISPRscores_subset[1:2, ], cores = cores)
  expect_equal(as.integer(table(cp$EIF1AX$Cluster_Assignment)), c(914L, 134L))
})

test_that("mts_targetCoverageFromMutations reproduces known mutation counts", {
  skip_on_cran()
  tc <- mts_targetCoverageFromMutations(mutData = depMapMUT_subset, edgeData = mRes,
                                        rowNum = 100, cores = cores)
  expect_equal(nrow(tc), 80L)
  expect_equal(max(tc$NumSamplesWithMutation), 1552)
  expect_equal(sum(tc$NumSamplesWithMutation), 57995)
  expect_equal(tc$Gene[which.max(tc$NumSamplesWithMutation)], "BAIAP2L1")
})

test_that("mts_omics returns informative errors for invalid arguments", {
    skip_on_cran()
  expect_error(mts_omics(dataMatrix = depMapXPR_subset[1:3, ], genepairs = 1, qVal = 1, cores = cores), "must be either a dataframe or a matrix")
  expect_error(mts_omics(dataMatrix = depMapXPR_subset[1:3, ], cores = 0, qVal = 1), "cores should be >= 1")
  expect_error(mts_omics(dataMatrix = depMapXPR_subset[1:3, ], p_adjustMethod = "ZZ", qVal = 1, cores = cores), "Invalid p-value correction method")
  expect_error(mts_omics(dataMatrix = depMapXPR_subset[1:3, ], SyntheticLethalityPrediction = "yes", qVal = 1, cores = cores), "Invalid input for SyntheticLethalityPrediction")
  expect_error(mts_omics(dataMatrix = depMapXPR_subset[1:3, ], effectsize = "yes", qVal = 1, cores = cores), "Invalid input for effectsize")
  expect_error(mts_omics(dataMatrix = depMapXPR_subset[1:3, ], categorical1 = "yes", qVal = 1, cores = cores), "Invalid input for categorical1")
  expect_error(mts_omics(dataMatrix = depMapXPR_subset[1:3, ], directionality = "up", qVal = 1, cores = cores), "Invalid value of directionality")
})

test_that("mts_CrisprTS returns informative errors for wrong argument types", {
    skip_on_cran()
  expect_error(mts_CrisprTS(crisprMatrix = as.matrix(depMapCRISPRscores_subset)), "crisprMatrix must be a data frame")
  expect_error(mts_CrisprTS(crisprMatrix = depMapCRISPRscores_subset, resultList = 1), "resultList must be a list of data frames")
  expect_error(mts_CrisprTS(crisprMatrix = depMapCRISPRscores_subset, resultList = multisepList, tissueMatrix = 1), "tissueMatrix must be a data frame")
  expect_error(mts_CrisprTS(crisprMatrix = depMapCRISPRscores_subset, resultList = multisepList, tissueMatrix = depMapTissue_subset, allDisRes = 1), "allDisRes must be a data frame")
})

test_that("mts_plotCRISPRGeneCluster handles diseaseFilter values", {
  expect_error(mts_plotCRISPRGeneCluster(resultList = multisepOutputFull, mrna_gene = "RPP25", crispr_gene = "DNAJC19", crisprMatrix = depMapCRISPRscores_subset, tissueMatrix = depMapTissue_subset, plotType = "crispr_only", diseaseFilter = " nope"), "Disease filter not recognised")
  expect_no_error(mts_plotCRISPRGeneCluster(resultList = multisepOutputFull, mrna_gene = "RPP25", crispr_gene = "DNAJC19", crisprMatrix = depMapCRISPRscores_subset, tissueMatrix = depMapTissue_subset, plotType = "crispr_only", diseaseFilter = "All"))
  expect_no_error(mts_plotCRISPRGeneCluster(resultList = multisepOutputFull, mrna_gene = "RPP25", crispr_gene = "DNAJC19", crisprMatrix = depMapCRISPRscores_subset, tissueMatrix = depMapTissue_subset, plotType = "Integrated", diseaseFilter = "Lung Cancer"))
})

Try the MultiSEp package in your browser

Any scripts or data that you put into this service are public.

MultiSEp documentation built on Aug. 27, 2026, 5:07 p.m.