Skip to content

Commit afc04d1

Browse files
authored
Merge pull request #20 from m-jahn/colors
fix: Y axis scales and various smaller issues
2 parents d4bdec6 + 4daf282 commit afc04d1

17 files changed

+268
-276
lines changed

DESCRIPTION

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Description: The goal of 'ggcoverage' is to simplify the process of visualizing
1414
peak annotation, contact map annotation, link annotation and peotein feature annotation.
1515
License: MIT + file LICENSE
1616
Encoding: UTF-8
17-
RoxygenNote: 7.1.1
17+
RoxygenNote: 7.3.0
1818
biocViews:
1919
Imports:
2020
dplyr,
@@ -56,6 +56,5 @@ Suggests:
5656
htmltools,
5757
BSgenome.Hsapiens.UCSC.hg19,
5858
graphics,
59-
HiCDataHumanIMR90,
60-
GenomeMatrix
59+
HiCDataHumanIMR90
6160
VignetteBuilder: knitr

NAMESPACE

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ S3method(ggplot_add,ideogram)
99
S3method(ggplot_add,link)
1010
S3method(ggplot_add,peak)
1111
S3method(ggplot_add,tad)
12-
S3method(ggplot_add,tad2)
1312
S3method(ggplot_add,transcript)
1413
export(FormatTrack)
1514
export(GetConsensusPeak)
@@ -57,7 +56,6 @@ importFrom(Biostrings,readDNAStringSet)
5756
importFrom(GenomeInfoDb,"seqlengths<-")
5857
importFrom(GenomeInfoDb,seqlengths)
5958
importFrom(GenomeInfoDb,seqnames)
60-
importFrom(GenomeMatrix,matrixPlotter)
6159
importFrom(GenomicAlignments,alphabetFrequencyFromBam)
6260
importFrom(GenomicAlignments,coverage)
6361
importFrom(GenomicAlignments,readGAlignments)
@@ -122,6 +120,7 @@ importFrom(ggplot2,theme_classic)
122120
importFrom(ggplot2,unit)
123121
importFrom(ggpp,annotate)
124122
importFrom(ggrepel,geom_text_repel)
123+
importFrom(grDevices,col2rgb)
125124
importFrom(grDevices,colorRampPalette)
126125
importFrom(magrittr,"%>%")
127126
importFrom(methods,extends)
@@ -140,3 +139,5 @@ importFrom(stringr,str_locate)
140139
importFrom(utils,menu)
141140
importFrom(utils,read.csv)
142141
importFrom(utils,read.table)
142+
importFrom(utils,tail)
143+
importFrom(utils,write.table)

R/geom_coverage.R

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@
2626
#' @return Layers of ggplot2.
2727
#' @importFrom ggplot2 aes_string scale_fill_manual geom_rect geom_text aes geom_step
2828
#' @importFrom rlang .data
29-
#' @importFrom grDevices colorRampPalette
29+
#' @importFrom grDevices colorRampPalette col2rgb
3030
#' @importFrom RColorBrewer brewer.pal
3131
#' @importFrom rlang as_label
3232
#' @importFrom stats as.formula
3333
#' @importFrom ggh4x facet_wrap2 strip_themed elem_list_rect
3434
#' @importFrom dplyr group_by summarise
3535
#' @importFrom magrittr %>%
3636
#' @importFrom ggrepel geom_text_repel
37+
#' @importFrom utils tail
3738
#'
3839
#' @export
3940
#' @examples
@@ -239,8 +240,10 @@ geom_coverage <- function(data, mapping = NULL, color = NULL, rect.color = NA,
239240
# prepare facet scale
240241
if (facet.y.scale == "free") {
241242
facet.ys <- "free_y"
243+
data.range <- dplyr::group_by(data, .data[[facet.key]])
242244
} else if (facet.y.scale == "fixed") {
243245
facet.ys <- "fixed"
246+
data.range <- data
244247
}
245248
region.facet <- facet_wrap2(
246249
facets = facet.formula, ncol = 1, scales = facet.ys, strip.position = "right",
@@ -256,11 +259,10 @@ geom_coverage <- function(data, mapping = NULL, color = NULL, rect.color = NA,
256259
}
257260

258261
if (range.position == "in") {
259-
data.range <- data %>%
260-
dplyr::group_by(.data[[facet.key]]) %>%
262+
data.range <- data.range %>%
261263
dplyr::summarise(.groups = "drop_last",
262-
min_score = CeilingNumber(min(.data[[ymax.str]])),
263-
max_score = CeilingNumber(max(.data[[ymax.str]]))
264+
min_score = pretty(.data[[ymax.str]])[1],
265+
max_score = tail(pretty(.data[[ymax.str]]), 1)
264266
)
265267
data.range$label <- paste0("[", data.range$min_score, ", ", data.range$max_score, "]")
266268
region.range <- geom_text(

R/geom_link.R

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,24 @@
2626
#'
2727
#' @examples
2828
#' library(ggcoverage)
29-
#' # create test dataframe (random)
29+
#' # create test dataframe
30+
#' # (random, but use seed to obtain same result every time)
31+
#' set.seed(123)
3032
#' df <- data.frame(
31-
#' seqnames = "chr9", start = seq(from = 4000000, to = 5999000, by = 1000),
32-
#' end = seq(from = 4001000, to = 6000000, by = 1000), score = sample(1:100, 2000, replace = TRUE),
33+
#' seqnames = "chr2L", start = seq(from = 8000000, to = 8300000, by = 1000),
34+
#' end = seq(from = 8001000, to = 8301000, by = 1000), score = sample(1:100, 301, replace = TRUE),
3335
#' Type = "Example", Group = "Example"
3436
#' )
37+
#' # get links
38+
#' link.file = system.file("extdata", "HiC", "HiC_link.bedpe", package = "ggcoverage")
39+
#'
3540
#' # create plot
3641
#' ggcoverage(
37-
#' data = df, color = "grey", region = "chr9:4000000-6000000",
42+
#' data = df, color = "grey",
3843
#' mark.region = NULL, range.position = "out"
3944
#' ) +
4045
#' geom_link(link.file = link.file, file.type = "bedpe", show.rect = TRUE)
46+
#'
4147
geom_link <- function(link.file, file.type = "bedpe", score.col = NULL, score.threshold = NULL,
4248
score.color = c("blue", "grey", "red"), scale.range = 10,
4349
plot.space = 0.1, plot.height = 0.2, show.rect = FALSE) {

R/geom_protein.R

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@
3535
#' @export
3636
#'
3737
#' @examples
38-
#' # library(ggplot2)
39-
#' # library(ggcoverage)
40-
#' # coverage.file <- system.file("extdata", "Proteomics", "MS_BSA_coverage.xlsx", package = "ggcoverage")
41-
#' # fasta.file <- system.file("extdata", "Proteomics", "MS_BSA_coverage.fasta", package = "ggcoverage")
42-
#' # protein.id = "sp|
43-
#' # ggplot() +
44-
#' # geom_peptide(coverage.file = coverage.file, fasta.file = fasta.file, protein.id = protein.id)
38+
#' library(ggplot2)
39+
#' library(ggcoverage)
40+
#' coverage.file <- system.file("extdata", "Proteomics", "MS_BSA_coverage.xlsx", package = "ggcoverage")
41+
#' fasta.file <- system.file("extdata", "Proteomics", "MS_BSA_coverage.fasta", package = "ggcoverage")
42+
#' protein.id = "sp|P02769|ALBU_BOVIN"
43+
#' ggplot() +
44+
#' geom_protein(coverage.file = coverage.file, fasta.file = fasta.file, protein.id = protein.id)
4545
geom_protein <- function(coverage.file, fasta.file, protein.id, XCorr.threshold = 2,
4646
confidence = "High", contaminant = NULL, remove.na = TRUE,
4747
color = "grey", mark.bare = TRUE, mark.color = "red", mark.alpha = 0.5,
@@ -187,7 +187,7 @@ geom_protein <- function(coverage.file, fasta.file, protein.id, XCorr.threshold
187187
# range position
188188
if (range.position == "in") {
189189
# prepare range
190-
max.abundance <- CeilingNumber(max(coverage.final$abundance))
190+
max.abundance <- max(pretty(coverage.final$abundance))
191191
abundance.range <- data.frame(label = paste0("[0, ", scales::scientific(max.abundance, digits = 2), "]"))
192192
range.text <- geom_text(
193193
data = abundance.range,

R/geom_tad.R

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,54 @@
2121
#' @importFrom ggplot2 ggplot_add ggplot labs theme_classic theme element_blank element_rect
2222
#' element_text margin scale_y_continuous scale_x_continuous
2323
#' @importFrom patchwork wrap_plots
24+
#' @importFrom utils write.table
2425
#'
25-
#' @export
26+
#' @examples
27+
#' library(ggcoverage)
28+
#' library(GenomicRanges)
29+
#'
30+
#' # prepare track dataframe
31+
#' track.file = system.file("extdata", "HiC", "H3K36me3.bw", package = "ggcoverage")
32+
#' track.df = LoadTrackFile(track.file = track.file, format = "bw",
33+
#' region = "chr2L:8050000-8300000", extend = 0)
34+
#' track.df$score = ifelse(track.df$score <0, 0, track.df$score)
35+
#' # check the data
36+
#' head(track.df)
37+
#'
38+
#' # Load Hi-C data
39+
#' hic.mat.file = system.file("extdata", "HiC", "HiC_mat.txt", package = "ggcoverage")
40+
#' hic.mat = read.table(file = hic.mat.file, sep = "\t")
41+
#' hic.mat = as.matrix(hic.mat)
42+
#'
43+
#' # bin data
44+
#' hic.bin.file = system.file("extdata", "HiC", "HiC_bin.txt", package = "ggcoverage")
45+
#' hic.bin = read.table(file = hic.bin.file, sep = "\t")
46+
#' colnames(hic.bin) = c("chr", "start", "end")
47+
#' hic.bin.gr = GenomicRanges::makeGRangesFromDataFrame(df = hic.bin)
48+
#'
49+
#' # transfrom function
50+
#' FailSafe_log10 <- function(x){
51+
#' x[is.na(x) | is.nan(x) | is.infinite(x)] <- 0
52+
#' return(log10(x+1))
53+
#' }
2654
#'
55+
#' # load link data: prepare arcs
56+
#' link.file = system.file("extdata", "HiC", "HiC_link.bedpe", package = "ggcoverage")
57+
#'
58+
#' # basic coverage
59+
#' basic.coverage = ggcoverage(
60+
#' data = track.df, color = "grey",
61+
#' mark.region = NULL, range.position = "out"
62+
#' )
63+
#'
64+
#' # add annotations
65+
#' basic.coverage +
66+
#' geom_tad(matrix = hic.mat, granges = hic.bin.gr, value.cut = 0.99,
67+
#' color.palette = "viridis", transform.fun = FailSafe_log10,
68+
#' top = FALSE, show.rect = TRUE) +
69+
#' geom_link(link.file = link.file, file.type = "bedpe", show.rect = TRUE)
70+
#'
71+
#' @export
2772
geom_tad <- function(matrix, granges, color.palette = NULL, value.cut = NULL,
2873
transform.fun = NULL, plot.space = 0.1, plot.height = 1,
2974
top = TRUE, show.rect = FALSE) {
@@ -35,7 +80,6 @@ geom_tad <- function(matrix, granges, color.palette = NULL, value.cut = NULL,
3580
class = "tad"
3681
)
3782
}
38-
3983
#' @export
4084
ggplot_add.tad <- function(object, plot, object_name) {
4185
# get plot data

R/geom_tad2.R

Lines changed: 0 additions & 116 deletions
This file was deleted.

R/theme_ggcoverage.R

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ theme_coverage <- function(space = 0.2) {
2020
axis.title = element_blank()
2121
),
2222
ggplot2::annotate("segment", x = -Inf, xend = Inf, y = -Inf, yend = -Inf, size = rel(1)),
23-
scale_y_continuous(expand = expansion(mult = c(0))),
23+
scale_y_continuous(
24+
limits = ~ c(pretty(.x)[1], tail(pretty(.x), 1)),
25+
expand = expansion(mult = c(0))
26+
),
2427
scale_x_continuous(labels = scales::comma, expand = c(0, 0))
2528
)
2629
}
@@ -39,7 +42,7 @@ theme_coverage <- function(space = 0.2) {
3942
theme_coverage2 <- function(space = 0.2) {
4043
list(
4144
scale_y_continuous(
42-
limits = ~ c(0, CeilingNumber(max(.x))),
45+
limits = ~ range(pretty(.x)),
4346
breaks = ~ .x[2],
4447
expand = expansion(mult = c(0))
4548
),
@@ -484,7 +487,7 @@ theme_cnv <- function(x.range, margin.len) {
484487
)
485488
}
486489

487-
# theme for ggprotein: suitable for range position is in
490+
# theme for ggprotein: suitable for range.position = "in"
488491
#' Theme for geom_protein.
489492
#'
490493
#' @return List of layers.
@@ -508,7 +511,7 @@ theme_protein <- function() {
508511
)
509512
}
510513

511-
# theme for ggprotein: suitable for range position is out
514+
# theme for ggprotein: suitable for range.position = "out"
512515
#' Theme for geom_protein.
513516
#'
514517
#' @return List of layers.
@@ -520,7 +523,7 @@ theme_protein <- function() {
520523
theme_protein2 <- function() {
521524
list(
522525
scale_y_continuous(
523-
limits = ~ c(0, CeilingNumber(max(.x)), digits = 2),
526+
limits = ~ range(pretty(.x)),
524527
breaks = ~ .x[2],
525528
expand = expansion(mult = c(0)),
526529
labels = function(x) format(x, scientific = TRUE, digits = 2)

0 commit comments

Comments
 (0)