neurogenomics / EpiCompare

Comparison, benchmarking & QC of epigenetic datasets
https://doi.org/doi:10.18129/B9.bioc.EpiCompare
13 stars 3 forks source link

Simplify loops #67

Closed bschilder closed 2 years ago

bschilder commented 2 years ago

You can simplify looping over named lists with mapply. This can speed up your code, reduce the number of variables in your namespace (thus conserving memory), and makes it a bit easier to read.

For example:

Before

 ref_in_sample_list <- list()
    # obtain overlapping peaks 
    for(file in peaklist_tidy){
      ref_in_sample <- IRanges::subsetByOverlaps(x = reference_tidy[[1]], 
                                                 ranges = file)
      ref_in_sample_list <- c(ref_in_sample_list, ref_in_sample)
    }
    names(ref_in_sample_list) <- names(peaklist_tidy)

After

  ref_in_sample_list <- mapply(peaklist_tidy, FUN = function(file){
          IRanges::subsetByOverlaps(x = reference_tidy[[1]], 
                                    ranges = file)
      }) 
serachoi1230 commented 2 years ago

This was actually in my list of things to do, I'll work on it now! Thank you!!

bschilder commented 2 years ago

I got things started, so let me make my pushes first. Then you can pull down my changes and so what's left to do