SpatialQuery 0.0.6__tar.gz → 0.0.8__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {spatialquery-0.0.6/SpatialQuery.egg-info → spatialquery-0.0.8}/PKG-INFO +1 -1
- {spatialquery-0.0.6 → spatialquery-0.0.8}/SpatialQuery/__init__.py +3 -1
- {spatialquery-0.0.6 → spatialquery-0.0.8}/SpatialQuery/plotting.py +142 -0
- {spatialquery-0.0.6 → spatialquery-0.0.8}/SpatialQuery/spatial_query.py +90 -24
- {spatialquery-0.0.6 → spatialquery-0.0.8}/SpatialQuery/spatial_query_multiple_fov.py +94 -21
- {spatialquery-0.0.6 → spatialquery-0.0.8}/SpatialQuery/spatial_utils.py +86 -0
- {spatialquery-0.0.6 → spatialquery-0.0.8/SpatialQuery.egg-info}/PKG-INFO +1 -1
- {spatialquery-0.0.6 → spatialquery-0.0.8}/pyproject.toml +1 -1
- {spatialquery-0.0.6 → spatialquery-0.0.8}/LICENSE +0 -0
- {spatialquery-0.0.6 → spatialquery-0.0.8}/MANIFEST.in +0 -0
- {spatialquery-0.0.6 → spatialquery-0.0.8}/README.md +0 -0
- {spatialquery-0.0.6 → spatialquery-0.0.8}/SpatialQuery/scfind4sp/__init__.py +0 -0
- {spatialquery-0.0.6 → spatialquery-0.0.8}/SpatialQuery/scfind4sp/core_methods.py +0 -0
- {spatialquery-0.0.6 → spatialquery-0.0.8}/SpatialQuery/scfind4sp/cpp_src/EliasFano.h +0 -0
- {spatialquery-0.0.6 → spatialquery-0.0.8}/SpatialQuery/scfind4sp/cpp_src/QueryScore.cpp +0 -0
- {spatialquery-0.0.6 → spatialquery-0.0.8}/SpatialQuery/scfind4sp/cpp_src/QueryScore.h +0 -0
- {spatialquery-0.0.6 → spatialquery-0.0.8}/SpatialQuery/scfind4sp/cpp_src/Serialization.h +0 -0
- {spatialquery-0.0.6 → spatialquery-0.0.8}/SpatialQuery/scfind4sp/cpp_src/__init__.py +0 -0
- {spatialquery-0.0.6 → spatialquery-0.0.8}/SpatialQuery/scfind4sp/cpp_src/const.h +0 -0
- {spatialquery-0.0.6 → spatialquery-0.0.8}/SpatialQuery/scfind4sp/cpp_src/eliasFano.cpp +0 -0
- {spatialquery-0.0.6 → spatialquery-0.0.8}/SpatialQuery/scfind4sp/cpp_src/fp_growth.cpp +0 -0
- {spatialquery-0.0.6 → spatialquery-0.0.8}/SpatialQuery/scfind4sp/cpp_src/fp_growth.h +0 -0
- {spatialquery-0.0.6 → spatialquery-0.0.8}/SpatialQuery/scfind4sp/cpp_src/serialization.cpp +0 -0
- {spatialquery-0.0.6 → spatialquery-0.0.8}/SpatialQuery/scfind4sp/cpp_src/typedef.h +0 -0
- {spatialquery-0.0.6 → spatialquery-0.0.8}/SpatialQuery/scfind4sp/cpp_src/utils.cpp +0 -0
- {spatialquery-0.0.6 → spatialquery-0.0.8}/SpatialQuery/scfind4sp/cpp_src/utils.h +0 -0
- {spatialquery-0.0.6 → spatialquery-0.0.8}/SpatialQuery/spatial_differential_pattern.py +0 -0
- {spatialquery-0.0.6 → spatialquery-0.0.8}/SpatialQuery/spatial_gene_covarying.py +0 -0
- {spatialquery-0.0.6 → spatialquery-0.0.8}/SpatialQuery/utils.py +0 -0
- {spatialquery-0.0.6 → spatialquery-0.0.8}/SpatialQuery.egg-info/SOURCES.txt +0 -0
- {spatialquery-0.0.6 → spatialquery-0.0.8}/SpatialQuery.egg-info/dependency_links.txt +0 -0
- {spatialquery-0.0.6 → spatialquery-0.0.8}/SpatialQuery.egg-info/requires.txt +0 -0
- {spatialquery-0.0.6 → spatialquery-0.0.8}/SpatialQuery.egg-info/top_level.txt +0 -0
- {spatialquery-0.0.6 → spatialquery-0.0.8}/setup.cfg +0 -0
- {spatialquery-0.0.6 → spatialquery-0.0.8}/setup.py +0 -0
|
@@ -8,7 +8,8 @@ from .plotting import (
|
|
|
8
8
|
plot_motif_rand,
|
|
9
9
|
plot_motif_celltype,
|
|
10
10
|
plot_all_center_motif,
|
|
11
|
-
plot_motif_composition
|
|
11
|
+
plot_motif_composition,
|
|
12
|
+
plot_motif_radius_tracking
|
|
12
13
|
)
|
|
13
14
|
|
|
14
15
|
__all__ = [
|
|
@@ -24,5 +25,6 @@ __all__ = [
|
|
|
24
25
|
'plot_motif_celltype',
|
|
25
26
|
'plot_all_center_motif',
|
|
26
27
|
'plot_motif_composition',
|
|
28
|
+
'plot_motif_radius_tracking',
|
|
27
29
|
'interactive_motif'
|
|
28
30
|
]
|
|
@@ -1346,3 +1346,145 @@ def plot_gene_pair_spatial(
|
|
|
1346
1346
|
print(f"[SAVED] {pdf_name}")
|
|
1347
1347
|
else:
|
|
1348
1348
|
plt.show()
|
|
1349
|
+
|
|
1350
|
+
|
|
1351
|
+
def plot_motif_radius_tracking(tracking: dict,
|
|
1352
|
+
abbrev: Optional[dict] = None,
|
|
1353
|
+
cmap: str = 'Blues',
|
|
1354
|
+
row_height: float = 0.16,
|
|
1355
|
+
figsize: Optional[tuple] = None,
|
|
1356
|
+
title: Optional[str] = None,
|
|
1357
|
+
save_path: Optional[str] = None):
|
|
1358
|
+
"""
|
|
1359
|
+
Plot how de novo motifs change across radii, next to their targeted enrichment.
|
|
1360
|
+
|
|
1361
|
+
Left panel: one row per tracked motif; a dot where de novo discovery reports the motif
|
|
1362
|
+
as significant at that radius, and a line or arrow to what it becomes at the next radius
|
|
1363
|
+
(persists, absorbed into a larger reported motif, splits into smaller reported motifs,
|
|
1364
|
+
or drops out). Right panel: targeted observed/expected (O/E) ratio of every tracked motif
|
|
1365
|
+
at every radius; grey cells are not significant, boxed cells are reported de novo.
|
|
1366
|
+
|
|
1367
|
+
Parameters
|
|
1368
|
+
----------
|
|
1369
|
+
tracking : dict
|
|
1370
|
+
Output from motif_radius_tracking.
|
|
1371
|
+
abbrev : dict, optional
|
|
1372
|
+
Short names for cell types in the row labels, e.g. {'endothelial cell': 'Endo'}.
|
|
1373
|
+
Cell types missing from the dict keep their full name.
|
|
1374
|
+
cmap : str, default='Blues'
|
|
1375
|
+
Colormap for the O/E heatmap.
|
|
1376
|
+
row_height : float, default=0.16
|
|
1377
|
+
Height per motif row in inches, used when figsize is None.
|
|
1378
|
+
figsize : tuple, optional
|
|
1379
|
+
Figure size. Defaults to a width of 9 inches and a height set by the number of motifs.
|
|
1380
|
+
title : str, optional
|
|
1381
|
+
Figure title. Defaults to the center cell type.
|
|
1382
|
+
save_path : str, optional
|
|
1383
|
+
Path to save the figure. If None, the figure will not be saved. Save as .pdf
|
|
1384
|
+
to keep the text editable.
|
|
1385
|
+
|
|
1386
|
+
Returns
|
|
1387
|
+
-------
|
|
1388
|
+
None
|
|
1389
|
+
Displays the two-panel figure.
|
|
1390
|
+
"""
|
|
1391
|
+
from matplotlib.patches import Patch, Rectangle
|
|
1392
|
+
|
|
1393
|
+
targeted = tracking['targeted']
|
|
1394
|
+
fates = tracking['fates']
|
|
1395
|
+
if len(targeted) == 0:
|
|
1396
|
+
print("No tracked motifs to plot.")
|
|
1397
|
+
return
|
|
1398
|
+
|
|
1399
|
+
abbrev = {} if abbrev is None else abbrev
|
|
1400
|
+
radii = sorted(targeted['radius'].unique())
|
|
1401
|
+
keys = targeted['motifs'].map(spatial_utils.canonical_motif)
|
|
1402
|
+
order = sorted(keys.unique(), key=lambda m: (len(m), m))
|
|
1403
|
+
row = {m: i for i, m in enumerate(order)}
|
|
1404
|
+
labels = [' – '.join(abbrev.get(t, t) for t in m) for m in order]
|
|
1405
|
+
n = len(order)
|
|
1406
|
+
|
|
1407
|
+
table = targeted.assign(key=keys)
|
|
1408
|
+
oe = table.pivot(index='key', columns='radius', values='OE').loc[order, radii].to_numpy(dtype=float)
|
|
1409
|
+
sig = table.pivot(index='key', columns='radius', values='if_significant').loc[order, radii].to_numpy(dtype=bool)
|
|
1410
|
+
reported = table.loc[table['reported_denovo'].astype(bool), ['key', 'radius']].itertuples(index=False)
|
|
1411
|
+
reported = [(k, r) for k, r in reported]
|
|
1412
|
+
|
|
1413
|
+
if figsize is None:
|
|
1414
|
+
figsize = (9, n * row_height + 1.6)
|
|
1415
|
+
fig, (ax_a, ax_b) = plt.subplots(1, 2, figsize=figsize,
|
|
1416
|
+
gridspec_kw={'width_ratios': [1, 1.4], 'wspace': 0.08})
|
|
1417
|
+
fig.subplots_adjust(left=0.25, right=0.84, top=1 - 0.75 / figsize[1], bottom=1.0 / figsize[1])
|
|
1418
|
+
|
|
1419
|
+
# Panel a: de novo reports and their fate at the next radius
|
|
1420
|
+
colors = {'persists': 'black', 'absorbed into larger': '#e0702f', 'splits into smaller': '#2a6fbb'}
|
|
1421
|
+
for k, r in reported:
|
|
1422
|
+
ax_a.plot(r, row[k], 'o', color='black', markersize=4, zorder=3)
|
|
1423
|
+
for _, f in fates.iterrows():
|
|
1424
|
+
y0 = row[spatial_utils.canonical_motif(f['motifs'])]
|
|
1425
|
+
if f['fate'] == 'persists':
|
|
1426
|
+
ax_a.plot([f['radius'], f['next_radius']], [y0, y0], color='black', linewidth=1, zorder=2)
|
|
1427
|
+
elif f['fate'] == 'drops out':
|
|
1428
|
+
ax_a.plot(f['next_radius'], y0, 'x', color='#8a8982', markersize=4, zorder=2)
|
|
1429
|
+
else:
|
|
1430
|
+
for target in f['next_motifs']:
|
|
1431
|
+
ax_a.annotate('', xy=(f['next_radius'], row[spatial_utils.canonical_motif(target)]),
|
|
1432
|
+
xytext=(f['radius'], y0),
|
|
1433
|
+
arrowprops=dict(arrowstyle='-|>', color=colors[f['fate']], linewidth=0.8,
|
|
1434
|
+
mutation_scale=7, shrinkA=3, shrinkB=3), zorder=1)
|
|
1435
|
+
ax_a.set_xlim(radii[0] - 0.5, radii[-1] + 0.5)
|
|
1436
|
+
ax_a.set_ylim(n - 0.5, -0.5)
|
|
1437
|
+
ax_a.set_xticks(radii)
|
|
1438
|
+
ax_a.set_yticks(range(n))
|
|
1439
|
+
ax_a.set_yticklabels(labels)
|
|
1440
|
+
ax_a.set_xlabel('Radius')
|
|
1441
|
+
ax_a.set_title(f"De novo (support = {tracking['min_support']})")
|
|
1442
|
+
ax_a.spines[['top', 'right']].set_visible(False)
|
|
1443
|
+
present = set(fates['fate'])
|
|
1444
|
+
handles = [plt.Line2D([], [], color=c, linewidth=1, label=f) for f, c in colors.items() if f in present]
|
|
1445
|
+
if 'drops out' in present:
|
|
1446
|
+
handles.append(plt.Line2D([], [], color='#8a8982', marker='x', linestyle='none', label='drops out'))
|
|
1447
|
+
if handles:
|
|
1448
|
+
ax_a.legend(handles=handles, loc='upper left', bbox_to_anchor=(0, -0.6 / (figsize[1] * ax_a.get_position().height)),
|
|
1449
|
+
ncol=len(handles), frameon=False, borderaxespad=0, handlelength=1.5)
|
|
1450
|
+
|
|
1451
|
+
# Panel b: targeted O/E, non-significant cells grey, de novo reports boxed
|
|
1452
|
+
colormap = plt.get_cmap(cmap).copy()
|
|
1453
|
+
colormap.set_bad('#d9d9d9')
|
|
1454
|
+
vmax = np.nanmax(oe)
|
|
1455
|
+
im = ax_b.imshow(np.ma.masked_where(~sig | np.isnan(oe), oe), cmap=colormap, vmin=0,
|
|
1456
|
+
vmax=np.ceil(vmax) if np.isfinite(vmax) else 1, aspect='auto')
|
|
1457
|
+
for k, r in reported:
|
|
1458
|
+
ax_b.add_patch(Rectangle((radii.index(r) - 0.5, row[k] - 0.5), 1, 1,
|
|
1459
|
+
fill=False, edgecolor='black', linewidth=1))
|
|
1460
|
+
ax_b.set_xticks(range(len(radii)))
|
|
1461
|
+
ax_b.set_xticklabels(radii)
|
|
1462
|
+
ax_b.set_yticks([])
|
|
1463
|
+
ax_b.set_xlabel('Radius')
|
|
1464
|
+
ax_b.set_title('Targeted O/E')
|
|
1465
|
+
ax_b.tick_params(length=0)
|
|
1466
|
+
for spine in ax_b.spines.values():
|
|
1467
|
+
spine.set_visible(False)
|
|
1468
|
+
|
|
1469
|
+
# Vertical legend on the right, fixed in inches so it does not stretch with the number of rows
|
|
1470
|
+
pos = ax_b.get_position()
|
|
1471
|
+
w_in, h_in = figsize[0] * pos.width, figsize[1] * pos.height
|
|
1472
|
+
cbar_h = min(1.0, h_in)
|
|
1473
|
+
cax = ax_b.inset_axes([1 + 0.12 / w_in, 1 - cbar_h / h_in, 0.1 / w_in, cbar_h / h_in])
|
|
1474
|
+
cb = fig.colorbar(im, cax=cax, orientation='vertical')
|
|
1475
|
+
cb.set_label('O/E')
|
|
1476
|
+
ax_b.legend(handles=[Patch(facecolor='#d9d9d9', edgecolor='none', label='Not significant'),
|
|
1477
|
+
Patch(facecolor='white', edgecolor='black', label='Reported de novo')],
|
|
1478
|
+
loc='upper left', bbox_to_anchor=(1 + 0.06 / w_in, 1 - (cbar_h + 0.15) / h_in),
|
|
1479
|
+
frameon=False, borderaxespad=0, handlelength=1.2, handleheight=1.2)
|
|
1480
|
+
|
|
1481
|
+
if title is None:
|
|
1482
|
+
centers = targeted['center'].unique() if 'center' in targeted.columns else []
|
|
1483
|
+
title = f'Motifs around {centers[0]} across radii' if len(centers) == 1 else 'Motifs across radii'
|
|
1484
|
+
fig.suptitle(title)
|
|
1485
|
+
|
|
1486
|
+
if save_path is not None:
|
|
1487
|
+
with plt.rc_context({'pdf.fonttype': 42, 'ps.fonttype': 42}):
|
|
1488
|
+
plt.savefig(save_path, dpi=300, bbox_inches='tight')
|
|
1489
|
+
|
|
1490
|
+
plt.show()
|
|
@@ -439,18 +439,7 @@ class spatial_query:
|
|
|
439
439
|
'n_motif', 'expectation', 'p-values', 'if_significant'])
|
|
440
440
|
return out_pd
|
|
441
441
|
|
|
442
|
-
|
|
443
|
-
out_pd['if_significant'] = True if out_pd['p-values'][0] < 0.05 else False
|
|
444
|
-
return out_pd
|
|
445
|
-
else:
|
|
446
|
-
p_values = out_pd['p-values'].tolist()
|
|
447
|
-
if_rejected, corrected_p_values = mt.fdrcorrection(p_values,
|
|
448
|
-
alpha=0.05,
|
|
449
|
-
method='poscorr')
|
|
450
|
-
out_pd['adj-pval'] = corrected_p_values
|
|
451
|
-
out_pd['if_significant'] = if_rejected
|
|
452
|
-
out_pd = out_pd.sort_values(by='adj-pval', ignore_index=True)
|
|
453
|
-
return out_pd
|
|
442
|
+
return spatial_utils.apply_fdr_correction(out_pd)
|
|
454
443
|
|
|
455
444
|
def motif_enrichment_dist(self,
|
|
456
445
|
ct: str,
|
|
@@ -637,18 +626,7 @@ class spatial_query:
|
|
|
637
626
|
'n_motif', 'expectation', 'p-values', 'if_significant'])
|
|
638
627
|
return out_pd
|
|
639
628
|
|
|
640
|
-
|
|
641
|
-
out_pd['if_significant'] = True if out_pd['p-values'][0] < 0.05 else False
|
|
642
|
-
return out_pd
|
|
643
|
-
else:
|
|
644
|
-
p_values = out_pd['p-values'].tolist()
|
|
645
|
-
if_rejected, corrected_p_values = mt.fdrcorrection(p_values,
|
|
646
|
-
alpha=0.05,
|
|
647
|
-
method='poscorr')
|
|
648
|
-
out_pd['adj-pval'] = corrected_p_values
|
|
649
|
-
out_pd['if_significant'] = if_rejected
|
|
650
|
-
out_pd = out_pd.sort_values(by='adj-pval', ignore_index=True)
|
|
651
|
-
return out_pd
|
|
629
|
+
return spatial_utils.apply_fdr_correction(out_pd)
|
|
652
630
|
|
|
653
631
|
def motif_composition_dist(self,
|
|
654
632
|
ct: str,
|
|
@@ -754,6 +732,57 @@ class spatial_query:
|
|
|
754
732
|
return pd.concat(out, ignore_index=True)
|
|
755
733
|
|
|
756
734
|
|
|
735
|
+
def motif_radius_tracking(self,
|
|
736
|
+
ct: str,
|
|
737
|
+
radii: List[float],
|
|
738
|
+
min_support: float = 0.5,
|
|
739
|
+
min_size: int = 0,
|
|
740
|
+
mode: str = 'nested',
|
|
741
|
+
) -> dict:
|
|
742
|
+
"""
|
|
743
|
+
Follow de novo motifs across radii.
|
|
744
|
+
|
|
745
|
+
De novo discovery is run at every radius. Every motif found significant at any radius
|
|
746
|
+
("tracked motif") is then tested at every radius with the targeted test, which needs no
|
|
747
|
+
support threshold. This separates two reasons a motif can leave the de novo output when
|
|
748
|
+
the radius changes: its enrichment really changes, or the support threshold, the
|
|
749
|
+
maximal-pattern filter or the nested descent stops reporting it while it stays enriched.
|
|
750
|
+
Plot the result with plot_motif_radius_tracking.
|
|
751
|
+
|
|
752
|
+
Parameters
|
|
753
|
+
----------
|
|
754
|
+
ct : str
|
|
755
|
+
Cell type of the center cells.
|
|
756
|
+
radii : List[float]
|
|
757
|
+
Radii to scan, at least two.
|
|
758
|
+
min_support : float, default=0.5
|
|
759
|
+
Support threshold of the de novo runs.
|
|
760
|
+
min_size : int, default=0
|
|
761
|
+
Minimum neighborhood size for each point to consider.
|
|
762
|
+
mode : {'maximal', 'nested'}, default='nested'
|
|
763
|
+
De novo mode, as in motif_enrichment_dist.
|
|
764
|
+
|
|
765
|
+
Returns
|
|
766
|
+
-------
|
|
767
|
+
dict
|
|
768
|
+
- 'denovo': the de novo tables of all radii stacked, with a ``radius`` column.
|
|
769
|
+
- 'targeted': targeted test of every tracked motif at every radius. Columns are those
|
|
770
|
+
of motif_enrichment_dist plus ``radius``, ``OE`` (n_center_motif / expectation),
|
|
771
|
+
``frac_anchors`` (n_center_motif / n_center) and ``reported_denovo`` (whether de novo
|
|
772
|
+
discovery reports the motif as significant at that radius). The FDR is across the
|
|
773
|
+
tracked motifs within each radius.
|
|
774
|
+
- 'fates': for each motif reported at a radius, what it becomes at the next radius:
|
|
775
|
+
'persists', 'absorbed into larger' (a strict superset is reported), 'splits into
|
|
776
|
+
smaller' (only strict subsets are reported) or 'drops out'. ``next_motifs`` lists
|
|
777
|
+
the reported motifs it maps to.
|
|
778
|
+
- 'min_support': the support threshold used.
|
|
779
|
+
"""
|
|
780
|
+
return spatial_utils.motif_radius_tracking(
|
|
781
|
+
run_enrichment=lambda r, motifs: self.motif_enrichment_dist(
|
|
782
|
+
ct=ct, motifs=motifs, max_dist=r, min_size=min_size,
|
|
783
|
+
min_support=min_support, mode=mode),
|
|
784
|
+
radii=radii, min_support=min_support)
|
|
785
|
+
|
|
757
786
|
def find_patterns_grid(self,
|
|
758
787
|
max_dist: float = 20,
|
|
759
788
|
min_size: int = 0,
|
|
@@ -1618,6 +1647,43 @@ class spatial_query:
|
|
|
1618
1647
|
return plotting.plot_motif_composition(comp_df=comp_df, motif=motif, bins=bins, colors=colors,
|
|
1619
1648
|
figsize=figsize, title=title, save_path=save_path)
|
|
1620
1649
|
|
|
1650
|
+
def plot_motif_radius_tracking(self,
|
|
1651
|
+
tracking: dict,
|
|
1652
|
+
abbrev: Optional[dict] = None,
|
|
1653
|
+
cmap: str = 'Blues',
|
|
1654
|
+
row_height: float = 0.16,
|
|
1655
|
+
figsize: Optional[tuple] = None,
|
|
1656
|
+
title: Optional[str] = None,
|
|
1657
|
+
save_path: Optional[str] = None):
|
|
1658
|
+
"""
|
|
1659
|
+
Plot how de novo motifs change across radii, next to their targeted O/E.
|
|
1660
|
+
|
|
1661
|
+
Parameters
|
|
1662
|
+
----------
|
|
1663
|
+
tracking : dict
|
|
1664
|
+
Output from motif_radius_tracking.
|
|
1665
|
+
abbrev : dict, optional
|
|
1666
|
+
Short names for cell types in the row labels.
|
|
1667
|
+
cmap : str, default='Blues'
|
|
1668
|
+
Colormap for the O/E heatmap.
|
|
1669
|
+
row_height : float, default=0.16
|
|
1670
|
+
Height per motif row in inches, used when figsize is None.
|
|
1671
|
+
figsize : tuple, optional
|
|
1672
|
+
Figure size. Defaults to a height set by the number of motifs.
|
|
1673
|
+
title : str, optional
|
|
1674
|
+
Figure title. Defaults to the center cell type.
|
|
1675
|
+
save_path : str, optional
|
|
1676
|
+
Path to save the figure. Save as .pdf to keep the text editable.
|
|
1677
|
+
|
|
1678
|
+
Returns
|
|
1679
|
+
-------
|
|
1680
|
+
None
|
|
1681
|
+
Displays the two-panel figure; see SpatialQuery.plotting.plot_motif_radius_tracking.
|
|
1682
|
+
"""
|
|
1683
|
+
return plotting.plot_motif_radius_tracking(tracking=tracking, abbrev=abbrev, cmap=cmap,
|
|
1684
|
+
row_height=row_height, figsize=figsize,
|
|
1685
|
+
title=title, save_path=save_path)
|
|
1686
|
+
|
|
1621
1687
|
def plot_gene_pair_heatmap(self,
|
|
1622
1688
|
gene_pair_df: pd.DataFrame,
|
|
1623
1689
|
figsize: tuple = (7, 5),
|
|
@@ -15,7 +15,7 @@ from collections import defaultdict
|
|
|
15
15
|
from sklearn.preprocessing import LabelEncoder
|
|
16
16
|
from statsmodels.stats.multitest import multipletests
|
|
17
17
|
|
|
18
|
-
from . import spatial_gene_covarying, plotting
|
|
18
|
+
from . import spatial_gene_covarying, plotting, spatial_utils
|
|
19
19
|
from .spatial_differential_pattern import differential_analysis_motif_knn, differential_analysis_motif_dist
|
|
20
20
|
|
|
21
21
|
from .spatial_query import spatial_query
|
|
@@ -571,16 +571,7 @@ class spatial_query_multi:
|
|
|
571
571
|
|
|
572
572
|
out_pd = pd.DataFrame(out)
|
|
573
573
|
|
|
574
|
-
|
|
575
|
-
out_pd['if_significant'] = True if out_pd['p-values'][0] < 0.05 else False
|
|
576
|
-
else:
|
|
577
|
-
p_values = out_pd['p-values'].tolist()
|
|
578
|
-
if_rejected, corrected_p_values = mt.fdrcorrection(p_values,
|
|
579
|
-
alpha=0.05,
|
|
580
|
-
method='poscorr')
|
|
581
|
-
out_pd['adj-pval'] = corrected_p_values
|
|
582
|
-
out_pd['if_significant'] = if_rejected
|
|
583
|
-
out_pd = out_pd.sort_values(by='adj-pval', ignore_index=True)
|
|
574
|
+
out_pd = apply_fdr_correction(out_pd)
|
|
584
575
|
|
|
585
576
|
if return_cellID:
|
|
586
577
|
return out_pd, motif_cell_ids, center_cell_ids
|
|
@@ -850,16 +841,7 @@ class spatial_query_multi:
|
|
|
850
841
|
|
|
851
842
|
out_pd = pd.DataFrame(out)
|
|
852
843
|
|
|
853
|
-
|
|
854
|
-
out_pd['if_significant'] = True if out_pd['p-values'][0] < 0.05 else False
|
|
855
|
-
else:
|
|
856
|
-
p_values = out_pd['p-values'].tolist()
|
|
857
|
-
if_rejected, corrected_p_values = mt.fdrcorrection(p_values,
|
|
858
|
-
alpha=0.05,
|
|
859
|
-
method='poscorr')
|
|
860
|
-
out_pd['adj-pval'] = corrected_p_values
|
|
861
|
-
out_pd['if_significant'] = if_rejected
|
|
862
|
-
out_pd = out_pd.sort_values(by='adj-pval', ignore_index=True)
|
|
844
|
+
out_pd = apply_fdr_correction(out_pd)
|
|
863
845
|
|
|
864
846
|
if return_cellID:
|
|
865
847
|
return out_pd, motif_cell_ids, center_cell_ids
|
|
@@ -992,6 +974,60 @@ class spatial_query_multi:
|
|
|
992
974
|
lambda s, cinds, int_labels, num_types: anchor_neighbor_counts_knn(
|
|
993
975
|
s.kd_tree, s.spatial_pos, cinds, int_labels, num_types, k, max_dist))
|
|
994
976
|
|
|
977
|
+
def motif_radius_tracking(self,
|
|
978
|
+
ct: str,
|
|
979
|
+
radii: List[float],
|
|
980
|
+
dataset: Union[str, List[str]] = None,
|
|
981
|
+
min_support: float = 0.5,
|
|
982
|
+
min_size: int = 0,
|
|
983
|
+
mode: str = 'nested',
|
|
984
|
+
) -> dict:
|
|
985
|
+
"""
|
|
986
|
+
Follow de novo motifs across radii.
|
|
987
|
+
|
|
988
|
+
De novo discovery is run at every radius. Every motif found significant at any radius
|
|
989
|
+
("tracked motif") is then tested at every radius with the targeted test, which needs no
|
|
990
|
+
support threshold. This separates two reasons a motif can leave the de novo output when
|
|
991
|
+
the radius changes: its enrichment really changes, or the support threshold, the
|
|
992
|
+
maximal-pattern filter or the nested descent stops reporting it while it stays enriched.
|
|
993
|
+
Plot the result with plot_motif_radius_tracking.
|
|
994
|
+
|
|
995
|
+
Parameters
|
|
996
|
+
----------
|
|
997
|
+
ct : str
|
|
998
|
+
Cell type of the center cells.
|
|
999
|
+
radii : List[float]
|
|
1000
|
+
Radii to scan, at least two.
|
|
1001
|
+
dataset : str or List[str], optional
|
|
1002
|
+
Datasets to use. Use all datasets if dataset=None.
|
|
1003
|
+
min_support : float, default=0.5
|
|
1004
|
+
Support threshold of the de novo runs.
|
|
1005
|
+
min_size : int, default=0
|
|
1006
|
+
Minimum neighborhood size for each point to consider.
|
|
1007
|
+
mode : {'maximal', 'nested'}, default='nested'
|
|
1008
|
+
De novo mode, as in motif_enrichment_dist.
|
|
1009
|
+
|
|
1010
|
+
Returns
|
|
1011
|
+
-------
|
|
1012
|
+
dict
|
|
1013
|
+
- 'denovo': the de novo tables of all radii stacked, with a ``radius`` column.
|
|
1014
|
+
- 'targeted': targeted test of every tracked motif at every radius. Columns are those
|
|
1015
|
+
of motif_enrichment_dist plus ``radius``, ``OE`` (n_center_motif / expectation),
|
|
1016
|
+
``frac_anchors`` (n_center_motif / n_center) and ``reported_denovo`` (whether de novo
|
|
1017
|
+
discovery reports the motif as significant at that radius). The FDR is across the
|
|
1018
|
+
tracked motifs within each radius.
|
|
1019
|
+
- 'fates': for each motif reported at a radius, what it becomes at the next radius:
|
|
1020
|
+
'persists', 'absorbed into larger' (a strict superset is reported), 'splits into
|
|
1021
|
+
smaller' (only strict subsets are reported) or 'drops out'. ``next_motifs`` lists
|
|
1022
|
+
the reported motifs it maps to.
|
|
1023
|
+
- 'min_support': the support threshold used.
|
|
1024
|
+
"""
|
|
1025
|
+
return spatial_utils.motif_radius_tracking(
|
|
1026
|
+
run_enrichment=lambda r, motifs: self.motif_enrichment_dist(
|
|
1027
|
+
ct=ct, motifs=motifs, dataset=dataset, max_dist=r, min_size=min_size,
|
|
1028
|
+
min_support=min_support, mode=mode),
|
|
1029
|
+
radii=radii, min_support=min_support)
|
|
1030
|
+
|
|
995
1031
|
def plot_motif_composition(self,
|
|
996
1032
|
comp_df: pd.DataFrame,
|
|
997
1033
|
motif: Optional[List[str]] = None,
|
|
@@ -1008,6 +1044,43 @@ class spatial_query_multi:
|
|
|
1008
1044
|
return plotting.plot_motif_composition(comp_df=comp_df, motif=motif, bins=bins, colors=colors,
|
|
1009
1045
|
figsize=figsize, title=title, save_path=save_path)
|
|
1010
1046
|
|
|
1047
|
+
def plot_motif_radius_tracking(self,
|
|
1048
|
+
tracking: dict,
|
|
1049
|
+
abbrev: Optional[dict] = None,
|
|
1050
|
+
cmap: str = 'Blues',
|
|
1051
|
+
row_height: float = 0.16,
|
|
1052
|
+
figsize: Optional[tuple] = None,
|
|
1053
|
+
title: Optional[str] = None,
|
|
1054
|
+
save_path: Optional[str] = None):
|
|
1055
|
+
"""
|
|
1056
|
+
Plot how de novo motifs change across radii, next to their targeted O/E.
|
|
1057
|
+
|
|
1058
|
+
Parameters
|
|
1059
|
+
----------
|
|
1060
|
+
tracking : dict
|
|
1061
|
+
Output from motif_radius_tracking.
|
|
1062
|
+
abbrev : dict, optional
|
|
1063
|
+
Short names for cell types in the row labels.
|
|
1064
|
+
cmap : str, default='Blues'
|
|
1065
|
+
Colormap for the O/E heatmap.
|
|
1066
|
+
row_height : float, default=0.16
|
|
1067
|
+
Height per motif row in inches, used when figsize is None.
|
|
1068
|
+
figsize : tuple, optional
|
|
1069
|
+
Figure size. Defaults to a height set by the number of motifs.
|
|
1070
|
+
title : str, optional
|
|
1071
|
+
Figure title. Defaults to the center cell type.
|
|
1072
|
+
save_path : str, optional
|
|
1073
|
+
Path to save the figure. Save as .pdf to keep the text editable.
|
|
1074
|
+
|
|
1075
|
+
Returns
|
|
1076
|
+
-------
|
|
1077
|
+
None
|
|
1078
|
+
Displays the two-panel figure; see SpatialQuery.plotting.plot_motif_radius_tracking.
|
|
1079
|
+
"""
|
|
1080
|
+
return plotting.plot_motif_radius_tracking(tracking=tracking, abbrev=abbrev, cmap=cmap,
|
|
1081
|
+
row_height=row_height, figsize=figsize,
|
|
1082
|
+
title=title, save_path=save_path)
|
|
1083
|
+
|
|
1011
1084
|
def find_fp_knn_fov(self,
|
|
1012
1085
|
ct: str,
|
|
1013
1086
|
dataset_i: str,
|
|
@@ -1154,3 +1154,89 @@ def get_all_neighbor_cells(sq_obj,
|
|
|
1154
1154
|
}
|
|
1155
1155
|
|
|
1156
1156
|
|
|
1157
|
+
|
|
1158
|
+
|
|
1159
|
+
MOTIF_FATES = ('persists', 'absorbed into larger', 'splits into smaller', 'drops out')
|
|
1160
|
+
|
|
1161
|
+
|
|
1162
|
+
def motif_radius_tracking(run_enrichment, radii, min_support: float) -> dict:
|
|
1163
|
+
"""Follow de novo motifs across radii and test every one of them at every radius.
|
|
1164
|
+
|
|
1165
|
+
Shared by ``spatial_query.motif_radius_tracking`` and
|
|
1166
|
+
``spatial_query_multi.motif_radius_tracking``.
|
|
1167
|
+
|
|
1168
|
+
Parameters
|
|
1169
|
+
----------
|
|
1170
|
+
run_enrichment : callable
|
|
1171
|
+
``run_enrichment(max_dist, motifs)`` returns a motif enrichment table. ``motifs=None``
|
|
1172
|
+
runs de novo discovery; a list of motifs runs the targeted test on exactly those motifs.
|
|
1173
|
+
radii : sequence of float
|
|
1174
|
+
Radii to scan, at least two.
|
|
1175
|
+
min_support : float
|
|
1176
|
+
Support threshold of the de novo runs (only used to label the result).
|
|
1177
|
+
|
|
1178
|
+
Returns
|
|
1179
|
+
-------
|
|
1180
|
+
dict with keys 'denovo', 'targeted', 'fates' and 'min_support'; see
|
|
1181
|
+
``spatial_query.motif_radius_tracking`` for the columns.
|
|
1182
|
+
"""
|
|
1183
|
+
radii = sorted(set(radii))
|
|
1184
|
+
if len(radii) < 2:
|
|
1185
|
+
raise ValueError("radii must hold at least two distinct values.")
|
|
1186
|
+
|
|
1187
|
+
tables = []
|
|
1188
|
+
for r in radii:
|
|
1189
|
+
df = run_enrichment(r, None).copy()
|
|
1190
|
+
df['radius'] = r
|
|
1191
|
+
tables.append(df)
|
|
1192
|
+
denovo = pd.concat(tables, ignore_index=True)
|
|
1193
|
+
significant = denovo[denovo['if_significant'].astype(bool)]
|
|
1194
|
+
reported = {r: {canonical_motif(m) for m in significant.loc[significant['radius'] == r, 'motifs']}
|
|
1195
|
+
for r in radii}
|
|
1196
|
+
tracked = sorted(set().union(*reported.values()), key=lambda m: (len(m), m))
|
|
1197
|
+
|
|
1198
|
+
empty_targeted = pd.DataFrame(columns=['motifs', 'radius', 'n_center_motif', 'n_center',
|
|
1199
|
+
'expectation', 'p-values', 'if_significant', 'OE',
|
|
1200
|
+
'frac_anchors', 'reported_denovo'])
|
|
1201
|
+
if len(tracked) == 0:
|
|
1202
|
+
print("No significant motif at any radius; nothing to track.")
|
|
1203
|
+
return {'denovo': denovo, 'targeted': empty_targeted,
|
|
1204
|
+
'fates': pd.DataFrame(columns=['motifs', 'radius', 'next_radius', 'fate', 'next_motifs']),
|
|
1205
|
+
'min_support': min_support}
|
|
1206
|
+
|
|
1207
|
+
# One targeted call per radius, so the FDR is across the tracked motifs at that radius.
|
|
1208
|
+
tables = []
|
|
1209
|
+
for r in radii:
|
|
1210
|
+
df = run_enrichment(r, [list(m) for m in tracked]).copy()
|
|
1211
|
+
if len(df) != len(tracked):
|
|
1212
|
+
raise RuntimeError(f"Targeted test at radius {r} returned {len(df)} rows for "
|
|
1213
|
+
f"{len(tracked)} motifs.")
|
|
1214
|
+
df['radius'] = r
|
|
1215
|
+
tables.append(df)
|
|
1216
|
+
targeted = pd.concat(tables, ignore_index=True)
|
|
1217
|
+
expectation = targeted['expectation'].astype(float)
|
|
1218
|
+
targeted['OE'] = targeted['n_center_motif'] / expectation.where(expectation > 0)
|
|
1219
|
+
targeted['frac_anchors'] = targeted['n_center_motif'] / targeted['n_center']
|
|
1220
|
+
targeted['reported_denovo'] = [canonical_motif(m) in reported[r]
|
|
1221
|
+
for m, r in zip(targeted['motifs'], targeted['radius'])]
|
|
1222
|
+
|
|
1223
|
+
rows = []
|
|
1224
|
+
for r, r_next in zip(radii[:-1], radii[1:]):
|
|
1225
|
+
later = reported[r_next]
|
|
1226
|
+
for motif in sorted(reported[r], key=lambda m: (len(m), m)):
|
|
1227
|
+
mset = set(motif)
|
|
1228
|
+
supersets = sorted(m for m in later if mset < set(m))
|
|
1229
|
+
subsets = sorted(m for m in later if set(m) < mset)
|
|
1230
|
+
if motif in later:
|
|
1231
|
+
fate, targets = 'persists', [motif]
|
|
1232
|
+
elif supersets:
|
|
1233
|
+
fate, targets = 'absorbed into larger', supersets
|
|
1234
|
+
elif subsets:
|
|
1235
|
+
fate, targets = 'splits into smaller', subsets
|
|
1236
|
+
else:
|
|
1237
|
+
fate, targets = 'drops out', []
|
|
1238
|
+
rows.append({'motifs': list(motif), 'radius': r, 'next_radius': r_next, 'fate': fate,
|
|
1239
|
+
'next_motifs': [list(m) for m in targets]})
|
|
1240
|
+
fates = pd.DataFrame(rows, columns=['motifs', 'radius', 'next_radius', 'fate', 'next_motifs'])
|
|
1241
|
+
|
|
1242
|
+
return {'denovo': denovo, 'targeted': targeted, 'fates': fates, 'min_support': min_support}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|