11
11
# fusion = gnomeR::fusion,
12
12
# cna = cna)
13
13
#
14
- # gen_dat2 <- gnomeR::summarize_by_gene (gen_dat)
14
+ # gen_dat2 <- gnomeR::summarize_by_patient (gen_dat)
15
15
#
16
16
# tp53_1 <- gen_dat %>% select(contains("TP53"))
17
17
# tp53_1 <- (tp53_1$TP53== 1 |tp53_1$TP53.Del== 1)%>% sum()
24
24
25
25
test_that(" test simplify marix needs a data frame" , {
26
26
27
- expect_error(summarize_by_gene (gene_binary = c(1 : 10 )))
27
+ expect_error(summarize_by_patient (gene_binary = c(1 : 10 )))
28
28
29
29
})
30
30
@@ -35,11 +35,49 @@ test_that("test simplify matrix needs a data frame", {
35
35
mutation = gnomeR :: mutations ,
36
36
fusion = gnomeR :: sv )
37
37
38
- expect_no_error(gen_dat2 <- gnomeR :: summarize_by_gene (gen_dat ))
38
+ expect_no_error(gen_dat2 <- gnomeR :: summarize_by_patient (gen_dat ))
39
39
40
40
41
41
})
42
42
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
+ })
43
81
44
82
test_that(" test that genes are properly summarized" , {
45
83
samples <- Reduce(intersect , list (gnomeR :: mutations $ sampleId , gnomeR :: cna $ sampleId ,
@@ -50,24 +88,49 @@ test_that("test that genes are properly summarized", {
50
88
mutation = gnomeR :: mutations ,
51
89
cna = gnomeR :: cna ,
52
90
fusion = gnomeR :: sv ,
53
- specify_panel = " impact" )%> %
91
+ specify_panel = " impact" ) %> %
54
92
dplyr :: select(c(sample_id , starts_with(" ARI" ), starts_with(" MAPK1" ), starts_with(" ERG" ))))
55
93
56
94
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 )
59
98
60
99
61
100
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 )) %> %
63
104
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 )
67
111
68
112
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
69
119
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_ )
70
124
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 )
71
134
})
72
135
73
136
@@ -85,17 +148,22 @@ test_that("test what happens to columns with all NA", {
85
148
select(c(sample_id , starts_with(" AR" ), starts_with(" PLCG2" ), starts_with(" PPM1D" )))
86
149
87
150
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 )
90
154
91
155
92
156
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 )) %> %
94
160
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 )
99
167
100
168
expect_equal(sum_impact , bin_impact_test )
101
169
@@ -106,10 +174,11 @@ test_that("test what happens to columns with all NA", {
106
174
107
175
108
176
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()
113
182
114
183
bin_impact <- create_gene_binary(samples = samples ,
115
184
mutation = gnomeR :: mutations ,
@@ -121,11 +190,11 @@ test_that("other vars are retained", {
121
190
set.seed(20230828 )
122
191
123
192
bin_impact $ random_color = sample(c(" blue" , " red" , " yellow" ),
124
- size = 50 , replace = TRUE )
193
+ size = length( samples ) , replace = TRUE )
125
194
126
195
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" )
129
198
130
199
expect_true(" random_color" %in% names(sum_impact ))
131
200
expect_true(" blue" %in% sum_impact $ random_color )
@@ -141,12 +210,5 @@ test_that("no warning message thrown when only 1 alt type", {
141
210
include_silent = FALSE
142
211
)
143
212
144
- expect_no_warning(summarize_by_gene(bin.mut ))
145
-
146
-
147
-
213
+ expect_no_warning(summarize_by_patient(bin.mut ))
148
214
})
149
-
150
-
151
-
152
-
0 commit comments