anndata_mcp.tools.utils
=======================

.. py:module:: anndata_mcp.tools.utils






Module Contents
---------------

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

   Bases: :py:obj:`zarr.storage.FsspecStore`


   A store that tracks the keys that have been accessed.


   .. py:method:: get(key, *args, **kwargs)
      :async:


      Get a key from the store.



.. py:function:: 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.

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

   :returns: A lazily-loaded AnnData object
   :rtype: AnnData


.. py:function:: truncate_string(string: str, max_output_len: int | None = None) -> str

   Truncate a string to the maximum length.


.. py:function:: get_shape_str(obj: Any) -> str

   Get the shape of an object as a string.


.. py:function:: class_string_to_type(class_string: str) -> str

   Convert a class string to a type.


.. py:function:: raw_type_to_string(raw_type: type, full_name: bool = False) -> str

   Convert a raw type to a string.


.. py:function:: extract_original_type(obj: Any) -> type

   Extract the original type of an object.


.. py:function:: extract_original_type_string(obj: Any, full_name: bool = False) -> str

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


.. py:function:: parse_slice(slice_str: str | None) -> slice

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

   :param slice_str: Slice string
   :type slice_str: str, optional

   :returns: Parsed slice object
   :rtype: slice


.. py:function:: 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.


.. py:function:: 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.


.. py:function:: array_to_csv(array: numpy.ndarray) -> str

   Convert a numpy array to a CSV string.


.. py:function:: 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.


.. py:function:: 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.


.. py:function:: 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.


.. py:function:: select_by_glob(items: list[str] | pandas.Index, pattern: str)

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


.. py:function:: 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.


.. py:function:: 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.

   :param obj: The object to traverse
   :type obj: Any
   :param keys: List of keys to traverse through the nested structure
   :type keys: list[str]

   :returns: The value at the nested key path
   :rtype: Any

   :raises KeyError: If any key in the path is not found
   :raises AttributeError: If any attribute in the path is not found


