TITLE: BIT 577 QIIME2 Tutorial
AUTHORS: Isabella Livingston

[DESCRIPTION]
Command line scripts for processing and analyzing 16S amplicon data using QIIME2.

[STEPS]
1. Download conda through the QIIME2 website or miniconda website. After installation, download and install QIIME2

2. Activate Qiime environment:

conda activate qiime2-amplicon-2023.9 

# change out to whatever version has been downloaded
3. Import data 

qiime tools import \
  --type 'SampleData[SequencesWithQuality]' \
  --input-path
/path/to/your/sequencefiles \
  --input-format CasavaOneEightSingleLanePerSampleDirFmt \
  --output-path demux-single-end.qza
4. Check quality 

qiime demux summarize --p-n 10000 --i-data demux-single-end.qza --o-visualization qual_viz 
 
qiime tools view qual_viz.qzv       
5. Denoise with DADA2

qiime dada2 denoise-single \
--i-demultiplexed-seqs demux-single-end.qza \
--p-trunc-len 200 \  #at what position you should trim your data based on the demux summary above 
--o-table table.qza \ #ASV table
--o-representative-sequences rep-sets.qza \
--o-denoising-stats denoising-stats.qza
6. Filter Sequence Table 

qiime feature-table summarize \
> --i-table table.qza \
> --o-visualization vis_table
 
qiime tools view vis_table.qzv
7. Align and Construct Phylogenetic Tree

qiime phylogeny align-to-tree-mafft-fasttree \    # program used for construction
--i-sequences rep-sets.qza \
--o-alignment aligned-rep-seqs.qza \
--o-masked-alignment masked-aligned-rep-seqs.qza \
--o-tree unrooted-tree.qza \
--o-rooted-tree rooted-tree.qza
8. Compute diversity measures (see QIIME2 for differences in alpha, beta, and emperor plots)

qiime diversity core-metrics-phylogenetic --i-table
table.qza --i-phylogeny rooted-tree.qza --p-sampling-depth 29761
--m-metadata-file /Users/isabellalivingston/Desktop/16S Sequence
Data/GSL_metadata.txt --o-rarefied-table rare_featuretable --o-faith-pd-vector
faithpd_vector --o-observed-features-vector observed_sample_features_vector
--o-shannon-vector shannon_sample_vector --o-evenness-vector
evenness_sample_vector --o-unweighted-unifrac-distance-matrix
unweighted_distance_matrix --o-weighted-unifrac-distance-matrix
weighted_distance_matrix --o-jaccard-distance-matrix jaccard_matrix
--o-bray-curtis-distance-matrix bc_matrix --o-unweighted-unifrac-pcoa-results
unweighted_pcoa --o-weighted-unifrac-pcoa-results weighted_pcoa
--o-jaccard-pcoa-results jaccard_pcoa --o-bray-curtis-pcoa-results bc_pcoa
--o-unweighted-unifrac-emperor unweighted_emperor --o-weighted-unifrac-emperor
weighted_emperor --o-jaccard-emperor jaccard_emperor --o-bray-curtis-emperor
bc_emperor
9. Test for differences in diversity using metadata

# Alpha Diversity

qiime diversity alpha-group-significance --i-alpha-diversity faithpd_vector.qza 
--m-metadata-file /path/to/metadata/txt file
--o-visualization faith-pd-group-significance.qzv
 
qiime diversity alpha-group-significance --i-alpha-diversity
evenness_sample_vector.qza --m-metadata-file
/Users/isabellalivingston/Desktop/16S\ Sequence\ Data/GSL_metadata.txt
--o-visualization evenness-group-significance.qzv
 
qiime diversity alpha-group-significance --i-alpha-diversity
shannon_sample_vector.qza --m-metadata-file
/Users/isabellalivingston/Desktop/Metadata\ for\ GSL.tsv --o-visualization
alpha_diversity
 
# Emperor plots 
qiime emperor plot --i-pcoa unweighted_pcoa.qza
--m-metadata-file /Users/isabellalivingston/Desktop/16S\ Sequence\
Data/GSL_metadata.txt --o-visualization unweighted-unifrac-emperor-sex.qzv

qiime emperor plot --i-pcoa bc_pcoa.qza --m-metadata-file
/Users/isabellalivingston/Desktop/16S\ Sequence\ Data/GSL_metadata.txt
--p-custom-axes sex --o-visualization bc-sex-sex.qzv

# Beta diversity  
qiime diversity beta-group-significance --i-distance-matrix
unweighted_distance_matrix.qza --m-metadata-file
/Users/isabellalivingston/Desktop/16S\ Sequence\ Data/GSL_metadata.txt
--m-metadata-column sex --o-visualization unweighted-unifrac-sex-significance.qzv
--p-pairwise
 

10. Taxonomic Classification 

wget https://data.qiime2.org/2018.2/common/gg-13-8-99-515-806-nb-classifier.qza #download classifier of choice
 
qiime feature-classifier classify-sklearn \
  --i-classifier /path/to/classifier.qza \
  --i-reads rep-sets.qza \
  --o-classification {TAXONOMY}.qza
 
qiime metadata tabulate \
--m-input-file {TAXONOMY}.qza --o-visualization {TAXONOMY_VIZ}.qzv
 
qiime taxa barplot \
  --i-table table.qza
\
  --i-taxonomy
{TAXONOMY}.qza \
  --m-metadata-file
/path/to/etadata/txtfile\
  --o-visualization
{TAXA-BAR-PLOTS_VIZ}.qzv
 
qiime tools view {TAXA-BAR-PLOTS_VIZ}.qzv
 
11. Export Data

Qiime tools export \
--input-path taxtable.qza \
--output-path exported-taxa-table
 
# Should get a confirmation like this: Exported taxtable.qza as BIOMV210DirFmt to directory
exported-taxa-table

# convert from biom to tsv for future analysis

Biom convert -i
/path/to/biom/file -o table.tsv
--to-tsv