-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGLiMMPS_functions.R
More file actions
285 lines (221 loc) · 9.47 KB
/
Copy pathGLiMMPS_functions.R
File metadata and controls
285 lines (221 loc) · 9.47 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
########################
# functions for GLiMMPS
#
# Last update: 08/21/2012
#
library(lme4)
################################################################################################################################################
# functions for sQTL associations
########
#
# The sQTL functions have input data as a list(y, n, SNP), and output the p-values and beta estimate for fixed effect (intercept and then the SNP effect)
##################
# generalized linear model
glm.sQTL<- function (data) {
options(warn=2) #
snp.pval <- 1
betas <- c(0,0)
#psi.fitted
if ( length(unique(data$SNP[!is.na(data$y) & data$n>0 & !is.na(data$SNP)] )) >1) {
response <- cbind(data$y, data$n-data$y)
SNP <- data$SNP
testglm = try(glm(response ~ SNP, family = binomial),FALSE) #
#summary(results)
# summary(results)$coefficients
#summary(results)$fitted.values
if (!( inherits(testglm,"try-error") || !testglm$converged)) { # only when there is no complete separation problem and it converges in logistic regression
results <- testglm
betas <- summary(results)$coefficients[,1]
snp.pval <- summary(results)$coefficients[2,4]
}
#psi.fitted <- results$fitted.value
}
options(error = NULL, warn = 0)
return ( list(betas =betas, pval = snp.pval ) )
}
##################
# generalized linear model with quasibinomial family to handle overdispersion; it's performing well.
glmquasi.sQTL<- function (data) {
options(warn=2) #
snp.pval <- 1
betas <- c(0,0)
#psi.fitted
if ( length(unique(data$SNP[!is.na(data$y) & data$n>0 & !is.na(data$SNP)] )) >1) {
nomissing <- (!is.na(data$y) & data$n>0 & !is.na(data$SNP))
response <- cbind(data$y[nomissing], data$n[nomissing]-data$y[nomissing])
SNP <- data$SNP[nomissing]
testglm = try(glm(response ~ SNP, family = quasibinomial),FALSE) #quasibinomial(Overdispersed Binomial Regression).
#summary(results)
# summary(results)$coefficients
#summary(results)$fitted.values
if (!( inherits(testglm,"try-error") || !testglm$converged)) { # only when there is no complete separation problem and it converges in logistic regression
results <- testglm
betas <- summary(results)$coefficients[,1]
snp.pval <- summary(results)$coefficients[2,4]
}
#psi.fitted <- results$fitted.value
}
options(error = NULL, warn = 0)
return ( list(betas =betas, pval = snp.pval ) )
}
##################
# generalized linear mixed model regression with Wald's test for fixed effect
glmmWald.sQTL <- function(data) {
####### glmm with Wald's test for fixed effect
# library(lme4)
options(warn=2) #
snp.pval <- 1
betas <- c(0,0)
if ( length(unique(data$SNP[!is.na(data$y) & data$n>0 & !is.na(data$SNP)] )) >1) {
nomissing <- (!is.na(data$y) & data$n>0 & !is.na(data$SNP))
response <- cbind(data$y[nomissing], data$n[nomissing]-data$y[nomissing])
SNP <- data$SNP[nomissing]
obs <- seq(1,length(SNP))
testglm = try( suppressMessages( glmer(response ~ SNP +(1|obs), family=binomial)),silent=TRUE) #invidual-level random effect for overdispersion.
if ( !( inherits(testglm,"try-error") )) { # only when it converges
betas <-fixef(testglm)
if (!is.na(fixef(testglm)[2])) {
snp.pval <- summary(testglm)$coefficients[2,4]
}
}
#psi.fitted <- results$fitted.value
}
options(error = NULL, warn = 0)
return ( list(betas =betas, pval = snp.pval ) )
}
###############
# GLiMMPS method
##################
# generalized linear mixed model regression with LRT test for fixed effect
glmm.sQTL <- function(data) {
####### glmm
# library(lme4)
#options(warn=2) #this turns all warnings into errors, KZ change
snp.pval <- 1
betas <- c(0,0)
if ( length(unique(data$SNP[!is.na(data$y) & data$n>0 & !is.na(data$SNP)] )) >1) {
nomissing <- (!is.na(data$y) & data$n>0 & !is.na(data$SNP))
response <- cbind(data$y[nomissing], data$n[nomissing]-data$y[nomissing])
SNP <- data$SNP[nomissing]
obs <- seq(1,length(SNP))
testglm = try( suppressMessages( glmer(response ~ SNP +(1|obs), family=binomial)),silent=TRUE) #invidual-level random effect for overdispersion.
testglm0 = try(suppressMessages( glmer(response ~ 1 +(1|obs), family=binomial)),silent=TRUE) #
# if (!( inherits(testglm,"try-error") || inherits(testglm0,"try-error") )) { # only when it converges ## KZchange
betas <- try(fixef(testglm), silent=TRUE)
if (! inherits(betas,"try-error") & !is.na(betas[2])) {
snp.pval <- anova(testglm, testglm0)$"Pr(>Chisq)"[2]
} else betas=c(0, 0)
# } ## KZchange
#psi.fitted <- results$fitted.value
}
# options(error = NULL, warn = 0) ## KZchange
return ( list(betas =betas, pval = snp.pval ) )
}
#######################################
# linear model
### use psi =y/n and do linear regression ##
lm.sQTL<- function (data) {
options(warn=2) #
snp.pval <- 1
betas <- c(0,0)
#psi.fitted
if ( length(unique(data$SNP[!is.na(data$y) & data$n>0 & !is.na(data$SNP)] )) >1) {
psi <- data$y/data$n
psi[data$n==0] <- NA
SNP <- data$SNP
testlm = try(lm(psi ~ SNP),FALSE) #quasibinomial(Overdispersed Binomial Regression).
#summary(results)
# summary(results)$coefficients
#summary(results)$fitted.values
if (!( inherits(testlm,"try-error") )) { # only when there is no complete separation problem and it converges in logistic regression
results <- testlm
betas <- summary(results)$coefficients[,1]
snp.pval <- summary(results)$coefficients[2,4]
}
#psi.fitted <- results$fitted.value
}
options(error = NULL, warn = 0)
return ( list(betas =betas, pval = snp.pval ) )
}
################################################################################################################################################
SNP.annotation <- function(snppos.vector, exon.start, exon.end,exon.strand) {
SNPtype <- rep("",length(snppos.vector))
for ( i in 1:length(snppos.vector) ){
snppos <- snppos.vector[i]
############### #########
dist.3ss <- ( snppos - exon.start)
dist.5ss <- ( snppos - exon.end)
type <- ">300bp"
if ( dist.3ss > (-300) && dist.5ss <= 300 ) {
type <- "<=300bp"
}
# 5'SS : 9 bases long. [3 bases in exon][6 bases in intron]
# 3'SS : 23 bases long. [20 bases in the intron][3 base in the exon]
if ( dist.3ss > 0 && dist.5ss <= 0 ) {
type <- "exon"
}
if ( dist.3ss > (-20) && dist.3ss<=3 && exon.strand=="+" ) {
type <- "3SS"
if ( dist.3ss ==0 || dist.3ss== -1 ) {
type <- "3SSAG"
}
}
if ( dist.5ss <= (20) && dist.5ss >(-3) && exon.strand=="-" ) {
type <- "3SS"
if ( dist.5ss ==1 || dist.5ss== 2 ) {
type <- "3SSAG"
}
}
if ( dist.5ss > (-3) && dist.5ss<=6 && exon.strand=="+") {
type <- "5SS"
if ( dist.5ss ==1 || dist.5ss== 2 ) {
type <- "5SSGT"
}
}
if ( dist.3ss > (-6) && dist.3ss<=3 && exon.strand=="-") {
type <- "5SS"
if ( dist.3ss ==0 || dist.3ss== -1 ) {
type <- "5SSGT"
}
}
SNPtype[i] <- type
} ## end of each SNP
return(SNPtype)
}
###############################################################################################################################################
########################
# plot functions for GLiMMPS
#
# plot the psi ~ SNP with dot size proportional to total read counts for each individual
psi.geno.plot <- function(y,n,snpID,genesymbol, exonname,exon.coordinate,outputdir ) {
ABs <- as.matrix(SNP.infor)[match(snpID, SNP.infor$SNP),c(5,4)] # A/a (Major/minor)
Alleles <- c(paste(ABs[1],ABs[1],sep="") ,paste(ABs[1],ABs[2],sep=""), paste(ABs[2],ABs[2],sep="")) # AA/Aa/aa
xlabel <- snpID
title <- paste( genesymbol,"\n", exon.coordinate,sep="") #exon.chr,":",exon.start,"-",exon.end,sep="")
title2 <- paste(genesymbol,exonname,snpID,sep="_")
gi <- seq(1,length(map[,2]))[as.character(map[,2])==snpID ]
SNP <- geno[,gi]
psi <- y/n
psi[n==0] <- NA
N <- length(n)
######################
# use the plot function for reach exonpsi-SNP pair
####### y, n, SNP, xlabel, title, Alleles (allele information)
cat ( paste("Generating Figure: psiplot_",title2,".pdf\n",sep=""))
pdf( paste(outputdir,"/psiplot_",title2,".pdf",sep=""),width=4,height=8)
# par(oma=c(0,0,0,0),mar=c(2.8,4.5,2.8,0.25), cex.lab=1.5,las=1,cex.axis=1.5) #,bty="l")
par(oma=c(0.5,0.5,0.5,0),mar=c(4,4.5,4,0.5), cex.lab=1.5,las=1,cex.axis=1.5) #,bty="l")
##############
ylim.range <- c(0,1) #range(psi,na.rm=T)
plot(jitter(SNP,factor=0.5), psi,ylab=expression(paste(psi," (RNA-Seq)")), xlab= xlabel, main=title,xlim=c(-0.25,2.75), xaxt="n",type="n" ,ylim=ylim.range) #ylim= c(0,1))
points(jitter(SNP,factor=0.5) ,psi , pch= 19, cex= log10(n+1)/1 ,col=1)
mtext(text=Alleles, side=1, at= c(0,1,2),cex=1.5,line= 1)
# mtext(text = xlabel, side=1,at=1,cex=1.5,line=1.6)
## legend of dot size
points( rep(2.3,6) , seq(1,6)*0.05+0.5 , pch= 19, cex= log10( c(1,5,10,20,50,100)+1)/1 )
text(rep(2.55,7) , seq(1,7)*0.05+0.5, c(1,5,10,20,50,100,"# reads"))
par("new"=T) # add boxplot on top
boxplot(psi~SNP, ylab="", xlab="", xaxt="n", yaxt="n",boxwex=0.35, xlim=c(-0.25,2.75), ylim=ylim.range,at=sort(unique(SNP[!is.na(SNP)])) ,border=gray(0.45),col=rgb(1,1,1,alpha=0.6) ,outline=FALSE)
dev.off()
}
########################