diff --git a/src/flowsom/io/read_fcs.py b/src/flowsom/io/read_fcs.py index 913d04a..c79f2ec 100644 --- a/src/flowsom/io/read_fcs.py +++ b/src/flowsom/io/read_fcs.py @@ -1,10 +1,29 @@ from __future__ import annotations import re +import warnings +from contextlib import contextmanager import anndata as ad import pandas as pd import readfcs +from anndata import ImplicitModificationWarning + + +@contextmanager +def _ignore_index_cast_warning(): + """Silence anndata's warning about casting integer indices to str. + + readfcs builds AnnData from frames with integer indices; anndata casts them + to str and warns. Harmless and not actionable by the user, so silence it. + """ + with warnings.catch_warnings(): + warnings.filterwarnings( + "ignore", + message="Transforming to str index", + category=ImplicitModificationWarning, + ) + yield def read_FCS(filepath): @@ -13,24 +32,25 @@ def read_FCS(filepath): :param filepath: An array containing a full path to the FCS file :type filepath: str """ - try: - f = readfcs.read(filepath, reindex=True) - f.var.n = f.var.n.astype(int) - f.var = f.var.sort_values(by="n") - # f.uns["meta"]["channels"].index = f.uns["meta"]["channels"].index.astype(int) - # f.uns["meta"]["channels"] = f.uns["meta"]["channels"].sort_index() - except ValueError: - f = readfcs.read(filepath, reindex=False) - markers = { - str(re.sub("S$", "", re.sub("^P", "", string))): f.uns["meta"][string] - for string in f.uns["meta"].keys() - if re.match("^p[0-9]+s$", string) - } - fluo_channels = list(markers.keys()) - non_fluo_channels = {i: f.var["channel"][i] for i in f.var.index if i not in fluo_channels} - index_markers = dict(markers, **non_fluo_channels) - f.var.rename(index=index_markers, inplace=True) - # f.uns["meta"]["channels"]["$PnS"] = [index_markers[key] for key in f.uns["meta"]["channels"].index] + with _ignore_index_cast_warning(): + try: + f = readfcs.read(filepath, reindex=True) + f.var.n = f.var.n.astype(int) + f.var = f.var.sort_values(by="n") + # f.uns["meta"]["channels"].index = f.uns["meta"]["channels"].index.astype(int) + # f.uns["meta"]["channels"] = f.uns["meta"]["channels"].sort_index() + except ValueError: + f = readfcs.read(filepath, reindex=False) + markers = { + str(re.sub("S$", "", re.sub("^P", "", string))): f.uns["meta"][string] + for string in f.uns["meta"].keys() + if re.match("^p[0-9]+s$", string) + } + fluo_channels = list(markers.keys()) + non_fluo_channels = {i: f.var["channel"][i] for i in f.var.index if i not in fluo_channels} + index_markers = dict(markers, **non_fluo_channels) + f.var.rename(index=index_markers, inplace=True) + # f.uns["meta"]["channels"]["$PnS"] = [index_markers[key] for key in f.uns["meta"]["channels"].index] return f