Skip to content

Commit

Permalink
update identities wrapper on Alignment class
Browse files Browse the repository at this point in the history
  • Loading branch information
thomashopf committed Nov 27, 2024
1 parent 4d794c0 commit 4c7a369
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions evcouplings/align/alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,7 @@ def pair_frequencies(self):

return self._pair_frequencies

def identities_to(self, seq, normalize=True):
def identities_to(self, seq, normalize=True, exclude_invalid=True):
"""
Calculate sequence identity between sequence
and all sequences in the alignment.
Expand All @@ -1001,17 +1001,29 @@ def identities_to(self, seq, normalize=True):
normalize : bool, optional (default: True)
Calculate relative identity between 0 and 1
by normalizing with length of alignment
exclude_invalid : bool, optional (default: True)
Exclude gaps and lowercase characters from identity
calculation
"""
self.__ensure_mapped_matrix()

# make sure this doesnt break with strings
# make sure this doesn't break with strings
seq = np.array(list(seq))

seq_mapped = map_matrix(seq, self.alphabet_map)
ids = identities_to_seq(seq_mapped, self.matrix_mapped)

# value to exclude from identity calculation (-1 if gaps should be included)
if exclude_invalid:
exclude_value = self.alphabet_map[self.alphabet_default]
else:
exclude_value = -1

ids = identities_to_seq(
seq_mapped, self.matrix_mapped, exclude_value
)

if normalize:
return ids / self.L
l = (seq_mapped != exclude_value).sum()
return ids / l
else:
return ids

Expand Down

0 comments on commit 4c7a369

Please sign in to comment.