11# ' Add Transcript 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 gene.name Gene name of all transcripts. Default: HNRNPC.
56# ' @param overlap.tx.gap The gap between transcript groups. Default: 0.1.
6- # ' @param overlap.style The style of transcript groups, choose from loose (each transcript occupies single line)
7- # ' and tight (place non-overlap transcripts in one line). Default: loose.
8- # ' @param tx.size The line size of transcript. Default: 1.
9- # ' @param utr.size The line size of UTR. Default: 2.
10- # ' @param exon.size The line size of exon. Default: 3.
11- # ' @param arrow.size The line size of arrow. Default: 1.5.
7+ # ' @param overlap.style The style of transcript groups, choose from loose (each
8+ # ' transcript occupies single line) and tight (place non-overlap transcripts
9+ # ' in one line). Default: loose.
10+ # ' @param tx.size Line size of transcript. Default: 1.
11+ # ' @param utr.size Line size of UTR. Default: 2.
12+ # ' @param exon.size Line size of exon. Default: 3.
13+ # ' @param arrow.angle Angle of the arrow head. Default 35°
14+ # ' @param arrow.length Length of arrows. Default: 1.5
15+ # ' @param arrow.type Whether to draw "closed" or "open" (default) arrow heads
16+ # ' @param color.by Color the line by. Default: strand.
1217# ' @param arrow.gap The gap distance between intermittent arrows. Default: NULL.
1318# ' Set arrow.num and arrow.gap to NULL to suppress intermittent arrows.
14- # ' @param arrow.num Total number of intermittent arrows over whole region. Default: 50.
15- # ' Set arrow.num and arrow.gap to NULL to suppress intermittent arrows.
16- # ' @param color.by Color the line by. Default: strand.
17- # ' @param fill.color Color used for \code{color.by}.
18- # ' Default: blue for - (minus strand), green for + (plus strand).
19+ # ' @param arrow.num Total number of intermittent arrows over whole region.
20+ # ' Default: 50. Set arrow.num and arrow.gap to NULL to suppress intermittent
21+ # ' arrows.
22+ # ' @param arrow.size.im Line width of intermittent arrows. Default: 0.5
23+ # ' @param arrow.length.im Length of intermittent arrows. Default: 1.5
24+ # ' @param arrow.type.im Whether to draw "closed" (default) or "open" heads for
25+ # ' intermittent arrows
26+ # ' @param color.by.im Color the intermittent arrows by variable. Default: NULL
27+ # ' (draws semi-transparent, white arrows)
28+ # ' @param fill.color Color used for \code{color.by}. Default: blue for - (minus
29+ # ' strand), green for + (plus strand).
1930# ' @param label.size The size of transcript label. Default: 3.
2031# ' @param label.vjust The vjust of transcript label. Default: 2.
2132# ' @param plot.space Top and bottom margin. Default: 0.1.
22- # ' @param plot.height The relative height of transcript annotation to coverage plot. Default: 0.2.
33+ # ' @param plot.height The relative height of transcript annotation to coverage
34+ # ' plot. Default: 0.2.
2335# '
2436# ' @return Plot.
2537# ' @importFrom dplyr %>%
3042# ' @importFrom ggplot2 ggplot_add ggplot geom_segment aes_string arrow unit geom_text labs theme_classic theme element_blank
3143# ' element_text element_rect margin scale_y_continuous scale_color_manual scale_x_continuous coord_cartesian
3244# ' @importFrom patchwork wrap_plots
45+ # ' @importFrom grDevices grey
3346# ' @export
3447# '
3548# ' @examples
5871# ' gtf_gr <- rtracklayer::import.gff(con = gtf_file, format = "gtf")
5972# '
6073# ' # plot coverage and gene annotation
61- # ' basic.coverage <- ggcoverage(data = track_df, range.position = "out")
62- # ' basic.coverage +
74+ # ' basic_coverage <- ggcoverage(data = track_df, range.position = "out")
75+ # ' basic_coverage +
6376# ' geom_transcript(gtf.gr = gtf_gr, label.vjust = 1.5)
77+ # '
78+ # ' # plot with custom style
79+ # ' basic_coverage +
80+ # ' geom_transcript(
81+ # ' gtf.gr = gtf_gr,
82+ # ' exon.size = 2.0,
83+ # ' arrow.size.im = 1.0,
84+ # ' arrow.length.im = 5,
85+ # ' arrow.type.im = "open",
86+ # ' color.by.im = "strand",
87+ # ' fill.color = c(
88+ # ' "-" = "darkblue",
89+ # ' "+" = "darkgreen"
90+ # ' )
91+ # ' )
6492geom_transcript <-
6593 function (gtf.gr ,
6694 gene.name = " HNRNPC" ,
@@ -69,10 +97,16 @@ geom_transcript <-
6997 tx.size = 1 ,
7098 utr.size = 2 ,
7199 exon.size = 3 ,
72- arrow.size = 3 ,
100+ arrow.angle = 35 ,
101+ arrow.length = 1.5 ,
102+ arrow.type = " open" ,
103+ color.by = " strand" ,
73104 arrow.gap = NULL ,
74105 arrow.num = 50 ,
75- 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 ,
76110 fill.color = c(
77111 " -" = " cornflowerblue" ,
78112 " +" = " darkolivegreen3"
@@ -81,33 +115,37 @@ geom_transcript <-
81115 label.vjust = 2 ,
82116 plot.space = 0.1 ,
83117 plot.height = 1 ) {
84- structure(
85- list (
86- gtf.gr = gtf.gr ,
87- gene.name = gene.name ,
88- overlap.tx.gap = overlap.tx.gap ,
89- overlap.style = overlap.style ,
90- tx.size = tx.size ,
91- utr.size = utr.size ,
92- exon.size = exon.size ,
93- arrow.size = arrow.size ,
94- arrow.gap = arrow.gap ,
95- arrow.num = arrow.num ,
96- color.by = color.by ,
97- fill.color = fill.color ,
98- label.size = label.size ,
99- label.vjust = label.vjust ,
100- plot.space = plot.space ,
101- plot.height = plot.height
102- ),
103- class = " transcript"
104- )
105- }
118+ structure(
119+ list (
120+ gtf.gr = gtf.gr ,
121+ gene.name = gene.name ,
122+ overlap.tx.gap = overlap.tx.gap ,
123+ overlap.style = overlap.style ,
124+ tx.size = tx.size ,
125+ utr.size = utr.size ,
126+ exon.size = exon.size ,
127+ arrow.angle = arrow.angle ,
128+ arrow.length = arrow.length ,
129+ arrow.type = arrow.type ,
130+ color.by = color.by ,
131+ arrow.gap = arrow.gap ,
132+ arrow.num = arrow.num ,
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 ,
137+ fill.color = fill.color ,
138+ label.size = label.size ,
139+ label.vjust = label.vjust ,
140+ plot.space = plot.space ,
141+ plot.height = plot.height
142+ ),
143+ class = " transcript"
144+ )
145+ }
106146
107147# ' @export
108148ggplot_add.transcript <- function (object , plot , object_name ) {
109- # get plot data
110- # track.data <- plot$layers[[1]]$data
111149 # get plot data, plot data should contain bins
112150 if (" patchwork" %in% class(plot )) {
113151 track.data <- plot [[1 ]]$ layers [[1 ]]$ data
@@ -135,11 +173,17 @@ ggplot_add.transcript <- function(object, plot, object_name) {
135173 tx.size <- object $ tx.size
136174 utr.size <- object $ utr.size
137175 exon.size <- object $ exon.size
138- arrow.size <- object $ arrow.size
176+ arrow.angle <- object $ arrow.angle
177+ arrow.length <- object $ arrow.length
178+ arrow.type <- object $ arrow.type
139179 color.by <- object $ color.by
140- fill.color <- object $ fill.color
141180 arrow.gap <- object $ arrow.gap
142181 arrow.num <- object $ arrow.num
182+ arrow.size.im <- object $ arrow.size.im
183+ arrow.length.im <- object $ arrow.length.im
184+ arrow.type.im <- object $ arrow.type.im
185+ color.by.im <- object $ color.by.im
186+ fill.color <- object $ fill.color
143187 label.size <- object $ label.size
144188 label.vjust <- object $ label.vjust
145189 plot.space <- object $ plot.space
@@ -202,17 +246,17 @@ ggplot_add.transcript <- function(object, plot, object_name) {
202246
203247 # create basic plot
204248 tx.plot <- ggplot() +
205- geom_arrows(gene.tx.df.tx , color.by , tx.size , arrow.size )
249+ geom_arrows(gene.tx.df.tx , color.by , tx.size , arrow.length , arrow.angle , arrow.type )
206250
207251 # deal with missing UTR
208252 if (is.null(gene.tx.df.utr )) {
209253 warning(" No UTR detected in provided GTF!" )
210254 } else {
211255 tx.plot <- tx.plot +
212- geom_arrows(gene.tx.df.utr , color.by , utr.size , arrow.size )
256+ geom_arrows(gene.tx.df.utr , color.by , utr.size , arrow.length , arrow.angle , arrow.type )
213257 }
214258 tx.plot <- tx.plot +
215- geom_arrows(gene.tx.df.exon , color.by , exon.size , arrow.size ) +
259+ geom_arrows(gene.tx.df.exon , color.by , exon.size , arrow.length , arrow.angle , arrow.type ) +
216260 theme_classic()
217261
218262 if (is.null(arrow.gap )) {
@@ -258,9 +302,22 @@ ggplot_add.transcript <- function(object, plot, object_name) {
258302 arrow.df $ start <- as.numeric(arrow.df $ start )
259303 arrow.df $ end <- as.numeric(arrow.df $ end )
260304 arrow.df $ group <- as.numeric(arrow.df $ group )
305+ if (is.null(color.by.im )) {
306+ color.by.im <- color.by
307+ arrow.df [[color.by ]] <- " im"
308+ fill.color [" im" ] <- grDevices :: grey(1 , alpha = 0.5 )
309+ } else if (color.by.im %in% colnames(arrow.df )) {
310+ stopifnot(unique(arrow.df [[color.by.im ]]) %in% names(fill.color ))
311+ } else {
312+ stop(paste0(
313+ " The selected variable '" ,
314+ color.by.im ,
315+ " ' for 'color.by.im' is not available in the data"
316+ ))
317+ }
261318 # add arrow
262319 tx.arrow.plot <- tx.plot +
263- geom_arrows(arrow.df , color.by , tx .size / 2 , arrow.size , 35 , TRUE )
320+ geom_arrows(arrow.df , color.by.im , arrow .size.im , arrow.length.im , arrow.angle , arrow.type.im )
264321
265322 # prepare label dataframe
266323 label.df <- data.frame (
0 commit comments