#!/bin/sh

cd $PWD

#Multiplied everything by 1000000
biom convert -i Metaphlan_Species_Permanova_Multiplied.tsv -o Metaphlan_Species_Permanova.biom --table-type "Table" --to-hdf5

qiime metadata tabulate \
  --m-input-file metadata.tsv \
  --o-visualization metadata.qzv
  
  
qiime tools import --input-path Metaphlan_Species_Permanova.biom --type FeatureTable[Frequency] --input-format BIOMV210Format --output-path Metaphlan_Species_Permanova.qza


# FILTERED Feature table
qiime feature-table summarize --i-table Metaphlan_Species_Permanova.qza --o-visualization Metaphlan_Species_Permanova_Feature_Table.qzv --m-sample-metadata-file metadata.tsv

mkdir alpha_rarefaction

# 16S rRNA dataset Alpha Diversity
qiime diversity alpha-rarefaction \
--i-table Metaphlan_Species_Permanova.qza \
--p-max-depth 999998 \
--m-metadata-file ./metadata.tsv \
--p-metrics shannon --p-metrics simpson --p-metrics observed_features \
--o-visualization alpha_rarefaction/rarefaction.qzv


qiime diversity core-metrics \
--i-table Metaphlan_Species_Permanova.qza \
--p-sampling-depth 999998 \
--m-metadata-file ./metadata.tsv \
--output-dir core_metrics \
--p-n-jobs 8 \
--verbose \
&> core_metrics_samples.log

# Simpson Index
qiime diversity alpha \
--i-table core_metrics/rarefied_table.qza \
--p-metric simpson \
--o-alpha-diversity core_metrics/simpson_vector.qza

# Run plugin for each alpha diversity result
cd core_metrics

for result in *vector.qza; \
do \
outname=${result/_vector.qza/_group_significance.qzv}; \
qiime diversity alpha-group-significance \
--i-alpha-diversity $result \
--m-metadata-file ../metadata.tsv \
--o-visualization $outname; \
done

cd ..

qiime diversity beta-group-significance --i-distance-matrix core_metrics/bray_curtis_distance_matrix.qza --m-metadata-file ./metadata.tsv --m-metadata-column AllSamples --o-visualization core_metrics/PERMANOVA_BrayCurtis_AllSamples_pairwise.qzv --p-pairwise --p-permutations 9999

qiime diversity beta-group-significance --i-distance-matrix core_metrics/bray_curtis_distance_matrix.qza --m-metadata-file ./metadata.tsv --m-metadata-column DeNovo --o-visualization core_metrics/PERMANOVA_BrayCurtis_DeNovo_pairwise.qzv --p-pairwise --p-permutations 9999

qiime diversity beta-group-significance --i-distance-matrix core_metrics/bray_curtis_distance_matrix.qza --m-metadata-file ./metadata.tsv --m-metadata-column Med --o-visualization core_metrics/PERMANOVA_BrayCurtis_Med_pairwise.qzv --p-pairwise --p-permutations 9999