Last active
September 1, 2022 15:18
-
-
Save mikelove/ada111db7bdf55ad7c778349a587714d to your computer and use it in GitHub Desktop.
Find closest gene
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(TxDb.Hsapiens.UCSC.hg19.knownGene) | |
txdb <- TxDb.Hsapiens.UCSC.hg19.knownGene | |
g <- genes(txdb) | |
library(org.Hs.eg.db) | |
g$symbol <- mapIds(org.Hs.eg.db, g$gene_id, "SYMBOL", "ENTREZID") | |
library(plyranges) | |
query <- data.frame( | |
seqnames=c("chr1","chr2","chr3"), | |
start=50e6, end=50e6) %>% | |
as_granges() | |
query %>% join_nearest(g) | |
# if you want TSS: | |
tss <- g %>% anchor_5p() %>% mutate(width=1) | |
query %>% join_nearest(tss) | |
# if you want the gene position also in the output: | |
g_start_end <- g %>% mutate(gene_start = start, gene_end = end) | |
query %>% join_nearest(g_start_end, distance=TRUE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment