Skip to content

Commit

Permalink
fixed bug in inferring ancestral states with ambiguous states
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjones315 committed Feb 2, 2024
1 parent a9c8cbd commit 6a600eb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
30 changes: 20 additions & 10 deletions cassiopeia/data/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,27 @@ def get_lca_characters(
all_states = [
vec[i] for vec in vecs if vec[i] != missing_state_indicator
]
chars = set.intersection(
*map(
set,
[
state if is_ambiguous_state(state) else [state]
for state in all_states
],

# this check is specifically if all_states consists of a single
# ambiguous state.
if len(all_states) == 1:
state = all_states[0]
if is_ambiguous_state(state) and len(state) == 1:
lca_vec[i] = state[0]
else:
lca_vec[i] = all_states[0]
else:
chars = set.intersection(
*map(
set,
[
state if is_ambiguous_state(state) else [state]
for state in all_states
],
)
)
)
if len(chars) == 1:
lca_vec[i] = list(chars)[0]
if len(chars) == 1:
lca_vec[i] = list(chars)[0]
return lca_vec


Expand Down
11 changes: 11 additions & 0 deletions test/data_tests/data_utilities_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,17 @@ def test_lca_characters_ambiguous(self):
)
self.assertEqual(ret_vec, [1, 2, 3, 0, 5])

def test_lca_characters_ambiguous_and_missing(self):
vecs = [
[(1, 1), (0, 2), (3,), (4,), (5,)],
[1, -1, -1, 3, -1],
[1, -1, 3, 2, -1],
]
ret_vec = data_utilities.get_lca_characters(
vecs, missing_state_indicator=-1
)
self.assertEqual(ret_vec, [1, (0,2), 3, 0, 5])

def test_resolve_most_abundant(self):
state = (1, 2, 3, 3)
self.assertEqual(data_utilities.resolve_most_abundant(state), 3)
Expand Down

0 comments on commit 6a600eb

Please sign in to comment.