How to simply change chromosome names of a GRanges object?
1
0
Entering edit mode
changxu.fan ▴ 10
@changxufan-24128
Last seen 29 days 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 • 137 views
ADD COMMENT
1
Entering edit mode
@james-w-macdonald-5106
Last seen 7 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
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

Login before adding your answer.

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