【问题标题】:How to match data based on a range of values [duplicate]如何根据一系列值匹配数据[重复]
【发布时间】:2020-06-03 19:46:34
【问题描述】:

我有 2 个基因数据集,我试图在其中查找基因组(文件 1)中某个位置的变体是否在另一个数据集(文件 2)中我的任何行的范围内匹配/找到,然后提取找到的匹配项文件 2 与文件 1 合并。一个条件是匹配项仅在具有相同染色体的情况下搜索变体。例如:

文件1:

Chromosome    Position
1              3
1              47
2              10
3              2

文件2:

Chromosome    Start    End
1              101      102
1              40       50  
2              40       50
3              20       22

预期输出:

Chromosome    Start    End
1              40       50 
#this is the only row from which a variant from file1 fits in its position range and is on the same chromosome

理想情况下,我会合并 file1 变体,以与 file2 中匹配的染色体开始和结束位置在同一行中对齐,但我是 R 新手,并坚持尝试基于如果它的位置编号在第二个文件的范围内。目前我正在努力适应:

dt1[ dt2, match := i.,ID  #including a made-up ID column for the sake of trying to adapt this code 
     on = .(Chromosome, Position > Start, Position < End ) ]

但这似乎不起作用,除此之外我不知道如何开始。任何有关如何解决此问题的帮助将不胜感激

数据:

dput(file1)
structure(list(Chromosome = c(1L, 1L, 2L, 3L), Position = c(3L, 
47L, 10L, 2L)), row.names = c(NA, -4L), class = c("data.table", 
"data.frame"))

dput(file2)
structure(list(Chromosome = c(1L, 1L, 2L, 3L), Start = c(101L, 
40L, 40L, 20L), End = c(102L, 50L, 50L, 22L)), row.names = c(NA, 
-4L), class = c("data.table", "data.frame"))

【问题讨论】:

  • bedtools intersect 不是你喜欢的?在 R 中,您可以使用 GenomicRanges Bioconductor 包中的 findOverlaps
  • 谢谢你,我不知道这些,我会调查它们。

标签: r data.table bioinformatics


【解决方案1】:

您可以使用tidyverse 包进行重新编码并获取chromosomes,其中Position 的值介于StartEnd 之间。

library(tidyverse)

df<-file1 %>%
  # Join by Chromosome, it will duplicate each Position by Start and End Values
  left_join(file2, 
            by = "Chromosome") %>% 
  # Create a new column to indicate if the Position is between Start and End values
  mutate(isRange = Position >= Start & Position <= End) %>%
  # Filter to stay with only the chromosomes where the previous condition is met
  filter(isRange)

【讨论】:

  • 为什么将isRange 设为数值而不是逻辑值?
  • 您也可以将其编码为FT,而不是01。它会得到相同的结果。
  • 三点:(1)结果是一样的,但是使用逻辑值的解决方案客观上更好,因为它更直接。您的解决方案也是如此,它只是添加了一个额外的冗余间接。 (2) 特别是在使用逻辑时,不需要使用ifelse,也不需要使用逻辑常量字面量:只写mutate(isRange = Position &gt;= Start &amp; Position &lt;= End)filter(isRange); (3) 不要用TF代替TRUEFALSE。它们更短,但它们是可以被覆盖的变量
  • 已经编辑了帖子以包含您的建议,因为它会产生更短、更直接的解决方案。
  • 我们也可以删除 mutate 步骤,只需将条件放在 filter 中:filter(Position &gt;= Start &amp; Position &lt;= End)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-16
  • 2022-11-23
  • 1970-01-01
  • 2020-12-13
  • 1970-01-01
  • 2021-01-02
  • 2020-01-04
相关资源
最近更新 更多