Last updated: 2020-03-17

Checks: 7 0

Knit directory: m6AQTL_reproducibleDocument/

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


Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

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(20200317) 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 version displayed above was the version of the Git repository at the time these results were generated.

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:    .Rproj.user/

Untracked files:
    Untracked:  analysis/joint_analysis_effectsizes_scatterplots_cor.Rmd

Unstaged changes:
    Modified:   analysis/Joint_analysis_of_QTLs.Rmd
    Modified:   analysis/index.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 R Markdown and HTML files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view them.

File Version Author Date Message
Rmd 6dfae66 kevinlkx 2020-03-17 wflow_publish(“analysis/pi1_QTLsharing_jointLCLs.Rmd”)

  • Estimate sharing (π1) at different p-value cutoffs of m6A-QTLs (lead SNPs)
  • Compare with random SNPs
  • source: m6AQTL_workflowr/analysis/pi1_QTLsharing_jointLCLs_m6AQTL_logOR.adjusted_leadSNPs_random_combinedtest_noTE.Rmd

Functions

# options(scipen = 999)
library(ggplot2)
library(qvalue)
library(metap)

bootstrap_pi1_ci <- function(pvalues, N = 1000, ci_percent = 0.8, pi0.method="bootstrap"){
  
  lambda <- seq(0.05, 0.9, 0.05)
  
  pi1_bootstrap <- function(pvalues, pi0.method){
    p_samples <- sample(pvalues, replace = T)
    pi1_samples <- (1 - pi0est(p_samples, lambda = lambda, pi0.method = pi0.method)$pi0)
    return(pi1_samples)
  }
  
  pvalues <- pvalues[!is.na(pvalues)]
  
  pi1_hat <- (1 - pi0est(pvalues, lambda = lambda, pi0.method = pi0.method)$pi0)

  if(N > 0){
    pi1_samples <- replicate(N, pi1_bootstrap(pvalues, pi0.method))
    ci <- quantile(pi1_samples, c(0.5 - ci_percent/2, 0.5 + ci_percent/2))
  }else{
    ci <- rep(pi1_hat,2)
  }
  
  result <- c(ci[1], pi1_hat, ci[2])
  names(result) <- c("ci_min", "pi1", "ci_max")
  return(result)
}


## Fisher's method of combining pvalues from independent tests
combine_p_Fisher <- function(pvalues){
  pvalues <- na.omit(pvalues)
  if(length(pvalues) == 0){
    pvalue_fisher <- NA
  }else if(length(pvalues) == 1){
    pvalue_fisher <- na.omit(pvalues)
  }else{
    pvalue_fisher <- metap::sumlog(pvalues)$p # using the Fisher's method in metap package
    # actual calculation below: 
    # chisq_stat <- -2*sum(log(pvalues))
    # k <- length(pvalues)
    # pvalue_fisher <- pchisq(chisq_stat, df = 2*k, lower.tail = FALSE)
  }
  return(pvalue_fisher)
}

combine_sumsq_zscores <- function(pvalues){
  k <- length(pvalues)
  zscore_abs <- p2z(pvalues)
  pvalue_combined <- pchisq(sum(zscore_abs^2), df = k, lower.tail = FALSE)
  return(pvalue_combined)
}

combine_pvalue_Fisher <- function(pvalue_phenotypes){
  
  p_EDA <- apply(pvalue_phenotypes[,c("Expression", "Decay", "APA")], 1, combine_p_Fisher )
  p_all <- apply(pvalue_phenotypes[,c("Expression", "Ribosome", "Protein", "Decay", "APA")], 1, combine_p_Fisher )
  
  pvalue_phenotypes_combined <- data.frame(pvalue_phenotypes,
                                           E.D.A = p_EDA,
                                           all = p_all)
  return(pvalue_phenotypes_combined)
}

cbbPalette <- c( "#E69F00", "#D55E00",  "#56B4E9",  "#F0E442", "#0072B2", "#000000","#CC79A7","#009E73")

Load p-values of molecular QTLs

m6A_version <- "jointPeak_threshold5_MeRIP_HISAT2Map"
m6A_phenotype_name <- "m6APeak_logOR_GC.IP.adjusted_qqnorm"
type_apaQTL <- "dist" # choose the closest SNP-transcript pairs for APA-QTLs

cat("m6A version:", m6A_version, "\n")
cat("m6A phenotype:", m6A_phenotype_name, "\n")

dir_m6AQTL_results <- paste0("/project2/xinhe/m6A/m6A_seq/m6A_QTL/results/hg19/m6A_QTLs/", m6A_version)

dir_output_full <- paste0(dir_m6AQTL_results, "/jointLCLs_analysis/", m6A_phenotype_name, "/m6AQTLs_full_PCs_APA", type_apaQTL)
m6AQTLs_info_full.df <- readRDS(paste0(dir_output_full,"/m6AQTLs_info_full.rds"))[,c("SNP", "PEAK", "genename_snp_pair")]
pvalue_joint_m6AQTLs_full.df <- readRDS(paste0(dir_output_full,"/pvalue_jointLCLs_m6AQTLs_full.rds"))
colnames(pvalue_joint_m6AQTLs_full.df) <- gsub("Txn", "", colnames(pvalue_joint_m6AQTLs_full.df))

dir_output <- "/project2/xinhe/m6A/data_shared/jointLCLs/pi1_m6AQTLs_leadSNPs_noTE"
dir.create(dir_output, showWarnings = F, recursive = T)

select phenotypes

phenotype_list <- c("m6A", "Expression", "Ribosome", "Protein", "Decay", "APA")

pvalue_joint_m6AQTLs_full.df <- pvalue_joint_m6AQTLs_full.df[,phenotype_list]

select lead SNPs

pvalue_joint_m6AQTLs_info_full.df <- cbind(m6AQTLs_info_full.df[,c("SNP", "PEAK")], pvalue_joint_m6AQTLs_full.df)

pvalue_joint_m6AQTLs_info_leadSNPs.df <- pvalue_joint_m6AQTLs_info_full.df[order(pvalue_joint_m6AQTLs_info_full.df$m6A), ]

pvalue_joint_m6AQTLs_info_leadSNPs.df <- pvalue_joint_m6AQTLs_info_leadSNPs.df[!duplicated(pvalue_joint_m6AQTLs_info_leadSNPs.df$PEAK), ]

pvalue_joint_m6AQTLs_leadSNPs.df <- pvalue_joint_m6AQTLs_info_leadSNPs.df[,phenotype_list]

dim(pvalue_joint_m6AQTLs_leadSNPs.df)

Combine phenotypes

pvalue_m6AQTLs_leadSNPs_combined <- combine_pvalue_Fisher(pvalue_joint_m6AQTLs_leadSNPs.df)

Sample 1e5 random sets

set.seed(5)

pvalue_joint_m6AQTLs_full.df <- na.omit(pvalue_joint_m6AQTLs_full.df)
pvalue_joint_random.df <- pvalue_joint_m6AQTLs_full.df[sample(1:nrow(pvalue_joint_m6AQTLs_full.df), 1e5),]
pvalue_random_combined <- combine_pvalue_Fisher(pvalue_joint_random.df)

Estimate sharing (π1) at different p-value cutoffs of m6A-QTLs

set.seed(100)

N_bootstrap <- 1000

pvalue_phenotypes <- pvalue_m6AQTLs_leadSNPs_combined

phenotype_list <- setdiff(colnames(pvalue_phenotypes), "m6A")

print(phenotype_list)

thresh_nlogP_list <- c(3, 4, 5, 6)

pi1_phenotypes.df <- expand.grid(thresh_nlogP = thresh_nlogP_list, 
                                 phenotypes = phenotype_list, 
                                 ci10 = NA, pi1 = NA, ci90 = NA)

for(i in 1:length(thresh_nlogP_list)){
  
  thresh_nlogP <- thresh_nlogP_list[i]
  cat("-log10(m6A pvalue) >", thresh_nlogP, "\n")
  
  idx_select <- which(pvalue_phenotypes$m6A < 10^(-thresh_nlogP))
  
  cat(length(idx_select), "gene-SNP pairs included \n")
  
  for(phenotype in phenotype_list){
    cat(phenotype, "...")
    pvalues <- pvalue_phenotypes[idx_select, phenotype]
    pvalues <- pvalues[!is.na(pvalues)]
    pi1_bootstrap <- bootstrap_pi1_ci(pvalues, N = N_bootstrap, ci_percent = 0.8, pi0.method = "bootstrap")
    
    pi1_phenotypes.df[pi1_phenotypes.df$thresh_nlogP == thresh_nlogP & pi1_phenotypes.df$phenotypes == phenotype, 
                      c("ci10", "pi1", "ci90")] <- pi1_bootstrap
  }
  
}

pi1_phenotypes.df$thresh_nlogP <- factor(pi1_phenotypes.df$thresh_nlogP, levels = thresh_nlogP_list)
pi1_phenotypes.df$phenotypes <- factor(pi1_phenotypes.df$phenotypes, levels = phenotype_list)

saveRDS(pi1_phenotypes.df, paste0(dir_output, "/pi1_phenotypes_jointLCLs_lead_m6AQTLs_PCs_combined_bootstrap.rds"))

Estimate sharing (π1) using random sets

pi1_random.df <- expand.grid(thresh_nlogP = c("random"), 
                             phenotypes = phenotype_list, 
                             ci10 = NA, pi1 = NA, ci90 = NA)

for(phenotype in phenotype_list){
  # cat(phenotype, ": ")
  pvalues_random <- na.omit(pvalue_random_combined[, phenotype])
  
  pvalues <- sample(pvalues_random, nrow(pvalue_joint_m6AQTLs_leadSNPs.df))
  
  pi1_bootstrap <- bootstrap_pi1_ci(pvalues, N = N_bootstrap, ci_percent = 0.8, pi0.method = "bootstrap")
  
  pi1_random.df[pi1_random.df$thresh_nlogP == "random" & pi1_random.df$phenotypes == phenotype, 
                c("ci10", "pi1", "ci90")] <- pi1_bootstrap
  
  # cat(length(na.omit(pvalues)), phenotype, "samples without missing values. \t")
  
}


saveRDS(pi1_random.df, paste0(dir_output, "/pi1_phenotypes_jointLCLs_random_combined_bootstrap.rds"))


pi1_phenotypes_all.df <- rbind(pi1_phenotypes.df, pi1_random.df)

saveRDS(pi1_phenotypes_all.df, paste0(dir_output, "/pi1_phenotypes_jointLCLs_lead_m6AQTLs_PCs_random_combined_bootstrap.rds"))

Plot π1 with error bars of 80% confidence intervals

pi1_phenotypes_all.df <- readRDS(paste0(dir_output, "/pi1_phenotypes_jointLCLs_lead_m6AQTLs_PCs_random_combined_bootstrap.rds"))

pi1_phenotypes_all.df$phenotypes <- gsub("E.D.A", "Combined", pi1_phenotypes_all.df$phenotypes)

phenotype_list <- c("Expression", "Ribosome", "Protein", "Decay", "APA", "Combined")
thresh_nlogP_list <- c("random", 3, 4, 5, 6)

pi1_phenotypes.df <- pi1_phenotypes_all.df[pi1_phenotypes_all.df$phenotypes %in% phenotype_list &
                                             pi1_phenotypes_all.df$thresh_nlogP %in% thresh_nlogP_list, ]

pi1_phenotypes.df$phenotypes <- factor(pi1_phenotypes.df$phenotypes, levels = phenotype_list)
pi1_phenotypes.df$thresh_nlogP <- factor(pi1_phenotypes.df$thresh_nlogP, levels = thresh_nlogP_list)

# Use 80% confidence intervals
p <- ggplot(pi1_phenotypes.df, 
            aes(x=phenotypes, y=pi1, fill=thresh_nlogP)) + 
  geom_bar(position=position_dodge(), stat="identity") +
  geom_errorbar(aes(ymin=ci10, ymax=ci90),
                width=.2,                    # Width of the error bars
                position=position_dodge(.9)) + 
  labs(title = "QTL sharing (ascertained on m6A-QTLs)",
       x = "\nAscertainment cutoff", 
       y = expression(paste("Sharing (", pi["1"], ")")), 
       fill = expression(paste(-log[10], " p-value"))) + 
  coord_cartesian(ylim=c(0,0.8)) +
  scale_fill_manual(values=cbbPalette) +
  theme_minimal() +
  theme(legend.position="bottom")

print(p)

sessionInfo()