An R script for FOCUS fine-mapping analysis #!/usr/bin/env Rscript slurm_arrayid <- Sys.getenv('SLURM_ARRAY_TASK_ID') job.id <- as.numeric(slurm_arrayid) #export PATH=$PATH:/gpfs/research/chongwu/shared/software #job.id = 4 library(data.table) library(RSQLite) library(tidyverse) args <- commandArgs(TRUE) job <- (eval(parse(text = args[[1]]))) job <- as.numeric(job) # prepare the indx ref_ld = "/gpfs/research/chongwu/shared/1000Genomes/1000G.EUR.ALLSNP.QC.CHR" loci = "/gpfs/research/chongwu/shared/LDetect_LD_regions/EUR/" sumstat = "/gpfs/research/chongwu/Chong/Lang/AD_TWAS/processed_data.txt" # generated from munge.R #significant gene list twasresInput = "gene_AD.csv" twasres = as.data.frame(fread(twasresInput)) #twasres[,"pvalue"] = as.numeric(twasres[,"pvalue"]) #job = 1 tissueall = c("Brain_Amygdala","Brain_Anterior_cingulate_cortex_BA24","Brain_Caudate_basal_ganglia","Brain_Cortex","Brain_Hippocampus","Brain_Hypothalamus","Brain_Nucleus_accumbens_basal_ganglia","Brain_Putamen_basal_ganglia","Brain_Substantia_nigra","Pituitary") parentdir = "/gpfs/research/chongwu/Chong/Lang/AD_TWAS/" if(job<=10) { tissue = tissueall[job] weights = paste("/gpfs/research/chongwu/shared/TWAS_JTI/UTMOST_",tissue,".db",sep="") sqlite.driver <- dbDriver("SQLite") db <- dbConnect(sqlite.driver,dbname = weights) dbListTables(db) weights = dbReadTable(db, "weights") } else { weights = NULL extra = NULL for (j in 1:length(tissueall)) { weight = paste("/gpfs/research/chongwu/shared/TWAS_JTI/UTMOST_",tissueall[j],".db",sep="") sqlite.driver <- dbDriver("SQLite") db <- dbConnect(sqlite.driver,dbname = weight) dbListTables(db) tmp = dbReadTable(db, "weights") tmp$name = paste(tmp[,"gene"],"_",tissueall[j],sep="") weights = rbind(weights,tmp) tmp = dbReadTable(db, "extra") tmp$name = paste(tmp[,"gene"],"_",tissueall[j],sep="") extra = rbind(extra,tmp) } dt <- data.table(extra) extra2 = dt[ , max(pred.perf.R2), by = gene] extra2 = as.data.frame(extra2) id = paste(extra2[,1],extra2[,2],sep="_") id2 = paste(extra[,"gene"],extra[,"pred.perf.R2"],sep="_") extra3 = extra[id2 %in% id,] extra3 = extra3[!duplicated(extra3[,"gene"]),] length(unique(weights$name )) weights = weights[weights$name %in% extra3$name,] tissue = "ALL" } outd = paste(parentdir,tissue,sep="") weightssave = paste(parentdir,tissue,"_weights.txt",sep="") saveprefix = paste(tissue,"_UT",sep="") genelist = paste(parentdir,tissue,"_UT_gene_list.txt",sep="") #tmp = dbReadTable(db, "construction") #1. wgtlist #rsid gene weight ref_allele eff_allele #1 rs141364387 ENSG00000169583 -0.19646764 C T # gene linformation wgtlist0 #gene.inf = readRDS("/gpfs/research/chongwu/shared/GTEX8/gencode.v26.hg19.genes.rds") #gene.inf$gene = gsub("\\..*","",gene.inf$gene_id) #gene.inf = gene.inf[!duplicated(gene.inf$gene),] #10321 #gene.inf = gene.inf[,c("V4","V5","V1","gene")] #gene.inf = as.data.frame(fread("ENSEMBL_GRch37_gene_list.txt")) #gene.inf = gene.inf[,c(5,6,4,1)] #colnames(gene.inf) = c("left","right","chr","gene") #gene.inf = as.data.frame(fread("gencode.v19.exclude_pseudogene-1.list")) #gene.inf[,"V5"] = gsub("\\..*","",gene.inf[,"V5"]) #gene.inf = gene.inf[,c(3,4,1,5)] #colnames(gene.inf) = c("left","right","chr","gene") # The above different versions do not cover all the genes gene.inf = as.data.frame(fread("/gpfs/research/chongwu/shared/gencode.v19.annotation.gtf.gz")) gene.inf = gene.inf[gene.inf[,3] %in% c("gene"),] genetmp = gene.inf[,9] tmp = gsub("\";.*","",genetmp) tmp2 = gsub("gene_id \"","",tmp) tmp2 = gsub("\\..*","",tmp2) gene.inf$gene = tmp2 tmp = gsub(".*gene_name \"","",genetmp) tmp = gsub("\";.*","",tmp) gene.inf$name = tmp gene.inf = gene.inf[,c(4,5,1,10)] colnames(gene.inf) = c("left","right","chr","gene") #gene.inf = gene.inf[gene.inf[,1]==paste0("chr",chr.id),] dim(weights) length(unique(weights$gene)) weights = weights[weights$gene %in% gene.inf$gene,] dim(weights) gene.inf = gene.inf[gene.inf$gene %in% weights$gene,] dim(gene.inf) write.table(weights,weightssave,col.names=TRUE,row.names=FALSE,quote=FALSE) write.table(gene.inf,genelist,col.names=TRUE,row.names=FALSE,quote=FALSE) #2. wgtlist0 # left right chr gene #1 169818772 169863408 chr1 ENSG00000000457 #2 169631245 169823221 chr1 ENSG00000000460 gene.inf2 = gene.inf[gene.inf[,"gene"] %in% twasres[,"gene"],] gene.inf2[,"chr"] = gsub("chr","",gene.inf2[,"chr"]) usedIndx = matrix(NA,dim(gene.inf2)[1],2) for(i in 1:dim(gene.inf2)[1]) { tmp.loci = fread(paste(loci, "fourier_ls-chr",gene.inf2[i,"chr"],".bed",sep="")) tmp.loci = as.data.frame(tmp.loci) usedIndx[i,1] = gene.inf2[i,"chr"] tmpIndx = 1:dim(tmp.loci)[1] usedIndx[i,2] = tmpIndx[tmp.loci[,2] < gene.inf2[i,1] & tmp.loci[,3] > gene.inf2[i,1] ] } my_data <- as_tibble(usedIndx) usedIndx = my_data %>% distinct() usedIndx = as.data.frame(usedIndx) chr.id = usedIndx[job.id,1] loci.indx = usedIndx[job.id,2] system(paste("Rscript FOGS.R --refld ",ref_ld," --outd ",outd," --loci ",loci," --weights ", weightssave," --genelist ",genelist, " --sumstat ",sumstat," --saveprefix ",saveprefix," --chr_id ",chr.id, " --locus_id ",loci.indx,sep=""))