knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
In this vignette, we will retrieve fluorescence intensity values around the position of each spot and display the mean intensity for each spot.
To acces the fluorescence intensity values in the Big Data Viewer .h5 file, we need to use the {rhdf5} library.
if (isTRUE(!requireNamespace("cellviz3d"))) { stop("Please install {cellviz3d} using:\n", " devtools::install_github(\"marionlouveaux/cellviz3d\")") # devtools::install_github("marionlouveaux/cellviz3d") } if (isTRUE(!requireNamespace("rhdf5"))) { stop("Please install {rhdf5} using:\n", "install.packages(\"BiocManager\"); ", "BiocManager::install(\"rhdf5\")") } library(cellviz3d) library(dplyr) library(ggplot2) library(ggridges) library(mamut2r) library(rhdf5) library(XML) library(tidyr)
To install {rhdf5}:
# source("http://bioconductor.org/biocLite.R") # biocLite("rhdf5") install.packages("BiocManager") BiocManager::install("rhdf5")
To install {cellviz3d}:
devtools::install_github("marionlouveaux/cellviz3d")
To get the correspondence between spots positions and pixels indices in the .h5 file, we need the .xml file generated by the Big Data Viewer (that was loaded the first time in the Fiji MaMut plugin to start the annotations).
fileXML <- xmlToList(system.file("extdata", "MaMuT_Parhyale_demo-mamut.xml", package = "mamut2r"))
fileXML_BDV <- xmlToList(system.file("extdata", "MaMuT_Parhyale_demo.xml", package = "mamut2r"))
We import the spots and tracks.
Spots_df <- Spots.as.dataframe(fileXML)
Tracks_df <- Tracks.as.dataframe(fileXML)
We now extract information relative to the View registration stored in the BIg Data Viewer file. According to the supplemental information from the Big Data Viewer paper (Pietzsch et al. 2015, Nature methods, Vol.12 No.6), "
read_ViewRegistration() collect all the affine transformations for all timepoints and only setup 0.
Registration_df <- read_ViewRegistration(fileXML_BDV, setup = 0)
prod_affine_registration() concatenate all affine transforms from a given timepoint together.
Registration_df2 <- prod_affine_registration(Registration_df)
The lines below apply the affine transform of the corresponding timepoint on each spot coordinates and return a new set of coordinates (POSITION_X_loc, POSITION_Y_loc, POSITION_Z_loc).
Spots_df_loc <- Spots_df %>% left_join(Registration_df2, by = c("FRAME" = "Timepoint")) %>% rowwise() %>% mutate(pos_local = list({#browser(); res <- crossprod(prod_affine_inv, c(POSITION_X, POSITION_Y, POSITION_Z, 1))[1:3,] tibble(POSITION_X_loc = res[1], POSITION_Y_loc = res[2], POSITION_Z_loc = res[3]) })) %>% unnest(pos_local, .drop = FALSE)
With the code below you can visually quickly check the positions of the spots before and after applying the affine transforms.
library(rgl) # Spots location before plot3d(Spots_df_loc[which(Spots_df_loc$FRAME == 0), c("POSITION_X", "POSITION_Y", "POSITION_Z")], size = 20, col = "green") # Spots location after plot3d(Spots_df_loc[which(Spots_df_loc$FRAME == 0), c("POSITION_X_loc", "POSITION_Y_loc", "POSITION_Z_loc")], size = 20)
all_cells <- getFluo(H5_path = "../data4mamut2r/MaMuT_Parhyale_demo.h5", Spots_df_loc, x_px = POSITION_X_loc, y_px = POSITION_Y_loc, z_px = POSITION_Z_loc, timepoint = FRAME, cubeSize = 5)
saveRDS(all_cells, "./inst/extdata/all_cells.rds")
all_cells <- readRDS(system.file("extdata", "all_cells.rds", package = "mamut2r"))
We can now display the output of getFluo(). Let's first select one timepoint only.
unique(Spots_df$FRAME)
The Parhyale dataset has 10 timepoints. The fluorescent data are stored in a list (here called all_cells), of 10 elements (one per timepoints), each of these containing as many elements as the number of spots in the timepoint. In these elements are stored the fluorescence intensities of all the pixels in a cube of 10x10x10 pixels (if using the default cubeSize) around the spot location. Let's have a look at the distribution of fluorescence of spots at the 6th timepoint, i.e. all_cells[[5]].
singleFrame <- lapply(all_cells[[5]], function(x){ data.frame(frame = x[[1]], name = x[[2]], fluo = as.vector(x[[3]])) }) df_singleFrame <- do.call(rbind.data.frame, singleFrame) head(df_singleFrame) ggplot(df_singleFrame, aes(x = fluo, y = name, fill = name)) + geom_density_ridges() + theme_ridges() + theme(legend.position = "none")
mmFluo() calculate the mean and median fluorescent value for each spot.
Spots_df <- mmFluo(Spots_df, all_cells)
You can visualise the distribution of the mean fluorescent values over the whole movie using {ggplot2}.
ggplot(data=Spots_df, aes(x=meanFluo)) + geom_histogram() + theme_classic()
You can also separate the data per timepoint using the facet wrap from {ggplot2} or the geom_density_ridges from {ggridges}:
ggplot(data=Spots_df, aes(x=meanFluo)) + geom_histogram() + facet_wrap(facets = "frame") + theme_classic() ggplot(Spots_df, aes(x = meanFluo, y = frame, fill = frame)) + geom_density_ridges() + theme_ridges() + theme(legend.position = "none")
Continuous numerical variables added to Spots_df, like for instance the mean fluorescence intensity value for each spot, can be converted to colors and then converted to integer readable by the MaMuT Fiji plugin and added as a "MANUAL_COLOR" field.
Spots_df_manualColor <- heatmap2int(Spots_df, meanFluo)
These new "MANUAL_COLOR" field can be added to the MaMuT .xml file. The "MANUAL_COLOR" field is already defined in the features of the MaMuT .xml file. Other new fields are not supported yet by the modifyXML(), as this function does not add new features to define new fields. Below is an example of how to modify the MaMuT .xml file.
modifyXML(MaMuTpath = my_path, newFilename = "MaMuT_Parhyale_demo-mamut_withManualColor.xml", Spots_df = Spots_df_manualColor)
We can visualise the fluorescence intensity values in 3D using the plotlySpots() function from {cellviz3d}, based mainly on the {plotly} library.
Spots_df <- countSpot(Spots_df, FRAME) plotlySpots(Spots_df = Spots_df, x = POSITION_X, y = POSITION_Y, z = POSITION_Z, timepoint = FRAME, singleTimepoint = "5", number = nb, color = meanFluo)
We can also visualise all the timepoints together, using the plotlySpots_all() function from {cellviz3d}, also mainly based on {plotly}.
plotlySpots_all(Spots_df = Spots_df, x = POSITION_X, y = POSITION_Y, z = POSITION_Z, timepoint = FRAME, number = nb, color = meanFluo)
# load another mamut file as reference: one or several nuclei in the vasculature tracked over the whole movie. # MaMuT_ref <- ".xml" # Spots_df_ref <- Spots.as.dataframe(MaMuT_ref) # fluo_ref <- getFluo(Spots_df_ref)
Spots_df <- mmFluo_wholeMovie(Spots_df, all_cells)
Spots_df <- mmFluo_perT(Spots_df, all_cells)
#Tn+1 - Tn diffFluo <- function(Spots_df, all_cells, meancol) { meancol <- enquo(meancol) Spots_df %>% mutate( prev = lag((!!meancol)) ) %>% mutate(diffFluo = (!!meancol) - prev) } Spots_df <- diffFluo(Spots_df, all_cells, meancol = meanFluo)
To calculate a mean and median fluorescence value per spot + calculate normalised fluorescence accross the whole movie, or for the current frame, all in one go, you can use the mmFluo_all() function.
mmFluo_all(Spots_df, all_cells)
We created an other mamut .xml file named "MaMuT_Parhyale_demo-mamut_withSpotOutside.xml" with one track outside the Parhyale nuclei. The spots in Track ID 42 are located on the background. They have the ID 3964, 3963, 3937, 3965, 3968, 3966, 3962, 3967, and 3961.
fileXML_out <- xmlToList(system.file("extdata", "MaMuT_Parhyale_demo-mamut_withSpotOutside.xml", package = "mamut2r"))
We import the spots with Spots.as.dataframe().
Spots_df_out <- Spots.as.dataframe(fileXML_out)
We calculate the position of the spots using the registration found above.
Spots_df_loc_out <- Spots_df_out %>% left_join(Registration_df2, by = c("FRAME" = "Timepoint")) %>% rowwise() %>% mutate(pos_local = list({#browser(); res <- crossprod(prod_affine_inv, c(POSITION_X, POSITION_Y, POSITION_Z, 1))[1:3,] tibble(POSITION_X_loc = res[1], POSITION_Y_loc = res[2], POSITION_Z_loc = res[3]) })) %>% unnest(pos_local, .drop = FALSE)
We retrieve the fluorescence in a cube of 10x10x10 pixels around each nucleus.
all_cells_out <- getFluo(H5_path = "../data4mamut2r/MaMuT_Parhyale_demo.h5", Spots_df_loc_out, x_px = POSITION_X_loc, y_px = POSITION_Y_loc, z_px = POSITION_Z_loc, timepoint = FRAME, cubeSize = 5)
saveRDS(all_cells_out, "./inst/extdata/all_cells_out.rds")
all_cells_out <- readRDS(system.file("extdata", "all_cells_out.rds", package = "mamut2r"))
We calculate the mean and median fluorescence values for all spots.
Spots_df_out <- mmFluo(Spots_df_out, all_cells_out)
We now display the spots using plotlySpots_all(). When mousing over the spots, we see that the fluorescence values of the spots with the IDs 3964, 3963, 3937, 3965, 3968, 3966, 3962, 3967, and 3961 are very low compared to the fluorescence values of the other spots.
Spots_df_out <- countSpot(Spots_df_out, FRAME) plotlySpots_all(Spots_df = Spots_df_out, x = POSITION_X, y = POSITION_Y, z = POSITION_Z, timepoint = FRAME, number = nb, color = meanFluo)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.