Qiime2 to Phyloseq

The purpose of this post is to demostrate a workflow to convert Qiime2 objects for use in Phyloseq within an R environment. All the data and scripts can be found at my Github \\ Qiime2_to_Phyloseq

Purpose

This script details the steps to convert qiime2 objects into a Phyloseq object. The workflow of processing data with Qiime2 can be found at the Moving Pictures tutorial. This script is adapted from Pedro J. Torres. All the data and scripts can be found at my Github

Requirements

Qiime2 artifacts needed to convert for phyloseq analysis:

  • Metadata/Mapping File
  • Table File (.qza)
  • Taxonomy File (.qza)
  • Tree File (.qza) {Optional}

Convert Qiime2 qza on Command Line

The chunk below contains commands executed on the command line. Qiime should be installed and in your path. Otherwise the commands can be copied and pasted into the terminal running a qiime environment.

system('mkdir -p Phyloseq')
#  mkdir -p Phyloseq

# Export table
system("qiime tools export 'Demo_Data/table.qza' --output-dir 'Phyloseq/'")
#  qiime tools export 'Demo_Data/table.qza' --output-dir 'Phyloseq/'

# Export taxonomy
system("qiime tools export Demo_Data/taxonomy.qza --output-dir Phyloseq/")
#  qiime tools export Demo_Data/taxonomy.qza --output-dir Phyloseq/

# Export tree
system("qiime tools export Demo_Data/unrooted-tree.qza --output-dir Phyloseq/")
#  qiime tools export Demo_Data/unrooted-tree.qza --output-dir Phyloseq/

The header of the taxonomy file must be converted to match proper format for merging with biom file

system("sed 's/Feature ID/#OTUID/' Phyloseq/taxonomy.tsv | sed 's/Taxon/taxonomy/' | sed 's/Confidence/confidence/' > Phyloseq/biom-taxonomy.tsv")

## Remove redundant file
system("rm Phyloseq/taxonomy.tsv")

Merge taxonomy with biom file. Qiime in path is also required for this command. The command can be copied and pasted into a terminal with a qiime environment

system("biom add-metadata \
    -i Phyloseq/feature-table.biom \
    -o Phyloseq/taxa_table.biom \
    --observation-metadata-fp Phyloseq/biom-taxonomy.tsv \
    --sc-separated taxonomy")

#  biom add-metadata -i Phyloseq/feature-table.biom -o Phyloseq/taxa_table.biom --observation-metadata-fp Phyloseq/biom-taxonomy.tsv --sc-separated taxonomy

Load in Qiime Data

Now the data is ready to use in Phyloseq! Below we load in the biom table, tree file (optional), and mapping file. Then we combine them into a phyloseq object. There is a warning that can be ignored upon importing the biom table. Tutorial for processing Phyloseq objects can be found here.

## Install Phyloseq try http:// if https:// URLs are not supported
#source("https://bioconductor.org/biocLite.R")
# biocLite("phyloseq")

library(phyloseq)

biom_data <- import_biom(BIOMfilename = "Phyloseq/taxa_table.biom", treefilename = "Phyloseq/tree.nwk")
## Warning in strsplit(msg, "\n"): input string 1 is invalid in this locale
## The mapping file is still in the demo data as we did not operate on it
mapping_file <- import_qiime_sample_data(mapfilename = "Demo_Data/sample-metadata.tsv")

# Merge the OTU and mapping data into a phyloseq object
phylo <- merge_phyloseq(biom_data, mapping_file)
#Add names to biom table and check phyloseq objects
colnames(tax_table(phylo))= c("Kingdom","Phylum","Class","Order","Family","Genus", "Species")
rank_names(phylo)
## [1] "Kingdom" "Phylum"  "Class"   "Order"   "Family"  "Genus"   "Species"
depths <- sample_sums(phylo)
depths
## L1S105 L1S140 L1S208 L1S257 L1S281  L1S57  L1S76   L1S8 L2S155 L2S175 
##   7865   7245   8270   6486   6755   8756   7922   7068   4112   4545 
## L2S204 L2S222 L2S240 L2S309 L2S357 L2S382 L3S242 L3S294 L3S313 L3S341 
##   3340   3485   5146   1549   2526   4166    917   1313   1191   1109 
## L3S360 L3S378 L4S112 L4S137  L4S63 L5S104 L5S155 L5S174 L5S203 L5S222 
##   1130   1279   8575   9961  10095   2253   1827   1969   2132   2555 
## L5S240  L6S20  L6S68  L6S93 
##   1817   6892   6022   7025

And your all set! Good luck processing that data!

Comments

Popular posts from this blog

Time to Make the Switch

RpoB Processing