unlist a exons GRangeslist object and add a gene name column
2
1
Entering edit mode
tangming2005 ▴ 190
@tangming2005-6754
Last seen 5 months ago
United States

Hi,

I can not find  a way to do it.

exons <- exonsBy(TxDb.Hsapiens.UCSC.hg19.knownGene, by = "gene")

unlist(exons)

# this will lost the gene name information.

How do I add the name of the gene for the exons?

I have read https://www.biostars.org/p/101513/ and A: Fast way to access metadata for GRangesList?

thanks.

Tommy

 

granges grangeslist txdb.hsapiens.ucsc.hg19.knowngene • 2.0k views
ADD COMMENT
3
Entering edit mode
@michael-lawrence-3846
Last seen 2.4 years ago
United States
stack(exons, "gene")

 

ADD COMMENT
0
Entering edit mode

Thanks! How can I miss this function?!   

If it is a list (not GRangesList) object,

I would do 

myname<- names(mylist)

lapply(myname, function(x) mylist[[x]]$gene<- x)

stack is very useful!

 

ADD REPLY
2
Entering edit mode
@herve-pages-1542
Last seen 4 days ago
Seattle, WA, United States

Hi,

The gene ids are stored in the names of the GRangesList object:

ex_by_gene <- exonsBy(TxDb.Hsapiens.UCSC.hg19.knownGene,
                      by="gene")
names(ex_by_gene)[1:3]
# [1] "1"   "10"  "100"

Unlisting doesn't loose them:

exons <- unlist(ex_by_gene)
names(exons)[1:29]
#  [1] "1"   "1"   "1"   "1"   "1"   "1"   "1"   "1"   "1"   "1"   "1"   "1"  
# [13] "1"   "1"   "1"   "10"  "10"  "100" "100" "100" "100" "100" "100" "100"
# [25] "100" "100" "100" "100" "100"

To put them in a metadata column:

mcols(exons)$gene_id <- names(exons)
exons
# GRanges object with 272776 ranges and 3 metadata columns:
#        seqnames               ranges strand |   exon_id   exon_name     gene_id
#           <Rle>            <IRanges>  <Rle> | <integer> <character> <character>
#      1    chr19 [58858172, 58858395]      - |    250809        <NA>           1
#      1    chr19 [58858719, 58859006]      - |    250810        <NA>           1
#      1    chr19 [58859832, 58860494]      - |    250811        <NA>           1
#      1    chr19 [58860934, 58862017]      - |    250812        <NA>           1
#      1    chr19 [58861736, 58862017]      - |    250813        <NA>           1
#    ...      ...                  ...    ... .       ...         ...         ...
#   9997    chr22 [50961997, 50962853]      - |    266958        <NA>        9997
#   9997    chr22 [50963871, 50964033]      - |    266960        <NA>        9997
#   9997    chr22 [50963901, 50964034]      - |    266961        <NA>        9997
#   9997    chr22 [50964430, 50964570]      - |    266963        <NA>        9997
#   9997    chr22 [50964675, 50964905]      - |    266965        <NA>        9997
#   -------
#   seqinfo: 93 sequences (1 circular) from hg19 genome

Hope this helps,

H.

ADD COMMENT
0
Entering edit mode

good to know this! thanks.

ADD REPLY

Login before adding your answer.

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