#!/bin/bash

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

'voronota-js-pdb-utensil-filter-atoms' script reads structure, restricts atoms by a query, outputs structure.

Options:
    --selection               string     selection string, default is '[]'
    --motif                   string     motif string, default is ''
    --help | -h                          flag to display help message and exit

Standard input:
    structure in PDB format
    
Standard output:
    structure in PDB format
    
Examples:
    
    cat "./model.pdb" | voronota-js-pdb-utensil-filter-atoms --selection '[-chain A,B -rnum 10:100]' > "./result.pdb"
    
    cat "./model.pdb" | voronota-js-pdb-utensil-filter-atoms '[-chain A,B -rnum 10:100 -aname ]' > "./result.pdb"
    
    cat "./model.pdb" | voronota-js-pdb-utensil-filter-atoms '(not [-chain A,B -rnum 10:100 -aname ])' > "./result.pdb"
    
    cat "./model.pdb" | voronota-js-pdb-utensil-filter-atoms '([-chain A -rnum 10:100] or [-chain B -rnum 101:200])' > "./result.pdb"
    
    cat "./model.pdb" | voronota-js-pdb-utensil-filter-atoms '([-protein] or [-t het])' > "./result.pdb"
    
    cat "./model.pdb" | voronota-js-pdb-utensil-filter-atoms '([-nucleic] or [-t het])' | voronota-js-pdb-utensil-filter-atoms '[-chain A,B -rnum 10:100]'
    
    cat "./model.pdb" | voronota-js-pdb-utensil-filter-atoms '[-chain A,A2]' > "./result.pdb"
    
    cat "./model.pdb" | voronota-js-pdb-utensil-filter-atoms '[-protein -aname CA,C,N,O,CB]' --motif 'LYYYK?Y' > "./result.pdb"
    
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"
MOTIF=""
AS_ASSEMBLY="true"
HELP_MODE="false"

while [[ $# > 0 ]]
do
	OPTION="$1"
	OPTARG="$2"
	shift
	case $OPTION in
	--selection)
		SELECTION="$OPTARG"
		SELECTION_READ="true"
		shift
		;;
	--motif)
		MOTIF="$OPTARG"
		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.pdb';
params.selection='$SELECTION';
params.motif='$MOTIF';
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+")", "-motif", params.motif, "-name", "restriction");
voronota_assert_full_success("Failed to select atoms");

voronota_restrict_atoms("-use", "[restriction]");
voronota_assert_full_success("Failed to restrict atoms");

voronota_export_atoms("-as-pdb", "-file", params.output_file);
voronota_assert_full_success("Failed to output structure");
EOF

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

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

cat "$TMPLDIR/output.pdb"

