#!/bin/bash

##############################################################################
###############                                                ###############
###############   QIIME2 analysis of combined 16S V3-V4 data   ###############
###############                                                ###############
##############################################################################

## activate conda environment with QIIME2
eval "$(conda shell.bash hook)"
conda activate qiime2-2021.8


#----------------------------  QIIME2 ANALYSIS  ---------------------------#


#-------  BATCH 1 (NovaSeq)  -------#

## Analyse 16S gut metagenome data from batch 1 and extract ASV sequences and abundance table

## input directory containing fastq reads for first batch
inputDIR1 = "./microbiome/reads/batch1"

## define directory to store results
resultsDIR1="./microbiome/results/batch1" # folder to store analysis results of first batch


## IMPORT SEQUENCE DATA TO QIIME2 OBJECTS
qiime tools import \
  --type 'SampleData[PairedEndSequencesWithQuality]' \
  --input-path $inputDIR1 \ # raw sequences in FASTQ format
  --input-format CasavaOneEightSingleLanePerSampleDirFmt \ # format of the sequence files
  --output-path $resultsDIR1/demux-paired-end.qza # raw sequences in QIIME2 object


## CREATE DATA VISUALIZATION (.QZV)
qiime demux summarize \
  --i-data $resultsDIR1/demux-paired-end.qza \
  --o-visualization $resultsDIR1/demux-paired-end.qzv # explore sequence quality and assess QC parameters


## DENOISE READS (remove PCR artifacts, retain ASVs)
qiime dada2 denoise-paired \
  --i-demultiplexed-seqs $resultsDIR1/demux-paired-end.qza \
  --p-trim-left-r 21  \ # remove R primer
  --p-trunc-len-r 0 \ # no R quality trimming
  --p-trim-left-f 17 \ # remove F primer
  --p-trunc-len-f 0 \ # no F quality trimming
  --p-n-threads 0 \ # use all cores available
  --o-representative-sequences $resultsDIR1/rep-seqs.qza \ # ASV sequences (output)
  --o-table $resultsDIR1/table.qza \ # ASV abundance table (output)
  --o-denoising-stats $resultsDIR1/stats.qza # denoising stats (output)


## FILTER OUT SAMPLES from table.qza and rep-seqs.qza to keep same samples as in batch 2

# folder with matching samples
mkdir -p $resultsDIR1/sample-filtered

# filtering out sequences from non matching samples in each batch
qiime feature-table filter-seqs \
  --i-data $resultsDIR1/rep-seqs.qza \ # ASV sequences from batch 1
  --m-metadata-file $resultsDIR1/sample-filtered/samples2keep.txt \ # file with matching samples samples between both datasets
  --p-no-exclude-ids FALSE \
  --o-filtered-data $resultsDIR1/sample-filtered/rep-seqs.qza &

qiime feature-table filter-samples \
  --i-table $resultsDIR1/table.qza \ # ASV abundance table
  --m-metadata-file $resultsDIR1/sample-filtered/samples2keep.txt \ # matching samples samples between both datasets
  --p-exclude-ids FALSE \
  --o-filtered-table $resultsDIR1/sample-filtered/table.qza



#-------  BATCH 2 (MiSeq)  -------#

## Analyse 16S gut metagenome data from batch 2 and extract ASV sequences and abundance table

## input directory containing fastq reads for second batch
inputDIR2 = "./microbiome/reads/batch2"

## re-define directory to store results
resultsDIR2="./microbiome/results/batch2" # folder to store analysis results of second batch


## IMPORT DATA TO QIIME2
qiime tools import \
  --type 'SampleData[PairedEndSequencesWithQuality]' \
  --input-path $inputDIR2 \ # raw sequences in FASTQ format
  --input-format CasavaOneEightSingleLanePerSampleDirFmt \ # format of the sequence files
  --output-path $resultsDIR2/demux-paired-end.qza # raw sequences in QIIME2 object


## CREATE DATA VISUALIZATION (.QZV)
qiime demux summarize \
  --i-data $resultsDIR2/demux-paired-end.qza \
  --o-visualization $resultsDIR2/demux-paired-end.qzv # explore sequence quality and assess QC parameters


## DENOISE READS (remove PCR artifacts, retain ASVs)
qiime dada2 denoise-paired \
  --i-demultiplexed-seqs $resultsDIR2/demux-paired-end.qza \
  --p-trim-left-r 21  \ # remove R primer
  --p-trunc-len-r 250 \ # R quality trimming, same length as PIGBIOTA reads
  --p-trim-left-f 17 \ # remove F primer
  --p-trunc-len-f 250 \ # F quality trimming, same length as PIGBIOTA reads
  --p-n-threads 0 \ # use all cores available
  --o-representative-sequences $resultsDIR2/rep-seqs.qza \ # ASV sequences
  --o-table $resultsDIR2/table.qza \ # ASV abundance table
  --o-denoising-stats $resultsDIR2/stats.qza # denoising stats



#-------  COMBINED DATASET  -------#

## Combine 16S gut metagenome data from batch 1 and 2 perform diverse analyses: ASV taxonomic classification, phylogenetic analysis, alpha and beta diversity analyses

## define directories
resultsDIR="./microbiome/results/batch12" # folder to store analysis results merged
taxDIR="./microbiome/results/batch12/taxonomy" # folder to store results from taxonomy analyses

## COMBINE DATA AFTER DENOISING (table.qza & rep-seqs.qza)
qiime feature-table merge \
  --i-data $resultsDIR1/sample-filtered/table.qza \
  --i-data $resultsDIR2/table.qza \
  --o-merged-data $resultsDIR/table.qza


qiime feature-table merge-seqs \
  --i-data $resultsDIR1/sample-filtered/rep-seqs.qza \
  --i-data $resultsDIR2/rep-seqs.qza \
  --o-merged-data $resultsDIR/rep-seqs.qza


## CREATE VISUALIZATIONS FOR DENOISING RESULTS (.QZV)
qiime feature-table summarize \
  --i-table $resultsDIR/table.qza \
  --o-visualization $resultsDIR/table.qzv \
  --m-sample-metadata-file $resultsDIR/samples_metadata.txt & # sample metadata file validated with Keemei in Google Sheets (https://docs.qiime2.org/2022.8/tutorials/metadata/?highlight=metadata) 

qiime feature-table tabulate-seqs \
  --i-data $resultsDIR/rep-seqs.qza \
  --o-visualization $resultsDIR/rep-seqs.qzv & 


## EXPORT FEATURE TABLE TO BIOM FORMAT
qiime tools export \
  --input-path $resultsDIR/table.qza \
  --output-path $resultsDIR/


## BUILD UNIFORM CLASSIFIER FROM GREENGENES DATABASE

 ## PRIMERS
 # FW S-D-Bact-0341-b-S-17 (V3_F341F)
 # 5-CCTACGGGNGGCWGCAG-3

 # RW S-D-Bact-0785-a-A-21 (V4_R805R)
 # 5-GACTACHVGGGTATCTAATCC-3

 ## Download and import Greengenes (gg) reference sequences clustered at 99% similarty
 wget ftp://greengenes.microbio.me/greengenes_release/gg_13_5/gg_13_8_otus.tar.gz
 tar xzf gg_13_8_otus.tar.gz
 mv gg_13_8_otus/rep_set/99_otus.fasta gg_13_8_otus/rep_set/gg-13.8-99-otus.fasta
 mv gg_13_8_otus/rep_set/gg-13.8-99-otus.fasta $taxDIR/
 rm -r gg_13_8_otus/
 rm gg_13_8_otus.tar.gz

 ## Import reference reads to QIIME2
 qiime tools import \
   --input-path $taxDIR/gg-13.8-99_otus.fasta \
   --type FeatureData[Sequence] \
   --output-path $taxDIR/gg_13_8_99-ref-seqs.qza

 ## Extract amplicons matching our experiment primers for increased efficiency
 qiime feature-classifier extract-reads \
   --i-sequences $taxDIR/gg_13_8_99-ref-seqs.qza \
   --p-f-primer CCTACGGGNGGCWGCAG \
   --p-r-primer GACTACHVGGGTATCTAATCC \
   --p-max-length 500 \
   --p-n-jobs 16 \ # adjust acording to number of CPUs available
   --o-reads $taxDIR/gg_13_8_99-ref-amplicon-seqs.qza

 ## Download reference taxonomy (already in QIIME2 format) from readytowear github
 wget https://github.com/BenKaehler/readytowear/blob/master/data/gg_13_8/full_length/ref-tax.qza
 mv ref-tax.qza gg_13_8_99-ref-tax.qza
 mv gg_13_8_99-ref-tax.qza $taxDIR/

 ## Build  uniform classifier
 qiime feature-classifier fit-classifier-naive-bayes \
   --i-reference-reads $taxDIR/gg_13_8_99-ref-amplicon-seqs.qza \
   --i-reference-taxonomy $taxDIR/gg_13_8_99-ref-tax.qza \
   --o-classifier $taxDIR/gg_13_8_99-amplicon-uniform-classifier.qza



## TAXONOMIC CLASSIFICATION OF ASVs

 ## Assign taxonomy to our features using trained classifier
 qiime feature-classifier classify-sklearn \
   --i-classifier $taxDIR/gg_13_8_99-amplicon-uniform-classifier.qza \
   --i-reads $resultsDIR/rep-seqs.qza \
   --p-n-jobs -1 \
   --o-classification $resultsDIR/taxonomy_gg-13.8-amplicon-uniform.qza

 ## Export taxonomy to TSV and format to
 qiime tools export \
  --input-path $resultsDIR/taxonomy_gg-13.8-amplicon-uniform.qza \
  --output-path $resultsDIR/

 mv taxonomy.tsv taxonomy_gg-13.8-amplicon-uniform.tsv

 ## Create taxonomy barplot
 qiime taxa barplot \
  --i-table $resultsDIR/filtered-table.qza \
  --i-taxonomy $resulstDIR/taxonomy_gg-13.8-amplicon-uniform.qza \
  --m-metadata-file $resultsDIR/samples_metadata.txt \ # sample metadata file validated with Keemei (https://docs.qiime2.org/2022.8/tutorials/metadata/?highlight=metadata)
  --o-visualization $resultsDIR/tax_gg-13.8-barplot.qzv
