Plotting spot localization using createPlotlist() - veeninglab/BactMAP GitHub Wiki
In this tutorial, you will learn how to use bactMAP to quickly plot the localization of fluorescent spots inside the cell. In this case, I wanted to know the general localization of SMC (Structural Maintanance Complex) inside Wild Type Streptococcus pneumoniae D39 cells. SMCs are important for the segregation and folding of the chromosomes and conserved from bacteria to mammalian cells (Gruber, 2017). It is recruited to the origin by the protein ParB (Minnen et al, 2011) and, when tagged to GFP, visible as spots inside the cells.
After imaging, the spots were detected using the imageJ plugin iSBatch, while the cell outlines are detected using Morphometrics, a Matlab-based segmentation tool. For this dataset, I chose these programs because they are quick and easy to use for this purpose, but you can follow this tutorial as well with data derived from other programs. Check the wiki page about data upload for more information on upload for other programs.
If you didn’t already, install BactMAP from my github page using the package devtools.
#if you don't have devtools yet, run the following line:
#install.packages("devtools")
#otherwise, immediately run:
devtools::install_github("vrrenske/bactmap")
library(bactMAP)
First, I’ll upload the peak file from the example folder in bactMAP & turn it into a usable spot dataframe spotframe. In the output I get is also the dataframe cellList with all the other information isBatch saved.
SMC_peaks <- system.file("extdata", "SMC_peaks.txt", package = "bactMAP")
SMC_extrP <- extr_ISBatch(SMC_peaks)
summary(SMC_extrP)
## Length Class Mode
## cellList 19 data.frame list
## spotframe 3 data.frame list
Then the Morphometrics cell outlines; again, cellList is the original frame, while mesh is the transformed dataframe.
Be aware: mesh files are quite a bit bigger (so slower) than spot files and transforming matlab files directly is generally taking longer than reading in text files as well. Conclusion: this function takes it's time.
#requires(R.matlab)
SMC_morph <- system.file("extdata", "SMC_mesh.MAT", package="bactMAP")
SMC_mesh <- extr_Morphometrics(SMC_morph)
summary(SMC_mesh)
## Length Class Mode
## cellList 22 data.frame list
## mesh 15 data.frame list
We can quickly view the cells & spots by plotting them using bactMAP::plotRaw(). Check out the tutorial on comparing segmentation data to see more ways to use this function!
In the plot below, you can see that the cells are not always properly segmented, but the majority is looking good. Same for the spot detection: some background spots were detected, but the majority is inside the cells. Note that the SMC spots are not very bright, so they cannot be detected in every cell.
plotRaw(meshdata=SMC_mesh$mesh, spotdata=SMC_extrP$spotframe, frameN=1)
To get the relative spot localization of SMC inside the cell, use the function bactMAP::spotsInBox() (check ?spotsInBox for more details). It returns a data frame spots_relative with the relative spot localizations in the cell, and a data frame mesh with the mesh (cell) outlines turned so the cell is laying with the length axis horizontal and it’s mid-point at [0,0].
Note: when your spot localization and mesh segmentation data are coming from the same file and the relative spot or object locations are not known yet, bactMAP automatically uses this function when extracting the data. When the relative localization data is already included in the source data, bactMAP takes this data instead.
#requires(shotGroups)
SMC_relative <- spotsInBox(SMC_extrP$spotframe, SMC_mesh$mesh)
#check content
head(SMC_relative$spots_relative)
## cellframe x y pip l d max.width
## 1 10.1 28.650 1564.917 1 -4.317377 2.0696303 15.62667
## 2 10.1 28.650 1564.917 1 -4.317377 2.0696303 15.62667
## 3 100.1 150.361 1457.640 1 12.046983 -3.6765720 15.90128
## 4 1003.1 998.760 1879.876 1 9.963469 5.2084555 16.45357
## 5 1005.1 1002.762 588.088 1 6.635676 1.2212769 14.88223
## 6 1006.1 1010.563 75.606 1 0.259034 0.7620872 14.95047
## max.length cell frame spot totalspot
## 1 20.71958 10 1 1 2
## 2 20.71958 10 1 2 2
## 3 29.79569 100 1 1 1
## 4 21.37774 1003 1 1 1
## 5 22.02933 1005 1 1 1
## 6 20.49093 1006 1 1 1
Here you can see that the columns “l” and “d” are added to the dataset. This are the relative coordinates of the spots in the cell over the length (l) and width (d) axis. Apart from that, the cell dimensions are added, just as cell number, a spot identifier (spot) and the total amount of spots in the given cell (totalspot).
To get an overview of the spot localization, I use bactMAP::createPlotList() to plot a whole range of plots to get a quick overview of the data.
The function needs a spotdata dataframe and a meshdata dataframe to plot the relative localization of the spots in cells grouped by cell length. I use the output of spotsInBox for this. The plotting function will check if the spot coordinates are already relative to the cell; if not, it will do spotsInBox internally. Generally, I like to do it seperately because if I then want to do plotting multiple times/ways, it saves me time! However, if you only want to use createPlotlist(), you can skip the spotsInBox step without problems.
SMCplots <- createPlotlist(spotdata = SMC_relative$spots_relative,
meshdata = SMC_relative$mesh,
mag="100x_FRAP")
Let’s check the output:
summary(SMCplots)
## Length Class Mode
## lengthplot 9 gg list
## widthplot 9 gg list
## mean_outlines 4 -none- list
## qplots 4 -none- list
## plottotal 9 gg list
## histograms 4 -none- list
## spotdata 23 data.frame list
## meshdata 18 data.frame list
## pixel2um 1 -none- numeric
- The plotlist output contains the spot & mesh dataframes used for the plotting containing all variables it created to be able to plot.
The plots in the created list are shown below from left-right/top-bottom:
- Lengthplot & widthplot are spot localizations over either length or width ordered by cell length, plotted as a density plot
- plottotal and qplots are cell projections: all x/y localizations of the cell plotted as a density plot with the average cell outline, either of all cells (plottotal) or grouped based on cell length (qplots)
- Histograms show histograms of the localizations on the length axes, faceted based on cell length the same way as qplots.

createPlotList() has more basic options:
You can change the output color by adding colorpalette=“mypalette” to the function, where “mypalette” is one of bactmap’s color palettes. These palettes are based on the Color Universal Design Colorblind-friendly Palettes. To make it easier to choose, you can check the palletes by using showCurrentPalettes():
showCurrentPalettes()
If there is a specific color palette you would like to add manually, you can do this with the function addPalette():
addPalette(palList = list("#740001","#ae0001", "#eeba30"), palName="Gryffindor") #Top popular palette from www.color-hex.com
Set AllPlot=TRUE to create histograms of the cell length/width localizations next to the cell projections (allplot and qplots). By setting the number of groups, you can control in how many groups the qplots and histograms will be devided.
SMCplots <- createPlotlist(spotdata = SMC_relative$spots_relative,
meshdata = SMC_relative$mesh,
groups=3,
mag="100x_FRAP",
AllPlot=TRUE,
colorpalette = "YellowHot")
If you like the plots which are created but you want to use another color, change the axis titles, or add a layer on top of the plots, it is possible to do this using the standard ggplot-commands. Some examples:
##change the colors
##of a single plot
Plotsingle <- SMCplots$lengthplot + viridis::scale_fill_viridis(option="magma")

## change axis titles:
PlotTitleChange <- SMCplots$lengthplot + ggtitle("Now there's a different title")
## remove titlee, axis titles & legend:
PlotNoText <- SMCplots$lengthplot + ggtitle("") + xlab("") + ylab("") + theme(legend.position="none")
## flip axis:
PlotFlip <- SMCplots$lengthplot + coord_flip()
##overlay plot with same data, but now as dotplot
PlotOverlay <- SMCplots$lengthplot + geom_point(aes(x=num, y=Lmid), size=1, color="#9e0051")
grid.arrange(PlotTitleChange, PlotNoText, PlotFlip, PlotOverlay)

Caldas, V. E., Punter, C. M., Ghodke, H., Robinson, A., & van Oijen, A. M. (2015). iSBatch: a batch-processing platform for data analysis and exploration of live-cell single-molecule microscopy images and other hierarchical datasets. Molecular BioSystems, 11(10), 2699-2708.
Ursell, T., Lee, T. K., Shiomi, D., Shi, H., Tropini, C., Monds, R. D., … & Huang, B. E. (2017). Rapid, precise quantification of bacterial cellular dimensions across a genomic-scale knockout library. BMC biology, 15(1), 17.
van Raaphorst, R., Kjos, M., & Veening, J. W. (2017). Chromosome segregation drives division site selection in Streptococcus pneumoniae. Proceedings of the National Academy of Sciences, 114(29), E5959-E5968.
Garnier, S (2018). viridis: Default Color Maps from ‘matplotlib’. R package version 0.5.1. https://CRAN.R-project.org/package=viridis
Bengtsson, H (2016). R.matlab: Read and Write MAT Files and Call MATLAB from Within R. R package version 3.6.1. https://CRAN.R-project.org/package=R.matlab
Wollschlaeger, D (2017). shotGroups: Analyze Shot Group Data. R package version 0.7.3. https://CRAN.R-project.org/package=shotGroups