如何将对齐序列减少到开始和结束坐标?

How to reduce aligned sequences to start and end coordinates?

我有一个数据 table 标记的坐标在两组(A 和 B)之间对齐。例如:

dt_long <- data.table(LABEL_A = c(rep("A", 20), rep("A", 15), rep ("A", 10), rep ("A", 15), rep ("A", 10)),
                  SEQ_A = c(11:30, 61:75, 76:85, 86:100, 110:119),
                  LABEL_B= c(rep("C", 20), rep("D", 15), rep("F", 10), rep("G",15), rep("D", 10)),
                  SEQ_B = c(1:20, 25:11, 16:25, 15:1, 1:5, 8:12))

如何将此信息缩减为短格式,其中给出了每个比对序列的开始和结束坐标。例如:

dt_short <- data.table(LABEL_A = c("A", "A", "A", "A", "A", "A"),
                 Start_A = c(11, 61, 76, 86, 110, 115),
                 End_A = c(30, 75, 85, 100, 114, 119),
                 LABEL_B= c("C", "D", "F", "G", "D", "D"),
                 Start_B = c(1, 25, 16, 15, 1, 8),
                 End_B = c(20, 11, 25, 1, 5, 12))

每个比对序列的长度应该相同。例如:

identical(abs(dt_short$End_A - dt_short$Start_A), abs(dt_short$End_B - dt_short$Start_B))

一个直接的方法是按两个标签分组,得到每组的firstlast,即

library(data.table)

dt_long[, .(Start_A = first(SEQ_A), End_A = last(SEQ_A), Start_B = first(SEQ_B), End_B = last(SEQ_B)), by = .(LABEL_A, LABEL_B)][]

#   LABEL_A LABEL_B Start_A End_A Start_B End_B
#1:       1       3      11    30       1    20
#2:       1       4      61    75      25    11
#3:       1       6      76    85      16    25
#4:       1       7      86   100      15     1

我们可以只对 dcast 进行子集化。当有许多不同的列组时也可以无缝工作

dcast(dt_long[, .SD[c(1, .N)], .(LABEL_A, LABEL_B)], 
  LABEL_A + LABEL_B ~ c("Start", "End")[rowid(LABEL_A, LABEL_B)], 
     value.var = c("SEQ_A", "SEQ_B"))
#    LABEL_A LABEL_B SEQ_A_End SEQ_A_Start SEQ_B_End SEQ_B_Start
#1:       1       3        30          11        20           1
#2:       1       4        75          61        11          25
#3:       1       6        85          76        25          16
#4:       1       7       100          86         1          15

您可以利用 rleid 并结合 Frank 的评论来删除分组列

dt_long[, .(
        LABEL_A=LABEL_A[1L], Start_A=SEQ_A[1L], End_A=SEQ_A[.N], 
        LABEL_B=LABEL_B[1L], Start_B=SEQ_B[1L], End_B=SEQ_B[.N]), 
    by=rleid(LABEL_A, LABEL_B,
        c(0L, cumsum(diff(SEQ_A) > 1L)), 
        c(0L, cumsum(diff(SEQ_B) > 1L)))][, (1) := NULL]

输出:

   LABEL_A Start_A End_A LABEL_B Start_B End_B
1:       A      11    30       C       1    20
2:       A      61    75       D      25    11
3:       A      76    85       F      16    25
4:       A      86   100       G      15     1
5:       A     110   114       D       1     5
6:       A     115   119       D       8    12