|
a |
|
b/tests/testthat/test_data.R |
|
|
1 |
context("Test outbreaker data and settings") |
|
|
2 |
|
|
|
3 |
|
|
|
4 |
## test data ## |
|
|
5 |
test_that("test: data are processed fine", { |
|
|
6 |
## skip on CRAN |
|
|
7 |
skip_on_cran() |
|
|
8 |
|
|
|
9 |
|
|
|
10 |
## get data |
|
|
11 |
x <- fake_outbreak |
|
|
12 |
out <- outbreaker_data(dates = x$onset, dna = x$dna, w_dens = x$w) |
|
|
13 |
out_nodna <- outbreaker_data(dates = x$onset, w_dens = x$w) |
|
|
14 |
|
|
|
15 |
## check output |
|
|
16 |
expect_is(out, "list") |
|
|
17 |
expect_is(out$D, "matrix") |
|
|
18 |
expect_equal(out$max_range, 11) |
|
|
19 |
expect_equal(out_nodna$L, 0) |
|
|
20 |
expect_equal(out$L, 1e4) |
|
|
21 |
expect_equal(out$w_dens, out$f_dens) |
|
|
22 |
expect_equal(out$log_w_dens[1,], out$log_f_dens) |
|
|
23 |
expect_error(outbreaker_data(dates = 1, w_dens = c(0,-1)), |
|
|
24 |
"w_dens has negative entries") |
|
|
25 |
|
|
|
26 |
expect_error(outbreaker_data(dates = 1, w_dens = c(0,1), f_dens = c(0,-1)), |
|
|
27 |
"f_dens has negative entries") |
|
|
28 |
|
|
|
29 |
wrong_lab_dna <- x$dna |
|
|
30 |
rownames(wrong_lab_dna) <- paste0("host_", seq_len(nrow(wrong_lab_dna))) |
|
|
31 |
expect_error(outbreaker_data(dates = x$onset, dna = wrong_lab_dna, w_dens = x$w), |
|
|
32 |
"DNA sequence labels don't match case ids") |
|
|
33 |
|
|
|
34 |
|
|
|
35 |
}) |
|
|
36 |
|
|
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
|
40 |
|
|
|
41 |
|
|
|
42 |
|
|
|
43 |
test_that("outbreaker_data accepts epicontacts and case labelling", { |
|
|
44 |
|
|
|
45 |
## skip on CRAN |
|
|
46 |
skip_on_cran() |
|
|
47 |
|
|
|
48 |
## outbreaker time, ctd, no DNA ## |
|
|
49 |
## analysis |
|
|
50 |
set.seed(1) |
|
|
51 |
|
|
|
52 |
## get data |
|
|
53 |
x <- fake_outbreak |
|
|
54 |
|
|
|
55 |
ids_char <- replicate(length(fake_outbreak$sample), |
|
|
56 |
paste(sample(letters, 5, TRUE), collapse = "")) |
|
|
57 |
|
|
|
58 |
ids_num <- sample.int(1000, length(fake_outbreak$sample), FALSE) |
|
|
59 |
|
|
|
60 |
## check for character and numeric ids |
|
|
61 |
for(ids in list(ids_char, ids_num)) { |
|
|
62 |
|
|
|
63 |
## make epi_contacts object |
|
|
64 |
tTree <- data.frame(i = ids[x$ances], |
|
|
65 |
j = ids[1:length(x$ances)]) |
|
|
66 |
ctd <- sim_ctd(tTree, eps = 0.9, lambda = 0.1) |
|
|
67 |
epi_c <- suppressWarnings(epicontacts::make_epicontacts(linelist = data.frame(id = ids), |
|
|
68 |
contacts = ctd, |
|
|
69 |
directed = TRUE)) |
|
|
70 |
|
|
|
71 |
data <- outbreaker_data(dates = x$onset, |
|
|
72 |
dna = x$dna, |
|
|
73 |
ctd = epi_c, |
|
|
74 |
w_dens = x$w) |
|
|
75 |
|
|
|
76 |
## test recursiveness |
|
|
77 |
data <- outbreaker_data(data = data) |
|
|
78 |
|
|
|
79 |
## check correct contacts are labelled as 1 in matrix |
|
|
80 |
ctd_ind <- apply(ctd, 2, match, ids) |
|
|
81 |
expect_equal(rep(1, nrow(ctd)), data$contacts[ctd_ind[,c(2, 1)]]) |
|
|
82 |
expect_equal(rep(0, nrow(ctd)), data$contacts[ctd_ind]) |
|
|
83 |
|
|
|
84 |
## check directionality is being passed |
|
|
85 |
config <- create_config(data = data) |
|
|
86 |
|
|
|
87 |
## check ids are carried through |
|
|
88 |
expect_equal(data$ids, epi_c$linelist$id) |
|
|
89 |
|
|
|
90 |
## make sure directionality is carried through |
|
|
91 |
expect_true(config$ctd_directed) |
|
|
92 |
|
|
|
93 |
|
|
|
94 |
## case labelling via dates |
|
|
95 |
dates <- x$onset |
|
|
96 |
names(dates) <- ids |
|
|
97 |
|
|
|
98 |
data <- outbreaker_data(dates = dates, |
|
|
99 |
dna = x$dna, |
|
|
100 |
ctd = ctd, |
|
|
101 |
w_dens = x$w) |
|
|
102 |
|
|
|
103 |
## test recursiveness |
|
|
104 |
data <- outbreaker_data(data = data) |
|
|
105 |
|
|
|
106 |
## check direcionality working |
|
|
107 |
config <- create_config(ctd_directed = TRUE, |
|
|
108 |
data = data) |
|
|
109 |
|
|
|
110 |
## check contact numbers are being updated |
|
|
111 |
data <- add_convolutions(data, config) |
|
|
112 |
|
|
|
113 |
## check ids are carried through |
|
|
114 |
expect_equal(data$ids, as.character(ids)) |
|
|
115 |
|
|
|
116 |
## make sure directionality is carried through |
|
|
117 |
expect_true(config$ctd_directed) |
|
|
118 |
|
|
|
119 |
## check the number of contacts are correct |
|
|
120 |
expect_equal(nrow(ctd), sum(data$contacts)) |
|
|
121 |
|
|
|
122 |
## toggle directionality |
|
|
123 |
data <- outbreaker_data(dates = dates, |
|
|
124 |
dna = x$dna, |
|
|
125 |
ctd = ctd, |
|
|
126 |
w_dens = x$w) |
|
|
127 |
|
|
|
128 |
data <- outbreaker_data(data = data) |
|
|
129 |
|
|
|
130 |
config <- create_config(ctd_directed = FALSE) |
|
|
131 |
|
|
|
132 |
data <- add_convolutions(data, config) |
|
|
133 |
|
|
|
134 |
## check the number of contacts are correct |
|
|
135 |
expect_equal(2*nrow(ctd), sum(data$contacts)) |
|
|
136 |
|
|
|
137 |
## check correct contacts are labelled as 1 in matrix |
|
|
138 |
ctd_ind <- apply(ctd, 2, match, ids) |
|
|
139 |
ctd_ind <- rbind(ctd_ind, ctd_ind[,c(2, 1)]) |
|
|
140 |
expect_equal(rep(1, 2*nrow(ctd)), data$contacts[ctd_ind]) |
|
|
141 |
|
|
|
142 |
## make sure directionality is carried through |
|
|
143 |
expect_false(config$ctd_directed) |
|
|
144 |
|
|
|
145 |
## identify non-matching labels |
|
|
146 |
wrong_dna <- x$dna |
|
|
147 |
rownames(wrong_dna) <- 1:length(x$onset) |
|
|
148 |
|
|
|
149 |
expect_error(data <- outbreaker_data(dates = dates, |
|
|
150 |
dna = wrong_dna, |
|
|
151 |
ctd = ctd, |
|
|
152 |
w_dens = x$w, |
|
|
153 |
ctd_directed = TRUE), |
|
|
154 |
"DNA sequence labels don't match case ids") |
|
|
155 |
|
|
|
156 |
} |
|
|
157 |
|
|
|
158 |
}) |
|
|
159 |
|
|
|
160 |
|
|
|
161 |
|