Question: unlist a exons GRangeslist object and add a gene name column
0
gravatar for tangming2005
22 months ago by
tangming2005140
United States
tangming2005140 wrote:

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

 

ADD COMMENTlink modified 22 months ago by Hervé Pagès ♦♦ 14k • written 22 months ago by tangming2005140
Answer: unlist a exons GRangeslist object and add a gene name column
3
gravatar for Michael Lawrence
22 months ago by
United States
Michael Lawrence11k wrote:
stack(exons, "gene")

 

ADD COMMENTlink written 22 months ago by Michael Lawrence11k

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 REPLYlink written 22 months ago by tangming2005140
Answer: unlist a exons GRangeslist object and add a gene name column
2
gravatar for Hervé Pagès
22 months ago by
Hervé Pagès ♦♦ 14k
United States
Hervé Pagès ♦♦ 14k wrote:

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 COMMENTlink modified 22 months ago • written 22 months ago by Hervé Pagès ♦♦ 14k

good to know this! thanks.

ADD REPLYlink written 22 months ago by tangming2005140
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 16.09
Traffic: 116 users visited in the last hour