Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

in read_metadata update pandas series object name to reflect name col… #564

Merged
merged 2 commits into from
Jun 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions augur/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def read_metadata(fname, query=None):
raise ValueError("Duplicate strain '{}'".format(val.strain))
meta_dict[val.strain] = val.to_dict()
elif hasattr(val, "name"):
val = val.rename(val["name"])
if val.name in meta_dict:
raise ValueError("Duplicate name '{}'".format(val.name))
meta_dict[val.name] = val.to_dict()
Expand Down
58 changes: 57 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,24 @@ def test_read_mask_file_drm_file(self, tmpdir):
fh.write("\n".join(drm_lines))
assert utils.read_mask_file(drm_file) == expected_sites

def test_read_metadata_no_filename(self):
expected_result = {}, []
assert utils.read_metadata('') == expected_result

def test_read_metadata_bad_filename(self, tmpdir):
bad_filename = "incorrect_path.tsv"
expected_result = {}, []

meta_fn = str(tmpdir / "metadata.tsv")
meta_lines = ["strain\tlocation\tquality",
"c_good\tcolorado\tgood",
"n_bad\tnevada\tbad",]
with open(meta_fn, "w") as fh:
fh.write("\n".join(meta_lines))

utils.read_metadata(bad_filename, query='quality=="good" & location=="colorado"')
assert utils.read_metadata('') == expected_result

def test_read_metadata_with_good_query(self, tmpdir):
meta_fn = str(tmpdir / "metadata.tsv")
meta_lines = ["strain\tlocation\tquality",
Expand All @@ -121,4 +139,42 @@ def test_read_metadata_bad_query(self, tmpdir):
with open(meta_fn, "w") as fh:
fh.write("\n".join(meta_lines))
with pytest.raises(SystemExit):
utils.read_metadata(meta_fn, query='badcol=="goodval"')
utils.read_metadata(meta_fn, query='badcol=="goodval"')
def test_read_metadata_duplicate_strain(self, tmpdir):
meta_fn = str(tmpdir / "metadata.tsv")
meta_lines = ["strain\tlocation\tquality",
"c_good\tcolorado\tgood",
"c_bad\tcolorado\tbad",
"c_bad\tcolorado\tbad",
"n_good\tnevada\tgood"]
with open(meta_fn, "w") as fh:
fh.write("\n".join(meta_lines))
with pytest.raises(ValueError):
utils.read_metadata(meta_fn, query='location=="colorado"')

def test_read_metadata_duplicate_name(self, tmpdir):
meta_fn = str(tmpdir / "metadata.tsv")
meta_lines = ["name\tlocation\tquality",
"c_good\tcolorado\tgood",
"c_bad\tcolorado\tbad",
"c_bad\tcolorado\tbad",
"n_good\tnevada\tgood"]
with open(meta_fn, "w") as fh:
fh.write("\n".join(meta_lines))

with pytest.raises(ValueError):
utils.read_metadata(meta_fn)

def test_read_metadata_no_strain_or_name(self, tmpdir):
expected_result = {}, []

meta_fn = str(tmpdir / "metadata.tsv")
meta_lines = ["location\tquality",
"colorado\tgood",
"colorado\tbad",
"colorado\tbad",
"nevada\tgood"]
with open(meta_fn, "w") as fh:
fh.write("\n".join(meta_lines))

assert utils.read_metadata('') == expected_result