Skip to content

Commit 469244b

Browse files
committed
Use the high-level dispatcher wrappers
1 parent a9047be commit 469244b

File tree

1 file changed

+48
-52
lines changed

1 file changed

+48
-52
lines changed

devtools/emulate-tools.py

Lines changed: 48 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,71 +5,67 @@
55
behaviour by using pysam's facilities.
66
"""
77

8+
import argparse
89
import gzip
910
import os
1011
import sys
1112
import tempfile
1213

1314
import pysam
15+
import pysam.samtools
16+
import pysam.bcftools
1417

15-
cmd = os.path.basename(sys.argv[0])
16-
print(f"emulate-tools.py: {cmd}", file=sys.stderr)
18+
command = os.path.basename(sys.argv[0])
1719

18-
if cmd == 'samtools' or cmd == 'bcftools':
19-
if len(sys.argv) > 1:
20-
print(f"emulate-tools.py: {cmd} {sys.argv[1]}", file=sys.stderr)
21-
tool = pysam.utils.PysamDispatcher(cmd, sys.argv[1], ())
22-
print(f"emulate-tools.py: => {tool}", file=sys.stderr)
23-
args = sys.argv[2:]
24-
try:
25-
if '-?' in args:
26-
print(tool.usage())
27-
elif '-o' in args or '-bo' in args or '-Co' in args:
28-
print("emulate-tools.py: pre for -o", file=sys.stderr)
29-
tool(*args, catch_stdout=False)
30-
print("emulate-tools.py: post for -o", file=sys.stderr)
31-
else:
32-
print("emulate-tools.py: pre for default", file=sys.stderr)
33-
output = tool(*args)
34-
print("emulate-tools.py: post for default", file=sys.stderr)
35-
if isinstance(output, str):
36-
sys.stdout.write(output)
37-
elif isinstance(output, bytes):
38-
sys.stdout.buffer.write(output)
39-
40-
except pysam.utils.SamtoolsError as e:
41-
sys.exit('emulate-tools.py: {}'.format(e))
20+
if command in ("samtools", "bcftools"):
21+
command = f"{command} {sys.argv[1]}"
22+
args = sys.argv[2:]
4223

24+
if command == "samtools view":
25+
pysam.samtools.view(*args)
26+
elif command == "samtools index":
27+
pysam.samtools.index(*args)
28+
elif command == "samtools faidx":
29+
pysam.samtools.faidx(*args)
30+
elif command == "samtools calmd":
31+
pysam.samtools.calmd(*args)
32+
elif command == "bcftools view":
33+
pysam.bcftools.view(*args)
34+
elif command == "bcftools index":
35+
pysam.bcftools.index(*args)
4336
else:
44-
if cmd == 'samtools':
45-
v = pysam.version.__samtools_version__
46-
else:
47-
v = pysam.version.__bcftools_version__
48-
print('Program: {}\nVersion: {}'.format(cmd, v), file=sys.stderr)
37+
sys.exit(f"emulate-tools.py: unknown subcommand {command!r}")
4938

50-
elif cmd == 'bgzip':
51-
if len(sys.argv) == 1:
52-
f = tempfile.NamedTemporaryFile(delete=False)
53-
f.write(sys.stdin.buffer.read())
54-
f.close()
55-
pysam.tabix_compress(f.name, '-', force=True)
56-
os.remove(f.name)
39+
else:
40+
parser = argparse.ArgumentParser()
41+
parser.add_argument("-c", action="store_false", dest="decompress")
42+
parser.add_argument("-d", action="store_true", dest="decompress")
43+
parser.add_argument("-f", action="store_true", dest="force")
44+
parser.add_argument("-p", dest="preset")
45+
parser.add_argument("input_file", nargs="?")
46+
opt = parser.parse_args()
5747

58-
elif len(sys.argv) == 2 and sys.argv[1] == '-d':
59-
with gzip.open(sys.stdin.buffer, 'rb') as f:
60-
sys.stdout.buffer.write(f.read())
48+
if command == "bgzip":
49+
if opt.decompress:
50+
print("gunzip from stdin", file=sys.stderr)
51+
with gzip.open(sys.stdin.buffer, 'rb') as f:
52+
sys.stdout.buffer.write(f.read())
6153

62-
elif len(sys.argv) == 3 and sys.argv[1] == '-c':
63-
pysam.tabix_compress(sys.argv[2], '-', force=True)
54+
elif opt.input_file:
55+
print(f"bgzip {opt.input_file!r}", file=sys.stderr)
56+
pysam.tabix_compress(opt.input_file, "-", force=True)
6457

65-
else:
66-
sys.exit('emulate-tools.py: unrecognised bgzip command')
58+
else:
59+
print("bgzip from stdin", file=sys.stderr)
60+
f = tempfile.NamedTemporaryFile(delete=False)
61+
f.write(sys.stdin.buffer.read())
62+
f.close()
63+
pysam.tabix_compress(f.name, "-", force=True)
64+
os.remove(f.name)
6765

68-
elif cmd == 'tabix':
69-
if len(sys.argv) == 5 and sys.argv[1] == '-f' and sys.argv[2] == '-p':
70-
pysam.tabix_index(sys.argv[4], preset=sys.argv[3])
71-
else:
72-
sys.exit('emulate-tools.py: unrecognised tabix command')
66+
elif command == "tabix":
67+
print(f"pysam.tabix_index({opt.input_file!r}, preset={opt.preset!r})", file=sys.stderr)
68+
pysam.tabix_index(opt.input_file, preset=opt.preset)
7369

74-
else:
75-
sys.exit('emulate-tools.py: unknown command "{}"'.format(cmd))
70+
else:
71+
sys.exit(f"emulate-tools.py: unknown command {command!r}")

0 commit comments

Comments
 (0)