1
|
|
|
import sys |
2
|
|
|
from ij import IJ, ImagePlus, ImageStack |
3
|
|
|
import fiji.plugin.trackmate.Settings as Settings |
4
|
|
|
import fiji.plugin.trackmate.Model as Model |
5
|
|
|
import fiji.plugin.trackmate.SelectionModel as SelectionModel |
6
|
|
|
import fiji.plugin.trackmate.TrackMate as TrackMate |
7
|
|
|
import fiji.plugin.trackmate.Logger as Logger |
8
|
|
|
import fiji.plugin.trackmate.detection.DetectorKeys as DetectorKeys |
9
|
|
|
import fiji.plugin.trackmate.detection.DogDetectorFactory as DogDetectorFactory |
10
|
|
|
import fiji.plugin.trackmate.tracking.sparselap.SparseLAPTrackerFactory as SparseLAPTrackerFactory |
11
|
|
|
import fiji.plugin.trackmate.tracking.LAPUtils as LAPUtils |
12
|
|
|
import fiji.plugin.trackmate.visualization.hyperstack.HyperStackDisplayer as HyperStackDisplayer |
13
|
|
|
import fiji.plugin.trackmate.features.FeatureFilter as FeatureFilter |
14
|
|
|
import fiji.plugin.trackmate.features.FeatureAnalyzer as FeatureAnalyzer |
15
|
|
|
import fiji.plugin.trackmate.features.spot.SpotContrastAndSNRAnalyzerFactory as SpotContrastAndSNRAnalyzerFactory |
16
|
|
|
import fiji.plugin.trackmate.action.ExportStatsToIJAction as ExportStatsToIJAction |
17
|
|
|
import fiji.plugin.trackmate.io.TmXmlReader as TmXmlReader |
18
|
|
|
import fiji.plugin.trackmate.action.ExportTracksToXML as ExportTracksToXML |
19
|
|
|
import fiji.plugin.trackmate.io.TmXmlWriter as TmXmlWriter |
20
|
|
|
import fiji.plugin.trackmate.features.ModelFeatureUpdater as ModelFeatureUpdater |
21
|
|
|
import fiji.plugin.trackmate.features.SpotFeatureCalculator as SpotFeatureCalculator |
22
|
|
|
import fiji.plugin.trackmate.features.spot.SpotContrastAndSNRAnalyzer as SpotContrastAndSNRAnalyzer |
23
|
|
|
import fiji.plugin.trackmate.features.spot.SpotIntensityAnalyzerFactory as SpotIntensityAnalyzerFactory |
24
|
|
|
import fiji.plugin.trackmate.features.track.TrackSpeedStatisticsAnalyzer as TrackSpeedStatisticsAnalyzer |
25
|
|
|
import fiji.plugin.trackmate.features.track.TrackDurationAnalyzer as TrackDurationAnalyzer |
26
|
|
|
import fiji.plugin.trackmate.util.TMUtils as TMUtils |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
# Get currently selected image |
30
|
|
|
#imp = WindowManager.getCurrentImage() |
31
|
|
|
#imp = IJ.openImage('{target_file}') |
32
|
|
|
#IJ.run(imp, "Properties...", "channels=1 slices=1 frames=651 unit=pixel pixel_width=1.0000 pixel_height=1.0000 voxel_depth=1.0000"); |
33
|
|
|
#imp = IJ.openImage('/home/ubuntu/data/RED_nPEG_37C_pH72_S1_1_1_2.tif') |
34
|
|
|
imp = IJ.openImage('http://fiji.sc/samples/FakeTracks.tif') |
35
|
|
|
#imp.show() |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
#------------------------- |
39
|
|
|
# Instantiate model object |
40
|
|
|
#------------------------- |
41
|
|
|
|
42
|
|
|
model = Model() |
43
|
|
|
|
44
|
|
|
# Set logger |
45
|
|
|
model.setLogger(Logger.IJ_LOGGER) |
46
|
|
|
|
47
|
|
|
#------------------------ |
48
|
|
|
# Prepare settings object |
49
|
|
|
#------------------------ |
50
|
|
|
|
51
|
|
|
settings = Settings() |
52
|
|
|
settings.setFrom(imp) |
53
|
|
|
|
54
|
|
|
# Configure detector |
55
|
|
|
settings.detectorFactory = DogDetectorFactory() |
56
|
|
|
settings.detectorSettings = {{ |
57
|
|
|
DetectorKeys.KEY_DO_SUBPIXEL_LOCALIZATION : True, |
58
|
|
|
DetectorKeys.KEY_RADIUS : {radius}, |
|
|
|
|
59
|
|
|
DetectorKeys.KEY_TARGET_CHANNEL : 1, |
60
|
|
|
DetectorKeys.KEY_THRESHOLD : {threshold}, |
|
|
|
|
61
|
|
|
DetectorKeys.KEY_DO_MEDIAN_FILTERING : {do_median_filtering} |
|
|
|
|
62
|
|
|
}} |
63
|
|
|
|
64
|
|
|
# Configure tracker |
65
|
|
|
settings.trackerFactory = SparseLAPTrackerFactory() |
66
|
|
|
settings.trackerSettings = LAPUtils.getDefaultLAPSettingsMap() |
67
|
|
|
settings.trackerSettings['LINKING_MAX_DISTANCE'] = {linking_max_distance} |
|
|
|
|
68
|
|
|
settings.trackerSettings['GAP_CLOSING_MAX_DISTANCE']={gap_closing_max_distance} |
|
|
|
|
69
|
|
|
settings.trackerSettings['MAX_FRAME_GAP']= {max_frame_gap} |
|
|
|
|
70
|
|
|
|
71
|
|
|
# Add the analyzers for some spot features. |
72
|
|
|
# You need to configure TrackMate with analyzers that will generate |
73
|
|
|
# the data you need. |
74
|
|
|
# Here we just add two analyzers for spot, one that computes generic |
75
|
|
|
# pixel intensity statistics (mean, max, etc...) and one that computes |
76
|
|
|
# an estimate of each spot's SNR. |
77
|
|
|
# The trick here is that the second one requires the first one to be in |
78
|
|
|
# place. Be aware of this kind of gotchas, and read the docs. |
79
|
|
|
settings.addSpotAnalyzerFactory(SpotIntensityAnalyzerFactory()) |
80
|
|
|
settings.addSpotAnalyzerFactory(SpotContrastAndSNRAnalyzerFactory()) |
81
|
|
|
|
82
|
|
|
#filter2 = FeatureFilter('QUALITY', {quality}, True) |
83
|
|
|
#settings.addSpotFilter(filter2) |
84
|
|
|
|
85
|
|
|
#filter3 = FeatureFilter('MEDIAN_INTENSITY', {median_intensity}, False) |
86
|
|
|
#settings.addSpotFilter(filter3) |
87
|
|
|
#filter4 = FeatureFilter('SNR', {snr}, True) |
88
|
|
|
#settings.addSpotFilter(filter4) |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
# Add an analyzer for some track features, such as the track mean speed. |
92
|
|
|
settings.addTrackAnalyzer(TrackSpeedStatisticsAnalyzer()) |
93
|
|
|
settings.addTrackAnalyzer(TrackDurationAnalyzer()) |
94
|
|
|
|
95
|
|
|
#filter5 = FeatureFilter('TRACK_DISPLACEMENT', {track_displacement}, True) |
96
|
|
|
#settings.addTrackFilter(filter5) |
97
|
|
|
|
98
|
|
|
settings.initialSpotFilterValue = 1 |
99
|
|
|
|
100
|
|
|
print(str(settings)) |
101
|
|
|
|
102
|
|
|
#---------------------- |
103
|
|
|
# Instantiate trackmate |
104
|
|
|
#---------------------- |
105
|
|
|
|
106
|
|
|
trackmate = TrackMate(model, settings) |
107
|
|
|
|
108
|
|
|
#------------ |
109
|
|
|
# Execute all |
110
|
|
|
#------------ |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
ok = trackmate.checkInput() |
114
|
|
|
if not ok: |
115
|
|
|
sys.exit(str(trackmate.getErrorMessage())) |
116
|
|
|
|
117
|
|
|
ok = trackmate.process() |
118
|
|
|
if not ok: |
119
|
|
|
sys.exit(str(trackmate.getErrorMessage())) |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
|
123
|
|
|
#---------------- |
124
|
|
|
# Display results |
125
|
|
|
#---------------- |
126
|
|
|
|
127
|
|
|
model.getLogger().log('Found ' + str(model.getTrackModel().nTracks(True)) + ' tracks.') |
128
|
|
|
|
129
|
|
|
selectionModel = SelectionModel(model) |
130
|
|
|
#displayer = HyperStackDisplayer(model, selectionModel, imp) |
131
|
|
|
#displayer.render() |
132
|
|
|
#displayer.refresh() |
133
|
|
|
|
134
|
|
|
# The feature model, that stores edge and track features. |
135
|
|
|
fm = model.getFeatureModel() |
136
|
|
|
|
137
|
|
|
model.getLogger().log('Data starts here.') |
138
|
|
|
model.getLogger().log('Track_ID' +','+ 'Spot_ID' +','+ 'Frame' +','+ 'X' +','+ 'Y' +','+ 'Quality' +','+ 'SN_Ratio' +','+ 'Mean_Intensity') |
139
|
|
|
for id in model.getTrackModel().trackIDs(True): |
140
|
|
|
|
141
|
|
|
# Fetch the track feature from the feature model. |
142
|
|
|
#v = fm.getTrackFeature(id, 'TRACK_MEAN_SPEED') |
143
|
|
|
#dur = fm.getTrackFeature(id, 'TRACK_DURATION') |
144
|
|
|
#model.getLogger().log('') |
145
|
|
|
#model.getLogger().log('Track ' + str(id) + ': mean velocity = ' + str(v) + ' ' + model.getSpaceUnits() + '/' + model.getTimeUnits()) |
146
|
|
|
#model.getLogger().log('Track ' + str(id) + ': duration = ' + str(dur) + ' ' + model.getTimeUnits()) |
147
|
|
|
track = model.getTrackModel().trackSpots(id) |
148
|
|
|
for spot in track: |
149
|
|
|
sid = spot.ID() |
150
|
|
|
# Fetch spot features directly from spot. |
151
|
|
|
x=spot.getFeature('POSITION_X') |
152
|
|
|
y=spot.getFeature('POSITION_Y') |
153
|
|
|
t=spot.getFeature('FRAME') |
154
|
|
|
q=spot.getFeature('QUALITY') |
155
|
|
|
snr=spot.getFeature('SNR') |
156
|
|
|
mean=spot.getFeature('MEAN_INTENSITY') |
157
|
|
|
model.getLogger().log(str(id) +','+ str(sid) +','+ str(t) +','+ str(x) +','+ str(y) +','+ str(q) +','+ str(snr) +','+ str(mean)) |
158
|
|
|
|