File: test-colranks.R

package info (click to toggle)
r-bioc-scran 1.26.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,692 kB
  • sloc: cpp: 733; makefile: 2
file content (133 lines) | stat: -rw-r--r-- 4,540 bytes parent folder | download | duplicates (2)
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
# This tests out the scaledColRanks function.
# require(scran); require(testthat); source("setup.R"); source("test-colranks.R")

ncells <- 100
ngenes <- 200

set.seed(430000)
test_that("scaledColRanks correctly computes the ranks", {
	dummy <- matrix(rnbinom(ncells*ngenes, mu=10, size=20), ncol=ncells, nrow=ngenes)

    emp.ranks <- scaledColRanks(dummy)
    ref <- apply(dummy, 2, FUN=function(y) {
        r <- rank(y)
        r <- r - mean(r)
        r/sqrt(sum(r^2))/2
    })
    expect_equal(emp.ranks, ref)

    # Behaves with many ties.
	dummy <- matrix(sample(50, ncells*ngenes, replace=TRUE), ncol=ncells, nrow=ngenes)
    emp.ranks <- scaledColRanks(dummy)
    ref <- apply(dummy, 2, FUN=function(y) {
        r <- rank(y)
        r <- r - mean(r)
        r/sqrt(sum(r^2))/2
    })
    expect_equal(emp.ranks, ref)

    # Behaves with no ties.
	dummy <- matrix(rnorm(ncells*ngenes), ncol=ncells, nrow=ngenes)
    emp.ranks <- scaledColRanks(dummy)
    ref <- apply(dummy, 2, FUN=function(y) {
        r <- rank(y)
        r <- r - mean(r)
        r/sqrt(sum(r^2))/2
    })
    expect_equal(emp.ranks, ref)

    # Works correctly with shuffling.
    shuffled <- sample(ncells)
    emp.ranks <- scaledColRanks(dummy, subset.row=shuffled)
    ref <- apply(dummy, 2, FUN=function(y) {
        r <- rank(y[shuffled])
        r <- r - mean(r)
        r/sqrt(sum(r^2))/2
    })
    expect_equal(emp.ranks, ref)

    # Works correctly on sparse matrices.
    sparse <- abs(Matrix::rsparsematrix(ngenes, ncells, density=0.1))
    out <- scaledColRanks(sparse, min.mean=0)
    ref <- scaledColRanks(as.matrix(sparse), min.mean=0)
    expect_identical(unname(out), unname(ref))
})

set.seed(430001)
test_that("scaledColRanks responds to other options", {
	mat <- matrix(rnbinom(ncells*ngenes, mu=10, size=20), ncol=ncells, nrow=ngenes)

    # Subsetting.
    keep <- sample(ngenes, ngenes/2) 
    rnks <- scaledColRanks(mat, subset.row=keep)
    expect_identical(rnks, scaledColRanks(mat[keep,]))

    # Minimum mean.
    rnks <- scaledColRanks(mat, min.mean=10)
    expect_identical(rnks, scaledColRanks(mat, subset.row=scuttle::calculateAverage(mat) >= 10))

    # Transposition.
    rnks <- scaledColRanks(mat, transposed=TRUE)
    expect_identical(rnks, t(scaledColRanks(mat)))
})

set.seed(430002)
test_that("scaledColRanks naming is handled correctly", {
	mat <- matrix(rnbinom(ncells*ngenes, mu=10, size=20), ncol=ncells, nrow=ngenes)
    rownames(mat) <- seq_len(nrow(mat))
    colnames(mat) <- seq_len(ncol(mat))

    rnks <- scaledColRanks(mat)
    expect_identical(dimnames(rnks), dimnames(mat))

    rnks <- scaledColRanks(mat, transposed=TRUE)
    expect_identical(dimnames(rnks), rev(dimnames(mat)))

    rnks <- scaledColRanks(mat, subset.row=1:10)
    expect_identical(rownames(rnks), rownames(mat)[1:10])

    rnks <- scaledColRanks(mat, withDimnames=FALSE)
    expect_identical(dimnames(rnks), NULL)
})

set.seed(430003)
test_that("scaledColRanks handles sparsity requests", {
	mat <- matrix(rnbinom(ncells*ngenes, mu=1, size=20), ncol=ncells, nrow=ngenes)
    ref <- scaledColRanks(mat)

    library(Matrix)
    rnks <- scaledColRanks(mat, as.sparse=TRUE)
    expect_s4_class(rnks, "dgCMatrix")
    expect_identical(rnks!=0, as(mat, "dgCMatrix")!=0)

    centred <- sweep(rnks, 2, Matrix::colMeans(rnks), "-")
    centred <- as.matrix(centred)
    dimnames(centred) <- NULL
    expect_equal(centred, ref)

    # With transposition.
    rnks <- scaledColRanks(mat, as.sparse=TRUE, transposed=TRUE)
    expect_identical(rnks!=0, as(t(mat), "dgCMatrix")!=0)

    centred <- rnks - Matrix::rowMeans(rnks)
    centred <- as.matrix(centred)
    dimnames(centred) <- NULL
    expect_equal(centred, t(ref))
})

set.seed(430003)
test_that("scaledColRanks handles DA inputs", {
	dummy <- matrix(rnbinom(ncells*ngenes, mu=10, size=20), ncol=ncells, nrow=ngenes)
    expect_identical(scaledColRanks(dummy), scaledColRanks(DelayedArray(dummy)))
    expect_identical(scaledColRanks(dummy, transposed=TRUE), scaledColRanks(DelayedArray(dummy), transposed=TRUE))
    expect_identical(scaledColRanks(dummy, as.sparse=TRUE), scaledColRanks(DelayedArray(dummy), as.sparse=TRUE))
})

set.seed(430004)
test_that("scaledColRanks handles silly inputs", {
	mat <- matrix(rnbinom(ncells*ngenes, mu=10, size=20), ncol=ncells, nrow=ngenes)
    expect_error(scaledColRanks(mat[0,,drop=FALSE]), "rank variances of zero detected for a cell")

    out <- scaledColRanks(mat[,0,drop=FALSE])
    expect_identical(dim(out), c(as.integer(ngenes), 0L))
})