Hi, I have found a nice solution for doing that in R, I post it so maybe it might be useful to other people:
Let' say you have a data frame (df) with list of SNPs ID (rs IDs and not annotated IDs) with chromosome number and location in Base Pairs (BP) on the genome ( I show the first 10 rows but the df continues with other many rows):
SNP std.XPEHH CHR BP rank
1 rs376121383 6.48482 chr2 97868880 1
2 rs201474380 6.47901 chr2 97868313 2
3 rs3926003 6.46181 chr2 97870247 3
4 rs112920594 6.42510 chr2 97866321 4
5 rs372154564 6.42510 chr2 97866355 5
6 rs190916690 6.42453 chr2 97868544 6
7 rs370914866 6.41392 chr2 97867072 7
8 2:97865943 6.40923 chr2 97865943 8
9 2:97868257 6.40907 chr2 97868257 9
10 rs116237095 6.40907 chr2 97868240 10
In order to get the following output data frame:
ENTREZID rank SNP std.XPEHH CHR BP GENE NAME
1 11066 1 rs376121383 6.48482 chr2 97868880 SNRNP35
2 11066 2 rs201474380 6.47901 chr2 97868313 SNRNP35
3 11066 3 rs3926003 6.46181 chr2 97870247 SNRNP35
4 11066 5 rs372154564 6.42510 chr2 97866355 SNRNP35
5 11066 4 rs112920594 6.42510 chr2 97866321 SNRNP35
6 11066 6 rs190916690 6.42453 chr2 97868544 SNRNP35
7 11066 7 rs370914866 6.41392 chr2 97867072 SNRNP35
8 11066 8 2:97865943 6.40923 chr2 97865943 SNRNP35
9 11066 9 2:97868257 6.40907 chr2 97868257 SNRNP35
10 11066 10 rs116237095 6.40907 chr2 97868240 SNRNP35
It is possible to add the corresponding gene name to the SNP location by converting the df to GRanges object and use the findOverlaps function to check which of the SNPs coordinates are intersecting in which genes (based on their coordinates in the TxDb.Hsapiens.UCSC.hg19.knownGene database as GRanges object):
#To find overlapping SNPs coordinates to gene coordinates Load following libraries
library(GenomicRanges)
library(TxDb.Hsapiens.UCSC.hg19.knownGene)
head(df)
# add rank column to the df (this will be usuful later on when fining overlapping rows, already added in the previous df)
df <- df %>% mutate(rank = 1:nrow(df))
# add "chr" before chromosome number in the data frame df in the CHR column (already added in the previous df)
df$CHR <- sprintf('chr%s', df$CHR)
# convert the data frame to GRanges object,
# it takes the chromosome number and the SNP coordinate on Base Pair (BP)
SNPs.gr <- GRanges( seqnames=df$CHR,
IRanges(start=df$BP,
width=1))
headSNPs.gr)
# add the info of the reference genome
genomeSNPs.gr) <- 'hg19'
# get the coordinates of all the genes from database
txs <- transcriptsBy(TxDb.Hsapiens.UCSC.hg19.knownGene)
head(txs)
# find which coordinates of the SNP list are folling into the coordinates of the gene list in the data base
df_overlap<-findOverlaps(df,txs)
#modify name of first column of the previous output to merge with original df
names(df_overlap)[1]<-"rank"
df_merged<-merge(df, df_overlap, by="rank", all=T)
head(df_merged)
# To convert to ENTREZ IDs to HUGO gene names:
library(EnsDb.Hsapiens.v86)
hsens=EnsDb.Hsapiens.v86
my.symbols <- as.character(df_merged$subjectHits)
df_HUGO<-select(hsens,
keys = my.symbols,
columns = c("ENTREZID", "SYMBOL"),
keytype = "ENTREZID")
names(df_merged)[6]<-"ENTREZID"
df_final<-merge(df_merged, df_HUGO, by="ENTREZID", all=T)
This has been asked before many times, search for "map SNP to gene":
ok thanks a lot for all the links. I am taking a look.