Get fully annotated `.biom` tables from Qiime 2 to Qiime 1 (and e.g. Phyloseq)

Dear all,

I thought I share my bash script solution to get fully annotated .biom tables from Qiime 2 to Qiime 1 (and e.g. Phyloseq - including tree, sequences and metadata). You will still need to adjust this script, but it might save you some time. YOU MUST EDIT THIS SCRIPT PRIOR TO USE - AND PLEASE DO NOT RUN WITHOUT UNDERSTANDING WHAT IT DOES Kind regards, Paul

#!/bin/bash

========================================================
# Converting Qiime 2 qza artifacts to  Qiime 1 biom files. 
# Paths will nee adjustments.
#  DO NOT RUN WITHOUT EDITING ACCORDING TO YOUR NEEDS!!!!! 

# For debugging only
# ------------------ 
set -x

# Paths can be adjusted for remote execution
# ----------------------------------------------
if [[ "$HOSTNAME" != "foo" ]]; then
    printf "Execution on remote...\n"
    trpth="/data/CU_combined"
    qiime() { qiime2cli "$@"; }
    cores="$(nproc --all)"
elif [[ "$HOSTNAME" == "foo" ]]; then
    printf "Execution on local...\n"
    trpth="$(dirname "$PWD")" # script directory
    cores='2'
fi

# Define input files - expand array at your convenience
# ----------------------------------------------------------------------

clust_tab[1]='Zenodo/Qiime/500_18S_100_cl_tab.qza'

clust_seq[1]='Zenodo/Qiime/500_18S_100_cl_seq.qza'

clust_tax[1]='Zenodo/Qiime/510_18S_100_cl_tax.qza'

mptpth='Zenodo/Qiime/100_18S_tree_mdp_root.qza'

mappng='Zenodo/Manifest/05_18S_merged_metadata.tsv'

# Define output files files
# -------------------------

clust_exp[1]='Zenodo/Qiime/520_18S_100_cl_q1exp'

ottre="520_18S_tree_midpoint_root.tre"

# Looping over array - ADJUST i !
# -------------------------

for ((i=1;i<=1;i++)); do
   
   printf "Exporting Qiime 2 files at $(date +"%T")...\n"
   qiime tools export "$trpth"/"${clust_tab[$i]}" --output-dir "$trpth"/"${clust_exp[$i]}" && \
   qiime tools export "$trpth"/"${clust_seq[$i]}" --output-dir "$trpth"/"${clust_exp[$i]}" && \
   qiime tools export "$trpth"/"${clust_tax[$i]}" --output-dir "$trpth"/"${clust_exp[$i]}" && \
   unzip -p "$trpth"/"$mptpth" > "$trpth"/"${clust_exp[$i]}"/"$ottre" || { echo 'export failed' ; exit 1; }
   
   printf "Modifying taxonomy file to match exported feature table at $(date +"%T") ...\n" && \
   new_header='#OTUID  taxonomy    confidence' && \
   sed -i.bak "1 s/^.*$/$new_header/" "$trpth"/"${clust_exp[$i]}"/taxonomy.tsv || { echo 'edit failed' ; exit 1; }
   
   printf "Adding taxonomy information to .biom file at $(date +"%T") ...\n"
   biom add-metadata \
     -i "$trpth"/"${clust_exp[$i]}"/feature-table.biom \
     -o "$trpth"/"${clust_exp[$i]}"/features-tax.biom \
     --observation-metadata-fp "$trpth"/"${clust_exp[$i]}"/taxonomy.tsv \
     --observation-header OTUID,taxonomy,confidence \
     --sc-separated taxonomy || { echo 'taxonomy addition failed' ; exit 1; }
   
   printf "Adding metadata information to .biom file at $(date +"%T")...\n"
   biom add-metadata \
     -i "$trpth"/"${clust_exp[$i]}"/features-tax.biom \
     -o "$trpth"/"${clust_exp[$i]}"/features-tax-meta.biom \
     --sample-metadata-fp "$trpth"/"$mappng" \
     --observation-header OTUID,taxonomy,confidence || { echo 'metadata addition failed' ; exit 1; }
done
7 Likes

I just want to let you know that I was able to run this with success after modification of your code. Thank you very much. @Paul_Czechowski

1 Like

Update for qiime 2018.11 - slightly modify the last call in the last script section:

printf "Adding metadata information to .biom file at $(date +"%T")...\n"
  biom add-metadata \
  -i "$trpth"/"$biom_dir"/features-tax.biom \
  -o "$trpth"/"$biom_dir"/features-tax-meta.biom \
  -m "$trpth"/"$map_file" \
  --observation-header OTUID,taxonomy,confidence \
  --sample-header SampleID,BarcodeSequence,LinkerPrimerSequence,Port,Location,Type,Temp,Sali,Lati,Long,Run,Facility,CollYear || { echo 'metadata addition failed' ; exit 1; }
1 Like