Skip to content

Instantly share code, notes, and snippets.

@erictleung
Created March 16, 2018 22:46
Show Gist options
  • Save erictleung/eda33bceceebb190eb9a44c93a077d32 to your computer and use it in GitHub Desktop.
Save erictleung/eda33bceceebb190eb9a44c93a077d32 to your computer and use it in GitHub Desktop.
# ---
# 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
# ---
# 1 Export OTU table
# - table-no-mitochondria-no-chloroplast.qza replace with your file
# - phyloseq => replace with where you'd like to output directory
qiime tools export \
table-no-mitochondria-no-chloroplast.qza \
--output-dir 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 \
taxonomy.qza \
--output-dir phyloseq
# Manually change "feature ID" to "OTUID"
# 3 Export phylogenetic tree
qiime tools export \
unrooted-tree.qza \
--output-dir phyloseq
# 4 Merge files
# Filtered sequences will make taxonomy and OTU tables have different lengths
# ---
# title: Manipulate QIIME2 output in R
# description: |
# Take in output from 1_qiime_part.sh and manipulate files in R
# ---
# Setup environment
library(here)
# Read in OTU table
in_path <- here("phyloseq", "otu_table.txt")
otu <- read.table(file = in_path, header = TRUE)
head(otu)
# Read in taxonomy table
tax <- read.table(Sile = "taxonomy.tsv", sep = '\t', header = TRUE)
head(tax)
# Merge files
merged_file <- merge(otu, tax, by.x = c("OTUID"), by.y = c("OTUID"))
head(merged_file)
# 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
out_path <- here("phyloseq", "combined_otu_tax.tsv")
write.table(merged_file, file = out_path, 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
@kediza
Copy link

kediza commented Oct 26, 2018

Hi

awesome script, please assist in this I ran merged_file <- merge(otu, tax, by.x = c("OTUID"), by.y = c("OTUID")) and got the error
Error in fix.by(by.x, x) : 'by' must specify a uniquely valid column, please help

@LinaMaMar
Copy link

Hi, thank you so much for the script. I am getting errors when I try to import the files as a phyloseq object
For otu_table, I am getting:
Error in validObject(.Object) : invalid class “otu_table” object:
OTU abundance data must have non-zero dimensions.

For taxonomy table, I am getting:
Error in dimnames(x) <- dn :
length of 'dimnames' [2] not equal to array extent

Could you help me with this, please?

Thank you so much

@adcerv
Copy link

adcerv commented Apr 3, 2019

Hi, thank you so much for the script. I am getting errors when I try to import the files as a phyloseq object
For otu_table, I am getting:
Error in validObject(.Object) : invalid class “otu_table” object:
OTU abundance data must have non-zero dimensions.

For taxonomy table, I am getting:
Error in dimnames(x) <- dn :
length of 'dimnames' [2] not equal to array extent

Could you help me with this, please?

Thank you so much

Hello, LinaMaMar!
Me too I see some errors. I have some solutions for example.

library(here)
in_path<- here("otu_table.txt")
otu <- read.table(file = in_path, header = TRUE)
head(otu)

tax<- read.table (file="taxonomy.tsv" ,sep='\t', header = TRUE)
head(tax)

merged_file<- merge(otu,tax,by.x= c("OTUID"), by.y= c("OTUID"))
head(merged_file)

out_path<-here("combined_otu_tax.tvs")
write.table(merged_file, file = out_path, sep = "\t", col.names = TRUE, row.names = FALSE)

I hope it helps you.
Regards.

@nataliagaeta29
Copy link

Hello!

I am trying to import the files from qiime2 ("moving pictures tutorial") to the phyloseq, but, during the step ## Import all as phyloseq objects, I got the error messages below:

  1. For: OTU <- otu_table(otu, taxa_are_rows = TRUE) --> I got this message: Error in validObject(.Object) : invalid class “otu_table” object: OTU abundance data must have non-zero dimensions.

  2. For TAX <- tax_table(taxonomy) --> I got this message: Error in dimnames(x) <- dn : length of 'dimnames' [2] not equal to array extent

All the previous steps worked. But I am stucked now.
Could you help me please?

Thank you for your attention

Regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment