Skip to content

Commit 209396c

Browse files
committed
Test suite now has option to decode cram files to test string level equality, rather than binary equality
1 parent bd4a804 commit 209396c

1 file changed

Lines changed: 26 additions & 8 deletions

File tree

tests/test_umi_tools.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,28 @@ def get_tests_directory():
4747

4848
return testdir
4949

50-
51-
def _read(fn):
50+
def _read(fn, cram=False):
5251
if fn.endswith(".gz"):
5352
with gzip.open(fn) as inf:
5453
data = inf.read()
5554
else:
5655
with open(fn, "rb") as inf:
5756
data = inf.read()
5857

58+
if fn.endswith(".cram") or cram:
59+
import pysam
60+
data = pysam.AlignmentFile(fn)
61+
reference = data.header.to_dict()["SQ"]
62+
reference = set(x["UR"] for x in reference)
63+
assert len(reference) == 1, "Test suite does not support multiple reference files"
64+
reference = list(reference)[0]
65+
66+
data.close()
67+
with pysam.AlignmentFile(fn, reference_filename=reference) as inf:
68+
data = [inf.text]
69+
data.extend([read.to_string() for read in inf.fetch(until_eof=True)])
70+
return data
71+
5972
if IS_PY3:
6073
try:
6174
data = data.decode("ascii")
@@ -117,7 +130,8 @@ def get_script_parameters():
117130
values['outputs'],
118131
values['references'],
119132
current_dir,
120-
values.get('sort', False)
133+
values.get('sort', False),
134+
values.get('cram', False)
121135
))
122136
return parameters
123137

@@ -152,13 +166,14 @@ def test_script_has_main(script):
152166
assert [x for x in open(script) if x.startswith("def main(")], "no main function"
153167

154168

155-
@pytest.mark.parametrize("test_name,stdin,options,outputs,references,current_dir,sort", get_script_parameters())
169+
@pytest.mark.parametrize("test_name,stdin,options,outputs,references,current_dir,sort,cram", get_script_parameters())
156170
def test_script(test_name,
157171
stdin,
158172
options, outputs,
159173
references,
160174
current_dir,
161-
sort):
175+
sort,
176+
cram):
162177
'''check script.
163178
# 1. Name of the script
164179
# 2. Filename to use as stdin
@@ -241,10 +256,13 @@ def test_script(test_name,
241256
msg = "reference file '%s' does not exist (%s): %s" %\
242257
(reference, tmpdir, statement)
243258

259+
if reference.endswith(".cram"):
260+
cram = True
261+
244262
if not fail:
245263

246-
a = _read(output)
247-
b = _read(reference)
264+
a = _read(output, cram)
265+
b = _read(reference, cram)
248266

249267
if sort:
250268
a = sorted(a)
@@ -272,7 +290,7 @@ def test_script(test_name,
272290
'in diff are with respect to sorted reference '
273291
'and output files\n\n')
274292

275-
msg += "first 10 differences: {}".format(
293+
msg += "\nfirst 10 differences: {}".format(
276294
"\n--\n".join(
277295
["\n".join(map(str, (x)))
278296
for x in diffs]))

0 commit comments

Comments
 (0)