# purpose: to re-run the 18s data using the new version of SILVA - 138

cd /mounts/pollock/16S/Iceland-2019/Skalanes-Test-Pit/

mkdir run3-18s

# get correct files 

cp /mounts/pollock/16S/Iceland-2019/Skalanes-Test-Pit/run1-18s/Iceland-2019-STP-FWDs-run1-18s-raw.qza /mounts/pollock/16S/Iceland-2019/Skalanes-Test-Pit/run3-18s/
cp /mounts/pollock/16S/Iceland-2019/Skalanes-Test-Pit/run1-18s/Skalanes-Test-Pit-Manafest.tsv /mounts/pollock/16S/Iceland-2019/Skalanes-Test-Pit/run3-18s/
cp /mounts/pollock/16S/Iceland-2019/Skalanes-Test-Pit/run1-18s/Skalanes-Test-Pit-Metadata.tsv /mounts/pollock/16S/Iceland-2019/Skalanes-Test-Pit/run3-18s/
cp /eccs/home/smithem3/silva-138-99-nb-classifier.qza /mounts/pollock/16S/Iceland-2019/Skalanes-Test-Pit/run3-18s/

## note if this does not work try re-training the classifyer with this tutorial: https://forum.qiime2.org/t/processing-filtering-and-evaluating-the-silva-database-and-other-reference-sequence-data-with-rescript/15494


conda activate qiime2-2021.4
source tab-qiime

qiime demux summarize \
	--i-data Iceland-2019-STP-FWDs-run1-18s-raw.qza \
	--o-visualization Iceland-2019-STP-FWDs-run2-18s-raw.qzv

# trunc at 223 (quality score below 25) and trim at 31 to remove primer (did not check to see if primer was present) - leaves 192 bp 

nohup qiime dada2 denoise-single \
	--p-n-threads 8 \
	--i-demultiplexed-seqs Iceland-2019-STP-FWDs-run1-18s-raw.qza \
	--p-trunc-len 223 \
    --p-trim-left 31 \
	--o-representative-sequences Iceland-2019-STP-FWDs-rep-seqs-dada2-run3-18s.qza \
	--o-table Iceland-2019-STP-FWDs-table-dada2-run3-18s.qza \
	--o-denoising-stats Iceland-2019-STP-FWDs-stats-dada2-run3-18s.qza &

# To check on the job, you can use ps to see how it’s running. 

# summarize resulting statistics 
qiime metadata tabulate \
	--m-input-file Iceland-2019-STP-FWDs-stats-dada2-run3-18s.qza \
	--o-visualization Iceland-2019-STP-FWDs-stats-dada2-run3-18s.qzv 

# Move the .qzv file to the apache web browser:
cp Iceland-2019-STP-FWDs-stats-dada2-run3-18s.qzv /cluster/fieldscience/artifacts/Iceland-2019-Skalanes-Test-Pit-run1-18s

# note, everything is the same as before, keep the same parameters for phylogeny 

# generate feature summaries
qiime feature-table summarize \
	--i-table Iceland-2019-STP-FWDs-table-dada2-run3-18s.qza \
	--o-visualization Iceland-2019-STPs-FWDs-table-dada2-run3-18s.qzv \
	--m-sample-metadata-file Skalanes-Test-Pit-Metadata.tsv

# on average we kept 78% of reads after DADA2 without lowering the pmin 
# minimum number of reads in lowest sample is 36394 - Retained 582,304 (62.57%) features in 16 (94.12%) samples at the specifed sampling depth.
# average # of reads after DADA is 58164

# Create a phylogenetic tree from the data: 
rm nohup.out
nohup qiime phylogeny align-to-tree-mafft-fasttree \
	--p-n-threads 8 \
	--i-sequences Iceland-2019-STP-FWDs-rep-seqs-dada2-run3-18s.qza \
	--o-alignment Iceland-2019-STP-aligned-rep-seqs-dada2-run3-18s.qza \
	--o-masked-alignment Iceland-2019-STP-masked-aligned-rep-seqs-dada2-run3-18s.qza \
	--o-tree Iceland-2019-STP-unrooted-tree-run3-18s.qza \
	--o-rooted-tree Iceland-2019-STP-rooted-tree-run3-18s.qza &

# Taxonomic analysis

rm nohup.out
nohup qiime feature-classifier classify-sklearn \
	--p-n-jobs 8 \
	--i-classifier silva-138-99-nb-classifier.qza \
	--i-reads Iceland-2019-STP-FWDs-rep-seqs-dada2-run3-18s.qza \
	--o-classification Iceland-2019-STP-taxonomy-silva-run3-18s.qza &

qiime metadata tabulate \
	--m-input-file Iceland-2019-STP-taxonomy-silva-run3-18s.qza \
	--m-input-file Iceland-2019-STP-FWDs-rep-seqs-dada2-run3-18s.qza \
	--o-visualization Iceland-2019-STP-taxonomy-silva-table-run3-18s.qzv

qiime taxa barplot \
	--i-table Iceland-2019-STP-FWDs-table-dada2-run3-18s.qza \
	--i-taxonomy Iceland-2019-STP-taxonomy-silva-run3-18s.qza \
	--m-metadata-file Skalanes-Test-Pit-Metadata.tsv \
	--o-visualization Iceland-2019-rSTP-taxonomy-silva-bar-plots-run3-18s.qzv

cp Iceland-2019-STP-taxonomy-silva-table-run3-18s.qzv /cluster/fieldscience/artifacts/Iceland-2019-Skalanes-Test-Pit-run1-18s
cp Iceland-2019-rSTP-taxonomy-silva-bar-plots-run3-18s.qzv /cluster/fieldscience/artifacts/Iceland-2019-Skalanes-Test-Pit-run1-18s

