Skip to content

Commit

Permalink
implementing Variant.__repr__
Browse files Browse the repository at this point in the history
added tests for contents

edit changelog

fix changelog entry
  • Loading branch information
chriscrsmith authored and mergify[bot] committed Jan 19, 2023
1 parent ba5d66a commit ad4dd4a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
10 changes: 10 additions & 0 deletions python/CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
--------------------
[0.5.5] - 2023-01-XX
--------------------

**Features**

- Add ``__repr__`` for variants to return a string representation of the raw data
without spewing megabytes of text (:user:`chriscrsmith`, :pr:`2695`, :issue:`2694`)


--------------------
[0.5.4] - 2023-01-13
--------------------
Expand Down
16 changes: 15 additions & 1 deletion python/tests/test_genotypes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# MIT License
#
# Copyright (c) 2019-2022 Tskit Developers
# Copyright (c) 2019-2023 Tskit Developers
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -2215,3 +2215,17 @@ def test_variant_html_repr_no_site(self):
html = v._repr_html_()
ElementTree.fromstring(html)
assert len(html) > 1600

def test_variant_repr(self, ts_fixture):
v = next(ts_fixture.variants())
str_rep = repr(v)
assert len(str_rep) > 0 and len(str_rep) < 10000
assert re.search(r"\AVariant", str_rep)
assert re.search(rf"\'site\': Site\(id={v.site.id}", str_rep)
assert re.search(rf"position={v.position}", str_rep)
alleles = re.escape("'alleles': " + str(v.alleles))
assert re.search(rf"{alleles}", str_rep)
assert re.search(r"\'genotypes\': array\(\[", str_rep)
assert re.search(rf"position={v.position}", str_rep)
assert re.search(rf"\'has_missing_data\': {v.has_missing_data}", str_rep)
assert re.search(rf"\'isolated_as_missing\': {v.isolated_as_missing}", str_rep)
13 changes: 12 additions & 1 deletion python/tskit/genotypes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# MIT License
#
# Copyright (c) 2018-2022 Tskit Developers
# Copyright (c) 2018-2023 Tskit Developers
# Copyright (c) 2015-2018 University of Oxford
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down Expand Up @@ -340,6 +340,17 @@ def _repr_html_(self) -> str:
"""
return util.variant_html(self)

def __repr__(self):
d = {
"site": self.site,
"samples": self.samples,
"alleles": self.alleles,
"genotypes": self.genotypes,
"has_missing_data": self.has_missing_data,
"isolated_as_missing": self.isolated_as_missing,
}
return f"Variant({repr(d)})"


#
# Miscellaneous auxiliary methods.
Expand Down

0 comments on commit ad4dd4a

Please sign in to comment.