Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Connectivity Graph (V1) #176

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions src/bayestme/cli/load_spaceranger.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import argparse
import logging
import os.path

import bayestme.common
import bayestme.log_config
from bayestme import data
from bayestme.plot.common import plot_spot_connectivity_graph

logger = logging.getLogger(__name__)

Expand All @@ -28,5 +30,12 @@ def main():
bayestme.log_config.configure_logging(args)

dataset = data.SpatialExpressionDataset.read_spaceranger(args.input)
plot_spot_connectivity_graph(
node_coordinates=dataset.positions_tissue,
edges=dataset.edges,
output_fn=os.path.join(
os.path.dirname(args.output), "spot_connectivity_graph.pdf"
),
)

dataset.save(args.output)
11 changes: 10 additions & 1 deletion src/bayestme/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
RELATIVE_EXPRESSION_ATTR = f"{BAYESTME_ANNDATA_PREFIX}_relative_expression"
POSITIONS_X_COLUMN = "array_col"
POSITIONS_Y_COLUMN = "array_row"
REAL_POSITION_X_COLUMN = "pxl_col_in_fullres"
REAL_POSITION_Y_COLUMN = "pxl_row_in_fullres"
VISIUM_SPATIAL_COLUMNS = [
"barcode",
"in_tissue",
Expand Down Expand Up @@ -309,6 +311,13 @@ def read_spaceranger(cls, data_path):
positions = positions_df.loc[
ad.obs_names, [POSITIONS_X_COLUMN, POSITIONS_Y_COLUMN]
].values

physical_positions = positions_df.loc[
ad.obs_names, [REAL_POSITION_X_COLUMN, REAL_POSITION_Y_COLUMN]
].values

edges = utils.get_edges(physical_positions[tissue_mask], Layout.HEX)

gene_names = ad.var_names.values
barcodes = ad.obs_names.values
raw_count = ad.X
Expand All @@ -319,7 +328,7 @@ def read_spaceranger(cls, data_path):
tissue_mask=tissue_mask,
gene_names=gene_names,
layout=Layout.HEX,
edges=utils.get_edges(positions[tissue_mask], Layout.HEX),
edges=edges,
barcodes=barcodes,
)

Expand Down
33 changes: 33 additions & 0 deletions src/bayestme/plot/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from matplotlib.colors import Normalize
from matplotlib.patches import RegularPolygon, Wedge, Patch, Polygon
from scipy.spatial import Voronoi
import networkx as nx

import shapely
from matplotlib import patches
Expand Down Expand Up @@ -418,3 +419,35 @@ def plot_gene_raw_counts(

fig.savefig(output_file)
plt.close(fig)


def plot_spot_connectivity_graph(node_coordinates, edges, output_fn):
# Create a graph instance
fig, ax = plt.subplots(1, figsize=(10, 10))
G = nx.Graph()

# Add nodes and their positions to the graph
for i, (x, y) in enumerate(node_coordinates):
G.add_node(i, pos=(x, y))

# Add edges to the graph
for edge in edges:
G.add_edge(*edge)

# Get node positions for plotting
pos = nx.get_node_attributes(G, "pos")

# Draw the graph
nx.draw(
G,
pos,
with_labels=False,
node_color="skyblue",
node_size=5,
edge_color="gray",
ax=ax,
)

ax.set_title("Spot Connectivity Graph")

fig.savefig(output_fn)
16 changes: 0 additions & 16 deletions src/bayestme/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,22 +351,6 @@ def get_regular_grid_edges(
return edges


def plot_points_and_edges(positions, edges, output_path):
fig, ax = plt.subplots(1, 1, figsize=(10, 10))

# Plot the points
ax.scatter(positions[:, 0], positions[:, 1], c="blue", label="Points")

# Plot the edges
for edge in edges:
point1 = positions[edge[0], :]
point2 = positions[edge[1], :]
ax.plot([point1[0], point2[0]], [point1[1], point2[1]], c="red")

# Show the plot
plt.savefig(output_path, dpi=600)


def filter_reads_to_top_n_genes(reads, n_gene):
n_gene = min(n_gene, reads.shape[1])
top = np.argsort(np.std(np.log(1 + reads), axis=0))[::-1]
Expand Down
Loading