From e93d4485efd60b6650ad539d8bc118db52abaa7d Mon Sep 17 00:00:00 2001 From: Patrick Kunzmann Date: Sun, 8 Sep 2024 16:13:05 +0200 Subject: [PATCH] Add string representation for `SequenceProfile` --- src/biotite/sequence/profile.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/biotite/sequence/profile.py b/src/biotite/sequence/profile.py index d208b2b3f..f44c5d6af 100644 --- a/src/biotite/sequence/profile.py +++ b/src/biotite/sequence/profile.py @@ -156,8 +156,23 @@ def gaps(self, new_gaps): ) self._gaps = new_gaps + def __str__(self): + # Add an additional row and column for the position and symbol indicators + print_matrix = np.full( + (self.symbols.shape[0] + 1, self.symbols.shape[1] + 1), "", dtype=object + ) + print_matrix[1:, 1:] = self.symbols.astype(str) + print_matrix[0, 1:] = [str(sym) for sym in self.alphabet] + print_matrix[1:, 0] = [str(i) for i in range(self.symbols.shape[0])] + max_len = len(max(print_matrix.flatten(), key=len)) + return "\n".join( + [ + " ".join([str(cell).rjust(max_len) for cell in row]) + for row in print_matrix + ] + ) + def __repr__(self): - """Represent SequenceProfile as a string for debugging.""" return ( f"SequenceProfile(np.{np.array_repr(self.symbols)}, " f"np.{np.array_repr(self.gaps)}, Alphabet({self.alphabet}))"