forked from hd2326/BiologicalProcessActivity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColorGradient.R
68 lines (64 loc) · 2.07 KB
/
ColorGradient.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
expColor <- function(gene, exp, col = c("Grey", "Pink", "Red")){
ind <- match(gene, rownames(exp))
ind <- ind[!is.na(ind)]
if (length(ind) == 0){
c <- rep(col[1], ncol(exp))
}else{
if (length(ind) == 1){
c <- exp[ind, ]
}else c <- colMeans(exp[ind, ])
if (sum(c) > 0){
c <- floor(((c-min(c))/(max(c)-min(c)))*100)+1
c <- colorRampPalette(col)(102)[c]
}else c <- rep(col[1], ncol(exp))
}
return(c)
}
#expression color gradient
vpColor <- function(reg, vp, col = c("Blue", "Grey", "Red"), range = 5){
ind <- match(reg, rownames(vp))
ind <- ind[!is.na(ind)]
if (length(ind) == 0){
c <- rep(col[2], ncol(vp))
}else{
if (length(ind) == 1){
c <- vp[ind, ]
}else c <- colMeans(vp[ind, ])
c[abs(c) > range] <- range * sign(c)[abs(c) > range]
c <- sign(c)*(abs(c)/range)*100
c <- sapply(c, function(x, col){
if (x > 0) colorRampPalette(col[c(2, 3)])(102)[floor(x)+1]
else colorRampPalette(col[c(2, 1)])(102)[floor(abs(x))+1]
}, col=col)
}
return(c)
}
#activity color gradient
nesColor <- function(set, nes, col = c("Blue", "Grey", "Red"), range = 5){
ind <- match(set, rownames(nes))
if (!is.na(ind)){
c <- nes[ind, ]
c[abs(c) > range] <- range * sign(c)[abs(c) > range]
c <- sign(c)*(abs(c)/range)*100
c <- sapply(c, function(x, col){
if (x > 0) colorRampPalette(col[c(2, 3)])(102)[floor(x)+1]
else colorRampPalette(col[c(2, 1)])(102)[floor(abs(x))+1]
}, col=col)
}else c <- rep(col[2], ncol(nes))
return(c)
}
#nES color gradient
geneColor <- function(x){
col <- colSums(x > 0)
col <- floor(((col-min(col))/(max(col)-min(col)))*1000)+1
col <- colorRampPalette(c("grey", "red"))(1000)[col]
return(col)
}
#gene number color gradient
countColor <- function(count){
col <- log2(colSums(count))
col <- floor(((col-min(col))/(max(col)-min(col)))*1000)+1
col <- colorRampPalette(c("grey", "red"))(1000)[col]
return(col)
}
#count number color gradient