Biostrings gregexpr2 gives errors while gregexpr works fine

ε祈祈猫儿з 提交于 2021-02-10 15:38:43

问题


I'm replacing gregexpr with gregexpr2 to detect overlapping matches. When I try.

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

gregexpr2("TAAT|ATTA",subSeq)

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

whereas

gregexpr("TAAT|ATTA",subSeq)

works fine.

What happened?


回答1:


It is quite clear if you read 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).

I bolded the relevant sentence above. So, your gregexpr2 searches for TAAT|ATTA text in your input, and since there is no pipe, no match is found.

If you need regex overlapping matches, use str_match_all from stringr:

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

The str_match_all function keeps all the capturing group values (matched with (...) pattern parts), so you will collect all the overlapping matches due to the capturing group used inside a positive lookahead (that is a non-consuming pattern letting the regex engine fire the pattern at each location inside the string).

Pattern details:

  • (?= - start of a non-consuming positive lookahead that will trigger at each location inside the string
    • ( - start of a capturing group
      • TAAT - TAAT substring
      • | - or
      • ATTA - ATTA substring
    • ) - end of the capturing group
  • ) - end of the positive lookahead.


来源:https://stackoverflow.com/questions/44856575/biostrings-gregexpr2-gives-errors-while-gregexpr-works-fine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!