【发布时间】:2020-10-22 06:47:28
【问题描述】:
我有一个基因数据集,我希望在其中订购样本/基因,按基因组中彼此相距一定距离的样本/基因进行分组。
例如,我的数据集如下所示:
#dt1
Gene chromosome position CP
Gene1 1 70000200 1:70000200
Gene2 5 10000476 5:10000476
Gene3 1 70000201 1:70000201
Gene4 5 10000475 5:10000475
我还有一个原点位置数据集:
#dt2
chromosome position CP
1 70005000 1:70005000
5 10005000 5:10005000
如果基因在我的第二个 dt2 数据集中的任何位置的 +/- 500000 距离内并且位于同一染色体上,我正在尝试对我的第一个数据集中的基因进行分组。我的实际数据中存在一个问题,对于一个针对多个起源 dt2 位置的基因来说,这可能是正确的,所以我也在尝试排序到它最接近的那个。
输出旨在给有序组:
Gene chromosome position Group
Gene1 1 70000200 1
Gene3 1 70000201 1
Gene4 5 10000475 2
Gene2 5 10000476 2
Gene1 和 Gene3 在 dt2 原点的 500000 范围内,并且都在同一条染色体上,因此被归为一组,对于基因 4 和 2 来说是相同的
目前我正在尝试这样做:
dt2[, c("low", "high") := .(position - 500000, position + 500000)]
#find matches on chromosome, with position between low&high
dt1[ dt2, match := i.CP,
on = .(chromosome, position > low, position < high ) ]
#outputs:
Gene chromosome position CP match
1 Gene1 1 70000200 1:70000200 1:70005000
2 Gene2 5 10000476 5:10000476 5:10005000
3 Gene3 1 70000201 1:70000201 1:70005000
4 Gene4 5 10000475 5:10000475 5:10005000
我在 2 个级别上遇到了问题,似乎没有在我的实际数据上获得匹配列的输出,所以我想知道是否有其他方法可以尝试对此进行编码。我还在努力将匹配列转换为分组匹配并在预期的输出中识别我想要的组,我有生物学背景,所以我不确定如何改变这一点 - 任何帮助将不胜感激。
输入数据:
#dt1:
structure(list(Gene = c("Gene1", "Gene2", "Gene3", "Gene4"),
chromosome = c(1L, 5L, 1L, 5L), position = c(70000200L, 10000476L,
70000201L, 10000475L), CP = c("1:70000200", "5:10000476",
"1:70000201", "5:10000475"), match = c("1:70005000", "5:10005000",
"1:70005000", "5:10005000")), row.names = c(NA, -4L), class = c("data.table",
"data.frame"))
#dt2:
structure(list(chromosome = c(1L, 5L), position = c(70005000L,
10005000L), CP = c("1:70005000", "5:10005000"), low = c(69505000,
9505000), high = c(70505000, 10505000)), row.names = c(NA, -2L
), class = c("data.table", "data.frame"))
【问题讨论】:
标签: r data.table conditional-statements bioinformatics