1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
# This tests the pseudoBulkSpecific functions.
# library(testthat); library(scran); source("test-pseudo-spec.R")
set.seed(10000)
library(scuttle)
sce <- mockSCE(ncells=1000)
sce$samples <- gl(8, 125) # Pretending we have 8 samples.
# Making up some clusters.
sce <- logNormCounts(sce)
clusters <- kmeans(t(logcounts(sce)), centers=3)$cluster
# Creating a set of pseudo-bulk profiles:
info <- DataFrame(sample=sce$samples, cluster=clusters)
pseudo <- sumCountsAcrossCells(sce, info)
pseudo$DRUG <- gl(2,4)[pseudo$sample]
test_that("pseudoBulkSpecific works correctly in vanilla cases", {
# Spiking in DE for all clusters.
pseudo2 <- pseudo
xDRUG <- pseudo$DRUG
assay(pseudo2)[1,xDRUG==1] <- assay(pseudo2)[1,xDRUG==1] * 100
assay(pseudo2)[2,xDRUG==2] <- assay(pseudo2)[2,xDRUG==2] * 100
ref <- pseudoBulkDGE(pseudo2,
label=pseudo2$cluster,
design=~DRUG,
coef="DRUG2"
)
out <- pseudoBulkSpecific(pseudo2,
label=pseudo2$cluster,
design=~DRUG,
coef="DRUG2"
)
expect_identical(names(ref), names(out))
for (i in names(ref)) {
left <- ref[[i]]
right <- out[[i]]
expect_true(all(left$PValue <= right$PValue))
expect_identical(left$LogFC, right$LogFC)
expect_true(all(right$PValue[1:2] > 0.01))
expect_true(all(left$PValue[1:2] < 0.01))
}
# Also works for voom.
ref <- pseudoBulkDGE(pseudo2,
label=pseudo2$cluster,
design=~DRUG,
coef="DRUG2",
method="voom"
)
out <- pseudoBulkSpecific(pseudo2,
label=pseudo2$cluster,
design=~DRUG,
coef="DRUG2",
method="voom"
)
expect_identical(names(ref), names(out))
for (i in names(ref)) {
left <- ref[[i]]
right <- out[[i]]
expect_true(all(left$P.Value <= right$P.Value))
expect_identical(left$LogFC, right$LogFC)
expect_true(all(right$P.Value[1:2] > 0.05))
expect_true(all(left$P.Value[1:2] < 0.05))
}
})
test_that("pseudoBulkSpecific gives the same answers with a reference", {
out <- pseudoBulkSpecific(pseudo,
label=pseudo$cluster,
design=~DRUG,
coef="DRUG2"
)
ref <- pseudoBulkDGE(pseudo,
label=pseudo$cluster,
design=~DRUG,
coef="DRUG2"
)
metadata(ref)$tag <- "I'm here!"
metadata(ref[[1]])$tag <- "I'm still here!"
out2 <- pseudoBulkSpecific(pseudo,
label=pseudo$cluster,
design=~DRUG,
coef="DRUG2",
reference=ref
)
expect_identical(metadata(out2)$tag, "I'm here!")
expect_identical(metadata(out2[[1]])$tag, "I'm still here!")
metadata(out2)$tag <- NULL
metadata(out2[[1]])$tag <- NULL
expect_identical(out, out2)
})
test_that("pseudoBulkSpecific works correctly with sorting", {
ref <- pseudoBulkDGE(pseudo,
label=pseudo$cluster,
design=~DRUG,
coef="DRUG2"
)
out <- pseudoBulkSpecific(pseudo,
label=pseudo$cluster,
design=~DRUG,
coef="DRUG2",
reference=ref
)
out2 <- pseudoBulkSpecific(pseudo,
label=pseudo$cluster,
design=~DRUG,
coef="DRUG2",
reference=ref,
sorted=TRUE
)
for (i in names(ref)) {
left <- out[[i]]
right <- out2[[i]]
expect_identical(left[order(left$PValue),], right)
}
})
test_that("pseudoBulkSpecific works correctly with zero replacement", {
pseudo2 <- pseudo
assay(pseudo2)[1,pseudo2$cluster!=1] <- 0
ref <- pseudoBulkDGE(pseudo2,
label=pseudo2$cluster,
design=~DRUG,
coef="DRUG2"
)
out <- pseudoBulkSpecific(pseudo2,
label=pseudo2$cluster,
design=~DRUG,
coef="DRUG2",
reference=ref,
)
out2 <- pseudoBulkSpecific(pseudo2,
label=pseudo2$cluster,
design=~DRUG,
coef="DRUG2",
reference=ref,
missing.as.zero=TRUE
)
expect_identical(out[[1]]$OtherAverage[1], NA_real_)
expect_identical(out2[[1]]$OtherAverage[1], 0)
})
test_that("pseudoBulkSpecific errors out correctly", {
expect_error(pseudoBulkSpecific(pseudo,
label=pseudo$cluster,
design=~DRUG,
coef=1:2
), "cannot be specified")
})
|