knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 6,
  fig.height = 6
)

This package uses two-dimensional particle trajectories to study the spatial distribution of particles relative to surrounding cellular structures. It calculates the nearest distance between each point of a trajectory and surrounding structures represented as a binary masks in reference images. CloseEnough can also randomize the trajectories within a pre-defined area and compare the relative positioning of particles and surrounding structures to a stochastic distribution.

Getting started

This R package that can be downloaded from GitHub. To install this package from GitHub, the devtools package must first be installed. To install devtools, start R and enter:

install.packages("devtools")
library(devtools)

Then to install the CloseEnough package, enter:

install_github("MaxLev/CloseEnough", build = TRUE)

Once the CloseEnough package is installed, it can be loaded by the following command:

``` {r library, message = FALSE} library(CloseEnough)

## Display images

This package depends on the _EBImage_ package to upload and display images. To start off, let us display an image distributed with the _CloseEnough_ package. 

```r
## Load image
data(cellmovie)

## Display image
EBImage::display(cellmovie, method = "browser")

Display cell outline and trajectories

Let us display the cell outline and overlay the trajectories of the green foci on the image.

## Load cell outline
data(outline)

## Load trajectories
data(trajectory)

## Display image
EBImage::display(cellmovie, method = "raster")

## Trace outline
lines(outline$x, outline$y, type = "l", col = "white")

## Draw trajectories in white
ntraj = nlevels(trajectory["trajectory"])
for (i in 1:ntraj) {
  traj_i <- subset(trajectory["data"], trajectory == i)
  lines(traj_i$x, traj_i$y, type="l", col = "white")
}

Particle trajectories can be rotated or mirrored if needed

Let us rotate the particle trajectories to the right by a 90 degree angle:

``` {r, message = FALSE} rotated_traj <- rotate_trajectory(trajectory, 90)

To reflect them along the y axis just enter:

``` {r, message = FALSE}
mirror_traj <- mirror_trajectory(trajectory, axis = "y")

Now let us visualize these three populations of trajectories by drawing them over a binary image representing a simple cell mask:

``` {r, message = FALSE}

Load cell mask

data(cellmask)

Display image

EBImage::display(cellmask, method = "raster")

Draw original trajectories in black

ntraj = nlevels(trajectory["trajectory"]) for (i in 1:ntraj) { traj_i <- subset(trajectory["data"], trajectory == i) lines(traj_i$x, traj_i$y, type="l", col = "black") }

Draw the rotated trajectories in blue

ntraj = nlevels(rotated_traj["trajectory"]) for (i in 1:ntraj) { traj_i <- subset(rotated_traj["data"], trajectory == i) lines(traj_i$x, traj_i$y, type="l", col = "blue") }

And finally, add the reflected trajectories in green

ntraj = nlevels(mirror_traj["trajectory"]) for (i in 1:ntraj) { traj_i <- subset(mirror_traj["data"], trajectory == i) lines(traj_i$x, traj_i$y, type="l", col = "green") }

## Randomization of particle trajectories within the cytoplasm
_CloseEnough_ can easily randomize particle trajectories within a given area provided by a binary mask image. This provides valuable information regarding the spatial distribution of particles within a cellular context. As an example, let us randomize the trajectories of those green foci within the cytoplasm and observe how the spatial distribution of those foci changes relative to the mitochondrial network.

``` {r, message = FALSE}
## Randomize trajectories using an image mask as reference.
rand_traj <- randomize(trajectory, cellmask)

## Display image
EBImage::display(cellmovie, method = "raster")

## Trace cell outline
lines(outline$x, outline$y, type = "l", col = "white")

## Draw original trajectories in white
ntraj = nlevels(trajectory["trajectory"])
for (i in 1:ntraj) {
  traj_i <- subset(trajectory["data"], trajectory == i)
  lines(traj_i$x, traj_i$y, type="l", col = "white")
}

## Add the randomized trajectories in blue
ntraj = nlevels(rand_traj["trajectory"])
for (i in 1:ntraj) {
  traj_i <- subset(rand_traj["data"], trajectory == i)
  lines(traj_i$x, traj_i$y, type="l", col = "blue")
}

Analyze spatial distribution

CloseEnough can calculate the minimal distance between the particles and other cellular structures within a thresholded binary image. In this example, we will calculate the minimal distance between every green foci within the trajectories and the mitochondrial network. We will also performed the same calculations with our randomized trajectories.

``` {r, fig.width = 8, fig.height = 6}

Load the thresholded mask of the mitochondrial network

data(mitomask)

Calculate minimal distance between foci and mitochondria

Channel refers to the channel that needs to be used within the binary image. In this case there is only one channel.

traj_mito <- min_distance(mitomask, trajectory, channel = 1)

Calculate minimal distance between randomized foci and mitochondria

rand_mito <- min_distance(mitomask, rand_traj, channel = 1)

## Visualize spatial distribution
Finally, these values can be visualized using any R graphic tools. The original and randomized distributions can also be plotted and compared using our function _profileplot_.

``` {r, message = FALSE}
graph <- profileplot(traj = traj_mito, randtraj = rand_mito)
## Display graph
graph


MaxLev/CloseEnough documentation built on May 9, 2019, 2:19 a.m.