Graph Construction

These wrappers facilitate calling R-based adjacency-building approaches:

SmCCNet:
  • Constructs networks via sparse canonical correlation. Ideal for multi-omics correlation or partial correlation tasks.

Using SmCCNet to build an adjacency matrix from omics + phenotype data.
import pandas as pd
from bioneuralnet.datasets import DatasetLoader
from bioneuralnet.external_tools import SmCCNet

# 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 and clinical data
merged_omics = pd.concat([omics1, omics2, clinical, phenotype], axis=1)

# Initialize and run SmCCNet
smccnet = SmCCNet(
    phenotype_df=phenotype,
    omics_dfs=[omics1, omics2],
    data_types=["genes", "proteins"],
    kfold=3,
    subSampNum=500,
)

global_network, smccnet_clusters = smccnet.run()

# Display output sizes
print(f"Global Network Shape: {global_network.shape}")
print(f"Number of SmCCNet Clusters: {len(smccnet_clusters)}")
WGCNA:
  • Weighted Gene Co-expression Network Analysis wrapper for R’s WGCNA package.

Demonstration of WGCNA adjacency generation from expression data.
import pandas as pd
from bioneuralnet.external_tools import WGCNA


def run_wgcna_workflow(
    omics_data: pd.DataFrame,
    phenotype_df: pd.DataFrame,
    data_types: list = ["gene", "miRNA"],
    soft_power: int = 6,
    min_module_size: int = 30,
    merge_cut_height: float = 0.25,
) -> pd.DataFrame:
    try:
        wgcna_instance = WGCNA(
            phenotype_df=phenotype_df,
            omics_dfs=omics_data,
            data_types=data_types,
            soft_power=soft_power,
            min_module_size=min_module_size,
            merge_cut_height=merge_cut_height,
        )

        adjacency_matrix = wgcna_instance.run()
        print("Adjacency matrix generated using WGCNA.")

        return adjacency_matrix

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


def main():
    try:
        print("Starting WGCNA Workflow...")

        omics_data = pd.DataFrame(
            {
                "gene_feature1": [0.1, 0.2, 0.3],
                "gene_feature2": [0.4, 0.5, 0.6],
                "miRNA_feature1": [0.7, 0.8, 0.9],
                "miRNA_feature2": [1.0, 1.1, 1.2],
            },
            index=["GeneA", "GeneB", "GeneC"],
        )

        phenotype_data = pd.DataFrame(
            [0, 1, 0], index=["GeneA", "GeneB", "GeneC"], name="Phenotype"
        )
        adjacency_matrix = run_wgcna_workflow(
            omics_data=omics_data, phenotype_data=phenotype_data
        )

        print("\nGenerated Adjacency Matrix:")
        print(adjacency_matrix)

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

        print(f"Adjacency matrix saved to {output_file}")
        print("\nWGCNA Workflow completed successfully.")

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


if __name__ == "__main__":
    main()

Note: 1. You must have R installed, plus the respective CRAN packages (e.g. “WGCNA” or “SmCCNet”), for these wrappers to work. 2. The adjacency matrices generated here can then be passed to GNNEmbedding, DPMON, or other BioNeuralNet modules.