我有两个数据: GWAS输出的SNP列表和基因开始/结束坐标列表。我想要过滤(使用dplyr包),只提取那些Position位于起始/结束边界内的基因。
我认为%in%可能是正确的方法,但我正在挣扎于这样一个事实,即基因坐标是一系列值。因此,我不能只查找SNP位置与基因位置匹配的行。
我见过使用BiomaRt包和其他解决方案的解决方案,但我正在寻找dplyr解决方案。提前谢谢。
基因数据:
Gene Start End
gene1 1 5
gene2 10 15
gene3 20 25
gene4 30 35SNP数据文件:
Position SNP_ID
6 ss1
8 ss2
9 ss3
11 ss4
16 ss5
19 ss6
27 ss7
34 ss8期望产出:
Gene Start End
gene2 10 15
gene4 30 35发布于 2019-05-08 18:38:44
这项任务是鉴定至少有一个SNP的基因。我们可以通过使用Start和End来遍历map2和End的位置,并询问它们之间是否有SNP的位置:
library( tidyverse )
dfg %>% mutate( AnyHits = map2_lgl(Start, End, ~any(dfs$Position %in% seq(.x,.y))) )
# # A tibble: 4 x 4
# Gene Start End AnyHits
# <chr> <dbl> <dbl> <lgl>
# 1 gene1 1 5 FALSE
# 2 gene2 10 15 TRUE
# 3 gene3 20 25 FALSE
# 4 gene4 30 35 TRUE 从这里开始,它只是一个简单的%>% filter(AnyHits),可以将数据帧减少到至少有一个SNP命中的行。
数据:
# Genes
dfg <- tibble( Gene = str_c("gene",1:4),
Start = c(1,10,20,30),
End = c(5,15,25,35) )
# SNPs
dfs <- tibble( Position = c(6,8,9,11,16,19,27,34),
SNP_ID = str_c("ss",1:8) )https://stackoverflow.com/questions/56045743
复制相似问题