【问题标题】:'position-aware' aligning of sequences with letter annotations序列与字母注释的“位置感知”对齐
【发布时间】:2020-04-28 03:05:54
【问题描述】:

我们有 2 个 DNA 序列(字符串):

>1
ATGCAT
135198
>2
ATCAT

预期输出:首先,我们需要对齐这两个字符串,然后通过索引获取相关注释:

ATGCAT
AT-CAT
13-198

第一部分可以使用 Biostrings 包完成:

library(Biostrings)

p <- DNAString("ATCAT")
s <- DNAString("ATGCAT")
s_annot <- "135198"

x <- pairwiseAlignment(pattern = p, subject = s)

aligned(x)
# A DNAStringSet instance of length 1
#     width seq
# [1]     6 AT-CAT
as.character(x)
# [1] "AT-CAT"
as.matrix(x)
#     [,1] [,2] [,3] [,4] [,5] [,6]
# [1,] "A"  "T"  "-"  "C"  "A"  "T" 

第二部分的当前解决方法:

annot <- unlist(strsplit(s_annot, ""))
annot[ which(c(as.matrix(x)) == "-") ] <- "-"
# [1] "1" "3" "-" "1" "9" "8"

工作正常,但我想知道是否有 Biostrings 方法(或任何其他包),也许通过将注释保留在 metadata 插槽中,然后在对齐之后,我们在元数据中得到匹配碱基的匹配注释,如下所示:

getSlots("DNAString")
#      shared              offset              length     elementMetadata            metadata 
# "SharedRaw"           "integer"           "integer" "DataTable_OR_NULL"              "list" 

# just an idea, non-working code
s@metadata <- unlist(strsplit(s_annot , ""))
x <- pairwiseAlignment(pattern = p, subject = s)
metadata(x)
# [[1]]
# [1] "1" "3" "-" "1" "9" "8"

注意:

  • BioStars 窃取作者的许可 - https://www.biostars.org/p/415896/
  • 作者想要 biopython 解决方案,所以添加标签,如果可能的话也发布 python 解决方案。

【问题讨论】:

    标签: python r bioinformatics biopython bioconductor


    【解决方案1】:

    一个可能的解决方案:

    dna_fun <- function(s, p, a) {
      s <- strsplit(s, "")[[1]]
      p <- strsplit(p, "")[[1]]
      a <- strsplit(a, "")[[1]]
      ls <- length(s)
      lp <- length(p)
    
      r <- lapply(c(1,seq(lp)), function(x) {
        v <- rep(1, 5)
        v[x] <- 2
        v
      })
    
      mat <- sapply(r, rep, x = p)
      tfm <- mat == matrix(rep(s, ls), ncol = ls)
      m <- which.max(colSums(tfm))
    
      p2 <- mat[, m]
      p2[!tfm[,m]] <- "-"
    
      a[!tfm[,m]] <- "-"
    
      p2 <- paste(p2, collapse = "")
      a <- paste(a, collapse = "")
    
      return(list(p2, a))
    }
    

    与:

    dna_fun(s1, s2, annot)
    

    你得到:

    [[1]]
    [1] "AT-CAT"
    
    [[2]]
    [1] "13-198"
    

    如果您有相应的向量,您可以将Mapdna_fun 函数一起使用:

    s11 <- c("ATGCAT","ATCGAT")
    s22 <- c("ATCAT","ATCAT")
    annot2 <- c("135198","145892")
    
    lm <- Map(dna_fun, s11, s22, annot2)
    
    data.table::rbindlist(lm, idcol = "dna")
    

    这给出了:

          dna     V1     V2
    1: ATGCAT AT-CAT 13-198
    2: ATCGAT ATC-AT 145-92
    

    数据:

    s1 <- "ATGCAT"
    s2 <- "ATCAT"
    annot <- "135198"
    

    【讨论】:

      【解决方案2】:

      根据要求,Biopython 解决方案:

      from Bio import Align
      
      p = "ATCAT"
      s = "ATGCAT"
      s_annot = "135198"
      
      aligner = Align.PairwiseAligner()
      alignment = str(aligner.align(p, s)[0]).split()
      middle = alignment.pop(1)
      alignment.append("".join(c if m == "|" else m for c, m in zip(s_annot, middle)))
      
      print("\n".join(alignment))
      

      输出:

      AT-CAT
      ATGCAT
      13-198
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-01-30
        • 1970-01-01
        • 2022-10-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-28
        • 1970-01-01
        相关资源
        最近更新 更多