将参数传递给R Phyloseqsubset_taxa包装器

时间:2019-05-03 14:32:08

标签: r subset phyloseq

我将解释最终目标,以及我作为测试所要尝试的。 (因为我可能会以错误的方式进行操作。)

我正在使用phyloseq软件包可视化微生物组数据。我想通过让用户选择分析级别并使我的脚本生成可视化效果而在某种程度上“自动化”它,而无需人工输入每种组合。

问题是将变量传递给子集函数。我主要得到这些错误(取决于我尝试过的paste0,eval,parse,as.ologic,expression,noquote .... etc的组合):

Error in subset.data.frame(oldDF, ...) : 'subset' must be logical
Error in dimnames(x) <- dn : 
  length of 'dimnames' [1] not equal to array extent

用户可以设置分析级别。因此,现在说有两个级别,自动选择第二个级别也意味着您也想要第一个级别。 (我还没有从事这一部分的工作,但是我想提前解释一下。

#Set lineage level
lin_level <- 1
lin_list <- c("k__Kingdom", "p__Phylum","c__Class", "o__Order","f__Family")
lin_select <- lin_list[lin_level]
sub_lin <- lin_list[(lin_level +1)]

#Kingdom
king_list <- "k__Bacteria"

#set Phylum list
if (lin_select == "p__Phylum"){
phylum_list <- c("p__Firmicutes","p__Proteobacteria","p__Bacteroidetes","p__Actinobacteria","p__Tenericutes")
}

subgroup <- "All"

从这里开始,脚本最终将到达绘图部分。如果lin_level设置为1,则如下所示:

FIXED
gphic = subset_taxa(physeq1, Kingdom=="k__Bacteria")
title = paste0(subgroup," ", "Bacteria-only")
plot_bar(gpsfb, "Phylum", "Abundance", "Phylum", 
         title=title, facet_grid="Type~.")

AUTOMATED
gphic = subset_taxa(physeq1, (substring(lin_select,4)) == king_list)
title = paste0(subgroup," ", (substring(king_list,4)),"-only")
plot_bar(gpsfb, (substring(sub_lin,4)), "Abundance", (substring(sub_lin,4)), 
         title=title, facet_grid="Type~.")

但是,尝试将(substring(lin_select,4))== king_list用作参数会导致错误。

我已经搜索了有关此问题的各种线索,但未能获得不同的答案。最终,我需要为Kingdom(王国)运行一次制图部分,然后为Phylum列表中的每个项目再次运行。但是在到达那里之前,我需要能够将参数传递给子集函数。

我尝试过的事情:

test <- paste0(substring(lin_select,4),"==","\"","p__Bacteroidetes","\"")
noquote(test)
[1] Phylum=="p__Bacteroidetes"

gphic = subset_taxa(physeq1, noquote(test))
Error in subset.data.frame(oldDF, ...) : 'subset' must be logical

gphic = subset_taxa(physeq1, paste0(substring(lin_select,4),"==","\"","p__Bacteroidetes","\""))
Error in subset.data.frame(oldDF, ...) : 'subset' must be logical

gphic = subset_taxa(physeq1, as.logical(test))
Error in dimnames(x) <- dn : 
  length of 'dimnames' [1] not equal to array extent

as.logical(noquote(test))
[1] NA

gphic = subset_taxa(physeq1, as.logical(noquote(test)))
Error in dimnames(x) <- dn : 
  length of 'dimnames' [1] not equal to array extent

noquote(test)
[1] Phylum=="p__Bacteroidetes"

as.logical(noquote(test))
[1] NA

as.logical(as.character(noquote(test)))
[1] NA

test2 <- eval(parse(text= test))
Error in eval(parse(text = test)) : object 'Phylum' not found

test2 <- eval(test)
gphic = subset_taxa(physeq1, as.logical(test2))
Error in dimnames(x) <- dn : 
  length of 'dimnames' [1] not equal to array extent

as.logical(test2)
[1] NA

还有许多其他排列尝试用不同的方式进行细分的方法,但您明白了。

1 个答案:

答案 0 :(得分:0)

gphic = subset_taxa(physeq1,  eval(as.name(level_tax)) == king_list)

在这里, level_tax 是循环中的变量。说 level_tax =“ Order”,然后通过 as.name (level_tax)或 as.symbol 将字符串“ Order”转换为变量名(level_tax)。然后,我们使用 eval(),它接受一个表达式并在指定的环境中求值