#!/bin/sh
# Run UGENE command-line workflows shipped in the ugene-data package.
# These commands also serve as usage examples for UGENE's CLI.
set -e

export LC_ALL=C.UTF-8

data=/usr/share/ugene/data
workflows="$data/cmdline"
samples="$data/samples"
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT

# Basic sanity check: the launcher and its private libraries must work.
ugene --help > /dev/null

# Example 1: convert a sequence file from FASTA to GenBank.
ugene \
    --task="$workflows/convert-seq.uwl" \
    --in="$samples/FASTA/human_T1.fa" \
    --out="$tmp/human_T1.gb" \
    --format=genbank
test -s "$tmp/human_T1.gb"
grep -q '^LOCUS' "$tmp/human_T1.gb"

# Example 2: reverse-complement the same sequence.
ugene \
    --task="$workflows/revcompl.uwl" \
    --in="$samples/FASTA/human_T1.fa" \
    --out="$tmp/human_T1_rc.fasta" \
    --format=fasta
test -s "$tmp/human_T1_rc.fasta"
grep -q '^>' "$tmp/human_T1_rc.fasta"

# The reverse-complement workflow must preserve the number of records.
test "$(grep -c '^>' "$samples/FASTA/human_T1.fa")" = \
     "$(grep -c '^>' "$tmp/human_T1_rc.fasta")"

# Applying the reverse complement twice restores the original sequence.
ugene \
    --task="$workflows/revcompl.uwl" \
    --in="$tmp/human_T1_rc.fasta" \
    --out="$tmp/human_T1_rc2.fasta" \
    --format=fasta
test "$(sed '/^>/d' "$samples/FASTA/human_T1.fa" | tr -d '\n')" = \
     "$(sed '/^>/d' "$tmp/human_T1_rc2.fasta" | tr -d '\n')"
