Skip to content

Commit d47f97c

Browse files
authored
Merge pull request #1884 from benjeffery/fix_dump
Add individual parents to text dump
2 parents c352421 + 7a766a7 commit d47f97c

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

python/CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@
8080
- Add the ``discrete_genome`` property to the TreeSequence class which is true if
8181
all coordinates are discrete (:user:`jeromekelleher`, :issue:`1144`, :pr:`1819`)
8282

83+
**Fixes**
84+
85+
- `dump_tables` omitted individual parents. (:user:`benjeffery`, :issue:`1828`, :pr:`1884`)
86+
8387
--------------------
8488
[0.3.7] - 2021-07-08
8589
--------------------

python/tests/test_highlevel.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2650,28 +2650,57 @@ def convert(v):
26502650
assert str(mutation.parent) == splits[4]
26512651
assert tests.base64_encode(mutation.metadata) == splits[5]
26522652

2653+
def verify_individuals_format(self, ts, individuals_file, precision):
2654+
"""
2655+
Verifies that the individuals we output have the correct form.
2656+
"""
2657+
2658+
def convert(v):
2659+
return "{:.{}f}".format(v, precision)
2660+
2661+
output_individuals = individuals_file.read().splitlines()
2662+
assert len(output_individuals) - 1 == ts.num_individuals
2663+
assert list(output_individuals[0].split()) == [
2664+
"id",
2665+
"flags",
2666+
"location",
2667+
"parents",
2668+
"metadata",
2669+
]
2670+
for individual, line in zip(ts.individuals(), output_individuals[1:]):
2671+
splits = line.split("\t")
2672+
assert str(individual.id) == splits[0]
2673+
assert str(individual.flags) == splits[1]
2674+
assert ",".join(map(str, individual.location)) == splits[2]
2675+
assert ",".join(map(str, individual.parents)) == splits[3]
2676+
assert tests.base64_encode(individual.metadata) == splits[4]
2677+
26532678
def test_output_format(self):
26542679
for ts in get_example_tree_sequences():
26552680
for precision in [2, 7]:
26562681
nodes_file = io.StringIO()
26572682
edges_file = io.StringIO()
26582683
sites_file = io.StringIO()
26592684
mutations_file = io.StringIO()
2685+
individuals_file = io.StringIO()
26602686
ts.dump_text(
26612687
nodes=nodes_file,
26622688
edges=edges_file,
26632689
sites=sites_file,
26642690
mutations=mutations_file,
2691+
individuals=individuals_file,
26652692
precision=precision,
26662693
)
26672694
nodes_file.seek(0)
26682695
edges_file.seek(0)
26692696
sites_file.seek(0)
26702697
mutations_file.seek(0)
2698+
individuals_file.seek(0)
26712699
self.verify_nodes_format(ts, nodes_file, precision)
26722700
self.verify_edges_format(ts, edges_file, precision)
26732701
self.verify_sites_format(ts, sites_file, precision)
26742702
self.verify_mutations_format(ts, mutations_file, precision)
2703+
self.verify_individuals_format(ts, individuals_file, precision)
26752704

26762705
def verify_approximate_equality(self, ts1, ts2):
26772706
"""

python/tskit/trees.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3731,16 +3731,28 @@ def dump_text(
37313731
print(row, file=mutations)
37323732

37333733
if individuals is not None:
3734-
print("id", "flags", "location", "metadata", sep="\t", file=individuals)
3734+
print(
3735+
"id",
3736+
"flags",
3737+
"location",
3738+
"parents",
3739+
"metadata",
3740+
sep="\t",
3741+
file=individuals,
3742+
)
37353743
for individual in self.individuals():
37363744
metadata = individual.metadata
37373745
if base64_metadata:
37383746
metadata = base64.b64encode(metadata).decode(encoding)
37393747
location = ",".join(map(str, individual.location))
3740-
row = ("{id}\t" "{flags}\t" "{location}\t" "{metadata}").format(
3748+
parents = ",".join(map(str, individual.parents))
3749+
row = (
3750+
"{id}\t" "{flags}\t" "{location}\t" "{parents}\t" "{metadata}"
3751+
).format(
37413752
id=individual.id,
37423753
flags=individual.flags,
37433754
location=location,
3755+
parents=parents,
37443756
metadata=metadata,
37453757
)
37463758
print(row, file=individuals)

0 commit comments

Comments
 (0)