How to read mutiple FCS files using the purrr instead of loop function

Hi
is it possible to read several FCS files using the purrr package instead of making a loop function like so:

for (f in files) {
  name= str_split(f, "_")[[1]][2]
  data_FC_sample <- flowCore::exprs(flowCore::read.FCS(filename = paste0("data/_raw/CD8_gated_files/",f), 
                                                       transformation = FALSE, 
                                                       truncate_max_range = FALSE))
  data_FC_sample <- data_FC_sample %>% 
    as.tibble() %>% 
    mutate(name = name)
  
  data_FC  <- data_FC %>% 
    bind_rows(data_FC_sample)
}

when using the map function I get different errors

covid_files = list.files(path = "data/raw/CD8_gated_files/")
covid_data = map(covid_files, read.FCS)

Error:
Error in .f(.x[[i]], ...) : 'export_AP0201_CD8+.fcs' is not a valid file

or

covid_data = map(read.FCS(covid_files, transformation = FALSE,
                            truncate_max_range = FALSE))

Error:
Error in as_mapper(.f, ...) : argument ".f" is missing, with no default

When running the read.FCS (from the flowCore package) on single files is saved as a "flowFrame"

I can't test this but the code using purrr should be something like this

covid_files <- list.files(path = "data/raw/CD8_gated_files/",
                          pattern = "\\.fcs$",
                          full.names = TRUE)

covid_data <- covid_files %>% 
    set_names() %>% 
    map_dfr(.f = ~ read.FCS(filename = .x,
                            transformation = FALSE, 
                            truncate_max_range = FALSE) %>% 
                as.tibble(),
            .id = "name"
    )

Yes, great! it worked! thanks!

I just had to add a "exprs()" after read.FCS to get it as a matrix instead of a flowFrame.

covid_data <- covid_files %>%
  set_names() %>% 
  map_dfr(.f = ~read.FCS(filename = .x,
                         transformation = FALSE,
                         truncate_max_range = FALSE) %>% 
            exprs() %>% 
            as_tibble(), 
          .id = "name"
)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.