Aim: generate figures for draft manuscript

1 Supplementary code

Following section includes any intermediary code used in this .Rmd.

1.1 Create output directory

out_dir <- here::here("results", "99_manuscript_figures")
dir.create(out_dir, showWarnings = T)

1.2 Loading plotting functions

source(here::here("R", "plot_global_corr.R"))
source(here::here("R", "plot_chord_diagram.R"))
source(here::here("R", "plot_locus.R"))
source(here::here("R", "plot_edge_diagram.R"))

2 Figure 1

Overview of local and global genetic correlations between neurodegenerative diseases and neuropsychiatric disorders.

2.1 Load data

# LAVA results
results <- readRDS(here::here("results", "02_univar_bivar_test", "results_summary.rds"))

# LDSC results
global <- 
  read_delim(
    file = here::here("results", "01_input_prep", "ldsc_corr", "ldsc_correlations.txt"),
    delim = "\t"
    ) 

# Trait h2
h2 <- 
  read_delim(
    here::here("results", "00_trait_h2", "h2.txt")
  )

# GWAS loci
gwas_loci <- 
  readRDS(here::here("results", "01_input_prep", "gwas_filtered_loci.rds"))

# LD blocks
ld_blocks <- 
  readRDS(here::here("results", "01_input_prep", "gwas_filtered_loci_w_genes.rds")) %>% 
  dplyr::ungroup()

# Overlap with Gerring et al.
overlap_gerring <-
  readRDS(here::here("results", "02_univar_bivar_test", "results_overlap_with_gerring.rds"))

2.2 Data wrangling

p_threshold <- 0.05/nrow(results$bivar)

# Tidy labels
bivar_tidy <- 
  results$bivar %>% 
  dplyr::inner_join(
    ld_blocks %>% 
      dplyr::select(locus, assoc_gwas)
  ) 

global_tidy <- 
  global %>%
  dplyr::mutate(
    p1 = p1 %>%
      stringr::str_replace_all("[:digit:]", "") %>%
      stringr::str_remove("\\..*"),
    p2 = p2 %>%
      stringr::str_replace_all("[:digit:]", "") %>%
      stringr::str_remove("\\..*")
  )

# N of phen with CI 97.5 == 1
n_upper_r2_ci <-
  bivar_tidy %>% 
  dplyr::filter(
    p < p_threshold,
    r2.upper == 1
  ) %>% 
  dplyr::count(phen1, phen2, name = "n_upper_r2_ci") 

bivar_count <-
  bivar_tidy %>% 
  dplyr::filter(
    p < p_threshold
  ) %>% 
  dplyr::count(phen1, phen2, name = "total_n") %>% 
  dplyr::left_join(n_upper_r2_ci) %>% 
  dplyr::mutate(
    label = str_c(phen1, " vs ", phen2),
    n_upper_r2_ci =
      case_when(
        is.na(n_upper_r2_ci) ~ 0,
        TRUE ~ as.numeric(n_upper_r2_ci)
      ),
    n_upper_r2_ci_not_one =
      total_n - n_upper_r2_ci
  ) %>% 
  tidyr::pivot_longer(
    cols = contains("n_upper_r2")
  ) %>% 
  dplyr::mutate(
    name =
      case_when(
        name == "n_upper_r2_ci" ~ "TRUE",
        TRUE ~ "FALSE"
      )
  )

overlap_gerring_long <-
  overlap_gerring %>% 
  dplyr::filter(overlapping_phen == 2) %>% 
  dplyr::select(
    locus.rhr,
    chr, start, stop,
    phen1.rhr, phen2.rhr,
    contains("n_snps"),
    contains("rho"),
    contains("r2"),
    p.rhr,
    p.gerring
    ) %>% 
  dplyr::mutate(
    significant = 
      case_when(
        p.rhr < p_threshold & p.gerring < 0.05/24054 ~ "Both",
        p.rhr >= p_threshold & p.gerring < 0.05/24054 ~ "Gerring et. al"
      )
  ) %>% 
  tidyr::pivot_longer(cols = n_snps.gerring:p.gerring, names_to = "feature") %>% 
  dplyr::mutate(
    dataset =
      case_when(
        stringr::str_detect(feature, "rhr") ~ "rhr",
        stringr::str_detect(feature, "gerring") ~ "gerring"
      ),
    feature =
      feature %>% 
      stringr::str_remove("\\.rhr|\\.gerring"), 
    feature =  
      case_when(
        feature == "p" ~ "-log10(p)",
        TRUE ~ feature
      ),
    label = 
      str_c(locus.rhr, ": ", phen1.rhr, "+", phen2.rhr)
  ) %>% 
  tidyr::pivot_wider(
    names_from = dataset,
    values_from = value
  )  %>% 
  dplyr::mutate(
    across(
      .cols = rhr:gerring,
      ~ case_when(
        feature == "-log10(p)" ~ -log10(.x),
        TRUE ~ .x
      )
    )
  )

2.3 Main figure

plot <- vector(mode = "list", length = 2)

plot[[1]] <- 
  plot_global_bivar(
    global_corr = 
      global_tidy,
    bivar_corr = 
      bivar_tidy,
    n_phenotypes = 6
  ) + 
  theme(legend.position = "bottom")

plot[[2]] <- 
  bivar_count %>% 
  ggplot(
    aes(
      x = 
        fct_reorder(
          .f = label,
          .x = total_n,
          .fun = max,
          .desc = T
        ),
      y = value,
      fill = name
    )
  ) +
  geom_col(
    colour = "black"
  ) +
  scale_fill_grey(breaks=c("TRUE", "FALSE")) +
  labs(
    x = "",
    y = "Number of significant\nlocal genetic correlations",
    fill = "Does upper limit of\nr2 95% CI include 1?"
  ) +
  theme_rhr +
  theme(legend.position = "bottom")

fig <-
  cowplot::plot_grid(
    cowplot::plot_grid(
      NULL, plot[[1]],
      labels = letters[1:2]
    ),
    plot[[2]],
    ncol = 1,
    labels = c("", "c"),
    rel_heights = c(1.5,1)
  )
  
  

fig

png(
  file = file.path(out_dir, "fig_1a_global_local.png"),
  bg = "transparent", 
  width = 120, 
  height = 120, 
  units = "mm",
  res = 300
)

plot_bivar_chord_diagram(
    bivar_corr = 
      bivar_tidy %>% 
      dplyr::filter(
        p < p_threshold
      ),
    fct_phen = fct_disease,
    palette = RColorBrewer::brewer.pal(n = 6, name = "BrBG")
  )

dev.off()

ggsave(
  fig,
  path = out_dir, 
  filename = "fig_1bc_global_local.png",
  device = "png", 
  width = 150, 
  height = 180, 
  dpi = 300, 
  units = "mm"
)

2.4 Supplementary figure(s)

suppfig <- list()

plot <- list()

plot[[1]] <- 
  gwas_loci %>% 
  dplyr::distinct(LD_LOC, LD_CHR, LD_start, LD_end) %>% 
  dplyr::count(LD_CHR) %>% 
  ggplot(
    aes(
      x = 
        forcats::fct_reorder(
          .f = as.factor(LD_CHR), 
          .x = n, 
          .fun = median, 
          .desc = FALSE
        ),
      y = n
    )
  ) +
  geom_col(colour = "black", fill = "#d9d9d9", size = 0.2) +
  labs(
    x = "Chromosome",
    y = "Number of LD blocks"
  ) +
  coord_flip() + 
  theme_rhr + 
  theme(axis.text.x = element_text(angle = 0, hjust = 0.5))

plot[[2]] <- 
  gwas_loci %>% 
  dplyr::count(LD_CHR, GWAS) %>% 
  dplyr::mutate(
    LD_CHR = as.factor(LD_CHR),
    GWAS = 
      str_to_upper(GWAS) %>% 
      fct_relevel(., fct_disease)
  ) %>% 
  ggplot(
    aes(
      x = fct_rev(LD_CHR),
      y = n,
      fill = GWAS
    )
  ) +
  geom_col(
    position = position_dodge2(preserve = "single"),
    colour = "black", 
    size = 0.2
  ) +
  scale_fill_brewer(
    palette = "BrBG",
    type = "div"
  ) +
  labs(
    x = "Chromosome",
    y = "Number of SNPs"
  ) + 
  coord_flip() + 
  facet_wrap(vars(GWAS), scales = "free_x") +
  theme_rhr +
  theme(
    axis.text.x = element_text(angle = 0, hjust = 0.5),
    legend.position = "none"
    )

suppfig[[1]] <- 
  cowplot::plot_grid(
    plotlist = plot,
    align = "v",
    axis = "lr",
    ncol = 1,
    nrow = 2,
    rel_heights = c(1,2.5),
    labels = letters[1:length(plot)]
  )

suppfig[[1]]

plots <- list()

plots[[1]] <- 
  overlap_gerring_long %>% 
  dplyr::filter(
    feature %in% 
      c("rho")
  ) %>% 
  ggplot(
    aes(
      x = rhr,
      y = gerring
    )
  )  +
  geom_point(
    size = 1.5,
    alpha = 0.8,
    shape = 21,
    colour = "black",
    aes(fill = significant)
    
  ) +
  geom_abline(
    intercept = 0,
    linetype = "dashed",
    colour = "grey",
    alpha = 0.7
  ) +
  ggrepel::geom_label_repel(
    aes(
      label = label
      ),
    size = 1.5,
    alpha = 0.7,
    box.padding = 0.2,
    min.segment.length = unit(0, 'lines')
  ) +
  facet_wrap(vars(feature)) +
  coord_fixed(
    xlim = c(0, max(overlap_gerring$rho.gerring)), 
    ylim = c(0, max(overlap_gerring$rho.gerring))
    ) +
  labs(
    x = "This study",
    y = "Gerring et al. 2022"
  ) +
  theme_rhr + 
  theme(
    axis.text.x = element_text(angle = 0, hjust = 0.5),
    legend.position = "top"
  )

plots[[2]] <-
  overlap_gerring_long %>% 
  dplyr::filter(
    feature %in% 
      c("-log10(p)")
  ) %>% 
  ggplot(
    aes(
      x = rhr,
      y = gerring
    )
  ) +
  geom_point(
    size = 1.5,
    alpha = 0.8,
    shape = 21,
    colour = "black",
    aes(fill = significant)
  ) +
  geom_hline(
    yintercept = -log10(0.05/24054),
    linetype = "dashed",
    colour = "grey",
    alpha = 0.7
    ) +
  geom_vline(
    xintercept = -log10(p_threshold),
    linetype = "dashed",
    colour = "grey",
    alpha = 0.7
    ) +
  ggrepel::geom_label_repel(
    aes(
      label = label
      ),
    size = 1.5,
    alpha = 0.7,
    # box.padding = 0.25,
    min.segment.length = unit(0, 'lines')
  ) +
  facet_wrap(vars(feature)) +
  coord_fixed(
    xlim = c(0, max(-log10(overlap_gerring$p.gerring))*1.25), 
    ylim = c(0, max(-log10(overlap_gerring$p.gerring))*1.25)
  ) +
  labs(
    x = "This study",
    y = "Gerring et al. 2022"
  ) +
  theme_rhr + 
  theme(
    axis.text.x = element_text(angle = 0, hjust = 0.5),
    legend.position = "top"
  )

plots[[3]] <- 
  overlap_gerring_long %>% 
  dplyr::filter(
    feature %in% 
      c("n_snps")
  ) %>% 
  ggplot(
    aes(
      x = rhr,
      y = gerring
    )
  ) +
  geom_point(
    size = 1.5,
    alpha = 0.8,
    shape = 21,
    colour = "black",
    aes(fill = significant)
  ) +
  geom_abline(
    intercept = 0,
    linetype = "dashed",
    colour = "grey",
    alpha = 0.7
  ) +
  ggrepel::geom_label_repel(
    aes(
      label = label
      ),
    size = 1.5,
    alpha = 0.7,
    box.padding = 0.4,
    min.segment.length = unit(0, 'lines')
  ) +
  facet_wrap(vars(feature)) +
  coord_fixed(
    xlim = c(0, max(overlap_gerring$n_snps.gerring)*1.25), 
    ylim = c(0, max(overlap_gerring$n_snps.gerring)*1.25)
  ) +
  labs(
    x = "This study",
    y = "Gerring et al. 2022"
  ) +
  theme_rhr + 
  theme(
    axis.text.x = element_text(angle = 0, hjust = 0.5),
    legend.position = "top"
  )

suppfig[[2]] <- 
  ggpubr::ggarrange(
    plotlist = plots,
    labels = letters[c(1:length(plots))],
    common.legend = TRUE, 
    align = "h"
  )

suppfig[[2]]

ggsave(
  suppfig[[1]],
  path = out_dir, 
  filename = "suppfig_1_ldblocks.png",
  device = "png", 
  width = 130, 
  height = 180, 
  dpi = 300, 
  units = "mm"
)

ggsave(
  suppfig[[2]],
  path = out_dir, 
  filename = "suppfig_2_overlap_gerring.png",
  device = "png", 
  width = 150, 
  height = 150, 
  dpi = 300, 
  units = "mm"
)

2.5 Supplementary table(s)

2.5.1 LD blocks

xlsx <- 
  setNames(
    vector(mode = "list", length = 2),
    c("column_descriptions", "results")
  )

xlsx[[2]] <-
  ld_blocks
  
xlsx[[1]] <-
  tibble(
    column_name = colnames(ld_blocks),
    description = 
      c(
        "LD block ID",
        "LD block chromosome",
        "LD block start base pair",
        "LD block end base pair",
        "Number of genes overlapping LD block",
        "Genes overlapping LD block (ensembl ID)",
        "Genes overlapping LD block (gene name)",
        "Number of GWAS with genome-wide significant SNPs overlapping LD block",
        "GWAS with genome-wide significant SNPs overlapping LD block"
      )
  )

openxlsx::write.xlsx(
  xlsx,
  file = file.path(out_dir, "SupplementaryTable1_ld_blocks.xlsx"),
  row.names = FALSE,
  headerStyle = openxlsx::createStyle(textDecoration = "BOLD"),
  firstRow = TRUE,
  append = TRUE,
  overwrite = TRUE
)

2.5.2 Global correlations

xlsx <- 
  setNames(
    vector(mode = "list", length = 3),
    c("column_descriptions", "trait_h2", "global_rg")
  )

xlsx[[3]] <-
  global

xlsx[[2]] <-
  h2

xlsx[[1]] <-
  tibble(
    sheet = rep("trait_h2", ncol(h2)),
  column_name = colnames(h2),
  description = 
    c(
      "Phenotype",
      "Estimated SNP heritability (h2, observed scale)",
      "Standard error of h2",
      "Genomic inflation factor (lambda GC); equivalent of median(chi^2)/0.4549, where denominator indicates expected median of the chi-squared distribution with 1 degree of freedom",
      "Mean chi-square statistic",
      "LD score regression intercept",
      "Standard error for LD score regression intercept",
      "Heritability Z-score (equivalent of h2/se)"
    )
) %>% 
  bind_rows(
    tibble(
      sheet = rep("global_rg", ncol(global)),
  column_name = colnames(global),
  description = 
    c(
      "Phenotype 1",
      "Phenotype 2",
      "The estimated genetic correlation",
      "The bootstrap standard error of the genetic correlation estimate",
      "Z-score for genetic correlation",
      "P-value for genetic correlation",
      "Estimated SNP heritability (h2, observed scale) of the second phenotype ",
      "Standard error of h2 for phenotype 2",
      "Single-trait LD score regression intercept for phenotype 2",
      "Standard error for single-trait LD score regression intercept for phenotype 2",
      "Estimated genetic covariance between p1 and p2",
      "Bootstrap standard error of cross-trait LD score regression intercept"
    )
)
  )


openxlsx::write.xlsx(
  xlsx,
  file = file.path(out_dir, "SupplementaryTable2_ldsc_global.xlsx"),
  row.names = FALSE,
  headerStyle = openxlsx::createStyle(textDecoration = "BOLD"),
  firstRow = TRUE,
  append = TRUE,
  overwrite = TRUE
)

2.5.3 Univariate/bivariate correlations

xlsx <- 
  setNames(
    vector(mode = "list", length = 3),
    c("column_descriptions", "univariate", "bivariate")
  )

xlsx[[2]] <-
  results$univ

xlsx[[3]] <-
  results$bivar %>% 
  dplyr::select(-fdr)

xlsx[[1]] <-
  tibble(
    column = colnames(results$univ),
    description = 
      c(
        "LD block ID",
        "LD block chromosome",
        "LD block start base pair",
        "LD block end base pair",
        "The number of SNPs within the LD block",
        "The number of PCs retained within the LD block",
        "Analysed phenotype",
        "Observed local heritability",
        "P-value from the univariate test (F-test for continuous, Chi-sq for binary)"
      )
  ) %>%
  dplyr::mutate(sheet = "univariate") %>% 
  dplyr::bind_rows(
    tibble(
      column = colnames(results$bivar),
      description = 
        c(
          "LD block ID",
          "LD block chromosome",
          "LD block start base pair",
          "LD block end base pair",
          "The number of SNPs within the LD block",
          "The number of PCs retained within the LD block",
          "Phenotype 1",
          "Phenotype 2",
          "Standardised coefficient for the local genetic correlation",
          "Lower 95% confidence estimate for rho",
          "Upper 95% confidence estimate for rho",
          "Equivalent of taking the square of rho. Denotes the proportion of variance in genetic signal for phen1 explained by phen2 (and vice versa)",
          "Lower 95% confidence estimate for r2",
          "Upper 95% confidence estimate for r2",
          "Simulation p-values for the local genetic correlation"
        )
    ) %>% 
      dplyr::mutate(sheet = "bivariate")
  ) %>% 
  dplyr::select(sheet, everything())

openxlsx::write.xlsx(
  xlsx,
  file = file.path(out_dir, "SupplementaryTable3_lava_local.xlsx"),
  row.names = FALSE,
  headerStyle = openxlsx::createStyle(textDecoration = "BOLD"),
  firstRow = TRUE,
  append = TRUE,
  overwrite = TRUE
)

2.5.4 Overlaps

overlap_smeland <- 
  readRDS(here::here("results", "02_univar_bivar_test", "results_overlap_with_smeland.rds"))

xlsx <- 
  setNames(
    vector(mode = "list", length = 3),
    c("column_descriptions", "smeland", "gerring")
  )

xlsx[[2]] <-
  overlap_smeland %>% 
  dplyr::select(
    -n_snps, -n_pcs, 
    -contains("r2"), -fdr, 
    -contains("lower"), -contains("upper"),
    -smeland_same_direction_of_effect, 
    -rho_consistent_w_effect_consistency
    )

xlsx[[3]] <-
  overlap_gerring %>% 
    dplyr::mutate(
      trait_pair.rhr =
        str_c(phen1.rhr, "-", phen2.rhr),
      trait_pair.gerring = 
        str_c(phen1.gerring, "-", phen2.gerring)
    ) %>% 
  dplyr::select(
    locus = locus.rhr,
    chr, start, stop,
    overlapping_phen,
    trait_pair.rhr,
    contains("rhr"),
    trait_pair.gerring,
    contains("gerring"),
    -contains("phen"),
    -locus.gerring,
    -contains("lower"),
    -contains("upper")
  )

xlsx[[1]] <-
  tibble(
    column = colnames(xlsx[[2]]),
    description = 
      c(
        "LD block ID",
        "LD block chromosome",
        "LD block start base pair",
        "LD block end base pair",
        "Phenotype 1",
        "Phenotype 2",
        "LAVA: standardised coefficient for the local genetic correlation",
        "LAVA: simulation p-values for the local genetic correlation",
        "Smeland et al. 2021: lead SNP (rsID)",
        "Smeland et al. 2021: lead SNP (genomic location, GRCh37)",
        "Smeland et al. 2021: alleles",
        "Smeland et al. 2021: nearest gene to lead SNP",
        "Smeland et al. 2021: p-value of lead SNP in original PD GWAS",
        "Smeland et al. 2021: effect size of lead SNP in original PD GWAS (effect sizes are given with reference to allele 1)",
        "Smeland et al. 2021: p-value of lead SNP in original SCZ GWAS",
        "Smeland et al. 2021: effect size of lead SNP in original SCZ GWAS (effect sizes are given with reference to allele 1)",
        "Smeland et al. 2021: conjunctional FDR value"
      )
  ) %>%
  dplyr::mutate(sheet = "smeland") %>% 
  dplyr::bind_rows(
    tibble(
      column = colnames(xlsx[[3]]),
      description = 
        c(
          "LD block ID",
          "LD block chromosome",
          "LD block start base pair",
          "LD block end base pair",
          "This study: trait pair",
          "This study: number of PCs retained within the LD block",
          "This study: number of SNPs within the LD block",
          "This study: simulation p-values for the local genetic correlation",
          "This study: proportion of variance in genetic signal for phen1 explained by phen2 (and vice versa)",
          "This study: standardised coefficient for the local genetic correlation",
          "Gerring et al. 2022: trait pair",
          "Gerring et al. 2022: number of PCs retained within the LD block",
          "Gerring et al. 2022: number of SNPs within the LD block",
          "Gerring et al. 2022: simulation p-values for the local genetic correlation",
          "Gerring et al. 2022: proportion of variance in genetic signal for phen1 explained by phen2 (and vice versa)",
          "Gerring et al. 2022: standardised coefficient for the local genetic correlation"          
        )
    ) %>% 
      dplyr::mutate(sheet = "gerring")
  ) %>% 
  dplyr::select(sheet, everything())

openxlsx::write.xlsx(
  xlsx,
  file = file.path(out_dir, "SupplementaryTable4_lava_comparisons.xlsx"),
  row.names = FALSE,
  headerStyle = openxlsx::createStyle(textDecoration = "BOLD"),
  firstRow = TRUE,
  append = TRUE,
  overwrite = TRUE
)

3 Figure 2

3.1 Data wrangling

bivar_pair_type <-
  bivar_tidy  %>% 
  dplyr::filter(
    p < p_threshold
  ) %>%  
  dplyr::group_by(locus, phen1, phen2) %>% 
  dplyr::mutate(
    phen1_overlapping_assoc_gwas =
      any(phen1 %in% unlist(assoc_gwas)),
    phen2_overlapping_assoc_gwas =
      any(phen2 %in% unlist(assoc_gwas)),
    sum_phen_overlapping_assoc_gwas =
      sum(phen1_overlapping_assoc_gwas, phen2_overlapping_assoc_gwas),
    phen1_is_neurodegen = 
      phen1 %in% c("AD", "PD", "LBD"),
    phen2_is_neurodegen = 
      phen2 %in% c("AD", "PD", "LBD"),
    sum_phen_is_neurodegen =
      sum(phen1_is_neurodegen, phen2_is_neurodegen),
    trait_pair_type =
      case_when(
        sum_phen_is_neurodegen == 2 ~ "Both neurodegenerative",
        sum_phen_is_neurodegen == 1 ~ "Mixed",
        sum_phen_is_neurodegen == 0 ~ "Both neuropsychiatric"
      )
  ) %>% 
  dplyr::group_by(locus, phen1, phen2) %>% 
  dplyr::mutate(
    locus_labels =
      stringr::str_c(
        "LD block ", locus, 
        " (", str_c(unlist(assoc_gwas), collapse = ", "), ")",
        "\nchr", chr, ": ", scales::comma(start), "-", scales::comma(stop)
      )
  )

locus_labels <- 
  setNames(
    nm = unique(bivar_pair_type$locus),
    object = unique(bivar_pair_type$locus_labels)
  )

3.2 Main figure

plot <- vector(mode = "list", length = 4)

plot[[1]] <- 
  bivar_pair_type %>% 
  ggplot(
    aes(
      x = as.factor(sum_phen_overlapping_assoc_gwas),
      fill = trait_pair_type
    )
  ) + 
  geom_bar(colour = "black") +
  scale_fill_manual(
    values = RColorBrewer::brewer.pal(n = 3, name = "BrBG")[c(1,3,2)],
    name = "Traits in pair"
  ) + 
  labs(x = "Number of traits in pair with genome-\nwide significant SNPs overlapping LD block", y = "Count") +
  coord_flip() +
  guides(
    fill = guide_legend(
      nrow = 3,
      title.position = "top"
      )
    ) +
  theme_rhr +
  theme(
    legend.position = "bottom",
    axis.text.x = element_text(angle = 0, hjust = 0.5, vjust = 0)
  )

plot[[2]] <-
  ggplot +
  geom_blank()

plot[[3]] <-
  plot_edge_diagram(
    bivar_corr = 
      bivar_pair_type %>% 
      dplyr::filter(locus %in% c(1719, 2281)),
    phen = fct_disease, 
    locus_labels = locus_labels,
    multiple_corr = FALSE,
    geom_node_size = 2,
    geom_label_size = 1.7,
    base_size = 8,
    ncol = 1,
    seed = 57 #26,35
  ) +
  theme(
    legend.position = "none"
  )

plot[[4]] <-
  bivar_tidy %>% 
  dplyr::filter(
    locus %in% c(681, 1273, 2351),
    phen1 %in% c("AD", "LBD", "PD"),
    phen2 %in% c("AD", "LBD", "PD")
  ) %>% 
  dplyr::mutate(
    rho_fill = 
      case_when(
        p < p_threshold ~ round(rho, 2)
      ),
    rho_label =
      case_when(
        locus == 1273 & phen1 == "AD" & phen2 == "PD" |
          locus == 2351 & phen1 == "AD" & phen2 == "LBD" ~ str_c(round(rho, 2), "*"),
        TRUE ~ as.character(round(rho, 2))
      )
  ) %>% 
  ggplot(
    aes(
      x = phen1,
      y = phen2
    )
  ) +
  geom_tile(
    aes(fill = rho_fill),
    colour = "black"
  ) +
  geom_text(
    aes(label = rho_label),
    size = 2.5
  ) +
  labs(x = "", y = "", fill = "Local rg (rho)") +
  facet_wrap(
    vars(locus), 
    ncol = 3,
    labeller = as_labeller(locus_labels)
  ) +
  scale_fill_distiller(
    palette = "RdYlBu", 
    direction = -1, 
    na.value = "#cccccc", 
    limits = c(-1, 1)
    ) +
  theme_rhr + 
  theme(
    axis.text.x = element_text(angle = 0, hjust = 0.5),
    panel.grid = element_blank()
    )

fig <-
  cowplot::plot_grid(
    cowplot::plot_grid(
      plotlist = plot[c(1:3)],
      ncol = 3,
      nrow = 1,
      # align = "hv",
      # axis = "bt",
      rel_widths = c(1,0.8,1.5),
      labels = c("", "b", ""), 
      # move "b" to right of middle blank plot
      label_x = 0.9, label_y = 1 
    ),
    plot[[4]],
    ncol = 1,
    nrow = 2,
    # align = "v",
    # axis = "lr",
    rel_heights = c(1.5,1),
    labels = c("a", "c")
  )


fig

ggsave(
  fig,
  path = out_dir, 
  filename = "fig_2_local_regions.png",
  device = "png", 
  width = 180, 
  height = 160, 
  dpi = 300, 
  units = "mm"
)

3.3 Supplementary figure(s)

Refer to 05_run_noproxy_check.html.

3.4 Supplementary table(s)

Refer to 05_run_noproxy_check.html.

4 Figure 3

4.1 Load data

multireg <-
  list.files(
    here::here(
      "results", 
      "03_partial_corr_multi_reg"
      ),
    pattern = ".multireg.lava.rds", 
    full.names = T
  ) %>% 
  readRDS() %>% 
  lapply(., function(x){
    
    x %>% 
      lapply(., function(y){
        
        y %>% 
          lapply(., function(z){
            
            z %>% 
              qdapTools::list_df2df(col1 = "model_number")
            
          }) %>% 
          qdapTools::list_df2df(col1 = "list_name")
        
      }) %>% 
      qdapTools::list_df2df()
    
  }) %>% 
    qdapTools::list_df2df(col1 = "locus") %>% 
    dplyr::select(-X1) %>% 
    dplyr::mutate(
      model_type = 
        case_when(
          locus == 1719 & list_name == "L1" ~ "intermediate",
          locus == 1719 & list_name == "L2" ~ "full",
          TRUE ~ "full"
          )
    ) %>% 
    dplyr::select(
      locus, contains("model"), 
      outcome, predictors, 
      everything(), -contains("list")
      )

4.2 Data wrangling

multireg_tidy <-
  # filter for full models
  multireg %>% 
  dplyr::filter(
    model_type == "full"
  ) %>%
  dplyr::select(locus, contains("model"), outcome) %>% 
  dplyr::distinct() %>% 
  # join all predictors
  dplyr::inner_join(
    multireg,
    c("locus", "model_number", "model_type", "outcome")
  ) %>% 
  dplyr::mutate(
    predictors = predictors %>%
      stringr::str_replace_all("[:digit:]", "") %>%
      stringr::str_remove("\\..*"),
    outcome = outcome %>% 
      stringr::str_replace_all("[:digit:]", "") %>%
      stringr::str_remove("\\..*"),
    p_text = case_when(
      p < 0.001 ~ "***",
      p < 0.01 ~ "**",
      p < 0.05 ~ "*"
    ),
    locus = 
      as.numeric(locus)
  )

multireg_tidy <- 
  multireg_tidy %>% 
  dplyr::inner_join(
    multireg_tidy %>% 
      dplyr::group_by(outcome, locus) %>% 
      dplyr::summarise(predictors = str_c(predictors, collapse = " + ")) %>% 
      dplyr::mutate(model = str_c(outcome, " ~ ", predictors)) %>% 
      dplyr::select(outcome, locus, model)
  )

4.3 Main figure

plot <- vector(mode = "list", length = 2)

# plot multi reg with at least one significant predictor
plot[[1]] <-
  multireg_tidy %>% 
  dplyr::filter(
    p < 0.05
  ) %>% 
  dplyr::distinct(locus, model) %>% 
  dplyr::inner_join(multireg_tidy) %>% 
  ggplot(
    aes(
      x = predictors, 
      y = gamma, 
      ymin = gamma.lower, 
      ymax = gamma.upper
    )
  ) +
  geom_pointrange(
    colour = "#888888", fatten = 0.25
  ) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  geom_text(
    aes(
      y = gamma.upper + (0.1 * gamma.upper), 
      label = p_text
    )
  ) +
  facet_wrap(
    vars(locus, model), 
    scales = "free_x",
    nrow = 1
  ) +
  labs(x = "Predictors", y = "Standardised multiple\nregression coefficient") +
  theme_rhr

plot[[2]] <- 
  multireg_tidy  %>% 
  dplyr::filter(
    p < 0.05
  ) %>%  
  dplyr::mutate(
    locus_model =
      str_c(locus, model, sep = "\n")
  ) %>% 
  dplyr::distinct(
    locus, locus_model, r2, r2.lower, r2.upper
  ) %>% 
  ggplot(
    aes(
      x = locus_model %>% 
        fct_reorder(., .x = as.numeric(locus), .fun = max),
      y = r2,
      ymin = r2.lower,
      ymax = r2.upper
    )
  ) +
  geom_col(colour = "black", fill = "#d9d9d9") + 
  geom_errorbar(width=.2) +
  labs(
    x = "Locus and model",
    y = "Multivariate r2"
  ) +
  theme_rhr

  
fig <- 
  cowplot::plot_grid(
    plotlist = plot, 
    ncol = 1, 
    axis = "lr", 
    align = "v",
    labels = letters[1:length(plot)]
  )

fig

ggsave(
  fig,
  path = out_dir, 
  filename = "fig_3_multireg.pdf",
  device = "pdf", 
  width = 130, 
  height = 120, 
  dpi = 300, 
  units = "mm"
)

4.4 Supplementary figure

supp_fig <- vector(mode = "list", length = 2)

# plot non-signif multi reg 
supp_fig[[1]] <- 
  multireg_tidy %>% 
  dplyr::filter(
    p >= 0.05
  ) %>% 
  dplyr::group_by(
    locus, model
  ) %>% 
  dplyr::filter(
    n() > 1
  ) %>% 
  dplyr::mutate(
    model = str_wrap(model, width = 15)
  ) %>% 
  ggplot(
    aes(
      x = predictors, 
      y = gamma, 
      ymin = gamma.lower, 
      ymax = gamma.upper
      )
    ) +
  geom_pointrange(
    colour = "#888888", fatten = 0.25
    ) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  geom_text(
    aes(
      y = gamma.upper + (0.1 * gamma.upper), 
      label = p_text
      )
    ) +
  facet_wrap(
    vars(locus, model), 
    scales = "free_x",
    nrow = 1
    ) +
  labs(x = "Predictors", y = "Standardised multiple\nregression coefficient") +
  theme_rhr

supp_fig[[2]] <- 
  multireg_tidy  %>% 
  dplyr::filter(
    p >= 0.05
  ) %>% 
  dplyr::group_by(
    locus, model
  ) %>% 
  dplyr::filter(
    n() > 1
  ) %>%  
  dplyr::mutate(
    model = str_wrap(model, width = 15),
    locus_model =
      str_c(locus, model, sep = "\n")
  ) %>% 
  dplyr::distinct(
    locus, locus_model, r2, r2.lower, r2.upper
  ) %>% 
  ggplot(
    aes(
      x = locus_model %>% 
        fct_reorder(., .x = as.numeric(locus), .fun = max),
      y = r2,
      ymin = r2.lower,
      ymax = r2.upper
    )
  ) +
  geom_col(colour = "black", fill = "#d9d9d9") + 
  geom_errorbar(width=.2) +
  labs(
    x = "Locus and model",
    y = "Multivariate r2"
  ) +
  theme_rhr 

  
supp_fig <- 
  cowplot::plot_grid(
    plotlist = supp_fig, 
    ncol = 1, 
    axis = "lr", 
    align = "v",
    labels = letters[1:length(supp_fig)]
  )

supp_fig

ggsave(
  supp_fig,
  path = out_dir, 
  filename = "suppfig_4_multireg.png",
  device = "png", 
  width = 170, 
  height = 140, 
  dpi = 300, 
  units = "mm"
)

4.5 Supplementary tables

xlsx <- 
  setNames(
    vector(mode = "list", length = 2),
    c("column_descriptions", "results")
  )

xlsx[[2]] <-
  multireg

xlsx[[1]] <-
  tibble(
    column = colnames(multireg),
    description = 
      c(
        "LD block ID",
        "For multiple regressions with multiple intermediate models, this is a model identifier",
        "Whether the regression model is an intermediate or full model",
        "Outcome phenotype",
        "Predictor phenotype",
        "Standardised multiple regression coefficient",
        "Lower bound of 95% confidence interval for gamma",
        "Upper bound of 95% confidence interval for gamma",
        "Proportion of variance in genetic signal for the outcome phenotype explained by all predictor phenotypes simultaneously",
        "Lower bound of 95% confidence interval for r2",
        "Upper bound of 95% confidence interval for r2",
        "Simulation p-values for the gammas"
      )
  )

openxlsx::write.xlsx(
  xlsx,
  file = file.path(out_dir, "SupplementaryTable6_multireg.xlsx"),
  row.names = FALSE,
  headerStyle = openxlsx::createStyle(textDecoration = "BOLD"),
  firstRow = TRUE,
  append = TRUE,
  overwrite = TRUE
)

5 Figure 4

5.1 Load data

# Genic regions
gene_filtered_loci <- 
  readRDS(
    here::here("results", "04_eqtl_univar_bivar", "window_100000", "gene_filtered_loci.rds")
  )

# LAVA results
qtl_tidy <- readRDS(here::here("results", "04_eqtl_univar_bivar", "results_summary.rds"))

# Reference gene annotation
ref <- import("/data/references/ensembl/gtf_gff3/v87/Homo_sapiens.GRCh37.87.gtf")
ref <- ref %>% keepSeqlevels(c(1:22), pruning.mode = "coarse") 
ref <- ref[ref$type == "gene"]

# Partial correlations
pcorr <-
  readRDS(here::here("results", "04_eqtl_univar_bivar", "window_100000", "pcor", "eqtl_pcor.partcorr.lava.rds"))

5.2 Data wrangling

qtl_to_plot <- 
  qtl_tidy$bivar %>% 
  lapply(.,function(df){
    
    df %>% 
      dplyr::filter(
        str_detect(phen2, "ENSG")
      ) %>% 
      dplyr::mutate(
        fill_rho = case_when(
          p < 0.05 ~ round(rho, 2)
        ),
        sig = case_when(
          fdr < 0.05 ~ "**"
        ),
        nom_sig = case_when(
          p < 0.05 & fdr > 0.05 ~ "."
        ),
        chr_gene = str_c("chr ",chr,": ", gene_name)
      )
    
  })

# Filter for genic regions containing significant eqtl-gwas local rg
# Include all significant local rgs in genic region 
qtl_tidy_filtered <-
  qtl_tidy$bivar$window_100000 %>% 
  dplyr::filter(fdr < 0.05, str_detect(phen2, "ENSG")) %>% 
  dplyr::distinct(ld_block, eqtl_dataset, gene_locus) %>% 
  dplyr::inner_join(qtl_tidy$bivar$window_100000) %>% 
  dplyr::filter(fdr < 0.05)

# Create list with results split by LD block
qtl_list <- 
  setNames(
    qtl_tidy_filtered %>% 
      dplyr::group_split(ld_block),
    nm = 
      str_c("locus_", unique(qtl_tidy_filtered$ld_block))
  )

# Number of genes implicated in each ld block
n_genes <- 
  gene_filtered_loci %>% 
  dplyr::ungroup() %>% 
  dplyr::select(ld_block = locus, gene_locus = gene_id) %>% 
  dplyr::distinct(ld_block, gene_locus) %>% 
  dplyr::count(ld_block, name = "n_genes") %>% 
  dplyr::inner_join(
    qtl_tidy_filtered %>% 
      dplyr::distinct(ld_block, gene_locus) %>% 
      dplyr::count(ld_block, name = "n_signif_genes")
  ) %>% 
  dplyr::mutate(n_not_signif_genes = n_genes - n_signif_genes) %>% 
  tidyr::pivot_longer(
    cols = contains("signif"),
    names_to = "signif",
    values_to = "n"
  ) %>% 
  dplyr::mutate(
    signif =
      case_when(
        signif == "n_signif_genes" ~ "TRUE",
        TRUE ~ "FALSE"
      )
  )

# Number of distinct eQTL-disease rgs per LD block and dataset 
# Use this to determine number of gene eQTLs shared across disease traits (within an eQTL dataset)
n_shared_eqtl <- 
  qtl_tidy_filtered %>% 
  dplyr::filter(str_detect(phen2, "ENSG")) %>% 
  dplyr::distinct(ld_block, eqtl_dataset, phen1, phen2) %>% 
  dplyr::count(ld_block, eqtl_dataset, phen2, name = "n_disease") %>% 
  dplyr::count(ld_block, n_disease, name = "n_genes") %>% 
  dplyr::mutate(
    shared_eqtl =
      case_when(
        n_disease == 1 ~ "FALSE",
        n_disease > 1 ~ "TRUE"
      )
  )

# Pivot results
qtl_bivar_long <- 
  qtl_tidy$bivar %>% 
  qdapTools::list_df2df(col1 = "window_size") %>% 
  tidyr::pivot_longer( 
    cols = -c("window_size", "ld_block", "eqtl_dataset", "gene_locus", "gene_name", "phen1", "phen2"),
    names_to = "feature",
    values_to = "value"
  ) %>% 
  tidyr::pivot_wider(names_from = "window_size") %>% 
  dplyr::mutate(
    across(
      .cols = contains("window"),
      ~ case_when(
        feature == "p" ~ -log10(.x),
        TRUE ~ .x
      )
    )
  ) %>%
  dplyr::mutate(
    feature = 
      case_when(
        feature == "p" ~ "-log10(p)",
        TRUE ~ feature
      ), 
    phenotype_corr =
      case_when(
        str_detect(phen2, "ENSG") ~ "gwas-eqtl",
        TRUE ~ "gwas-gwas"
      ),
    doe_100000 =
      case_when(
        feature == "rho" & window_100000 < 0 ~ "-",
        feature == "rho" & window_100000 > 0 ~ "+"
      ),
    doe_50000 =
      case_when(
        feature == "rho" & window_50000 < 0 ~ "-",
        feature == "rho" & window_50000 > 0 ~ "+" 
      ),
    doe_same =
      case_when(
        doe_100000 == doe_50000 ~ TRUE,
        doe_100000 != doe_50000 ~ FALSE
      )
  )

# Create granges of ld blocks
loci_gr <-
  loci %>%
  dplyr::rename(locus = LOC) %>% 
  dplyr::filter(locus %in% qtl_tidy_filtered$ld_block) %>% 
  GenomicRanges::makeGRangesFromDataFrame(
    .,
    keep.extra.columns = TRUE,
    ignore.strand = TRUE,
    seqinfo = NULL,
    seqnames.field = "CHR",
    start.field = "START",
    end.field = "STOP"
  )

# Tidy pcorr
pcorr <-
  pcorr %>% 
  lapply(., function(list){
    
    list %>%
      qdapTools::list_df2df(col1 = "list_name") %>%
      dplyr::mutate(
        eqtl_dataset =
          case_when(
            stringr::str_detect(phen1, "PSYCH") |
              stringr::str_detect(phen2, "PSYCH") |
              stringr::str_detect(z, "PSYCH") ~ "PSYCHENCODE",
            stringr::str_detect(phen1, "EQTLGEN") |
              stringr::str_detect(phen2, "EQTLGEN") |
              stringr::str_detect(z, "EQTLGEN") ~ "EQTLGEN"
          )
      ) %>%
      dplyr::mutate(
        across(
          .cols = phen1:z,
          ~ case_when(
            !str_detect(.x, "ENSG") ~ .x %>%
              str_replace_all("[:digit:]", "") %>%
              str_remove("\\..*"),
            str_detect(.x, "ENSG") ~ str_remove(.x, ".*_")
          )
        )
      )
    
  }) %>%
  qdapTools::list_df2df(col1 = "list_name_2") %>%
  dplyr::select(
    eqtl_dataset,
    gene_locus,
    gene_name,
    everything(),
    -contains("list_name")
  ) %>%
  as_tibble()

5.3 Main figure

margin <- ggplot2::margin(t = 1, r = 1, b = 1, l = 5, unit = "mm")

plots <- vector(mode = "list")

plots[[1]] <-
  n_genes %>% 
  ggplot(
    aes(
      x = as.factor(ld_block),
      y = n,
      fill = signif
    )
  ) +
  geom_col(colour = "black") +
  geom_hline(yintercept = 1, linetype = "dashed") +
  labs(
    x = "LD block",
    y = "Number of eQTL genes",
    fill = "Significant eQTL-GWAS\nlocal rg?"
  ) +
  scale_fill_grey(breaks=c("TRUE", "FALSE")) +
  guides(
    fill = guide_legend(
      nrow = 1,
      title.position = "top"
    )
  ) +
  theme_rhr +
  theme(
    legend.position = "bottom",
    plot.margin = margin
  )

plots[[2]] <- 
  n_shared_eqtl %>% 
  ggplot(
    aes(
      x = as.factor(ld_block),
      y = n_genes,
      fill = shared_eqtl
    )
  ) +
  geom_col(
    colour = "black"
  ) +
  geom_hline(yintercept = 1, linetype = "dashed") +
  labs(
    x = "LD block",
    y = "Number of eQTL genes",
    fill = "eQTL shared across\nmultiple disease traits?"
  ) +
  scale_fill_grey(breaks=c("TRUE", "FALSE")) +
  guides(
    fill = guide_legend(
      nrow = 1,
      title.position = "top"
    )
  ) +
  theme_rhr  +
  theme(
    legend.position = "bottom",
    plot.margin = margin
  )

ld_blocks <- 
  c(1273, 1719, 2351)

for(i in 1:length(ld_blocks)){
  
  plots[[i+2]] <-
   qtl_to_plot$window_100000 %>% 
      dplyr::filter(ld_block == ld_blocks[i], fdr < 0.05) %>% 
      ggplot(
        aes(
          x = 
            fct_reorder(
              gene_name,
              start
            ),
          y = phen1
        )
      ) +
      geom_point(aes(fill = fill_rho), colour = "black", shape = 22, size = 8) +
      geom_text(aes(label = fill_rho), size = 2) +
      # coord_fixed() +
      facet_grid(rows = vars(eqtl_dataset)) +
      labs(x = str_c("eQTL gene, LD block ", ld_blocks[i]), y = "Disease trait", fill = "Local rg (rho)") +
      scale_fill_distiller(
        palette = "RdYlBu",
        limits = c(-1, 1), breaks = c(-1,0,1), na.value = "grey"
        ) +
      theme_rhr +
      theme(
        # plot.margin = ggplot2::margin(t = 2, r = 0, b = 0, l = 0, unit = "mm"),
        legend.position = "none"
      )
  
}

plots[[6]] <- 
  plot_qtl_edge_diagram(
    bivar_corr_qtl = 
      qtl_list[["locus_1273"]] %>% 
      dplyr::filter(gene_name %in% c("CLU", "SCARA5", "PBK")),
    phen = fct_disease,
    geom_node_size = 2,
    base_size = 8,
    margin = margin,
    seed = 89
  )

fig <-
  cowplot::plot_grid(
    cowplot::plot_grid(
      cowplot::plot_grid(
        cowplot::plot_grid(
          plotlist = plots[c(1:2)],
          nrow = 1,
          labels = c("a", "b")
        ),
        plots[[3]],
        rel_heights = c(1.5,1),
        ncol = 1,
        labels = c("", "d")
      ),
      ncol = 2,
      plots[[4]],
      rel_widths = c(2,1),
      labels = c("", "c")
    ),
    ggpubr::ggarrange(
      plots[[6]]$`CLU:EQTLGEN`,
      plots[[6]]$`SCARA5:EQTLGEN`,
      plots[[6]]$`PBK:EQTLGEN`,
      nrow = 1, ncol = 3,
      common.legend = TRUE,
      legend = "none",
      labels = c("e","")
    ),
    plots[[5]] + theme(legend.position = "bottom"),
    ncol = 1,
    rel_heights = c(1.75,0.75,1.5),
    labels = c("", "", "f")
  )

fig

ggsave(
  fig,
  path = out_dir, 
  filename = "fig_4_eqtl.pdf",
  device = "pdf", 
  width = 150, 
  height = 240, 
  dpi = 300, 
  units = "mm"
)

5.4 Supplementary figure(s)

supp_fig <- vector(mode = "list", length = 3)

ld_blocks <- unique(qtl_tidy$bivar$window_100000$ld_block)

plots <- 
  setNames(
    object = vector(mode = "list", length = length(ld_blocks)),
    nm = str_c("ld", ld_blocks)
  )

for(block in ld_blocks){
  
  plots[[str_c("ld",block)]] <- 
    qtl_to_plot$window_100000 %>% 
    dplyr::filter(ld_block == block) %>% 
    ggplot(
      aes(
        x = 
          fct_reorder(
            gene_name,
            start
          ),
        y = phen1
      )
    ) +
    geom_point(aes(fill = fill_rho), colour = "black", shape = 22, size = 5) +
    # coord_fixed() +
    geom_text(aes(label=sig), size = 3, vjust = 0.75) +
    geom_text(aes(label=nom_sig), size = 5, vjust = 0.1) +
    facet_grid(rows = vars(eqtl_dataset)) +
    labs(x = str_c("eQTL gene, LD block ", block), y = "Disease trait", fill = "Local rg (rho)") +
    scale_fill_distiller(
      palette = "RdYlBu",
      limits = c(-1, 1), breaks = c(-1,0,1), na.value = "grey") +
    guides(colour = guide_colourbar(title = "Local rg")) +
    theme_rhr +
    theme(
      plot.margin = ggplot2::margin(t = 2, r = 2, b = 2, l = 2, unit = "mm")
    )
  
}

supp_fig[[1]] <-
  ggpubr::ggarrange(
    ggpubr::ggarrange(
      plotlist = 
        plots[c(1:2)]  %>% 
        lapply(., function(plot){
          
          plot +
            theme(legend.position = "none")
          
        }),
      align = "h",
      labels = letters[1:2]
    ),
    ggpubr::ggarrange(
      plotlist = 
        plots[c(3:4)]  %>% 
        lapply(., function(plot){
          
          plot +
            theme(legend.position = "none")
          
        }),
      align = "h",
      labels = letters[3:4]
    ),
    plots[[5]],
    ncol = 1,
    heights = c(1,1.5,1.25),
    common.legend = T,
    labels = c("", "", "e")
  )

supp_fig[[1]]

supp_fig[[2]] <-
  qtl_tidy_filtered %>% 
  dplyr::mutate(
    phenotype_corr =
      case_when(
        str_detect(phen2, "ENSG") ~ "gwas-eqtl",
        TRUE ~ "gwas-gwas"
      )
  ) %>% 
  # dplyr::filter(phenotype_corr == "gwas-eqtl") %>% 
  ggplot(
    aes(
      x = phenotype_corr,
      y = r2
      )
    ) +
  geom_boxplot(fill = "#d9d9d9", outlier.alpha = 0) +
  ggbeeswarm::geom_beeswarm(
    # aes(colour = fct_relevel(phen1, levels = fct_disease)), 
    alpha = 0.5, 
    # size = 2
    ) +
  # scale_colour_manual(values = RColorBrewer::brewer.pal(n = 6, name = "BrBG")) +
  labs(
    x = "Trait type in pair",
    y = "Explained variance (r2)",
    # colour = "Trait"
  ) +
  theme_rhr

supp_fig[[2]]

overlap <- readRDS(here::here("results", "04_eqtl_univar_bivar", "fdr_overlap.rds"))

plots <- vector(mode = "list", length = 2)

plots[[1]] <- 
  qtl_tidy$bivar %>% 
  qdapTools::list_df2df(col1 = "window_size") %>% 
  tidyr::pivot_longer( 
    cols = -c("window_size", "ld_block", "eqtl_dataset", "gene_locus", "gene_name", "phen1", "phen2"),
    names_to = "feature",
    values_to = "value"
  ) %>% 
  dplyr::mutate(
    window_size =
      window_size %>% 
      readr::parse_number() %>% 
      format(scientific = FALSE) %>% 
      as.factor()
  ) %>% 
  dplyr::filter(
    feature %in% c("fdr"),
    value < 0.05
  ) %>% 
  dplyr::count(window_size, feature, name = "n_total") %>% 
  dplyr::mutate(
    unique =
      case_when(
        feature == "fdr" ~ n_total - (overlap %>% dplyr::filter(fdr_100000 < 0.05 & fdr_50000 < 0.05) %>% nrow())
      ),
    shared =
      n_total-unique
  ) %>% 
  tidyr::pivot_longer(
    cols = unique:shared,
    names_to = "sharing",
    values_to = "n"
  ) %>% 
  ggplot(
    aes(
      x = window_size,
      y = n,
      fill = sharing
    )
  ) + 
  geom_col(colour = "black") +
  scale_fill_grey() +
  guides(
    fill = guide_legend(
      nrow = 2,
      title.position = "top"
    )
  ) +
  labs(
    x = "Window size (kb)",
    y = "Number of significant bivariate local rg",
    fill = "Sharing of local rgs"
  ) +
  theme_rhr +
  theme(legend.position = "bottom")
  
  
plots[[2]] <- 
  qtl_bivar_long %>% 
  dplyr::filter(
    !feature %in%
      c("chr", "start", "stop", "fdr", "bonferroni", "n_pcs", "n_snps", "r2"),
    !str_detect(feature, "upper"),
    !str_detect(feature, "lower")
  ) %>%
  ggplot(
    aes(
      x = window_50000,
      y = window_100000
    )
  ) + 
  geom_point(
    aes(
      fill = doe_same
    ),
    shape = 21,
    colour = "black",
    alpha = 0.4
  ) + 
  geom_smooth(
    method = "lm",
    formula = "y~x",
    level = 0.99,
    colour = "black",
    fill = "grey"
  ) +
  ggpubr::stat_cor(
    method = "pearson",
    cor.coef.name = "R",
    size = 3
  ) +
  geom_abline(
    intercept = 0,
    linetype = "dashed",
    colour = "#EE442F"
  ) +
  facet_wrap(
    vars(phenotype_corr, feature),
    scales = "free"
  ) +
  labs(
    x = "50-kb window",
    y = "100-kb window",
    fill = "Same direction of effect?"
  ) +
  scale_fill_discrete(na.translate = F) +
  theme_rhr +
  guides(
    fill = guide_legend(
      nrow = 2,
      title.position = "top"
    )
  ) +
  theme(
    axis.text.x = element_text(angle = 0, hjust = 0.5),
    legend.position = "bottom"
    )

supp_fig[[3]] <-
  cowplot::plot_grid(
    plotlist = plots, 
    nrow = 1, 
    axis = "bt", 
    align = "h",
    rel_widths = c(1,3),
    labels = letters[1:length(plots)]
  )

supp_fig[[3]]

ggsave(
  supp_fig[[1]],
  path = out_dir, 
  filename = "suppfig_5_eqtl_all.png",
  device = "png", 
  width = 150, 
  height = 230, 
  dpi = 300, 
  units = "mm"
)

ggsave(
  supp_fig[[2]],
  path = out_dir, 
  filename = "suppfig_6_r2.png",
  device = "png", 
  width = 150, 
  height = 150, 
  dpi = 300, 
  units = "mm"
)

ggsave(
  supp_fig[[3]],
  path = out_dir, 
  filename = "suppfig_7_windowsize_analysis.png",
  device = "png", 
  width = 90, 
  height = 90, 
  dpi = 300, 
  units = "mm"
)

5.5 Supplementary table(s)

ld_blocks <- 
  qtl_tidy$bivar$window_100000 %>% .[["ld_block"]] %>% unique() %>% str_c("ld", .)

# Full results
results_by_block <- 
  qtl_tidy %>% 
  lapply(., function(list_of_df){
    
    tmp <-
      list_of_df %>% 
      lapply(., function(df){
        
        
        setNames(
          object =
            df %>%
            dplyr::group_split(ld_block),
          nm = 
            df[["ld_block"]] %>% 
            unique() %>% 
            sort() %>% 
            str_c("ld", .)
        )
        
      })
    
  })

col_descriptions <-
  tibble(
    column = colnames(results_by_block$univ$window_100000$ld681),
    description = 
      c(
        "LD block ID",
        "eQTL dataset from which gene eQTLs are derived",
        "Ensembl ID for gene for which eQTLs were tested in genic region",
        "HGNC symbol for gene for which eQTLs were tested in genic region",
        "Genic region chromosome",
        "Genic region start base pair",
        "Genic region end base pair",
        "The number of SNPs within the genic region",
        "The number of PCs retained within the genic region",
        "Analysed phenotype",
        "Observed local heritability",
        "P-value from the univariate test (F-test for continuous, Chi-sq for binary)"
      )
  ) %>% 
  dplyr::mutate(sheet = "univariate") %>% 
  dplyr::bind_rows(
    tibble(
      column = colnames(results_by_block$bivar$window_100000$ld681),
      description = 
        c(
          "LD block ID",
          "eQTL dataset from which gene eQTLs are derived",
          "Ensembl ID for gene for which eQTLs were tested in genic region",
          "HGNC symbol for gene for which eQTLs were tested in genic region",
          "Genic region chromosome",
          "Genic region start base pair",
          "Genic region end base pair",
          "The number of SNPs within the genic region",
          "The number of PCs retained within the genic region",
          "Phenotype 1",
          "Phenotype 2. Gene expression traits (i.e. gene eQTLs) are denoted by their ensembl gene id",
          "Standardised coefficient for the local genetic correlation",
          "Lower 95% confidence estimate for rho",
          "Upper 95% confidence estimate for rho",
          "Equivalent of taking the square of rho. Denotes the proportion of variance in genetic signal for phen1 explained by phen2 (and vice versa)",
          "Lower 95% confidence estimate for r2",
          "Upper 95% confidence estimate for r2",
          "Simulation p-values for the local genetic correlation",
          "FDR-corrected simulation p-values",
          "Bonferroni-corrected simulation p-values"
        )
    ) %>% 
      dplyr::mutate(sheet = "bivariate")
  ) %>% 
  dplyr::select(sheet, column, description)

for(window in names(qtl_tidy$bivar)){
  
  # Create ld block figures ----
  qtl_list <- 
    setNames(
      qtl_tidy$bivar[[window]] %>% 
        dplyr::filter(fdr < 0.05) %>% 
        dplyr::group_split(ld_block),
      nm = 
        str_c(ld_blocks)
    )
  
  qtl_genes <-
    qtl_tidy$bivar[[window]] %>%
    dplyr::filter(fdr < 0.05) %>% 
    dplyr::group_by(eqtl_dataset, gene_locus) %>%
    dplyr::filter(str_detect(phen2, "ENSG")) %>% 
    .[["gene_locus"]] %>% 
    unique()
  
  fig_list <- vector(mode = "list", length = length(ld_blocks))
  
  for(block in ld_blocks){
    
    locus_plot <- 
      plot_locus(
        locus_gr = loci_gr[loci_gr$locus == str_remove(block, "ld")], 
        ref = ref,
        highlight_gene = qtl_genes,
        highlight_gene_label = "QTL - local rg?",
        window = str_remove(window, "window_") %>% as.numeric()
      )
    
    edge_plot <- 
      plot_qtl_edge_diagram(
        bivar_corr_qtl = qtl_list[[block]],
        phen = fct_disease,
        seed = 89
      )
    
    if(length(edge_plot)/4 > 1){
      
      height <- c(1,2.5)
      
    } else{
      
      height <- c(1,0.75)
    }
    
    fig_list[[which(ld_blocks == block)]] <- 
      ggpubr::ggarrange(
        locus_plot,
        ggpubr::ggarrange(
          plotlist = edge_plot,
          ncol = 4,
          nrow = ceiling(length(edge_plot)/4), 
          common.legend = TRUE,
          legend = "top"
        ),
        ncol = 1,
        labels = letters[1:2],
        heights = height
      )
    
    names(fig_list)[which(ld_blocks == block)] <- block
  }
  
  # Save to workbook ----
  wb <- openxlsx::createWorkbook()
  
  # Write column descriptions ----
  openxlsx::addWorksheet(wb, sheetName = "column_descriptions")
  
  openxlsx::writeData(
    wb,
    sheet = "column_descriptions",
    x = col_descriptions,
    row.names = FALSE,
    headerStyle = openxlsx::createStyle(textDecoration = "BOLD")
  )
  
  openxlsx::freezePane(
    wb,
    sheet = "column_descriptions",
    firstRow = TRUE
  )
  
  for(block in ld_blocks){
    
    # Write results ----
    for(test in names(results_by_block)){
      
      sheetname <- str_c(block, "_", test)
      
      openxlsx::addWorksheet(wb, sheetName = sheetname)
      
      openxlsx::writeData(
        wb,
        sheet = sheetname,
        x = results_by_block[[test]][[window]][[block]],
        row.names = FALSE,
        headerStyle = openxlsx::createStyle(textDecoration = "BOLD")
      )
      
      openxlsx::freezePane(
        wb,
        sheet = sheetname,
        firstRow = TRUE
      )
      
    }
    
    # Add figure ----
    print(fig_list[[block]])
    
    if(block == "ld2351"){
      
      height <- 45
      
    } else{
      
      height <- 20
    }
    
    openxlsx::insertPlot(
      wb,
      sheet = str_c(block, "_bivar"),
      width = 20,
      height = height,
      units = "cm",
      startRow = 2,
      startCol = 22
    )
    
  }
  
  openxlsx::saveWorkbook(
    wb,
    file = file.path(out_dir, str_c("SupplementaryTable_lava_local_eqtl_", window,".xlsx")),
    overwrite = TRUE
  ) 
  
}
xlsx <- 
  setNames(
    vector(mode = "list", length = 2),
    c("column_descriptions", "results")
  )

xlsx[[2]] <-
  pcorr

xlsx[[1]] <-
  tibble(
    column = colnames(pcorr),
    description = 
      c("eQTL dataset from which gene eQTLs are derived",
        "Ensembl ID for gene for which eQTLs were tested in genic region",
        "HGNC symbol for gene for which eQTLs were tested in genic region",
        "Genic region chromosome",
        "Genic region start base pair",
        "Genic region end base pair",
        "The number of SNPs within the genic region",
        "The number of PCs retained within the genic region",
        "Phenotype 1. Gene expression traits (i.e. gene eQTLs) are denoted by their ensembl gene id",
        "Phenotype 2. Gene expression traits (i.e. gene eQTLs) are denoted by their ensembl gene id",
        "Phenotype which the genetic correlation between phenotype 1 and 2 was conditioned on",
        "The proportion of genetic signal in phenotype 1 explained by z",
        "The proportion of genetic signal in phenotype 2 explained by z",
        "The partial correlation between phen1 and phen2 conditioned on z",
        "Lower bound of 95% confidence interval for partial correlation",
        "Upper bound of 95% confidence interval for partial correlation",
        "Simulation p-values for the partial genetic correlation"
      )
  )

openxlsx::write.xlsx(
  xlsx,
  file = file.path(out_dir, "SupplementaryTable9_pcorr.xlsx"),
  row.names = FALSE,
  headerStyle = openxlsx::createStyle(textDecoration = "BOLD"),
  firstRow = TRUE,
  append = TRUE,
  overwrite = TRUE
)



6 Session info

Show/hide

## ─ Session info ───────────────────────────────────────────────────────────────────────────────────────────────────────
##  setting  value                       
##  version  R version 4.0.5 (2021-03-31)
##  os       Ubuntu 16.04.7 LTS          
##  system   x86_64, linux-gnu           
##  ui       X11                         
##  language (EN)                        
##  collate  en_GB.UTF-8                 
##  ctype    en_GB.UTF-8                 
##  tz       Europe/London               
##  date     2023-04-23                  
## 
## ─ Packages ───────────────────────────────────────────────────────────────────────────────────────────────────────────
##  ! package              * version  date       lib source         
##  P abind                  1.4-5    2016-07-21 [?] CRAN (R 4.0.5) 
##  P assertthat             0.2.1    2019-03-21 [?] CRAN (R 4.0.5) 
##  P backports              1.3.0    2021-10-27 [?] CRAN (R 4.0.5) 
##  P beeswarm               0.4.0    2021-06-01 [?] CRAN (R 4.0.5) 
##  P Biobase                2.50.0   2020-10-27 [?] Bioconductor   
##  P BiocGenerics         * 0.36.1   2021-04-16 [?] Bioconductor   
##  P BiocParallel           1.24.1   2020-11-06 [?] Bioconductor   
##  P Biostrings             2.58.0   2020-10-27 [?] Bioconductor   
##  P bit                    4.0.4    2020-08-04 [?] CRAN (R 4.0.5) 
##  P bit64                  4.0.5    2020-08-30 [?] CRAN (R 4.0.5) 
##  P bitops                 1.0-7    2021-04-24 [?] CRAN (R 4.0.5) 
##  P bookdown               0.22     2021-04-22 [?] CRAN (R 4.0.5) 
##  P broom                  0.7.10   2021-10-31 [?] CRAN (R 4.0.5) 
##  P bslib                  0.3.1    2021-10-06 [?] CRAN (R 4.0.5) 
##  P car                    3.0-11   2021-06-27 [?] CRAN (R 4.0.5) 
##  P carData                3.0-4    2020-05-22 [?] CRAN (R 4.0.5) 
##  P cellranger             1.1.0    2016-07-27 [?] CRAN (R 4.0.5) 
##  P chron                  2.3-56   2020-08-18 [?] CRAN (R 4.0.5) 
##  P circlize             * 0.4.13   2021-06-09 [?] CRAN (R 4.0.5) 
##  P cli                    3.1.0    2021-10-27 [?] CRAN (R 4.0.5) 
##  P colorspace             2.0-2    2021-06-24 [?] CRAN (R 4.0.5) 
##  P cowplot              * 1.1.1    2020-12-30 [?] CRAN (R 4.0.5) 
##  P crayon                 1.4.2    2021-10-29 [?] CRAN (R 4.0.5) 
##  P curl                   4.3.2    2021-06-23 [?] CRAN (R 4.0.5) 
##  P data.table             1.14.2   2021-09-27 [?] CRAN (R 4.0.5) 
##  P DBI                    1.1.1    2021-01-15 [?] CRAN (R 4.0.5) 
##  P dbplyr                 2.1.1    2021-04-06 [?] CRAN (R 4.0.5) 
##  P DelayedArray           0.16.3   2021-03-24 [?] Bioconductor   
##  P digest                 0.6.29   2021-12-01 [?] CRAN (R 4.0.5) 
##  P dplyr                * 1.0.7    2021-06-18 [?] CRAN (R 4.0.5) 
##  P ellipsis               0.3.2    2021-04-29 [?] CRAN (R 4.0.5) 
##  P evaluate               0.14     2019-05-28 [?] CRAN (R 4.0.5) 
##  P fansi                  0.5.0    2021-05-25 [?] CRAN (R 4.0.5) 
##  P farver                 2.1.0    2021-02-28 [?] CRAN (R 4.0.5) 
##  P fastmap                1.1.0    2021-01-25 [?] CRAN (R 4.0.5) 
##  P forcats              * 0.5.1    2021-01-27 [?] CRAN (R 4.0.5) 
##  P foreign                0.8-81   2020-12-22 [?] CRAN (R 4.0.5) 
##  P fs                     1.5.1    2021-11-30 [?] CRAN (R 4.0.5) 
##  P generics               0.1.1    2021-10-25 [?] CRAN (R 4.0.5) 
##  P GenomeInfoDb         * 1.26.7   2021-04-08 [?] Bioconductor   
##  P GenomeInfoDbData       1.2.4    2021-07-03 [?] Bioconductor   
##  P GenomicAlignments      1.26.0   2020-10-27 [?] Bioconductor   
##  P GenomicRanges        * 1.42.0   2020-10-27 [?] Bioconductor   
##  P ggbeeswarm             0.6.0    2017-08-07 [?] CRAN (R 4.0.5) 
##  P ggforce                0.3.3    2021-03-05 [?] CRAN (R 4.0.5) 
##  P ggplot2              * 3.3.5    2021-06-25 [?] CRAN (R 4.0.5) 
##  P ggpubr                 0.4.0    2020-06-27 [?] CRAN (R 4.0.5) 
##  P ggraph               * 2.0.5    2021-02-23 [?] CRAN (R 4.0.5) 
##  P ggrepel                0.9.1    2021-01-15 [?] CRAN (R 4.0.5) 
##  P ggsignif               0.6.3    2021-09-09 [?] CRAN (R 4.0.5) 
##  P GlobalOptions          0.1.2    2020-06-10 [?] CRAN (R 4.0.5) 
##  P glue                   1.5.1    2021-11-30 [?] CRAN (R 4.0.5) 
##  P graphlayouts           0.7.1    2020-10-26 [?] CRAN (R 4.0.5) 
##  P gridExtra              2.3      2017-09-09 [?] CRAN (R 4.0.5) 
##  P gtable                 0.3.0    2019-03-25 [?] CRAN (R 4.0.5) 
##  P haven                  2.4.3    2021-08-04 [?] CRAN (R 4.0.5) 
##  P here                   1.0.1    2020-12-13 [?] CRAN (R 4.0.5) 
##  P highr                  0.9      2021-04-16 [?] CRAN (R 4.0.5) 
##  P hms                    1.1.1    2021-09-26 [?] CRAN (R 4.0.5) 
##  P htmltools              0.5.2    2021-08-25 [?] CRAN (R 4.0.5) 
##  P httr                   1.4.2    2020-07-20 [?] CRAN (R 4.0.5) 
##  P igraph                 1.2.7    2021-10-15 [?] CRAN (R 4.0.5) 
##  P IRanges              * 2.24.1   2020-12-12 [?] Bioconductor   
##  P jquerylib              0.1.4    2021-04-26 [?] CRAN (R 4.0.5) 
##  P jsonlite               1.7.2    2020-12-09 [?] CRAN (R 4.0.5) 
##  P knitr                  1.36     2021-09-29 [?] CRAN (R 4.0.5) 
##  P labeling               0.4.2    2020-10-20 [?] CRAN (R 4.0.5) 
##  P lattice                0.20-44  2021-05-02 [?] CRAN (R 4.0.5) 
##  P LAVA                   0.0.6    2021-09-01 [?] xgit (@7be3421)
##  P lifecycle              1.0.1    2021-09-24 [?] CRAN (R 4.0.5) 
##  P lubridate              1.8.0    2021-10-07 [?] CRAN (R 4.0.5) 
##  P magrittr               2.0.1    2020-11-17 [?] CRAN (R 4.0.5) 
##  P MASS                   7.3-54   2021-05-03 [?] CRAN (R 4.0.5) 
##  P Matrix                 1.3-4    2021-06-01 [?] CRAN (R 4.0.5) 
##  P MatrixGenerics         1.2.1    2021-01-30 [?] Bioconductor   
##  P matrixStats            0.61.0   2021-09-17 [?] CRAN (R 4.0.5) 
##  P mgcv                   1.8-36   2021-06-01 [?] CRAN (R 4.0.5) 
##  P modelr                 0.1.8    2020-05-19 [?] CRAN (R 4.0.5) 
##  P munsell                0.5.0    2018-06-12 [?] CRAN (R 4.0.5) 
##  P nlme                   3.1-152  2021-02-04 [?] CRAN (R 4.0.5) 
##  P openxlsx               4.2.4    2021-06-16 [?] CRAN (R 4.0.5) 
##  P pillar                 1.6.4    2021-10-18 [?] CRAN (R 4.0.5) 
##  P pkgconfig              2.0.3    2019-09-22 [?] CRAN (R 4.0.5) 
##  P polyclip               1.10-0   2019-03-14 [?] CRAN (R 4.0.5) 
##  P purrr                * 0.3.4    2020-04-17 [?] CRAN (R 4.0.5) 
##  P qdapTools              1.3.5    2020-04-17 [?] CRAN (R 4.0.5) 
##  P R6                     2.5.1    2021-08-19 [?] CRAN (R 4.0.5) 
##  P RColorBrewer           1.1-2    2014-12-07 [?] CRAN (R 4.0.5) 
##  P Rcpp                   1.0.7    2021-07-07 [?] CRAN (R 4.0.5) 
##  P RCurl                  1.98-1.5 2021-09-17 [?] CRAN (R 4.0.5) 
##  P readr                * 2.0.2    2021-09-27 [?] CRAN (R 4.0.5) 
##  P readxl                 1.3.1    2019-03-13 [?] CRAN (R 4.0.5) 
##  P reprex                 2.0.0    2021-04-02 [?] CRAN (R 4.0.5) 
##  P rio                    0.5.27   2021-06-21 [?] CRAN (R 4.0.5) 
##  P rlang                  0.4.12   2021-10-18 [?] CRAN (R 4.0.5) 
##  P rmarkdown              2.11     2021-09-14 [?] CRAN (R 4.0.5) 
##  P rprojroot              2.0.2    2020-11-15 [?] CRAN (R 4.0.5) 
##  P Rsamtools              2.6.0    2020-10-27 [?] Bioconductor   
##  P rstatix                0.7.0    2021-02-13 [?] CRAN (R 4.0.5) 
##  P rstudioapi             0.13     2020-11-12 [?] CRAN (R 4.0.5) 
##  P rtracklayer          * 1.50.0   2020-10-27 [?] Bioconductor   
##  P rvest                  1.0.1    2021-07-26 [?] CRAN (R 4.0.5) 
##  P S4Vectors            * 0.28.1   2020-12-09 [?] Bioconductor   
##  P sass                   0.4.0    2021-05-12 [?] CRAN (R 4.0.5) 
##  P scales                 1.1.1    2020-05-11 [?] CRAN (R 4.0.5) 
##  P sessioninfo          * 1.1.1    2018-11-05 [?] CRAN (R 4.0.5) 
##  P shape                  1.4.6    2021-05-19 [?] CRAN (R 4.0.5) 
##  P stringi                1.7.6    2021-11-29 [?] CRAN (R 4.0.5) 
##  P stringr              * 1.4.0    2019-02-10 [?] CRAN (R 4.0.5) 
##  P SummarizedExperiment   1.20.0   2020-10-27 [?] Bioconductor   
##  P tibble               * 3.1.6    2021-11-07 [?] CRAN (R 4.0.5) 
##  P tidygraph              1.2.0    2020-05-12 [?] CRAN (R 4.0.5) 
##  P tidyr                * 1.1.4    2021-09-27 [?] CRAN (R 4.0.5) 
##  P tidyselect             1.1.1    2021-04-30 [?] CRAN (R 4.0.5) 
##  P tidyverse            * 1.3.1    2021-04-15 [?] CRAN (R 4.0.5) 
##  P tweenr                 1.0.2    2021-03-23 [?] CRAN (R 4.0.5) 
##  P tzdb                   0.2.0    2021-10-27 [?] CRAN (R 4.0.5) 
##  P utf8                   1.2.2    2021-07-24 [?] CRAN (R 4.0.5) 
##  P vctrs                  0.3.8    2021-04-29 [?] CRAN (R 4.0.5) 
##  P vipor                  0.4.5    2017-03-22 [?] CRAN (R 4.0.5) 
##  P viridis                0.6.2    2021-10-13 [?] CRAN (R 4.0.5) 
##  P viridisLite            0.4.0    2021-04-13 [?] CRAN (R 4.0.5) 
##  P vroom                  1.5.5    2021-09-14 [?] CRAN (R 4.0.5) 
##  P withr                  2.4.3    2021-11-30 [?] CRAN (R 4.0.5) 
##  P xfun                   0.27     2021-10-18 [?] CRAN (R 4.0.5) 
##  P XML                    3.99-0.8 2021-09-17 [?] CRAN (R 4.0.5) 
##  P xml2                   1.3.2    2020-04-23 [?] CRAN (R 4.0.5) 
##  P XVector                0.30.0   2020-10-27 [?] Bioconductor   
##  P yaml                   2.2.1    2020-02-01 [?] CRAN (R 4.0.5) 
##  P zip                    2.2.0    2021-05-31 [?] CRAN (R 4.0.5) 
##  P zlibbioc               1.36.0   2020-10-27 [?] Bioconductor   
## 
## [1] /home/rreynolds/misc_projects/neurodegen-psych-local-corr/renv/library/R-4.0/x86_64-pc-linux-gnu
## [2] /opt/R/4.0.5/lib/R/library
## 
##  P ── Loaded and on-disk path mismatch.