Skip to content

Commit 9fd5183

Browse files
committed
Change function to add host col to metadata and formula and integrate with run_mtxDE
1 parent 4c9eb56 commit 9fd5183

File tree

2 files changed

+52
-44
lines changed

2 files changed

+52
-44
lines changed

R/mtxDE.R

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ add_perc_host_metadata <- function(metadata, sampleID,
392392
}
393393
}
394394
# select only the sampleID and host column in report
395-
report <- report %>% dplyr::select(sampleID, host_col)
395+
report <- report %>% dplyr::select(all_of(sampleID), all_of(host_col))
396396

397397
# left join percent host based on sampleID to metadata file
398398
new_metadata <- dplyr::left_join(metadata, report, by = sampleID)
@@ -633,32 +633,34 @@ filter_tables_by_shared_columns <- function(table1, table2, table1_name,
633633

634634
#' Add percent host column to formula
635635
#'
636-
#' @param data A data frame containing the merged feature table and metadata,
636+
#' @param metadata A data frame containing the merged feature table and metadata,
637637
#' ready for regression.
638+
#' @param formula A string representing the formula for the regression model.
638639
#' @param host_col A string representing the name of the percent host column
639640
#' within the `data` data.frame.
640-
#' @param formula A string representing the formula for the regression model.
641-
#' @param fixed.vars A vector of strings representing the fixed effect
642-
#' variables to include in the regression.
643-
#' @param reg.method A string indicating the regression method to be used.
644-
#' Options include "zibr", "gamlss", "lm", and "lmer".
645-
#'
646-
#'
647-
#' @returns list of updated `formula` and `fixed.vars` with the matching dna
648-
#' column added to them
641+
#' @param report A dataframe that contains the percent host
642+
#' column `host_col` and the `sampleID` column.
643+
#' @param sampleID A string representing the column name in `metadata`
644+
#' that contains the sample IDs.
645+
#' @returns list of updated `formula` and `fixed.vars` with the
646+
#' `host_col`added to them
649647
#'
650648
#' @keywords internal
651-
.add_host_to_formula <- function(data, host_col, formula=NULL, fixed.vars=NULL,
652-
reg.method) {
653-
if (!(host_col %in% colnames(data))) {
654-
stop("The given percent host column not found in data: ", host_col)
655-
}
656-
if (reg.method == "zibr") {
657-
fixed.vars <- c(fixed.vars, host_col)
658-
} else {
659-
formula <- paste0(formula, " + ", host_col)
649+
.add_host_to_formula_metadata <- function(metadata, formula, sampleID,
650+
report=NULL, host_col=NULL) {
651+
if (!is.null(host_col)) {
652+
if (host_col %in% all.vars(as.formula(formula)) == FALSE) {
653+
formula <- paste0(formula, " + ", host_col)
654+
}
655+
if (!is.null(report)) {
656+
if ((host_col %in% colnames(metadata)) == FALSE) {
657+
# add host_col to metadata, assuming sampleID col in report
658+
metadata <- add_perc_host_metadata(metadata, sampleID,
659+
report, host_col)
660+
}
661+
}
660662
}
661-
return(list(formula = formula, fixed.vars = fixed.vars))
663+
return(list(formula = formula, metadata = metadata))
662664
}
663665

664666

@@ -928,7 +930,9 @@ run_mtxDE <- function(formula, feature.table, metadata, sampleID,
928930
zibr_time_ind=NULL,
929931
ncores=1,
930932
show_progress=TRUE,
931-
dna.table=NULL){
933+
dna.table=NULL,
934+
host_col=NULL,
935+
report=NULL){
932936
if(reg.method %in% c("zibr", "gamlss")){
933937
check_for_ones(feature.table) # Beta regression can't handle ones
934938
feature.table <- filter_undetected(feature.table) # or undetected feats
@@ -938,6 +942,12 @@ run_mtxDE <- function(formula, feature.table, metadata, sampleID,
938942
}
939943
}
940944
formula <- paste0(" ~ ", formula)
945+
if (!is.null(host_col)) {
946+
add_host <- .add_host_to_formula_metadata(metadata, formula,
947+
sampleID, report, host_col)
948+
metadata <- add_host$metadata
949+
formula <- add_host$formula
950+
}
941951
data <- .prepare_data_mtxDE(feature.table, metadata,
942952
formula, sampleID, zibr_time_ind,
943953
dna.table = dna.table)

tests/testthat/test-mtxDE.R

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -234,32 +234,30 @@ test_that(".add_dna_to_formula correctly adds dna col", {
234234

235235
})
236236

237-
test_that(".add_host_to_formula works", {
238-
data <-data.frame(
239-
Row.names=paste0("sample_", seq_len(4)),
240-
a=c(0.1, 0.0, 0.0, 0.0),
241-
b=c(0.5, 0.5, 0.5, 0.4),
242-
phenotype=c(0,0,1,1),
243-
a_mgx=c(0.1, 0.0, 0.0, 0.0),
244-
b_mgx=c(0.5, 0.5, 0.5, 0.4),
245-
percent_host=c(0.5, 0.6, 0.8, 0.9)
246-
)
237+
test_that(".add_host_to_formula_metadata works", {
238+
metadata <- data.frame(SampleID=paste0("sample_", seq_len(4)),
239+
phenotype=c(0,0,1,1),
240+
participant=c(0,1,0,1),
241+
timepoint=c(0,0,1,1))
242+
report <- data.frame(SampleID=paste0("sample_", seq_len(4)),
243+
percent_host=c(0.9, 0.8, 0.5, 0.9))
244+
expected_metadata <- data.frame(SampleID=paste0("sample_", seq_len(4)),
245+
phenotype=c(0,0,1,1),
246+
participant=c(0,1,0,1),
247+
timepoint=c(0,0,1,1),
248+
percent_host=c(0.9, 0.8, 0.5, 0.9))
249+
247250
formula <- " ~ phenotype"
248251
expected_formula <- " ~ phenotype + percent_host"
249-
expected_fixed.vars <- c("phenotype", "percent_host")
250252

251-
result <- .add_host_to_formula(data = data,
252-
host_col = "percent_host",
253+
result <- .add_host_to_formula_metadata(metadata = metadata,
253254
formula = formula,
254-
fixed.vars = c("phenotype"),
255-
reg.method = "zibr")
256-
result2 <- .add_host_to_formula(data = data,
257-
host_col = "percent_host",
258-
formula = formula,
259-
fixed.vars = c("phenotype"),
260-
reg.method = "lm")
261-
expect_equal(result$fixed.vars, expected_fixed.vars)
262-
expect_equal(result2$formula, expected_formula)
255+
sampleID = "SampleID",
256+
report = report,
257+
host_col = "percent_host")
258+
259+
expect_equal(result$metadata, expected_metadata)
260+
expect_equal(result$formula, expected_formula)
263261
})
264262

265263
test_that("run_mtxDE works", {

0 commit comments

Comments
 (0)