Skip to content

Commit

Permalink
Added clustering as in the scverse + edited bugs in readme
Browse files Browse the repository at this point in the history
  • Loading branch information
artuurC committed Sep 6, 2023
1 parent e41e506 commit 5627553
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 32 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ Starting from an FCS file that is properly transformed, compensated and checked
import FlowSOM as fs

# Load the FCS file
ff = fs.main.read_FCS("./tests/data/ff.fcs")
ff = fs.io.read_FCS("./tests/data/ff.fcs")

# Run the FlowSOM algorithm
fsom = fs.main.FlowSOM(ff, cols_to_use=[8, 11, 13, 14, 15, 16, 17], xdim=10, ydim=10, n_clus=10)
fsom = fs.FlowSOM(ff, cols_to_use=[8, 11, 13, 14, 15, 16, 17], xdim=10, ydim=10, n_clus=10)

# Plot the FlowSOM results
p = fs.pl.plot_stars(fsom, background_values=fsom.get_cluster_data().obs.metaclustering)
Expand Down
87 changes: 61 additions & 26 deletions docs/notebooks/example.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/FlowSOM/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from . import read_write as io
from . import preprocessing as pp
from . import plotting as pl
from .main import FlowSOM
from .main import FlowSOM, flowsom_clustering

__all__ = ["plotting", "main"]

Expand Down
18 changes: 15 additions & 3 deletions src/FlowSOM/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class FlowSOM:
"""

def __init__(self, inp, cols_to_use: np.array = None, n_clus=10, max_meta=None, seed: int = None, **kwargs):
def __init__(self, inp, cols_to_use: np.array = None, n_clus=10, seed: int = None, **kwargs):
"""Initialize the FlowSOM AnnData object
:param inp: A file path to an FCS file or a AnnData FCS file to cluster
Expand All @@ -30,8 +30,6 @@ def __init__(self, inp, cols_to_use: np.array = None, n_clus=10, max_meta=None,
:type cols_to_use: np.array
:param n_clus: The number of metacluster
:type n_clus: int
:param max_meta: To be adapted
:type max_meta: int
:param seed: A fixed seed
:type seed: int
"""
Expand Down Expand Up @@ -423,3 +421,17 @@ def get_cell_data(self):

def get_cluster_data(self):
return self.mudata["cluster_data"]


def flowsom_clustering(inp, cols_to_use=None, n_clus=10, xdim=10, ydim=10, seed=None, **kwargs):
"""Perform FlowSOM clustering on an anndata object and returns the anndata
object with the FlowSOM clusters and metaclusters added as variable
:param inp: An anndata or filepath to an FCS file
:type inp: ad.AnnData / str
"""
fsom = FlowSOM(inp, cols_to_use=cols_to_use, n_clus=n_clus, xdim=xdim, ydim=ydim, seed=seed, **kwargs)
inp.obs["FlowSOM_clusters"] = fsom.mudata["cell_data"].obs["clustering"]
inp.obs["FlowSOM_metaclusters"] = fsom.mudata["cell_data"].obs["metaclustering"]
inp.uns["FlowSOM"] = {"cols_to_use": cols_to_use, "n_clus": n_clus, "xdim": xdim, "ydim": ydim, "seed": seed}
return inp
7 changes: 7 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ def test_new_data(fcs):
assert fsom_new.get_cell_data().shape == (999, 18)


def test_flowsom_clustering(fcs):
inp = fs.flowsom_clustering(fcs, cols_to_use=[8, 11, 13, 14, 15, 16, 17])
assert "FlowSOM_clusters" in inp.obs.keys()
assert "FlowSOM_metaclusters" in inp.obs.keys()
assert "FlowSOM" in inp.uns.keys()


def test_aggregate_flowframes():
new_ff = fs.pp.aggregate_flowframes(
["./tests/data/ff.fcs", "./tests/data/ff.fcs"], c_total=5000, channels=[8, 11, 13, 14, 15, 16, 17]
Expand Down

0 comments on commit 5627553

Please sign in to comment.