-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhegp.R
More file actions
198 lines (166 loc) · 5.65 KB
/
hegp.R
File metadata and controls
198 lines (166 loc) · 5.65 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
# hegp.R
# Author: Richard Mott, UCL Genetics Institute.
# (C) 2020 Richard Mott
# Generate a set of orthogonal matrices of max dimension <=bloocksize suitable for encrypting genotype dosage and phenotype datasets of the same dimension as the one in D. The dimension of the last matrix is reduced in order to ensure the sum of the dimensions exactly equals the number of individuals in D. If blocksize<=0 then a single block to encrypt the dataset is generated
make.encrypter <- function( D, blocksize=0, shuffle=TRUE ) {
N = nrow(D$geno)
if ( blocksize <= 0 ) blocksize = N;
start = 1
end = min( blocksize, N)
encrypter = list(N=N)
block = list()
b = 1
df = NULL
while ( end <= N ) {
if ( end > N-100 ) end = N
bsize = end-start+1
block[[b]] = rustiefel( bsize, bsize, shuffle )
df = rbind( df, c(start, end, bsize ))
start = end+1
if ( end == N ) {
end = N+1
} else {
end = min( start + blocksize-1, N )
}
b = b+1
}
df = data.frame(df)
names(df) = c( "start", "end", "size" )
encrypter$blocks = df
encrypter$block = block
return ( encrypter )
}
# Encypt a dataset D using the encrypter
encrypt.D <- function( D, encrypter, invert=FALSE, kinship=FALSE ) {
blocks = encrypter$blocks
block = encrypter$block
encrypted = list( y=rep( 0, length=length(D$y)), geno = matrix( 0, nrow=nrow(D$geno), ncol=ncol(D$geno)), cov=matrix(0, nrow=nrow(D$cov), ncol=ncol(D$cov)))
colnames(encrypted$geno) = colnames(D$geno)
colnames(encrypted$cov) = colnames(encrypted$cov)
for( i in 1:nrow(blocks) ) {
P = encrypter$block[[i]]
if ( invert ) P = t(P)
idx = blocks$start[i]:blocks$end[i]
encrypted$y[idx] = P %*% D$y[idx]
encrypted$geno[idx,] = P %*% D$geno[idx,]
encrypted$cov[idx,] = P %*% D$cov[idx,]
encrypted$map = D$map
}
names(encrypted$y) = names(D$y)
rownames(encrypted$geno) = rownames(D$geno)
rownames(encrypted$cov) = rownames(D$cov)
if ( kinship ) {
encrypted$kinship = make.kinship(encrypted$geno)
colnames(encrypted$kinship) = rownames(encrypted$geno)
rownames(encrypted$kinship) = rownames(encrypted$geno)
}
return( encrypted )
}
# create a dataset comprising a genotype dosage matrix, phenotype vector and covariate matrix
build.D <- function( y, dosages, cov=NULL, map=NULL, kinship=FALSE ) {
y = y[!is.na(y)]
ids = intersect( names(y) , rownames(dosages))
y = y[match(ids, names(y), nomatch=0)]
dosages = dosages[match(ids, rownames(dosages), nomatch=0),]
N = length(y)
if ( !is.numeric(y)) {
warning( "y is not numeric")
return(NULL)
}
if ( is.null(cov)) {
cov = matrix(1, nrow=N, ncol=1)
colnames(cov) = c("intercept")
}
if ( sum(is.na(y)) + sum(is.na(dosages)) + sum(is.na(cov)) > 0 ) {
warning( "data contain missing values" )
return(NULL)
}
if ( N != nrow(dosages ) | ( !is.null(cov) & N != nrow(cov) )) {
warning( "dimensions incompatible")
return(NULL)
}
y.s = scale(y)
names(y.s) = names(y)
af = apply(dosages, 2, mean)
af = ifelse( af < 0.5, af, 1-af)
geno = safe.scale(dosages)
cov = safe.scale(cov)
D = list( y=y.s, geno=geno, cov=cov, map=map, maf=af )
if ( kinship ) {
K = make.kinship(D$geno)
D$kinship = K
}
return(D)
}
safe.scale <- function( mat) {
# scale the columns of a matrix delaing safely with situation where variance of column is 0
# if a column is not numeric then it is converted to c column of 0's
# row and column names are preserved.
m = apply(mat,2, function(x) {
if ( is.numeric(x)) {
s = sd(x)
if ( s > 0.0) {
x = (x-mean(x))/s
} else {
x = rep(0,len=length(x))
}
}
else {
x = rep(0,len=length(x))
}
})
colnames(m) = colnames(mat)
rownames(m) = rownames(mat)
return(m)
}
# Perform a simple quantitative trait GWAS without a mixed model, regressing phenotype on genotype dosage.
basic.gwas <- function( D, mc.cores=10 ) {
to = floor((1:mc.cores)*ncol(D$geno)/mc.cores)
to[mc.cores] = ncol(D$geno)
start = c(1, to[1:(mc.cores-1)]+1)
lp = mclapply( 1:mc.cores, function( segment ) {
# cat("segment", segment, start[segment],to[segment],"\n")
r = as.numeric(cor( D$y, D$geno[,start[segment]:to[segment]]))
r2 = r*r
n = length(D$y)
t = r * sqrt( (n-2)/(1-r2) )
logP = -pt( abs(t), df=n-2, lower.tail=FALSE, log.p=TRUE)/log(10)
return(logP) })
logP = unlist(lp)
return( data.frame(cbind(D$map, logP)))
}
# A simple GWAS with a mixed model...
basic.mm.gwas <- function( D, mc.cores=10) {
y = D$y
if( is.null(names(y))) names(y) = as.character(1:length(y))
genotypes = D$geno
rownames(genotypes) = names(y)
mm = mixed.model.gwas( y, genotypes, kinship=D$kinship, nperm=0 )
return( data.frame(cbind(D$map, logP=-log10(mm$pval[1,]))))
}
# Simulate a random orthogonal matrix of dimensions m*R using the Steifel Manifold (function adapted from R package rsteifel)
rustiefel <- function (m, R=m, shuffle=TRUE)
{
rn = rnorm(m * R)
if ( shuffle ) { # permute the random numbers for extra security
f = file("/dev/random", "rb")
data = readBin(f, integer(), size=4, n = m*R, endian="little")
r = order(data)
rn = rn[r]
}
X <- matrix(rn, m, R)
tmp <- eigen(t(X) %*% X)
X %*% (tmp$vec %*% sqrt(diag(1/tmp$val, nrow = R)) %*% t(tmp$vec))
}
# Quantile normalise a dataset
qnorm.D <- function( D, digits=NA) {
n = length(D$y)
q = qnorm((1:n)/(n+1))
if ( !is.na(digits)) q= signif(q,digits=digits)
Dq = list()
Dq$y[order(D$y)] = q
Dq$geno = apply( D$geno, 2, function( x, q) { z=rep(0,length(q)); z[order(x)]=q; z}, q)
Dq$cov = apply( D$cov, 2, function( x, q){ z=rep(0,length(q)); z[order(x)]=q; z}, q)
Dq$map = D$map
return(Dq)
}