Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
.RData
.Ruserdata
.Rproj.user
.lintr

vignettes/figures
*.pdf
*.html
*.html

*.code-workspace
.vscode
10 changes: 8 additions & 2 deletions R/create.clone.genome.distribution.plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ create.clone.genome.distribution.plot <- function(
) {

# Preprocess ----------------------------------------------------------------------------------
if (!all(c('chr', 'pos', 'clone.id') %in% names(snv.df))) {
stop('snv.df does not contain at least one of chr, pos or clone.id columns')
required.cols <- c('chr', 'pos', 'clone.id');
missing.cols <- required.cols[!(required.cols %in% names(snv.df))];
if (length(missing.cols) != 0) {
stop(paste0(
'snv.df must contain the columns ',
oxford.comma.vector.concat(required.cols),
'; snv.df is missing ',
oxford.comma.vector.concat(missing.cols, paste(required.cols, collapse = ', ')), '.'));
}
if (is.null(clone.order)) {
clone.order <- sort(unique(snv.df$clone.id));
Expand Down
10 changes: 8 additions & 2 deletions R/data.frame.to.array.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ data.frame.to.array <- function(
y.axis = 'ID'
) {

if (is.null(DF[[x.axis]]) | is.null(DF[[value]]) | is.null(DF[[y.axis]])) {
stop(paste('Dataframe does not contain one of the columns:', value, x.axis, y.axis));
required.cols <- c(value, x.axis, y.axis);
missing.cols <- required.cols[!(required.cols %in% names(DF))];
if (length(missing.cols) != 0) {
stop(paste0(
'Dataframe must contain the columns: ',
oxford.comma.vector.concat(required.cols),
'; Dataframe is missing ',
oxford.comma.vector.concat(missing.cols, paste(required.cols, collapse = ', ')), '.'));
}
arr <- reshape(
data = DF[, c(x.axis, y.axis, value)],
Expand Down
16 changes: 16 additions & 0 deletions R/utility.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,19 @@ get.encoded.distance <- function(points) {

return(encoded.distances);
}


oxford.comma.vector.concat <- function(vec, empty.value = '', flatten.empty.value = TRUE) {
if (length(vec) == 0) {
if (flatten.empty.value) {
oxford.comma.vector.concat(empty.value, flatten.empty.value = FALSE)
} else {
empty.value;
}
} else if (length(vec) == 1) {
paste(vec, collapse = ', ');
} else {
sep <- if (length(vec) > 2) ', and ' else ' and ';
paste(paste(head(vec, -1), collapse = ', '), tail(vec, 1), sep = sep);
}
}
22 changes: 22 additions & 0 deletions tests/testthat/test-create.clone.genome.distribution.plot.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
test_that('create.clone.genome.distribution.plot enforces data.frame columns', {
snv.df <- data.frame();
expect_error(
create.clone.genome.distribution.plot(snv.df),
'must contain the columns chr, pos, and clone.id; snv.df is missing chr, pos, and clone.id.'
);
snv.df <- data.frame(chr = 1);
expect_error(
create.clone.genome.distribution.plot(snv.df),
'must contain the columns chr, pos, and clone.id; snv.df is missing pos and clone.id.'
);
snv.df <- data.frame(chr = 1, pos = 1);
expect_error(
create.clone.genome.distribution.plot(snv.df),
'must contain the columns chr, pos, and clone.id; snv.df is missing clone.id.'
);
snv.df <- data.frame(clone.id = 1);
expect_error(
create.clone.genome.distribution.plot(snv.df),
'must contain the columns chr, pos, and clone.id; snv.df is missing chr and pos.'
);
});
22 changes: 22 additions & 0 deletions tests/testthat/test-data.frame.to.array.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
test_that('data.frame.to.array enforces data.frame columns', {
DF <- data.frame();
expect_error(
data.frame.to.array(DF),
'Dataframe must contain the columns: CCF, SNV.id, and ID; Dataframe is missing CCF, SNV.id, and ID.'
);
DF <- data.frame(CCF = 1);
expect_error(
data.frame.to.array(DF),
'Dataframe must contain the columns: CCF, SNV.id, and ID; Dataframe is missing SNV.id and ID.'
);
DF <- data.frame(CCF = 1, SNV.id = 1);
expect_error(
data.frame.to.array(DF),
'Dataframe must contain the columns: CCF, SNV.id, and ID; Dataframe is missing ID.'
);
DF <- data.frame(SNV.id = 1);
expect_error(
data.frame.to.array(DF),
'Dataframe must contain the columns: CCF, SNV.id, and ID; Dataframe is missing CCF and ID.'
);
});
64 changes: 64 additions & 0 deletions tests/testthat/test-utility.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,67 @@ test_that(

expect_equal(order(result), expected.order);
});

test_that(
'oxford.comma.vector.concat returns `empty.value` when input is empty', {
vec <- NULL;
expect_equal(oxford.comma.vector.concat(vec), '');
expect_equal(oxford.comma.vector.concat(vec, 'default'), 'default');
vec <- c(NULL);
expect_equal(oxford.comma.vector.concat(vec), '');
expect_equal(oxford.comma.vector.concat(vec, 'default'), 'default');
vec <- c(NULL, NULL);
expect_equal(oxford.comma.vector.concat(vec), '');
expect_equal(oxford.comma.vector.concat(vec, 'default'), 'default');
vec <- c();
expect_equal(oxford.comma.vector.concat(vec), '');
expect_equal(oxford.comma.vector.concat(vec, 'default'), 'default');
});

test_that(
'oxford.comma.vector.concat returns `empty.value` in correct format when input is empty', {
vec <- NULL;
expect_equal(oxford.comma.vector.concat(vec, flatten.empty.value = FALSE), '');
expect_equal(oxford.comma.vector.concat(vec, 'default', flatten.empty.value = FALSE), 'default');
vec <- c(NULL);
expect_equal(oxford.comma.vector.concat(vec, flatten.empty.value = FALSE), '');
expect_equal(oxford.comma.vector.concat(vec, 'default', flatten.empty.value = FALSE), 'default');
vec <- c(NULL, NULL);
expect_equal(oxford.comma.vector.concat(vec, flatten.empty.value = FALSE), '');
expect_equal(oxford.comma.vector.concat(vec, 'default', flatten.empty.value = FALSE), 'default');
vec <- c();
expect_equal(oxford.comma.vector.concat(vec, flatten.empty.value = FALSE), '');
expect_equal(oxford.comma.vector.concat(vec, 'default', flatten.empty.value = FALSE), 'default');

vec <- c();
vec.default <- NULL;
expect_null(oxford.comma.vector.concat(vec, vec.default, flatten.empty.value = FALSE));
vec <- c();
vec.default <- c(1, 2, 3);
expect_equal(oxford.comma.vector.concat(vec, vec.default), '1, 2, and 3');
expect_equal(oxford.comma.vector.concat(vec, vec.default, flatten.empty.value = FALSE), c(1, 2, 3));
});

test_that(
'oxford.comma.vector.concat returns grammatically correct oxford comma', {
vec <- c(1);
expect_equal(oxford.comma.vector.concat(vec), '1');
vec <- c(1, 2);
expect_equal(oxford.comma.vector.concat(vec), '1 and 2');
vec <- c(1, 2, 3);
expect_equal(oxford.comma.vector.concat(vec), '1, 2, and 3');
vec <- c(1, 2, 3, 4);
expect_equal(oxford.comma.vector.concat(vec), '1, 2, 3, and 4');
});

test_that(
'oxford.comma.vector.concat returns grammatically correct oxford comma for flattened default value', {
vec.default <- c(1);
expect_equal(oxford.comma.vector.concat(NULL, vec.default), '1');
vec.default <- c(1, 2);
expect_equal(oxford.comma.vector.concat(NULL, vec.default), '1 and 2');
vec.default <- c(1, 2, 3);
expect_equal(oxford.comma.vector.concat(NULL, vec.default), '1, 2, and 3');
vec.default <- c(1, 2, 3, 4);
expect_equal(oxford.comma.vector.concat(NULL, vec.default), '1, 2, 3, and 4');
});