Biostrings gregexpr2 给出错误,而 gregexpr 工作正常

Biostrings gregexpr2 gives errors while gregexpr works fine

我正在用 gregexpr2 替换 gregexpr 来检测重叠匹配项。当我尝试。

>subSeq
 3000-letter "DNAString" instance
 seq: ACACGTGTTCTATTTTCATTTGCTGACATTTTCTAGTGCATCATTTTTTATTTTATTTTCATT....

gregexpr2("TAAT|ATTA",subSeq)

Error in matches[[i]] : subscript out of bounds

gregexpr("TAAT|ATTA",subSeq)

工作正常。

发生什么事了?

看了就很清楚了gregexpr2 documentation:

This is a replacement for the standard gregexpr function that does exact matching only. Standard gregexpr() misses matches when they are overlapping. The gregexpr2 function finds all matches but it only works in "fixed" mode i.e. for exact matching (regular expressions are not supported).

我把上面的相关句子加粗了。因此,您的 gregexpr2 在您的输入中搜索 TAAT|ATTA 文本,由于没有管道,因此找不到匹配项。

如果您需要正则表达式重叠匹配,请使用 stringr:

中的 str_match_all
library(stringr)
> x <- "TAATTA"
> str_match_all(x, "(?=(TAAT|ATTA))")
[[1]]
     [,1] [,2]  
[1,] ""   "TAAT"
[2,] ""   "ATTA"

str_match_all 函数保留所有捕获组值(与 (...) 模式部分匹配),因此您将收集所有重叠匹配项,因为捕获组在正前瞻中使用(即是一种非消耗模式,让正则表达式引擎在字符串内的每个位置触发模式。

图案详情:

  • (?= - 非消耗 positive lookahead 的开始,它将在字符串内的每个位置触发
    • ( - 捕获组的开始
      • TAAT - TAAT 子字符串
      • | - 或
      • ATTA - ATTA 子字符串
    • ) - 捕获组结束
  • ) - 正前瞻结束。