biotite-dev / biotite

A comprehensive library for computational molecular biology
https://www.biotite-python.org
BSD 3-Clause "New" or "Revised" License
581 stars 92 forks source link

Custom annotations do not appear in Atom `__repr__()` #602

Closed ljarosch closed 1 week ago

ljarosch commented 2 weeks ago

When adding custom annotations to an AtomArray, they are not displayed in the repr() of that array. I think this is because the annotations are hardcoded in the __repr__ method of Atom.

Showing all annotations would make it more straightforward to interactively do structural analysis, especially in a Notebook context where you frequently print atom arrays.

Below is a suggestion for a potential fix. If this would be an interesting feature to be added to Biotite I'm happy to make a PR.

def __repr__(self):
    # print out key-value pairs and format strings in quotation marks
    annot_parts = [
        f'{key}="{value}"' if isinstance(value, str) else f'{key}={value}'
        for key, value in self._annot.items()
    ]

    annot = ', '.join(annot_parts)
    return f'Atom(np.{np.array_repr(self.coord)}, {annot})'
padix-key commented 2 weeks ago

Since repr() is intended to return actual code that recreates an object (https://docs.python.org/3/library/functions.html#repr), I would even consider the current behavior a bug, as information would be missing. Hence, your PR would be welcome.