GRanges - add names when constructing a new GRanges, rather than afterwards?
2
0
Entering edit mode
Janet Young ▴ 740
@janet-young-2360
Last seen 5.0 years ago
Fred Hutchinson Cancer Research Center,…

Hi there,

 

Here's a request that would let me skip a step or two once in a while:  it's not a big deal, but it would be nice if it were possible. I'd like to be able to add names to my GRanges objects when I construct them, rather than adding them afterwards. It would be convenient sometimes to let me avoid creating temporary objects that I'd rather do without.   I think the code below walks you through what I'm trying to do.  

thanks for thinking about it!

Janet

##############

library(GenomicRanges)

### constructing a GRanges object and adding names afterwards works well:
myGR <- GRanges(seqnames=c("chr1","chr2"), ranges=IRanges(start=c(100,200), width=100))
names(myGR) <- c("region1","region2")
myGR

### but I can't add names when I construct the object. That gives me a column in values(myGR_2) called "names".  Not quite what I wanted:
myGR_2 <- GRanges(seqnames=c("chr1","chr2"), ranges=IRanges(start=c(100,200), width=100), names=c("region1","region2"))
myGR_2

### one reason to want that is because sometimes I want to add to an existing GRanges object, and I'd like to be able to do that without creating extra interim objects.  Here's just one example, but you could imagine wrapping the GRanges constructor in various other functions without wanting to add names in a separate step. 

### I'd like to be able to do this in one step:
myGR_3 <- c( myGR, GRanges(seqnames=c("chr2","chr2"), ranges=IRanges(start=c(300,400),width=100), names=c("region3","region4")) )

### but instead I have to do it in three steps:  
temp <- GRanges(seqnames=c("chr2","chr2"), ranges=IRanges(start=c(300,400),width=100))
names(temp) <- c("region3","region4")
myGR_3 <- c( myGR, temp)

sessionInfo()

#######
R version 3.2.2 (2015-08-14)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 14.04.2 LTS

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats4    parallel  stats     graphics  grDevices utils     datasets 
[8] methods   base     

other attached packages:
[1] GenomicRanges_1.21.26 GenomeInfoDb_1.5.15   IRanges_2.3.21       
[4] S4Vectors_0.7.18      BiocGenerics_0.15.6  

loaded via a namespace (and not attached):
[1] zlibbioc_1.15.0 XVector_0.9.4  

genomicranges • 4.3k views
ADD COMMENT
2
Entering edit mode
@michael-lawrence-3846
Last seen 2.9 years ago
United States

GRanges() will take the names from the ranges argument:

myGR <- GRanges(c("chr1", "chr2"), IRanges(c(100, 200), width=100, names=c("a", "b")))

Also, there's always setNames() from base R:

namedGR <- setNames(myGR, names)
ADD COMMENT
0
Entering edit mode

turns out it's easy: sorry that wasn't more obvious to me.  Thanks!

 

ADD REPLY
0
Entering edit mode

turns out it's easy: sorry that wasn't more obvious to me.  Thanks!

 

ADD REPLY
0
Entering edit mode
Malcolm Cook ★ 1.6k
@malcolm-cook-6293
Last seen 3 months ago
United States

> myGR <- `names<-`(GRanges(seqnames=c("chr1","chr2"), ranges=IRanges(start=c(100,200), width=100)), c("region1","region2"))
> myGR
GRanges object with 2 ranges and 0 metadata columns:
          seqnames     ranges strand
             <Rle>  <IRanges>  <Rle>
  region1     chr1 [100, 199]      *
  region2     chr2 [200, 299]      *
  -------
  seqinfo: 2 sequences from an unspecified genome; no seqlengths

 

ADD COMMENT
0
Entering edit mode

Thanks, Malcolm - I'll go with Michael's method, which is easier to understand (I have to admit that the backtick syntax is mysterious to me right now)

ADD REPLY

Login before adding your answer.

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