Skip to content

Read BED Files ​

This function reads BED files from a specified directory or file path and combines them into a single data frame. BED files use 0-based coordinate starts, while this function transforms the coordinates to 1-based during import. chromStart and thickStart are increased by 1; end positions and blockSizes are kept as they are. blockStarts become 1-based positions relative to chromStart (the first block starts at 1), as in rtracklayer::import.bed(), so the start of a block is chromStart + blockStart - 1.

Usage ​

r
read_bed(path)

Arguments ​

ArgumentDescription
pathA character string specifying the directory containing BED files or the file path to a single BED file.

Value ​

A data frame combining data from the BED files.

Details ​

This function can read multiple BED files from a directory or a single BED file from a specified path. It adds a 'filename' column with the name of the file, and combines the data frames from all files into one. Columns are separated by tabs, or by spaces in a file without any tab. Columns after the twelve of BED are named V13, V14, and so on.

The columns chrom, name, strand and itemRgb are character, also when their values look like numbers (a chromosome named 1), so that the files of a directory can be combined. score is numeric; a score of "." (no score) is NA.

Examples ​

r
bed_file <- tempfile(fileext = ".bed")
writeLines(c(
  "chr1\t100\t500\tgeneA\t0\t+",
  "chr1\t700\t1200\tgeneB\t0\t-"
), bed_file)

# Read a single BED file
bed_data <- read_bed(bed_file)

# Read all BED files in a directory
bed_data <- read_bed(dirname(bed_file))

unlink(bed_file)