Mapping function over list returns 'NULL' for all list elements in R

Viewed 16

I am trying to apply a loop over a list of files (called filelist) that contain "barcodes" (33 nucleotide stretches of sequence). The goal is to subsample, with replacement, the barcodes in 10000 barcode increments and generate a dataframe for each element that contains all subsample sizes and the number of unique barcodes in that subsample. Ideally I would like my output to be a list full of data frames that look like this:

       0     0
     10000   623
     20000   986
     30000  1280
     40000  1522
     50000  1790
     60000  1988
     70000  2214
     80000  2437
     90000  2647
    100000  2770
    110000  3002
    120000  3124

Filelist contains four of these elements:

$cxt_mg_12dpi_4_S65
# A tibble: 1,253,512 × 1
   barcode                          
   <chr>                            
 1 CTTACGGTCACGGTAACTGCAGCTACCCTTCTA
 2 CTTACCGTCACCGTAACCGCAGCCACTCTACTC
 3 CTTACGGTCACGGTAACTGCAGCTACCCTTCTA
 4 CTAACAGTCACGGTCACCGCCGCAACGCTTCTT
 5 CTTACAGTTACCGTTACAGCAGCCACACTCCTG
 6 CTTACGGTCACGGTAACTGCAGCTACCCTTCTA
 7 CTAACAGTCACGGTCACCGCCGCAACGCTTCTT
 8 CTTACCGTCACCGTAACCGCAGCCACTCTACTC
 9 CTTACGGTCACGGTAATTGCAGCTACCCTTCTA
10 CTAACAGTCACGGTCACCGCCGCAACGCTTCTT
# … with 1,253,502 more rows

Each element has a distinct name, but all elements contain one column named barcode. I am trying to map the following function over filelist:

bootstrap_rarefaction <- function (x) { 
  
  dataset_size <- nrow(x)
  subsample_offset <- 10000
  subsample_sizes <- seq(from = 0, to = dataset_size, by = subsample_offset)
  
  all_data <- data.frame(subsample_size = integer(),
                         richess = integer())
  
  for (subsample_size in subsample_sizes) {
    subsample <- slice_sample(x, n = subsample_size, replace = T)
    richness <- nrow(subsample %>% 
                       select(barcode) %>% 
                       distinct())
    output <- c(subsample_size, richness)
    all_data <- rbind.data.frame(all_data, output)
    all_data
  }
}

like so:

map(filelist, bootstrap_rarefaction)

But it returns this:

$cxt_mg_12dpi_4_S65
NULL

$cxt_sg_12dpi_1_S72
NULL

$cxt_sg_12dpi_10_S81
NULL

$cxt_sg_12dpi_2_S73
NULL

The function works when I run it on one element of the list separately, though it doesn't print all_data, I have to manually go back and print it to see the output. I have tried using lmap instead of map but this produces an error associated with the seq() command in the loop. If I try to run the function on one element of the list separately like so:

temp <- bootstrap_rarefaction(test)

It just says NULL when I print temp. I have seen some questions and answers that address filtering out any 'NULL' output but none of the elements in this list should be returning 'NULL'. If any element were to contain fewer than 10,000 barcodes it should return a dataframe that looks like this:

0    0

I'm not sure of where to go from here and any advice would be greatly appreciated. This is my first time posting a question here so I apologize if I left out anything important.

0 Answers
Related