Skip to content

Commit

Permalink
Merge pull request #668 from tomkinsc/ct-priorities-parsing-bugfix
Browse files Browse the repository at this point in the history
fix parsing of priorities tsv file to allow spaces in sequence IDs
  • Loading branch information
huddlej authored Feb 12, 2021
2 parents fa5fa43 + 190ae73 commit 528cf28
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion augur/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def read_priority_scores(fname):
with open(fname, encoding='utf-8') as pfile:
return defaultdict(float, {
elems[0]: float(elems[1])
for elems in (line.strip().split() for line in pfile.readlines())
for elems in (line.strip().split('\t') if '\t' in line else line.strip().split() for line in pfile.readlines())
})
except Exception as e:
print(f"ERROR: missing or malformed priority scores file {fname}", file=sys.stderr)
Expand Down
14 changes: 14 additions & 0 deletions tests/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ def mock_run_shell_command(mocker):
mocker.patch("augur.filter.run_shell_command")


@pytest.fixture
def mock_priorities_file_valid_with_spaces_and_tabs(mocker):
mocker.patch(
"builtins.open", mocker.mock_open(read_data="strain 1\t5\nstrain 2\t6\nstrain 3\t8\n")
)

class TestFilter:
def test_read_vcf_compressed(self):
seq_keep, all_seq = augur.filter.read_vcf(
Expand Down Expand Up @@ -89,6 +95,14 @@ def test_read_priority_scores_malformed(self, mock_priorities_file_malformed):
# builtins.open is stubbed, but we need a valid file to satisfy the existence check
augur.filter.read_priority_scores("tests/builds/tb/data/lee_2015.vcf")

def test_read_priority_scores_valid_with_spaces_and_tabs(self, mock_priorities_file_valid_with_spaces_and_tabs):
# builtins.open is stubbed, but we need a valid file to satisfy the existence check
priorities = augur.filter.read_priority_scores(
"tests/builds/tb/data/lee_2015.vcf"
)

assert priorities == {"strain 1": 5, "strain 2": 6, "strain 3": 8}

def test_read_priority_scores_does_not_exist(self):
with pytest.raises(FileNotFoundError):
augur.filter.read_priority_scores("/does/not/exist.txt")
Expand Down

0 comments on commit 528cf28

Please sign in to comment.