Search code examples
rdataframeimportphyloseq

Converting existing data in R into Phyloseq OTU table


I have been attempting to "phyloseq-ize" my asv_table, asv_id, and metadata for a 16S analysis, created using qiime2 and uploaded to R using read.table(). I have been able to successfully import my asv_id and metadata (using tax_table() and sample_data() respectively), but I'm struggling with my asv_table.

The structure of my asv_table is a data frame containing a header of my taxa, and my rows are samples (I have also attempted this with transposed data where my taxa are rows and my samples are columns). My initial attempt was simply:

ps <- phyloseq(otu_table(asv_table, taxa_are_rows = FALSE))

yet this yielded:

Error in validObject(.Object) : invalid class “otu_table” object: 
 Non-numeric matrix provided as OTU table.
Abundance is expected to be numeric.

This led me to attempt to change the class of my data to numeric, and try importing again:

asv_table_num <- data.matrix(asv_table, rownames.force = NA)

ps <- phyloseq(otu_table(otumat_num, taxa_are_rows = FALSE))

Error in validObject(.Object) : invalid class “phyloseq” object: 
 Component taxa/OTU names do not match.
 Taxa indices are critical to analysis.
 Try taxa_names()

I'm worried this is heading down a rabbit hole as a simpler solution stares me in the face (I'm having a hard time researching my issue on the internet... usually indicative of me creating a unique issue no one else has encountered).

I would appreciate any guidance or help on the topic!

Thank you in advance.


Solution

  • I was able to figure my issue out eventually, so I'm going to post my solution for anyone out there who may be having the same problem.

    library(tidyverse)
    devtools::install_github("jbisanz/qiime2R")
    library(qiime2R)
    
    asv_table <- read_qza("table.qza")
    
    library(phyloseq)
    
    ps <- qza_to_phyloseq(features = "table.qza")