-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpacnet_utils.R
More file actions
386 lines (337 loc) · 15.6 KB
/
pacnet_utils.R
File metadata and controls
386 lines (337 loc) · 15.6 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#Adaptation to ccn_hmClass to prevent re-ordering the groups by name
acn_queryClassHm <- function (classMat, grps = NULL, isBig = FALSE, cRow = FALSE,
cCol = FALSE, fontsize_row = 4, fontsize_col = 4, main = NA,
scale = FALSE, customAnnoColor = NULL, ...)
{
cools <- colorRampPalette(c("black", "limegreen", "yellow"))(100)
bcol <- "white"
if (isBig) {
bcol <- NA
} else {
bcol <- "grey60"
}
if (is.null(grps)) {
if (scale) {
mymin <- min(classMat)
mymax <- max(classMat)
}
else {
mymin <- 0
mymax <- 1
}
return(pheatmap::pheatmap(classMat, col = cools, breaks = seq(from = mymin,
to = mymax, length.out = 100), cluster_rows = cRow,
cluster_cols = cCol, main = main, fontsize_row = fontsize_row, border_color=bcol,
fontsize_col = fontsize_col, ...))
}
else {
#grps <- grps[order(grps)]
cells <- names(grps)
#groupNames <- sort(unique(grps))
groupNames <- unique(grps)
xcol <- colorRampPalette(rev(brewer.pal(n = 12, name = "Set3")))(length(groupNames))
names(xcol) <- groupNames
anno_colors <- list(group = xcol)
xx <- data.frame(group = as.factor(grps))
rownames(xx) <- cells
if (scale) {
mymin <- min(classMat)
mymax <- max(classMat)
}
else {
mymin <- 0
mymax <- 1
}
if (is.null(customAnnoColor) == FALSE) {
if (!all(groupNames %in% names(customAnnoColor))) {
stop(paste0("Not all group name has a color in custom color vetor.",
"\n"))
}
customAnnoColor <- customAnnoColor[groupNames]
names(customAnnoColor) <- groupNames
anno_colors <- list(group = customAnnoColor)
xx <- data.frame(group = as.character(grps))
rownames(xx) <- cells
}
return(pheatmap::pheatmap(classMat, col = cools, breaks = seq(from = mymin,
to = mymax, length.out = 100), cluster_rows = cRow,
cluster_cols = cCol, show_colnames = FALSE, annotation_names_row = FALSE,
annotation_col = xx, main = main, annotation_names_col = FALSE,
annotation_colors = anno_colors, fontsize_row = fontsize_row, border_color=bcol,
fontsize_col = fontsize_col, ...))
}
}
heatmapPlotly <- function(c_scoresMatrix) {
melted_tab = data.frame("classificationScore" = NULL,
"sampleName" = NULL,
"tissueType" = NULL)
for(sampleName in colnames(c_scoresMatrix)) {
temp_cscore = c_scoresMatrix[, sampleName]
tempTab = data.frame("classificationScore" = temp_cscore,
"sampleName" = sampleName,
"tissueType" = rownames(c_scoresMatrix))
melted_tab = rbind(melted_tab, tempTab)
}
melted_tab$tissueType = factor(x = melted_tab$tissueType,levels = rev(levels(melted_tab$tissueType)))
cools<-colorRampPalette(c("black", "limegreen", "yellow"))( 100 )
p <- ggplot(data = melted_tab, aes(sampleName, tissueType, fill = classificationScore))+
geom_tile(color = "white")+
scale_fill_gradient2(low = cools[1], high = cools[length(cools)], mid = cools[length(cools)/2],
midpoint = 0.5, limit = c(0,1), space = "Lab",
name="Classification Score") +
xlab("Query Samples")+
ylab("Tissue Types")+
theme_minimal()+
theme(axis.text.x = element_text(angle = 45, vjust = 1,
size = 12, hjust = 1))
return(ggplotly(p))
}
heatmapPlotlyRef <- function(c_scoresMatrix, sampTab) {
melted_tab = data.frame("classificationScore" = NULL,
"sampleName" = NULL,
"citation" = NULL,
"tissueType" = NULL,
"description" = NULL)
for(sampleName in colnames(c_scoresMatrix)) {
temp_cscore = c_scoresMatrix[, sampleName]
samp_citation <- sampTab[sampleName, "citation"]
samp_descrip <- sampTab[sampleName, "description1"]
tempTab = data.frame("classificationScore" = temp_cscore,
"sampleName" = sampleName,
"citation" = samp_citation,
"tissueType" = rownames(c_scoresMatrix),
"description" = samp_descrip
)
melted_tab = rbind(melted_tab, tempTab)
}
melted_tab$citation <- as.character(melted_tab$citation)
melted_tab[which(is.na(melted_tab$citation)),"citation"] <- "random"
melted_tab$citation <- as.character(lapply(melted_tab$citation,
FUN = function(descrip){
return(paste(strwrap(descrip, width=15), collapse="\n"))
}))
melted_tab$description <- as.character(melted_tab$description)
melted_tab[which(is.na(melted_tab$description)),"description"] <- "random"
melted_tab$tissueType <- factor(melted_tab$tissueType,
levels = sort(unique(melted_tab$tissueType), decreasing=TRUE))
cools<-colorRampPalette(c("black", "limegreen", "yellow"))( 100 )
if (length(unique(melted_tab$citation)) >= 15) {
col_num <- 5
} else if (length(unique(melted_tab$citation)) >= 10) {
col_num <- 3
} else {
col_num <- 2
}
p <- ggplot(data = melted_tab, aes(sampleName, tissueType, fill = classificationScore, text=description))+
geom_tile(color = "white") +
#facet_grid(.~citation, scales="free_x") +
facet_wrap(~citation, scales="free_x", ncol=col_num) +
#facet_wrap(~citation, scales="free_x") +
scale_fill_gradient2(low = cools[1], high = cools[length(cools)], mid = cools[length(cools)/2],
midpoint = 0.5, limit = c(0,1), space = "Lab",
name="Classification Score") +
xlab("Samples")+
ylab("Tissue Types")+
theme_minimal()+
theme(axis.text.x=element_blank()) +
theme(axis.title.y=element_blank()) +
theme(panel.spacing = unit(0.5, "lines"))
ggplotly(p, tooltip="all") %>% layout(margin=c(0,0,0.02,0.01))
}
heatmapRef <- function(c_scoresMatrix, sampTab) {
melted_tab = data.frame("classificationScore" = NULL,
"sampleName" = NULL,
"citation" = NULL,
"tissueType" = NULL,
"description" = NULL)
for(sampleName in colnames(c_scoresMatrix)) {
temp_cscore = c_scoresMatrix[, sampleName]
samp_citation <- sampTab[sampleName, "citation"]
samp_descrip <- sampTab[sampleName, "description1"]
tempTab = data.frame("classificationScore" = temp_cscore,
"sampleName" = sampleName,
"citation" = samp_citation,
"tissueType" = rownames(c_scoresMatrix),
"description" = samp_descrip
)
melted_tab = rbind(melted_tab, tempTab)
}
melted_tab$citation <- as.character(melted_tab$citation)
melted_tab[which(is.na(melted_tab$citation)),"citation"] <- "random"
melted_tab$citation <- as.character(lapply(melted_tab$citation,
FUN = function(descrip){
return(paste(strwrap(descrip, width=15), collapse="\n"))
}))
melted_tab$description <- as.character(melted_tab$description)
melted_tab[which(is.na(melted_tab$description)),"description"] <- "random"
melted_tab$tissueType <- factor(melted_tab$tissueType,
levels = sort(unique(melted_tab$tissueType), decreasing=TRUE))
cools<-colorRampPalette(c("black", "limegreen", "yellow"))( 100 )
p <- ggplot(data = melted_tab, aes(sampleName, tissueType, fill = classificationScore, text=description))+
geom_tile(color = "white") +
facet_wrap(~citation, scales="free_x") +
scale_fill_gradient2(low = cools[1], high = cools[length(cools)], mid = cools[length(cools)/2],
midpoint = 0.5, limit = c(0,1), space = "Lab",
name="Classification Score") +
xlab("Samples")+
ylab("Tissue Types")+
theme_minimal()+
theme(axis.text.x=element_blank()) +
theme(axis.title.y=element_blank()) +
theme(panel.spacing = unit(0.5, "lines"))
return(p)
}
subsetGRNall <- function(grnAll, iGenes) {
# Subset grnTable based on iGenes
allTargets <- grnAll$overallGRN$grnTable$TG
newGRNTable <- grnAll$overallGRN$grnTable[which(allTargets %in% iGenes),]
newTFsAll <- newGRNTable$TF
newGRNTable <- newGRNTable[which(newTFsAll %in% iGenes),]
grnAll$overallGRN$grnTable <- newGRNTable
## Subset overallGRN graph based on iGenes
vertex_names <- V(grnAll$overallGRN$graph)$name
graph_iGenes <- which(vertex_names %in% iGenes)
newGraph <- induced_subgraph(graph=grnAll$overallGRN$graph, vids=graph_iGenes, impl="copy_and_delete")
grnAll$overallGRN$graph <- newGraph
# Subset specGenes based on iGenes and tissue type
tissueTypes <- names(grnAll$specGenes$context$general)
newGeneral <- grnAll$specGenes$context$general
for (tissueType in tissueTypes) {
tissueSpecGenes <- newGeneral[[tissueType]]
tissueSpecGenes <- tissueSpecGenes[which(names(tissueSpecGenes) %in% iGenes)]
newGeneral[[tissueType]] <- tissueSpecGenes
}
grnAll$specGenes$context$general <- newGeneral
# Subset ctGRN geneLists, graphLists, and tfTargets based on iGenes and tissue type
grnAll$ctGRNs$geneLists <- newGeneral
newGraphLists <- grnAll$ctGRNs$graphLists
for (tissueType in tissueTypes) {
tissueGRN <- newGraphLists[[tissueType]]
iVertices <- vertex_attr(tissueGRN, name="name")
iVertices <- iVertices[which(iVertices %in% iGenes)]
tissueGRN <- induced_subgraph(graph=tissueGRN, vids=iVertices, impl="copy_and_delete")
newGraphLists[[tissueType]] <- tissueGRN
}
grnAll$ctGRNs$graphLists <- newGraphLists
newTFTargets <- grnAll$ctGRNs$tfTargets
for (tissueType in tissueTypes) {
tissueTFTargets <- newTFTargets[[tissueType]]
tissueTFTargets <- tissueTFTargets[which(names(tissueTFTargets) %in% iGenes)]
for (TF in names(tissueTFTargets)) {
newTargets <- tissueTFTargets[[TF]]
newTargets <- newTargets[which(newTargets %in% iGenes)]
tissueTFTargets[[TF]] <- newTargets
}
newTFTargets[[tissueType]] <- tissueTFTargets
}
grnAll$ctGRNs$tfTargets <- newTFTargets
return(grnAll)
}
subsetTrainNormParam <- function(trainNormParam, grnAll, iGenes) {
tissueTypes <- names(grnAll$specGenes$context$general)
newTVals <- trainNormParam$tVals
for (tissueType in tissueTypes) {
newIndices <- which(names(newTVals[[tissueType]][["mean"]]) %in% iGenes)
newTVals[[tissueType]][["mean"]] <- newTVals[[tissueType]][["mean"]][newIndices]
newTVals[[tissueType]][["sd"]] <- newTVals[[tissueType]][["sd"]][newIndices]
}
trainNormParam$tVals <- newTVals
return(trainNormParam)
}
### compute zscore
zscore<-function(x,### numeric vector
meanVal, ### mean of distribution to compute zscore of x against
sdVal ### standard deviation of distribution to compute zscore of x agains
){
(x-meanVal)/sdVal;
### zscore
}
### Compute the mean zscore of given genes in each sample
cn_zscoreVect<-function(genes, ### genes
xvals, ### named vector
tVals, ### tvals
ctt ### ctt
){
ans<-vector();
for(gene in genes){
ans<-append(ans, zscore(xvals[gene], tVals[[ctt]][['mean']][[gene]], tVals[[ctt]][['sd']][[gene]]));
}
ans;
### zscore vector
}
#' network influence score
#'
#' Computes network influence score (NIS).
#' @param expDat ranked query expression matrix, output of running logRank()
#' @param stQuery query sample metadata table
#' @param iGenes character vector of gene names found in both training data and query data
#' @param grnAll GRN object, output of running ccn_makeGRN()
#' @param trainNormParam normalization parameters, output of running ccn_trainNorm()
#' @param subnet name of cell/tissue type subnetwork to evaluate
#' @param ctt string indicating the cell/tissue type to compare against
#' @param colname_sid colname of stQuery with unique sample labels
#' @param relaWeight whether to weight by overall expression such that TFs with higher expression in ctt are more important (1=do the weighting)
#'
#' @return numeric matrix where rows are TFs in CT GRN and columns are query samples. Negative score indicates TF should be upregulated to acquire a ctt fate. Positive score indicates TF should be downregulated to acquire a ctt fate.
pacnet_nis <- function(expDat, stQuery, iGenes, grnAll, trainNormParam, subnet, ctt, colname_sid="sra_id", relaWeight=0) {
tfTargList<-grnAll$ctGRNs$tfTargets
nTargets<-vector();
targetScore<-vector();
tfScore<-vector();
totalScore<-vector();
tfWeights<-vector();
tfs<-names(tfTargList[[subnet]]);
netGenes<-names(grnAll$ctGRNs$geneLists[[subnet]])
netGenes<-intersect(netGenes, iGenes)
sids<-as.vector(stQuery[,colname_sid])
ans<-matrix(0, nrow=length(tfs), ncol=nrow(stQuery))
rownames(ans)<-tfs;
colnames(ans)<-sids;
tVals <- trainNormParam[["tVals"]]
# compute a matrix of zscores.
zzzMat<-matrix(0, nrow=length(netGenes), ncol=nrow(stQuery));
for(i in seq(length(sids))){
sid<-sids[i];
print(paste0("Computing zscores for ", sid))
xvals<-as.vector(expDat[netGenes, sid])
names(xvals)<-netGenes
zzzMat[,i]<-cn_zscoreVect(netGenes, xvals, tVals, ctt)
}
rownames(zzzMat)<-netGenes
colnames(zzzMat)<-stQuery[,colname_sid]
for(sid in sids) {
print(paste0("TF scoring for ", sid));
xvals<-as.vector(expDat[,sid]);
names(xvals)<-rownames(expDat);
# assign weights
### # meanVect<-unlist(tVals[[ctt]][['mean']][netGenes]);
meanVect<-unlist(tVals[[subnet]][['mean']][netGenes]);
meanVect <- meanVect / min(meanVect) # Added to make function work with rank-based tVals
weights<-(2**meanVect)/sum(2**meanVect);
for(i in seq(length(tfs))){
tf<-tfs[i];
# zscore of TF relative to target C/T
## tfScore[i]<-zscore(xvals[tf], tVals[[ctt]][['mean']][[tf]], tVals[[ctt]][['sd']][[tf]]);
tfScore[i]<-zzzMat[tf,sid];
targs<-tfTargList[[subnet]][[tf]];
targs<-intersect(targs, iGenes);
# Zscores of TF targets, relative to C/T
## tmp<-cn_zscoreVect(targs, xvals, tVals, ctt );
tmp<-zzzMat[targs,sid];
targetScore[i]<-sum(tmp*weights[targs]);
## new one:
totalScore[i]<-targetScore[i] + (length(targs)*tfScore[i]*weights[tf]);
if(relaWeight!=1){ # don't weight by expression
meanW<-mean(weights)
totalScore[i]<- sum(tmp)*meanW + (length(targs)*tfScore[i])*meanW
}
nTargets[i]<-length(targs) ;
tfWeights[i]<-weights[tf];
}
xxx<-data.frame(tf=tfs, tfScore=tfScore, targetScore=targetScore, nTargets=nTargets,tfWeight=tfWeights, totalScore=totalScore);
xxx<-xxx[order(xxx$totalScore),]; # puts the worst ones at top when plotting
xxx$tf<-factor(xxx$tf, as.vector(unique(xxx$tf)));
ans[as.vector(xxx$tf),sid]<-as.vector(xxx$totalScore);
}
return(ans) # returns network influence score.
}