This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Is possible to convert BSgenome object to GRange?

Hi everybody, I was wondering if there is any way to convert BSgenome object to GRange. I'm using library(BSgenome.Dmelanogaster.UCSC.dm3)

and I want a GR object like this:

FBgn0000003     chr3R [ 2648220, 2648518] + | FBgn0000003
FBgn0000008     chr2R [18024494, 18060346] + | FBgn0000008
.........

Any function for doing that? Thanks!

bsgenome granges

1 answer

A BSgenome object is essentially a sequence and mask database. A GRanges object is a set of regions with optional metadata. While you can use these together, unless you want a GRanges object having the chromosome/contig lengths then your question is essentially like asking "Can I use purple to make a car?"

Having said that, perhaps you've created a view of a BSgenome that you want to make a GRanges object out of. In that case, use the granges() function.

I understand what you say. My concern is if there is an easy option to construct a GRange (ranges and strand) object using the complete Dmelanogaster genome, let's say something like that, in a fast way:

 genes <- GRanges(seqnames=c("chr2L" , "chr2R" , "chr3L" , "chr3R" , "chr4"  , "chrX"  , "chrU" , "chrM"  , "chr2LHet" , "chr2RHet" , "chr3LHet" , "chr3RHet" , "chrXHet"  , "chrYHet"  , "chrUextra"),
                       ranges=IRanges(
                       start=c(?,?,?,?),
                       end=c(?,?,?,?))),
                       strand=c("?", "?"),
                     seqlengths=c( chr2L= 23011544,   chr2R= 21146708,  chr3L=24543557,  chr3R=27905053,   chr4=1351857,   chrX=22422827,  chrU= 10049037,     chrM= 19517,   chr2LHet= 368872,   chr2RHet= 3288761,    chr3LHet= 2555491,  chr3RHet=  2517507, chrXHet=204112,   chrYHet= 347038 , chrUextra= 29004656)
library("BSgenome.Dmelanogaster.UCSC.dm6")
library("GenomicRanges")
nContigs = length(seqnames(Dmelanogaster))
gr = GRanges(seqnames=seqnames(Dmelanogaster),
                      ranges=IRanges(start=rep(1, nContigs), end=seqlengths(Dmelanogaster)),
                      strand=rep("*", nContigs))

BSgenome objects have no concept of genes, so that sort of information isn't there.

That exactly what I was looking for! Thanks!

A slightly quicker way:

GRanges(seqinfo(BSgenome.Dmelanogaster.UCSC.dm6))

Super quick! Thanks very much!!

I think it would be great to promote this comment as an answer, so that it can be upvoted at the top.

Log in to answer this question.