[e26484]: / OmicsFold / tests / testthat / test-blockrank.R

Download this file

164 lines (119 with data), 5.7 kB

  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
library("mixOmics")
library("MASS")
library(OmicsFold)
library(mixOmics)
library(dplyr)
# Singleomics case
# Create simulated model
set.seed(20220222)
n_features <- 3
n_obs <- 1e4
X <- mvrnorm(n = n_obs, mu = rep(0, n_features),
Sigma = diag(n_features))
eps <- rnorm(n_obs, 0, 0.1)
Y <- X[, 1] + 0.5*X[, 2] + 0.3*X[, 3] + eps
Y <- as.factor(Y > 0)
splsda.simulated <- splsda(X, Y, ncomp = 2, keepX = rep(2, 2))
blockrank.splsda <- blockrank.splsda(splsda.simulated)
# Test simulated singleomics model
test_that("number of scores is number of features single", {
expect_equal(length(unlist(blockrank.splsda)), n_features)
})
test_that("scores are between 0 and 1 single", {
expect_true(max(unlist(blockrank.splsda)) <= 1 && min(unlist(blockrank.splsda)) >=0)
})
test_that("at least one feature has score = 1 single", {
expect_equal(max(unlist(blockrank.splsda)), 1)
})
# Test expected ranks correct
test_that("blockrank score of x1 is greater than that of x2, which is greater than that of x3 single",{
expect_true(blockrank.splsda[[1]][1] > blockrank.splsda[[1]][2] && blockrank.splsda[[1]][2] > blockrank.splsda[[1]][3])
})
# Create plsda model
plsda.simulated <- plsda(X, Y, ncomp = 2)
blockrank.plsda <- blockrank.splsda(plsda.simulated)
# Test simulated plsda model
test_that("number of scores is number of features plsda", {
expect_equal(length(unlist(blockrank.plsda)), n_features)
})
test_that("scores are between 0 and 1 plsda", {
expect_true(max(unlist(blockrank.plsda)) <= 1 && min(unlist(blockrank.plsda)) >=0)
})
test_that("at least one feature has score = 1 single", {
expect_equal(max(unlist(blockrank.plsda)), 1)
})
test_that("blockrank score of x1 is greater than that of x2, which is greater than that of x3 plsda",{
expect_true(blockrank.plsda[[1]][1] > blockrank.plsda[[1]][2] && blockrank.plsda[[1]][2] > blockrank.plsda[[1]][3])
})
# Multiomics tests
# Creating simulated model
X <- list(block1 = X[, 1:2], block2 = as.matrix(X[, 3]))
design <- matrix(0 , 2, 2)
ncomp <- 2
block.splsda.simulated <- block.splsda(X, Y, ncomp = ncomp, design = design)
blockrank.simulated <- blockrank.diablo(block.splsda.simulated)
# Testing simulated model
test_that("number of scores is number of features simulated", {
expect_equal(length(unlist(blockrank.simulated)), n_features)
})
test_that("scores are between 0 and 1 simulated", {
expect_true(max(unlist(blockrank.simulated)) <= 1 && min(unlist(blockrank.simulated)) >=0)
})
test_that("at least one feature has score = 1 simulated", {
expect_equal(max(unlist(blockrank.simulated)), 1)
})
test_that("blockrank score of x1 is greater than that of x2, which is greater than that of x3 simulated",{
expect_true(blockrank.simulated[[1]][1] > blockrank.simulated[[1]][2] && blockrank.simulated[[1]][2] > blockrank.simulated[[2]][1])
})
# Create block.plsda model
block.plsda.simulated <- block.splsda(X, Y, ncomp = ncomp, design = design)
blockrank.block.plsda <- blockrank.diablo(block.plsda.simulated)
# Testing block.plsda model
test_that("number of scores is number of features block.plsda", {
expect_equal(length(unlist(blockrank.block.plsda)), n_features)
})
test_that("scores are between 0 and 1 block.plsda", {
expect_true(max(unlist(blockrank.block.plsda)) <= 1 && min(unlist(blockrank.block.plsda)) >=0)
})
test_that("at least one feature has score = 1 block.plsda", {
expect_equal(max(unlist(blockrank.block.plsda)), 1)
})
test_that("blockrank score of x1 is greater than that of x2, which is greater than that of x3 block.plsda",{
expect_true(blockrank.block.plsda[[1]][1] > blockrank.block.plsda[[1]][2] && blockrank.block.plsda[[1]][2] > blockrank.block.plsda[[2]][1])
})
# Creating example model
data('breast.TCGA')
diablo.data <- list(mRNA = breast.TCGA$data.train$mrna,
miRNA = breast.TCGA$data.train$mirna,
proteomics = breast.TCGA$data.train$protein)
diablo.Y <- breast.TCGA$data.train$subtype
diablo.design <- matrix(0.1, ncol = length(diablo.data),
nrow = length(diablo.data),
dimnames = list(names(diablo.data), names(diablo.data)))
diag(diablo.design) <- 0
diablo.ncomp <- 2
diablo.keepX <- list(mRNA = c(6, 14), miRNA = c(5, 18), proteomics = c(6, 7))
example.model <- block.splsda(X = diablo.data, Y = diablo.Y,
ncomp = diablo.ncomp, keepX = diablo.keepX,
design = diablo.design)
blockrank.example <- blockrank.diablo(example.model)
# Testing example model
test_that("number of scores is number of features example", {
n_features <- sum(sapply(example.model[["X"]], ncol))
expect_equal(length(unlist(blockrank.example)), n_features)
})
test_that("scores are between 0 and 1 example", {
expect_true(max(unlist(blockrank.example)) <= 1 && min(unlist(blockrank.example)) >=0)
})
test_that("at least one feature has score = 1 example", {
expect_equal(max(unlist(blockrank.example)), 1)
})
test_that("all features with score of 0 are all the features not in model example", {
all.features.scores <- unlist(blockrank.example)
features.score.0 <- all.features.scores[all.features.scores == 0]
feature.loadings <- example.model[["loadings"]][names(example.model[["loadings"]]) != "Y"]
feature.loadings.logical <- sapply(feature.loadings, function(x) apply(x, 1, function(y) as.integer(any(as.logical(y)))))
feature.loadings.0 <- unlist(feature.loadings.logical)
feature.loadings.0 <- feature.loadings.0[feature.loadings.0 == 0]
expect_equal(features.score.0, feature.loadings.0)
})