Convert an R data.frame to a bed file in R
1
0
Entering edit mode
21 months ago
LDT ▴ 330

Dear all,

I have a data.frame that looks like this.

bed <- data.frame(chrom=c(rep("Chr1",5)),
                        chromStart=c(18915152,24199229,73730,81430,89350),
                        chromEnd=c(18915034,24199347,74684,81550,89768), 
                         strand=c("-","+","+","+","+"))

write.table(bed, "test_xRNA.bed",row.names = F,col.names = F, sep="\t", quote=FALSE)

Created on 2022-07-29 by the [reprex package](https://reprex.tidyverse.org) (v2.0.1)

and I want to convert it into a bed file. I try to do it with the writing.table function, but I fail miserably by taking this error comment when I look at the intersect

Error: unable to open file or unable to determine types for file test_xRNA.bed

- Please ensure that your file is TAB delimited (e.g., cat -t FILE).
- Also ensure that your file has integer chromosome coordinates in the 
  expected columns (e.g., cols 2 and 3 for BED).

Any ideas of how I can properly convert a data.frame to a .bed file in R?

I have heard about the rtracklayer package, does anyone have an experience with it?

bam bed bedtools R rtracklayer • 2.3k views
ADD COMMENT
2
Entering edit mode
21 months ago

You have a few things going on here. The end of the range always needs to be larger than the beginning of the range. My guess is that at some point the start and end positions were switched for negative strand ranges. You're also missing a name and score column before strand, which are required if strand is included.

The easiest way to fix this would be to fix your ranges, convert your data.frame to a GRanges object using GenomicRanges::makeGRangesFromDataFrame, and then save it as a bed file with rtracklayer::export.bed. This will also ensure the format is correct (i.e. taking care of the missing name and score columns).

ADD COMMENT

Login before adding your answer.

Traffic: 2453 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6