rape-phylo

unable to find an inherited method for function ‘UniFrac’ for signature ‘"phylo"’


library(tidyverse)
library(ape)
library(phyloseq)

unifrac_unweighted <- UniFrac(tree, weighted = F)

Fehler in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘UniFrac’ for signature ‘"phylo"’
3.
stop(gettextf("unable to find an inherited method for function %s for signature %s", sQuote(fdef@generic), sQuote(cnames)), domain = NA)
2.
(function (classes, fdef, mtable) { methods <- .findInheritedMethods(classes, fdef, mtable) if (length(methods) == 1L) ...
1.
UniFrac(tree_BLS, weighted = T)

tree is a phylogenetic tree (proved with class(tree) ). I already tried to reinstall phyloseq but it does not help. How to avoid this error?


Solution

  • The UniFrac function requirs a phyloseq object:

    physeq: (Required). ‘phyloseq-class’, containing at minimum a
              phylogenetic tree (‘phylo-class’) and contingency table
              (‘otu_table-class’). See examples below for coercions that
              might be necessary
    

    Not very sure if it makes sense for you, but maybe consider the below example. The key is to construct the phyloseq object correctly, one part I am not sure of is how to construct the otu_table, I set taxa_are_row=FALSE :

    #you get a tree
    sample_tree = rtree(20,tip.label=letters[1:20])
    
    # there are counts associated with each sample in tree
    sample_counts = matrix(rnbinom(100*20,mu=20,size=1),
    ncol=20,dimnames=list(1:100,letters[1:20]))
    
    x1 = phyloseq(
    otu_table(sample_counts,taxa_are_row=FALSE), 
    sample_tree)
    
    UniFrac(x1)