Skip to content

Commit

Permalink
Reset Node.hg_snp_set when Tree is reinstantiated (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
dpoznik authored Mar 19, 2024
1 parent 254cfa1 commit e38066f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
Format based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)


## [2.1.9]

### Fixed
- `Node.hg_snp` values would drift upon repeated `Tree` instantiation

[2.1.9]: https://github.com/23andMe/yhaplo/compare/2.1.8..2.1.9


## [2.1.8]

### Added
Expand Down
17 changes: 17 additions & 0 deletions tests/test_tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import numpy as np

from yhaplo.config import Config
from yhaplo.tree import Tree
from yhaplo.utils.context_managers import logging_disabled


def test_hg_snp_idempotency():
config = Config(suppress_output=True)
with logging_disabled():
tree_1 = Tree(config)
tree_2 = Tree(config)

hg_snps_1 = np.array([node.hg_snp for node in tree_1.depth_first_node_list])
hg_snps_2 = np.array([node.hg_snp for node in tree_2.depth_first_node_list])

assert (hg_snps_1 == hg_snps_2).all()
21 changes: 10 additions & 11 deletions yhaplo/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Node:
tree: "tree_module.Tree"
config: Config
args: argparse.Namespace
hg_snp_set: set[str] = set()
hg_snp_set: set[str]

def __init__(
self,
Expand All @@ -77,7 +77,7 @@ def __init__(
self.parent = parent
if parent is None:
if tree is not None:
type(self).set_tree_config_and_args(tree)
type(self).set_class_variables(tree)
else:
raise ValueError("Root node requires a tree instance")

Expand Down Expand Up @@ -204,12 +204,13 @@ def most_highly_ranked_dropped_marker(self) -> "snp_module.DroppedMarker":
# Class methods
# ----------------------------------------------------------------------
@classmethod
def set_tree_config_and_args(cls, tree: "tree_module.Tree") -> None:
def set_class_variables(cls, tree: "tree_module.Tree") -> None:
"""Set tree, config, and args."""

cls.tree = tree
cls.config = tree.config
cls.args = tree.args
cls.hg_snp_set = set()

@classmethod
def truncate_haplogroup_label(cls, haplogroup: str) -> str:
Expand Down Expand Up @@ -308,14 +309,12 @@ def priority_sort_snp_list_and_set_hg_snp(self) -> None:
self.hg_snp = self.parent.hg_snp + symbol

# Uniquify if necessary
if self.hg_snp in type(self).hg_snp_set:
i = 1
hg_snp_uniqe = f"{self.hg_snp}{i}"
while hg_snp_uniqe in type(self).hg_snp_set:
i += 1
hg_snp_uniqe = f"{self.hg_snp}{i}"

self.hg_snp = hg_snp_uniqe
original_hg_snp = self.hg_snp
i = 0
while self.hg_snp in type(self).hg_snp_set:
i += 1
self.hg_snp = f"{original_hg_snp}{i}"

else:
logger.warning(
"WARNING. Attempted to set star label, "
Expand Down

0 comments on commit e38066f

Please sign in to comment.