Re: mTrackJ data sets -- MTrack3: like MTrack2, but with a different results format

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: mTrackJ data sets -- MTrack3: like MTrack2, but with a different results format

David Eccles (gringer)
From:    David Knecht <[hidden email]>
> We are generating large data sets from mTrackJ in which large numbers
> of cells are being tracked from each image stack.  We are trying to
> get them into excel or Prism or other software for further analysis.
> The problem is that the data is imported as a single column of
> objects, so if you want to set up a series of columns with data for
> each cell in a set of columns, you have to manually cut and paste
> each cell's dataset into a new set of columns.  Is there a way to
> automate this process or a way to import so that the cell/object sets
> get sorted in the first place?

Hi David,

You've provided me with the incentive I needed to send my code out to
the world.

I've actually been recently working on a multi-object tracker because of
issues I had with the MTrack2 results format (which sounds like it is
similar to the mTrackJ results format). The 7-column results file
produced by this updated code is much easier to parse using a script
(the R code I have for loading the results file is a single line, while
it took about 30 lines to slice and join the older '3-columns per track'
format into a better structure):

http://user.interface.org.nz/~gringer/hacking/mtrack3.html

The pain with this plugin is that you need to subtrack background and
threshold the image first so that ImageJ's particle tracker works
properly. This can be done in a macro, but that still reduces the
usability of the plugin because the macros need to be custom-designed
for each use case.

I've also written an R script (for tracking flies) that is designed to
process results from both MTrack2 and MTrack3-style results formats, so
could probably be also used (or slightly modified) for mTrackJ results.
Here are some parts of the script that may be of use to you:

-- begin script --

   if(result.format == "MTrack3"){
     ## load file into data frame
     data.df <- read.csv(input.filename, row.names = 1)
     ## replace column names...R thinks the first column should be 'X'
     colnames(data.df) <- sub(".1","",colnames(data.df));
   } else if(result.format == "MTrack2"){
     input.file <- file(input.filename);
     open(input.file);
     ## get header line
     header.line <- scan(input.file, what = character(0), nlines = 1);
     ## extract out track listing
     track.line <- unlist(strsplit(readLines(input.file, n = 1)," "));
     ## set up rotated results table
     results <- data.frame(Frame = NULL, Track = NULL,
                           X = NULL, Y = NULL);
     while(length(track.line) == 4){
       ## read num.frames lines from the file
       data.df <- read.delim(input.file, nrows = num.frames,
                             header = FALSE);
       ## determine track numbers for columns
       track.start = as.numeric(track.line[2]);
       track.end = as.numeric(track.line[4]);
       cat("Iteration for",track.start,"...",track.end,"\n");
       ## add data to end of results structure
       if(track.start < track.end){ # work around odd bug if 75 tracks
         for(trackPos in (track.start:track.end - track.start)){
           results <- rbind(results,
                            data.frame(Frame = data.df[,1],
                                       Track = trackPos + track.start,
                                       X = data.df[,2+trackPos * 3],
                                       Y = data.df[,3+trackPos * 3]));
         }
       }
       track.line <- unlist(strsplit(readLines(input.file, n = 1)," "));
     }
     close(input.file);
     results <- subset(results, !is.na(X));
     data.df <- results;
   }

## some examples of how to work with the data
track.speeds <- tapply(1:length(data.df$Track),
                        data.df$Track,function(x){
                           sqrt(diff(data.df$X[x])^2 +
                                diff(data.df$Y[x])^2)
                        });
mean.speeds <- sapply(track.speeds,mean);
## output CSV file for e.g. Prism input
write.csv(data.df, file = "track_positions.csv", row.names = FALSE);

-- end script --

Hope this helps,

David Eccles
Bioinformatics and Next-generation Sequencing
Max Planck Institute for Molecular Biomedicine
Münster, Germany
Reply | Threaded
Open this post in threaded view
|

Re: mTrackJ data sets -- MTrack3: like MTrack2, but with a different results format

giubi
Eccles, David wrote
I've actually been recently working on a multi-object tracker because of
issues I had with the MTrack2 results format (which sounds like it is
similar to the mTrackJ results format). The 7-column results file
produced by this updated code is much easier to parse using a script
(the R code I have for loading the results file is a single line, while
it took about 30 lines to slice and join the older '3-columns per track'
format into a better structure):

http://user.interface.org.nz/~gringer/hacking/mtrack3.html
Hey David,

This is exactly what I was looking for! Thank you for sharing.

I have one odd behaviour when I try to change MTrack3 to MTrack2 in my macro.
I need to process many stacks and give a different name to the tracks results file.

Below a simple code to show my problem:

// Create a stack suitable for MTrack2/3
for (i=0; i<5; i++) run("Blobs (25K)");
run("Images to Stack", "name=Stack title=[]");
setThreshold(126, 255);
run("Convert to Mask", " ");

dirOut = getDirectory("Choose where save the results files ");

// If I run this line with MTrack2 I get 5 files, with MTrack3 just one
for (i=1; i<=5; i++) run("MTrack2 ", "save save=" + dirOut + "file_number_" + i);

Cheers,
Giuseppe