hdWGCNA

repository·dev·Indexed 19 days ago

https://github.com/smorabit/hdwgcna

An R package for performing weighted gene co-expression network analysis (WGCNA) on high-dimensional transcriptomics data, such as single-cell RNA-seq or spatial transcriptomics. Designed for use with Seurat objects, hdWGCNA supports context-specific network construction, transcription factor regulatory network analysis, and the use of metacells to reduce data sparsity.

Tokens
110.2K
Snippets
301
Records
436
Agent score
67%

What's inside hdWGCNA

  1. Overview of hdWGCNA

    dev

    hdWGCNA is an R package designed for performing weighted gene co-expression network analysis (WGCNA) on high-dimensional transcriptomics data, specifically single-cell RNA-seq or spatial transcriptomics.

    Key features include:

    • Modular Design: Can construct context-specific co-expression networks across cellular and spatial hierarchies.
    • Module Identification: Identifies modules of highly co-expressed genes and provides biological context via statistical testing and knowledge sources.
    • Transcription Factor Analysis: Supports Transcription Factor Regulatory Network Analysis.
    • Data Compatibility: Uses datasets formatted as Seurat objects.

    For practical implementation, users should refer to the hdWGCNA in single-cell data tutorial or the hdWGCNA in spatial transcriptomics data tutorial.

  2. License information for hdWGCNA

    dev

    The hdWGCNA repository is licensed under the GNU General Public License (GPL) version 3. This is a free, copyleft license that guarantees users the freedom to share and change all versions of the program.

    Key implications of this license include:

    • Freedom to modify and distribute: You can run, modify, and propagate the software.
    • Copyleft requirement: If you distribute a modified version, you must pass on the same freedoms to the recipients, including providing access to the source code.
    • No Warranty: The software is provided "as is" without any warranty of any kind.
    • Patent Protection: The license includes provisions to prevent software patents from being used to render the program non-free.
  3. Perform supervised UMAP analysis in hdWGCNA

    dev

    You can perform a supervised UMAP where gene module assignments act as labels to better distinguish groups in the embedding.

    To enable this, set supervised=TRUE in RunModuleUMAP. You can control the influence of the labels using the target_weight parameter:

    • target_weight near 0: Weights based on the underlying data structure.
    • target_weight near 1: Weights heavily based on the module labels.
    # run supervised UMAP:
    seurat_obj <- RunModuleUMAP(
      seurat_obj,
      n_hubs = 10,
      n_neighbors=15,
      min_dist=0.1,
      supervised=TRUE,
      target_weight=0.5
    )
    
    # get the hub gene UMAP table from the seurat object
    umap_df <- GetModuleUMAP(seurat_obj)
  4. Select feature types for ModuleFeaturePlot

    dev

    When calling ModuleFeaturePlot, the features argument determines the underlying data used for the color gradient. Choose the one that matches your analysis goal:

    • "hMEs": High-dimensional Module Eigengenes.
    • "MEs": Module Eigengenes.
    • "scores": Module scores (calculated via GetModuleScores).
    • "average": Average module expression (calculated via GetAvgModuleExpr). Note: setting this option automatically sets restrict_range = FALSE.
  5. How to construct metaspots in hdWGCNA

    dev

    Metaspots are computed from a Seurat object containing spatial transcriptomics data.

    Important: ConstructMetaspots is an internal function designed to be called by MetaspotsByGroups. You should NOT run ConstructMetaspots directly in your workflow. Instead, use the high-level MetaspotsByGroups function to handle metaspot construction as part of your analysis pipeline.

    # Note: Do not call ConstructMetaspots directly.
    # Use MetaspotsByGroups instead.
  6. How hdWGCNA network visualizations work

    dev

    hdWGCNA provides three primary ways to visualize co-expression networks:

    1. ModuleNetworkPlot: Focuses on individual modules. It creates separate plots for each module, highlighting the most central (hub) genes in a ring-based layout. This is best for inspecting the internal structure of specific functional modules.
    2. HubGeneNetworkPlot: Focuses on the global relationship between modules. It combines selected hub genes from multiple modules into one large network, showing how modules connect to one another via intermodular edges.
    3. ModuleUMAPPlot: Visualizes all genes in the co-expression network simultaneously using UMAP dimensionality reduction. This provides a global view of how genes cluster in a low-dimensional space based on their co-expression patterns.
  7. Strategies for GSEA: All genes vs. Module-specific genes

    dev

    When performing GSEA, you have two primary strategies for defining your ranked gene list (ranks):

    1. Using all genes in the co-expression network: Rank all genes (excluding the 'grey' module) by their connectivity (kME) to a specific module. This approach typically yields more significant results because it provides a larger background for comparison.
    2. Using only genes within a specific module: Rank only the genes belonging to the module of interest. While this can be more specific, it often results in fewer significant hits, especially for small modules, because the statistical power is reduced.

    Recommendation: For small modules, it is generally better to use the ranked list of all genes in the co-expression network to ensure sufficient statistical power.

  8. Analyze Transcription Factor Regulatory Networks

    dev
    Version 0.4.00 introduced functionality for Transcription Factor (TF) Regulatory Network Analysis. This includes functions like ConstructTFNetwork and MotifScan (which was updated in 0.4.04 to handle cases where one gene name corresponds to multiple motifs).
  9. Specify different network layouts in ggraph

    dev

    The visual structure of a network depends heavily on the layout algorithm. ggraph supports several layouts, including:

    • stress (default)
    • kk (Kamada Kawai)
    • layout_with_fr() (Fruchterman-Reingold)
    • layout_as_tree()
    • layout_nicely()
    • layout_in_circle()

    You can also provide a custom layout using 2D coordinates (e.g., from a UMAP) by using ggraph::create_layout(graph, custom_layout_df).

    # 1. default layout
    p1 <- ggraph(graph) + 
      geom_edge_link(aes(alpha=weight, color=edge_color)) + 
      geom_node_point(aes(color=module)) + 
      scale_colour_manual(values=mod_cp) + 
      scale_edge_colour_manual(values=mod_cp) + 
      ggtitle("layout = 'stress' (auto)") + 
      NoLegend()
    
    # 2. Kamada Kawai (kk) layout
    graph2 <- graph; E(graph)$weight <- E(graph)$weight + 0.0001
    p2 <- ggraph(graph, layout='kk', maxiter=100) + 
      geom_edge_link(aes(alpha=weight, color=edge_color)) + 
      geom_node_point(aes(color=module)) + 
      scale_colour_manual(values=mod_cp) + 
      scale_edge_colour_manual(values=mod_cp) + 
      ggtitle("layout = 'kk'") + 
      NoLegend()
    
    # 3. igraph layout_with_fr
    p3 <- ggraph(graph, layout=layout_with_fr(graph)) + 
      geom_edge_link(aes(alpha=weight, color=edge_color)) + 
      geom_node_point(aes(color=module)) + 
      scale_colour_manual(values=mod_cp) + 
      scale_edge_colour_manual(values=mod_cp) + 
      ggtitle("layout_with_fr()") + 
      NoLegend()
    
    # 4. igraph layout_as_tree
    p4 <- ggraph(graph, layout=layout_as_tree(graph)) + 
      geom_edge_link(aes(alpha=weight, color=edge_color)) + 
      geom_node_point(aes(color=module)) + 
      scale_colour_manual(values=mod_cp) + 
      scale_edge_colour_manual(values=mod_cp) + 
      ggtitle("layout_as_tree()") + 
      NoLegend()
    
    # 5. igraph layout_nicely
    p5 <- ggraph(graph, layout=layout_nicely(graph)) + 
      geom_edge_link(aes(alpha=weight, color=edge_color)) + 
      geom_node_point(aes(color=module)) + 
      scale_colour_manual(values=mod_cp) + 
      scale_edge_colour_manual(values=mod_cp) + 
      ggtitle("layout_nicely()") + 
      NoLegend()
    
    # 6. igraph layout_in_circle
    p6 <- ggraph(graph, layout=layout_in_circle(graph)) + 
      geom_edge_link(aes(alpha=weight, color=edge_color)) + 
      geom_node_point(aes(color=module)) + 
      scale_colour_manual(values=mod_cp) + 
      scale_edge_colour_manual(values=mod_cp) + 
      ggtitle("layout_in_circle()") + 
      NoLegend()
  10. Construct metacells and metaspots

    dev

    To reduce noise and improve computational efficiency in single-cell or spatial transcriptomics (ST) data, hdWGCNA provides functions to aggregate data into metacells or metaspots.

    Key functions:

    • MetacellsByGroups(): Constructs metacells from single-cell data based on specified groups.
    • MetaspotsByGroups(): Constructs metaspots from spatial transcriptomics (ST) data based on specified groups.
  11. How consensus co-expression network analysis works in hdWGCNA

    dev

    Consensus co-expression network analysis differs from the standard workflow by constructing individual networks across distinct datasets (e.g., different biological sexes or species) and then computing an integrated co-expression network. This allows for the identification of conserved networks across conditions or the construction of a unified network from multiple datasets.

    The workflow involves:

    1. Metacell Construction: Using MetacellsByGroups with the relevant metadata columns in group.by to ensure groups are partitioned correctly.
    2. Expression Setup: Using SetMultiExpr instead of SetDatExpr to create separate expression matrices for each group.
    3. Soft Power Selection: Using TestSoftPowersConsensus to find optimal thresholds for each individual matrix.
    4. Network Construction: Using ConstructNetwork with consensus=TRUE to build individual networks and integrate them.
    # Example of the consensus workflow logic
    seurat_obj <- SetMultiExpr(seurat_obj, group_name = "ASC", group.by = "cell_type", multi.group.by ="msex")
    seurat_obj <- TestSoftPowersConsensus(seurat_obj)
    seurat_obj <- ConstructNetwork(seurat_obj, soft_power=c(8,5), consensus=TRUE, tom_name = "Sex_Consensus")
  12. Construct spatial metaspots using MetaspotsByGroups

    dev

    To address the sparsity of gene expression in Visium ST datasets, hdWGCNA uses a data aggregation approach to produce spatial metaspots. Unlike single-cell metacells which aggregate based on transcriptomes, metaspots aggregate neighboring spots based on spatial coordinates.

    Use MetaspotsByGroups to perform this aggregation. The group.by parameter allows you to slice the Seurat object to construct metaspots separately for different groups (e.g., different ST slides, clusters, or anatomical regions).

    Workflow:

    1. Initialize hdWGCNA setup with SetupForWGCNA.
    2. Aggregate spots using MetaspotsByGroups.
    3. Normalize the resulting metaspots with NormalizeMetacells.
    4. (Optional) Extract the metaspot object using GetMetacellseurat_vhd.
    seurat_obj <- SetupForWGCNA(
      seurat_obj,
      gene_select = "fraction",
      fraction = 0.05,
      wgcna_name = "vis"
    )
    
    seurat_obj <- MetaspotsByGroups(
      seurat_obj,
      group.by = c("region"),
      ident.group = "region",
      assay = 'Spatial'
    )
    seurat_obj  <- NormalizeMetacells(seurat_obj)
    
    # To extract the metaspot object:
    m_obj <- GetMetacellseurat_vhd(seurat_obj)