Intro to cerebroViz

Tanner Koomar

2016-09-27

cerebroViz Basics

cerebroViz is a tool for visualizing spatiotemporal data in the brain. As input, it requires a matrix with rows corresponding to brain regions and columns corresponding to time points. You can learn more about the 30 regions which cerebroViz recognizes in the Brain Regions in cerebroViz section.

cerebroViz includes an example matrix, cerebroEx, which is gene expression data for the POGZ gene taken from the Brainspan developmental transcriptome1.

library("cerebroViz")
data(cerebroEx)
head(cerebroEx)[, c(1:7)]
##       12 pcw   16 pcw  21 pcw 24 pcw 37 pcw   4 mos  1 yrs
## A1C 4.286033 4.301767      NA 3.9131 3.1623 2.91330 2.9752
## AMY 3.878933 4.002200 4.03940 4.1134 3.2270 2.95360 2.8207
## CB  3.380100 3.341000 4.10145 3.3962 3.8083 2.95250 3.5043
## DFC 4.176567 3.896300 3.82330 3.8742 3.0935 2.90495 2.9340
## HIP 3.790200 4.265950 3.70530 3.3259 3.2731 2.57755 2.7461
## IPC 3.976267 4.256967 3.88800 3.7798 3.1009 2.49860 2.9446

Calling cerebroViz on this matrix produces two files in the current working directory: one with the suffix “_outer.svg” and another with the suffix “_slice.svg”. The outer file corresponds to a lobe view of the brain, while the slice file corresponds to a sagittal view.

cerebroViz(cerebroEx)
## Success! Your diagrams have been saved.



Back to top

Specifying a Timepoint

cerebroViz allows visualization of brain data temporally, as well as spatially. Multiple time points (corresponding to the columns of a properly formatted matrix) can be specified for rendering by passing an integer vector to the timePoint = argument of cerebroViz(). This generates an outer and slice image for each time point, where the number of the corresponding time point is appended to the file name after outer or slice. For example: ex1_outer_1.svg is the lobe view for the first time point and ex1_slice_2.svg is the sagittal view for the second time point.

library('cerebroViz')

cerebroViz(cerebroEx, timePoint = c(1, 5, 9))
## Success! Your diagrams have been saved.
Timepoint 1

Timepoint 5

Timepoint 9



Back to top

Changing the Ouput File

Users may specify a custom prefix for the rendered images with the filePrefix = argument of cerebroViz(). The saved filenames with also contain a suffix indicating outer or slice and the requested time point before the file extension. By default, cerebroViz() save files to the working directory, but a file path may be passed to filePrefix = so images may be saved elsewhere:

library('cerebroViz')

dir.create('custom_directory/')

cerebroViz(cerebroEx, filePrefix = 'custom_directory/a_custom_filename', timePoint = c(1, 5, 9))
## Success! Your diagrams have been saved.
list.files('custom_directory/')
## [1] "a_custom_filename_outer_1.svg" "a_custom_filename_outer_5.svg"
## [3] "a_custom_filename_outer_9.svg" "a_custom_filename_slice_1.svg"
## [5] "a_custom_filename_slice_5.svg" "a_custom_filename_slice_9.svg"


Back to top

Divergant Scales

By default, cerebroViz() maps input data to a linear color scale. When working with divergent data (e.g. gene expression), it is desirable to have a neutral “middle” color.

A divergent scale can be specified by passing the divData = TRUE argument. In this case, cerebroViz() maps the median value of all data points in the matrix to a neutral yellow and the extreme values to a pleasant red and blue.

library('cerebroViz')

summary(as.vector(cerebroEx))
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##   1.317   2.637   2.966   3.071   3.596   4.600      15
cerebroViz(cerebroEx, divData = TRUE, timePoint = 11, filePrefix = 'divergent')
## Success! Your diagrams have been saved.



Back to top

Clamping Outliers

The removal of outliers from data is crucial to producing interpretable visualizations. For example, if we set the data for the cerebellum in our example data cerebroEx to an extremely high value:

ex1_out <- cerebroEx

ex1_out["CB",11] <- 55

The cerebellum has an extremely high signal, and all other values are indistinguishable.

cerebroViz(ex1_out, timePoint = 11, filePrefix = 'outlier')
## Success! Your diagrams have been saved.

To compensate for outliers, a value can be passed to the clamp = argument of cerebroViz(). The clamp value is used as a coefficient with the Median Absolute Deviation (MAD) to calculate a range of ‘acceptable’ values.

\[median \pm clamp \times MAD \]

Values greater than this range are reduced (or “clamped”) to its maximum value, while values less than the range are increased to its minimum. The ideal clamp value depends on the shape of the data, but values near 3 are often used as a rule of thumb. Applying this process to the ex1_out matrix, which has been edited to contain an outlier:

cerebroViz(ex1_out, clamp = 3, timePoint = 11, filePrefix = 'outlier_clamped')
## Success! Your diagrams have been saved.



Back to top

Comparisons

It is often useful to compare observations across corresponding time points, such as with gene or isoform expression. So long as the data are collected in comparable units, like RPKM, they can be compared by subtracting the matricies from one another and visualized with cerebroViz().

ex_dif <- cerebroEx[,1:3] - cerebroEx2[,1:3]

cerebroViz(ex_dif, filePrefix = 'ex_dif')
## Success! Your diagrams have been saved.

In these diagrams, a positive value indicates higher expression in cerebroEx relative to cerebroEx2, while a negative value corresponds to higher expression of cerebroEx2. In this case, it appears cerebroEx is always higher.



Back to top

Customizing cerebroViz’s Appearance

Labeling regions

To label the regions of the brain for which you have data, pass the regLabel = TRUE argument to cerebroViz(). The labels generated by regLabel always display the cerebroViz default, but are fully editable in any vector graphics manipulation program, or even a text editor.

cerebroEx -> ex1

cerebroViz(ex1, regLabel = TRUE, filePrefix = 'regLabel')
## Success! Your diagrams have been saved.


Back to top

Labeling time points

Viewers find it easier to follow a series when every image in the series is labled. Passing the figLable = TRUE argument to cerebroViz() prints the column name for each figure on the lower left corner of the vizualization.

cerebroEx -> ex1

cerebroViz(ex1, figLabel = TRUE, timePoint = c(5, 9), filePrefix = 'figLabel')
## Success! Your diagrams have been saved.
Outer

Slice


Back to top

Crosshatching Missing Regions

Depending on specified color schemes, it may be difficult to distinguish a color (likely a neutral color in divergent data) from the background color of the brain. When some data points are missing (NA), corresponding regions may appear to be near the median, when they are actually missing. The argument naHatch = TRUE may be passed to cerebroViz() to apply a cross-hatch to these regions to help distinguish them from the background or the neutral color.

cerebroEx -> ex1

cerebroViz(ex1, naHatch = TRUE, filePrefix = 'naHatch')
## Success! Your diagrams have been saved.


Back to top

Removing the legend

The color scale bar can be toggled off with the legend = FALSE argument of cerebroViz(), as may be desired when generating images to be used in multi-panel figures.

cerebroEx -> ex1

cerebroViz(ex1, legend = FALSE, filePrefix = 'legend')
## Success! Your diagrams have been saved.


Back to top

Custom color palettes

The palette = argument of cerebroViz() allows a custom color palette to be set for the vizulaization. To define a new palette, we recommend passing the RColorBrewer::brewer.pal() function to palette =. In fact, the default palettes for cerebroViz() use brewer.pal()!

cerebroViz scale RColorBrewer palette
cerebroViz(x, divData = FALSE) brewr.pal(9, "YlOrRd")
cerebroViz(x, divData = TRUE) rev(brewr.pal(11, "RdYlBu"))*

RColorBrewer::brewer.pal() takes 2 arguments: n, the number of colors in the palette; and a name corresponding to the palette to use. An odd number of colors are reccomended whever divData = TRUE, ensuring the median is assigned to a known color.

library(cerebroViz)
library(RColorBrewer)

cerebroViz(cerebroEx, palette = rev(brewer.pal(11, "PiYG")), divData = TRUE, timePoint = 11, filePrefix = "palette")
## Success! Your diagrams have been saved.

To see a list of palettes available from RColorBrewer, use RColorBrewer::display.brewer.all(). More information about brewer.pal() can be read in the documentation for RColorBrewer.

* Note that for divData = TRUE, the call to brewer.pal() is wrapped in rev() so the “cooler” color corresponds to the low end of the scale and the “warmer” to high values.


Back to top

Advanced custom colors

cerebroViz() allows users to fine-tune the color schemes used for both the visualization and the brain background.The color scale to which the data are mapped can be specified by passing a vector of color names, RGB values or hex values with a length of two or greater to the palette = argument:

library(cerebroViz)

cerebroViz(cerebroEx, palette = c("cornflowerblue", "antiquewhite", "coral"), timePoint = 11, filePrefix = "custom_palette")
## Success! Your diagrams have been saved.

Users are advised to specify an odd number of colors whenever using divData = TRUE, ensuring the median is assigned to a known color.

The color of the brain background (“empty” regions of the brain), brain outline, and image background can be specified, in that order, to the secPalette = argument. Like palette, this argument accepts a vector of color names, RGB values or hex values. Unlike palette, secPalette = only accepts a vector of length three.

library(cerebroViz)

cerebroViz(cerebroEx, secPalette = c("darkgrey", "white", "lightgrey"), timePoint = 11, filePrefix = "secPalette")
## Success! Your diagrams have been saved.



Back to top

Generating a heatmap with cerebroScale

Because it is impractical to interpret many cerebroViz diagrams simultaneously, it often makes sense to also visualize the data as a heatmap. In order to account for the custom scaling cerebroViz() can perform thanks to clamp = and divData =, the convenience function cerebroScale() is included.

When passed a matrix in proper cerebroViz format, cerebroScale() returns a new matrix of the same dimensions where all values have been rescaled with the minimum value set to 0.0, and the maximum set to 1.0.

head(cerebroEx)[,c(1:7)]
##       12 pcw   16 pcw  21 pcw 24 pcw 37 pcw   4 mos  1 yrs
## A1C 4.286033 4.301767      NA 3.9131 3.1623 2.91330 2.9752
## AMY 3.878933 4.002200 4.03940 4.1134 3.2270 2.95360 2.8207
## CB  3.380100 3.341000 4.10145 3.3962 3.8083 2.95250 3.5043
## DFC 4.176567 3.896300 3.82330 3.8742 3.0935 2.90495 2.9340
## HIP 3.790200 4.265950 3.70530 3.3259 3.2731 2.57755 2.7461
## IPC 3.976267 4.256967 3.88800 3.7798 3.1009 2.49860 2.9446
head(cerebroScale(cerebroEx, clamp = NULL, divData = FALSE))[,c(1:7)]
##        12 pcw    16 pcw    21 pcw    24 pcw    37 pcw     4 mos     1 yrs
## A1C 0.9044210 0.9092137        NA 0.7908188 0.5621116 0.4862617 0.5051176
## AMY 0.7804110 0.8179603 0.8292921 0.8518338 0.5818204 0.4985378 0.4580541
## CB  0.6284574 0.6165469 0.8481936 0.6333618 0.7588948 0.4982028 0.6662910
## DFC 0.8710755 0.7857012 0.7634641 0.7789692 0.5411539 0.4837182 0.4925673
## HIP 0.7533813 0.8983033 0.7275192 0.6119471 0.5958633 0.3839862 0.4353296
## IPC 0.8100605 0.8955668 0.7831729 0.7502132 0.5434081 0.3599366 0.4957963

If the divData = TRUE argument is passed to cerebroScale(), the median of the dataset is also scaled to 0.5, so it will map to the neutral color of the palette The clamp = # argument clamps outliers, as described here, before converting to the to the 0 to 1 range .

summary(as.vector(cerebroEx))
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##   1.317   2.637   2.966   3.071   3.596   4.600      15
summary(as.vector(cerebroScale(cerebroEx, clamp = NULL, divData = FALSE)))
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##  0.0000  0.4021  0.5023  0.5343  0.6942  1.0000      15
summary(as.vector(cerebroScale(cerebroEx, clamp = 3, divData = TRUE)))
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##  0.0000  0.3643  0.5000  0.5410  0.7572  1.0000      15

Note that both divData = and clamp = are required arguments for cerebroScale(). If divData = FALSE and clamp = NULL (the default values in cerebroViz()), there is no need to use cerebroScale() to rescale data before visualizing as a heatmap.

Using the heatmap() function from the {stats} package is the quickest way to generate a heatmap corresponding to a dataset which had been visualized with cerebroViz. Color schemes for heatmap() are specified with the col = argument, and can accept RColorBrewer::brewer.pal() as input. If using a default cerebroViz() color palette:

cerebroViz scale RColorBrewer palette
cerebroViz(x, divData = FALSE) brewr.pal(9, "YlOrRd"")
cerebroViz(x, divData = TRUE) rev(brewr.pal(11, "RdYlBu"))

Note to pass the scale = "none" and Colv = NA arguments to heatmap() so that individual time-points are neither separately centered and scaled, nor rearranged into a dendrogram.

cerebroScale(cerebroEx, clamp = NULL, divData = TRUE) -> ex1_scaled

heatmap(ex1_scaled, Colv = NA, scale = "none", col = rev(brewer.pal(11, "RdYlBu")))

cerebroViz(cerebroEx, clamp = NULL, divData = TRUE, filePrefix = "scaled")
## Success! Your diagrams have been saved.

Back to top

Brain Regions in cerebroViz

Cerebroviz provides 30 regions to which brain data can be mapped. To view a data frame containing mapping of cerebroViz’s regions to those of several popular databases:

  data(regionMap)
  regionMap
cerebroViz name brainspan gtex roadmap
A1C primary auditory cortex A1C . .
AMY amygdala AMY Amygdala .
ANG angular gyrus . . Angular Gyrus
BS brainstem . . .
CAU caudate . Caudate (basal ganglia) Anterior Caudate
CB cerebellum CBC Cerebellum Cerebellum
CNG anterior cingulate cortex . Anterior cingulate cortex Cingulate Gyrus
DFC dorsolateral prefrontal cortex DFC . Mid Frontal Lobe
FL frontal lobe . Frontal Cortex .
HIP hippocampus HIP Hippocampus Hippocampus Middle
HTH hypothalamus . Hypothalamus .
IPC inferior parietal cortex IPC . .
ITC inferior temporal cortex ITC . Inferior Temporal Lobe
M1C primary motor cortex M1C . .
MED medulla oblongata . . .
MFC medial prefrontal cortex MFC . .
OL occipital lobe . . .
OFC orbital frontal cortex OFC . .
PL parietal lobe . . .
PIT pituitary gland . . .
PUT putamen . Putamen (basal ganglia) .
PON pons . . .
S1C primary somatosensory cortex S1C . .
SN substantia nigra . Substantia nigra Substantia Nigra
STC posterior (caudal) superior temporal cortex STC . .
STR striatum STR . .
TL temporal lobe . . .
THA thalamus MD . .
V1C primary visual cortex V1C . .
VFC ventrolateral prefrontal cortex VFC . .

Because many databases utilize different abbreviation for analogous regions, users are encouraged to name the rows of their data using the cerebroViz conventions described in the above table. If this is not possible, a matrix with 2 columns, where [,1] contains cerebroViz convention names and [,2] contains the corresponding custom name can be passed to the customNames = argument of cerebroViz. The names cerebroViz uses by convention are reserved, and my not be assigned to custom regions in this way.

Custom Regions Example

For example, Brainspan (from which cerebroEx was taken), uses the abbreviation CBC instead of CB, and MD instead of THA. Recreating this original nomenclature throws an error:

rownames(cerebroEx)[c(3, 14)]
## [1] "CB"  "THA"
rownames(cerebroEx)[c(3, 14)] <- c('CBC', 'MD')
cerebroViz(cerebroEx, filePrefix = "missing_name", timePoint = 9)
## Warning in cerebroViz(cerebroEx, filePrefix = "missing_name", timePoint
## = 9): Unknown row names in input data: CBC, MD. Unknown regions will
## be excluded from visualization. See the help manual for 'customNames'
## argument.
## Warning in cerebroViz(cerebroEx, filePrefix = "missing_name", timePoint
## = 9): The following regions encompass other regions of the brain: STR.
## Subregions are masked in output.
## Success! Your diagrams have been saved.


To map these regions to the custom names: create a 2 column matrix where [,1] contains cerebroViz convention names and [,2] contains the corresponding custom name, then pass it to the customNames = argument of cerebroViz().

matrix(c("CB", "THA", "CBC", "MD"), ncol=2 ) -> cnm
cnm
##      [,1]  [,2] 
## [1,] "CB"  "CBC"
## [2,] "THA" "MD"
cerebroViz(cerebroEx, customNames = cnm, filePrefix = "custom_name", timePoint = 9)
## Success! Your diagrams have been saved.


The data are now properly mapped to the corresponding regions. Note that both the “missing_name” and “custom_name” images have the same scale. cerebroViz uses all data in the supplied matrix to calculate its color scale, even those which cannot be mapped to regions in the output images.



Back to top

Make an animated gif of cerebroViz diagrams

cerebroViz diagrams can be quickly and easily converted into animated gifs to highlight temporal changes with the command-line tool Imagemagick. Imagemagick is a free image manipulation suite available for Windows, Mac and Linux distributions.

When generating SVGs which will be animated with cerebroViz(), it is recommended to use the figLabel = TRUE argument to print the column name for the provided matrix on the corresponding SVG:

library(cerebroViz)
dir.create("gif_directory")
cerebroViz(cerebroEx2, timePoint = c(1:50), divData = TRUE,  filePrefix = "gif_directory/gif", figLabel = TRUE)
## Success! Your diagrams have been saved.

To convert the SVGs generated by cerebroViz(), enter the directory containing them in the shell, and run the below command, where -delay n is the number of milliseconds between frames and -loop n the number of times the animation should loop (0 = infinite).

cd gif_directory
convert -delay 100 -loop 0 `ls -v *_slice_*` animated_slice.gif
convert -delay 100 -loop 0 `ls -v *_outer_*` animated_slice.gif

Back to top


  1. BrainSpan: Atlas of the Developing Human Brain [Internet]. Funded by ARRA Awards 1RC2MH089921-01, 1RC2MH090047-01, and 1RC2MH089929-01. © 2011. Available from: http://developing human brain.org.