biosurf / cyCombine

Robust Integration of Single-Cell Cytometry Datasets
Other
24 stars 7 forks source link

Error in batch correction #51

Open huqianwen5399 opened 3 months ago

huqianwen5399 commented 3 months ago

Hello, I am currently using the cyCombine R package to analyze mass cytometry data. I am encountering some issues and would like to seek assistance. Thank you.

corrected <- uncorrected %>%

  • batch_correct(markers = markers,
  • norm_method = "scale", # "rank" is recommended when combining data with heavy batch effects
  • rlen = 10, # Consider a larger value, if results are not convincing (e.g. 100)
  • covar = "condition") Creating SOM grid.. Scaling expression data.. Batch correcting data.. Correcting Label group 1 Error: objects ‘rowSums’, ‘colSums’, ‘rowMeans’, ‘colMeans’ are not exported by 'namespace:BiocGenerics'

Additionally, I am not quite sure if the "condition" in the metadata refers to the grouping, such as WT and KO?

I am looking forward to your response.

shdam commented 3 months ago

Hi there,

Thank you for trying out cyCombine! - Sorry to hear you are encountering issues.

Which version of cyCombine are you using? And can you provide me with the traceback() for this error? The functions mentioned aren't explicitly used in cyCombine, so I am uncertain where the problem occurs exactly.

As you say, the covar should be the column name of the grouping.

Best regards, Søren

huqianwen5399 commented 3 months ago

Hello, Thank you very much for your response. The cyCombine R package version I am using is ‘0.2.19’

Here is the traceback() result:

`> # Directory containing .fcs files

data_dir <- "01_rawdata/"

Markers of interest

panel = read_excel("./02_metadata/pannel.xlsx")

markers <- panel %>%

  • filter(marker_class != "none") %>%
  • pull(antigen)

    Compile fcs files, down-sample, and preprocess

    uncorrected <- prepare_data(data_dir = data_dir,

  • markers = markers,
  • metadata = file.path("02_metadata/metadata_cycombine1.xlsx"), # Can also be .csv file or data.frame object
  • sample_ids = NULL,
  • batch_ids = "batch",
  • filename_col = "Filename",
  • condition = "condition",
  • down_sample = TRUE,
  • seed = 473,
  • cofactor = 5) Reading 18 files to a flowSet.. Down sampling to 5e+05 cells
    Extracting expression data.. Your flowset is now converted into a dataframe. Transforming data using asinh with a cofactor of 5.. Done!

    Run batch correction

    corrected <- uncorrected %>%

  • batch_correct(markers = markers,
  • norm_method = "scale", # "rank" is recommended when combining data with heavy batch effects
  • rlen = 10, # Consider a larger value, if results are not convincing (e.g. 100)
  • covar = "condition") Creating SOM grid.. Scaling expression data.. Batch correcting data.. Correcting Label group 1 Error: objects ‘rowSums’, ‘colSums’, ‘rowMeans’, ‘colMeans’ are not exported by 'namespace:BiocGenerics' traceback() 29: stop(sprintf(ngettext(length(miss), "object %s is not exported by 'namespace:%s'", "objects %s are not exported by 'namespace:%s'"), paste(sQuote(miss), collapse = ", "), getNamespaceName(expenv)), call. = FALSE, domain = NA) 28: importIntoEnv(impenv, impnames, ns, impvars) 27: namespaceImportFrom(ns, loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]), i[[2L]], from = package) 26: loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) 25: namespaceImport(ns, loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]), from = package) 24: loadNamespace(x) 23: combat(., batch = df$batch, sample = df$sample, mod_matrix = mod_matrix, parametric = parametric, ref.batch = ref.batch, ...) 22: tibble::as_tibble(.) 21: list2(...) 20: dplyr::bind_cols(., dplyr::select(df, -dplyr::all_of(c(markers, label)))) 19: dplyr::mutate(., dplyr::across(dplyr::all_of(markers), function(x) { min <- min(df[[dplyr::cur_column()]]) max <- max(df[[dplyr::cur_column()]]) x <- ifelse(x < min, min, x) x <- ifelse(x > max, max, x) return(x) })) 18: df %>% dplyr::select(dplyr::all_of(markers)) %>% combat(batch = df$batch, sample = df$sample, mod_matrix = mod_matrix, parametric = parametric, ref.batch = ref.batch, ...) %>% tibble::as_tibble() %>% dplyr::bind_cols(dplyr::select(df, -dplyr::all_of(c(markers, label)))) %>% dplyr::mutate(dplyr::across(dplyr::all_of(markers), function(x) { min <- min(df[[dplyr::cur_column()]]) max <- max(df[[dplyr::cur_column()]]) x <- ifelse(x < min, min, x) x <- ifelse(x > max, max, x) return(x) })) 17: .f(.x, .y, ...) 16: (function (.x, .y) { res <- .f(.x, .y, ...) if (!inherits(res, "data.frame")) { abort("The result of .f must be a data frame.", call = error_call) } if (any(bad <- names(res) %in% tbl_group_vars)) { msg <- glue("The returned data frame cannot contain the original grouping variables: {names}.", names = paste(names(res)[bad], collapse = ", ")) abort(msg, call = error_call) } bind_cols(.y[rep(1L, nrow(res)), , drop = FALSE], res) })(dots[[1L]][[1L]], dots[[2L]][[1L]]) 15: mapply(.f, .x, .y, MoreArgs = list(...), SIMPLIFY = FALSE) 14: map2(chunks, group_keys, .f, ...) 13: group_map.data.frame(.data, fun, .keep = .keep) 12: group_map(.data, fun, .keep = .keep) 11: group_modify.grouped_df(., .keep = TRUE, function(df, ...) { num_covar <- 1 num_anchor <- 1 num_batches <- df$batch %>% factor() %>% nlevels() lab <- df[[label]][1] if (num_batches == 1) { batch <- df$batch[1] message(paste("Label group", lab, "only contains cells from batch", batch)) df <- df %>% dplyr::select(-label) return(df) } message(paste("Correcting Label group", lab)) if (!is.null(covar)) { if (!cyCombine:::check_confound(df$batch, stats::model.matrix(~df[[covar]]))) { num_covar <- df[[covar]] %>% factor() %>% nlevels() covar_counts <- df %>% dplyr::count(.data[[covar]]) %>% dplyr::pull(n) if (sum(covar_counts) < max(covar_counts) + num_covar 5) { ... 10: dplyr::group_modify(., .keep = TRUE, function(df, ...) { num_covar <- 1 num_anchor <- 1 num_batches <- df$batch %>% factor() %>% nlevels() lab <- df[[label]][1] if (num_batches == 1) { batch <- df$batch[1] message(paste("Label group", lab, "only contains cells from batch", batch)) df <- df %>% dplyr::select(-label) return(df) } message(paste("Correcting Label group", lab)) if (!is.null(covar)) { if (!cyCombine:::check_confound(df$batch, stats::model.matrix(~df[[covar]]))) { num_covar <- df[[covar]] %>% factor() %>% nlevels() covar_counts <- df %>% dplyr::count(.data[[covar]]) %>% dplyr::pull(n) if (sum(covar_counts) < max(covar_counts) + num_covar 5) { ... 9: dplyr::ungroup(.) 8: dplyr::arrange(., id) 7: dplyr::select(., id, dplyr::everything()) 6: dplyr::mutate(., batch = as.factor(batch)) 5: df %>% dplyr::group_by(.data[[label]]) %>% dplyr::group_modify(.keep = TRUE, function(df, ...) { num_covar <- 1 num_anchor <- 1 num_batches <- df$batch %>% factor() %>% nlevels() lab <- df[[label]][1] if (num_batches == 1) { batch <- df$batch[1] message(paste("Label group", lab, "only contains cells from batch", batch)) df <- df %>% dplyr::select(-label) return(df) } message(paste("Correcting Label group", lab)) if (!is.null(covar)) { if (!cyCombine:::check_confound(df$batch, stats::model.matrix(~df[[covar]]))) { num_covar <- df[[covar]] %>% factor() %>% nlevels() covar_counts <- df %>% dplyr::count(.data[[covar]]) %>% dplyr::pull(n) if (sum(covar_counts) < max(covar_counts) + num_covar * ... 4: cyCombine::correct_data(., label = label, covar = covar, anchor = anchor, markers = markers, parametric = parametric, method = method, ref.batch = ref.batch, ...) 3: df %>% cyCombine::correct_data(label = label, covar = covar, anchor = anchor, markers = markers, parametric = parametric, method = method, ref.batch = ref.batch, ...) 2: batch_correct(., markers = markers, norm_method = "scale", rlen = 10, covar = "condition") 1: uncorrected %>% batch_correct(markers = markers, norm_method = "scale", rlen = 10, covar = "condition")`
shdam commented 3 months ago

Hmm, it is a quite peculiar error. Try reinstalling BiocGenerics and Matrix, restart R, and rerun the correction.

shdam commented 3 months ago

Hi there,

Did you manage to solve the issue? I am curious what caused this odd error.

Best regards, Søren