Skip to content

Commit 90ba0e1

Browse files
committed
Update test-summarize-by-patient.R
1 parent 70a710f commit 90ba0e1

File tree

1 file changed

+94
-32
lines changed

1 file changed

+94
-32
lines changed

tests/testthat/test-summarize-by-patient.R

+94-32
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# fusion = gnomeR::fusion,
1212
# cna = cna)
1313
#
14-
# gen_dat2 <- gnomeR::summarize_by_gene(gen_dat)
14+
# gen_dat2 <- gnomeR::summarize_by_patient(gen_dat)
1515
#
1616
# tp53_1 <- gen_dat %>% select(contains("TP53"))
1717
# tp53_1 <- (tp53_1$TP53== 1 |tp53_1$TP53.Del== 1)%>% sum()
@@ -24,7 +24,7 @@
2424

2525
test_that("test simplify marix needs a data frame", {
2626

27-
expect_error(summarize_by_gene(gene_binary = c(1:10)))
27+
expect_error(summarize_by_patient(gene_binary = c(1:10)))
2828

2929
})
3030

@@ -35,11 +35,49 @@ test_that("test simplify matrix needs a data frame", {
3535
mutation = gnomeR::mutations,
3636
fusion = gnomeR::sv)
3737

38-
expect_no_error(gen_dat2 <- gnomeR::summarize_by_gene(gen_dat))
38+
expect_no_error(gen_dat2 <- gnomeR::summarize_by_patient(gen_dat))
3939

4040

4141
})
4242

43+
test_that("test output matches summarize_by_gene if only 1 sample per patient", {
44+
45+
# restrict to 1 sample/patient
46+
samples <- gnomeR::mutations %>%
47+
mutate(patient_id = gnomeR::extract_patient_id(sampleId)) %>%
48+
distinct(patient_id, .keep_all = TRUE) %>%
49+
pull(sampleId)
50+
51+
gen_dat <- create_gene_binary(samples = samples,
52+
mutation = gnomeR::mutations,
53+
fusion = gnomeR::sv)
54+
55+
expect_equal(gnomeR::summarize_by_gene(gen_dat) %>% select(-sample_id),
56+
gnomeR::summarize_by_patient(gen_dat) %>% select(-patient_id))
57+
58+
59+
})
60+
61+
test_that("test that output is only 1 row/patient", {
62+
63+
# restrict to 1 sample/patient
64+
# bind gnomeR::mutations to ensure there are duplicates
65+
samples <- bind_rows(gnomeR::mutations,
66+
gnomeR::mutations) %>%
67+
mutate(patient_id = gnomeR::extract_patient_id(sampleId)) %>%
68+
pull(sampleId)
69+
70+
gen_dat <- create_gene_binary(samples = samples,
71+
mutation = gnomeR::mutations,
72+
fusion = gnomeR::sv)
73+
74+
n_rec_pt <- gnomeR::summarize_by_patient(gen_dat) %>%
75+
count(patient_id, name = "n_rec_pt") %>%
76+
distinct(n_rec_pt) %>%
77+
pull(n_rec_pt)
78+
79+
expect_equal(n_rec_pt, n_rec_pt)
80+
})
4381

4482
test_that("test that genes are properly summarized", {
4583
samples <- Reduce(intersect, list(gnomeR::mutations$sampleId, gnomeR::cna$sampleId,
@@ -50,24 +88,49 @@ test_that("test that genes are properly summarized", {
5088
mutation = gnomeR::mutations,
5189
cna = gnomeR::cna,
5290
fusion = gnomeR::sv,
53-
specify_panel = "impact")%>%
91+
specify_panel = "impact") %>%
5492
dplyr::select(c(sample_id, starts_with("ARI"), starts_with("MAPK1"), starts_with("ERG"))))
5593

5694

57-
sum_impact <- summarize_by_gene(bin_impact)%>%
58-
dplyr::mutate(across(!sample_id, as.numeric))
95+
sum_impact <- summarize_by_patient(bin_impact) %>%
96+
dplyr::mutate(across(!patient_id, as.numeric)) %>%
97+
arrange(patient_id)
5998

6099

61100
bin_impact_test <- bin_impact %>%
62-
tidyr::pivot_longer(!sample_id)%>%
101+
mutate(patient_id = gnomeR::extract_patient_id(sample_id)) %>%
102+
select(-sample_id) %>%
103+
tidyr::pivot_longer(cols = c(everything(), -patient_id)) %>%
63104
mutate(name = str_remove(name, ".Amp|.Del|.fus"))%>%
64-
tidyr::pivot_wider(names_from = name, values_from = value, values_fn = function (x) sum(x))%>%
65-
mutate(across(!sample_id, ~ifelse(. > 0, 1, 0)))%>%
66-
relocate(colnames(sum_impact))
105+
tidyr::pivot_wider(names_from = name, values_from = value, values_fn = function (x) sum(x)) %>%
106+
mutate(across(.cols = c(-patient_id),
107+
~ifelse(. > 0, 1, 0))) %>%
108+
mutate_if(~ all(is.na(.)), ~as.numeric(NA_integer_)) %>%
109+
relocate(colnames(sum_impact)) %>%
110+
arrange(patient_id)
67111

68112
expect_equal(sum_impact, bin_impact_test)
113+
})
114+
115+
test_that("test alteration hierarchy", {
116+
# alteration if alteration on any sample
117+
# no alteration if no alteration on all samples (no NAs)
118+
# else NA
69119

120+
# dummy binary matrix with comparisons of interest
121+
bin_impact_dummy <- tribble(~sample_id, ~ALK, ~EGFR, ~KRAS, ~TP53, ~KMT2D, ~SMARCA4,
122+
paste0(gnomeR::mutations$patientId[1], "-T01", "-IM5"), 1, 0, 1, 1, 0, NA_integer_,
123+
paste0(gnomeR::mutations$patientId[1], "-T01", "-IM5"), 1, 0, 0, NA_integer_, NA_integer_, NA_integer_)
70124

125+
sum_impact <- summarize_by_patient(bin_impact_dummy) %>%
126+
dplyr::mutate(across(!patient_id, as.numeric)) %>%
127+
arrange(patient_id)
128+
129+
bin_impact_expected <- tribble(~patient_id, ~ALK, ~EGFR, ~KRAS, ~TP53, ~KMT2D, ~SMARCA4,
130+
gnomeR::mutations$patientId[1], 1, 0, 1, 1, NA_integer_, NA_integer_) %>%
131+
mutate(across(-patient_id, as.numeric))
132+
133+
expect_equal(sum_impact, bin_impact_expected)
71134
})
72135

73136

@@ -85,17 +148,22 @@ test_that("test what happens to columns with all NA", {
85148
select(c(sample_id, starts_with("AR"), starts_with("PLCG2"), starts_with("PPM1D")))
86149

87150

88-
sum_impact <- summarize_by_gene(bin_impact)%>%
89-
mutate(across(!sample_id, as.numeric))
151+
sum_impact <- summarize_by_patient(bin_impact)%>%
152+
mutate(across(!patient_id, as.numeric)) %>%
153+
arrange(patient_id)
90154

91155

92156
bin_impact_test <- bin_impact %>%
93-
tidyr::pivot_longer(!sample_id)%>%
157+
mutate(patient_id = gnomeR::extract_patient_id(sample_id)) %>%
158+
select(-sample_id) %>%
159+
tidyr::pivot_longer(cols = c(everything(), -patient_id)) %>%
94160
mutate(name = str_remove(name, ".Amp|.Del|.fus"))%>%
95-
tidyr::pivot_wider(names_from = name, values_from = value, values_fn = function (x) sum(x))%>%
96-
mutate(across(!sample_id, ~ifelse(. > 0, 1, 0)))%>%
97-
relocate(colnames(sum_impact))%>%
98-
mutate_if(~ all(is.na(.)), ~as.numeric(NA_integer_))
161+
tidyr::pivot_wider(names_from = name, values_from = value, values_fn = function (x) sum(x)) %>%
162+
mutate(across(.cols = c(-patient_id),
163+
~ifelse(. > 0, 1, 0))) %>%
164+
mutate_if(~ all(is.na(.)), ~as.numeric(NA_integer_)) %>%
165+
relocate(colnames(sum_impact)) %>%
166+
arrange(patient_id)
99167

100168
expect_equal(sum_impact, bin_impact_test)
101169

@@ -106,10 +174,11 @@ test_that("test what happens to columns with all NA", {
106174

107175

108176
test_that("other vars are retained", {
109-
samples <- Reduce(intersect, list(gnomeR::mutations$sampleId,
110-
gnomeR::cna$sampleId,
111-
gnomeR::sv$sampleId))
112-
177+
samples <- gnomeR::mutations %>%
178+
mutate(patient_id = gnomeR::extract_patient_id(sampleId)) %>%
179+
head(50) %>%
180+
pull(sampleId) %>%
181+
unique()
113182

114183
bin_impact <- create_gene_binary(samples = samples,
115184
mutation = gnomeR::mutations,
@@ -121,11 +190,11 @@ test_that("other vars are retained", {
121190
set.seed(20230828)
122191

123192
bin_impact$random_color = sample(c("blue", "red", "yellow"),
124-
size = 50, replace = TRUE)
193+
size = length(samples), replace = TRUE)
125194

126195
expect_true("random_color" %in% names(bin_impact))
127-
sum_impact <- summarize_by_gene(bin_impact,
128-
other_vars = "random_color")
196+
sum_impact <- summarize_by_patient(bin_impact,
197+
other_vars = "random_color")
129198

130199
expect_true("random_color" %in% names(sum_impact))
131200
expect_true("blue" %in% sum_impact$random_color)
@@ -141,12 +210,5 @@ test_that("no warning message thrown when only 1 alt type", {
141210
include_silent = FALSE
142211
)
143212

144-
expect_no_warning(summarize_by_gene(bin.mut))
145-
146-
147-
213+
expect_no_warning(summarize_by_patient(bin.mut))
148214
})
149-
150-
151-
152-

0 commit comments

Comments
 (0)