@@ -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 )
0 commit comments