I wish to extract the n positions closest to the end of a GRanges object or in other words, making the total width of the ranges in the object n if we count from the end (fixed to the end). Something similar can be done with the resize
method although this one only manipulates the width of each range instead of the width of the whole object.
For example, I have the GRanges:
g <- GenomicRanges::GRanges(
seqnames = c("chr12", "chr12"),
ranges = IRanges::IRanges(c(9102084, 9099001), c(9102551, 9099001)),
strand = c("-", "-")
)
print(g)
GRanges object with 2 ranges and 0 metadata columns:
seqnames ranges strand
<Rle> <IRanges> <Rle>
[1] chr12 9102084-9102551 -
[2] chr12 9099001 -
-------
seqinfo: 24 sequences from an unspecified genome; no seqlengths
And I wonder if there already exists a function f
in GenomicRanges (or combination of functions) which allows me the following result:
n <- 10
g2 <- f(g, width = n, fix = "end")
print(g2)
GRanges object with 2 ranges and 0 metadata columns:
seqnames ranges strand
<Rle> <IRanges> <Rle>
[1] chr12 9102084-9102092 -
[2] chr12 9099001 -
-------
seqinfo: 1 sequence from an unspecified genome; no seqlengths
sum(GenomicRanges::width(g2))
10
Note that I wish to work only with the positions defined in the original ranges and not with 9099001:9099010 for instance.