Graph Embeddings

BioNeuralNet supports multiple embedding approaches:

  1. GNNEmbedding (GCN, GAT, GraphSAGE, GIN)

  2. Node2Vec (classic random-walk embedding)

GNN Embedding Example:

Generating GNN-based Embeddings with correlation-based node features (optional).
import pandas as pd
from bioneuralnet.datasets import DatasetLoader
from bioneuralnet.network_embedding import GNNEmbedding
from bioneuralnet.external_tools import SmCCNet
from bioneuralnet.utils import plot_embeddings

# Load example synthetic dataset
loader = DatasetLoader("example1")
omics1, omics2, phenotype, clinical = loader.load_data()

# Display dataset dimensions
print("Dataset Shapes:")
print(f"Omics1: {omics1.shape}")  # Expected: (358, 500)
print(f"Omics2: {omics2.shape}")  # Expected: (358, 100)
print(f"Phenotype: {phenotype.shape}")  # Expected: (358, 1)
print(f"Clinical: {clinical.shape}")  # Expected: (358, 6)")

# Merge omics data
merged_omics = pd.concat([omics1, omics2], axis=1)

# Generate global network using SmCCNet
smccnet = SmCCNet(
    phenotype_df=phenotype,
    omics_dfs=[omics1, omics2],
    data_types=["genes", "proteins"],
    kfold=3,
    subSampNum=500,
)
global_network, smccnet_clusters = smccnet.run()

# Initialize and run GNN Embedding
embeddings = GNNEmbedding(
    adjacency_matrix=global_network,
    omics_data=merged_omics,
    phenotype_data=phenotype,
    clinical_data=clinical,
    tune=True,
)

embeddings.fit()
embeddings_output = embeddings.embed(as_df=True)

# Display output shape
print(f"GNN Embeddings Shape: {embeddings_output.shape}")


global_node_labels = embeddings._prepare_node_labels()
embeddings_array = embeddings_output.values  
fig1 = plot_embeddings(embeddings_array, global_node_labels.to_frame(name="phenotype"), method="tsne")

Node2Vec Embedding Example:

Using Node2Vec to produce node embeddings from an adjacency matrix.
import pandas as pd
from bioneuralnet.external_tools import node2vec


def main():
    try:
        print("Starting Node2Vec Embedding Workflow...")

        adjacency_matrix = pd.DataFrame(
            {
                "GeneA": [1.0, 1.0, 0.0, 0.0],
                "GeneB": [1.0, 1.0, 1.0, 0.0],
                "GeneC": [0.0, 1.0, 1.0, 1.0],
                "GeneD": [0.0, 0.0, 1.0, 1.0],
            },
            index=["GeneA", "GeneB", "GeneC", "GeneD"],
        )

        node2vec_embedding = node2vec(
            adjacency_matrix=adjacency_matrix,
            embedding_dim=64,
            walk_length=30,
            num_walks=200,
            window_size=10,
            workers=4,
            seed=42,
        )

        embeddings = node2vec_embedding.run()

        print("\nNode Embeddings:")
        print(embeddings)

        output_file = "output/embeddings.csv"
        embeddings.to_csv(output_file)

        print("\nNode2Vec Embedding Workflow completed successfully.")

    except Exception as e:
        print(f"An error occurred during execution: {e}")
        raise e


if __name__ == "__main__":
    main()

The resulting embeddings can be used for: - Clustering - Subject-level integration - Visualization - Disease prediction