Skip to content

Instantly share code, notes, and snippets.

@MADscientist314
Forked from erictleung/1_qiime_part.sh
Last active October 16, 2019 15:44
Show Gist options
  • Save MADscientist314/172104a8137ea4b07118fe91bebdf9d4 to your computer and use it in GitHub Desktop.
Save MADscientist314/172104a8137ea4b07118fe91bebdf9d4 to your computer and use it in GitHub Desktop.
Updated QIIME2 to Phyloseq Code for qiime2-2019.7 version syntax. Orignally derived from https://github.com/joey711/phyloseq/issues/821#issuecomment-371701469
# ---
# title: Export QIIME2 OTU table to compatible file for phyloseq
# description: |
# Three main steps to get to compatible file to import to phyloseq
#
# Outline:
# 1. Export OTU table
# 2. Export taxonomy table
# 3. Export phylogenetic tree
# ---
# 0 Make a directory called phyloseq
mkdir ./phyloseq
# 1 Export OTU table
# - tablet.qza replace with your file
# - phyloseq => optionally replace with where you'd like to output directory
qiime tools export --input-path table.qza --output-path phyloseq
# OTU tables exports as feature-table.biom so convert to .tsv
# - Change -i and -o paths accordingly
biom convert -i phyloseq/feature-table.biom -o phyloseq/otu_table.txt --to-tsv
# Manually change #OTUID to OTUID
# 2 Export taxonomy table
qiime tools export --input-path taxonomy.qza --output-path phyloseq
# Manually change feature ID to OTUID
# 3 Export phylogenetic tree
qiime tools export --input-path unrooted-tree.qza --output-path phyloseq
# Setup environment
library(phyloseq)
library(ggplot2)
library(ape)
setwd("./phyloseq/")
# title: Manipulate QIIME2 output in R
# description: |
# Take in output from 1_qiime_part.sh and manipulate files in R
# ---
# Read in OTU table
otu <- read.table(file = "otu_table.txt", header = F, row.names = 1)
# Read in taxonomy table
tax <- read.table(file = "taxonomy.tsv", sep = '\t', header = TRUE)
# Merge files
merged_file <- merge(otu, tax, by.x = c(0), by.y = c("Feature.ID"),)
# Note: the number of rows should equal your shortest file length, drops taxonomy
# for OTUs that don't exist in your OTU table
# Output merged .txt file
write.table(merged_file, file = "combined_otu_tax.tsv", sep = "\t", col.names = TRUE, row.names = FALSE)
# Setup environment
library(phyloseq)
library(ggplot2)
library(ape)
# Read in OTU table
otu_table_in <- read.csv("otu_matrix.csv", sep = ",", row.names = 1)
otu_table_in <- as.matrix(otu_table_in)
# Read in taxonomy
# Separated by kingdom, phylum, class, order, family, genus, species
taxonomy <- read.csv("taxonomy.csv", sep = ",", row.names = 1)
taxonomy <- as.matrix(taxonomy)
# Read in metadata
metadata <- read.table("metadata.txt", row.names = 1)
# Read in tree
phy_tree <- read_tree("tree.nwk")
# Import all as phyloseq objects
OTU <- otu_table(otu_table_in, taxa_are_rows = TRUE)
TAX <- tax_table(taxonomy)
META <- sample_data(metadata)
# Sanity checks for consistent OTU names
taxa_names(TAX)
taxa_names(OTU)
taxa_names(phy_tree)
# Same sample names
sample_names(OTU)
sample_names(META)
# Finally merge!
ps <- phyloseq(OTU, TAX, META, phy_tree)
ps
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment