#!/bin/bash

module load trimmomatic/0.39
module load jdk/1.7.0_80

##Trimmomatic for SE reads 

RAWREADS=
CLEANREADS=

cd ${RAWREADS}

for i in $(ls *.fastq.gz); do

	j=$(echo "${i}" | awk -F _ '{print$1}')

	trimmomatic SE -threads 20 ${i} ${CLEANREADS}/${j}_00_L001_R1_001.fastq.gz SLIDINGWINDOW:4:20 MINLEN:115 HEADCROP:15;

done 

##Qiime2 analysis of separate datasets 

source ~/anaconda3/bin/activate 
conda activate qiime2-2020.2

CLEANFILESPERDIRECTORY=
MFILE=
WORKDIR=/hpcfs/home/la.chica10/ProyectoMicrobioma/ReAnalisis_2020/Correcciones_PeerJ_Iter1/CoreMicrobiomePerStudy      

cd ${CLEANFILESPERDIRECTORY}

for AMPLICONS in $(ls -d */); do 
	AMPLICON=$(echo "${AMPLICONS}" | awk -F / '{print$1}')
	
#Importing data to qiime2
qiime tools import --type SampleData[SequencesWithQuality] --input-path ${AMPLICONS} --output-path ${WORKDIR}/${AMPLICON}_Seqs.qza --input-format CasavaOneEightSingleLanePerSampleDirFmt;
qiime demux summarize --i-data ${WORKDIR}/${AMPLICON}_Seqs.qza --o-visualization ${WORKDIR}/${AMPLICON}_Seqs.qzv


##Deblur 
qiime deblur denoise-16S --i-demultiplexed-seqs ${WORKDIR}/${AMPLICON}_Seqs.qza --p-trim-length 130 --p-jobs-to-start 10 --o-table ${WORKDIR}/${AMPLICON}_table_Deblur.qza --o-representative-sequences ${WORKDIR}/${AMPLICON}_RepSeq_Deblur.qza --o-stats ${WORKDIR}/${AMPLICON}_Stats_Deblur.qza --p-sample-stats;
qiime deblur visualize-stats --i-deblur-stats ${WORKDIR}/${AMPLICON}_Stats_Deblur.qza --o-visualization ${WORKDIR}/${AMPLICON}_Stats_Deblur.qzv;
qiime feature-table summarize --i-table ${WORKDIR}/${AMPLICON}_table_Deblur.qza --o-visualization ${WORKDIR}/${AMPLICON}_table_Deblur.qzv;


# #Filters
FREC=10
MINSAMPLES=3
qiime feature-table filter-features --i-table ${WORKDIR}/${AMPLICON}_table_Deblur.qza --p-min-frequency ${FREC} --p-min-samples ${MINSAMPLES} --o-filtered-table ${WORKDIR}/${AMPLICON}_table_Deblur_MinFrecuency${FREC}_Minsamples${MINSAMPLES}.qza; 
qiime feature-table summarize --i-table ${WORKDIR}/${AMPLICON}_table_Deblur_MinFrecuency${FREC}_Minsamples${MINSAMPLES}.qza --o-visualization ${WORKDIR}/${AMPLICON}_table_Deblur_MinFrecuency${FREC}_Minsamples${MINSAMPLES}.qzv; #--m-sample-metadata-file ${MFILE}; 

##Taxonomy assignment 
SEQ=${WORKDIR}/${AMPLICON}_RepSeq_Deblur.qza
TAXONOMIA=${WORKDIR}/${AMPLICON}_TaxonomyTable_GG

qiime feature-classifier classify-sklearn --i-classifier ~/Databases/GreenGenes/Qiime2_gg_13_8/gg_13-8_99_otus_nb_Classifier.qza --i-reads ${SEQ} --o-classification ${TAXONOMIA}.qza --p-n-jobs 10;
qiime metadata tabulate --m-input-file ${TAXONOMIA}.qza --o-visualization ${TAXONOMIA}.qzv;

##Genus Collapse
TABLE=${WORKDIR}/${AMPLICON}_table_Deblur_MinFrecuency${FREC}_Minsamples${MINSAMPLES}_GenusCollapsed_GG

qiime taxa collapse --i-table ${WORKDIR}/${AMPLICON}_table_Deblur_MinFrecuency${FREC}_Minsamples${MINSAMPLES}.qza --i-taxonomy ${TAXONOMIA}.qza --p-level 6 --o-collapsed-table ${TABLE}.qza; 
qiime feature-table summarize --i-table ${TABLE}.qza --o-visualization ${TABLE}.qzv; #--m-sample-metadata-file ${MFILE};

qiime tools export  --input-path ${table}.qza  --output-path ${table}

done
##Merging multiple tables 
qiime feature-table merge --i-tables --p-overlap-method 'error_on_overlapping_sample' --o-merged-table ${WORKDIR}/AmpliconMergeGenus_ErrorOnOverlap.qza

##Analysis of merged table 

DEPTH=500
qiime diversity alpha-rarefaction --i-table ${WORKDIR}/AmpliconMergeGenus_ErrorOnOverlap.qza --p-max-depth ${DEPTH} --o-visualization ${WORKDIR}/RarefactionPlot_${DEPTH}.qzv --m-metadata-file ${MFILE} 

qiime feature-table rarefy --i-table ${WORKDIR}/AmpliconMergeGenus_ErrorOnOverlap.qza --p-sampling-depth ${DEPTH} --o-rarefied-table ${WORKDIR}/TableMerge_Rarefied_${DEPTH}.qza

qiime feature-table summarize --i-table ${WORKDIR}/TableMerge_Rarefied_${DEPTH}.qza --m-sample-metadata-file ${MFILE} --o-visualization ${WORKDIR}/TableMerge_Rarefied_${DEPTH}.qzv 

qiime diversity core-metrics --i-table ${WORKDIR}/AmpliconMergeGenus_ErrorOnOverlap.qza --p-sampling-depth ${DEPTH} --m-metadata-file ${MFILE} --output-dir ${WORKDIR}/Diversidad_${DEPTH} --p-n-jobs 10;

qiime diversity alpha-group-significance --i-alpha-diversity ${WORKDIR}/Diversidad_${DEPTH}/shannon_vector.qza --m-metadata-file ${MFILE} --o-visualization ${WORKDIR}/Diversidad_${DEPTH}/AlphaSignificanceShanon.qzv;

qiime tools export  --input-path ${WORKDIR}/Diversidad_${DEPTH}/shannon_vector.qza  --output-path ${WORKDIR}/Diversidad_${DEPTH}/shannon_vector_${DEPTH}

qiime tools export  --input-path ${WORKDIR}/Diversidad_${DEPTH}/bray_curtis_pcoa_results.qza  --output-path ${WORKDIR}/Diversidad_${DEPTH}/bray_curtis_pcoa_results_${DEPTH}

qiime diversity beta-group-significance --i-distance-matrix ${WORKDIR}/Diversidad_${DEPTH}/bray_curtis_distance_matrix.qza --m-metadata-file ${MFILE} --m-metadata-column Region --p-pairwise --o-visualization ${WORKDIR}/Diversidad_${DEPTH}/Permanova_Region.qzv;

qiime diversity beta-group-significance --i-distance-matrix ${WORKDIR}/Diversidad_${DEPTH}/bray_curtis_distance_matrix.qza --m-metadata-file ${MFILE} --m-metadata-column Author --p-pairwise --o-visualization ${WORKDIR}/Diversidad_${DEPTH}/Permanova_Author.qzv;

qiime taxa barplot --i-table ${WORKDIR}/AmpliconMergeGenus_ErrorOnOverlap.qza --m-metadata-file ${MFILE} --o-visualization ${WORKDIR}/TaxaBarplots.qzv;
