Entering edit mode
I have one simple goal:
gr <- GRanges(seqnames = c("chr1", "chr2"),
ranges = IRanges(start = 1:2, end = 4:5))
I want to change the chromosome names all to "chrX"
> seqnames(gr) <- "chrX"
Error in .normalize_seqnames_replacement_value(value, x) :
levels of supplied 'seqnames' must be identical to 'seqlevels(x)'
Please let me know if there is a simple way to do this.
Best regards
Exactly.
Note that a more idiomatic way of creating this new GRanges is with:
This will ensure that the strand and metadata cols of the original object are preserved.
H.
What's the rationale for using an
Rle
? Is it more efficient?This is to make sure that the supplied seqnames has the correct length.
Note that the
GRanges()
constructor will turn the supplied seqnames into an Rle, and, if its length is not the same as the length of the supplied IRanges, it will recycle the shortest to the length of the longest. This normally works well, except whengr
has length 0:Supplying the seqnames as
rep("chrX", length(gr))
orRle("chrX", length(gr))
is guaranteed to always work. However usingRle()
is more efficient than usingrep()
and is not more typing.H.