mgnipy.V2.mixins module#
- class BiomesTreeMixin[source]#
Bases:
object- show_tree(method='compact')[source]#
- Parameters:
method (Literal ['compact', 'show', 'print', 'horizontal', 'hshow', 'h', 'hprint', 'vertical', 'vshow', 'v', 'vprint'])
- property tree: Tree#
Convert the biomes metadata to a tree structure for visualization or analysis.
- Returns:
A tree representation of the biomes and their relationships.
- Return type:
Tree
- class CheckpointMixin[source]#
Bases:
objectCheckpoint manager for request-makers.
This mixin provides methods to write paginated results to disk as they are retrieved, and to load them back into memory on subsequent runs. It generates a unique cache key based on the query parameters and resource type, and organizes cached pages in a directory structure under a specified cache root.
The mixin assumes the host class has the following attributes: - self.params: A dictionary of query parameters for the current request. - self.config: An instance of MGnipyConfig containing configuration settings, including the cache directory. - self.resource: A string representing the type of resource being queried (e.g., “samples”, “runs”). - self._results: A dictionary mapping page numbers to lists of dictionaries containing the query results. - self.count: An integer representing the total number of records for the current query. - self.num_requests: An integer representing the total number of paginated requests needed for the current query. which is the case for: QuerySets, MGnifier, proxies
Example
>>> from mgnipy.V2.mgnifier.query_set import QuerySet >>> from mgnipy.V2.mixins import CheckpointMixin >>> # Creating a class that uses mixin >>> class MyQuerySet(QuerySet, CheckpointMixin): ... # Initialize QuerySet (not mixin) ... def __init__(self, *args, **kwargs): ... super().__init__(*args, **kwargs) >>> # init >>> qs = MyQuerySet(resource="studies") # default config is w/ cache enabled >>> # now can use checkpoint methods, e.g. >>> qs.cache_key # print the unique cache key '...' >>> qs.load_cache() []
- property cache_key: str #
Generate deterministic hash from resource + params.
- Returns:
A unique cache key for the current query parameters and resource. For a query to the ‘samples’ resource with parameters {‘biome_lineage’: ‘root:Environmental:Terrestrial’}, the cache key will be a SHA256 hash of the string representation of the resource and parameters, ensuring that identical queries will have the same cache key and thus access the same cached results.
- Return type:
Example
>>> from mgnipy.V2.mixins import CheckpointMixin >>> from mgnipy import MGnipyConfig >>> # Prepare parameters and config >>> params = {'lineage': 'root:Environmental:Terrestrial'} >>> resource = 'biome' >>> config = MGnipyConfig(cache_dir="/path/to/cache") >>> # Create CheckpointMixin instance and compute cache key >>> cache_handler = CheckpointMixin() >>> cache_handler.params = params >>> cache_handler.resource = resource >>> cache_handler.config = config >>> cache_handler.cache_key '1eb56ddf5a2e7d60d8155c8bbe01f032f959a2519d43e99f31f533abffa3166f'
- load_cache_manifest()[source]#
Load the cache manifest file if present, and update total records and total requests.
- Returns:
The contents of the manifest file, or an empty dictionary if the manifest is not found or fails to load.
- Return type:
- load_cache_results()[source]#
Load cached pages/request nums into results.
Loads cached pages from disk into the in-memory results dictionary (self._results), if available.
- try_load_cache()[source]#
Attempt to load cached results and manifest into memory if not already loaded. This method checks if the cache has already been loaded to avoid redundant operations. If the cache has not been loaded, it will attempt to load it and set the _cache_loaded attribute accordingly.
Notes
This method is intended to be called internally before accessing cached results.
If cache_dir is None then _cache_loaded will be True after initial attempt.
If an error occurs during cache loading, it will be logged, and _cache_loaded will be set to False.
Dependent on .mixins.CheckpointMixin
- Return type:
None
- class ClientManagerMixin[source]#
Bases:
objectContext manager mixin for managing the lifecycle of an HTTP client (synchronous or asynchronous) within a class. This mixin provides methods to enter and exit the context, ensuring that the client is properly initialized and closed, and handles cases where the client may have been closed or is not available.
Requirements#
self.client must be an instance of a class that implements the context manager protocol (i.e., has __enter__ and __exit__ methods for synchronous clients, and __aenter__ and __aexit__ methods for asynchronous clients).
self.config must be a configuration object or dictionary that can be used to initialize a new HTTP client instance if the existing client is closed or unavailable.
- property async_httpx_client: AsyncClient#
Get the asynchronous httpx client instance from the AuthenticatedClient.
- Returns:
The asynchronous httpx client instance.
- Return type:
httpx.AsyncClient
- property httpx_client: Client#
Get the synchronous httpx client instance from the AuthenticatedClient.
- Returns:
The synchronous httpx client instance.
- Return type:
httpx.Client
- class StreamMixin[source]#
Bases:
objectMixin providing streaming helpers for downloads.
# TODO remove below dependencies on mgnifier This mixin assumes the host class provides the following helpers/properties:
.exec.httpx_client and .exec.httpx_aclient attributes
_get_type_by_alias(alias) to resolve file types
downloads_df when needed for examples/tests
The implementation mirrors the streaming helpers previously defined on
MGazineso they can be reused by other classes.- stream(*, alias=None, url=None, chunksize=None, max_skip=5, **kwargs)[source]#
Streams a single download based on its alias or url.
If
chunksizeis specified then iterators of dataframes or strings will be returned; otherwise the full data will be returned as a single object.Supported formats and their handlers#
tsv: handled by
stream_pandas()(pandas) orstream_polars()(polars). Gzipped TSVs are supported via the gzip/compression options.csv: handled by
stream_pandas()/stream_polars()(sep=”,”).txt: handled by
stream_txt()(returns full text or yields line chunks).html: handled by
stream_html()(opens URL in browser).fasta: handled by
stream_fasta()(scikit-bio generator).gff: handled by
stream_gff()(scikit-bio generator).biom: handled by
stream_biom()(scikit-bio generator).gzipped HTTP resources: use
stream_gzipped()for a file-like object, orstream_json()for gzipped JSON content.jsonl / ndjson: handled by
stream_jsonl()(pandas or polars modes).json: handled by
stream_json()(returns full JSON or streams via ijson).tree/newick: handled by
stream_tree()(scikit-bio newick reader).other: if the URL ends with
.jsonit’s streamed viastream_json(); otherwise use the download helper for unsupported binary formats.
- param alias:
The alias of the download to stream.
- type alias:
Optional[str]
- param url:
The url of the download to stream.
- type url:
Optional[HttpUrl]
- param chunksize:
The size of the chunks to read from the stream.
- type chunksize:
Optional[int]
- param max_skip:
The maximum number of rows to skip before raising an error. Default is 5.
- type max_skip:
int, optional
- param **kwargs:
Additional keyword arguments to pass to the streamer function.
- returns:
The streamer result for the resolved alias or url.
- rtype:
Any
- stream_biom(url, **skbio_kwargs)[source]#
Stream a biom file from a URL using scikit-bio’s read function. Refer there for more info.
- Parameters:
url (str ) – The URL to the biom file to stream.
**skbio_kwargs – Additional keyword arguments passed to skbio.io.read(), such as into and verify.
- Returns:
A generator yielding scikit-bio Sequence objects parsed from the biom file.
- Return type:
Generator
- stream_fasta(url, **skbio_kwargs)[source]#
Stream a FASTA file from a URL using scikit-bio’s read function. Refer there for more info.
- Parameters:
url (str ) – The URL to the FASTA file to stream.
**skbio_kwargs – Additional keyword arguments passed to skbio.io.read(), such as into and verify.
- Returns:
A generator yielding scikit-bio Sequence objects parsed from the FASTA file.
- Return type:
Generator
- stream_gff(url, **skbio_kwargs)[source]#
Stream a GFF file from a URL using scikit-bio’s read function. Refer there for more info.
- Parameters:
url (str ) – The URL to the GFF file to stream.
**skbio_kwargs – Additional keyword arguments passed to skbio.io.read(), such as into and verify.
- Returns:
A generator yielding scikit-bio Sequence objects parsed from the GFF file.
- Return type:
Generator
- stream_gzipped(url, chunksize=None, decode=False, encoding='utf-8', errors='replace', **httpx_kwargs)[source]#
Stream a gzipped HTTP resource and present a file-like interface.
When
chunksizeis None the entire compressed payload is fetched and decompressed into memory. Whenchunksizeis provided a streaming file-like object is returned.- Parameters:
- Return type:
bytes | str | BufferedReader | TextIOWrapper
- stream_pandas(url, sep='\t', chunksize=None, max_skip=5, low_memory=False, **pd_kwargs)[source]#
Read a TSV from a URL or local file with resilient header handling.
The helper will retry with increasing
skiprowswhenpandasraises aParserError(useful for files with extra header lines). Whenchunksizeis provided an iterator is returned.- Parameters:
url (str ) – The URL or local file path to read the TSV from.
sep (str ) – The delimiter to use (default is tab).
chunksize (int or None) – If an integer is provided, returns an iterator that yields DataFrames of that many rows. If None, returns a single DataFrame.
max_skip (int ) – The maximum number of lines to skip when trying to parse the TSV.
**pd_kwargs – Additional keyword arguments passed to
pd.read_csv.low_memory (bool )
- Returns:
A DataFrame containing the TSV data, or an iterator yielding DataFrames if
chunksizeis specified.- Return type:
pd.DataFrame or TextFileReader
- Raises:
ValueError – If
chunksizeis not a positive integer or None.RuntimeError – If the TSV cannot be parsed after skipping up to
max_skiplines.Pandas ParserError – If the TSV cannot be parsed due to a format error (after retries).
- stream_polars(url, sep='\t', chunksize=None, max_skip=5, low_memory=False, **pl_kwargs)[source]#
Read a TSV from a URL or local file into a Polars DataFrame with resilient header handling.
The helper will retry with increasing
skip_rowswhen Polars raises an error (useful for files with extra header lines). Whenchunksizeis provided an iterator is returned.- Parameters:
url (str ) – The URL or local file path to read the TSV from.
sep (str ) – The delimiter to use (default is tab).
chunksize (int or None) – If an integer is provided, returns an iterator that yields DataFrames of that many rows. If None, returns a single DataFrame.
max_skip (int ) – The maximum number of lines to skip when trying to parse the TSV.
**pl_kwargs – Additional keyword arguments passed to
pl.read_csv.low_memory (bool )
- Returns:
A Polars DataFrame containing the TSV data, or an iterator yielding DataFrames if
chunksizeis specified.- Return type:
pl.DataFrame or Iterator[pl.DataFrame]
- Raises:
ValueError – If
chunksizeis not a positive integer or None.RuntimeError – If the TSV cannot be parsed after skipping up to
max_skiplines.Polars Error – If the TSV cannot be parsed due to a format error (after retries).
- stream_txt(url, chunksize=None, **httpx_kwargs)[source]#
Stream a plain-text resource. When
chunksizeisNonethe full text is returned as a string. Whenchunksizeis an integer the function yields lists of lines.- Parameters:
url (str ) – The URL to stream the text from.
chunksize (int or None) – If an integer is provided, yields lists of lines of that size. If None, yields the entire text as a single string.
httpx_client (httpx.Client, optional) – An optional httpx.Client to use for the request. If None, a new client will be created for the request.
**httpx_kwargs – Additional keyword arguments passed to the httpx.Client.request() method
- Returns:
The full text as a string if chunksize is None, or a generator yielding lists of lines if chunksize is an integer.
- Return type:
str or Generator