TrackMate export (XML) code in Matlab

I am Debanjan and currently using Trackmate for single molecule tracking in live cells in Fiji. I must acknowledge that its an awesome piece of SMT program.

I am transitioning to Matlab for seamless integration with other analysis software package for the tracks, obtained from Trackmate. I am able to analyse SMT with Trackmate inside Matlab using Jean-Yves 's code available in the following weblink http://imagej.net/Using_TrackMate_from_MATLAB

What I’m unable to do is to export those tracks in the ‘model’ as XML file in Matlab. I will really appreciate if you could append few lines of code in the above example that will ExportTracksToXML easily.

Thanks,
Debanjan

Hi @debanjan.goswami,

The supplemental material of the TrackMate paper include a documentation. Its 3rd part covers MATLAB import intensively. You will find what you want there.

2 Likes

@tinevez Thanks for your prompt response. I have probably not made myself very clear. I want to export tracks using ‘ExportTracksToXML’ action in Matlab. The 3rd part in the documentation only elaborates how to import tracks in Matlab. I am running trackmate inside Matlab and have successfully created tracks on images. I want to use save these tracks for record and further analyse them with your ‘@msdanalyser’ and vbSPT in Matlab. If you could append few lines of code where this example end http://imagej.net/Using_TrackMate_from_MATLAB to export the result as XML. I really appreciate your help.

Debanjan

Aha?! Then you have to explain what you tried and what does not work.

I have run in to another problem today. I have updated the Fiji with Java 8 enabling the use of latest TrackMate 3.4. Fiji is working fine so as TrackMate. But, inside Matlab script, the initializing the ‘model’ for trackmate is giving error that was working fine previously with TrackMate 2.8. Do I need to update the ‘mij.jar’ file in the Matlab too?

Code:

model = fiji.plugin.trackmate.Model();

Error:

Undefined variable “fiji” or class “fiji.plugin.trackmate.Model”.

Error in TrackMate_MatlabScript (line 19)
model = fiji.plugin.trackmate.Model();

Did you see the note on the Miji page:

The current library for ImageJ/MATLAB integration is ImageJ-MATLAB; it has many advantages over this legacy project.

Can you try following these instructions for running ImageJ within Matlab, and see whether it works better?

@Jan Thanks for your prompt response. I have updated the Java Environment variable and added it to Matlab. Then, following command

addpath(genpath(‘C:\Users\goswamid2\Desktop\Fiji.app\scripts’));
ImageJ;

It did work with following errors.

[ERROR] Cannot create plugin: class=‘org.scijava.plugins.scripting.javascript.JavaScriptScriptLanguage’, name=‘JavaScript’, priority=0.0, enabled=true, pluginType=ScriptLanguage
java.lang.IllegalArgumentException: No such script engine: javascript
at org.scijava.script.AdaptedScriptLanguage.findFactory(AdaptedScriptLanguage.java:151)


a lot of of these

log4j:WARN No appenders could be found for logger (loci.formats.ClassList).
log4j:WARN Please initialize the log4j system properly.

How do I take care of the errors? And, log4j warning?

Thanks a bunch.

Debanjan

@tinevez @imagejan following is what I am doing and the last line is not working. I am running trackmate inside Matlab and have successfully created tracks on images. I want to use save these tracks for record and further analyse them with your ‘@msdanalyser’ and vbSPT in Matlab.

addpath(genpath('C:\Users\goswamid2\Desktop\Fiji.app\scripts'));
clear;
imp = ij.ImagePlus('C:\Users\goswamid2\Documents\MATLAB\Halo-CALM_Cell8.ome-1.tif');
imp.show()
model = fiji.plugin.trackmate.Model();
model.setLogger(fiji.plugin.trackmate.Logger.IJ_LOGGER)
settings = fiji.plugin.trackmate.Settings();
settings.setFrom(imp)
settings.detectorFactory = fiji.plugin.trackmate.detection.DogDetectorFactory(); %use LogDetectorFactory() for spot sizes ≈5 to ≈20 pixels in diameter.
map = java.util.HashMap();
map.put('DO_SUBPIXEL_LOCALIZATION', true);
map.put('RADIUS', 0.2);
map.put('TARGET_CHANNEL', 1);
map.put('THRESHOLD', 50);
map.put('DO_MEDIAN_FILTERING', false);
settings.detectorSettings = map;
filter1 = fiji.plugin.trackmate.features.FeatureFilter('QUALITY', 0.0, true);
settings.addSpotFilter(filter1)
settings.trackerFactory = fiji.plugin.trackmate.tracking.oldlap.SimpleLAPTrackerFactory(); 
settings.trackerSettings = fiji.plugin.trackmate.tracking.LAPUtils.getDefaultLAPSettingsMap(); 
deltaT = settings.dt;
settings.trackerSettings.put('LINKING_MAX_DISTANCE', 0.7 );
settings.trackerSettings.put('GAP_CLOSING_MAX_DISTANCE', 0.7 );
settings.addTrackAnalyzer(fiji.plugin.trackmate.features.track.TrackDurationAnalyzer())
filter2 = fiji.plugin.trackmate.features.FeatureFilter('TRACK_DURATION', 0.2, true);
settings.addTrackFilter(filter2)
img = fiji.plugin.trackmate.util.TMUtils.rawWraps(settings.imp);
settings.addSpotAnalyzerFactory(fiji.plugin.trackmate.features.spot.SpotIntensityAnalyzerFactory())
settings.addSpotAnalyzerFactory(fiji.plugin.trackmate.features.spot.SpotContrastAndSNRAnalyzerFactory())
settings.addSpotAnalyzerFactory(fiji.plugin.trackmate.features.spot.SpotRadiusEstimatorFactory())
settings.addSpotAnalyzerFactory(fiji.plugin.trackmate.features.spot.SpotMorphologyAnalyzerFactory())
settings.addEdgeAnalyzer(fiji.plugin.trackmate.features.edges.EdgeTargetAnalyzer())
settings.addEdgeAnalyzer(fiji.plugin.trackmate.features.edges.EdgeTimeLocationAnalyzer())
settings.addEdgeAnalyzer(fiji.plugin.trackmate.features.edges.EdgeVelocityAnalyzer())    
settings.addTrackAnalyzer(fiji.plugin.trackmate.features.track.TrackSpeedStatisticsAnalyzer())
settings.addTrackAnalyzer(fiji.plugin.trackmate.features.track.TrackDurationAnalyzer())
settings.addTrackAnalyzer(fiji.plugin.trackmate.features.track.TrackIndexAnalyzer())
settings.addTrackAnalyzer(fiji.plugin.trackmate.features.track.TrackLocationAnalyzer())
trackmate = fiji.plugin.trackmate.TrackMate(model, settings);
ok = trackmate.checkInput();
if ~ok
    display(trackmate.getErrorMessage())
end
ok = trackmate.process();
if ~ok
    display(trackmate.getErrorMessage())
end
selectionModel = fiji.plugin.trackmate.SelectionModel(model);
displayer =  fiji.plugin.trackmate.visualization.hyperstack.HyperStackDisplayer(model, selectionModel, imp);
displayer.setDisplaySettings(fiji.plugin.trackmate.visualization.TrackMateModelView.KEY_SPOT_COLORING, ...
  fiji.plugin.trackmate.visualization.SpotColorGeneratorPerTrackFeature(trackmate.getModel(), ...
  fiji.plugin.trackmate.Spot.QUALITY))
displayer.setDisplaySettings(fiji.plugin.trackmate.visualization.TrackMateModelView.KEY_TRACK_COLORING, ...
  fiji.plugin.trackmate.visualization.PerTrackFeatureColorGenerator(trackmate.getModel(), ...
  fiji.plugin.trackmate.features.track.TrackIndexAnalyzer.TRACK_INDEX))
displayer.render()
displayer.refresh()
display(model.toString())
file='C:\Users\goswamid2\Documents\MATLAB\Halo-CALM_Cell8.ome-1.xml';
fiji.plugin.trackmate.action.ExportTracksToXML.export(model, settings, file); **%NOT WORKING**

I will really appreciate your time to correct it.

Thanks,
Debanjan

Thanks, I will look into it. I am just a bit out of time now.
In the meantime, what is the error message?

Plus I must be doing something wrong now, as I could not launch ImageJ from MATLAB and therefore could not run your script.

I will investigate and return to this, but for now could you please copy/paste the error message?

@tinevez Thanks for looking in to it. You could add one line to call ImageJ after the first line, ie

addpath(genpath(‘C:\Users\goswamid2\Desktop\Fiji.app\scripts’));
ImageJ;

Following is the error message -

No method ‘export’ with matching signature found for class ‘fiji.plugin.trackmate.action.ExportTracksToXML’.

Error in TrackMate_MatlabScript (line 170)
fiji.plugin.trackmate.action.ExportTracksToXML.export(model, settings, file);

Debanjan

Hi @debanjan.goswami

So I was not able to debug your script at all, but this is not related to your problem. Apparently the ImageJ - MATLAB integration is broken at least on the Windows and Mac with MATLAB R2016 a or b.

When I try to run ImageJ.m, I get the following error:

>> ImageJ
Undefined function 'start' for input arguments of type    
'net.imagej.matlab.ImageJMATLAB'.

Error in ImageJ (line 42)
        ImageJMATLAB.start();

I will report it to the team but I am afraid @hinerm left.


Nonetheless I think I have found the source of your problem. When you call a Java method, you must match exactly the Hava signature of that method.

In your case, you are calling the ExportTracksToXML.export(model, settings, file) method, which works for a Model, a Settings and a File objects.

But if you look at line 60, you fille see that file is not a java.io.File. but a MATLAB string. I think you can make it work by changing this to:

file = java.io.File('C:\Users\goswamid2\Documents\MATLAB\Halo-CALM_Cell8.ome-1.xml');

Can you tell us whether this works?

1 Like

It worked. Thanks a lot.

For the ImageJ part, I am using the old method and it works. Following is the code.

javaaddpath 'C:\Program Files\MATLAB\R2016b\java\mij.jar'
addpath(genpath('C:\Users\goswamid2\Desktop\Fiji.app\scripts'));
addpath ('C:\Users\goswamid2\Documents\Matlab Scripts\bfmatlab');
clear;
Miji(false);
reader=bfGetReader('C:\Users\goswamid2\Documents\MATLAB\Halo-CALM_Cell8.ome-2.tif');
omeMeta = reader.getMetadataStore();
tVal1=omeMeta.getPixelsTimeIncrement(0).value();
tVal=tVal1.doubleValue();
imp = ij.ImagePlus('C:\Users\goswamid2\Documents\MATLAB\Halo-CALM_Cell8.ome-2.tif');
imp.show();

But by using this code, I am encountering another problem. The ‘settings’ (for tracking) is then set from the image.

settings = fiji.plugin.trackmate.Settings();
settings.setFrom(imp)

I guess,
ImagePlus (reader) cannot correctly the dimension order (ie ‘XYZCT’) saved by the Bioformat (OME) metadata. In my case, it is reading the ‘T’ as ‘Z’ and the tracking is obstructed. As soon as I save the same movie file using FIJI’s save command (instead of ‘bfsave’ in Matlab) the TrackMate is working properly. I am trying finding a remedy for that.

Thank you again for your time and help. I really appreciate it.

Debanjan

1 Like

Are you sure that you are reading in OME format actually?

From this file name, I’d suppose that ImageJ reads the file as plain tif. What happens if you rename the file to .ome.tif instead of .ome-2.tif, or if you explicitly use Bio-Formats to import the image?

@imagejan That was a good point. I have saved the movie again as “Halo-CALM_Cell8-2.ome.tif” and got the same error as follows

Error using TrackMate_MatlabScript (line 147)
Java exception occurred:
java.lang.IllegalArgumentException: Argument out of range: 1

originated from the following code

ok = trackmate.process();
if ~ok
    display(trackmate.getErrorMessage())
end

This problem is occurring when I read the movie file by calling ImagePlus inside the Matlab as well as when I open the movie using FIJI’s standard ‘open’ command. During the erroneous image reading in FIJI, TrackMate corrects the order by asking whether the order has been switched between ‘Time’ and ‘Plane’. But interestingly this problem goes away when I open the same movie file thru BioFormat. In that case, the order is read correctly and no further warning is given by TrackMate function. That is why I think the ImagePlus function in not correctly reading the .ome.tif file. I have also noticed someone else complained about a similar problem couple of years ago (not 100% sure though). http://forum.image.sc/t/bioformats-export-as-ome-tiff-scale-bug/2625
https://github.com/scifio/scifio-ome-xml/issues/8.

Whereas, the same movie file when opened using BioFormat and then saved using FIJI’s ‘save’ function, can then be used to track particles using TrackMate.

Let me know if it make sense or I am doing something wrong in the following image reading. Image properties switched between ‘T’ and ‘Z’.

>>imp = ij.ImagePlus('C:\Users\goswamid2\Documents\MATLAB\Halo-CALM_Cell8.tif');
>> imp
imp =
img[Halo-CALM_Cell8.tif (100x100x1x1x2000)]

and

>>imp = ij.ImagePlus('C:\Users\goswamid2\Documents\MATLAB\Halo-CALM_Cell8-3.ome.tif');
>> imp
imp =
img[Halo-CALM_Cell8-3.ome.tif (100x100x1x2000x1)]

Thanks for looking in to it. It has already been quite bit of help.

I am encountering similar problem when files (time-series movie) that are saved using Bio-Format’s bfsave as .ome.tif can’t be read accurately using ImagePlus function. The dimension order is switched between ‘Time’ and ‘Z-stack’. I think the ImagePlus function in not correctly reading the .ome.tif file format and metadata. I can reproduce the same problem even when I save .ome.tif file using Fiji’s 'saveas' followed by opening the file using FIJI’s standard 'open' function.

I highly appreciate any help on this matter.

Thanks,
Debanjan

PS. I am following up on this topic after a long time because I was unable to post in this forum ‘502 bad gateway’ error

1 Like

I’m running into the same problem, trying to export spots from Trackmate in matlab into an XML file. I can generate an XML file, but the only thing in the XML file is:

<?xml version="1.0" encoding="UTF-8"?>
<Tracks nTracks="0" spaceUnits="pixels" frameInterval="1.0" timeUnits="frames" generationDateTime="Fri, 19 Jul 2019 17:57:37" from="TrackMate v3.8.0" />

If I query the number of spots (here spots is my SpotCollection)

spots.getNSpots(1)

ans =

    3128

So the spots exist, but alas, I can’t get them to export in Matlab for some reason. I’m sure I’m missing something trivial.

I started a topic on this, but as it’s a very similar topic to this, I thought i’d cross post in case others came across the same problem.

just a question,
are you trying to do only detection or also tracking?

I’m just trying to do detection.

ok, I’ve also sometimes used programmatically trackmate to do just detection; not in matlab but in jython.
I’ve noticed that if you export using a model with only the detection part without the tracking the XML is someway not fine.

my solution is to just put a fake tracker adding it to the model, even if you don’t need it.

then my XML were good.

at the moment I am writing you using a smartphone… so I cannot easily put you down a snippet.
just add a tracker and try to recover the nspots in matlab.

hth
Emanuele

I tried a stack that I generated that is just one spot, 10pixels in diameter, moving 10px/frame, using the first template from https://imagej.net/Using_TrackMate_from_MATLAB, changing just the radius to 5px, and the threshold to 10, and then added the lines

display(model.toString())

fm = model.getFeatureModel()

%----------------
% Saving results
%----------------

outfile=java.io.File('F:\Projects\Matlab\test_track.xml');
fiji.plugin.trackmate.action.ExportTracksToXML.export(model, settings, outfile);

after the %echo results.

I still only get metadata head for an xml file, with no spots nor tracks.

If I query the model, I get spots still:

spots =  model.getSpots()

spots =

fiji.plugin.trackmate.SpotCollection@a6ffbc5: contains 51 spots total in 1 different frames, over which 51 are visible:
	frame 0: 51 spots total, 51 visible.

Could it be that the z/t sequence is getting mixed up? I only think that as there are 51 spots apparently in 1 frame. I’m still not getting any results to export though. Just a bit bewildering.