Skip to content

Commit ecc0c36

Browse files
committed
refactor sample size check
1 parent 4f02134 commit ecc0c36

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

R/plot-pgs.R

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,29 +183,34 @@ create.pgs.density.plot <- function(
183183
pgs.by.phenotype <- lapply(
184184
X = pgs.by.phenotype,
185185
FUN = function(x) {
186-
x <- x[sapply(x, length) > 1];
186+
large.categories <- sapply(
187+
X = x,
188+
function(y) {
189+
length(y[!is.na(y)]) > 1
190+
}
191+
);
192+
x <- x[large.categories];
187193
}
188194
);
189195

190196
# iterate over phenotype variables
191197
for (phenotype in names(pgs.by.phenotype)) {
192198
pgs.data.for.plotting <- pgs.by.phenotype[[phenotype]];
193199

194-
# count non-NA values per phenotype category
195-
n.samples.per.category <- sapply(pgs.data.for.plotting, function(x) {length(x[!is.na(x)])});
196-
if (any(n.samples.per.category < 5)) {
197-
# remove phenotype categories containing fewer than 5 samples
198-
pgs.data.for.plotting <- pgs.data.for.plotting[n.samples.per.category >= 5];
200+
# handle case where all categories have fewer than 2 samples
201+
if (length(pgs.data.for.plotting) == 0) {
199202
# issue a warning
200-
warning(paste0(names(n.samples.per.category)[n.samples.per.category < 5], ' category has fewer than 5 samples, density curves will not be plotted'));
203+
warning(paste0('No ', phenotype, ' categories with more than 2 samples, plotting aggregated density instead'));
204+
pgs.density.by.phenotype.plots[[paste0(pgs.column,'_',phenotype)]] <- pgs.density.plots[[pgs.column]];
205+
next;
201206
}
202207

203208
# color handling
204209
max.colors <- 12;
205210
max.lty <- 6;
206211
max.categories <- max.colors * max.lty;
207212
if (length(pgs.data.for.plotting) > max.categories) {
208-
# Issue a warning that plot is not bein color-coded
213+
# Issue a warning that plot is not being color-coded
209214
warning(paste0('Skipping colors for ', pgs.column, ' and ', phenotype, ' due to too many categories'));
210215
# plot all lines in black
211216
group.xaxis.formatting <- BoutrosLab.plotting.general::auto.axis(

tests/testthat/test-plotting.R

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,12 @@ test_that(
190190
create.pgs.density.plot(
191191
pgs.data = pgs.test,
192192
phenotype.columns = c('continuous.phenotype', 'binary.phenotype', 'categorical.phenotype'),
193-
output.dir = temp.dir,
193+
output.dir = '.',#temp.dir,
194194
filename.prefix = 'TEST-all-phenotypes'
195195
)
196196
);
197197

198-
# check handling of only continuous phenotype (not supposed to be plotted, how can I test for multipanel dimensions over a multipanel object?)
198+
# check handling of only continuous phenotype (not supposed to be plotted
199199
expect_no_error(
200200
create.pgs.density.plot(
201201
pgs.data = pgs.test,
@@ -223,6 +223,19 @@ test_that(
223223
file.exists(file.path(temp.dir, test.filename.continuous.phenotype))
224224
);
225225

226+
227+
# check handling of categorical phenotype with fewer than 2 samples in all categories
228+
low.n.test.data <- pgs.test;
229+
low.n.test.data$categorical.phenotype <- paste0('cat', 1:nrow(low.n.test.data));
230+
expect_warning(
231+
create.pgs.density.plot(
232+
pgs.data = low.n.test.data,
233+
phenotype.columns = c('categorical.phenotype'),
234+
output.dir = temp.dir,
235+
filename.prefix = 'TEST-all-small-categories'
236+
)
237+
);
238+
226239
}
227240
);
228241

0 commit comments

Comments
 (0)