-
Notifications
You must be signed in to change notification settings - Fork 79
/
analysis.R
298 lines (281 loc) · 11.8 KB
/
analysis.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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
# Functions to perform the deconvolution analysis
#
# Author: Xuran Wang
#####################################################################################################
#' Estimate cell type proportion with MuSiC and NNLS
#'
#' @param Y vector of bulk tissue expression
#' @param X matrix, Signature matrix
#' @param S vector of Avg. Library size
#' @param Sigma matrix of Subject level variation (gene * cell type)
#' @param iter.max numeric, maximum iteration number
#' @param nu regulation parameter, take care of weight when taking reciprocal
#' @param eps Threshold of convergence
#'
#' @return a list with elements:
#' \itemize{
#' \item {p.nnls: vector of cell type proportions estimated by nnls (add up to 1);}
#' \item {q.nnls: vector of original estimation from nnls;}
#' \item {fit.nnls: fitted value of Y from nnls;}
#' \item {resid.nnls: residual value from nnls;}
#' \item {p.weight: vector of cell type proportions estimated by weighted-nnls (add up to 1);}
#' \item {q.weight: vector of original estimation from weight-nnls;}
#' \item {fit.weight: fitted value of Y from weighted-nnls;}
#' \item {resid.weight: residual value from weighted-nnls;}
#' \item {weight.gene: weight calculated from weighted-nnls;}
#' \item {converge: 'Reach Maxiter', 'Converge at m';}
#' \item {rsd: residual value from weighted-nnls transformed by weight;}
#' \item {R.squared: R square of weighted-nnls;}
#' \item {var.p: variance of weighted-nnls estimated cell type proportions;}
#'}
#' @export
#' @importFrom nnls nnls
music.basic = function(Y, X, S, Sigma, iter.max, nu, eps){
k = ncol(X)
lm.D = nnls(X, Y)
r = resid(lm.D);
weight.gene = 1/(nu + r^2 + colSums( (lm.D$x*S)^2*t(Sigma) ))
Y.weight = Y*sqrt(weight.gene)
D.weight = sweep(X, 1, sqrt(weight.gene), '*')
lm.D.weight = nnls(D.weight, Y.weight)
p.weight = lm.D.weight$x/sum(lm.D.weight$x)
p.weight.iter = p.weight
r = resid(lm.D.weight)
for(iter in 1:iter.max){
weight.gene = 1/(nu + r^2 + colSums( (lm.D.weight$x*S)^2*t(Sigma) ))
Y.weight = Y*sqrt(weight.gene)
D.weight = X * as.matrix(sqrt(weight.gene))[,rep(1,k)]
lm.D.weight = nnls(D.weight, Y.weight )
p.weight.new = lm.D.weight$x/sum(lm.D.weight$x)
r.new = resid(lm.D.weight)
if(sum(abs(p.weight.new - p.weight)) < eps){
p.weight = p.weight.new;
r = r.new
R.squared = 1 - var(Y - X%*%as.matrix(lm.D.weight$x))/var(Y)
fitted = X%*%as.matrix(lm.D.weight$x)
var.p = diag(solve(t(D.weight)%*%D.weight)) * mean(r^2)/sum(lm.D.weight$x)^2
return(list(p.nnls = (lm.D$x)/sum(lm.D$x), q.nnls = lm.D$x, fit.nnls = fitted(lm.D), resid.nnls = resid(lm.D),
p.weight = p.weight, q.weight = lm.D.weight$x, fit.weight = fitted, resid.weight = Y - X%*%as.matrix(lm.D.weight$x),
weight.gene = weight.gene, converge = paste0('Converge at ', iter),
rsd = r, R.squared = R.squared, var.p = var.p));
}
p.weight = p.weight.new;
r = r.new;
}
fitted = X%*%as.matrix(lm.D.weight$x)
R.squared = 1 - var(Y - X%*%as.matrix(lm.D.weight$x))/var(Y)
var.p = diag(solve(t(D.weight)%*%D.weight)) * mean(r^2)/sum(lm.D.weight$x)^2
return(list(p.nnls = (lm.D$x)/sum(lm.D$x), q.nnls = lm.D$x, fit.nnls = fitted(lm.D), resid.nnls = resid(lm.D),
p.weight = p.weight, q.weight = lm.D.weight$x, fit.weight = fitted, resid.weight = Y - X%*%as.matrix(lm.D.weight$x),
weight.gene = weight.gene, converge = 'Reach Maxiter', rsd = r,
R.squared = R.squared, var.p = var.p))
}
#' Scaling bulk data and signature matrix and estimate cell type proportion
#'
#' @param Y vector of bulk tissue expression
#' @param D matrix, Signature matrix
#' @param S vector of Avg. Library size
#' @param Sigma matrix of subject level variation (gene * cell type)
#' @param iter.max numeric, maximum iteration number
#' @param nu regulation parameter, when take reciprocal
#' @param eps threshold of convergence
#' @param centered logic, subtract avg of Y and D
#' @param normalize logic, divide Y and D by their standard deviation
#'
#'
#' @return a list same as nnls.weight.basic
#' @export
music.iter = function(Y, D, S, Sigma, iter.max = 1000, nu = 0.0001, eps = 0.01, centered = FALSE, normalize = FALSE){
if(length(S)!=ncol(D)){
common.cell.type = intersect(colnames(D), names(S))
if(length(common.cell.type)<=1){
stop('Not enough cell types!')
}
D = D[,match(common.cell.type, colnames(D))]
S = S[match(common.cell.type, names(S))]
}
if(ncol(Sigma) != ncol(D)){
common.cell.type = intersect(colnames(D), colnames(Sigma))
if(length(common.cell.type)<=1){
stop('Not enough cell type!')
}
D = D[, match(common.cell.type, colnames(D))]
Sigma = Sigma[, match(common.cell.type, colnames(Sigma))]
S = S[match(common.cell.type, names(S))]
}
k = ncol(D); # number of cell types
common.gene = intersect(names(Y), rownames(D))
if(length(common.gene)< 0.1*min(length(Y), nrow(D))){
stop('Not enough common genes!')
}
Y = Y[match(common.gene, names(Y))];
D = D[match(common.gene, rownames(D)), ]
Sigma = Sigma[match(common.gene, rownames(Sigma)), ]
X = D
## First, no intercept and no normalization
if(centered){
X = X - mean(X)
Y = Y - mean(Y)
}
if(normalize){
X = X/sd(as.vector(X));
S = S*sd(as.vector(X));
Y = Y/sd(Y)
}else{
Y = Y*100
}
lm.D = music.basic(Y, X, S, Sigma, iter.max = iter.max, nu = nu, eps = eps)
return(lm.D)
}
#' Calculate weight with cross cell type covariance
#'
#' @param Sp vector, library size of each cell type
#' @param Sigma.ct matrix, cell type^2 by genes. Each columns store the cross cell-type covariance matrix of a gene.
#'
#' @return vector of weight
#'
#' @export
weight.cal.ct = function(Sp, Sigma.ct){
nGenes = ncol(Sigma.ct); n.ct = length(Sp);
weight = sapply(1:nGenes, function(g){
sum(Sp%*%t(Sp)*matrix(Sigma.ct[,g], n.ct))
})
return(weight)
}
#' Calculate weight with cross-subject variance for each cell types
#'
#' @param p vector, cell type proportions
#' @param M.S vector of average library size for each cell type
#' @param Sigma gene by cell type matrix. Each entry is the cross-subject variance.
#'
#' @return vector of weight
#'
#' @export
Weight_cal <- function(p, M.S, Sigma){
Sigma%*%(M.S * t(p))^2
}
#' Estimate cell type proportion with MuSiC and NNLS
#'
#' weight is estimated with cell type covariance
#'
#' @param Y vector of bulk tissue expression
#' @param X matrix, Signature matrix
#' @param S vector of Avg. Library size
#' @param Sigma.ct matrix of Subject level variation with
#' @param iter.max numeric, maximum iteration number
#' @param nu regulation parameter, take care of weight when taking reciprocal
#' @param eps Threshold of convergence
#'
#'
#' @return a list with elements:
#' \itemize{
#' \item {p.nnls: vector of cell type proportions estimated by nnls (add up to 1);}
#' \item {q.nnls: vector of original estimation from nnls;}
#' \item {fit.nnls: fitted value of Y from nnls;}
#' \item {resid.nnls: residual value from nnls;}
#' \item {p.weight: vector of cell type proportions estimated by weighted-nnls (add up to 1);}
#' \item {q.weight: vector of original estimation from weight-nnls;}
#' \item {fit.weight: fitted value of Y from weighted-nnls;}
#' \item {resid.weight: residual value from weighted-nnls;}
#' \item {weight.gene: weight calculated from weighted-nnls;}
#' \item {converge: 'Reach Maxiter', 'Converge at m';}
#' \item {rsd: residual value from weighted-nnls transfromed by weight;}
#' \item {R.squared: R square of weighted-nnls:}
#' \item {var.p: variance of weighted-nnls estimated cell type proportions.}
#' }
#' @export
#' @importFrom nnls nnls
music.basic.ct = function(Y, X, S, Sigma.ct, iter.max, nu, eps){
k = ncol(X)
lm.D = nnls(X, Y)
r = resid(lm.D);
weight.gene = 1/(nu + r^2 + weight.cal.ct(lm.D$x*S, Sigma.ct))
Y.weight = Y*sqrt(weight.gene)
D.weight = sweep(X, 1, sqrt(weight.gene), '*')
lm.D.weight = nnls(D.weight, Y.weight)
p.weight = lm.D.weight$x/sum(lm.D.weight$x)
p.weight.iter = p.weight
r = resid(lm.D.weight)
for(iter in 1:iter.max){
weight.gene = 1/(nu + r^2 + weight.cal.ct(lm.D$x*S, Sigma.ct))
Y.weight = Y*sqrt(weight.gene)
D.weight = X * as.matrix(sqrt(weight.gene))[,rep(1,k)]
lm.D.weight = nnls(D.weight, Y.weight )
p.weight.new = lm.D.weight$x/sum(lm.D.weight$x)
r.new = resid(lm.D.weight)
if(sum(abs(p.weight.new - p.weight)) < eps){
p.weight = p.weight.new;
r = r.new
R.squared = 1 - var(Y - X%*%as.matrix(lm.D.weight$x))/var(Y)
fitted = X%*%as.matrix(lm.D.weight$x)
var.p = diag(solve(t(D.weight)%*%D.weight)) * mean(r^2)/sum(lm.D.weight$x)^2
return(list(p.nnls = (lm.D$x)/sum(lm.D$x), q.nnls = lm.D$x, fit.nnls = fitted(lm.D), resid.nnls = resid(lm.D),
p.weight = p.weight, q.weight = lm.D.weight$x, fit.weight = fitted, resid.weight = Y - X%*%as.matrix(lm.D.weight$x),
weight.gene = weight.gene, converge = paste0('Converge at ', iter),
rsd = r, R.squared = R.squared, var.p = var.p));
break;
}
p.weight = p.weight.new;
r = r.new;
}
fitted = X%*%as.matrix(lm.D.weight$x)
R.squared = 1 - var(Y - X%*%as.matrix(lm.D.weight$x))/var(Y)
var.p = diag(solve(t(D.weight)%*%D.weight)) * mean(r^2)/sum(lm.D.weight$x)^2
return(list(p.nnls = (lm.D$x)/sum(lm.D$x), q.nnls = lm.D$x, fit.nnls = fitted(lm.D), resid.nnls = resid(lm.D),
p.weight = p.weight, q.weight = lm.D.weight$x, fit.weight = fitted, resid.weight = Y - X%*%as.matrix(lm.D.weight$x),
weight.gene = weight.gene, converge = 'Reach Maxiter', rsd = r,
R.squared = R.squared, var.p = var.p))
}
#' Scaling bulk data and signature matrix and estimate cell type proportion
#'
#' @inheritParams music.basic.ct
#'
#' @param Y vector of bulk tissue expression
#' @param X matrix, Signature matrix
#' @param S vector of Avg. Library size
#' @param Sigma.ct matrix of Subject level variation with cross-cell-type covariance.
#' @param iter.max numeric, maximum iteration number. Default is 1000
#' @param nu regulation parameter, take care of weight when taking recipical
#' by default nu = 0.0001.
#' @param eps Threshold of convergence. Default is 0.01.
#' @param centered logic, subtract avg of Y and D.
#' Default is FALSE.
#' @param normalize logic, divide Y and D by their standard deviation.
#' Default is FALSE
#'
#' @return a list same as nnls.weight.basic
#' @export
music.iter.ct = function(Y, D, S, Sigma.ct, iter.max = 1000, nu = 0.0001, eps = 0.01, centered = FALSE, normalize = FALSE){
if(length(S)!=ncol(D)){
common.cell.type = intersect(colnames(D), names(S))
if(length(common.cell.type)<=1){
stop('Not enough cell types!')
}
D = D[,match(common.cell.type, colnames(D))]
S = S[match(common.cell.type, names(S))]
}
k = ncol(D); # number of cell types
common.gene = intersect(names(Y), rownames(D))
common.gene = intersect(common.gene, colnames(Sigma.ct))
if(length(common.gene)< 0.1*min(length(Y), nrow(D), ncol(Sigma.ct))){
stop('Not enough common genes!')
}
Y = Y[match(common.gene, names(Y))];
D = D[match(common.gene, rownames(D)), ]
Sigma.ct = Sigma.ct[, match(common.gene, colnames(Sigma.ct))]
X = D
## First, no intercept and no normalization
if(centered){
X = X - mean(X)
Y = Y - mean(Y)
}
if(normalize){
X = X/sd(as.vector(X));
S = S*sd(as.vector(X));
Y = Y/sd(Y)
}else{
Y = Y*100
}
lm.D = music.basic.ct(Y, X, S, Sigma.ct, iter.max = iter.max, nu = nu, eps = eps)
return(lm.D)
}