Skip to content
Merged
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
56 changes: 38 additions & 18 deletions src/flowsom/io/read_fcs.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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


Expand Down
Loading