Skip to contents

This function will read in Bismark coverage files and optionally filter for coverage and feature-wise variance.

Usage

read_bismark(
  files,
  coverage = 1,
  prop_samples = 1,
  remove_zero_var = FALSE,
  return_mats = FALSE
)

Arguments

files

Named vector of file paths to the bismark coverage files

coverage

Minimum coverage for a CpG site. Default (1)

prop_samples

Proportion of samples that site must be covered to be retained. Default (1)

remove_zero_var

Should zero variance features be removed after filtering for coverage? Default (FALSE)

return_mats

if TRUE, return matrices of the filtered data in a list. Else, return the filtered data.table in bismark format. Default (FALSE)

Value

Either a data.table of filtered data in bismark format with an added Coverage column (Me + Un) and, if remove_zero_var = TRUE, a Var column with the per-site variance of Percent; or, if return_mats = TRUE, a list of matrices of the filtered data with columns in the order of files. Note that when prop_samples < 1, sites are filtered as a whole: rows from samples below the coverage threshold are kept at sites that pass.

Details

The function can optionally return a list of CpG x Sample matrices of the filtered data if return_mats = TRUE. The following matrices will be returned for each of the retained CpG sites: Coverage, Percent Methylation (Percent), count of methylated CpGs (Methylated) and count of unmethylated CpGs (Unmethylated)

Examples

# Write three small bismark coverage files
# (chrom, start, end, percent, methylated, unmethylated)
cov <- list(
  S1 = "chr1\t100\t100\t50\t5\t5\nchr1\t200\t200\t100\t20\t0\nchr2\t50\t50\t0\t0\t10\n",
  S2 = "chr1\t100\t100\t80\t8\t2\nchr1\t200\t200\t100\t30\t0\nchr2\t50\t50\t25\t1\t3\n",
  S3 = "chr1\t100\t100\t20\t2\t8\nchr1\t200\t200\t100\t20\t0\nchr2\t50\t50\t40\t4\t6\n"
)
files <- vapply(names(cov), function(n) {
  f <- file.path(tempdir(), paste0(n, ".bismark.cov"))
  writeLines(cov[[n]], f, sep = "")
  f
}, character(1))

# Read in all files using default parameters -- returns a data.table
result <- read_bismark(files)
#> 
#> Reading in files...
#> Filtering for Coverage >= 1 in 100% of samples...
#> Done.

# Filter for coverage >= 10 in all samples and remove zero variance features
dt <- read_bismark(files, coverage = 10, remove_zero_var = TRUE)
#> 
#> Reading in files...
#> Filtering for Coverage >= 10 in 100% of samples...
#> Removing any zero variance features...
#> Done.

# Same filtering but return a list of CpG x Sample matrices
l <- read_bismark(files, coverage = 10, return_mats = TRUE)
#> 
#> Reading in files...
#> Filtering for Coverage >= 10 in 100% of samples...
#> Coercing data to matrices...
#> Done.
l$Coverage
#>          S1 S2 S3
#> chr1.100 10 10 10
#> chr1.200 20 30 20
l$Percent
#>           S1  S2  S3
#> chr1.100  50  80  20
#> chr1.200 100 100 100

unlink(files)