Convert a bed file to bigBed with rtracklayer
1
1
Entering edit mode
L ▴ 50
@dd482574
Last seen 16 months ago
Germany

Dear all,

I am trying to find a way to convert a bed to .bigBed file in R using the rtracklayer Bioconductor package. I have heard that it is possible to do the conversion, but I have not found any other way apart from Kent Utilities.

Any help or guidance is highly appreciated. Thank you for your time

bed bigBed rtracklayer ucsc • 1.6k views
ADD COMMENT
1
Entering edit mode
@james-w-macdonald-5106
Last seen 19 hours ago
United States

See ?import.bed and ?export.bb

ADD COMMENT
0
Entering edit mode

Thank you, James, for the guidance. I have learnt a lot, but I am struggling heavily to export a .bigBed file. I am running from error to error.

Here is a link to the file that I am trying to convert to.bigBed and this is what I am doing

test <-  importBed(paste0(path_bed,"arath_gff_sorted_tmp.bed"), header = FALSE, sep = "\t") %>%               # import the bed file
  as.data.frame() %>%                                                                                 # make it to data frame
   mutate(name=str_replace_all(name,"transcript:","")) %>%                                            # change the name
  GenomicRanges::makeGRangesFromDataFrame(., keep.extra.columns=TRUE,  ignore.strand = FALSE, start.field="chromStart",
                                          end.field=c("chromEnd"))                                    # convert to GRanges

The size of each chromosome is

seqlengths(test) <- c(30427671,26975502,23459830,19698289,18585056,366924,154478) 

Warning message:
In valid.GenomicRanges.seqinfo(x, suggest.trim = TRUE) :
  GRanges object contains 4546 out-of-bound ranges located on sequence 5. Note that ranges located on a sequence whose length is unknown (NA) or
  on a circular sequence are not considered out-of-bound (use seqlengths() and isCircular() to get the lengths and circularity flags of the
  underlying sequences). You can use trim() to trim these ranges. See ?`trim,GenomicRanges-method` for more information.

I am not sure why these ranges are out of bounds, and then when I try to export it to bigBed, it's a disaster.

export.bb(test, paste0(path_bed,"test"))


Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'x' in selecting a method for function 'as.data.frame': error in evaluating the argument 'x' in selecting a method for function 't': invalid color specification "255,0,0"

Any hint or clue on how to move on, is highly appreaciated

ADD REPLY
1
Entering edit mode

If you use rtracklayer (importBed is not a function in rtracklayer), it should be a simple one-liner. Using the example data from ?export.bb, as I don't have a bed file handy

>  test_path <- system.file("tests", package = "rtracklayer")
>   test_bb <- file.path(test_path, "test.bb")
> export.bb(import(test_bb), "whatevs.bb")

It's not clear to me why you are doing all that conversion to a data.frame and back. There are any number of ways you can modify a GRanges object directly, so coercing to and from other data formats is probably not necessary. I also don't use the tidyverse as a general rule, as it's usually IMO a repackaging of things that you could already do, but more easily. For example

mutate(name=str_replace_all(name, "transcript:", ""))

Is, if I understand correctly, the same as

gsub("transcript:", "", name)

And more to the point, you can just do

test <- import(paste0(path_bed,"arath_gff_sorted_tmp.bed"))
test$name <- gsub("transcript:", "", test$name)
seqlengths(test) <- (<vector of seqlens that are longer than what you used, probably>)
export(test, paste0(path_bed, "test.bb"))

Also, the error you get from the validity test is telling you that there are almost 5000 sequences on chromosome 5 that are past the length (18585056) that you specified for that chromosome. Either your chromosome length is wrong, or the bed file is wrong.

The final error looks like a tidyverse collision to me. I am 99.999% sure that there is no function t used in export.bb that has a color specification.

ADD REPLY
0
Entering edit mode

Thank you so much, James. I have already learnt a lot. I ll study your reply and come back. Thank you again for your time

ADD REPLY

Login before adding your answer.

Traffic: 696 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