Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: factor variables auto converted to characters #95

Merged
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
12 changes: 11 additions & 1 deletion R/oncoplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,14 @@ ggoncoplot <- function(.data,
}
if(!is.null(col_mutation_type)){
assertions::assert_string(col_mutation_type)
# Assert mutation type columne sensible
# Assert mutation type is a valid column name
assertions::assert_names_include(.data, col_mutation_type)

# If column type is a factor, convert to character
if(is.factor(.data[[col_mutation_type]])) .data[[col_mutation_type]] <- as.character(.data[[col_mutation_type]])

# Assert column type = character
#assertions::assert_character(.data[[col_mutation_type]])
assertions::assert_no_missing(.data[[col_mutation_type]], arg_name = paste0("Mutation Type Column: ", col_mutation_type))
assertions::assert_excludes(.data[[col_mutation_type]], illegal = "", msg = "{.strong Mutation Type} column cannot contain zero-length strings")
}
Expand All @@ -153,10 +159,14 @@ ggoncoplot <- function(.data,
}

#Assert sample column sensible
if(is.factor(.data[[col_samples]])) .data[[col_samples]] <- as.character(.data[[col_samples]])
assertions::assert_character(.data[[col_samples]])
assertions::assert_no_missing(.data[[col_samples]])
assertions::assert_excludes(.data[[col_samples]], illegal = "", msg = "{.strong Sample} column cannot contain zero-length strings") # Asserts no empty string

#Assert gene column sensible
if(is.factor(.data[[col_genes]])) .data[[col_genes]] <- as.character(.data[[col_genes]])
assertions::assert_character(.data[[col_genes]])
assertions::assert_no_missing(.data[[col_genes]])
assertions::assert_excludes(.data[[col_genes]], illegal = "", msg = "{.strong Gene} column cannot contain zero-length strings") # Asserts no empty string

Expand Down
27 changes: 25 additions & 2 deletions tests/testthat/test-ggoncoplot_simulated_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,37 @@ test_that("ggoncoplot works with pathway mapping", {

#NFE2L2 & EMPTY should both be in final plot

gg <- suppressMessages(ggoncoplot(
expect_error(
gg <- suppressMessages(ggoncoplot(
df_mutations,
col_genes = "Genes",
col_samples = "Samples",
col_mutation_type = "VariantType", interactive = TRUE, topn = Inf,
pathway = df_pathway
))
)), NA
)
})


test_that("ggoncoplot works with factor-type columns", {

# Create a version of the data
df_mutations_factor <- df_mutations
df_mutations_factor[["Genes"]] <- as.factor(df_mutations[["Genes"]])
df_mutations_factor[["Samples"]] <- as.factor(df_mutations[["Samples"]])
df_mutations_factor[["VariantType"]] <- as.factor(df_mutations[["VariantType"]])

expect_error(
suppressMessages(ggoncoplot(
df_mutations_factor,
col_genes = "Genes",
col_samples = "Samples",
col_mutation_type = "VariantType"
)),
NA
)
})




4 changes: 1 addition & 3 deletions vignettes/quick_start.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -364,14 +364,12 @@ gbm_df |>
ggoncoplot outputs can be combined with other packages. Simply set interactive = FALSE so ggoncoplot returns a ggplot object instead of a ggiraph. Do the same for express. Then you can con

```{r}


## Create a Glioblastoma Oncoplot
ggoncoplot <- gbm_df |>
ggoncoplot(
col_genes = 'Hugo_Symbol',
col_samples = 'Tumor_Sample_Barcode',
col_mutation_type = 'Variant_Classification',
col_mutation_type = 'Variant_Classification',
interactive = FALSE
)

Expand Down
Loading