#!/bin/bash
# 2/1/97 - Rewrite. Old version causing some errors on mfold server
# when not all levels present. Old version was also too slow.
# Sorts energy dot plot file $1 and removes helices of length
# less than $SIZE, where SIZE = $2 (3 if $2 not defined). 
# No optimal helices are removed.
# 8/26/05 - Redone using awk in July 05 and now corrected.

export _POSIX2_VERSION=0

if [ $# -lt 1 ]; then
	echo -e "\t*** Usage: filter-sort input_file size"
	exit 1
fi
if [ ! -s $1 ]; then
    echo -e "$1 does not exist or is empty."
    exit 1
else
    if [ $# -lt 2 ]; then
	    SIZE=3
    else
	SIZE=$2
    fi
fi

echo -e "Removing non-optimal helices of length less than $SIZE"

head -n1 $1|tr -s '\011 ' '\011\011' > tmp1-$1
tail -n+2 $1|tr -s '\011 ' '\011\011'|\
    awk -v SIZE=$SIZE '{if($1 == 1 || $2 >= SIZE) print $0}'|sort -n -k5 >> tmp1-$1

mv tmp1-$1 $1
echo -e "$1 replaced by new file."
