#!/usr/bin/env bash
###############################################################################
# Auto-generated by provenance_lib v.0.2.0 at 03:24:23 PM on 17 Aug, 2022

# This document is a representation of the scholarly work of the creator of the
# QIIME 2 Results provided as input to provenance_lib, and may be protected by
# intellectual property law. Please respect all copyright restrictions and
# licenses governing the use, modification, and redistribution of this work.

# For User Support, post to the Community Plugin Support channel of the QIIME 2
# Forum: https://forum.qiime2.org
# Documentation/issues: https://github.com/qiime2/provenance_lib

# UUIDs of all target QIIME 2 Results are shown at the end of the file

# Instructions for use:
# 1. Open this script in a text editor or IDE. Support for BASH
#    syntax highlighting can be helpful.
# 2. Search or scan visually for '<' or '>' characters to find places where
#    user input (e.g. a filepath or column name) is required. These must be
#    replaced with your own values. E.g. <column name> -> 'patient_id'.
#    Failure to remove '<' or '>' may result in `No such File ...` errors
# 3. Search for 'FIXME' comments in the script, and respond as directed.
# 4. Remove all 'FIXME' comments from the script completely. Failure to do so
#    may result in 'Missing Option' errors
# 5. Adjust the arguments to the commands below to suit your data and metadata.
#    If your data is not identical to that in the replayed analysis,
#    changes may be required. (e.g. sample ids or rarefaction depth)
# 6. Optional: replace any filenames in this script that begin with 'XX' with
#    unique file names to ensure they are preserved. QIIME 2 saves all outputs
#    from all actions in this script to disk regardless of whether those
#    outputs were in the original collection of replayed results. The filenames
#    of "un-replayed" artifacts are prefixed with 'XX' so they may be easily
#    located. These names are not guaranteed to be unique, so 'XX_table.qza'
#    may be overwritten by another 'XX_table.qza' later in the script.
# 7. Activate your replay conda environment, and confirm you have installed all
#    plugins used by the script.
# 8. Run this script with `bash <path to this script>`, or copy-paste commands
#    into the terminal for a more interactive analysis.
# 9. Optional: to delete all results not required to produce the figures and
#    data used to generate this script, navigate to the directory in which you
#    ran the script and `rm XX*.qz*`
###############################################################################

# This tells bash to -e exit immediately if a command fails
# and -x show all commands in stdout so you can track progress
set -e -x

## Prepare the reference data and tree

qiime tools import \
  --type 'FeatureData[Sequence]' \
  --input-path <your data here> \
  --output-path feature-data-sequence-0.qza

qiime tools import \
  --type 'Phylogeny[Unrooted]' \
  --input-path <your data here> \
  --output-path phylogeny-unrooted-0.qza

qiime phylogeny midpoint-root \
  --i-tree phylogeny-unrooted-0.qza \
  --o-rooted-tree rooted-tree-0.qza

## Analyze 454 data

qiime tools import \
  --type 'SampleData[SequencesWithQuality]' \
  --input-path <your data here> \
  --output-path sample-data-sequences-with-quality-0.qza

qiime dada2 denoise-pyro \
  --i-demultiplexed-seqs sample-data-sequences-with-quality-0.qza \
  --p-trunc-len 150 \
  --p-trim-left 16 \
  --p-max-ee 2.0 \
  --p-trunc-q 2 \
  --p-max-len 600 \
  --p-pooling-method independent \
  --p-chimera-method consensus \
  --p-min-fold-parent-over-abundance 1.0 \
  --p-n-threads 0 \
  --p-n-reads-learn 250000 \
  --p-hashed-feature-ids \
  --o-table table-0.qza \
  --o-representative-sequences representative-sequences-0.qza \
  --o-denoising-stats XX_denoising_stats

qiime vsearch cluster-features-closed-reference \
  --i-sequences representative-sequences-0.qza \
  --i-table table-0.qza \
  --i-reference-sequences feature-data-sequence-0.qza \
  --p-perc-identity 0.97 \
  --p-strand both \
  --p-threads 8 \
  --o-clustered-table clustered-table-0.qza \
  --o-clustered-sequences XX_clustered_sequences \
  --o-unmatched-sequences XX_unmatched_sequences

qiime feature-table filter-samples \
  --i-table clustered-table-0.qza \
  --p-min-frequency 0 \
  --p-max-frequency 93500 \
  --p-min-features 0 \
  --p-no-exclude-ids \
  --p-filter-empty-features \
  --o-filtered-table filtered-table-0.qza

# Replay attempts to represent metadata inputs accurately, but metadata .tsv
# files are merged automatically by some interfaces, rendering distinctions
# between file inputs invisible in provenance. We output the recorded
# metadata to disk to enable visual inspection.

# The following command may have received additional metadata .tsv files. To
# confirm you have covered your metadata needs adequately, review the
# original metadata, saved at
# './recorded_metadata/diversity_core_metrics_phylogenetic_0/'

qiime diversity core-metrics-phylogenetic \
  --i-table filtered-table-0.qza \
  --i-phylogeny rooted-tree-0.qza \
  --p-sampling-depth 4000 \
  --m-metadata-file <your metadata filepath> \
  --p-no-with-replacement \
  --p-n-jobs-or-threads 8 \
  --o-unweighted-unifrac-emperor unweighted-unifrac-emperor-0.qzv \
  --o-rarefied-table XX_rarefied_table \
  --o-faith-pd-vector XX_faith_pd_vector \
  --o-observed-features-vector XX_observed_features_vector \
  --o-shannon-vector XX_shannon_vector \
  --o-evenness-vector XX_evenness_vector \
  --o-unweighted-unifrac-distance-matrix XX_unweighted_unifrac_distance_matrix \
  --o-weighted-unifrac-distance-matrix XX_weighted_unifrac_distance_matrix \
  --o-jaccard-distance-matrix XX_jaccard_distance_matrix \
  --o-bray-curtis-distance-matrix XX_bray_curtis_distance_matrix \
  --o-unweighted-unifrac-pcoa-results XX_unweighted_unifrac_pcoa_results \
  --o-weighted-unifrac-pcoa-results XX_weighted_unifrac_pcoa_results \
  --o-jaccard-pcoa-results XX_jaccard_pcoa_results \
  --o-bray-curtis-pcoa-results XX_bray_curtis_pcoa_results \
  --o-weighted-unifrac-emperor XX_weighted_unifrac_emperor \
  --o-jaccard-emperor XX_jaccard_emperor \
  --o-bray-curtis-emperor XX_bray_curtis_emperor


###############################################################################
# The following QIIME 2 Results were parsed to produce this script:
# 787a817d-05ea-49a9-b19a-a15edc57c837
###############################################################################
