Skip to content

Commit

Permalink
Merge pull request #127 from gdrplatform/GDR-2635.2
Browse files Browse the repository at this point in the history
refactor: update as per review
  • Loading branch information
bczech authored Aug 7, 2024
2 parents cdb98a0 + c4ea2f2 commit 79ba8c0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
38 changes: 22 additions & 16 deletions R/standardize_MAE.R
Original file line number Diff line number Diff line change
Expand Up @@ -295,19 +295,19 @@ refine_rowdata <- function(rd, se, default_v = "Undefined") {
#' @export
#' @keywords standardize_MAE
set_unique_cl_names <- function(se) {
checkmate::assertClass(se, "SummarizedExperiment")
checkmate::assert_class(se, "SummarizedExperiment")

col_data <- SummarizedExperiment::colData(se)
cl_name_idf <- get_env_identifiers("cellline_name")
clid_idf <- get_env_identifiers("cellline")
cellline_name <- get_env_identifiers("cellline_name")
clid <- get_env_identifiers("cellline")

if (!is.null(col_data[[cl_name_idf]])) {
duplicated_ids <- col_data[[cl_name_idf]][duplicated(col_data[[cl_name_idf]])]
if (!is.null(col_data[[cellline_name]])) {
duplicated_ids <- col_data[[cellline_name]][duplicated(col_data[[cellline_name]])]
if (length(duplicated_ids) > 0) {
for (dup_id in unique(duplicated_ids)) {
dup_indices <- which(col_data[[cl_name_idf]] == dup_id)
col_data[[cl_name_idf]][dup_indices] <- paste0(col_data[[cl_name_idf]][dup_indices],
" (", col_data[[clid_idf]][dup_indices], ")")
dup_indices <- which(col_data[[cellline_name]] == dup_id)
col_data[[cellline_name]][dup_indices] <- paste0(col_data[[cellline_name]][dup_indices],
" (", col_data[[clid]][dup_indices], ")")
}
SummarizedExperiment::colData(se) <- col_data
}
Expand All @@ -334,28 +334,34 @@ set_unique_cl_names <- function(se) {
#' @export
#' @keywords standardize_MAE
set_unique_drug_names <- function(se) {
checkmate::assertClass(se, "SummarizedExperiment")
checkmate::assert_class(se, "SummarizedExperiment")

row_data <- SummarizedExperiment::rowData(se)
drug_columns <- unlist(get_env_identifiers(c("drug_name", "drug_name2", "drug_name3"), simplify = FALSE))
gnumber_columns <- unlist(get_env_identifiers(c("drug", "drug2", "drug3"), simplify = FALSE))
drug_columns <- intersect(unlist(get_env_identifiers(c("drug_name", "drug_name2", "drug_name3"), simplify = FALSE)),
names(row_data))
gnumber_columns <- intersect(unlist(get_env_identifiers(c("drug", "drug2", "drug3"), simplify = FALSE)),
names(row_data))

for (i in seq_along(drug_columns)) {
drug_col <- drug_columns[i]
gnumber_col <- gnumber_columns[i]

if (!is.null(row_data[[drug_col]])) {
# Find duplicated drug names
duplicated_drugs <- row_data[[drug_col]][duplicated(row_data[[drug_col]])]
if (length(duplicated_drugs) > 0) {
for (dup_drug in unique(duplicated_drugs)) {
dup_indices <- which(row_data[[drug_col]] == dup_drug)
unique_drugs <- unique(duplicated_drugs)

for (dup_drug in unique_drugs) {
dup_indices <- which(row_data[[drug_col]] == dup_drug)
if (length(unique(row_data[[gnumber_col]][dup_indices])) > 1) {
row_data[[drug_col]][dup_indices] <- paste0(row_data[[drug_col]][dup_indices],
" (", row_data[[gnumber_col]][dup_indices], ")")
}
SummarizedExperiment::rowData(se) <- row_data
}
}
}

SummarizedExperiment::rowData(se) <- row_data
return(se)
}

Expand Down Expand Up @@ -386,7 +392,7 @@ set_unique_drug_names <- function(se) {
#' @export
#' @keywords standardize_MAE
set_unique_identifiers <- function(mae) {
checkmate::assertClass(mae, "MultiAssayExperiment")
checkmate::assert_class(mae, "MultiAssayExperiment")

for (name in names(MultiAssayExperiment::experiments(mae))) {
se <- MultiAssayExperiment::experiments(mae)[[name]]
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test-standardize_MAE.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ test_that("set_unique_drug_names works correctly", {
se <- set_unique_drug_names(se)

expect_equal(SummarizedExperiment::rowData(se)$DrugName, c("DrugA (G1)", "DrugA (G2)"))

se2 <- SummarizedExperiment::SummarizedExperiment(
assays = list(counts = matrix(1:9, ncol = 3)),
rowData = S4Vectors::DataFrame(DrugName = c("DrugA", "DrugA", "DrugB"),
Gnumber = c("G1", "G2", "G5"),
DrugName_2 = c("DrugC", "DrugC", "DrugD"),
Gnumber_2 = c("G3", "G3", "G5")
))

se2 <- set_unique_drug_names(se2)
expect_equal(SummarizedExperiment::rowData(se2)$DrugName, c("DrugA (G1)", "DrugA (G2)", "DrugB"))
expect_equal(SummarizedExperiment::rowData(se2)$DrugName_2, c("DrugC", "DrugC", "DrugD"))
})

test_that("set_unique_identifiers works correctly", {
Expand Down

0 comments on commit 79ba8c0

Please sign in to comment.