-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFUSION.compute_hsq.R
246 lines (214 loc) · 9.73 KB
/
FUSION.compute_hsq.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
## Mofied from https://github.com/LieberInstitute/fusion_twas/blob/jhpce/FUSION.compute_weights.R
suppressMessages(library("optparse"))
suppressMessages(library('plink2R'))
suppressMessages(library('glmnet'))
suppressMessages(library('methods'))
option_list = list(
make_option("--bfile", action="store", default=NA, type='character',
help="Path to PLINK binary input file prefix (minus bed/bim/fam) [required]"),
make_option("--out", action="store", default=NA, type='character',
help="Path to output files [required]"),
make_option("--tmp", action="store", default=NA, type='character',
help="Path to temporary files [required]"),
make_option("--pheno", action="store", default=NA, type='character',
help="Path to molecular phenotype file (PLINK format) [optional, taken from bfile otherwise]"),
make_option("--PATH_plink", action="store", default="plink", type='character',
help="Path to plink executable [%default]"),
make_option("--PATH_gcta", action="store", default="/jhpce/shared/jhpce/libd/fusion_twas/github/fusion_twas/gcta_nr_robust", type='character',
help="Path to plink executable [%default]"),
make_option("--PATH_gemma", action="store", default="gemma", type='character',
help="Path to plink executable [%default]"),
make_option("--covar", action="store", default=NA, type='character',
help="Path to quantitative covariates (PLINK format) [optional]"),
make_option("--resid", action="store_true", default=FALSE,
help="Also regress the covariates out of the genotypes [default: %default]"),
make_option("--hsq_p", action="store", default=0.01, type='double',
help="Minimum heritability p-value for which to compute weights [default: %default]"),
make_option("--hsq_set", action="store", default=NA, type='double',
help="Skip heritability estimation and set hsq estimate to this value [optional]"),
make_option("--crossval", action="store", default=5, type='double',
help="How many folds of cross-validation, 0 to skip [default: %default]"),
make_option("--verbose", action="store", default=1, type="integer",
help="How much chatter to print: 0=nothing; 1=minimal; 2=all [default: %default]"),
make_option("--noclean", action="store_true", default=FALSE,
help="Do not delete any temporary files (for debugging) [default: %default]"),
make_option("--rn", action="store_true", default=FALSE,
help="Rank-normalize the phenotype after all QC: [default: %default]"),
make_option("--save_hsq", action="store_true", default=FALSE,
help="Save heritability results even if weights are not computed [default: %default]"),
make_option("--models", action="store", default="blup,lasso,top1,enet", type='character',
help="Comma-separated list of prediction models [default: %default]\n
Available models:\n
top1:\tTop eQTL (standard marginal eQTL Z-scores always computed and stored)\n
blup:\t Best Unbiased Linear Predictor (dual of ridge regression)\n
bslmm:\t Bayesian Sparse Linear Model (spike/slab MCMC)\n
lasso:\t LASSO regression (with heritability used as lambda)\n
enet:\t Elastic-net regression (with mixing parameter of 0.5)\n")
)
opt = parse_args(OptionParser(option_list=option_list))
models = unique( c(unlist(strsplit(opt$models,',')),"top1") )
M = length(models)
if ( opt$verbose == 2 ) {
SYS_PRINT = F
} else {
SYS_PRINT = T
}
# --- PREDICTION MODELS
# BSLMM
weights.bslmm = function( input , bv_type , snp , out=NA ) {
if ( is.na(out) ) out = paste(input,".BSLMM",sep='')
arg = paste( opt$PATH_gemma , " -miss 1 -maf 0 -r2 1 -rpace 1000 -wpace 1000 -bfile " , input , " -bslmm " , bv_type , " -o " , out , sep='' )
system( arg , ignore.stdout=SYS_PRINT,ignore.stderr=SYS_PRINT)
eff = read.table( paste(out,".param.txt",sep=''),head=T,as.is=T)
eff.wgt = rep(NA,length(snp))
m = match( snp , eff$rs )
m.keep = !is.na(m)
m = m[m.keep]
eff.wgt[m.keep] = (eff$alpha + eff$beta * eff$gamma)[m]
return( eff.wgt )
}
# PLINK: LASSO
weights.lasso = function( input , hsq , snp , out=NA ) {
if ( is.na(out) ) out = paste(input,".LASSO",sep='')
arg = paste( opt$PATH_plink , " --allow-no-sex --bfile " , input , " --lasso " , hsq , " --out " , out , " --memory 2000 --threads 1", sep='' )
system( arg , ignore.stdout=SYS_PRINT,ignore.stderr=SYS_PRINT )
if ( !file.exists(paste(out,".lasso",sep='')) ) {
cat( paste(out,".lasso",sep='') , " LASSO output did not exist\n" )
eff.wgt = rep(NA,length(snp))
} else {
eff = read.table( paste(out,".lasso",sep=''),head=T,as.is=T)
eff.wgt = rep(0,length(snp))
m = match( snp , eff$SNP )
m.keep = !is.na(m)
m = m[m.keep]
eff.wgt[m.keep] = eff$EFFECT[m]
}
return( eff.wgt )
}
# Marginal Z-scores (used for top1)
weights.marginal = function( genos , pheno , beta=F ) {
if ( beta ) eff.wgt = t( genos ) %*% (pheno) / ( nrow(pheno) - 1)
else eff.wgt = t( genos ) %*% (pheno) / sqrt( nrow(pheno) - 1 )
return( eff.wgt )
}
# Elastic Net
weights.enet = function( genos , pheno , alpha=0.5 ) {
eff.wgt = matrix( 0 , ncol=1 , nrow=ncol(genos) )
# remove monomorphics
sds = apply( genos , 2 , sd )
keep = sds != 0 & !is.na(sds)
enet = cv.glmnet( x=genos[,keep] , y=pheno , alpha=alpha , nfold=5 , intercept=T , standardize=F )
eff.wgt[ keep ] = coef( enet , s = "lambda.min")[2:(sum(keep)+1)]
return( eff.wgt )
}
# --- CLEANUP
cleanup = function() {
if ( ! opt$noclean ) {
arg = paste("rm -f " , opt$tmp , "*", sep='')
system(arg)
}
}
# Perform i/o checks here:
files = paste(opt$bfile,c(".bed",".bim",".fam"),sep='')
if ( !is.na(opt$pheno) ) files = c(files,opt$pheno)
if ( !is.na(opt$covar) ) files = c(files,opt$covar)
for ( f in files ) {
if ( !file.exists(f) ){
cat( "ERROR: ", f , " input file does not exist\n" , sep='', file=stderr() )
cleanup()
q()
}
}
if ( system( paste(opt$PATH_plink,"--help") , ignore.stdout=T,ignore.stderr=T ) != 0 ) {
cat( "ERROR: plink could not be executed, set with --PATH_plink\n" , sep='', file=stderr() )
cleanup()
q()
}
if ( !is.na(opt$hsq_set) && system( opt$PATH_gcta , ignore.stdout=T,ignore.stderr=T ) != 0 ){
cat( "ERROR: gcta could not be executed, set with --PATH_gcta\n" , sep='', file=stderr() )
cleanup()
q()
}
if ( sum(models=="bslmm" | models=="blup") != 0 && system( paste(opt$PATH_gemma,"-h") , ignore.stdout=T,ignore.stderr=T ) != 0 ){
cat( "ERROR: gemma could not be executed, set with --PATH_gemma or remove 'bslmm' and 'blup' from models\n" , sep='', file=stderr() )
cleanup()
q()
}
# ---
fam = read.table(paste(opt$bfile,".fam",sep=''),as.is=T)
# Make/fetch the phenotype file
if ( !is.na(opt$pheno) ) {
pheno.file = opt$pheno
pheno = read.table(pheno.file,as.is=T)
# Match up data
m = match( paste(fam[,1],fam[,2]) , paste(pheno[,1],pheno[,2]) )
m.keep = !is.na(m)
fam = fam[m.keep,]
m = m[m.keep]
pheno = pheno[m,]
} else {
pheno.file = paste(opt$tmp,".pheno",sep='')
pheno = fam[,c(1,2,6)]
write.table(pheno,quote=F,row.names=F,col.names=F,file=pheno.file)
}
if ( opt$rn ) {
library('GenABEL')
library(preprocessCore)
pheno[,3] = rntransform( pheno[,3] )
write.table(pheno,quote=F,row.names=F,col.names=F,file=pheno.file)
}
# Load in the covariates if needed
if ( !is.na(opt$covar) ) {
covar = ( read.table(opt$covar,as.is=T,head=T) )
if ( opt$verbose >= 1 ) cat( "Loaded",ncol(covar)-2,"covariates\n")
# Match up data
m = match( paste(fam[,1],fam[,2]) , paste(covar[,1],covar[,2]) )
m.keep = !is.na(m)
fam = fam[m.keep,]
pheno = pheno[m.keep,]
m = m[m.keep]
covar = covar[m,]
reg = summary(lm( pheno[,3] ~ as.matrix(covar[,3:ncol(covar)]) ))
if ( opt$verbose >= 1 ) cat( reg$r.sq , "variance in phenotype explained by covariates\n" )
pheno[,3] = scale(reg$resid)
raw.pheno.file = pheno.file
pheno.file = paste(pheno.file,".resid",sep='')
write.table(pheno,quote=F,row.names=F,col.names=F,file=pheno.file)
}
geno.file = opt$tmp
# recode to the intersection of samples and new phenotype
arg = paste( opt$PATH_plink ," --allow-no-sex --bfile ",opt$bfile," --pheno ",pheno.file," --keep ",pheno.file," --make-bed --out ",geno.file, " --memory 2000 --threads 1", sep='')
system(arg , ignore.stdout=SYS_PRINT,ignore.stderr=SYS_PRINT)
# --- HERITABILITY ANALYSIS
if ( is.na(opt$hsq_set) ) {
if ( opt$verbose >= 1 ) cat("### Estimating heritability\n")
# 1. generate GRM
arg = paste( opt$PATH_plink," --allow-no-sex --bfile ",opt$tmp," --make-grm-bin --out ",opt$tmp, " --memory 2000 --threads 1", sep='')
system(arg , ignore.stdout=SYS_PRINT,ignore.stderr=SYS_PRINT)
# 2. estimate heritability
if ( !is.na(opt$covar) ) {
arg = paste( opt$PATH_gcta ," --grm ",opt$tmp," --pheno ",raw.pheno.file," --qcovar ",opt$covar," --out ",opt$tmp," --reml --reml-no-constrain --reml-lrt 1",sep='')
} else {
arg = paste( opt$PATH_gcta ," --grm ",opt$tmp," --pheno ",pheno.file," --out ",opt$tmp," --reml --reml-no-constrain --reml-lrt 1",sep='')
}
system(arg , ignore.stdout=SYS_PRINT,ignore.stderr=SYS_PRINT)
# 3. evaluate LRT and V(G)/Vp
if ( !file.exists( paste(opt$tmp,".hsq",sep='') ) ) {
cat(opt$tmp,"does not exist, likely GCTA could not converge, skipping gene\n",file=stderr())
cleanup()
q()
}
hsq.file = read.table(file=paste(opt$tmp,".hsq",sep=''),as.is=T,fill=T)
hsq = as.numeric(unlist(hsq.file[hsq.file[,1] == "V(G)/Vp",2:3]))
hsq.pv = as.numeric(unlist(hsq.file[hsq.file[,1] == "Pval",2]))
if ( opt$verbose >= 1 ) cat("Heritability (se):",hsq,"LRT P-value:",hsq.pv,'\n')
if ( opt$save_hsq ) cat( opt$out , hsq , hsq.pv , '\n' , file=paste(opt$out,".hsq",sep='') )
} else {
if ( opt$verbose >= 1 ) cat("### Skipping heritability estimate\n")
hsq = opt$hsq_set
hsq.pv = NA
}
save(hsq, hsq.pv, file = paste( opt$out , ".hsq.Rdata" , sep='' ))
# --- CLEAN-UP
if ( opt$verbose >= 1 ) cat("Cleaning up\n")
cleanup()