Tracking with R


We're developing a tracking module, "TrackObjects", for CellProfiler. The goal of tracking is to follow an object from frame to frame in a time-lapse movie and to describe a lineage relationship for objects such as cells which might divide. I'm currently working on an implementation of the tracking method described in Jaqaman, Robust single-particle tracking in live-cell time lapse sequences Nature Methods, Vol 5 (2008) p 695.
The paper describes a two-step procedure that joins objects from one frame to the next, then joins the starts and ends of runs of objects to ends, starts and middles of other joins to correct errors caused by gaps in segmentations and to track merging and splitting events. At the heart of it is a linear assignment problem solver which optimizes the possible choices over an entire image (in the first pass) or the entire movie (in the second pass). The algorithm takes both object velocities and historical variance into account (using a Kalman filter). The method looks promising.

Viewing tracking data in R

3-D tracking rendering in R
I love R. It's a spectacular example of the best of open-source programming. The underpinnings are computationally elegant, you can do a lot of mathematics with very little typing and the user community is enormous - R is now an "everything but the kitchen sink" project. You'll find what you are looking for; someone has done it for you.
The picture above is a static image of an OpenGL graphing package for R: RGL. As with most OpenGL stuff, the image without the interaction doesn't do it justice. You can easily rotate and zoom and explore your data. The above is a rendering of the tracking of a movie as calculated by CellProfiler's TrackObjects. Each color represents a track with the z-axis being time. If you have R, you can do this yourself with tracking data from CellProfiler. Probably, the best idea is to save one data file per movie; this makes the R code exceptionally easy:

Accessing tracks from the database

You can access track data from the database from R. You need some sort of database connection. I've tried RMySQL which didn't work for me and RODBC which did (but you have to set up an ODBC data source to the database). For RODBC, the following gets you a channel object:
> library('RODBC')
> conn=odbcConnect('my-dsn')
> sqlQuery(conn,'use my-database')
After this, you can fetch the data for one of your tracks. Each image set in a track has the same group number and they are ordered by group index. You can join your image table to your object table through ImageNumber. Here's a possible script:
> stmt = "select i.Image_Metadata_Plate, i.Image_Metadata_Well, i.Image_Group_Index, s.* from my-image-table i join my-object-table s on i.ImageNumber = s.ImageNumber where i.Group_Number = group-number"
> data = sqlQuery(conn, stmt)
> plot3d(data$my-object-name_Location_Center_X, data$my-object-name_Location_Center_Y, data$Image_Group_Index, col=data$my-object-name_TrackObjects_Label, main=paste("Plate=",data$Image_MetadataPlate,"Well=",data$Image_MetadataWell))