# Show the system information

## Execute environment: LINUX
cat /etc/lsb-release
### DISTRIB_ID=Ubuntu
### DISTRIB_RELEASE=20.04
### DISTRIB_CODENAME=focal
### DISTRIB_DESCRIPTION="Ubuntu 20.04.4 LTS"

## Execute environment: PICRUSt2
place_seqs.py -v
### place_seqs.py 2.5.0
hsp.py -v
### hsp.py 2.5.0
metagenome_pipeline.py -v
### metagenome_pipeline.py 2.5.0
pathway_pipeline.py -v
### pathway_pipeline.py 2.5.0
add_descriptions.py -v
### add_descriptions.py 2.5.0



#0 Preprocess
# Set available number of resources
THREADS=$(($(grep processor /proc/cpuinfo | wc -l)/2))

# Define the variables
SEQS="${HOME}/220602_qiime2/rarefacted-rep-seqs/rarefacted-rep-seqs.fasta"
TABLE="${HOME}/220602_qiime2/rarefacted-table/rarefacted-table.tsv"

# Make output directories
OUTDIR="${PWD}/$(date "+%y%m%d")_picrust2"



#1 Place reads into reference tree *HMMER, EPA-NG, GAPPA
## Place amplicon sequence variants into reference phylogenetic tree based on 20,000 16S sequences from genomes in the Integrated Microbial Genomes database
nohup place_seqs.py \
    -s ${SEQS} \
    -o ${OUTDIR}/placed_seqs.tre \
    -t epa-ng \
    -p ${THREADS} \
    --intermediate ${OUTDIR}/intermediate/place_seqs \
    --verbose \
    &> ${OUTDIR}/log/place_seqs.log &
wait



#2 Hidden-state prediction of gene families *castor-R
## Run hidden-state prediction with maximum parsimony method to get 16S rRNA gene copy number, E.C. number, and KO abundances per predicted genome with the nearest-sequenced taxon index (NSTI) values which correspond to the branch length in the tree from the placed ASV to the nearest reference 16S sequence
hsp.py \
    -i 16S \
    -t ${OUTDIR}/placed_seqs.tre \
    -o ${OUTDIR}/marker_predicted_and_nsti.tsv.gz \
    -p ${THREADS} \
    -n

hsp.py \
    -i EC \
    -t ${OUTDIR}/placed_seqs.tre \
    -o ${OUTDIR}/EC_predicted.tsv.gz \
    -p ${THREADS}

hsp.py \
    -i KO \
    -t ${OUTDIR}/placed_seqs.tre \
    -o ${OUTDIR}/KO_predicted.tsv.gz \
    -p ${THREADS}

## Log removed ASVs
echo "Removed by place_seqs" > ${OUTDIR}/log/removed_asv.tsv

diff -U0 \
    <(cat ${TABLE} | awk 'NR>1 {print $1}' | sort -V) \
    <(zcat ${OUTDIR}/marker_predicted_and_nsti.tsv.gz | awk 'NR>1 {print $1}' | sort -V) |
    grep -E "^-[^-]" | cut -b2- > ${OUTDIR}/temp1 || :

while read line
do
    cat ${TABLE} | grep "${line}" | cut -f1
done < ${OUTDIR}/temp1 >> ${OUTDIR}/log/removed_asv.tsv



#3 Generate metagenome predictions
## Predicting E.C. and KO abundances in sequencing samples (adjusts gene family abundances by ASVs abundances, i.e. 16S rRNA gene copy numbers, removing ASVs represent above 2 NSTI value)
nohup metagenome_pipeline.py \
    -i ${TABLE} \
    -m ${OUTDIR}/marker_predicted_and_nsti.tsv.gz \
    -f ${OUTDIR}/EC_predicted.tsv.gz \
    -o ${OUTDIR}/EC_metagenome_out \
    --strat_out \
    &> ${OUTDIR}/log/metagenome.log &
wait

nohup metagenome_pipeline.py \
    -i ${TABLE} \
    -m ${OUTDIR}/marker_predicted_and_nsti.tsv.gz \
    -f ${OUTDIR}/KO_predicted.tsv.gz \
    -o ${OUTDIR}/KO_metagenome_out \
    --strat_out \
    &>> ${OUTDIR}/log/metagenome.log &
wait

## Log removed ASVs
echo "Removed by metagenome" >> ${OUTDIR}/log/removed_asv.tsv

zcat ${OUTDIR}/marker_predicted_and_nsti.tsv.gz | head -n1 > ${OUTDIR}/log/removed_asv.tsv

diff -U0 \
    <(zcat ${OUTDIR}/marker_predicted_and_nsti.tsv.gz | awk 'NR>1 {print $1}' | sort -V) \
    <(zcat ${OUTDIR}/EC_metagenome_out/seqtab_norm.tsv.gz | awk 'NR>1 {print $1}' | sort -V) |
    grep -E "^-[^-]" | cut -b2- > ${OUTDIR}/temp2 || :

while read line
do
    zcat ${OUTDIR}/marker_predicted_and_nsti.tsv.gz | grep "${line}"
done < ${OUTDIR}/temp2 >> ${OUTDIR}/log/removed_asv.tsv

rm ${OUTDIR}/temp*



#4 Pathway-level inference
## Infer MetaCyc pathway abundances and coverages based on predicted E.C. number abundances
nohup pathway_pipeline.py \
    -i ${OUTDIR}/EC_metagenome_out/pred_metagenome_contrib.tsv.gz \
    -o ${OUTDIR}/pathways_out_contrib \
    --intermediate ${OUTDIR}/pathways_working_contrib \
    -p ${THREADS} \
    &> ${OUTDIR}/log/pathway_EC.log &
wait



#5 Add functional descriptions
## Add descriptions as new column in gene family and pathway abundance tables
add_descriptions.py \
    -i ${OUTDIR}/EC_metagenome_out/pred_metagenome_unstrat.tsv.gz \
    -m EC \
    -o ${OUTDIR}/EC_metagenome_out/pred_metagenome_unstrat_descrip.tsv.gz

add_descriptions.py \
    -i ${OUTDIR}/KO_metagenome_out/pred_metagenome_unstrat.tsv.gz \
    -m KO \
    -o ${OUTDIR}/KO_metagenome_out/pred_metagenome_unstrat_descrip.tsv.gz

add_descriptions.py \
    -i ${OUTDIR}/pathways_out_contrib/path_abun_unstrat.tsv.gz \
    -m METACYC \
    -o ${OUTDIR}/pathways_out_contrib/path_abun_unstrat_descrip.tsv.gz



#6 Postprocess
# Copy executed script
cp -av ${PWD}/$0 ${OUTDIR}/log/${0%%.sh}__executed.sh
cp -av ${PWD}/${0%%.sh}.conf ${OUTDIR}/log/${0%%.sh}__executed.conf



exit 0