Skip to content

Commit e38066f

Browse files
authored
Reset Node.hg_snp_set when Tree is reinstantiated (#30)
1 parent 254cfa1 commit e38066f

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
Format based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
44

55

6+
## [2.1.9]
7+
8+
### Fixed
9+
- `Node.hg_snp` values would drift upon repeated `Tree` instantiation
10+
11+
[2.1.9]: https://github.com/23andMe/yhaplo/compare/2.1.8..2.1.9
12+
13+
614
## [2.1.8]
715

816
### Added

tests/test_tree.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import numpy as np
2+
3+
from yhaplo.config import Config
4+
from yhaplo.tree import Tree
5+
from yhaplo.utils.context_managers import logging_disabled
6+
7+
8+
def test_hg_snp_idempotency():
9+
config = Config(suppress_output=True)
10+
with logging_disabled():
11+
tree_1 = Tree(config)
12+
tree_2 = Tree(config)
13+
14+
hg_snps_1 = np.array([node.hg_snp for node in tree_1.depth_first_node_list])
15+
hg_snps_2 = np.array([node.hg_snp for node in tree_2.depth_first_node_list])
16+
17+
assert (hg_snps_1 == hg_snps_2).all()

yhaplo/node.py

+10-11
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class Node:
5656
tree: "tree_module.Tree"
5757
config: Config
5858
args: argparse.Namespace
59-
hg_snp_set: set[str] = set()
59+
hg_snp_set: set[str]
6060

6161
def __init__(
6262
self,
@@ -77,7 +77,7 @@ def __init__(
7777
self.parent = parent
7878
if parent is None:
7979
if tree is not None:
80-
type(self).set_tree_config_and_args(tree)
80+
type(self).set_class_variables(tree)
8181
else:
8282
raise ValueError("Root node requires a tree instance")
8383

@@ -204,12 +204,13 @@ def most_highly_ranked_dropped_marker(self) -> "snp_module.DroppedMarker":
204204
# Class methods
205205
# ----------------------------------------------------------------------
206206
@classmethod
207-
def set_tree_config_and_args(cls, tree: "tree_module.Tree") -> None:
207+
def set_class_variables(cls, tree: "tree_module.Tree") -> None:
208208
"""Set tree, config, and args."""
209209

210210
cls.tree = tree
211211
cls.config = tree.config
212212
cls.args = tree.args
213+
cls.hg_snp_set = set()
213214

214215
@classmethod
215216
def truncate_haplogroup_label(cls, haplogroup: str) -> str:
@@ -308,14 +309,12 @@ def priority_sort_snp_list_and_set_hg_snp(self) -> None:
308309
self.hg_snp = self.parent.hg_snp + symbol
309310

310311
# Uniquify if necessary
311-
if self.hg_snp in type(self).hg_snp_set:
312-
i = 1
313-
hg_snp_uniqe = f"{self.hg_snp}{i}"
314-
while hg_snp_uniqe in type(self).hg_snp_set:
315-
i += 1
316-
hg_snp_uniqe = f"{self.hg_snp}{i}"
317-
318-
self.hg_snp = hg_snp_uniqe
312+
original_hg_snp = self.hg_snp
313+
i = 0
314+
while self.hg_snp in type(self).hg_snp_set:
315+
i += 1
316+
self.hg_snp = f"{original_hg_snp}{i}"
317+
319318
else:
320319
logger.warning(
321320
"WARNING. Attempted to set star label, "

0 commit comments

Comments
 (0)