有效地将多个字符串/关键字与R中的多个文本匹配

马特·钱伯斯

我正在尝试有效地将精确的肽段(26个字符的字母AZ 1中的氨基酸的短序列)映射到蛋白质(相同字母的较长序列)。我知道的最有效的方法是Aho-Corasicktrie(肽是关键字)。不幸的是,我在R中找不到可以与非核苷酸字母一起使用的AC版本(BiostringsPDict和Starrmatch_ac都对DNA进行了硬编码)。

作为拐杖,我一直在尝试并行化基本的grep方法。但是我很难找到一种方法,而又不会产生大量的IO开销。这是一个简单的示例:

peptides = c("FSSSGGGGGGGR","GAHLQGGAK","GGSGGSYGGGGSGGGYGGGSGSR","IISNASCTTNCLAPLAK")
if (!exists("proteins"))
{
  biocLite("biomaRt", ask=F, suppressUpdates=T, suppressAutoUpdate=T)
  library(biomaRt)
  ensembl = useMart("ensembl",dataset="hsapiens_gene_ensembl")
  proteins = getBM(attributes=c('peptide', 'refseq_peptide'), filters='refseq_peptide', values=c("NP_000217", "NP_001276675"), mart=ensembl)
  row.names(proteins) = proteins$refseq_peptide
}

library(snowfall)
library(Biostrings)
library(plyr)
sfInit(parallel=T, cpus=detectCores()-1)

allPeptideInstances = NULL
i=1
increment=100
count=nrow(proteins)
while(T)
{
  print(paste(i, min(count, i+increment), sep=":"))
  text_source = proteins[i:min(count, i+increment),]
  text = text_source$peptide

  #peptideInstances = sapply(peptides, regexpr, text, fixed=T, useBytes=T)
  peptideInstances = sfSapply(peptides, regexpr, text, fixed=T, useBytes=T)
  dimnames(peptideInstances) = list(text_source$refseq_peptide, colnames(peptideInstances))

  sparsePeptideInstances = alply(peptideInstances, 2, .fun = function(x) {x[x > 0]}, .dims = T)

  allPeptideInstances = c(allPeptideInstances, sparsePeptideInstances, recursive=T)
  if (i==count | nrow(text_source) < increment)
    break
  i = i+increment
}

sfStop()

这里有一些问题:

  • peptideInstances这是一个密集的矩阵,因此从每个工作人员返回它非常冗长。我将其分解为多个块,这样就不会处理40,000(蛋白质)x 60,000(肽)的矩阵。
  • 与肽平行时,由于它们更大,因此与蛋白质平行时更有意义。但是我对尝试用蛋白质来进行测试感到沮丧,因为:
  • 如果text_source中只有一种蛋白质,则此代码将中断。

另外,如果有人知道R中有更好的解决方案,我很乐意使用它。我花了足够的时间在此上,我可能会更好地实施Aho-Corasick。

1其中一些是歧义代码,但为简单起见,请忽略该代码。

马特·钱伯斯

我学习了Rcpp,并亲自实施了Aho-Corasick。现在,CRAN具有良好的通用多关键字搜索

以下是一些用法示例:

listEquals = function(a, b) { is.null(unlist(a)) && is.null(unlist(b)) || !is.null(a) && !is.null(b) && all(unlist(a) == unlist(b)) }

# simple search of multiple keywords in a single text
keywords = c("Abra", "cadabra", "is", "the", "Magic", "Word")
oneSearch = AhoCorasickSearch(keywords, "Is Abracadabra the Magic Word?")
stopifnot(listEquals(oneSearch[[1]][[1]], list(keyword="Abra", offset=4)))
stopifnot(listEquals(oneSearch[[1]][[2]], list(keyword="cadabra", offset=8)))
stopifnot(listEquals(oneSearch[[1]][[3]], list(keyword="the", offset=16)))
stopifnot(listEquals(oneSearch[[1]][[4]], list(keyword="Magic", offset=20)))
stopifnot(listEquals(oneSearch[[1]][[5]], list(keyword="Word", offset=26)))

# search a list of lists
# * sublists are accessed by index
# * texts are accessed by index
# * non-matched texts are kept (to preserve index order)
listSearch = AhoCorasickSearchList(keywords, list(c("What in", "the world"), c("is"), "secret about", "the Magic Word?"))
stopifnot(listEquals(listSearch[[1]][[1]], list()))
stopifnot(listEquals(listSearch[[1]][[2]][[1]], list(keyword="the", offset=1)))
stopifnot(listEquals(listSearch[[2]][[1]][[1]], list(keyword="is", offset=1)))
stopifnot(listEquals(listSearch[[3]], list()))
stopifnot(listEquals(listSearch[[4]][[1]][[1]], list(keyword="the", offset=1)))
stopifnot(listEquals(listSearch[[4]][[1]][[2]], list(keyword="Magic", offset=5)))
stopifnot(listEquals(listSearch[[4]][[1]][[3]], list(keyword="Word", offset=11)))

# named search of a list of lists
# * sublists are accessed by name
# * matched texts are accessed by name
# * non-matched texts are dropped
namedSearch = AhoCorasickSearchList(keywords, list(subject=c(phrase1="What in", phrase2="the world"),
                                                   verb=c(phrase1="is"),
                                                   predicate1=c(phrase1="secret about"),
                                                   predicate2=c(phrase1="the Magic Word?")))
stopifnot(listEquals(namedSearch$subject$phrase2[[1]], list(keyword="the", offset=1)))
stopifnot(listEquals(namedSearch$verb$phrase1[[1]], list(keyword="is", offset=1)))
stopifnot(listEquals(namedSearch$predicate1, list()))
stopifnot(listEquals(namedSearch$predicate2$phrase1[[1]], list(keyword="the", offset=1)))
stopifnot(listEquals(namedSearch$predicate2$phrase1[[2]], list(keyword="Magic", offset=5)))
stopifnot(listEquals(namedSearch$predicate2$phrase1[[3]], list(keyword="Word", offset=11)))

# named search of multiple texts in a single list with keyword grouping and aminoacid alphabet
# * all matches to a keyword are accessed by name
# * non-matched keywords are dropped
proteins = c(protein1="PEPTIDEPEPTIDEDADADARARARARAKEKEKEKEPEPTIDE",
             protein2="DERPADERPAPEWPEWPEEPEERAWRAWWARRAGTAGPEPTIDEKESEQUENCE")
peptides = c("PEPTIDE", "DERPA", "SEQUENCE", "KEKE", "PEPPIE")
peptideSearch = AhoCorasickSearch(peptides, proteins, alphabet="aminoacid", groupByKeyword=T)
stopifnot(listEquals(peptideSearch$PEPTIDE, list(list(keyword="protein1", offset=1),
                                                 list(keyword="protein1", offset=8),
                                                 list(keyword="protein1", offset=37),
                                                 list(keyword="protein2", offset=38))))
stopifnot(listEquals(peptideSearch$DERPA, list(list(keyword="protein2", offset=1),
                                               list(keyword="protein2", offset=6))))
stopifnot(listEquals(peptideSearch$SEQUENCE, list(list(keyword="protein2", offset=47))))
stopifnot(listEquals(peptideSearch$KEKE, list(list(keyword="protein1", offset=29),
                                              list(keyword="protein1", offset=31),
                                              list(keyword="protein1", offset=33))))
stopifnot(listEquals(peptideSearch$PEPPIE, NULL))

# grouping by keyword without text names: offsets are given without reference to the text
names(proteins) = NULL
peptideSearch = AhoCorasickSearch(peptides, proteins, groupByKeyword=T)
stopifnot(listEquals(peptideSearch$PEPTIDE, list(1, 8, 37, 38)))
stopifnot(listEquals(peptideSearch$DERPA, list(1, 6)))
stopifnot(listEquals(peptideSearch$SEQUENCE, list(47)))
stopifnot(listEquals(peptideSearch$KEKE, list(29, 31, 33)))

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

有效地搜索字符串中的关键字

来自分类Dev

在字符串熊猫列中查找多个关键字的更有效方法

来自分类Dev

如何有效地连接多个字符串从每一个对象列表变量?

来自分类Dev

将多个链接记录中的多个字符串串联成每个记录链单个字符串的有效方法

来自分类Dev

替换字符串中多个字符的最有效方法

来自分类Dev

如何有效地建立这个字符串?

来自分类Dev

Clojure:轻松地从无效字符串转换为有效关键字

来自分类Dev

R中的多个字符串/模式匹配

来自分类Dev

如何有效地匹配两个数据帧中的字符串

来自分类Dev

有效地计算两个字符串之间的编辑距离

来自分类Dev

如何有效地排序R中字符串中的字符?

来自分类Dev

有效地将空字符串替换为数组中的null

来自分类Dev

有效地将空字符串替换为数组中的null

来自分类Dev

匹配多个字符串

来自分类Dev

在R中通过索引有效地替换字符串元素

来自分类Dev

如何在R中有效地对字符串中的字母重新排序?

来自分类Dev

在R中通过索引有效地替换字符串元素

来自分类Dev

Javascript:如何最有效地将数组连接到少于64个字符的块中?

来自分类Dev

匹配并替换文本向量中的多个字符串,而无需在R中循环

来自分类Dev

Python中的多个字符串匹配

来自分类Dev

如何在 JavaScript 中的字符串数组中有效地查找包含另一个字符串的组字符串?

来自分类Dev

通过匹配项的数量有效地对字符串匹配进行排名

来自分类Dev

在R中有效分割数据帧中的多个字符向量

来自分类Dev

替代图像文本中的多个字符串

来自分类Dev

如何有效地将字符串与许多正则表达式匹配

来自分类Dev

在程序宏中,如何检查字符串是否是有效的变量名而不是关键字?

来自分类Dev

优化两个字符串中的关键字搜索

来自分类Dev

转换bash或R中每N个字符串的有效方法

来自分类Dev

有效地替换字符串中的子字符串

Related 相关文章

  1. 1

    有效地搜索字符串中的关键字

  2. 2

    在字符串熊猫列中查找多个关键字的更有效方法

  3. 3

    如何有效地连接多个字符串从每一个对象列表变量?

  4. 4

    将多个链接记录中的多个字符串串联成每个记录链单个字符串的有效方法

  5. 5

    替换字符串中多个字符的最有效方法

  6. 6

    如何有效地建立这个字符串?

  7. 7

    Clojure:轻松地从无效字符串转换为有效关键字

  8. 8

    R中的多个字符串/模式匹配

  9. 9

    如何有效地匹配两个数据帧中的字符串

  10. 10

    有效地计算两个字符串之间的编辑距离

  11. 11

    如何有效地排序R中字符串中的字符?

  12. 12

    有效地将空字符串替换为数组中的null

  13. 13

    有效地将空字符串替换为数组中的null

  14. 14

    匹配多个字符串

  15. 15

    在R中通过索引有效地替换字符串元素

  16. 16

    如何在R中有效地对字符串中的字母重新排序?

  17. 17

    在R中通过索引有效地替换字符串元素

  18. 18

    Javascript:如何最有效地将数组连接到少于64个字符的块中?

  19. 19

    匹配并替换文本向量中的多个字符串,而无需在R中循环

  20. 20

    Python中的多个字符串匹配

  21. 21

    如何在 JavaScript 中的字符串数组中有效地查找包含另一个字符串的组字符串?

  22. 22

    通过匹配项的数量有效地对字符串匹配进行排名

  23. 23

    在R中有效分割数据帧中的多个字符向量

  24. 24

    替代图像文本中的多个字符串

  25. 25

    如何有效地将字符串与许多正则表达式匹配

  26. 26

    在程序宏中,如何检查字符串是否是有效的变量名而不是关键字?

  27. 27

    优化两个字符串中的关键字搜索

  28. 28

    转换bash或R中每N个字符串的有效方法

  29. 29

    有效地替换字符串中的子字符串

热门标签

归档