You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I like to use Milo for analyzing intra-celltype heterogeneity, particularly with neighborhood groups.
To my understanding, neighborhood groups are currently implemented only in R.
Therefore, I would like to ask if this feature will be added to milopy?
In case that there are no preparations for this, yet, would you be interested to implement neighborhood groups on basis of the following code? For any case, I would be thankful for any help to make it work correctly.
As an example I process the embryo gastrulation data from the MiloR tutorial in R, calculate neighborhood groups, transfer to python and calculate neighborhood groups for comparison with code I adapted from the clustering modules in scanpy.
Code for examples:
python-code for find_nhood_groups:
from contextlib import contextmanager, suppress
def get_igraph_from_adjacency(adjacency, directed=None):
"""Get igraph graph from adjacency matrix. Copy/pasted from scanpy."""
import igraph as ig
sources, targets = adjacency.nonzero()
weights = adjacency[sources, targets]
if isinstance(weights, np.matrix):
weights = weights.A1
g = ig.Graph(directed=directed)
g.add_vertices(adjacency.shape[0]) # this adds adjacency.shape[0] vertices
g.add_edges(list(zip(sources, targets)))
with suppress(KeyError):
g.es["weight"] = weights
if g.vcount() != adjacency.shape[0]:
logg.warning(
f"The constructed graph has only {g.vcount()} nodes. "
"Your adjacency matrix contained redundant nodes."
)
return g
def find_nhood_groups(
adata,
SpatialFDR_threshold = 0.1,
merge_discord = False,
max_lfc_delta=None,
overlap=1,
subset_nhoods=None,
):
"""Get igraph graph from adjacency matrix. Adjusted from scanpy louvain clustering."""
nhs = adata.obsm["nhoods"].copy()
nhood_adj = adata.uns["nhood_adata"].obsp["nhood_connectivities"].copy()
da_res = adata.uns["nhood_adata"].obs.copy()
is_da = np.asarray(da_res.SpatialFDR <= SpatialFDR_threshold)
# da_res.loc[is_da, 'logFC'].values @ (da_res.loc[is_da, 'logFC']).T.values
if merge_discord is False:
# discord_sign = np.sign(da_res.loc[is_da, 'logFC'].values @ (da_res.loc[is_da, 'logFC'])) < 0
# Assuming da.res is a pandas DataFrame and is.da is a boolean array
discord_sign = np.sign(da_res.loc[is_da, 'logFC'].values @ da_res.loc[is_da, 'logFC'].values.T) < 0
# nhood_adj[is_da, is_da][discord_sign] <- 0
nhood_adj[(is_da, is_da)][discord_sign] = 0
if overlap > 1:
nhood_adj[nhood_adj < overlap] = 0
if max_lfc_delta is not None:
lfc_diff = np.array([da_res['logFC'].values - x for x in da_res['logFC'].values])
nhood_adj[np.abs(lfc_diff) > max_lfc_delta] = 0
# binarise
# nhood_adj = np.asarray((nhood_adj > 0) + 0)
nhood_adj = (nhood_adj > 0).astype(int)
g = get_igraph_from_adjacency(nhood_adj, directed = False)
weights = None
part = g.community_multilevel(weights=weights)
groups = np.array(part.membership)
adata.uns["nhood_adata"].obs["nhood_groups"] = groups
Hi Max! Thank you for taking the time to code this up. I am soon going to archive this python package, since we've moved and improved all the functionality in here within the Pertpy toolbox https://github.com/scverse/pertpy/tree/main (see this tutorial for example usage). I would recommend posting a feature request in pertpy - you can reference this issue - and we can take it from there! It should be relatively easy to implement if there's sufficient interest in this feature
Dear @emdann,
I like to use Milo for analyzing intra-celltype heterogeneity, particularly with neighborhood groups.
To my understanding, neighborhood groups are currently implemented only in R.
Therefore, I would like to ask if this feature will be added to milopy?
In case that there are no preparations for this, yet, would you be interested to implement neighborhood groups on basis of the following code? For any case, I would be thankful for any help to make it work correctly.
As an example I process the embryo gastrulation data from the MiloR tutorial in R, calculate neighborhood groups, transfer to python and calculate neighborhood groups for comparison with code I adapted from the clustering modules in scanpy.
Code for examples:
python-code for
find_nhood_groups
:Example code -- R processing
Transfer to python:
Best regards,
max
The text was updated successfully, but these errors were encountered: