Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

any smooth parameter #32

Closed
ileump opened this issue May 9, 2024 · 5 comments
Closed

any smooth parameter #32

ileump opened this issue May 9, 2024 · 5 comments

Comments

@ileump
Copy link

ileump commented May 9, 2024

hi,
I'd like to ask if ggcoverage has parameters related to smooth?

@m-jahn
Copy link
Collaborator

m-jahn commented May 13, 2024

Hi!
No sorry, there is no smoothing parameter to really smooth the coverage plots. However, since the input are regular data frames you can apply any type of smoothing to the data beforehand. See these two examples, using rollapply from the zoo package. The result is not super pretty with such sparse data but I think the message is clear.

library(ggcoverage)
library(ggplot2)
library(zoo)

# import track data
meta.file <- system.file("extdata", "RNA-seq", "meta_info.csv", package = "ggcoverage")
sample.meta <- utils::read.csv(meta.file)
track.folder <- system.file("extdata", "RNA-seq", package = "ggcoverage")

track.df <- LoadTrackFile(
  track.folder = track.folder,
  format = "bw",
  meta.info = sample.meta,
  region = "chr14:21,677,306-21,737,601"
)

# plot tracks without smoothing
ggplot() +
  geom_coverage(data = track.df, facet.key = "Type", group.key = "Group")

image

# plot tracks with smooting
track.df <- track.df %>%
  group_by(Type) %>%
  mutate(score = zoo::rollapply(score, width = 5, FUN = mean, fill = 0)) %>%
  as.data.frame()

ggplot() +
  geom_coverage(data = track.df, facet.key = "Type", group.key = "Group")

image

@ileump
Copy link
Author

ileump commented May 13, 2024

image

image
Thank you for your response. About the 'smooth' parameter, previously I visualized the .bw files using https://epigenomegateway.wustl.edu/browser/, as shown in the figure. I'm not sure if the 'smooth' parameter in that tool is consistent with the method you mentioned?

@m-jahn
Copy link
Collaborator

m-jahn commented May 13, 2024

No this is for sure something different. My example was only to show that you can apply any function of choice to the peak/score data. It's an R-builtin data type, the data.frame, so you can scale or transform the data as you like before plotting. To find a smoothing function that works for you is up to you though.

@ileump
Copy link
Author

ileump commented May 13, 2024

Thank for your time.

@m-jahn
Copy link
Collaborator

m-jahn commented May 14, 2024

Maybe smooth.spline is the function you are looking for to smooth your Y-data.
This example works with an equal-spaced coverage (10 nt window) from a random bam file.
Compare the upper non-smoothed with lower smoothed coverage.

library(ggcoverage)
library(tidyverse)

track.df <- LoadTrackFile(
  track.file = "~/Downloads/spn_filt.bam",
  format = "bam",
  norm.method = "None"
)

# add track with smoothed Y data
track.df <- bind_rows(
  track.df,
  track.df %>%
  mutate(
    score = smooth.spline(x = start, y = score, spar = 0.2)$y,
    Type = "spn_filt Smoothed"
  ) %>%
  as.data.frame()
)

ggplot() +
  geom_coverage(data = track.df, facet.key = "Type", group.key = "Group")

image

Sorry for the quality, this is an artifact of plotting high density bars in low res.

@m-jahn m-jahn closed this as completed May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants