Skip to content

Commit a76a704

Browse files
committed
avg coverage
1 parent e9fb5ee commit a76a704

File tree

9 files changed

+111
-30
lines changed

9 files changed

+111
-30
lines changed

NEWS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
## Minor changes
88
* `geom_coverage` and `ggcoverage` supports changing shared type of y-axis scales across facets (with `facet.y.scale` parameter).
9-
* `geom_coverage` and `ggcoverage` supports joint view of multiple tracks.
9+
* `geom_coverage` and `ggcoverage` supports joint view of multiple tracks (with `plot.type` parameter).
10+
* `geom_coverage` and `ggcoverage` supports joint view of average coverage of multiple tracks (with `joint.avg` parameter).
1011
* `LoadTrackFile` loads bam directly instead of converting bam to bigwig and loading the bigwig.
1112

1213
-------------

R/geom_coverage.R

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
#' @param single.nuc Logical value, whether to visualize at single nucleotide level (use bar plot). Default: FALSE.
88
#' @param plot.type The type of the plot, choose from facet (separate plot for every sample) and
99
#' joint (combine all sample in a single plot). Default: facet.
10-
#' @param facet.key Sample type key to create coverage plot. Default: Type.
10+
#' @param facet.key Sample type key to create coverage plot. Used in both facet and joint plot. Default: Type.
11+
#' @param joint.avg Logical value, whether to show average coverage across \code{group.key}. Default: FALSE.
1112
#' @param facet.order The order of coverage plot. Default: NULL.
1213
#' @param facet.color The color of sample text. Default: NULL (select automatically).
1314
#' @param facet.y.scale The shared type of y-axis scales across facets, choose from free (facets have different y-axis scales),
@@ -52,7 +53,7 @@
5253
#' # geom_coverage(data = track.df, color = "auto", mark.region = NULL)
5354
geom_coverage <- function(data, mapping = NULL, color = NULL, rect.color = NA,
5455
single.nuc = FALSE, plot.type = c("facet", "joint"),
55-
facet.key = "Type", facet.order = NULL,
56+
facet.key = "Type", joint.avg = FALSE, facet.order = NULL,
5657
facet.color = NULL, facet.y.scale = c("free", "fixed"),
5758
group.key = "Group", range.size = 3, range.position = c("in", "out"),
5859
mark.region = NULL, mark.color = "grey", mark.alpha = 0.5,
@@ -231,28 +232,60 @@ geom_coverage <- function(data, mapping = NULL, color = NULL, rect.color = NA,
231232
plot.ele <- append(plot.ele, region.range)
232233
}
233234
} else if (plot.type == "joint") {
234-
# preocess data
235235
data.split <- split(x = data, f = data[, facet.key])
236-
data.split.final <- lapply(data.split, function(ds) {
237-
ds <- ds[order(ds$start), ]
238-
ds.ll <- ds[nrow(ds), ]
239-
if (ds.ll$start < ds.ll$end) {
240-
ds.ll.new <- ds.ll
241-
ds.ll.new$start <- ds.ll.new$end
242-
ds.final <- rbind(ds, ds.ll.new) %>% as.data.frame()
243-
} else {
244-
ds.final <- ds
245-
}
246-
rownames(ds.final) <- NULL
247-
return(ds.final)
248-
})
249-
# get final data
250-
data.final <- do.call(rbind, data.split.final) %>% as.data.frame()
251-
rownames(data.final) <- NULL
236+
if (joint.avg) {
237+
# process data
238+
data.list <- lapply(data.split, function(ds) {
239+
ds.list <- apply(ds, 1, function(x) {
240+
x.start <- seq(x["start"], x["end"])
241+
x.len <- length(x.start)
242+
x.df <- data.frame(
243+
seqnames = rep(x["seqnames"], x.len), start = x.start,
244+
score = rep(x["score"], x.len)
245+
)
246+
x.df[, facet.key] <- x[facet.key]
247+
x.df[, group.key] <- x[group.key]
248+
x.df
249+
})
250+
ds.final <- do.call(rbind, ds.list)
251+
rownames(ds.final) <- NULL
252+
return(ds.final)
253+
})
254+
data.final <- do.call(rbind, data.list)
255+
rownames(data.final) <- NULL
256+
# as numeric
257+
data.final$score <- as.numeric(data.final$score)
258+
data.final$start <- as.numeric(data.final$start)
259+
# get average
260+
data.final.mean <- data.final %>%
261+
dplyr::group_by(.data[[group.key]], .data[["start"]]) %>%
262+
dplyr::summarise(score = mean(.data[["score"]]))
263+
# create avg plot
264+
avg.plot <- geom_line(data = data.final.mean, mapping = mapping)
265+
plot.ele <- list(avg.plot)
266+
} else {
267+
# preocess data
268+
data.split.final <- lapply(data.split, function(ds) {
269+
ds <- ds[order(ds$start), ]
270+
ds.ll <- ds[nrow(ds), ]
271+
if (ds.ll$start < ds.ll$end) {
272+
ds.ll.new <- ds.ll
273+
ds.ll.new$start <- ds.ll.new$end
274+
ds.final <- rbind(ds, ds.ll.new) %>% as.data.frame()
275+
} else {
276+
ds.final <- ds
277+
}
278+
rownames(ds.final) <- NULL
279+
return(ds.final)
280+
})
281+
# get final data
282+
data.final <- do.call(rbind, data.split.final) %>% as.data.frame()
283+
rownames(data.final) <- NULL
252284

253-
# create step plot
254-
region.step <- geom_step(data = data.final, mapping = mapping)
255-
plot.ele <- list(region.step)
285+
# create step plot
286+
region.step <- geom_step(data = data.final, mapping = mapping)
287+
plot.ele <- list(region.step)
288+
}
256289
}
257290

258291
# add rect

R/ggcoverage.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#' @param plot.type The type of the plot, choose from facet (separate plot for every sample) and
99
#' joint (combine all sample in a single plot). Default: facet.
1010
#' @param facet.key Sample type key to create coverage plot. Default: Type.
11+
#' @param joint.avg Logical value, whether to show average coverage across \code{group.key}. Default: FALSE.
1112
#' @param facet.order The order of Coverage plot. Default: NULL.
1213
#' @param facet.color The color of sample text. Default: NULL (select automatically).
1314
#' @param facet.y.scale The shared type of y-axis scales across facets, choose from free (facets have different y-axis scales),
@@ -30,7 +31,7 @@
3031
#' @importFrom scales comma
3132
#' @importFrom grDevices colorRampPalette
3233
#' @importFrom RColorBrewer brewer.pal
33-
#' @importFrom rlang as_label
34+
#' @importFrom rlang as_label .data
3435
#' @importFrom stats as.formula
3536
#' @importFrom ggh4x facet_wrap2 strip_themed
3637
#' @importFrom dplyr group_by summarise
@@ -53,7 +54,7 @@
5354
#' # gtf.gr <- rtracklayer::import.gff(con = gtf.file, format = "gtf")
5455
#' # ggcoverage(data = track.df, color = "auto", range.position = "out")
5556
ggcoverage <- function(data, single.nuc = FALSE, mapping = NULL, color = NULL,
56-
rect.color = NA, plot.type = c("facet", "joint"), facet.key = "Type",
57+
rect.color = NA, plot.type = c("facet", "joint"), facet.key = "Type", joint.avg = FALSE,
5758
facet.order = NULL, facet.color = NULL, facet.y.scale = c("free", "fixed"),
5859
group.key = "Group", range.size = 3, range.position = c("in", "out"), plot.space = 0.2,
5960
mark.region = NULL, mark.color = "grey", mark.alpha = 0.5, show.mark.label = TRUE, mark.label.size = 4) {
@@ -65,7 +66,7 @@ ggcoverage <- function(data, single.nuc = FALSE, mapping = NULL, color = NULL,
6566
geom_coverage(
6667
data = data, mapping = mapping, color = color, rect.color = rect.color,
6768
single.nuc = single.nuc, plot.type = plot.type,
68-
facet.key = facet.key, facet.order = facet.order,
69+
facet.key = facet.key, joint.avg = joint.avg, facet.order = facet.order,
6970
facet.color = facet.color, facet.y.scale = facet.y.scale,
7071
group.key = group.key, range.size = range.size, range.position = range.position,
7172
mark.region = mark.region, mark.color = mark.color, mark.alpha = mark.alpha, show.mark.label = show.mark.label,

README.Rmd

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,19 @@ The basic coverage plot has **two types**:
119119
* **joint**: Visualize all tracks in a single plot.
120120

121121
#### joint view
122+
Create line plot for **every sample** (`facet.key = "Type"`) and color by **every sample** (`group.key = "Type"`):
122123
```{r basic_coverage_joint, warning=FALSE, fig.height = 4, fig.width = 12, fig.align = "center"}
123124
basic.coverage = ggcoverage(data = track.df, color = "auto",
124-
plot.type = "joint", group.key = "Type",
125+
plot.type = "joint", facet.key = "Type", group.key = "Type",
126+
mark.region = mark.region, range.position = "out")
127+
basic.coverage
128+
```
129+
130+
Create **group average line plot** (sample is indicated by `facet.key = "Type"`, group is indicated by `group.key = "Group"`):
131+
```{r basic_coverage_joint_avg, warning=FALSE, fig.height = 4, fig.width = 12, fig.align = "center"}
132+
basic.coverage = ggcoverage(data = track.df, color = "auto",
133+
plot.type = "joint", facet.key = "Type", group.key = "Group",
134+
joint.avg = TRUE,
125135
mark.region = mark.region, range.position = "out")
126136
basic.coverage
127137
```

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,15 +158,31 @@ The basic coverage plot has **two types**:
158158

159159
#### joint view
160160

161+
Create line plot for **every sample** (`facet.key = "Type"`) and color
162+
by **every sample** (`group.key = "Type"`):
163+
161164
``` r
162165
basic.coverage = ggcoverage(data = track.df, color = "auto",
163-
plot.type = "joint", group.key = "Type",
166+
plot.type = "joint", facet.key = "Type", group.key = "Type",
164167
mark.region = mark.region, range.position = "out")
165168
basic.coverage
166169
```
167170

168171
<img src="man/figures/README-basic_coverage_joint-1.png" width="100%" style="display: block; margin: auto;" />
169172

173+
Create **group average line plot** (sample is indicated by
174+
`facet.key = "Type"`, group is indicated by `group.key = "Group"`):
175+
176+
``` r
177+
basic.coverage = ggcoverage(data = track.df, color = "auto",
178+
plot.type = "joint", facet.key = "Type", group.key = "Group",
179+
joint.avg = TRUE,
180+
mark.region = mark.region, range.position = "out")
181+
basic.coverage
182+
```
183+
184+
<img src="man/figures/README-basic_coverage_joint_avg-1.png" width="100%" style="display: block; margin: auto;" />
185+
170186
#### facet view
171187

172188
``` r
Loading

man/geom_coverage.Rd

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/ggcoverage.Rd

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vignettes/ggcoverage.Rmd

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,10 @@ The basic coverage plot has **two types**:
136136
* **joint**: Visualize all tracks in a single plot.
137137

138138
#### joint view
139+
Create line plot for **every sample** (`facet.key = "Type"`) and color by **every sample** (`group.key = "Type"`):
139140
```{r basic_coverage_joint, eval=FALSE}
140141
basic.coverage = ggcoverage(data = track.df, color = "auto",
141-
plot.type = "joint", group.key = "Type",
142+
plot.type = "joint", facet.key = "Type", group.key = "Type",
142143
mark.region = mark.region, range.position = "out")
143144
basic.coverage
144145
```
@@ -147,6 +148,19 @@ basic.coverage
147148
knitr::include_graphics("../man/figures/README-basic_coverage_joint-1.png")
148149
```
149150

151+
Create **group average line plot** (sample is indicated by `facet.key = "Type"`, group is indicated by `group.key = "Group"`):
152+
```{r basic_coverage_joint_avg, eval=FALSE}
153+
basic.coverage = ggcoverage(data = track.df, color = "auto",
154+
plot.type = "joint", facet.key = "Type", group.key = "Group",
155+
joint.avg = TRUE,
156+
mark.region = mark.region, range.position = "out")
157+
basic.coverage
158+
```
159+
160+
```{r basic_coverage_joint_avg_plot, echo=FALSE, fig.height = 4, fig.width = 12, fig.align = "center"}
161+
knitr::include_graphics("../man/figures/README-basic_coverage_joint_avg-1.png")
162+
```
163+
150164
#### facet view
151165
```{r basic_coverage, eval=FALSE}
152166
basic.coverage = ggcoverage(data = track.df, color = "auto", plot.type = "facet",

0 commit comments

Comments
 (0)