R中DNAStringset的子集列表

时间:2019-04-18 07:42:31

标签: r list lapply

在我的旧问题Subsetting defined group out of DNAStringSet上,我很难为列表做同样的事情。 我所有导致错误的尝试:

Error in .Call2("new_XStringSet_from_CHARACTER", ans_class, ans_elementType, : key 56 (char '8') not in lookup table

工作示例: library("DECIPHER") library("Biostrings")

我要调用一个名为Biglist的DNAStringsets列表和一个名为patterns的选择来获取新列表:

  aDNAStringSet <- DNAStringSet(c("GCATCCATTAC", "AATCGCCATCC", "GCATACCTTAC", "GCATACCTTAC", "GCATACCTTAC"))
  names(aDNAStringSet) <- c("seq1", "seq2", "seq3", "seq4", "seq5") 

  bDNAStringSet <- DNAStringSet(c("GCATCCATTAC", "AATCGCCATCC", "GCATACCTTAC", "GCATACCTTAC", "GCATACCTTAC"))
  names(bDNAStringSet) <- c("seq1", "seq2", "seq3", "seq4", "seq5") 

  cDNAStringSet <- DNAStringSet(c("GCATCCATTAC", "AATCGCCATCC", "GCATACCTTAC", "GCATACCTTAC", "GCATACCTTAC"))
  names(cDNAStringSet) <- c("seq1", "seq2", "seq3", "seq4", "seq5") 

  dDNAStringSet <- DNAStringSet(c("GCATCCATTAC", "AATCGCCATCC", "GCATACCTTAC", "GCATACCTTAC", "GCATACCTTAC"))
  names(dDNAStringSet) <- c("seq1", "seq2", "seq3", "seq4", "seq5") 



  Biglist<-list(A=aDNAStringSet, B=bDNAStringSet, C=cDNAStringSet, D=dDNAStringSet) 

  patterns <- c("seq2", "seq4", "seq5") 

使用grep可以隔离elements,但这不是我想要的:

  newlist<-Biglist[grep("A", names(Biglist))]

使用lapply应该是正确的方法,但是此行会导致错误:

  newlist<-lapply(Biglist, function(y) y[y %in% patterns])

Error in .Call2("new_XStringSet_from_CHARACTER", ans_class, ans_elementType, : key 101 (char 'e') not in lookup table

我也不理解错误消息。 有什么想法吗?

1 个答案:

答案 0 :(得分:2)

由于您正在使用S4类,因此,您需要一个不同的子集方法,在这种情况下,它是@

#To use `DNAStringSet` function install Biostrings using the following
#source("http://www.bioconductor.org/biocLite.R") 
#biocLite(c("Biostrings"))
#library(Biostrings)

lapply(Biglist, function(x) x[x@ranges@NAMES %in% patterns])