-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrefparse.sh
More file actions
executable file
·102 lines (91 loc) · 2.9 KB
/
Copy pathrefparse.sh
File metadata and controls
executable file
·102 lines (91 loc) · 2.9 KB
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
#!/usr/bin/env bash
#
# Companion script for dockerised refparse execution.
#
# Builds the image if it isn't already present, then turns one or more LRG
# (.xml) or GenBank (.gb/.gbk) input files into PDFs (default) or plain-text
# transcriptions (--text).
#
# Usage:
# ./refparse.sh input/LRG_110.xml
# ./refparse.sh --text input/GB_TEST.gb input/LRG_1.xml
#
# Output:
# PDFs -> output/pdf/
# generated .tex -> output/tex/
# text mode -> output/txt/
#
# Override the image name with REFPARSE_IMAGE=... if needed.
#
set -euo pipefail
IMAGE="${REFPARSE_IMAGE:-mattwellie/refparse:latest}"
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
mode="pdf"
inputs=()
for arg in "$@"; do
case "$arg" in
--text) mode="text" ;;
-h | --help)
# print the leading comment block (skip the shebang, stop at code)
awk 'NR==1 {next} /^#/ {sub(/^# ?/, ""); print; next} {exit}' "$0"
exit 0
;;
-*)
echo "Unknown option: $arg" >&2
exit 2
;;
*) inputs+=("$arg") ;;
esac
done
if [ "${#inputs[@]}" -eq 0 ]; then
echo "Usage: $0 [--text] FILE [FILE ...]" >&2
exit 2
fi
# Build the image once if it isn't available locally.
if ! docker image inspect "$IMAGE" >/dev/null 2>&1; then
echo ">> building $IMAGE"
docker build -t "$IMAGE" "$REPO_ROOT"
fi
out="$REPO_ROOT/output"
primers="$REPO_ROOT/primers"
mkdir -p "$out" "$primers"
text_flag=""
[ "$mode" = "text" ] && text_flag="--text"
# Generate a .tex (or .txt) per input. The container derives the output name
# from the "input/<file>" path, so each file's directory is mounted as /input.
for f in "${inputs[@]}"; do
if [ ! -f "$f" ]; then
echo "!! no such file: $f" >&2
exit 1
fi
dir="$(cd "$(dirname "$f")" && pwd)"
base="$(basename "$f")"
echo ">> $base ($mode)"
docker run --rm \
-v "$dir:/input" -v "$out:/output" -v "$primers:/primers" \
"$IMAGE" -i "input/$base" $text_flag
done
if [ "$mode" = "text" ]; then
mkdir -p "$out/txt"
mv -f "$out"/*.txt "$out/txt/" 2>/dev/null || true
echo ">> text transcriptions in output/txt/"
exit 0
fi
# Compile each freshly generated .tex to PDF using the same image, then tidy up.
shopt -s nullglob
tex_files=("$out"/*.tex)
if [ "${#tex_files[@]}" -eq 0 ]; then
echo "!! no .tex files were generated" >&2
exit 1
fi
for tex in "${tex_files[@]}"; do
docker run --rm --user "$(id -u):$(id -g)" \
-w /sources -v "$REPO_ROOT:/sources" \
--entrypoint pdflatex "$IMAGE" \
-interaction=nonstopmode -output-directory=output "output/$(basename "$tex")"
done
mkdir -p "$out/pdf" "$out/tex"
mv -f "$out"/*.pdf "$out/pdf/" 2>/dev/null || true
mv -f "$out"/*.tex "$out/tex/" 2>/dev/null || true
rm -f "$out"/*.log "$out"/*.aux "$out"/*.out "$out"/*.upa "$out"/*.upb
echo ">> PDFs in output/pdf/, .tex sources in output/tex/"