Skip to content

Commit

Permalink
Make the check for negative matches/uncovered positions compatible wi…
Browse files Browse the repository at this point in the history
…th clusters; add a similar check in calc_rels_per_read
  • Loading branch information
matthewfallan committed Jun 26, 2024
1 parent a4570a2 commit b708ce4
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/seismicrna/core/batch/count.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,16 +261,18 @@ def calc_rels_per_pos(mutations: dict[int, dict[int, np.ndarray]],
# The number of matches is the coverage minus the number of
# reads with another kind of relationship that is not the
# no-coverage relationship (no coverage is counted later).
if (num_match := cover_per_pos.loc[pos_base] - num_reads_pos) < 0:
num_match = cover_per_pos.loc[pos_base] - num_reads_pos
if np.atleast_1d(num_match)[0] < 0:
raise ValueError("Number of matches must be ≥ 0, "
f"but got {num_match} at position {pos}")
counts[MATCH].loc[pos_base] = num_match
# The number of non-covered positions is the number of reads
# minus the number that cover the position.
if (num_cover := num_reads - cover_per_pos.loc[pos_base]) < 0:
num_nocov = num_reads - cover_per_pos.loc[pos_base]
if np.atleast_1d(num_nocov)[0] < 0:
raise ValueError("Number of non-covered positions must be ≥ 0, "
f"but got {num_cover} at position {pos}")
counts[NOCOV].loc[pos_base] = num_cover
f"but got {num_nocov} at position {pos}")
counts[NOCOV].loc[pos_base] = num_nocov
return dict(counts)


Expand All @@ -291,6 +293,9 @@ def calc_rels_per_read(mutations: dict[int, dict[int, np.ndarray]],
for mut, reads in mutations[pos].items():
rows = read_indexes[reads]
counts[MATCH].values[rows, column] -= 1
if counts[MATCH].values[rows, column].min(initial=0) < 0:
raise ValueError("Number of matches must be ≥ 0, but got "
f"{counts[MATCH].values[rows, column]}")
counts[mut].values[rows, column] += 1
return dict(counts)

Expand Down

0 comments on commit b708ce4

Please sign in to comment.