Last updated: 2020-11-24

Checks: 7 0

Knit directory: myproject/

This reproducible R Markdown analysis was created with workflowr (version 1.6.2). 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(20200813) 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 701de6d. 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:    .DS_Store
    Ignored:    .RData
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/
    Ignored:    analysis/.DS_Store
    Ignored:    analysis/11.24.2020.2.png
    Ignored:    analysis/11.24.2020.3.png
    Ignored:    analysis/11.24.2020.4.png
    Ignored:    analysis/11.24.2020.5.png
    Ignored:    analysis/11.24.2020.6.png
    Ignored:    analysis/11.24.2020.7.png
    Ignored:    analysis/11.24.2020.8.png
    Ignored:    analysis/11.24.2020.9.png
    Ignored:    genotype/

Untracked files:
    Untracked:  ALL.chr1.phase3_shapeit2_mvncall_integrated_v5a.20130502.genotypes.vcf.gz
    Untracked:  ALL.chr2.phase3_shapeit2_mvncall_integrated_v5a.20130502.genotypes.vcf.gz
    Untracked:  getfastqtest2.csv

Unstaged changes:
    Modified:   analysis/prep.Rmd
    Modified:   analysis/susie.Rmd
    Modified:   analysis/varLD.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/11.WIP.Rmd) and HTML (docs/11.WIP.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 701de6d mariesaitou 2020-11-24 Add my first analysis
html b8ee295 mariesaitou 2020-11-24 Build site.
html 218140e mariesaitou 2020-11-24 Build site.
html 687384e mariesaitou 2020-11-24 Build site.
html 2624e35 mariesaitou 2020-11-24 Build site.
Rmd 9fde80d mariesaitou 2020-11-24 Add my first analysis

1. mashR sharing

library(mashr)
library(tidyverse)

data <- readRDS("fastqtl_to_mash_output/input.file.mash.rds")

data.temp = mash_set_data(data$random.b,data$random.s,zero_Shat_reset = 1)
Vhat = estimate_null_correlation_simple(data.temp)
rm(data.temp)

data.random = mash_set_data(data$random.b,data$random.s,V=Vhat,zero_Shat_reset = 1)
data.strong = mash_set_data(data$strong.b,data$strong.s, V=Vhat,zero_Shat_reset = 1)

U.pca = cov_pca(data.strong,2)
U.ed = cov_ed(data.strong, U.pca)
U.c = cov_canonical(data.random)
m = mash(data.random, Ulist = c(U.ed,U.c))

BH  <- read.csv("DEG.susie.1025.BH.csv", header = T, sep = ",")

## extract genes based on susieR results
TF <- BH$susie.BH=="EUR"
#TF <- BH$susie.BH!="dummy"
TF[is.na(TF)] <- F
core <-  BH[TF,]
#mashr:::get_ncond(m)
#kore$gene

#save.image(file = "mashr.space.RData")
#load(file = "mashr.space.RData")


m_genes <- sapply(str_split(rownames(m[["result"]][["PosteriorMean"]]), "_"), `[`,1)
new_m_pm <- m[["result"]][["PosteriorMean"]][m_genes %in% core$gene,]
new_m_lfsr <- m[["result"]][["lfsr"]][m_genes %in% core$gene,]

get_significant_results2 = function(new_m_pm, new_m_lfsr, thresh = 0.05, conditions = NULL) {
  if (is.null(conditions)) {
    conditions = 1:ncol(new_m_pm)
  }
  top = apply(new_m_lfsr[,conditions,drop=FALSE],1,min) # find top effect in each condition
  sig = which(top < thresh)
  ord = order(top[sig],decreasing=FALSE)
  sig[ord]
}
get_pairwise_sharing2= function(new_m_pm, new_m_lfsr, factor=0.5, lfsr_thresh=0.05, FUN= identity){
  R = ncol(new_m_pm)
  lfsr = new_m_lfsr
  S=matrix(NA,nrow = R, ncol=R)
  SD=matrix(NA,nrow = R, ncol=R)
  N=matrix(NA,nrow = R, ncol=R)
  for(i in 1:R){
    for(j in i:R){
      sig_i=get_significant_results2(new_m_pm, new_m_lfsr,thresh=lfsr_thresh,conditions = i)
      sig_j=get_significant_results2(new_m_pm, new_m_lfsr,thresh=lfsr_thresh,conditions = j)
      a=union(sig_i,sig_j)
      ratio=FUN(new_m_pm[a,i])/FUN(new_m_pm[a,j])##divide effect sizes
      S[i,j]=mean(ratio>factor & ratio<(1/factor))
      SD[i,j]=sd(ratio>factor & ratio<(1/factor))
      N[i,j]=length(ratio>factor & ratio<(1/factor))
    }
  }
  S[lower.tri(S, diag = FALSE)] = t(S)[lower.tri(S, diag = FALSE)]
  colnames(S) = row.names(S) = colnames(new_m_pm)
  
  SD[lower.tri(SD, diag = FALSE)] = t(SD)[lower.tri(SD, diag = FALSE)]
  colnames(SD) = row.names(SD) = colnames(new_m_pm)
  
  N[lower.tri( N, diag = FALSE)] = t( N)[lower.tri( N, diag = FALSE)]
  colnames( N) = row.names( N) = colnames(m$result$PosteriorMean)
  
  return(list(S,SD, N))
}

## calculate the sharing based on different local false sign rate cut-off
get_pairwise_sharing2(new_m_pm, new_m_lfsr, factor = 0.5, lfsr_thresh = 1, FUN = identity)
get_pairwise_sharing2(new_m_pm, new_m_lfsr, factor = 0.5, lfsr_thresh = 0.5, FUN = identity)
get_pairwise_sharing2(new_m_pm, new_m_lfsr, factor = 0.5, lfsr_thresh = 0.1, FUN = identity)
get_pairwise_sharing2(new_m_pm, new_m_lfsr, factor = 0.5, lfsr_thresh = 0.05, FUN = identity)
result

result

2. susieR result summary

df <- read.csv( "DEG.susie.1025.BH.csv",header = T, sep = ",",)
## remove FDR>0.1 genes in fast QTL
df2<-subset(df, df$EUR.YRI.BH!="FALSE.FALSE")
## colored by susieR results (NA = FDR<0.1 in fastQTL but not fine-mapped in susieR)
ggplot(df2, aes(x="", fill=susie.BH))+ geom_bar(position="fill")+theme_bw()
result

result

3. Minor allele frequency spectrum of the fine-mapped SNPs in only one population

d2  <- read.csv("egene.freq.DEG.beta.1025.BH.csv", header = T, sep = ",")
d2diff<-subset(d2,d2$susie.BH=="both.diffSNP")
ggplot(d2diff, aes(x=Yoruba.minor, fill=SNP.status)) +
  geom_histogram(aes(y=..density..), position="dodge",bins = 10)+
  theme(legend.position="top")
result

result

4. Paintor x SusieR

genelist=read.csv("susieR.gene.diff.csv", stringsAsFactors = F)
file <-paste("/Users/saitoumarie/Dropbox/Chicago/RCC/eQTL.practice/LDSC/PESCA/",genelist[,"gene"], ".Paintor.results",sep="")

paintor<- lapply(file, FUN=read.table, header = TRUE, stringsAsFactors = F)
names(paintor) <- genelist[,"gene"]
# get list of posterior probabilities
posterior <- lapply(paintor, '[[',"Posterior_Prob")

# define functions for convenience
larger <- function(x,y){
  x > y
}
getlist <- function(x,y){
  x[y,]
}
put_name <- function(x,y){
  if(nrow(x)!=0){
    x['gene'] <- y
    x
  }
}

# subset elements with posterior probability > 0.8
result <- mapply(getlist, paintor, lapply(posterior,larger, 0.8), SIMPLIFY=F) 
result <- mapply(put_name, result, names(result), SIMPLIFY = F) # put gene name in dataframe

#merge data frames
result.dataframe = data.frame()
for(i in 1:length(result)){
  result.dataframe <- rbind(result.dataframe, result[[i]])
}

result.dataframe
write.csv(result.dataframe, file = "Paintor.diffSNP.PIP80.csv")

Paintor = read.csv("Paintor.susie.80.csv", header=T)
ggplot(Paintor, aes(x=ZSCORE.EUR, y=ZSCORE.YRI, shape=SNP.SusieR, color=SNP.SusieR, size = 5, alpha=0.9)) +
  scale_color_manual(values=c('#E69F00', '#999999','#56B4E9'))+
  geom_point()+theme_bw()

result result

5.credible set analysis

Yoruba = read.csv("/Users/saitoumarie/Dropbox/Chicago/RCC/eQTL.practice/finemap/YRI.susieR.425.csv", header=T)
Yoruba.CS<-as.data.frame(cbind(unique(Yoruba$gene),lapply(unique(Yoruba$gene),eachgene, Yoruba)))
Yoruba.CS$V1<-unlist(Yoruba.CS$V1)
Yoruba.CS$V2<-unlist(Yoruba.CS$V2)
# hist(Yoruba.CS$V2) ## containes non-BH genes
length(unique(lapply(unique(Yoruba$gene),eachgene, Yoruba)))
genes <- unique(Yoruba$gene)

EUR = read.csv("/Users/saitoumarie/Dropbox/Chicago/RCC/eQTL.practice/finemap/EUR.susieR.425.csv", header=T)
EUR.CS<-as.data.frame(cbind(unique(EUR$gene),lapply(unique(EUR$gene),eachgene, EUR)))
EUR.CS$V2<-unlist(EUR.CS$V2)
EUR.CS$V1<-unlist(EUR.CS$V1)
hist(EUR.CS$V2)

length(unique(lapply(unique(EUR$gene),eachgene, EUR)))
genes <- unique(EUR$gene)

df = read.csv("/Users/saitoumarie/Dropbox/Chicago/RCC/eQTL.practice/DEG.susie.1025.BH.csv", header=T)

YRI.BH<-subset(df, df$in_YRI.BH=="TRUE")
YRI.CSBH<-merge(Yoruba.CS,YRI.BH,by.x="V1",by.y="gene")
hist(YRI.CSBH$V2)


EUR.BH<-subset(df, df$in_EUR.BH=="TRUE")
EUR.CSBH<-merge(EUR.CS,EUR.BH,by.x="V1",by.y="gene")
hist(EUR.CSBH$V2)

### compare PIP in SNPs 
library(dplyr)

EUR$gene.CS <- paste(EUR$gene, ".", EUR$L)
EUR %>% group_by(gene.CS) %>% summarize(count=n())

Yoruba$gene.CS <- paste(Yoruba$gene, ".", Yoruba$L)
Yoruba %>% group_by(gene.CS) %>% summarize(count=n())
result

result

maxlist1  <- data.frame()
for (i in 1:length(unique(Yoruba$gene.CS))){
  uni_list = Yoruba[Yoruba$gene.CS==unique(Yoruba$gene.CS)[i],]
  temp <- uni_list[uni_list$PIP==max(uni_list$PIP),]
  maxlist1 <- rbind(maxlist1, temp)
}
maxlistnondup<-maxlist1[!duplicated(maxlist1$gene), ]
hist(maxlistnondup$PIP)
result

result

6. susieR 2-pop merged

library(susieR)
library(data.table)
## read the gene list

genelist=read.csv("BHloc.bothpop.ex.csv", stringsAsFactors = F)
#gene.expression.Africa = read.table("Yoruba.TPM.scaled.gene.bed")
gene.expression = read.csv("scaled.bothpops.TPM.csv", stringsAsFactors = F)

## read a gene from the list
filename<-paste("both/",genelist[,"gene"], ".455.genotype.recode.vcf", sep="")
genotype<- lapply(both/filename, FUN=read.table, header = FALSE, stringsAsFactors = F)
names(genotype) <- genelist[,"gene"]
genotype.df <- rbindlist(genotype, fill=T, idcol = T)
genotype.df<-genotype.df[!duplicated(genotype.df[,c(".id","V3" )])&!duplicated(genotype.df[,c(".id","V3" )], fromLast = T),]


## convert vcf format as input dataset

genotype.data <- genotype.df[,11:length(genotype.df[1,])] 

genotype.data[genotype.data=="0|0"]<- 0L
genotype.data[genotype.data=="0|1"]<- 1L
genotype.data[genotype.data=="1|0"]<- 1L
genotype.data[genotype.data=="1|1"]<- 2L
genotype.data1<- as.matrix(genotype.data)
genotype.data<- matrix(as.numeric(genotype.data1), nrow = nrow(genotype.data))

## scale the genotypes
scale.gen <- scale(t(genotype.data))
#hist(scale.gen.Yoruba[,20])

## extract genes from the gene expression list
test.expression <- gene.expression[unlist(lapply(genelist$gene, grep, gene.expression$gene)),]
expression <- t(test.expression[,-(1:4)])

## susieR
fitted.test<- as.list(NULL)
for(i in 1:2998){
  fitted.test[[i]] <-   susie(scale.gen[,(genotype.df$.id==genelist$gene[i])], expression[,i],
                                     L = 10,
                                     estimate_residual_variance = TRUE, 
                                     estimate_prior_variance = FALSE,
                                     scaled_prior_variance = 0.95,
                                     verbose = TRUE)
}


## attach gene names to the result
fitted<-  as.list(NULL)
for(i in 1:2998){
  if(length(fitted.test[[i]]$sets$cs)!=0){
    fitted[[i]] <- cbind(stack(fitted.test[[i]]$sets$cs),fitted.test[[i]][["pip"]][unlist(fitted.test[[i]]$sets$cs)])
  }
}

check<- rbindlist(fitted, idcol=T)
check$name <- genelist[check$.id,"gene"]

names(check)
names(check)[4] <- "PIP"
result.temp <- as.list(NULL)
for(i in 1:length(check$.id)){result.temp[i] <- lapply(genotype[check$name[i]], "[", check$values[i],1:3,)}
result<- data.frame(check$name, check$ind, check$values,check$PIP, rbindlist(result.temp))
names(result)<-c("gene","L","SNP","PIP","chr","loc","rs")

library(dplyr)
result<-result %>% as.data.frame() %>% mutate(gene.SNP = paste(!!!rlang::syms(c("gene", "rs")), sep="."))
write.csv(result, file = "susieR.bothpopsPIP.csv", append = T)
result

result


sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS  10.16

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

other attached packages:
[1] workflowr_1.6.2

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.5       rstudioapi_0.11  whisker_0.4      knitr_1.30      
 [5] magrittr_1.5     R6_2.4.1         rlang_0.4.8      stringr_1.4.0   
 [9] tools_4.0.2      xfun_0.18        git2r_0.27.1     htmltools_0.5.0 
[13] ellipsis_0.3.1   rprojroot_1.3-2  yaml_2.2.1       digest_0.6.27   
[17] tibble_3.0.4     lifecycle_0.2.0  crayon_1.3.4     later_1.1.0.1   
[21] vctrs_0.3.4      promises_1.1.1   fs_1.5.0         glue_1.4.2      
[25] evaluate_0.14    rmarkdown_2.5    stringi_1.5.3    compiler_4.0.2  
[29] pillar_1.4.6     backports_1.1.10 httpuv_1.5.4     pkgconfig_2.0.3