
###QIIME2 - bioinformatics Borneo small mammals microbiome


# Running QIIME2
conda activate qiime2-2023.2

# enable tab complete
source tab-qiime

### Beginning of pipeline

# Move to working directory
cd path/to/directory

# import input demultiplexed paired end fastq files into an artifact
qiime tools import --type 'SampleData[PairedEndSequencesWithQuality]' --input-path path/to/fastq/folder --input-format CasavaOneEightSingleLanePerSampleDirFmt --output-path ./demux-paired-end.qza

# summarise number of reads per sample, and quality information
qiime demux summarize --i-data demux-paired-end.qza --o-visualization demux.qzv

##### Denoising 
qiime dada2 denoise-paired --i-demultiplexed-seqs demux-paired-end.qza --p-trim-left-f 19 --p-trim-left-r 19 --p-trunc-len-f 240 --p-trunc-len-r 220 --p-n-threads 16 --o-table table.qza --o-representative-sequences rep-seqs.qza --o-denoising-stats denoising-stats.qza --verbose

# summarise de-noising stats, and information in feature table and feature sequences
qiime metadata tabulate --m-input-file denoising-stats.qza --o-visualization denoising-stats.qzv
qiime feature-table summarize --i-table table.qza --o-visualization table.qzv --m-sample-metadata-file metadata_qiime2_Borneo_sm_microbiota
qiime feature-table tabulate-seqs --i-data rep-seqs.qza  --o-visualization rep-seqs.qzv

##### Taxonomic classification
qiime feature-classifier classify-sklearn --i-classifier path/to/classifier/silva-138-99-515-806-nb-classifier.qza --i-reads rep-seqs.qza --o-classification silva-taxonomy.qza

qiime metadata tabulate --m-input-file silva-taxonomy.qza --o-visualization silva-taxonomy.qzv

# filter to remove host sequences (annotated to Bacteria only in Silva), chloroplasts, mitohondria 
qiime taxa filter-table --i-table table.qza --i-taxonomy silva-taxonomy.qza --p-exclude D_0__Bacteria,Unassigned --p-mode exact --o-filtered-table filtered
qiime taxa filter-table --i-table filtered.qza --i-taxonomy silva-taxonomy.qza --p-exclude mitochondria,chloroplast --o-filtered-table filtered.table

qiime feature-table summarize --i-table filtered.table.qza --o-visualization filtered.table.qzv --m-sample-metadata-file metadata_qiime2_Borneo_sm_microbiota

##### Phylogenetic analysis
# create ASV phylogenetic tree
qiime phylogeny align-to-tree-mafft-fasttree --i-sequences rep-seqs.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
# export ASV phylogenetic tree
qiime tools export --input-path rooted-tree.qza --output-path exported-tree
mv exported-tree/tree.nwk asv_phylogenetic_tree.nwk
  
##### Alpha diversity rarefaction curves
qiime diversity alpha-rarefaction --i-table filtered.table.qza --i-phylogeny rooted-tree.qza --p-min-depth 100 --p-steps 50 --p-max-depth 50000 --m-metadata-file metadata_qiime2_Borneo_sm_microbiota --o-visualization alpha-rarefaction.qzv

##### Export ASV abundance table and taxonomy table
qiime tools export --input-path table.qza --output-path exported-asv-table
biom convert -i exported-asv-table/feature-table.biom -o exported-asv-table/asv_table.txt --to-tsv --header-key taxonomy
qiime tools export --input-path silva-taxonomy.qza --output-path exported-silva-taxonomy

### End of pipeline

##### Move to R for statistical analysis











