Segmentation Tutorial part 3 - veeninglab/BactMAP GitHub Wiki

A closer look

Now we have six datasets in our environment, which are all large lists: one raw image output and five lists outputs of a phase segmentation. Below, we'll investigate the output, plot the segmentation over the tiff image and customize the plot output.


The output of extr_-functions after importing segmentation data

Each output of our five segmentation imports is a list including the following items:

  1. mesh: a dataframe with x/y coordinates of each cell & cell dimensions (optional: rotated cell coordinates & dimensions in micron)

  2. pixel2um : conversion factor pixel to micron used, for reference

  3. (optional) cellList: a dataframe which is the most direct translation of the original input file/files. This is different per segmentation program. This output is only included in your list if you put cellList=TRUE in your function argument.

When extracting spot, object, genealogy or other data using an extr-function, you get other output! Check out the sections on Data Structure in the Side Bar for more information.

We can see the items in our lists by using the command summary(). For instance, for Clement’s output clemOuf:

summary(clemOuf)
##          Length Class      Mode
## pixel2um  1     -none-     numeric
## mesh     20     data.frame list

In our case, we are mostly interested in the mesh, which summarizes the cell shapes.


The mesh

Let’s have a closer look at the mesh dataframe. You can access the members lists using the $-operator. Below, I keep working with Clement’s data to show what the mesh looks like, but you can pick any of the datasets instead if you want.

The following commands can be very useful for exploring dataframes. Try them:

View(clemOuf$mesh)

head(clemOuf$mesh)

summary(clemOuf$mesh)

In addition, the R package summarytools offers ways to quickly visualize and summarize each variable in a dataframe. The function dfSummary() is a nice way to explore the members of our dataframe.

You see that the mesh dataframe contains a lot of variables. They are explained in more detail in the mesh documentation.

  • X, Y, X_rot, Y_rot, Xrotum & Yrotum are all coordinates of cell outlines. X &Y are the original coordinates, X_rot & Y_rot are those in pixels, where mid-cell is at [0,0] and the length axis is parallel to the x axis, and Xrotum & Yrotum are the same points in micron.
  • Xmid and Ymid are the coordinates of each mid-cell.
  • cell and frame correnspond to the cell ID and which image frame it is (in our case, all is 1)
  • area, max.length & max.width are cell area, maximum length and maximum width. max_um is the maximum length in micron, where maxwum is the maximum width in micron.
  • angle is the angle of the length axis of the cell to the x axis. this is used to determine X_rot and Y_rot.
  • length, num, xy and steplength are all Oufti-specific. These variables are used to create the cell mesh. For more information, check extr_Oufti()’s documentation and Oufti’s web page.

You see that the function summary() already gives you some insight in the distribution of cell sizes within the dataset. Later I’ll plot the cell size distributions to compare the different segmentations to each other, but let's first check what the segmentation looks like.


Checking the cell outlines using plotRaw()

I used plotRaw() before to see whether our TIFF loaded well, but just plotting a TIFF in R is not very revolutionary when you have ImageJ on your computer as well! However, I found that it is pretty handy to plot your segmentation and/or spot detection data over the TIFF to see whether everything checks out as you expect. And of course, we can also use it to compare the different segmentations made in the lab.

The function plotRaw() uses the following input:

  • tiffdata (optional): a TIFF uploaded using extr_OriginalStack()
  • meshdata (optional): a mesh dataframe
  • frameN: the frame number of your TIFF stack you want to show. default = 1
  • xrange and yrange (optional): if you want to zoom in, give a vector with the minimum and maximum pixel number (example shown below)
  • viridisoption: specify color scheme (from viridis). default is “inferno”
  • meshcolor: the color of the cell outline. default = “white”

There are 3 input variables I won’t show in this tutorial, but which are useful to know about:

  • spotdata (optional): a spot dataframe or object dataframe
  • spotcolor: the color of the detected spots. default = “yellow”
  • valuerange: when showing TIFFs with low fluorescence signals instead of phase-contrast TIFFs, you might want to remove high signals to enhance contrast. Works similar to xrange/yrange. See the tutorial on localization plotting for examples.

To plot the segmentation over the raw TIFF, you can use the following command line. Note that all BactMAPs plots are made using ggplot2, which means that you can add ggplot2 layers on top of each plot. I make use of this below and put a custom title above my plot.

There are two ways to call for an R package when you need it; you can

  1. load the full package using the command library() or 2) use a single function from a package using the ::-operator. In the beginning of this tutorial we used library(bactMAP) to load my R package. Below, I use the ::-operator to call for ggplot2-functions.
plotRaw(pneumoTIFF, clemOuf$mesh) + ggplot2::ggtitle("Clement_Oufti")

Customize your plot

If you want too zoom in, you can use the xrange and yrange options of the plotRaw-function.

You can also make use of ggplot2 to change the color scale to something completely custom. Below, I use ggplot2's function scale_fill_gradient() to change the color scheme to greyscale. Since there was already a color scale specified inside the plotRaw function (namely "inferno”), your console will show a warning, but that’s fine!

plotRaw(pneumoTIFF, clemOuf$mesh, xrange=c(800,1000), yrange=c(1400,1600)) +
      ggplot2::ggtitle("Clement_Oufti") +
      ggplot2::scale_fill_gradient(low="black", high="white")

Now we have had a look at the composition of Clement’s dataset, it’s time to combine all datasets together to compare them.


⬅️ Segmentation Tutorial part 2: Data Import Segmentation Tutorial part 4: Combine Datasets ➡️
⚠️ **GitHub.com Fallback** ⚠️