无法找到函数“distance”的继承方法;对于签名“矩阵”、“字符”,但它是 df?

发布于 2025-01-11 09:09:14 字数 3981 浏览 0 评论 0原文

这是我根据 @Till 请求实现该函数的所有代码!

library("phyloseq")
library("qiime2R")
library("vegan")

# read in phyloseq objects from qiime
physeq<-qza_to_phyloseq(
  features="~/Documents/qiime2-analyses/CRD/fresh_run/table.qza",
  tree="~/Documents/qiime2-analyses/CRD/fresh_run/rooted-tree.qza",
  "~/Documents/qiime2-analyses/CRD/fresh_run/taxonomy.qza",
  metadata = "crd-metadata.txt")

# Clean out unwanted taxa annotations. Base script removes endozoicimonaceae, escherischia,
# and shigella contaminates
physeq <- subset_taxa(physeq, Family!="f__Endozoicimonaceae")
physeq <- subset_taxa(physeq, Family!="f__Enterobacteriaceae")
physeq <- subset_taxa(physeq, Family!="f__mitochondria")
physeq <- subset_taxa(physeq, Class!="c__Chloroplast")

#pull out otu table
otu_table <- (as.data.frame(otu_table(physeq)))

# rotate otu matrix layout
rownames(otu_table) <- factor(rownames(otu_table), levels = rownames(otu_table))
otu_ord <- as.data.frame(t(otu_table[rowSums(otu_table)!=0, ]))

# remove any rows or columns with only 0s
otu_ord <- otu_ord[, colSums(otu_ord !=0)>0]
otu_ord <- otu_ord[rowSums(otu_ord[])>0,]

#edits from observations of this StO chat
rownames(otu_ord) <- gsub("sample-", "", rownames(otu_ord))
rownames(otu_ord) <- as.numeric(rownames(otu_ord))
otu_ord <- as.matrix(otu_ord)

#the args of the function
raup_crick_abundance = function(spXsite=otu_ord, plot_names_in_col1=TRUE, 
                                classic_metric=FALSE, split_ties=TRUE, 
                                reps=9999, set_all_species_equal=FALSE, 
                                as.distance.matrix=TRUE, report_similarity=FALSE){

请注意,整个函数逐字来自 Stegen_etal_ISME_2013 github,链接如下和此处

我在此处链接的此函数中收到错误

    Error in (function (classes, fdef, mtable)  : 
      unable to find an inherited method for function ‘distance’ for signature ‘"matrix", "character"’
Called from: (function (classes, fdef, mtable) 
{
    methods <- .findInheritedMethods(classes, fdef, mtable)
    if (length(methods) == 1L) 
        return(methods[[1L]])
    else if (length(methods) == 0L) {
        cnames <- paste0("\"", vapply(classes, as.character, 
            ""), "\"", collapse = ", ")
        stop(gettextf("unable to find an inherited method for function %s for signature %s", 
            sQuote(fdef@generic), sQuote(cnames)), domain = NA)
    }
    else stop("Internal error in finding inherited methods; didn't return a unique method", 
        domain = NA)
})(list("matrix", "character"), new("nonstandardGenericFunction", 
    .Data = function (physeq, method, type = "samples", ...) 
    {
        standardGeneric("distance")
    }, generic = "distance", package = "phyloseq", group = list(), 
    valueClass = character(0), signature = c("physeq", "method", 
    "type"), default = NULL, skeleton = (function (physeq, method, 
        type = "samples", ...) 
    stop("invalid call in method dispatch to 'distance' (no default method)", 
        domain = NA))(physeq, method, type, ...)), <environment>)
Browse[1]> traceback()
No traceback available 

我的参数是一个没有字符串的data.frame(下面的dput())?据我了解,错误是说函数 distance () 不能用矩阵或字符串计算,我同意......

因此,我不确定为什么 distance () 无法操作使用我的参数,如果我正确地解释了错误。

谢谢。

这是我的 Qiime2 OTU table.qza 链接到此 Dropbox 文件,我的根树链接到此处,以及我的分类链接这里 重新创建我的 phyloseq 对象。

Here is all my code leading up to the function as per @Till request!

library("phyloseq")
library("qiime2R")
library("vegan")

# read in phyloseq objects from qiime
physeq<-qza_to_phyloseq(
  features="~/Documents/qiime2-analyses/CRD/fresh_run/table.qza",
  tree="~/Documents/qiime2-analyses/CRD/fresh_run/rooted-tree.qza",
  "~/Documents/qiime2-analyses/CRD/fresh_run/taxonomy.qza",
  metadata = "crd-metadata.txt")

# Clean out unwanted taxa annotations. Base script removes endozoicimonaceae, escherischia,
# and shigella contaminates
physeq <- subset_taxa(physeq, Family!="f__Endozoicimonaceae")
physeq <- subset_taxa(physeq, Family!="f__Enterobacteriaceae")
physeq <- subset_taxa(physeq, Family!="f__mitochondria")
physeq <- subset_taxa(physeq, Class!="c__Chloroplast")

#pull out otu table
otu_table <- (as.data.frame(otu_table(physeq)))

# rotate otu matrix layout
rownames(otu_table) <- factor(rownames(otu_table), levels = rownames(otu_table))
otu_ord <- as.data.frame(t(otu_table[rowSums(otu_table)!=0, ]))

# remove any rows or columns with only 0s
otu_ord <- otu_ord[, colSums(otu_ord !=0)>0]
otu_ord <- otu_ord[rowSums(otu_ord[])>0,]

#edits from observations of this StO chat
rownames(otu_ord) <- gsub("sample-", "", rownames(otu_ord))
rownames(otu_ord) <- as.numeric(rownames(otu_ord))
otu_ord <- as.matrix(otu_ord)

#the args of the function
raup_crick_abundance = function(spXsite=otu_ord, plot_names_in_col1=TRUE, 
                                classic_metric=FALSE, split_ties=TRUE, 
                                reps=9999, set_all_species_equal=FALSE, 
                                as.distance.matrix=TRUE, report_similarity=FALSE){

Please note the whole function is verbatim from Stegen_etal_ISME_2013 github linked below and here.

I am receiving the error

    Error in (function (classes, fdef, mtable)  : 
      unable to find an inherited method for function ‘distance’ for signature ‘"matrix", "character"’
Called from: (function (classes, fdef, mtable) 
{
    methods <- .findInheritedMethods(classes, fdef, mtable)
    if (length(methods) == 1L) 
        return(methods[[1L]])
    else if (length(methods) == 0L) {
        cnames <- paste0("\"", vapply(classes, as.character, 
            ""), "\"", collapse = ", ")
        stop(gettextf("unable to find an inherited method for function %s for signature %s", 
            sQuote(fdef@generic), sQuote(cnames)), domain = NA)
    }
    else stop("Internal error in finding inherited methods; didn't return a unique method", 
        domain = NA)
})(list("matrix", "character"), new("nonstandardGenericFunction", 
    .Data = function (physeq, method, type = "samples", ...) 
    {
        standardGeneric("distance")
    }, generic = "distance", package = "phyloseq", group = list(), 
    valueClass = character(0), signature = c("physeq", "method", 
    "type"), default = NULL, skeleton = (function (physeq, method, 
        type = "samples", ...) 
    stop("invalid call in method dispatch to 'distance' (no default method)", 
        domain = NA))(physeq, method, type, ...)), <environment>)
Browse[1]> traceback()
No traceback available 

within this function linked here.

My argument is a data.frame (dput() below) with no character strings? As I understand it, the error is saying the function distance () can't be calculated with matrix or character strings, which I agree with...

Therefore, I am unsure why distance () cannot operate with my arg, if I am interpreting the error correctly.

Thank you.

Here is my Qiime2 OTU table.qza linked to this Dropbox file, my rooted tree linked here, and my taxonomy linked here to recreate my phyloseq object.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2025-01-18 09:09:14

错误消息实际上试图传达的是您的矩阵具有字符值,而您调用的函数无法处理它。 data.frame 中的第一列是字符列。

如果您的数据框名为 ex_otu_ord,请尝试以下操作:

ex_otu_ord <- ex_otu_ord[-1]
ex_otu_ord <- as.matrix(ex_otu_ord)

然后再次尝试调用 ex_otu_ord 上的函数。

What the error message is actually trying to get across is that your matrix has character values and the function you called can not handle it. The first column in data.frame is a character column.

If your data frame is called ex_otu_ord, try this:

ex_otu_ord <- ex_otu_ord[-1]
ex_otu_ord <- as.matrix(ex_otu_ord)

Then try again to call the function on ex_otu_ord.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文