Last updated: 2025-07-15

Checks: 6 1

Knit directory: multigroup_ctwas_analysis/

This reproducible R Markdown analysis was created with workflowr (version 1.7.0). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


The R Markdown file has unstaged changes. To know which version of the R Markdown file created these results, you’ll want to first commit it to the Git repo. If you’re still working on the analysis, you can ignore this warning. When you’re finished, you can run wflow_publish to commit the R Markdown file and build the HTML.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20231112) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

The results in this page were generated with repository version 394933c. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .Rhistory
    Ignored:    cv/
    Ignored:    figures/lz/IBD-ebi-a-GCST004131/single/
    Ignored:    figures/lz_ld/IBD-ebi-a-GCST004131/single/

Unstaged changes:
    Modified:   analysis/realdata_final_compare_new_regionmerge.Rmd

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the repository in which changes were made to the R Markdown (analysis/realdata_final_compare_new_regionmerge.Rmd) and HTML (docs/realdata_final_compare_new_regionmerge.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
Rmd 6921b53 XSun 2025-06-30 update
html 6921b53 XSun 2025-06-30 update

Introduction

Region Merging Strategy Update

  • Old strategy (“without mapping table”):

When defining boundary genes for region merging, we only considered the molecular trait level. If a molecular trait exceeded a certain threshold, we evaluated whether region merging was needed based on that trait alone.

  • New strategy (“with mapping table”):

We now assess merging based on gene-level PIP. Since multiple molecular traits can span different regions but still map to the same gene, we incorporate this mapping information when defining boundary genes. This can result in more genes and regions being merged compared to the old approach.

In the figures below:

Without mapping table refers to the old strategy.

With mapping table refers to the new, gene-centric strategy.

Unique genes are those identified by one strategy but not the other.

library(ggplot2)
library(gridExtra)

trait_nopsy <- c("LDL-ukb-d-30780_irnt","IBD-ebi-a-GCST004131","aFib-ebi-a-GCST006414","SBP-ukb-a-360",
                 "T1D-GCST90014023","T2D-panukb","ATH_gtexukb","BMI-panukb","HB-panukb",
                 "Height-panukb","HTN-panukb","PLT-panukb","RA-panukb","RBC-panukb",
                 "WBC-ieu-b-30"
                 )
trait_psy <- c("SCZ-ieu-b-5102","ASD-ieu-a-1185","BIP-ieu-b-5110","MDD-ieu-b-102","PD-ieu-b-7",
               "ADHD-ieu-a-1183","NS-ukb-a-230")
traits <- c(trait_nopsy,trait_psy)

plot_overlap_barplot_boundarygene <- function(selected_boundary_genes_old,
                                 selected_boundary_genes_new, trait) {
 
  # Extract gene names
  old_genes <- selected_boundary_genes_old$id
  new_genes <- selected_boundary_genes_new$id
  
  # Compute overlap
  overlap_genes <- intersect(old_genes, new_genes)
  new_genes_unique <- setdiff(new_genes, overlap_genes)
  n_overlap <- length(overlap_genes)
  n_old <- length(old_genes)
  n_new<- length(new_genes)
  
  # Construct data frame for plotting
  df <- data.frame(
    group = rep(c("without \nmapping table","with \nmapping table"), each = 2),
    part = rep(c("Overlap", "Unique"), 2),
    count = c(n_overlap, n_old - n_overlap, n_overlap, n_new- n_overlap)
  )
  
  # Ensure proper stacking order
  df$part <- factor(df$part, levels = c("Unique", "Overlap"))
  
  # Plot
  p <- ggplot(df, aes(x = group, y = count, fill = part)) +
    geom_bar(stat = "identity", width = 0.6) +
    geom_text(aes(label = count), position = position_stack(vjust = 0.5), color = "white", size = 5) +
    scale_fill_manual(values = c("Overlap" = "#1f77b4", "Unique" = "#ff7f0e")) +
    labs(
      x = "",
      y = "Number of selected boundary genes",
      title = trait,
      fill = ""
    ) +
    theme_minimal(base_size = 14)+ 
    theme(axis.text.x = element_text(angle = 45, hjust = 1))
  
  return(p)
}


plot_overlap_barplot_region <- function(region_id_new,
                                 region_id_old, trait) {
  
  # Compute overlap
  overlap_genes <- intersect(region_id_new, region_id_old)
  new_genes_unique <- setdiff(region_id_new, region_id_old)
  n_overlap <- length(overlap_genes)
  n_old <- length(region_id_old)
  n_new<- length(region_id_new)
  
  # Construct data frame for plotting
  df <- data.frame(
    group = rep(c("without \nmapping table","with \nmapping table"), each = 2),
    part = rep(c("Overlap", "Unique"), 2),
    count = c(n_overlap, n_old - n_overlap, n_overlap, n_new- n_overlap)
  )
  
  # Ensure proper stacking order
  df$part <- factor(df$part, levels = c("Unique", "Overlap"))
  
  # Plot
  p <- ggplot(df, aes(x = group, y = count, fill = part)) +
    geom_bar(stat = "identity", width = 0.6) +
    geom_text(aes(label = count), position = position_stack(vjust = 0.5), color = "white", size = 5) +
    scale_fill_manual(values = c("Overlap" = "#1f77b4", "Unique" = "#ff7f0e")) +
    labs(
      x = "",
      y = "Number of merged regions",
      title = trait,
      fill = ""
    ) +
    theme_minimal(base_size = 14)+ 
    theme(axis.text.x = element_text(angle = 45, hjust = 1))
  
  return(p)
}


plot_overlap_barplot_siggene <- function(combined_pip_by_group_multi_old,
                                 combined_pip_by_group_multi_new, trait) {
 
  # Extract gene names
  old_genes <- combined_pip_by_group_multi_old$gene_name[combined_pip_by_group_multi_old$combined_pip > 0.8]
  new_genes <- combined_pip_by_group_multi_new$gene_name[combined_pip_by_group_multi_new$combined_pip > 0.8]
  
  # Compute overlap
  overlap_genes <- intersect(old_genes, new_genes)
  new_genes_unique <- setdiff(new_genes, overlap_genes)
  n_overlap <- length(overlap_genes)
  n_old <- length(old_genes)
  n_new<- length(new_genes)
  
  # Construct data frame for plotting
  df <- data.frame(
    group = rep(c("without \nmapping table","with \nmapping table"), each = 2),
    part = rep(c("Overlap", "Unique"), 2),
    count = c(n_overlap, n_old - n_overlap, n_overlap, n_new- n_overlap)
  )
  
  # Ensure proper stacking order
  df$part <- factor(df$part, levels = c("Unique", "Overlap"))
  
  # Plot
  p <- ggplot(df, aes(x = group, y = count, fill = part)) +
    geom_bar(stat = "identity", width = 0.6) +
    geom_text(aes(label = count), position = position_stack(vjust = 0.5), color = "white", size = 5) +
    scale_fill_manual(values = c("Overlap" = "#1f77b4", "Unique" = "#ff7f0e")) +
    labs(
      x = "",
      y = "Number of gene with PIP > 0.8",
      title = trait,
      fill = ""
    ) +
    theme_minimal(base_size = 14)+ 
    theme(axis.text.x = element_text(angle = 45, hjust = 1))
  
  return(p)
}
folder_results_old <- "/project/xinhe/xsun/multi_group_ctwas/23.multi_group_0515/snakemake_outputs/"

for (trait in traits) {
  # trait <- "LDL-ukb-d-30780_irnt"

  file_old_gene <- paste0(folder_results_old,"/",trait,"/",trait,".3qtls.thin1.shared_all.L5.regionmerge_selected_boundary_genes.RDS")
  if(file.exists(file_old_gene)){
    selected_boundary_genes_old <- readRDS(file_old_gene)
    region_id_old <- unique(selected_boundary_genes_old$region_id)
  }else{
    selected_boundary_genes_old <- NULL
    region_id_old <- NULL
  }
  combined_pip_by_group_multi_old <- readRDS(paste0(folder_results_old,trait,"/",trait,".3qtls.combined_pip_bytype_final.RDS"))
  
  
  file_new_genes <- paste0(folder_results_old,"/",trait,"/",trait,".3qtls.thin1.shared_all.L5.regionmerge_with_mappingtable_selected_boundary_genes.RDS")
  if(file.exists(file_new_genes)){
    selected_boundary_genes_new <- readRDS(file_new_genes)
    region_id_new <- unique(selected_boundary_genes_new$region_id)
  }else{
    selected_boundary_genes_new <- NULL
    region_id_new <- NULL
  }
  combined_pip_by_group_multi_new <- readRDS(paste0(folder_results_old,trait,"/",trait,".3qtls.combined_pip_rmmapping_bytype_final.RDS"))
  
  p1 <- plot_overlap_barplot_boundarygene(selected_boundary_genes_old = selected_boundary_genes_old, selected_boundary_genes_new = selected_boundary_genes_new, trait = trait)
  
  p2 <- plot_overlap_barplot_region(region_id_new = region_id_new,region_id_old = region_id_old, trait = trait)
  
  p3 <- plot_overlap_barplot_siggene(combined_pip_by_group_multi_old = combined_pip_by_group_multi_old, combined_pip_by_group_multi_new = combined_pip_by_group_multi_new,trait = trait)
  grid.arrange(p1, p2, p3, ncol = 3)
  
  combined_pip_by_group_multi_sig_new <- combined_pip_by_group_multi_new$gene_name[combined_pip_by_group_multi_new$combined_pip > 0.8]
  combined_pip_by_group_multi_sig_old <- combined_pip_by_group_multi_old$gene_name[combined_pip_by_group_multi_old$combined_pip > 0.8]
  unique_old <- combined_pip_by_group_multi_sig_old[!combined_pip_by_group_multi_sig_old %in% combined_pip_by_group_multi_sig_new]
  
  unique_new <- combined_pip_by_group_multi_sig_new[!combined_pip_by_group_multi_sig_new %in% combined_pip_by_group_multi_sig_old]
  
  print(sprintf("Unique genes (with mapping table): %s",paste0(unique_new,collapse = ", ")))
  print(sprintf("Unique genes (without mapping table): %s",paste0(unique_old,collapse = ", ")))
  
}

Version Author Date
6921b53 XSun 2025-06-30
[1] "Unique genes (with mapping table): "
[1] "Unique genes (without mapping table): "

Version Author Date
6921b53 XSun 2025-06-30
[1] "Unique genes (with mapping table): "
[1] "Unique genes (without mapping table): HLA-DQB1"

Version Author Date
6921b53 XSun 2025-06-30
[1] "Unique genes (with mapping table): "
[1] "Unique genes (without mapping table): GPR85"

Version Author Date
6921b53 XSun 2025-06-30
[1] "Unique genes (with mapping table): "
[1] "Unique genes (without mapping table): "

Version Author Date
6921b53 XSun 2025-06-30
[1] "Unique genes (with mapping table): "
[1] "Unique genes (without mapping table): "

Version Author Date
6921b53 XSun 2025-06-30
[1] "Unique genes (with mapping table): "
[1] "Unique genes (without mapping table): CAMK1D"

Version Author Date
6921b53 XSun 2025-06-30
[1] "Unique genes (with mapping table): "
[1] "Unique genes (without mapping table): "

Version Author Date
6921b53 XSun 2025-06-30
[1] "Unique genes (with mapping table): "
[1] "Unique genes (without mapping table): "

Version Author Date
6921b53 XSun 2025-06-30
[1] "Unique genes (with mapping table): "
[1] "Unique genes (without mapping table): CYP21A2"

Version Author Date
6921b53 XSun 2025-06-30
[1] "Unique genes (with mapping table): "
[1] "Unique genes (without mapping table): SLC22A3, MCTP2, MYPN, PAMR1, VMAC"

Version Author Date
6921b53 XSun 2025-06-30
[1] "Unique genes (with mapping table): "
[1] "Unique genes (without mapping table): "

Version Author Date
6921b53 XSun 2025-06-30
[1] "Unique genes (with mapping table): "
[1] "Unique genes (without mapping table): ZNF385A, MAMDC2, MORC2, BMF"

Version Author Date
6921b53 XSun 2025-06-30
[1] "Unique genes (with mapping table): "
[1] "Unique genes (without mapping table): "

Version Author Date
6921b53 XSun 2025-06-30
[1] "Unique genes (with mapping table): "
[1] "Unique genes (without mapping table): VEGFA, ABHD12, TAF8, ITSN1, ZNF106, VRK2, CCND3, XAF1"

Version Author Date
6921b53 XSun 2025-06-30
[1] "Unique genes (with mapping table): ACAP1, DMXL1, LIPA, C2CD2, ERAL1, KDM6B, PTEN"
[1] "Unique genes (without mapping table): ATP13A1, LSM4, FYCO1, SUSD1, ANKRD44"

Version Author Date
6921b53 XSun 2025-06-30
[1] "Unique genes (with mapping table): "
[1] "Unique genes (without mapping table): "

Version Author Date
6921b53 XSun 2025-06-30
[1] "Unique genes (with mapping table): "
[1] "Unique genes (without mapping table): "

Version Author Date
6921b53 XSun 2025-06-30
[1] "Unique genes (with mapping table): "
[1] "Unique genes (without mapping table): "

Version Author Date
6921b53 XSun 2025-06-30
[1] "Unique genes (with mapping table): "
[1] "Unique genes (without mapping table): "

Version Author Date
6921b53 XSun 2025-06-30
[1] "Unique genes (with mapping table): "
[1] "Unique genes (without mapping table): "

Version Author Date
6921b53 XSun 2025-06-30
[1] "Unique genes (with mapping table): "
[1] "Unique genes (without mapping table): "

Version Author Date
6921b53 XSun 2025-06-30
[1] "Unique genes (with mapping table): "
[1] "Unique genes (without mapping table): "

sessionInfo()
R version 4.2.0 (2022-04-22)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: CentOS Linux 7 (Core)

Matrix products: default
BLAS/LAPACK: /software/openblas-0.3.13-el7-x86_64/lib/libopenblas_haswellp-r0.3.13.so

locale:
[1] C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] gridExtra_2.3 ggplot2_3.5.1

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.12      highr_0.9        pillar_1.9.0     compiler_4.2.0  
 [5] bslib_0.3.1      later_1.3.0      jquerylib_0.1.4  git2r_0.30.1    
 [9] workflowr_1.7.0  tools_4.2.0      digest_0.6.29    jsonlite_1.8.0  
[13] evaluate_0.15    lifecycle_1.0.4  tibble_3.2.1     gtable_0.3.0    
[17] pkgconfig_2.0.3  rlang_1.1.2      cli_3.6.1        rstudioapi_0.13 
[21] yaml_2.3.5       xfun_0.41        fastmap_1.1.0    withr_2.5.0     
[25] dplyr_1.1.4      stringr_1.5.1    knitr_1.39       generics_0.1.2  
[29] fs_1.5.2         vctrs_0.6.5      sass_0.4.1       tidyselect_1.2.0
[33] rprojroot_2.0.3  grid_4.2.0       glue_1.6.2       R6_2.5.1        
[37] fansi_1.0.3      rmarkdown_2.25   farver_2.1.0     magrittr_2.0.3  
[41] whisker_0.4      scales_1.3.0     promises_1.2.0.1 htmltools_0.5.2 
[45] colorspace_2.0-3 httpuv_1.6.5     labeling_0.4.2   utf8_1.2.2      
[49] stringi_1.7.6    munsell_0.5.0