Skip to content

Commit 36ec796

Browse files
authored
Merge pull request #41 from m-jahn/dev
fix: various minor issues
2 parents 3aa2d4d + 1d9174f commit 36ec796

12 files changed

+496
-284
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,4 @@ VignetteBuilder:
5757
knitr
5858
biocViews:
5959
Encoding: UTF-8
60-
RoxygenNote: 7.3.1
60+
RoxygenNote: 7.3.2

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ importFrom(ggplot2,ggplot_add)
114114
importFrom(ggplot2,labs)
115115
importFrom(ggplot2,margin)
116116
importFrom(ggplot2,rel)
117+
importFrom(ggplot2,scale_color_continuous)
117118
importFrom(ggplot2,scale_color_gradientn)
118119
importFrom(ggplot2,scale_color_manual)
119120
importFrom(ggplot2,scale_fill_manual)

R/geom_coverage.R

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#' @importFrom stats as.formula
3232
#' @importFrom ggh4x facet_wrap2 strip_themed elem_list_rect
3333
#' @importFrom dplyr group_by summarise
34-
#' @importFrom dplyr %>%
34+
#' @importFrom dplyr %>% filter
3535
#' @importFrom ggrepel geom_text_repel
3636
#' @importFrom utils tail
3737
#'
@@ -119,16 +119,16 @@ geom_coverage <- function(data, mapping = NULL, color = NULL, rect.color = NA,
119119
if (length(color) < length(unique(data[, group.key]))) {
120120
warning("Fewer colors provided than there are groups in ", group.key, " variable, falling back to default colors")
121121
# sample group with same color
122-
fill.color <- AutoColor(data = data, n = 9, name = "Set1", key = group.key)
122+
fill.color <- AutoColor(data = data[[group.key]], pal = "Set1")
123123
} else {
124124
fill.color <- color
125125
}
126126
if (is.null(names(fill.color))) {
127127
names(fill.color) <- unique(data[, group.key])
128128
}
129-
sacle_fill_cols <- scale_fill_manual(values = fill.color)
129+
scale_fill_cols <- scale_fill_manual(values = fill.color)
130130
} else {
131-
sacle_fill_cols <- NULL
131+
scale_fill_cols <- NULL
132132
}
133133
if (!single.nuc) {
134134
mapping <- aes_string(xmin = "start", xmax = "end", ymin = "0", ymax = "score", fill = group.key)
@@ -140,7 +140,7 @@ geom_coverage <- function(data, mapping = NULL, color = NULL, rect.color = NA,
140140
if (length(color) != length(unique(data[, group.key]))) {
141141
warning("The color you provided is not as long as ", group.key, " column in data, select automatically!")
142142
# sample group with same color
143-
tmp.color <- AutoColor(data = data, n = 9, name = "Set1", key = group.key)
143+
tmp.color <- AutoColor(data = data[[group.key]], pal = "Set1")
144144
# change group key color
145145
color.color.df <- merge(unique(data[c(group.key)]), data.frame(color = tmp.color), by.x = group.key, by.y = 0)
146146
color.color <- color.color.df$color
@@ -169,7 +169,7 @@ geom_coverage <- function(data, mapping = NULL, color = NULL, rect.color = NA,
169169
fill.str.len <- length(unique(data[, fill.str]))
170170
if (is.null(color) | length(color) != fill.str.len) {
171171
# sample group with same color
172-
tmp.color <- AutoColor(data = data, n = 9, name = "Set1", key = group.key)
172+
tmp.color <- AutoColor(data = data[[group.key]], pal = "Set1")
173173
# change color
174174
fill.color.df <- merge(unique(data[c(fill.str, group.key)]), data.frame(color = tmp.color), by.x = group.key, by.y = 0)
175175
fill.color <- fill.color.df$color
@@ -180,9 +180,9 @@ geom_coverage <- function(data, mapping = NULL, color = NULL, rect.color = NA,
180180
names(fill.color) <- unique(data[, fill.str])
181181
}
182182
}
183-
sacle_fill_cols <- scale_fill_manual(values = fill.color)
183+
scale_fill_cols <- scale_fill_manual(values = fill.color)
184184
} else {
185-
sacle_fill_cols <- NULL
185+
scale_fill_cols <- NULL
186186
}
187187
} else if (plot.type == "joint") {
188188
message("For joint visualization, the mapping should contains start, score, color.")
@@ -191,7 +191,7 @@ geom_coverage <- function(data, mapping = NULL, color = NULL, rect.color = NA,
191191
color.str.len <- length(unique(data[, color.str]))
192192
if (is.null(color) | length(color) != color.str.len) {
193193
# sample group with same color
194-
tmp.color <- AutoColor(data = data, n = 9, name = "Set1", key = group.key)
194+
tmp.color <- AutoColor(data = data[[group.key]], pal = "Set1")
195195
# change color
196196
if (color.str == group.key) {
197197
color.color.df <- merge(unique(data[c(color.str)]), data.frame(color = tmp.color), by.x = group.key, by.y = 0)
@@ -223,7 +223,7 @@ geom_coverage <- function(data, mapping = NULL, color = NULL, rect.color = NA,
223223

224224
# facet color
225225
if (is.null(facet.color)) {
226-
facet.color <- AutoColor(data = data, n = 12, name = "Set3", key = facet.key)
226+
facet.color <- AutoColor(data = data[[facet.key]], pal = "Set3")
227227
}
228228

229229
# facet formula
@@ -255,8 +255,8 @@ geom_coverage <- function(data, mapping = NULL, color = NULL, rect.color = NA,
255255
plot.ele <- list(region.rect, region.facet)
256256

257257
# color the track
258-
if (!is.null(sacle_fill_cols)) {
259-
plot.ele <- append(plot.ele, sacle_fill_cols)
258+
if (!is.null(scale_fill_cols)) {
259+
plot.ele <- append(plot.ele, scale_fill_cols)
260260
}
261261

262262
if (range.position == "in") {
@@ -336,36 +336,29 @@ geom_coverage <- function(data, mapping = NULL, color = NULL, rect.color = NA,
336336
# add rect
337337
if (!is.null(mark.region)) {
338338
# get valid mark region
339-
region.start <- data[1, "start"]
340-
region.end <- data[nrow(data), "end"]
341-
valid.region.list <- list()
342-
for (r in 1:nrow(mark.region)) {
343-
if (mark.region[r, "start"] <= region.end & mark.region[r, "end"] >= region.start) {
344-
if (mark.region[r, "end"] >= region.end) {
345-
mark.region[r, "end"] <- region.end
346-
}
347-
if (mark.region[r, "start"] <= region.start) {
348-
mark.region[r, "start"] <- region.start
349-
}
350-
valid.region.list[[r]] <- mark.region[r, ]
351-
}
352-
}
353-
valid.region.df <- do.call(rbind, valid.region.list) %>% as.data.frame()
354-
colnames(valid.region.df) <- colnames(mark.region)
355-
339+
region.start <- min(data$start)
340+
region.end <- max(data$end)
341+
mark.region <- dplyr::filter(
342+
mark.region,
343+
.data[["start"]] >= region.start,
344+
.data[["end"]] <= region.end
345+
)
356346
region.mark <- geom_rect(
357-
data = valid.region.df,
347+
data = mark.region,
358348
aes_string(xmin = "start", xmax = "end", ymin = "-Inf", ymax = "Inf"),
359349
fill = mark.color, alpha = mark.alpha
360350
)
361351
plot.ele <- append(plot.ele, region.mark)
362352
# add rect label
363353
if (show.mark.label) {
364-
if ("label" %in% colnames(valid.region.df)) {
354+
if ("label" %in% colnames(mark.region)) {
365355
# create mark region label
366-
region.label <- valid.region.df
356+
region.label <- mark.region
367357
if (plot.type == "facet") {
368-
region.label[, facet.key] <- facet.order[1]
358+
region.label[, facet.key] <- factor(
359+
rep(facet.order[1], nrow(mark.region)),
360+
facet.order
361+
)
369362
}
370363
region.mark.label <- geom_text_repel(
371364
data = region.label,

R/geom_feature.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ ggplot_add.feature <- function(object, plot, object_name) {
9999
}
100100
} else {
101101
warning("The color you provided is smaller than Type column in data, select automatically!")
102-
used.feature.color <- AutoColor(data = valid.feature, n = 9, name = "Set1", key = "Type")
102+
used.feature.color <- AutoColor(data = valid.feature$Type, pal = "Set1")
103103
}
104104

105105
# create plot

R/geom_gene.R

Lines changed: 88 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,50 @@
11
#' Add Gene Annotation to Coverage Plot.
22
#'
3-
#' @param gtf.gr Granges object of GTF, created with \code{\link{import.gff}}. Default: NULL.
3+
#' @param gtf.gr Granges object of GTF, created with \code{\link{import.gff}}.
4+
#' Default: NULL.
45
#' @param overlap.gene.gap The gap between gene groups. Default: 0.1.
5-
#' @param overlap.style The style of gene groups, choose from loose (each gene occupies single line)
6-
#' and tight (place non-overlap genes in one line). Default: loose.
7-
#' @param gene.size The line size of gene. Default: 1.
8-
#' @param utr.size The line size of UTR. Default: 2.
9-
#' @param exon.size The line size of exon. Default: 3.
10-
#' @param arrow.size The line size of arrow. Default: 1.5.
6+
#' @param overlap.style The style of gene groups, choose from loose (each gene
7+
#' occupies single line) and tight (place non-overlap genes in one line).
8+
#' Default: loose.
9+
#' @param gene.size Line width of genes. Default: 1.
10+
#' @param utr.size Line width of UTRs. Default: 2.
11+
#' @param exon.size Line width of exons. Default: 3.
12+
#' @param arrow.angle Angle of the arrow head. Default 35°
13+
#' @param arrow.length Length of arrows. Default: 1.5
14+
#' @param arrow.type Whether to draw "closed" or "open" (default) arrow heads
15+
#' @param color.by Color the lines/arrows by variable. Default: "strand".
1116
#' @param arrow.gap The gap distance between intermittent arrows. Default: NULL.
1217
#' Set arrow.num and arrow.gap to NULL to suppress intermittent arrows.
13-
#' @param arrow.num Total number of intermittent arrows over whole region. Default: 50.
14-
#' Set arrow.num and arrow.gap to NULL to suppress intermittent arrows.
15-
#' @param color.by Color the line by. Default: strand.
18+
#' @param arrow.num Total number of intermittent arrows over whole region.
19+
#' Default: 50. Set arrow.num and arrow.gap to NULL to suppress intermittent
20+
#' arrows.
21+
#' @param arrow.size.im Line width of intermittent arrows. Default: 0.5
22+
#' @param arrow.length.im Length of intermittent arrows. Default: 1.5
23+
#' @param arrow.type.im Whether to draw "closed" (default) or "open" heads for
24+
#' intermittent arrows
25+
#' @param color.by.im Color the intermittent arrows by variable. Default: NULL
26+
#' (draws semi-transparent, white arrows)
1627
#' @param fill.color Color used for \code{color.by}.
17-
#' Default: blue for - (minus strand), green for + (plus strand).
28+
#' Default: blue for - (minus strand), green for + (plus strand).
1829
#' @param show.utr Logical value, whether to show UTR. Default: TRUE.
1930
#' @param label.size The size of gene label. Default: 3.
2031
#' @param label.vjust The vjust of gene label. Default: 2.
2132
#' @param plot.space Top and bottom margin. Default: 0.1.
22-
#' @param plot.height The relative height of gene annotation to coverage plot. Default: 0.2.
33+
#' @param plot.height The relative height of gene annotation to coverage plot.
34+
#' Default: 0.2.
2335
#'
2436
#' @return Plot.
2537
#' @importFrom dplyr %>%
2638
#' @importFrom rlang .data
2739
#' @importFrom GenomicRanges GRanges makeGRangesFromDataFrame setdiff
2840
#' @importFrom IRanges IRanges subsetByOverlaps findOverlaps
2941
#' @importFrom dplyr filter select arrange
30-
#' @importFrom ggplot2 ggplot_add ggplot geom_segment aes_string arrow unit geom_text labs theme_classic theme element_blank
31-
#' element_text element_rect margin scale_y_continuous scale_color_manual scale_x_continuous coord_cartesian
42+
#' @importFrom ggplot2 ggplot_add ggplot geom_segment aes_string arrow unit
43+
#' geom_text labs theme_classic theme element_blank element_text element_rect
44+
#' margin scale_y_continuous scale_color_manual scale_x_continuous
45+
#' coord_cartesian
3246
#' @importFrom patchwork wrap_plots
47+
#' @importFrom grDevices grey
3348
#' @export
3449
#'
3550
#' @examples
@@ -58,19 +73,40 @@
5873
#' gtf_gr <- rtracklayer::import.gff(con = gtf_file, format = "gtf")
5974
#'
6075
#' # plot coverage and gene annotation
61-
#' basic.coverage <- ggcoverage(data = track_df, range.position = "out")
62-
#' basic.coverage +
76+
#' basic_coverage <- ggcoverage(data = track_df, range.position = "out")
77+
#' basic_coverage +
6378
#' geom_gene(gtf.gr = gtf_gr)
79+
#'
80+
#'# plot with custom style
81+
#' basic_coverage +
82+
#' geom_gene(
83+
#' gtf.gr = gtf_gr,
84+
#' exon.size = 2.0,
85+
#' arrow.size.im = 1.0,
86+
#' arrow.length.im = 5,
87+
#' arrow.type.im = "open",
88+
#' color.by.im = "strand",
89+
#' fill.color = c(
90+
#' "-" = "darkblue",
91+
#' "+" = "darkgreen"
92+
#' )
93+
#' )
6494
geom_gene <- function(gtf.gr,
6595
overlap.gene.gap = 0.1,
6696
overlap.style = "loose",
6797
gene.size = 1,
6898
utr.size = 2,
6999
exon.size = 3,
70-
arrow.size = 1.5,
100+
arrow.angle = 35,
101+
arrow.length = 1.5,
102+
arrow.type = "open",
103+
color.by = "strand",
71104
arrow.gap = NULL,
72105
arrow.num = 50,
73-
color.by = "strand",
106+
arrow.size.im = 0.5,
107+
arrow.length.im = 1.5,
108+
arrow.type.im = "closed",
109+
color.by.im = NULL,
74110
fill.color = c(
75111
"-" = "cornflowerblue",
76112
"+" = "darkolivegreen3"
@@ -88,10 +124,16 @@ geom_gene <- function(gtf.gr,
88124
gene.size = gene.size,
89125
utr.size = utr.size,
90126
exon.size = exon.size,
91-
arrow.size = arrow.size,
127+
arrow.angle = arrow.angle,
128+
arrow.length = arrow.length,
129+
arrow.type = arrow.type,
130+
color.by = color.by,
92131
arrow.gap = arrow.gap,
93132
arrow.num = arrow.num,
94-
color.by = color.by,
133+
arrow.size.im = arrow.size.im,
134+
arrow.length.im = arrow.length.im,
135+
arrow.type.im = arrow.type.im,
136+
color.by.im = color.by.im,
95137
fill.color = fill.color,
96138
show.utr = show.utr,
97139
label.size = label.size,
@@ -127,12 +169,18 @@ ggplot_add.gene <- function(object, plot, object_name) {
127169
gene.size <- object$gene.size
128170
utr.size <- object$utr.size
129171
exon.size <- object$exon.size
130-
arrow.size <- object$arrow.size
172+
arrow.angle <- object$arrow.angle
173+
arrow.length <- object$arrow.length
174+
arrow.type <- object$arrow.type
131175
color.by <- object$color.by
132-
fill.color <- object$fill.color
133-
show.utr <- object$show.utr
134176
arrow.gap <- object$arrow.gap
135177
arrow.num <- object$arrow.num
178+
arrow.size.im <- object$arrow.size.im
179+
arrow.length.im <- object$arrow.length.im
180+
arrow.type.im <- object$arrow.type.im
181+
color.by.im <- object$color.by.im
182+
fill.color <- object$fill.color
183+
show.utr <- object$show.utr
136184
label.size <- object$label.size
137185
label.vjust <- object$label.vjust
138186
plot.space <- object$plot.space
@@ -203,11 +251,11 @@ ggplot_add.gene <- function(object, plot, object_name) {
203251
gene.info.used.utr <- gene.exon.utr$utr
204252
}
205253
gene.plot <- ggplot() +
206-
geom_arrows(gene.info.used.gene, color.by, gene.size, arrow.size) +
207-
geom_arrows(gene.info.used.exon, color.by, exon.size, arrow.size)
254+
geom_arrows(gene.info.used.gene, color.by, gene.size, arrow.length, arrow.angle, arrow.type) +
255+
geom_arrows(gene.info.used.exon, color.by, exon.size, arrow.length, arrow.angle, arrow.type)
208256
if (show.utr) {
209257
gene.plot <- gene.plot +
210-
geom_arrows(gene.info.used.utr, color.by, utr.size, arrow.size)
258+
geom_arrows(gene.info.used.utr, color.by, utr.size, arrow.length, arrow.angle, arrow.type)
211259
}
212260

213261
if (!is.null(arrow.gap) || !is.null(arrow.num)) {
@@ -251,8 +299,21 @@ ggplot_add.gene <- function(object, plot, object_name) {
251299
arrow.df$start <- as.numeric(arrow.df$start)
252300
arrow.df$end <- as.numeric(arrow.df$end)
253301
arrow.df$group <- as.numeric(arrow.df$group)
302+
if (is.null(color.by.im)) {
303+
color.by.im <- color.by
304+
arrow.df[[color.by]] <- "im"
305+
fill.color["im"] <- grDevices::grey(1, alpha = 0.5)
306+
} else if (color.by.im %in% colnames(arrow.df)) {
307+
stopifnot(unique(arrow.df[[color.by.im]]) %in% names(fill.color))
308+
} else {
309+
stop(paste0(
310+
"The selected variable '",
311+
color.by.im ,
312+
"' for 'color.by.im' is not available in the data"
313+
))
314+
}
254315
gene.plot <- gene.plot +
255-
geom_arrows(arrow.df, color.by, gene.size / 2, arrow.size, 35, TRUE)
316+
geom_arrows(arrow.df, color.by.im, arrow.size.im, arrow.length.im, arrow.angle, arrow.type.im)
256317
}
257318

258319
label.df <- data.frame(

0 commit comments

Comments
 (0)