-
Notifications
You must be signed in to change notification settings - Fork 14
/
plot_circle.R
340 lines (307 loc) · 10.9 KB
/
plot_circle.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
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
#' Scale a vector of numeric values to an interval.
#'
#' This function takes a vector of numeric values as well as an interval
#' [new_min, new_max] that the numeric values will be scaled (normalized) to.
#'
#' @param the_list A vector of numeric values.
#' @param new_min Minimum value for the new interval.
#' @param new_max Maximum value for the new interval.
#'
#' @return A data frame with fusion link data compatible with RCircos::RCircos.Link.Plot()
#'
#' # @examples # Apparently examples shouldn't be set on private functions
#' list012 <- c(0,1,2)
#' .scale_list_to_interval(list012, 1, 3)
#' # [1] 1 2 3
#'
#' @name chimeraviz-internals-scaleListToInterval
.scale_list_to_interval <- function(the_list, new_min, new_max) {
if (length(the_list) <= 1) {
stop(paste("Invalid list. Using this function with less than two values",
"makes no sense."))
}
(new_max - new_min) * (the_list - min(the_list)) /
(max(the_list) - min(the_list)) + new_min
}
#' Create link data for RCircos from the given fusions.
#'
#' This function takes a list of Fusion objects and creates a data frame in the
#' format that RCircos::RCircos.Link.Plot() expects for link data.
#'
#' @param fusion_list A list of Fusion objects.
#' @param min_link_width The minimum link line width. Default = 1
#' @param max_link_widt The maximum link line width. Default = 10
#'
#' @return A data frame with fusion link data compatible with RCircos::RCircos.Link.Plot()
#'
#' # @examples # Apparently examples shouldn't be set on private functions
#' defuse833ke <- system.file("extdata", "defuse_833ke_results.filtered.tsv", package="chimeraviz")
#' fusions <- import_defuse(defuse833ke, "hg19", 3)
#' linkData <- chimeraviz::.fusions_to_link_data(fusions)
#' # This linkData can be used with RCircos::RCircos.Link.Plot()
#'
#' @name chimeraviz-internals-fusions_to_link_data
.fusions_to_link_data <- function(
fusion_list,
min_link_width = 1,
max_link_widt = 10
) {
chromosome <- vector(mode = "character", length = length(fusion_list))
chrom_start <- vector(mode = "numeric", length = length(fusion_list))
chrom_end <- vector(mode = "numeric", length = length(fusion_list))
chromosome_1 <- vector(mode = "character", length = length(fusion_list))
chrom_start_1 <- vector(mode = "numeric", length = length(fusion_list))
chrom_end_1 <- vector(mode = "numeric", length = length(fusion_list))
link_width <- vector(mode = "numeric", length = length(fusion_list))
for (i in seq_along(fusion_list)) {
fusion <- fusion_list[[i]]
chromosome[[i]] <- fusion@gene_upstream@chromosome
chrom_start[[i]] <- fusion@gene_upstream@breakpoint
# This value shouldn't matter:
chrom_end[[i]] <- fusion@gene_upstream@breakpoint + 1
chromosome_1[[i]] <- fusion@gene_downstream@chromosome
chrom_start_1[[i]] <- fusion@gene_downstream@breakpoint
# This value shouldn't matter:
chrom_end_1[[i]] <- fusion@gene_downstream@breakpoint + 1
# Set link width = number of spanning+split reads supporting fusion
link_width[[i]] <- fusion@spanning_reads_count + fusion@split_reads_count
}
# Normalize all link width values to the interval
# [min_link_width, max_link_width]
if (length(link_width) > 1) {
link_width <-
.scale_list_to_interval(link_width, min_link_width, max_link_widt)
} else {
link_width[[1]] <- max_link_widt
}
data.frame(chromosome,
chrom_start,
chrom_end,
chromosome_1,
chrom_start_1,
chrom_end_1,
link_width)
}
#' Create gene label data for RCircos from the given fusions.
#'
#' This function takes a list of Fusion objects and creates a data frame in the
#' format that RCircos.Gene.Name.Plot() expects for gene label data.
#'
#' @param fusion_list A list of Fusion objects.
#'
#' @return A data frame with fusion gene label data compatible with RCircos.Gene.Name.Plot()
#'
#' # @examples # Apparently examples shouldn't be set on private functions
#' defuse833ke <- system.file("extdata", "defuse_833ke_results.filtered.tsv", package="chimeraviz")
#' fusions <- import_defuse(defuse833ke, "hg19", 3)
#' labelData <- chimeraviz::.fusions_to_gene_label_data(fusions)
#' # This labelData can be used with RCircos.Gene.Connector.Plot() and RCircos.Gene.Name.Plot()
#'
#' @name chimeraviz-internals-fusions_to_gene_label_data
.fusions_to_gene_label_data <- function(fusion_list) {
original_length <- length(fusion_list)
new_length <- original_length * 2
chromosome <- vector(mode = "character", length = new_length)
chrom_start <- vector(mode = "numeric", length = new_length)
chrom_end <- vector(mode = "numeric", length = new_length)
gene <- vector(mode = "character", length = new_length)
for (i in seq_along(fusion_list)) {
fusion <- fusion_list[[i]]
# We are building the list of gene names for both partner genes at once, so
# let's put the gene1 genes in the first half of the list..
chromosome[[i]] <- fusion@gene_upstream@chromosome
chrom_start[[i]] <- fusion@gene_upstream@breakpoint
# This value shouldn't matter:
chrom_end[[i]] <- fusion@gene_upstream@breakpoint + 1
gene[[i]] <- fusion@gene_upstream@name
# and put the gene2 genes in the other half.
chromosome[[i + original_length]] <- fusion@gene_downstream@chromosome
chrom_start[[i + original_length]] <- fusion@gene_downstream@breakpoint
# This value shouldn't matter:
chrom_end[[i + original_length]] <- fusion@gene_downstream@breakpoint + 1
gene[[i + original_length]] <- fusion@gene_downstream@name
}
data.frame(chromosome,
chrom_start,
chrom_end,
gene)
}
#' Create a circle plot of the given fusions.
#'
#' This function takes a list of Fusion objects and creates a circle plot
#' indicating which chromosomes the fusion genes in the list consists of.
#'
#' Note that only a limited number of gene names can be shown in the circle plot
#' due to the limited resolution of the plot. RCircos will automatically limit
#' the number of gene names shown if there are too many.
#'
#' @param fusion_list A list of Fusion objects.
#'
#' @return Creates a circle plot.
#'
#' @examples
#' defuse833ke <- system.file(
#' "extdata",
#' "defuse_833ke_results.filtered.tsv",
#' package="chimeraviz")
#' fusions <- import_defuse(defuse833ke, "hg19", 3)
#' # Temporary file to store the plot
#' pngFilename <- tempfile(
#' pattern = "circlePlot",
#' fileext = ".png",
#' tmpdir = tempdir())
#' # Open device
#' png(pngFilename, width = 1000, height = 750)
#' # Plot!
#' plot_circle(fusions)
#' # Close device
#' dev.off()
#'
#' @export
plot_circle <- function(fusion_list) {
.validate_plot_circle_params(fusion_list)
# Read cytoband information depending on genome version
if (fusion_list[[1]]@genome_version == "hg19") {
cytoband_file <- system.file(
"extdata",
"UCSC.HG19.Human.CytoBandIdeogram.txt",
package = "chimeraviz")
} else if (fusion_list[[1]]@genome_version == "hg38") {
cytoband_file <- system.file(
"extdata",
"UCSC.HG38.Human.CytoBandIdeogram.txt",
package = "chimeraviz")
} else if (fusion_list[[1]]@genome_version == "mm10") {
cytoband_file <- system.file(
"extdata",
"UCSC.MM10.Mus.musculus.CytoBandIdeogram.txt",
package = "chimeraviz"
)
} else {
stop("Invalid genome version.")
}
cytoband <- utils::read.table(cytoband_file)
# Set names to what RCircos expects
names(cytoband) <- c("Chromosome", "ChromStart", "ChromEnd", "Band", "Stain")
# We need the RCircos.Env object in the global namespace. Why? Because RCircos
# is weird that way.
assign("RCircos.Env", RCircos::RCircos.Env, .GlobalEnv)
# Sort the cytoband data so that the chromosomes appear in order
cytoband <- RCircos.Sort.Genomic.Data( # Exclude Linting
genomic.data = cytoband,
is.ideo = TRUE
)
# Initialize components
cyto_info <- cytoband
chr_exclude <- NULL
tracks_inside <- 3
tracks_outside <- 0
RCircos::RCircos.Set.Core.Components(
cyto_info,
chr_exclude,
tracks_inside,
tracks_outside
)
# Open a new window for plotting
RCircos::RCircos.Set.Plot.Area()
# Draw chromosome ideogram
RCircos::RCircos.Chromosome.Ideogram.Plot()
# Create gene label data in the format RCircos requires
gene_label_data <- .fusions_to_gene_label_data(fusion_list)
# Draw gene names
name_col <- 4;
side <- "in";
track_num <- 1;
# Plot connectors
RCircos.Gene.Connector.Plot( # Exclude Linting
gene_label_data,
track_num,
side
);
track_num <- 2;
# Plot gene names
RCircos.Gene.Name.Plot( # Exclude Linting
gene_label_data,
name_col,
track_num,
side
);
# Create link data in the format RCircos requires
link_data <- .fusions_to_link_data(fusion_list)
# Make sure the ordering is correct.
# Ref https://github.com/stianlagstad/chimeraviz/issues/52
multi_mixedorder <- function(..., na.last = TRUE, decreasing = FALSE) {
do.call(
order,
c(
lapply(
list(...),
function(l) {
if (is.character(l)) {
factor(
l,
levels = gtools::mixedsort(unique(l))
)
} else {
l
}
}
),
list(na.last = na.last, decreasing = decreasing)
)
)
}
ordered_link_width <- link_data[
multi_mixedorder(
as.character(link_data$chromosome),
link_data$chrom_start
),
]$link_width
# Draw link data
# Which track?
track_num <- 3
# Plot links
RCircos::RCircos.Link.Plot(
link.data = link_data,
track.num = track_num,
by.chromosome = TRUE,
start.pos = NULL,
genomic.columns = 3,
is.sorted = FALSE,
lineWidth = ordered_link_width)
# When done, remove the RCircos.Env element from the global namespace
remove("RCircos.Env", envir = .GlobalEnv)
}
.validate_plot_circle_params <- function(
fusion_list
) {
# Establish a new 'checkmate' object
coll <- checkmate::makeAssertCollection()
# Check input type and length
# Cehck that all the items in the input list are fusion objects
checkmate::assert_list(x = fusion_list,
min.len = 1,
types = "Fusion")
# Validate that the first fusion object has a valid genome
genome_of_first_fusion <- fusion_list[[1]]@genome_version
if (!genome_of_first_fusion %in% list("hg19", "hg38", "mm10")) {
coll$push(
paste0(
"Invalid input. genomeVersion must be either \"hg19\", \"hg38\" or ",
"\"mm10\"."
)
)
}
# Validate that all the fusions have the same genome
genome_version <- sapply(seq_along(fusion_list),
function(fl) fusion_list[[fl]]@genome_version)
if (any(genome_version != genome_of_first_fusion)) {
coll$push(
paste0(
"All Fusion objects in the fusion_list must have the same genome."
)
)
}
# Return errors and warnings (if any)
checkmate::reportAssertions(coll)
}