我有一个巨大的数据框df
,其中包含有关重叠间隔(A)和(B)以及它们所在的染色体(chrom)的信息。还有关于在区间(A)中观察到的值(基因表达水平)的信息。
chrom value Astart Aend Bstart Bend
chr1 0 0 54519752 17408 17431
chr1 0 0 54519752 17368 17391
chr1 0 0 54519752 567761 567783
chr11 0 2 93466832 568111 568133
chr11 0 2 93466832 568149 568171
chr11 0 2 93466832 1880734 1880756
chr11 4 93466844 93466880 93466856 93466878
chr11 2 93466885 135006516 93466889 93466911
chr11 2 93466885 135006516 94199710 94199732
请注意,相同的间隔可能会出现多次,例如,如果间隔(B)与两个(A)间隔重叠,则会报告两次:
Astart(1)=========================Aend1 Astart(2)========================Aend(2)
Bstart(1)=======================================Bend(1)
chrom value Astart Aend Bstart Bend
chr1 0 0 25 15 35 #A(1) and B(1) overlap
chr1 1 28 45 15 35 #A(2) and B(1) overlap
同样,如果间隔(A)与两个或更多(B)间隔重叠,则会报告两次或更多次:
Astart(3)===================================================================Aend(3)
Bstart(2)=========Bend(2) Bstart(3)===========Bend(3) Bstart(4)===============Bend(4)
chrom value Astart Aend Bstart Bend
chr4 0 10 100 15 25 #A(3) and B(2) overlap
chr4 0 10 100 30 75 #A(3) and B(3) overlap
chr4 3 10 100 80 120 #A(3) and B(4) overlap
我的目标是输出间隔(B)中的所有个别位置和(A)中的相应值。我有一段代码可以很好地输出(B)中的所有相关位置:
position <- unlist(mapply(seq, ans$Bstart, ans$Bend - 1))
> head(position)
[1] 17408 17409 17410 17411 17412 17413
这个问题是从那里检索染色体信息是不够的。当我列出这些位置时,我需要同时检查染色体信息和位置。那是因为在几条染色体上可能会出现相同的位置整数,所以我之后不能运行类似for position %in% range(Astart, Aend) output $chrom, $value
(虚拟代码)的东西。
如何同时检索(chrom, position, value)
?
预期结果将是这样的:
> head(expected_result)
chrom position value
chr1 17408 0
chr1 17409 0
chr1 17410 0
chr1 17411 0
chr1 17412 0
chr1 17413 0
#skipping some lines to show another part of the dataframe
chr11 93466856 4
chr11 93466857 4
答案 0 :(得分:1)
对ddply
的调用可能更优雅,但逻辑是相同的:
dfA = read.table(textConnection("chrom value Astart Aend Bstart Bend
chr1 0 0 54519752 17408 17431
chr1 0 0 54519752 17368 17391
chr1 0 0 54519752 567761 567783
chr11 0 2 93466832 568111 568133
chr11 0 2 93466832 568149 568171
chr11 0 2 93466832 1880734 1880756
chr11 4 93466844 93466880 93466856 93466878
chr11 2 93466885 135006516 93466889 93466911
chr11 2 93466885 135006516 94199710 94199732"), header = TRUE)
dfB = as.data.frame(do.call(rbind,
apply(dfA, MARGIN = 1, FUN = function(x) {
cbind(mapply(seq,
as.numeric(x['Bstart']),
as.numeric(x['Bend']) - 1),
x['chrom'], x['value'])
}
)))
lapply(dfB, typeof)