How to simply change chromosome names of a GRanges object?
1
0
Entering edit mode
changxu.fan ▴ 20
@changxufan-24128
Last seen 19 months ago
United States

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

genomicrang GenomicRanges • 3.7k views
ADD COMMENT
1
Entering edit mode
@james-w-macdonald-5106
Last seen 3 days ago
United States

I can't imagine the use case for doing that, but it's easy enough to do. You just can't do it to the original GRanges object, so far as I know. But you can make a new one easily enough.

> gr <- GRanges(seqnames = c("chr1", "chr2"),
              ranges = IRanges(start = 1:2, end = 4:5))

> gr
GRanges object with 2 ranges and 0 metadata columns:
      seqnames    ranges strand
         <Rle> <IRanges>  <Rle>
  [1]     chr1       1-4      *
  [2]     chr2       2-5      *
  -------
  seqinfo: 2 sequences from an unspecified genome; no seqlengths
> gr2 <- GRanges(rep("chrx", length(gr)), ranges(gr))
> gr2
GRanges object with 2 ranges and 0 metadata columns:
      seqnames    ranges strand
         <Rle> <IRanges>  <Rle>
  [1]     chrx       1-4      *
  [2]     chrx       2-5      *
  -------
  seqinfo: 1 sequence from an unspecified genome; no seqlengths
ADD COMMENT
1
Entering edit mode

Exactly.

Note that a more idiomatic way of creating this new GRanges is with:

GRanges(Rle("chrX", length(gr)), ranges(gr, use.mcols=TRUE), strand=strand(gr))

This will ensure that the strand and metadata cols of the original object are preserved.

H.

ADD REPLY
0
Entering edit mode

What's the rationale for using an Rle? Is it more efficient?

ADD REPLY
0
Entering edit mode

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 when gr has length 0:

gr0 <- gr[0]
GRanges("chrX", ranges(gr0, use.mcols=TRUE), strand=strand(gr0))
# Error in stop_if_wrong_length(what, ans_len) : 
#   'ranges' must have the length of the object to construct (1) or length
#   1

Supplying the seqnames as rep("chrX", length(gr)) or Rle("chrX", length(gr)) is guaranteed to always work. However using Rle() is more efficient than using rep() and is not more typing.

H.

ADD REPLY

Login before adding your answer.

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