## ---------------------------------------------------------------------
## TOY EXAMPLE
## ---------------------------------------------------------------------
gr <- GRanges(Rle(c("chr2", "chr1", "chr3", "chr1"), 4:1),
IRanges(1:10, width=5),
seqinfo=Seqinfo(c("chr1", "chr2", "chr3"), c(100, 50, 20)))
ar <- absoluteRanges(gr)
ar
gr2 <- relativeRanges(ar, seqlengths(gr))
gr2
## Sanity check:
stopifnot(all(gr == gr2))
## ---------------------------------------------------------------------
## ON REAL DATA
## ---------------------------------------------------------------------
## With a "small" genome
library(TxDb.Dmelanogaster.UCSC.dm3.ensGene)
txdb <- TxDb.Dmelanogaster.UCSC.dm3.ensGene
ex <- exons(txdb)
ex
isSmallGenome(ex)
## Note that because isSmallGenome() can return NA (see Value section
## above), its result should always be wrapped inside isTRUE() when
## used in an if statement:
if (isTRUE(isSmallGenome(ex))) {
ar <- absoluteRanges(ex)
ar
ex2 <- relativeRanges(ar, seqlengths(ex))
ex2 # original strand is not restored
## Sanity check:
strand(ex2) <- strand(ex) # restore the strand
stopifnot(all(ex == ex2))
}
## With a "big" genome (but we can reduce it)
library(TxDb.Hsapiens.UCSC.hg19.knownGene)
txdb <- TxDb.Hsapiens.UCSC.hg19.knownGene
ex <- exons(txdb)
isSmallGenome(ex)
## Not run:
# absoluteRanges(ex) # error!
# ## End(Not run)
## However, if we are only interested in some chromosomes, we might
## still be able to use absoluteRanges():
seqlevels(ex, force=TRUE) <- paste0("chr", 1:10)
isSmallGenome(ex) # TRUE!
ar <- absoluteRanges(ex)
ex2 <- relativeRanges(ar, seqlengths(ex))
## Sanity check:
strand(ex2) <- strand(ex)
stopifnot(all(ex == ex2))
Run the code above in your browser using DataLab