Skip to content

Commit 68e6568

Browse files
committed
highlight snv
1 parent 6e847e2 commit 68e6568

File tree

3 files changed

+53
-26
lines changed

3 files changed

+53
-26
lines changed

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# ggcoverage 1.1.0
22
## Major changes
3-
* Mark SNV with twill or star.
3+
* Mark SNV with twill (add twill to position with SNV), star (add star mark to position with SNV), and highlight (position without SNV is grey).
44
* Moved `FormatTrack` to `LoadTrackFile`, this can reduce load time and memory for big files.
55
* Supporting parallel to handle multiple files more efficiently.
66

R/geom_base.R

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
#' @param guide.line Nucleotide frequency guide line. Default: NULL (0.5).
1414
#' @param guide.line.color The color of guide line. Default: "red".
1515
#' @param guide.line.type The line type of guide line. Default: "dashed".
16-
#' @param mark.type The mark type to highlight SNV position, choose from twill and star. Default: twill.
16+
#' @param mark.type The mark type to highlight SNV position, choose from twill (add twill to position with SNV),
17+
#' star (add star mark to position with SNV), and highlight (position without SNV is grey). Default: twill.
1718
#' @param star.size The size of star when \code{mark.type} is star. Default: 1.
1819
#' @param show.aa Logical value, whether to show amino acid. Default: TRUE.
1920
#' @param sens Sense to translate: F for forward sense and R for reverse sense.
@@ -186,6 +187,12 @@ ggplot_add.base <- function(object, plot, object_name) {
186187
dplyr::pull(Pos) %>%
187188
unique()
188189
alt.pos.nuc.freq.long <- pos.nuc.freq.long %>% dplyr::filter(Pos %in% c(alt.pos))
190+
# get position without alt
191+
ref.pos <- pos.nuc.freq.long %>%
192+
dplyr::filter(Ref == Base & Total == Freq) %>%
193+
dplyr::pull(Pos) %>%
194+
unique()
195+
ref.pos.nuc.freq.long <- pos.nuc.freq.long %>% dplyr::filter(Pos %in% c(ref.pos))
189196
# create label offset
190197
pos.nuc.freq$Offset <- nuc.offset
191198
# add guide line
@@ -194,36 +201,55 @@ ggplot_add.base <- function(object, plot, object_name) {
194201
guide.line <- 0.5
195202
}
196203

197-
# create plot
198-
base.plot <- ggplot() +
199-
geom_bar(
200-
data = pos.nuc.freq.long, aes_string(x = "Pos", y = "Freq", fill = "Base"),
201-
position = "fill", stat = "identity", color = "white"
202-
)
203-
204204
# add mark
205205
if (length(alt.pos) >= 1) {
206-
if (mark.type == "twill") {
207-
base.plot <- base.plot +
208-
ggpattern::geom_col_pattern(
209-
data = alt.pos.nuc.freq.long,
210-
aes_string(
211-
x = "Pos", y = "Freq", pattern = "Base", fill = "Base",
212-
pattern_fill = "Base", pattern_angle = "Base"
213-
),
214-
position = "fill", colour = "black",
215-
pattern_density = 0.35, pattern_key_scale_factor = 1.3
206+
if (mark.type %in% c("twill", "star")) {
207+
# create plot
208+
base.plot <- ggplot() +
209+
geom_bar(
210+
data = pos.nuc.freq.long, aes_string(x = "Pos", y = "Freq", fill = "Base"),
211+
position = "fill", stat = "identity", color = "white"
212+
)
213+
if (mark.type == "twill") {
214+
base.plot <- base.plot +
215+
ggpattern::geom_col_pattern(
216+
data = alt.pos.nuc.freq.long,
217+
aes_string(
218+
x = "Pos", y = "Freq", pattern = "Base", fill = "Base",
219+
pattern_fill = "Base", pattern_angle = "Base"
220+
),
221+
position = "fill", colour = "black",
222+
pattern_density = 0.35, pattern_key_scale_factor = 1.3
223+
) +
224+
ggpattern::scale_pattern_fill_manual(values = c(nuc.color, "white"))
225+
} else if (mark.type == "star") {
226+
base.plot <- base.plot +
227+
geom_point(
228+
data = alt.pos.nuc.freq.long, aes_string(x = "Pos", y = "1.01"),
229+
shape = 8, show.legend = FALSE, size = star.size
230+
)
231+
}
232+
} else if (mark.type == "highlight") {
233+
base.plot <- ggplot() +
234+
geom_bar(
235+
data = ref.pos.nuc.freq.long, aes_string(x = "Pos", y = "Freq"),
236+
position = "fill", stat = "identity", color = "white", fill = "grey"
216237
) +
217-
ggpattern::scale_pattern_fill_manual(values = c(nuc.color, "white"))
218-
} else if (mark.type == "star") {
219-
base.plot <- base.plot +
220-
geom_point(
221-
data = alt.pos.nuc.freq.long, aes_string(x = "Pos", y = "1.01"),
222-
shape = 8, show.legend = FALSE, size = star.size
238+
geom_bar(
239+
data = alt.pos.nuc.freq.long, aes_string(x = "Pos", y = "Freq", fill = "Base"),
240+
position = "fill", stat = "identity", color = "white"
223241
)
242+
} else {
243+
stop("The mark.type you provided is not valid, please choose from twill, star, highlight.")
224244
}
225245
} else {
226246
message("No SNV detected, do not add mark!")
247+
# create plot
248+
base.plot <- ggplot() +
249+
geom_bar(
250+
data = pos.nuc.freq.long, aes_string(x = "Pos", y = "Freq", fill = "Base"),
251+
position = "fill", stat = "identity", color = "white"
252+
)
227253
}
228254

229255
if (show.aa) {

man/geom_base.Rd

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

0 commit comments

Comments
 (0)