generated from ome/guide-template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
tracking.groovy
259 lines (235 loc) · 9.6 KB
/
tracking.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* -----------------------------------------------------------------------------
* Copyright (C) 2020 University of Dundee. All rights reserved.
*
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ------------------------------------------------------------------------------
*/
/*
* This Groovy script uses the plugin Trackmater to analyse images, the generated tracks are
* saved to OMERO.
* In this script, we show:
* - How to connect to OMERO
* - How to open an image using Bio-Formats
* - How to create a trackmate model via its API
* - How to convert the ROIs created by Trackmate into OMERO ROIs
* More details about restricted privileges can be found at
* https://docs.openmicroscopy.org/latest/omero/sysadmins/restricted-admins.html
* Use this script in the Scripting Dialog of Fiji (File > New > Script).
* Select Groovy as language in the Scripting Dialog.
* Error handling is omitted to ease the reading of the script but this
* should be added if used in production to make sure the services are closed
* Information can be found at
* https://docs.openmicroscopy.org/latest/omero5/developers/Java.html
*/
#@ String(label="Username") USERNAME
#@ String(label="Password", style='password') PASSWORD
#@ String(label="Host", value='wss://workshop.openmicroscopy.org/omero-ws') HOST
#@ String(label="Port", value=443) PORT
#@ Integer(label="Image ID", value=28629) image_id
// Import-start
import java.awt.Color
import java.util.ArrayList
import omero.gateway.Gateway
import omero.gateway.LoginCredentials
import omero.gateway.SecurityContext
import omero.gateway.facility.BrowseFacility
import omero.gateway.facility.ROIFacility
import omero.gateway.model.EllipseData
import omero.gateway.model.PolylineData
import omero.gateway.model.ROIData
import omero.log.SimpleLogger
import omero.model.PolylineI
import static omero.rtypes.rstring
import ij.IJ
import fiji.plugin.trackmate.Spot
import fiji.plugin.trackmate.Settings
import fiji.plugin.trackmate.Model
import fiji.plugin.trackmate.SelectionModel
import fiji.plugin.trackmate.TrackMate
import fiji.plugin.trackmate.detection.DetectorKeys
import fiji.plugin.trackmate.detection.DogDetectorFactory
import fiji.plugin.trackmate.tracking.sparselap.SparseLAPTrackerFactory
import fiji.plugin.trackmate.tracking.LAPUtils
import fiji.plugin.trackmate.visualization.hyperstack.HyperStackDisplayer
import fiji.plugin.trackmate.features.spot.SpotContrastAndSNRAnalyzerFactory
import fiji.plugin.trackmate.features.spot.SpotIntensityAnalyzerFactory
import fiji.plugin.trackmate.features.track.TrackSpeedStatisticsAnalyzer
// Import-end
def connect_to_omero() {
"Connect to OMERO"
credentials = new LoginCredentials()
credentials.getServer().setHostname(HOST)
credentials.getUser().setUsername(USERNAME.trim())
credentials.getUser().setPassword(PASSWORD.trim())
simpleLogger = new SimpleLogger()
gateway = new Gateway(simpleLogger)
gateway.connect(credentials)
return gateway
}
def disconnect(gateway) {
gateway.disconnect()
}
// Load-Image
def get_image(gateway, image_id) {
"Retrieve the image"
browse = gateway.getFacility(BrowseFacility)
user = gateway.getLoggedInUser()
ctx = new SecurityContext(user.getGroupId())
return browse.getImage(ctx, image_id)
}
// Open-Image
def open_image_plus(host, port, username, password, group_id, image_id) {
"Open the image using the Bio-Formats Importer"
StringBuilder options = new StringBuilder()
options.append("location=[OMERO] open=[omero:server=")
options.append(host)
options.append("\nuser=")
options.append(username.trim())
options.append("\nport=")
options.append(port)
options.append("\npass=")
options.append(password.trim())
options.append("\ngroupID=")
options.append(group_id)
options.append("\niid=")
options.append(image_id)
options.append("] ")
options.append("windowless=true view=Hyperstack ")
IJ.runPlugIn("loci.plugins.LociImporter", options.toString())
}
// Create-Tracker
def create_tracker(imp) {
"Create the trackmate model for the specified ImagePlus object"
// Instantiate model object
model = new Model()
// Prepare settings object
settings = new Settings()
settings.setFrom(imp)
// Configure detector
settings.detectorFactory = new DogDetectorFactory()
settings.detectorSettings.put(DetectorKeys.KEY_DO_SUBPIXEL_LOCALIZATION, true)
settings.detectorSettings.put(DetectorKeys.KEY_RADIUS, new Double(2.5))
settings.detectorSettings.put(DetectorKeys.KEY_TARGET_CHANNEL, 1)
settings.detectorSettings.put(DetectorKeys.KEY_THRESHOLD, new Double(5.0))
settings.detectorSettings.put(DetectorKeys.KEY_DO_MEDIAN_FILTERING, false)
// Configure tracker
settings.trackerFactory = new SparseLAPTrackerFactory()
settings.trackerSettings = LAPUtils.getDefaultLAPSettingsMap()
settings.trackerSettings['LINKING_MAX_DISTANCE'] = new Double(10.0)
settings.trackerSettings['GAP_CLOSING_MAX_DISTANCE'] = new Double(10.0)
settings.trackerSettings['MAX_FRAME_GAP'] = 3
// Add the analyzers for some spot features
settings.addSpotAnalyzerFactory(new SpotIntensityAnalyzerFactory())
settings.addSpotAnalyzerFactory(new SpotContrastAndSNRAnalyzerFactory())
// Add an analyzer for some track features, such as the track mean speed.
settings.addTrackAnalyzer(new TrackSpeedStatisticsAnalyzer())
settings.initialSpotFilterValue = 1
// Instantiate trackmate
trackmate = new TrackMate(model, settings)
ok = trackmate.checkInput()
if (!ok) {
print(str(trackmate.getErrorMessage()))
return null
}
ok = trackmate.process()
if (!ok) {
print(str(trackmate.getErrorMessage()))
return null
}
// Display the results on top of the image
selectionModel = new SelectionModel(model)
displayer = new HyperStackDisplayer(model, selectionModel, imp)
displayer.render()
displayer.refresh()
// The feature model, that stores edge and track features.
fm = model.getFeatureModel()
space_units = model.getSpaceUnits()
time_units = model.getTimeUnits()
return model
}
// Convert-Tracks
def convert_tracks(model, dx, dy) {
"Convert the tracks into OMERO objects"
rois = new ArrayList()
tracks = model.getTrackModel().trackIDs(true)
tracks.each() { track_id ->
track = model.getTrackModel().trackSpots(track_id)
roi = new ROIData()
rois.add(roi)
points = ""
track.each() { spot ->
sid = spot.ID()
// Fetch spot features directly from spot.
x = spot.getFeature('POSITION_X')/dx
y = spot.getFeature('POSITION_Y')/dy
r = spot.getFeature('RADIUS')
z = spot.getFeature('POSITION_Z')
t = spot.getFeature('FRAME')
// Save spot as Point in OMERO
ellipse = new EllipseData(x, y, r, r)
ellipse.setZ((int) z)
ellipse.setT((int) t)
// set trackmate track ID and spot ID for later
ellipse.setText(track_id+':'+sid)
// set a default color
settings = ellipse.getShapeSettings()
settings.setStroke(Color.RED)
roi.addShapeData(ellipse)
points = points + x + ',' + y + ' '
}
// Save the track
points = points.trim()
polyline = new PolylineI()
polyline.setPoints(rstring(points))
pl = new PolylineData(polyline)
// set a default color
settings = pl.getShapeSettings()
settings.setStroke(Color.YELLOW)
roi.addShapeData(pl)
}
return rois
}
// Save-ROIs
def save_rois(gateway, rois, image_id) {
roi_facility = gateway.getFacility(ROIFacility)
user = gateway.getLoggedInUser()
ctx = new SecurityContext(user.getGroupId())
results = roi_facility.saveROIs(ctx, image_id, user.getId(), rois)
}
// Main part of the analysis
gateway = connect_to_omero()
exp = gateway.getLoggedInUser()
group_id = exp.getGroupId()
image = get_image(gateway, image_id)
open_image_plus(HOST, PORT, USERNAME, PASSWORD, group_id, image_id)
imp = IJ.getImage()
dx = imp.getCalibration().pixelWidth
dy = imp.getCalibration().pixelHeight
trackmate_model = create_tracker(imp)
if (trackmate_model == null) {
print("unable to create the trackmate model")
} else {
omero_rois = convert_tracks(trackmate_model, dx, dy)
save_rois(gateway, omero_rois, image_id)
print("done")
}
disconnect(gateway)