问题
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. Thegregexpr2function 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 groupTAAT-TAATsubstring|- orATTA-ATTAsubstring
)- end of the capturing group
)- end of the positive lookahead.
来源:https://stackoverflow.com/questions/44856575/biostrings-gregexpr2-gives-errors-while-gregexpr-works-fine