-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathngs_R_function.R
224 lines (185 loc) · 9.29 KB
/
ngs_R_function.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
##################################################################################################################################
# Data 2019-08-03
# Author Howard MENG
# E-mail [email protected]
# useful R function
##################################################################################################################################
#------------------------------------------------------------------------------------------>>>>>>>
# contents
#------------------------------------------------------------------------------------------>>>>>>>
# plot.TAD function for plot Hi-C TAD
# plot.matrix easy to plot heatmap
#------------------------------------------------------------------------------------------>>>>>>>
# function part
#------------------------------------------------------------------------------------------>>>>>>>
plot.TAD <- function(input.matrix,col.min = "red",col.max = "red",col.boundary = 0,ylim=NULL,minBound=0,maxBound=0.95,mat.upper=T,mat.part=T){
# 参数说明
## input.matrix: 输入矩阵
## col.min="red": 热图颜色,支持字符或者是RGB参数za
## col.max="red": 热图颜色,支持字符或者是RGB参数
## col.boundary=0: 两种颜色的分界线,也即白色对应的值
## ylim=NULL: 热图的y轴范围,1个bin的长度是1,方便截短
## minBound=0: 使用分位数来修正最小值
## maxBound=0.95: 使用分位数修正最大值,默认matrix中的95%分位数为最大值
## mat.upper=T: 如果为TRUE画上半部分,为FALSE画下半部分; 只有在mat.part=T时有效
## mat.part=T: 如果为TRUE则画三角,如果为FLASE则画倒置的matrix
input.matrix = as.matrix(input.matrix)
mat.nrow = dim(input.matrix)[1]
## color type 1:single color
## color type 2:blue white red black
## 生成矩阵需要的颜色
mat.lower.tri = as.vector(input.matrix[lower.tri(input.matrix,diag = F)])
mat.quantile = quantile(as.vector(input.matrix[lower.tri(input.matrix,diag = T)]),prob=c(minBound,maxBound))
if(col.boundary>mat.quantile[2] | col.boundary<mat.quantile[1]){
print("Error! col.boundary have to smaller than matrix maxBound value!")
return(NULL)
}
## fix value,too large or too small
mat.lower.tri[mat.lower.tri < mat.quantile[1]] = mat.quantile[1]
mat.lower.tri[mat.lower.tri > mat.quantile[2]] = mat.quantile[2]
mat.diagnal.value = diag(input.matrix)
mat.diagnal.value[mat.diagnal.value < mat.quantile[1]] = mat.quantile[1]
mat.diagnal.value[mat.diagnal.value > mat.quantile[2]] = mat.quantile[2]
## create color vector
mat.lower.tri.col_alpha = rep(0,length(mat.lower.tri))
mat.diagnal.col_alpha = rep(0,length(mat.diagnal.value))
mat.lower.tri.col_alpha[mat.lower.tri>=col.boundary] = ceiling((mat.lower.tri[mat.lower.tri>=col.boundary] - col.boundary) / (mat.quantile[2] - col.boundary) * 255)
mat.lower.tri.col_alpha[mat.lower.tri<col.boundary] = ceiling((col.boundary - mat.lower.tri[mat.lower.tri<col.boundary]) / (col.boundary - mat.quantile[1]) * 255)
mat.diagnal.col_alpha[mat.diagnal.value>=col.boundary] = ceiling((mat.diagnal.value[mat.diagnal.value>=col.boundary] - col.boundary) / (mat.quantile[2] - col.boundary) * 255)
mat.diagnal.col_alpha[mat.diagnal.value<col.boundary] = ceiling((col.boundary - mat.diagnal.value[mat.diagnal.value<col.boundary]) / (col.boundary - mat.quantile[1]) * 255)
mat.lower.tri.color = rep("##FFFFFF",length(mat.lower.tri))
mat.diagnal.color = rep("##FFFFFF",length(mat.diagnal.value))
mat.lower.tri.color[mat.lower.tri>=col.boundary] = rgb(t(col2rgb(col.max)),alpha = mat.lower.tri.col_alpha[mat.lower.tri>=col.boundary],maxColorValue = 255)
mat.lower.tri.color[mat.lower.tri<col.boundary] = rgb(t(col2rgb(col.min)),alpha = mat.lower.tri.col_alpha[mat.lower.tri<col.boundary],maxColorValue = 255)
mat.diagnal.color[mat.diagnal.value>=col.boundary] = rgb(t(col2rgb(col.max)),alpha = mat.diagnal.col_alpha[mat.diagnal.value>=col.boundary],maxColorValue = 255)
mat.diagnal.color[mat.diagnal.value<col.boundary] = rgb(t(col2rgb(col.min)),alpha = mat.diagnal.col_alpha[mat.diagnal.value<col.boundary],maxColorValue = 255)
# 根据矩阵的行列,生成x1,y1的坐标
x1.part = c(1:(mat.nrow -1))
y1.part = c(1:(mat.nrow -1))
x1 = x1.part
y1 = y1.part
for(i in c(1:(mat.nrow-1))){
x1.part = x1.part[-length(x1.part)] + 2
y1.part = y1.part[-length(y1.part)]
x1 = c(x1,x1.part)
y1 = c(y1,y1.part)
}
# 对角线的x1
x1.diagonal = c(0:(mat.nrow-1)) * 2
y1.diagonal = rep(0,mat.nrow)
if(mat.part==T){
# plot matrix upper OR lower only.
x1 = c(x1,x1.diagonal)
y1 = c(y1,y1.diagonal)
# 颜色向量也不同
mat.color = c(mat.lower.tri.color,mat.diagnal.color)
}else if(mat.part==F){
x1 = c(x1,x1.diagonal,x1)
y1 = c(y1,y1.diagonal,-y1)
mat.color = c(mat.lower.tri.color,mat.diagnal.color,mat.lower.tri.color)
}
# 根据x1 y1 生成剩余坐标
x2 = x1 + 1
x3 = x2 + 1
x4 = x2
y2 = y1 + 1
y3 = y1
y4 = y1 - 1
# 需要按照x1,x2,x3,x4,NA的格式进行生成x vector否则会把所有的点连在一起
NA_vector = rep(NA,length(x1))
# 生成按照x11,x12,x13,x14,NA,x21,x22,x23,x24,NA...排列的x与y
x_matrix = matrix(c(x1,x2,x3,x4,NA_vector),ncol = 5)
y_matrix = matrix(c(y1,y2,y3,y4,NA_vector),ncol = 5)
x = as.vector(t(x_matrix))
y = as.vector(t(y_matrix))
# 绘图部分
## 如果mat.upper 为F 则画倒置的图像
## 设置画布大小
x.point = c(0,mat.nrow*2)
if(mat.part){
if(! mat.upper){ y = -y ; y.point = c(-mat.nrow,1)}else{ y.point = c(-1,mat.nrow) }
}else{
y.point = c(-mat.nrow,mat.nrow)
}
## 设置ylim
if(is.null(ylim)){ ylim = y.point }
## 生成画布
plot(x.point,y.point,ylim=ylim,type="n",frame.plot =F,xaxt="n",yaxt="n",xlab="",ylab="")
## 画三角矩阵
polygon(x,y,col = mat.color,border = F)
}
plot.matrix <- function(mat,bound.min=0,bound.max=1,mat.lim=NULL,color_type=1,col.min = "red",col.max = "red",col.boundary = NULL){
# mat = hic matrix
# min_bound min quantile to miss data
# max_bound max quantile to miss data
mat <- as.matrix(mat)
#matrix info calculate
row_num <- dim(mat)[1]
col_num <- dim(mat)[2]
#matrix rects' coordinate
x1 <- rep(c(0:(col_num-1)),each=row_num)
x2 <- x1 + 1
y1 <- rep(c(0:(-row_num+1)),col_num)
y2 <- y1 -1
#matrix colour vector
if(color_type == 1){
## 生成矩阵需要的颜色
###确定 matrix的上下界
mat.quantile = quantile(as.vector(mat),prob=c(bound.min,bound.max))
if(is.null(mat.lim)){
mat.lim = mat.quantile
}else{
mat.lim = c(min(c(mat.quantile,mat.lim)),max(c(mat.quantile,mat.lim)))
}
if(is.null(col.boundary)){
col.boundary = mat.lim[1]
}
if(col.boundary>mat.lim[2]){
print("Error! col.boundary have to smaller than matrix mat.lim[2] value!")
return(NULL)
}else if(col.boundary<mat.lim[1]){
print("Error! col.boundary have to larger than matrix mat.lim[1] value!")
print(mat.lim)
print(col.boundary)
return(NULL)
}
## fix value,too large or too small
mat[mat < mat.lim[1]] = mat.lim[1]
mat[mat > mat.lim[2]] = mat.lim[2]
## create color vector
mat.col_alpha = rep(0,length(as.vector(mat)))
mat.col_alpha[mat >= col.boundary] = ceiling((mat[mat >= col.boundary] - col.boundary) / (mat.lim[2] - col.boundary) * 255)
mat.col_alpha[mat < col.boundary] = ceiling((col.boundary - mat[mat < col.boundary]) / (col.boundary - mat.lim[1]) * 255)
mat.color = rep("#FFFFFF",length(as.vector(mat)))
mat.color[mat>=col.boundary] = rgb(t(col2rgb(col.max)),alpha = mat.col_alpha[mat>=col.boundary],maxColorValue = 255)
mat.color[mat< col.boundary] = rgb(t(col2rgb(col.min)),alpha = mat.col_alpha[mat< col.boundary],maxColorValue = 255)
}else if(color_type == 2){
# input a col_list
}
# plot the final matrix
plot(x=c(0,col_num),y=c(-row_num,0),type="n",frame.plot = F,xaxt="n",yaxt="n",cex.main = 2,xlab="",ylab="")
rect(x1,y1,x2,y2,col = mat.color,border = NA)
}
#------------------------------------------------------------------------------------------>>>>>>>
# MIT License
#------------------------------------------------------------------------------------------>>>>>>>
# Copyright (c) 2019 MENG Howard
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# 2019-08-03 By MENG