-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRCircos.Histogram.Plot.R
276 lines (204 loc) · 9.43 KB
/
RCircos.Histogram.Plot.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
my.RCircos.Data.Point<-function(chromosome, start)
{
the.point <- 0;
RCircos.Cyto <- RCircos.Get.Plot.Ideogram();
# Which band the start position is in
# _________________________________________________________
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
chrom.rows <- grep(paste("^", chromosome, "$", sep=""), RCircos.Cyto$Chromosome);
the.row <- which(RCircos.Cyto$ChromStart[chrom.rows] <= start & RCircos.Cyto$ChromEnd[chrom.rows] >= start)[1];
# total length, units, and location of the band
# _________________________________________________________
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
band.length <- RCircos.Cyto$Length[chrom.rows[the.row]];
band.units <- RCircos.Cyto$Unit[chrom.rows[the.row]];
band.location <- RCircos.Cyto$Location[chrom.rows[the.row]];
# How far from the chromosome start the point is (by units)
# _________________________________________________________
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
the.bases <- start - RCircos.Cyto$ChromStart[chrom.rows[the.row]] ;
the.units <- the.bases/band.length*band.units;
# The exact point index of points for the circular line
# _________________________________________________________
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
the.point <- band.location - band.units + the.units;
# Return the index. Arguments are valudated outside of this
# function so that there is no need to catch exception.
# _________________________________________________________
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
return (round(the.point, digits=0));
}
RCircos.Validate.Genomic.Data<-function(genomic.data, plot.type=c("plot", "link"))
{
RCircos.Cyto <- RCircos.Get.Plot.Ideogram();
# Plot data has only one chromosome column and link data have two
# _______________________________________________________________
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
plot.type <- tolower(plot.type);
if(plot.type=="plot") {
chrom.col <- 1;
} else if(plot.type=="link") {
chrom.col <- c(1,4);
} else { stop("Plot type must be \"plot\" or \"line\""); }
for (a.col in 1:length(chrom.col))
{
the.col <- chrom.col[a.col];
# Make sure chromosome names in genomic data have prefix
# ______________________________________________________
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
genomic.data[,the.col] <- as.character(genomic.data[,the.col]);
for(a.row in 1:nrow(genomic.data)) {
if(length(grep("chr", genomic.data[a.row,the.col]))==0)
{ genomic.data[a.row,the.col] <- paste("chr",
genomic.data[a.row,the.col], sep=""); }
}
# Make sure chromosomes in input data are all included
# in chromosome ideogram data
# ______________________________________________________
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
cyto.chroms <- unique(as.character(RCircos.Cyto$Chromosome));
data.chroms <- unique(as.character(genomic.data[,the.col]));
if(sum(data.chroms %in% cyto.chroms) < length(data.chroms))
{
cat(paste("Some chromosomes are in genomic data only",
"and have been removed.\n\n"));
all.chroms <- as.character(genomic.data[,the.col]);
genomic.data <- genomic.data[all.chroms %in% cyto.chroms,];
}
data.chroms <- unique(as.character(genomic.data[,the.col]));
# Make sure chromosome start and end postions in genomic
# data are not negative.
# ______________________________________________________
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
if(min(genomic.data[,the.col+1])<0)
{ stop("Error! chromStart position less than 0."); }
if(min(genomic.data[,the.col+2])<0)
{ stop("Error! chromEnd position less than 0."); }
# Make sure chromosome start and end locations in genomic
# data are not out of chromosome length
# _______________________________________________________
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
for(a.chr in 1:length(data.chroms))
{
the.chr <- data.chroms[a.chr];
in.data <- genomic.data[genomic.data[,the.col]==the.chr,];
cyto.data <- RCircos.Cyto[grep(the.chr, RCircos.Cyto$Chromosome),]
if(max(in.data[,the.col+1])>max(cyto.data[,3]) |
max(in.data[,the.col+2])>max(cyto.data[,3]))
{
cat(paste(the.chr, max(in.data[,2]), max(in.data[,3]), "\n"));
stop("Error! Location is outside of chromosome length.");
}
}
# Make sure in genomic data all chromosome start positions
# are smaller than their paired chromosome end positions
# _______________________________________________________________
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
for(a.row in 1:nrow(genomic.data))
{
if(genomic.data[a.row, the.col+1]>genomic.data[a.row, the.col+2])
{
cat("chromStart greater than chromEnd.\n");
stop(paste("Row:", a.row, genomic.data[a.row, 2],
genomic.data[a.row, 3]));
}
}
}
# The validated data needs to be held for the RCircos session
# ___________________________________________________________
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
return (genomic.data);
}
my.RCircos.Get.Plot.Data<-function(genomic.data, plot.type)
{
# Check chromosome names, chromStart, and chromEnd positions
# _______________________________________________________________
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
genomic.data <- RCircos.Validate.Genomic.Data(genomic.data, plot.type);
# Calculate the point index for each chromosome location
# _______________________________________________________________
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
data.points <- rep(0, nrow(genomic.data));
for(a.row in 1:nrow(genomic.data))
{
chromosome <- as.character(genomic.data[a.row, 1]);
location <- round((genomic.data[a.row, 2] + genomic.data[a.row, 3])/2, digits=0);
data.points[a.row] <- my.RCircos.Data.Point(chromosome, location);
}
genomic.data["Location"] <- data.points;
# Sort the data by chromosome then start position
# ____________________________________________________
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
genomic.data <- genomic.data[order(genomic.data$Location),];
# The data needs to be held for the RCircos session
# ____________________________________________________
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
return (genomic.data);
}
my.RCircos.Histogram.Plot<-function(hist.data, data.col, track.num, side)
{
RCircos.Pos <- RCircos.Get.Plot.Positions();
RCircos.Par <- RCircos.Get.Plot.Parameters();
RCircos.Cyto <- RCircos.Get.Plot.Ideogram();
# Convert raw data to plot data. The raw data will be validated
# first during the convertion
# ____________________________________________________________
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hist.data <- RCircos.Get.Plot.Data(hist.data, "plot");
#--------------------------------------------------
# hist.data <- my.RCircos.Get.Plot.Data(ipd.lung.m6A, "plot");
#--------------------------------------------------
#--------------------------------------------------
# hist.data = hist.data[ !is.na(hist.data[,5]), ]
#--------------------------------------------------
#--------------------------------------------------
# print(head(hist.data))
#--------------------------------------------------
# Each heatmap cell is centered on data point location. Make
# sure each one will be in the range of it chromosome
# ____________________________________________________________
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hist.locations <- as.numeric(hist.data[, ncol(hist.data)]);
start <- hist.locations - RCircos.Par$hist.width;
end <- hist.locations + RCircos.Par$hist.width;
data.chroms <- as.character(hist.data[,1]);
chromosomes <- unique(data.chroms);
cyto.chroms <- as.character(RCircos.Cyto$Chromosome);
for(a.chr in 1:length(chromosomes))
{
cyto.rows <- which(cyto.chroms==chromosomes[a.chr]);
locations <- as.numeric(RCircos.Cyto$Location[cyto.rows]);
chr.start <- min(locations) - RCircos.Cyto$Unit[cyto.rows[1]];
chr.end <- max(locations);
data.rows <- which(data.chroms==chromosomes[a.chr]);
start[data.rows[start[data.rows]<chr.start]] <- chr.start;
end[data.rows[end[data.rows]>chr.end]] <- chr.end;
}
# Plot position for current track.
# ___________________________________________________________
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
locations <- RCircos.Track.Positions(side, track.num);
out.pos <- locations[1];
in.pos <- locations[2];
# Draw histogram
# ___________________________________________________________
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
num.subtrack <- RCircos.Par$sub.tracks;
RCircos.Track.Outline(out.pos, in.pos, RCircos.Par$sub.tracks);
for(a.point in 1:nrow(hist.data))
{
hist.height <- hist.data[a.point, data.col];
the.start <- start[a.point];
the.end <- end[a.point];
# Plot rectangle with specific height for each
# data point
# _______________________________________________
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
height <- in.pos + RCircos.Par$track.height*hist.height;
polygon.x<- c(RCircos.Pos[the.start:the.end,1]*height,
RCircos.Pos[the.end:the.start,1]*in.pos);
polygon.y<- c(RCircos.Pos[the.start:the.end,2]*height,
RCircos.Pos[the.end:the.start,2]*in.pos);
polygon(polygon.x, polygon.y, col="red", border=NA);
}
}