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

kpPlotDensity log scaling? #156

Open
aboyd003 opened this issue Oct 17, 2024 · 3 comments
Open

kpPlotDensity log scaling? #156

aboyd003 opened this issue Oct 17, 2024 · 3 comments
Assignees

Comments

@aboyd003
Copy link

Hi all,

Is there any method to log scale the values for the density plot? Ie I want the entire track output by kpPlotDensity to be on log scale. I read through the documentation and did not find anything referencing log scaling with respect to kpPlotDensity.

@bernatgel
Copy link
Owner

Hi @aboyd003

It is indeed not possible to set up a log scale for kpPlotDensity. To do it right, we should probably use the log capabilities of base R and implement it through the whole lot of plotting functions, and adapt also kpAxis to reflect that. It's doable, but not in a short time frame.

If you really need it now, your best option would be to create your own version of kpPlotDensity function (its only 16 lines of code) and make compute the log of the density before plotting. This should work (untested!)

"""
myLogKpPlotDensity <- function (karyoplot, data = NULL, window.size = 1e+06, ymin = NULL,
ymax = NULL, data.panel = 1, r0 = NULL, r1 = NULL, clipping = TRUE,
...)
{
if (!methods::is(karyoplot, "KaryoPlot"))
stop(paste0("In kpPlotDensity: 'karyoplot' must be a valid 'KaryoPlot' object"))
if (!methods::is(data, "GRanges"))
stop(paste0("In kpPlotDensity: 'data' must be a valid 'GRanges' object"))
windows <- tileGenome(karyoplot$chromosome.length, tilewidth = window.size,
cut.last.tile.in.chrom = TRUE)
dens <- GenomicRanges::countOverlaps(windows, data)

#MODIFIED CODE
dens <- log(x=dens, base=10)
#END MODIFIED CODE

if (is.null(ymax)) {
    ymax <- max(dens)
}
karyoplot <- kpPlotRibbon(karyoplot, data = windows, y0 = 0, 
    y1 = dens, ymin = ymin, ymax = ymax, data.panel = data.panel, 
    r0 = r0, r1 = r1, clipping = clipping, ...)
karyoplot$latest.plot <- list(funct = "kpPlotDensity", computed.values = list(density = dens, 
    windows = windows, max.density = max(dens)))
invisible(karyoplot)

}

"""

and then adjust the axis. If for the axis you get the maximum value from kp$latestplot... it will be already in log scale. If you compute it before plotting, then remember to take the log too.

Hope this helps

Bernat

@bernatgel bernatgel self-assigned this Oct 17, 2024
@aboyd003
Copy link
Author

Thank you.
I used your modified function :) and used it to modify the kpPlotBAMDensity function as well :)

@bernatgel
Copy link
Owner

bernatgel commented Oct 17, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants