Fast embedding and visualization of multidimensional datasets, originally intended for flow/mass cytometry data. Compatible with FlowSOM (https://github.com/SofieVG/FlowSOM).
You may read more about EmbedSOM in the research paper here:
Miroslav Kratochvíl, Abhishek Koladiya, and Jiří Vondrášek. “Generalized EmbedSOM on quadtree-structured self-organizing maps” F1000Research 8 (2019). doi:10.12688/f1000research.21642.2
Use devtools
:
::install_github('exaexa/EmbedSOM') devtools
EmbedSOM works by aligning the points to a precomputed
self-organizing map (SOM). The main function EmbedSOM
takes
the SOM and data, and returns a matrix with 2D point coordinates on each
row.
Quick way to get a visualization of multidimensional points saved in
rows of d
:
<- EmbedSOM::SOM(d, xdim=20, ydim=20) # compute the self-organizing map
map <- EmbedSOM::EmbedSOM(d, map) # compute 2D coordinates of points
e ::PlotEmbed(e) # plot the result with density EmbedSOM
There are some parameters that affect speed, precision and shape of
the embedding. Use ?EmbedSOM::EmbedSOM
to explore them in
the documentation.
To get started quickly, you can have a look at the vignettes:
Use flowCore
functionality to add any information to a
FCS. The following template saves the scaled FlowSOM object data as-is,
together with the embedding:
<- FlowSOM::ReadInput('original.fcs', scale=T, ...)
fs <- FlowSOM::BuildSOM(fs, ...)
fs <- EmbedSOM::EmbedSOM(fs, ...)
e ::write.FCS(new('flowFrame',
flowCoreexprs=as.matrix(data.frame(fs$data,
embedsom1=e[,1],
embedsom2=e[,2]))),
'original_with_embedding.fcs')
See flowCore
documentation for information about
advanced FCS-writing functionality, e.g. for column descriptions.
Train a SOM on an aggregate file, and use it to embed the individual files. It is important to always apply the same scaling and transformations on all input files.
<- FlowSOM::ReadInput(
fs ::AggregateFlowFrames(c('file1.fcs', 'file2.fcs', ...),
FlowSOMcTotal=100000),
scale=T, transform=...)
<- length(fs$scaled.scale)-2
n <- FlowSOM::SOM(fs)
map
<- FlowSOM::ReadInput('file1.fcs',
fs1 scale=T,
scaled.scale=fs$scaled.scale[1:n],
scaled.center=fs$scaled.center[1:n],
transform=...)
<- EmbedSOM::EmbedSOM(fs1, map=map)
e1 ::PlotEmbed(e1, fsom=fs1)
EmbedSOM# repeat as needed for file2.fcs, etc.
See documentation in ?PlotEmbed
. By default,
PlotEmbed
plots a simple colored representation of point
density. If supplied with a FCS column name (or number), it uses the a
color scale similar to ColorBrewer’s RdYlBu (with improvements for
transparent stuff) to plot a single marker expression. Parameters
red
, green
and blue
can be used
to set column names (or numbers) to mix RGB color from marker
expressions.
PlotEmbed
optionally accepts parameter col
with a vector of R colors, which, if provided, is just forwarded to the
internal plot
function. For example, use
col=rgb(0,0,0,0.2)
for transparent black points.
New! if you need to mix more nicer colors than just
the default RGB, use ExprColors
.
Use scattermore: https://github.com/exaexa/scattermore
A pretty fast (and still precise) way to dissect the dataset is to run a metaclustering on SOM clusters, and map the result to the individual points:
<- cutree(k=10, hclust(method='average', dist(map$codes)))[map$mapping[,1]] clusters
After that, the metaclusters can be plotted in the embedding. Because the clustering is related to the small SOM-defined “pre-clusters” rather than the individual points, it is necessary to map the individual points to these clusters first:
::PlotEmbed(e, clust=clusters) EmbedSOM
After you choose a metacluster in the embedding, use the color scale
to find its number, then filter the points in your dataset to the
corresponding subset. This example selects the point subset in
metacluster number 5
and 6
:
<- d[clusters %in% c(5,6), ] d
There is now support for 3D SOM grids and 3D embedding. You need the customized SOM function from EmbedSOM:
<- EmbedSOM::SOM(someData, xdim=8, ydim=8, zdim=8)
map <- EmbedSOM::EmbedSOM(data=someData, map=map) e
PlotEmbed
and other functions do not work on 3D points
in embed
, but you may use other libraries to display the
plots. For example, the plot3D
library:
::scatter3D(x=e[,1], y=e[,2], z=e[,3]) plot3D
Interactive rotatable and zoomable plots are provided by the
rgl
library:
::points3d(x=e[,1], y=e[,2], z=e[,3]) rgl
You may use parallelized versions of the algorithms. Several
functions (SOM
, GQTSOM
, EmbedSOM
)
support setting parallel=T
, which enables parallel
processing; you may fine-tune the number of used CPUs by setting
e.g. threads=5
.
For SOM training, you need to explicitly switch to the parallelizable
batch version, using batch=T, parallel=T
.
Additionally, EmbedSOM has support for SIMD-assisted computation of
both SOM and the embedding. If your CPU can work with SSE4 instructions
(almost every amd64
(a.k.a. x64
a.k.a.
x86_64
) CPU built after around 2013 can do that), just tell
R to compile your code with correct C++ flags, and SOM+EmbedSOM
computation should get faster! (the usual speedup is at least around 3x,
depending on the CPU and dataset shape)
First, add a correct line to the R Makevars
config
file:
$ cat ~/.R/Makevars
CXXFLAGS += -O3 -march=native
After reinstalling EmbedSOM, SIMD code will be used by default. Note
that only the functions from EmbedSOM are affected, i.e. you will need
to use EmbedSOM::SOM
instead of FlowSOM::SOM
and BuildSOM
to get this speedup.