Title: | Hilbert curve visualization |
---|---|
Description: | Functions to visualize long vectors of integer data by means of Hilbert curves |
Authors: | Simon Anders <[email protected]> |
Maintainer: | Simon Anders <[email protected]> |
License: | GPL (>= 3) |
Version: | 1.65.0 |
Built: | 2024-10-30 07:28:58 UTC |
Source: | https://github.com/bioc/HilbertVis |
These functions calculate the Hilbert curve in its finite approximations.
hilbertCurvePoint
gives the coordinates of one point and hilbertCurve
returns an array with the coordinates of all 4^lv
points. The
functions are not needed for hilbertImage
and only provided
for demonstration purposes. plotHilbertCurve
makes use of them.
hilbertCurve( lv ) hilbertCurvePoint( t, lv )
hilbertCurve( lv ) hilbertCurvePoint( t, lv )
lv |
The iteration level. A Hilbert curve of level |
t |
The point index in the Hilbert curve. Must be an integer in |
hilbertCurvePoint
returns a vector of two integer numbers, both in the range
0:(2^lv-1)
, indicating the coordinates of point t
. huilbertCurve
returns a
matrix with 4^lv
rows and 2 columns, giving all points of the curve at level lv
.
Simon Anders, EMBL-EBI, [email protected]
hilbertCurvePoint( 67, 4 ) hilbertCurve( 4 )
hilbertCurvePoint( 67, 4 ) hilbertCurve( 4 )
Calculate a Hilbert curve visualization of a long data vector and return it as a square matrix.
hilbertImage(data, level = 9, mode = "absmax")
hilbertImage(data, level = 9, mode = "absmax")
data |
A (potentially very long) vector of numerical data. |
level |
The level of the Hilbert curve, determining the size of the returned matrix |
mode |
The binning mode. See |
See the package vignette for an explanation of this visualization technique.
A matrix of dimension 2^level
x 2^level
. Each matrix element corresponds to a bin of consecutive elements
of the data vector, the bins arranged to follow the Hilbert curve of the given level. By default, the value of a
matrix element is either the largest or smallest element in the bin, whichever is larger by absolute value. (See
shrinkVector
for other possible binning modes.)
To display such a matrix graphically, you can use the standard functions image
or levelplot
but the function showHilbertImage
may be more convenient.
For an interactive GUI to explore a Hilbert curve visualisation,
use the function
hilbertDisplay
in the
HilbertVisGUI
package.
Simon Anders, EMBL-EBI, [email protected]
# Get a vector with example data dataVec <- makeRandomTestData( ) # Plot it in conventional (linear) fashion plotLongVector( dataVec ) # Note how the peaks look quite uniform # Get the Hilbert curve matrix hMat <- hilbertImage( dataVec ) # Plot it with the 'showHilbertImage' function showHilbertImage( hMat ) # Note how you can now see the non-uniformity hidden in the previous plot. # Note also the ugly aliasing when you change the size of the plot window. # Using EBImage allows to display in each matrix element as one pixel: # if( require ( EBImage ) ) # showHilbertImage( hMat, mode="EBImage" )
# Get a vector with example data dataVec <- makeRandomTestData( ) # Plot it in conventional (linear) fashion plotLongVector( dataVec ) # Note how the peaks look quite uniform # Get the Hilbert curve matrix hMat <- hilbertImage( dataVec ) # Plot it with the 'showHilbertImage' function showHilbertImage( hMat ) # Note how you can now see the non-uniformity hidden in the previous plot. # Note also the ugly aliasing when you change the size of the plot window. # Using EBImage allows to display in each matrix element as one pixel: # if( require ( EBImage ) ) # showHilbertImage( hMat, mode="EBImage" )
This function generates a long numeric vector and fills it with many narrow Gaussian peaks of varying width and position. Around 30 the distribution of peak width is changed to be substantially larger. This feature is easily visible with the Hilbert curve visualization but much harder to spot with conventional 1D plots.
makeRandomTestData(len = 1e+07, numPeaks = 500)
makeRandomTestData(len = 1e+07, numPeaks = 500)
len |
Length of the vector |
numPeaks |
Number of peaks to be placed in the vector |
A vector, of type 'numeric', with sample data.
Simon Anders, EMBL-EBI, [email protected]
# See the help page of function 'hilbertImage' for an example.
# See the help page of function 'hilbertImage' for an example.
Given intervals in the form of a "start" and an "end" vectors and corresponding values, generate a "wiggle vector" of a given length that contains the specified values in the vector elements indicated by the intervals.
makeWiggleVector(start, end, value, chrlength )
makeWiggleVector(start, end, value, chrlength )
start |
The start coordinates of the intervals. As usual in R, these are 1-based. |
end |
The end coordinates of the intervals. As usual, the end points are included. |
value |
The values to be put in the wiggle vector. Where intervals overlap, the values are added. |
chrlength |
The desired length of the returned vector. |
A vector as described above.
Simon Anders, EMBL-EBI, sanders\@fs.tum.de
For a value vector containing only ones, this function acts similar as
the pileup
function in the ShortRead package.
intervalStarts <- c(3,10,17,22) intervalEnds <- c(7,13,20,26) values <- c(2, 1.5, .3, 4) chrlength <- 30 wig <- makeWiggleVector( intervalStarts, intervalEnds, values, chrlength ) # The same effect can be achieved with the following R code, which, however # is much slower: wig2 <- numeric(chrlength) for( i in 1:length(values) ) wig2[ intervalStarts[i]:intervalEnds[i] ] <- wig2[ intervalStarts[i]:intervalEnds[i] ] + values[i] # Let's check that we got the same: all( wig == wig2 )
intervalStarts <- c(3,10,17,22) intervalEnds <- c(7,13,20,26) values <- c(2, 1.5, .3, 4) chrlength <- 30 wig <- makeWiggleVector( intervalStarts, intervalEnds, values, chrlength ) # The same effect can be achieved with the following R code, which, however # is much slower: wig2 <- numeric(chrlength) for( i in 1:length(values) ) wig2[ intervalStarts[i]:intervalEnds[i] ] <- wig2[ intervalStarts[i]:intervalEnds[i] ] + values[i] # Let's check that we got the same: all( wig == wig2 )
This function plots the Hilbert curve fractal at a chosen iteration level in order to give you an impression how it looks like.
plotHilbertCurve( lv, new.page = TRUE )
plotHilbertCurve( lv, new.page = TRUE )
lv |
The iteration level. A Hilbert curve of level |
new.page |
Boolean indicating whether to start a new graphics page (default: yes). |
An invisble NULL is returned. Furthermore, a plot is created.
Simon Anders, EMBL-EBI, [email protected]
plotHilbertCurve( 3 )
plotHilbertCurve( 3 )
This function does basically the same as just calling plot( vec )
but is much faster in case of a very long vector. This is because it first calls shrinkVector
.
plotLongVector(vec, offset = 1, shrinkLength = 4000, xlab = "", ylab = "", ...)
plotLongVector(vec, offset = 1, shrinkLength = 4000, xlab = "", ylab = "", ...)
vec |
The numerical vector to be plotted. May be an ordinary or an IRanges::Rle vector. |
offset |
The x axis is labelled with numbers from |
shrinkLength |
To which length to shrink the vector before plotting it. Should be at least the width of your plot in pixels. |
xlab |
The label of the x axis, to be passed to |
ylab |
The label of the y axis, to be passed to |
... |
Further arguments to be passed to |
Invisible Null and a plot.
Simon Anders, EMBL-EBI, [email protected]
plotLongVector( rep( 1:100000, 20 ) )
plotLongVector( rep( 1:100000, 20 ) )
A convenient wrapper around levelplot
to display a hilbert image matrix as
it is returned by hilbertImage
. Alternatively to levelplot
,
EBImage
is available as well.
showHilbertImage( mat, palettePos = colorRampPalette(c("white", "red"))(300), paletteNeg = colorRampPalette(c("white", "blue"))(300), maxPaletteValue = max(abs(mat)), mode = c("lattice", "EBImage", "EBImage-batch") )
showHilbertImage( mat, palettePos = colorRampPalette(c("white", "red"))(300), paletteNeg = colorRampPalette(c("white", "blue"))(300), maxPaletteValue = max(abs(mat)), mode = c("lattice", "EBImage", "EBImage-batch") )
mat |
The matrix to be displayed. In principle this can be any matrix,
but typically, it is one returned by |
palettePos |
The colour palette to be used for the positive entries in |
paletteNeg |
The colour palette to be used for the negative entries in |
maxPaletteValue |
The absolute value to which the right end of the palettes should correspond. (The left ends correspond to 0.) |
mode |
For mode "lattice", the function |
A lattice or EBImage graphics object. For all modes except “EBImage-batch” it is marked “invisible”.
Simon Anders, EMBL-EBI (sanders\@fs.tum.de)
# See ?hilbertImage for examples.
# See ?hilbertImage for examples.
Given a (potentially very long) vector, the vector is partitioned into a given number of (up to rounding errors) equally long bins, and a vector summerizing each of the bins with one number it returned.
shrinkVector(vec, newLength, mode = c("max", "min", "absmax", "mean"))
shrinkVector(vec, newLength, mode = c("max", "min", "absmax", "mean"))
vec |
The vector to be shrunk. May be an ordinary numeric or integer vector or an IRanges::Rle vector. |
newLength |
The desired size of the return vector, i.e., the number of partitions |
mode |
the summerization mode: 'max': take the maximal value of each bin; 'min': take the minimal value of each bin; 'absmax': take the value with largest absolute value; 'mean': take the mean of the bin values. |
A vector of length newLength
with the summary values of each of the bin
of vector
.
Simon Anders, EMBL-EBI (sanders\@fs.tum.de)
plotLongVector
,
Rsamtools::pileup
,
HilbertVisGui::simpleLinPlot
shrinkVector( 100000 + 1:1000, 17 )
shrinkVector( 100000 + 1:1000, 17 )