有什么办法可以为模式匹配功能做 lapply 吗?

Is there any way to do lapply for patternmatch function?

我正在尝试使用参考字符串为我的子字符串编制索引。子字符串有 1 或 2 个不匹配。

   substr   Ref
1 CTTGTAGG  AGGCCTTGTCGGT
2 TATGACT   ATTTATGATTGC

我想得到类似

的东西
    substr   Ref         substr_start
1 CTTGTAGG  AGGCCTTGTCGGT 5            
2 TATGACT   ATTTATGATTGC  4

Biostrings 的 R matchPattern 函数工作正常。但是我想 运行 通过循环或 lapply 来获取文件中所有条目的结果。

这是我尝试过的:

for(i in 1:length(file$substr)){
for(j in 1:length(file$Ref)){
matchPattern(file$substr[i], file$Ref[j], max.mismatch=1, min.mismatch=0, with.indels=FALSE, fixed=TRUE, algorithm="auto") 
}}

它只是抛出一个错误说 "unable to find an inherited method for function ‘matchpattern’ for signature ‘factor’"

请问有什么好的方法吗?也欢迎 R 之外的解决方案:)

您可以使用 mapplymatchPattern 应用于 file 和 return [=16= 中的每个 substrRef 值] 价值。

library(Biostrings)

file$substr_start <- mapply(function(x, y) {
         temp <- matchPattern(x, y, max.mismatch=1, min.mismatch=0, 
                  with.indels=FALSE, fixed=TRUE, algorithm="auto")
         start(temp)
}, file$substr, file$Ref)


file
#    substr           Ref substr_start
#1 CTTGTAGG AGGCCTTGTCGGT            5
#2  TATGACT  ATTTATGATTGC            4