-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftdmp-sort-table
executable file
·159 lines (130 loc) · 3.23 KB
/
ftdmp-sort-table
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/bin/bash
function print_help_and_exit
{
cat >&2 << 'EOF'
'ftdmp-sort-table' sorts an input
Options:
--columns string * space-separated list of signed column name
--tolerances string space-separated list of tolerances, default is no tolerances
--add-rank-column string name of rank column to add
--help | -h flag to display help message and exit
Standard input:
space-separated table
Standard output:
space-separated table
Examples:
cat scoring_results_table.txt | ftdmp-sort-table --columns "-iface_energy +scscore" --tolerances "0.0 0.0"
EOF
exit 1
}
if [ -z "$1" ]
then
print_help_and_exit
fi
if [ -z "$FTDMPDIR" ]
then
export FTDMPDIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
export PATH="${FTDMPDIR}/core/voronota/expansion_js:${FTDMPDIR}:${PATH}"
fi
command -v voronota-js &> /dev/null || { echo >&2 "Error: 'voronota-js' executable not in binaries path"; exit 1; }
COLUMNS=""
TOLERANCES=""
ADD_RANK_COLUMN=""
HELP_MODE="false"
while [[ $# > 0 ]]
do
OPTION="$1"
OPTARG="$2"
shift
case $OPTION in
--columns)
COLUMNS="$OPTARG"
shift
;;
--tolerances)
TOLERANCES="$OPTARG"
shift
;;
--add-rank-column)
ADD_RANK_COLUMN="$OPTARG"
shift
;;
-h|--help)
HELP_MODE="true"
;;
*)
echo >&2 "Error: invalid command line option '$OPTION'"
exit 1
;;
esac
done
if [ "$HELP_MODE" == "true" ]
then
print_help_and_exit
fi
if [ -z "$COLUMNS" ]
then
echo >&2 "Error: no columns specified"
exit 1
fi
readonly TMPLDIR=$(mktemp -d)
trap "rm -r $TMPLDIR" EXIT
echo "$COLUMNS" | sed 's/\s/\n/g' | egrep '.' \
| while read -r COLNAME
do
MULTIPLIER="1"
if [[ "$COLNAME" == "-"* ]]
then
MULTIPLIER="-1"
fi
echo "${COLNAME} ${MULTIPLIER}"
done \
| sed 's/^-//' \
| sed 's/^+//' \
> "${TMPLDIR}/columns_info"
N_COLUMNS="$(cat "${TMPLDIR}/columns_info" | wc -l)"
if [ -n "$TOLERANCES" ]
then
echo "$TOLERANCES" | sed 's/\s/\n/g' | egrep '.' \
> "${TMPLDIR}/tolerances_info"
N_TOLERANCES="$(cat "${TMPLDIR}/tolerances_info" | wc -l)"
if [ "$N_COLUMNS" -ne "$N_TOLERANCES" ]
then
echo >&2 "Error: numbers of columns and tolerances do not match"
exit 1
fi
paste "${TMPLDIR}/columns_info" "${TMPLDIR}/tolerances_info" > "${TMPLDIR}/full_columns_info"
else
cat "${TMPLDIR}/columns_info" | awk '{print $1 " " $2 " 0"}' > "${TMPLDIR}/full_columns_info"
fi
{
echo "voronota_tournament_sort('-input-file _stdin -output-file _stdout"
echo "-columns"
cat "${TMPLDIR}/full_columns_info" | awk '{print $1}'
echo "-multipliers"
cat "${TMPLDIR}/full_columns_info" | awk '{print $2}'
echo "-tolerances"
cat "${TMPLDIR}/full_columns_info" | awk '{print $3}'
if [ -n "$ADD_RANK_COLUMN" ]
then
echo "-add-rank-column ${ADD_RANK_COLUMN}"
fi
echo "');"
echo "voronota_assert_full_success('Failed to sort');"
} \
| tr '\n' ' ' \
| sed 's/$/\n/' \
> "${TMPLDIR}/sorting_script"
cat > "${TMPLDIR}/input"
if [ ! -s "${TMPLDIR}/input" ]
then
echo >&2 "Error: no input data in stdin"
exit 1
fi
cat "${TMPLDIR}/input" | voronota-js --no-setup-defaults "${TMPLDIR}/sorting_script" > "${TMPLDIR}/output"
if [ ! -s "${TMPLDIR}/output" ]
then
echo >&2 "Error: no output produced"
exit 1
fi
cat "${TMPLDIR}/output"