22# '
33# ' @param bed.file The path to consensus peaks file. Default: NULL.
44# ' @param peak.df The dataframe contains consensus peaks. Default: NULL.
5- # ' @param peak.color Peak color . Default: black .
5+ # ' @param peak.color Peak colors . Default: NULL .
66# ' @param peak.size The line size of peak. Default: 5.
7+ # ' @param color.by Name of optional column in bed file/data frame which is used for coloring. Default: NULL.
78# ' @param plot.space Top and bottom margin. Default: 0.1.
8- # ' @param plot.height The relative height of peak annotation to coverage plot. Default: 0.2 .
9+ # ' @param plot.height The relative height of peak annotation to coverage plot. Default: 0.1 .
910# '
1011# ' @return Plot.
1112# ' @importFrom utils read.table
1213# ' @importFrom dplyr arrange
1314# ' @importFrom dplyr %>%
14- # ' @importFrom ggplot2 ggplot_add ggplot geom_segment aes_string theme_classic theme element_blank element_text
15- # ' element_rect margin scale_x_continuous scale_y_continuous coord_cartesian
15+ # ' @importFrom ggplot2 ggplot_add ggplot geom_segment aes_string theme_classic
16+ # ' theme element_blank element_text element_rect margin scale_x_continuous
17+ # ' scale_y_continuous scale_color_continuous coord_cartesian
1618# ' @export
1719# '
1820# ' @examples
19- # ' # library(ggcoverage)
20- # ' # library(rtracklayer)
21- # ' # sample.meta <- data.frame(
22- # ' # SampleName = c("Chr18_MCF7_ER_1", "Chr18_MCF7_ER_2", "Chr18_MCF7_ER_3", "Chr18_MCF7_input"),
23- # ' # Type = c("MCF7_ER_1", "MCF7_ER_2", "MCF7_ER_3", "MCF7_input"),
24- # ' # Group = c("IP", "IP", "IP", "Input")
25- # ' # )
26- # ' # track folder
27- # ' # track.folder <- system.file("extdata", "ChIP-seq", package = "ggcoverage")
28- # ' # load bigwig file
29- # ' # track.df <- LoadTrackFile(
30- # ' # track.folder = track.folder, format = "bw",
31- # ' # meta.info = sample.meta
32- # ' # )
33- # ' # gtf.file <- system.file("extdata", "used_hg19.gtf", package = "ggcoverage")
34- # ' # gtf.gr <- rtracklayer::import.gff(con = gtf.file, format = "gtf")
35- # ' # create mark region
36- # ' # mark.region <- data.frame(start = c(76822533), end = c(76823743), label = c("Promoter"))
37- # ' # basic.coverage <- ggcoverage(
38- # ' # data = track.df, color = "auto", region = "chr18:76822285-76900000",
39- # ' # mark.region = mark.region, show.mark.label = FALSE
40- # ' # )
41- # ' # get consensus peak file
42- # ' # peak.file <- system.file("extdata", "ChIP-seq", "consensus.peak", package = "ggcoverage")
43- # ' # basic.coverage + geom_gene(gtf.gr = gtf.gr) + geom_peak(bed.file = peak.file)
44- geom_peak <- function (bed.file = NULL , peak.df = NULL , peak.color = " black" , peak.size = 5 ,
45- plot.space = 0.1 , plot.height = 0.1 ) {
21+ # ' # load metadata
22+ # ' sample_meta <- data.frame(
23+ # ' SampleName = c(
24+ # ' "Chr18_MCF7_ER_1",
25+ # ' "Chr18_MCF7_ER_2",
26+ # ' "Chr18_MCF7_ER_3",
27+ # ' "Chr18_MCF7_input"
28+ # ' ),
29+ # ' Type = c("MCF7_ER_1", "MCF7_ER_2", "MCF7_ER_3", "MCF7_input"),
30+ # ' Group = c("IP", "IP", "IP", "Input")
31+ # ' )
32+ # '
33+ # ' # import coverage track
34+ # ' track_folder <- system.file("extdata", "ChIP-seq", package = "ggcoverage")
35+ # ' track_df <- LoadTrackFile(
36+ # ' track.folder = track_folder,
37+ # ' format = "bw",
38+ # ' region = "chr18:76822285-76900000",
39+ # ' meta.info = sample_meta
40+ # ' )
41+ # '
42+ # ' # create mock peak file
43+ # ' df_peaks <- data.frame(
44+ # ' seqnames = c("chr18", "chr18", "chr18"),
45+ # ' start = c(76822533, 76846900, 76880000),
46+ # ' end = c(76836900, 76860000, 76887000),
47+ # ' score = c(4, 6, 13)
48+ # ' )
49+ # '
50+ # ' # plot with default color
51+ # ' ggcoverage(data = track_df) +
52+ # ' geom_peak(peak.df = df_peaks, peak.size = 3)
53+ # '
54+ # ' # plot with color by 'score' variable
55+ # ' ggcoverage(data = track_df) +
56+ # ' geom_peak(peak.df = df_peaks, peak.size = 3, color.by = "score")
57+ # '
58+ # ' # plot with color by 'score' variable and custom color scale
59+ # ' ggcoverage(data = track_df) +
60+ # ' geom_peak(peak.df = df_peaks, peak.size = 3, color.by = "score", peak.color = rainbow(5))
61+ # '
62+ geom_peak <- function (bed.file = NULL , peak.df = NULL , peak.color = NULL , peak.size = 5 ,
63+ color.by = NULL , plot.space = 0.1 , plot.height = 0.1 ) {
4664 structure(
4765 list (
4866 bed.file = bed.file , peak.df = peak.df , peak.color = peak.color , peak.size = peak.size ,
49- plot.space = plot.space , plot.height = plot.height
67+ color.by = color.by , plot.space = plot.space , plot.height = plot.height
5068 ),
5169 class = " peak"
5270 )
5371}
5472
5573# ' @export
5674ggplot_add.peak <- function (object , plot , object_name ) {
57- # get plot data
58- # plot.data <- plot$layers[[1]]$data
59- # get plot data, plot data should contain bins
6075 if (" patchwork" %in% class(plot )) {
6176 plot.data <- plot [[1 ]]$ layers [[1 ]]$ data
6277 } else {
6378 plot.data <- plot $ layers [[1 ]]$ data
6479 }
65- # prepare plot range
66- # the plot region are not normal, so start is minimum value
6780 plot.chr <- as.character(plot.data [1 , " seqnames" ])
68- # plot.region.start <- plot$coordinates$limits$x[1]
69- # plot.region.end <- plot$coordinates$limits$x[2]
70- # plot.region.start <- plot.data[1, "start"]
7181 plot.region.start <- min(plot.data [, " start" ])
72- # plot.region.end <- plot.data[nrow(plot.data), "end"]
7382 plot.region.end <- max(plot.data [, " end" ])
7483
7584 # get parameters
7685 bed.file <- object $ bed.file
7786 peak.df <- object $ peak.df
7887 peak.color <- object $ peak.color
7988 peak.size <- object $ peak.size
89+ color.by <- object $ color.by
8090 plot.space <- object $ plot.space
8191 plot.height <- object $ plot.height
8292
@@ -86,30 +96,55 @@ ggplot_add.peak <- function(object, plot, object_name) {
8696 } else if (! is.null(peak.df )) {
8797 bed.info <- peak.df
8898 }
89- bed.info <- bed.info [c(1 , 2 , 3 )]
90- colnames(bed.info ) <- c(" seqnames" , " start" , " end" )
91- # convert to 1-based
99+ colnames(bed.info )[c(1 , 2 , 3 )] <- c(" seqnames" , " start" , " end" )
92100 bed.info $ start <- as.numeric(bed.info $ start ) + 1
93-
94- # get valid bed
95101 valid.bed <- GetRegion(chr = plot.chr , df = bed.info , start = plot.region.start , end = plot.region.end )
96102
103+ # color management
104+ if (! is.null(color.by )) {
105+ if (is.numeric(valid.bed [[color.by ]])) {
106+ if (! is.null(peak.color )) {
107+ scale_colors <- scale_color_gradientn(colours = peak.color )
108+ } else {
109+ scale_colors <- scale_color_continuous()
110+ }
111+ } else {
112+ if (length(peak.color ) < length(unique(valid.bed [[color.by ]]))) {
113+ warning(" Fewer colors provided than there are groups in " , color.by , " variable, falling back to default colors" )
114+ auto_colors <- AutoColor(data = valid.bed [[color.by ]], pal = " Set3" )
115+ scale_colors <- scale_color_manual(values = auto_colors )
116+ } else {
117+ scale_colors <- scale_color_manual(values = peak.color )
118+ }
119+ }
120+ } else {
121+ color.by <- " color"
122+ if (is.null(peak.color )) {
123+ peak.color <- " black"
124+ }
125+ valid.bed $ color <- peak.color [1 ]
126+ scale_colors <- scale_color_manual(values = peak.color )
127+ }
128+
97129 peak.plot <- ggplot() +
98130 geom_segment(
99131 data = valid.bed ,
100132 mapping = aes_string(
101133 x = " start" ,
102134 y = " 1" ,
103135 xend = " end" ,
104- yend = " 1"
136+ yend = " 1" ,
137+ color = color.by
105138 ),
106139 size = peak.size ,
107- color = peak.color
108140 ) +
141+ scale_colors +
109142 labs(y = " Peak" )
110143
111144 # add theme
112- peak.plot <- peak.plot + theme_peak(margin.len = plot.space , x.range = c(plot.region.start , plot.region.end ))
145+ peak.plot <- peak.plot +
146+ theme_peak(margin.len = plot.space , x.range = c(plot.region.start , plot.region.end )) +
147+ theme(legend.position = " none" )
113148 # assemble plot
114149 patchwork :: wrap_plots(plot + theme(plot.margin = margin(t = plot.space , b = plot.space )),
115150 peak.plot ,
0 commit comments