# OmicNetR: Integrative Multi-Omics with Sparse CCA

## Overview

OmicNetR provides a minimal, reproducible workflow for integrating two omics layers (e.g., gene expression and metabolomics) using sparse canonical correlation analysis (sCCA).

This vignette demonstrates:

1. generating (or loading) two omics matrices,
2. aligning samples,
3. fitting sCCA,
4. creating a bipartite network,
5. plotting key visual summaries.

## Load OmicNetR

    library(OmicNetR)

## 1. Generate example data

For demonstration we use simulated multi-omics data. Replace this section with your own matrices in real analyses.

    omics_data <- generate_dummy_omics(
      n_samples = 60,
      n_genes = 800,
      n_metabolites = 150,
      n_linked = 20
    )

    X <- omics_data$X
    Y <- omics_data$Y

## 2. Align samples

Alignment ensures that `X` and `Y` contain the same samples in the same order.

    aligned <- align_omics(X, Y)
    #> Successfully aligned 60 matching samples.
    X_aligned <- aligned$X
    Y_aligned <- aligned$Y

## 3. Fit sparse CCA

The `penalty_X` and `penalty_Y` parameters control sparsity (higher = sparser).

    scca_model <- omic_scca(
      X = X_aligned,
      Y = Y_aligned,
      n_components = 2,
      penalty_X = 0.70,
      penalty_Y = 0.70
    )
    #> Model Optimization: Keeping 240 genes and 45 metabolites.
    str(scca_model, max.level = 1)
    #> List of 4
    #>  $ canonical_correlations: NULL
    #>  $ loadings              :List of 2
    #>  $ variates              :List of 2
    #>  $ penalties             :List of 2
    #>  - attr(*, "class")= chr "OmicNetR_sCCA"

## 4. Convert loadings to an interaction network

Edges are formed by pairing selected genes and metabolites and filtering by the absolute product of weights (`weight_threshold`).

    net_data <- scca_to_network(
      scca_model,
      comp_select = 1,
      weight_threshold = 0.01
    )

    # Keep the strongest edges for readability
    net_data <- net_data[order(abs(net_data$Weight_Product), decreasing = TRUE), ]
    net_data <- head(net_data, 50)
    head(net_data)
    #>        Gene Metabolite Weight_Product Interaction_Type
    #> 722  Gene_2      Met_4    -0.05146821         Negative
    #> 1202 Gene_2      Met_6    -0.05144102         Negative
    #> 728  Gene_8      Met_4    -0.05139479         Negative
    #> 1208 Gene_8      Met_6    -0.05136764         Negative
    #> 2402 Gene_2     Met_11     0.05108216         Positive
    #> 2408 Gene_8     Met_11     0.05100929         Positive

## 5. Visualize results

### Bipartite network

    plot_bipartite_network(net_data)
    #> Rendering network using Base-R Graphics Engine...

### Feature importance circle plot

    p <- plot_pathway_circle(scca_model, top_features = 30, pathway_db = "KEGG")
    p

### Correlation heatmap

This heatmap summarizes correlations between the top sCCA-selected features from each omic layer.

    plot_correlation_heatmap(scca_model = scca_model, X = X_aligned, Y = Y_aligned, top_n = 25)

## Notes for CRAN

- This vignette uses simulated data and avoids writing files to the package directory.
- Plots are created in-memory and rendered by knitr.