#!/bin/bash

function print_help_and_exit
{
cat >&2 << 'EOF'

'voronota-js-pdb-utensil-print-sequence-from-structure' script reads structure, and prints chain sequence.

Options:
    --selection               string     selection string, default is '[]'
    --not-as-assembly                    flag to read input not as a biological assembly, only read one MODEL block
    --help | -h                          flag to display help message and exit

Standard input:
    structure in PDB format
    
Standard output:
    chain sequences in FASTA format
    
Examples:
    
    cat "./model.pdb" | voronota-js-pdb-utensil-print-sequence-from-structure > "./sequences.fasta"
    
    cat "./model.pdb" | voronota-js-pdb-utensil-print-sequence-from-structure --selection '[-chain A,B]' > "./sequences.fasta"
    
    cat "./model.pdb" | voronota-js-pdb-utensil-print-sequence-from-structure --selection '[-protein]' > "./sequences.fasta"
    
    cat "./model.pdb" | voronota-js-pdb-utensil-print-sequence-from-structure --selection '[-nucleic]' > "./sequences.fasta"
    
EOF
exit 1
}

readonly ZEROARG=$0
ALLARGS=("$@")

if [[ $ZEROARG == *"/"* ]]
then
	cd "$(dirname ${ZEROARG})"
	export PATH="$(pwd):${PATH}"
	cd - &> /dev/null
fi

export LC_ALL=C

command -v voronota-js &> /dev/null || { echo >&2 "Error: 'voronota-js' executable not in binaries path"; exit 1; }

SELECTION="[]"
SELECTION_READ="false"
AS_ASSEMBLY="true"
HELP_MODE="false"

while [[ $# > 0 ]]
do
	OPTION="$1"
	OPTARG="$2"
	shift
	case $OPTION in
	--selection)
		SELECTION="$OPTARG"
		SELECTION_READ="true"
		shift
		;;
	--not-as-assembly)
		AS_ASSEMBLY="false"
		;;
	-h|--help)
		HELP_MODE="true"
		;;
	*)
		if [ "$SELECTION_READ" == "false" ]
		then
			SELECTION="$OPTION"
			SELECTION_READ="true";
		else
			echo >&2 "Error: invalid command line option '$OPTION'"
			exit 1
		fi
		;;
	esac
done

if [ "$HELP_MODE" == "true" ]
then
	print_help_and_exit
fi

if [ -z "$SELECTION" ]
then
	echo >&2 "Error: no selection provided"
	exit 1
fi

readonly TMPLDIR=$(mktemp -d)
trap "rm -r $TMPLDIR" EXIT

cat > "$TMPLDIR/input.pdb"

if [ ! -s "$TMPLDIR/input.pdb" ]
then
	echo >&2 "Error: no stdin data"
	exit 1
fi

{
cat << EOF
var params={}
params.input_file='$TMPLDIR/input.pdb';
params.output_file='$TMPLDIR/output.fasta';
params.selection='$SELECTION';
params.as_assembly='$AS_ASSEMBLY';
EOF

cat << 'EOF'
voronota_setup_defaults("-no-load-voromqa-potentials", "-no-load-more-atom-types", "-no-load-mock-voromqa-potential -include-heteroatoms");
voronota_assert_full_success("Failed to setup defaults");

voronota_import("-file", params.input_file, "-as-assembly", params.as_assembly);
voronota_assert_full_success("Failed to input structure");

voronota_select_atoms("-use", "("+params.selection+")", "-name", "restriction");
voronota_assert_full_success("Failed to select atoms");

voronota_export_sequence("-file", params.output_file, "-use", "[restriction]", "-full-residues", "-not-fill-start-gaps", "-not-fill-middle-gaps");
voronota_assert_full_success("Failed to print sequence");
EOF

} \
| voronota-js --no-setup-defaults

if [ ! -s "$TMPLDIR/output.fasta" ]
then
	echo >&2 "Error: no output produced for selection '$SELECTION'"
	exit 1
fi

cat "$TMPLDIR/output.fasta"

