如何按名称对 DNAStringSet 对象进行子集化?

How can I subset a DNAStringSet object by name?

假设我有这个 DNAStringSet(作为例子)

dataset1

  A DNAStringSet instance of length 38874
        width seq names                  
    [1]  2617 GGC yellow
    [2]  4306 ACG blue
    [3]  1070 CTC red
    [4]  1870 CAC white
    [5]  3732 CAC brown
    ...   ... ...
[38870]   390 TGC black
[38871]  1970 CAG orange

我有一个包含其中一些序列名称的向量:

dataset2 <- c("blue","black","red","brown")

如何从 dataset1 中提取名称在 dataset2 中的那些序列?

DNAStringSet 对象可以按名称子集,就像使用方括号表示法的普通 R 列表一样:

afastafile <- DNAStringSet(c("GCAAATGGG", "CCCGGGTT", "AAAGGGTT", "TTTGGGCC")) 
names(afastafile) <- c("ABC1_1", "ABC2_1", "ABC3_1", "ABC4_1")

afastafile
  A DNAStringSet instance of length 4
    width seq                       names               
[1]     9 GCAAATGGG                 ABC1_1
[2]     8 CCCGGGTT                  ABC2_1
[3]     8 AAAGGGTT                  ABC3_1
[4]     8 TTTGGGCC                  ABC4_1

nms <- c('ABC1_1', 'ABC3_1')
afastafile[nms]

  A DNAStringSet instance of length 2
    width seq                        names               
[1]     9 GCAAATGGG                  ABC1_1
[2]     8 AAAGGGTT                   ABC3_1

在你的情况下,你只需 dataset1[dataset2]