anndata_mcp.tools.utils#

Module Contents#

class anndata_mcp.tools.utils.AccessTrackingStore(fs: fsspec.asyn.AsyncFileSystem, read_only: bool = False, path: str = '/', allowed_exceptions: tuple[type[Exception], Ellipsis] = ALLOWED_EXCEPTIONS)#

Bases: zarr.storage.FsspecStore

A store that tracks the keys that have been accessed.

async get(key, *args, **kwargs)#

Get a key from the store.

anndata_mcp.tools.utils.read_lazy_general(path_or_url: str | pathlib.Path)#

Read an AnnData object lazily from either a file path or URL.

This function automatically detects whether the input is a URL or a file system path and handles it appropriately. For URLs, it uses AccessTrackingStore.from_url() to create a zarr store, then reads it lazily. For file paths, it uses read_lazy directly.

Parameters:

path_or_url (str | Path) – Either a file system path (e.g., “data/test.h5ad” or “data/test.zarr”) or a URL (e.g., “https://example.com/data.zarr/”)

Returns:

A lazily-loaded AnnData object

Return type:

AnnData

anndata_mcp.tools.utils.truncate_string(string: str, max_output_len: int | None = None) str#

Truncate a string to the maximum length.

anndata_mcp.tools.utils.get_shape_str(obj: Any) str#

Get the shape of an object as a string.

anndata_mcp.tools.utils.class_string_to_type(class_string: str) str#

Convert a class string to a type.

anndata_mcp.tools.utils.raw_type_to_string(raw_type: type, full_name: bool = False) str#

Convert a raw type to a string.

anndata_mcp.tools.utils.extract_original_type(obj: Any) type#

Extract the original type of an object.

anndata_mcp.tools.utils.extract_original_type_string(obj: Any, full_name: bool = False) str#

Extract the original type of an object and convert it to a string.

anndata_mcp.tools.utils.parse_slice(slice_str: str | None) slice#

Parse a slice string like ‘0:10’ or ‘:100’ into a slice object.

Parameters:

slice_str (str, optional) – Slice string

Returns:

Parsed slice object

Return type:

slice

anndata_mcp.tools.utils.extract_slice_from_dask_array(array: dask.array.core.Array, row_slice: slice, col_slice: slice) numpy.ndarray#

Extract a slice from a dask array.

anndata_mcp.tools.utils.extract_indices_from_dask_array(array: dask.array.core.Array, row_slice: slice, col_indices: list[int]) numpy.ndarray#

Extract data from a dask array using column indices.

anndata_mcp.tools.utils.array_to_csv(array: numpy.ndarray) str#

Convert a numpy array to a CSV string.

anndata_mcp.tools.utils.extract_data_from_dask_array(array: dask.array.core.Array, row_slice: slice, col_slice: slice, return_shape: bool = False) tuple[str, str] | str#

Extract data from a dask array.

anndata_mcp.tools.utils.extract_data_from_dask_array_with_indices(array: dask.array.core.Array, row_slice: slice, col_indices: list[int], return_shape: bool = False) tuple[str, str] | str#

Extract data from a dask array using column indices.

anndata_mcp.tools.utils.extract_data_from_dataset2d(dataset2d: anndata._core.xarray.Dataset2D, columns: list[str], row_slice: slice | None = None, index: bool = True, return_shape: bool = False) tuple[str, str] | str#

Extract data from a dataset2d.

anndata_mcp.tools.utils.select_by_glob(items: list[str] | pandas.Index, pattern: str)#

Select items from a list or index matching a glob pattern.

anndata_mcp.tools.utils.match_patterns(items: list[str] | pandas.Index, pattern_list: list[str]) tuple[list[str], str | None]#

Match items to patterns and return the matched items and a message listing any patterns that were not found.

anndata_mcp.tools.utils.get_nested_key(obj: Any, keys: list[str]) Any#

Retrieve a nested value from an object using a list of keys.

This function traverses through nested structures (dicts, objects with attributes, etc.) using the provided list of keys. It uses get() for dict-like objects and hasattr()/getattr() for objects with attributes where possible.

Parameters:
  • obj (Any) – The object to traverse

  • keys (list[str]) – List of keys to traverse through the nested structure

Returns:

The value at the nested key path

Return type:

Any

Raises:
  • KeyError – If any key in the path is not found

  • AttributeError – If any attribute in the path is not found