Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions src/biotite/structure/atoms.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from collections.abc import Sequence
import numpy as np
from biotite.copyable import Copyable
from biotite.structure import query
from biotite.structure.bonds import BondList


Expand Down Expand Up @@ -440,6 +441,72 @@ def _copy_annotations(self, clone):
if self._bonds is not None:
clone._bonds = self._bonds.copy()

def query(self, expr):
"""
Query the AtomArray using a pandas-like expression string.

Parameters
----------
expr : str
Query expression in pandas-like syntax.

Returns
-------
filtered_array : AtomArray or AtomArrayStack
Filtered atom array containing only atoms that match the query expression.

Examples
--------
Select all CA atoms in chain A:

>>> ca_atoms = atom_array.query("(chain_id == 'A') & (atom_name == 'CA')")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also print the return value here?


Select atoms without NaN coordinates:

>>> valid_atoms = atom_array.query("~has_nan_coord()")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's nice that all these new methods have doctest examples. However to make users aware of this new feature we should probably mention this atom selection alternative also in the tutorial. I think doc/tutorial/structure/filter.rst would be fitting.

"""
return query.query(self, expr)

def mask(self, expr):
"""
Query the AtomArray using a pandas-like expression string and return a boolean mask.

Parameters
----------
expr : str
Query expression in pandas-like syntax.

Returns
-------
mask : ndarray, dtype=bool
Boolean numpy array indicating which atoms match the query.

Examples
--------
>>> mask = atom_array.mask("atom_name == 'CA'")
"""
return query.mask(self, expr)

def idxs(self, expr):
"""
Query the AtomArray using a pandas-like expression string and return the indices of matching atoms.

Parameters
----------
expr : str
Query expression in pandas-like syntax.

Returns
-------
indices : ndarray, dtype=int
Numpy array of indices for atoms that match the query expression.

Examples
--------
>>> idxs = atom_array.idxs("atom_name == 'CA'")
"""
return query.idxs(self, expr)


class Atom(Copyable):
"""
Expand Down
Loading
Loading