#!/bin/bash                                                                     
# Adapted from https://git.metabarcoding.org/obitools/obitools3/-/wikis/Wolf-tutorial-with-the-OBITools3
set -ex

pkg=obitools

export LC_ALL=C.UTF-8
if [ "${AUTOPKGTEST_TMP}" = "" ] ; then
  AUTOPKGTEST_TMP=$(mktemp -d /tmp/${pkg}-test.XXXXXX)
  # shellcheck disable=SC2064
  trap "rm -rf ${AUTOPKGTEST_TMP}" 0 INT QUIT ABRT PIPE TERM
fi

cp -a /usr/share/doc/${pkg}/examples/* "${AUTOPKGTEST_TMP}"

cd "${AUTOPKGTEST_TMP}"

obi import --quality-solexa wolf_tutorial/wolf_F.fastq wolf/reads1
obi import --quality-solexa wolf_tutorial/wolf_R.fastq wolf/reads2
obi import --ngsfilter-input wolf_tutorial/wolf_diet_ngsfilter.txt wolf/ngsfile
obi ls wolf
obi ls -l wolf
obi ls wolf/reads1
obi ls wolf/ngsfile/sample
obi less wolf/reads1 > /dev/null
obi alignpairedend -R wolf/reads2 wolf/reads1 wolf/aligned_reads
obi alignpairedend -R wolf/reads2 wolf/reads1 - > aligned_reads.fastq
obi stats -a score_norm wolf/aligned_reads
obi grep -p "sequence['score_norm'] > 0.8" wolf/aligned_reads wolf/good_sequences
obi ngsfilter -t wolf/ngsfile -u wolf/unidentified_sequences wolf/good_sequences wolf/identified_sequences
obi uniq -m sample wolf/identified_sequences wolf/dereplicated_sequences
obi annotate -k COUNT -k MERGED_sample wolf/dereplicated_sequences wolf/cleaned_metadata_sequences
obi grep -p "len(sequence)>=80 and sequence['COUNT']>=10" wolf/cleaned_metadata_sequences wolf/denoised_sequences
obi clean -s MERGED_sample -r 0.05 -H wolf/denoised_sequences wolf/cleaned_sequences

tar cJf "${AUTOPKGTEST_ARTIFACTS:-.}/wolf.obidms.tar.xz" wolf.obidms
