4949import fiji .plugin .trackmate .io .IOUtils ;
5050import fiji .plugin .trackmate .util .TMUtils ;
5151
52- public class ExportTracksToXML extends AbstractTMAction {
52+ public class ExportTracksToXML extends AbstractTMAction
53+ {
5354
5455 public static final String NAME = "Export tracks to XML file" ;
5556
5657 public static final String KEY = "EXPORT_TRACKS_TO_XML_SIMPLE" ;
58+
5759 public static final String INFO_TEXT = "<html>" +
58- "Export the tracks in the current model content to a XML " +
59- "file in a simple format. " +
60- "<p> " +
61- "The file will have one element per track, and each track " +
62- "contains several spot elements. These spots are " +
63- "sorted by frame number, and have 4 numerical attributes: " +
64- "the frame number this spot is in, and its X, Y, Z position in " +
65- "physical units as specified in the image properties. " +
66- "<p>" +
67- "As such, this format <u>cannot</u> handle track merging and " +
68- "splitting properly, and is suited only for non-branching tracks." +
69- "</html>" ;
60+ "Export the tracks in the current model content to a XML " +
61+ "file in a simple format. " +
62+ "<p> " +
63+ "The file will have one element per track, and each track " +
64+ "contains several spot elements. These spots are " +
65+ "sorted by frame number, and have 4 numerical attributes: " +
66+ "the frame number this spot is in, and its X, Y, Z position in " +
67+ "physical units as specified in the image properties. " +
68+ "<p>" +
69+ "As such, this format <u>cannot</u> handle track merging and " +
70+ "splitting properly, and is suited only for non-branching tracks." +
71+ "</html>" ;
7072
7173 /**
7274 * Static utility that silently exports tracks in a simplified xml format,
@@ -84,125 +86,156 @@ public class ExportTracksToXML extends AbstractTMAction {
8486 * @throws IOException
8587 * if there is a problem writing the file.
8688 */
87- public static void export (final Model model , final Settings settings , final File file ) throws FileNotFoundException , IOException {
88- final Element root = marshall (model , settings , Logger .VOID_LOGGER );
89- final Document document = new Document (root );
90- final XMLOutputter outputter = new XMLOutputter (Format .getPrettyFormat ());
91- outputter .output (document , new FileOutputStream (file ));
89+ public static void export ( final Model model , final Settings settings , final File file ) throws FileNotFoundException , IOException
90+ {
91+ final Element root = marshall ( model , settings , Logger .VOID_LOGGER );
92+ final Document document = new Document ( root );
93+ final XMLOutputter outputter = new XMLOutputter ( Format .getPrettyFormat () );
94+ outputter .output ( document , new FileOutputStream ( file ) );
9295 }
9396
9497 @ Override
9598 public void execute ( final TrackMate trackmate , final SelectionModel selectionModel , final DisplaySettings displaySettings , final Frame parent )
9699 {
97- logger .log ("Exporting tracks to simple XML format.\n " );
100+ logger .log ( "Exporting tracks to simple XML format.\n " );
98101 final Model model = trackmate .getModel ();
99- final int ntracks = model .getTrackModel ().nTracks (true );
100- if (ntracks == 0 ) {
101- logger .log ("No visible track found. Aborting.\n " );
102+ final int ntracks = model .getTrackModel ().nTracks ( true );
103+ if ( ntracks == 0 )
104+ {
105+ logger .log ( "No visible track found. Aborting.\n " );
102106 return ;
103107 }
104108
105- logger .log (" Preparing XML data.\n " );
106- final Element root = marshall (model , trackmate .getSettings (), logger );
109+ logger .log ( " Preparing XML data.\n " );
110+ final Element root = marshall ( model , trackmate .getSettings (), logger );
107111
108112 File folder ;
109- try {
110- folder = new File (trackmate .getSettings ().imp .getOriginalFileInfo ().directory );
111- } catch (final NullPointerException npe ) {
112- folder = new File (System .getProperty ("user.dir" )).getParentFile ().getParentFile ();
113+ try
114+ {
115+ folder = new File ( trackmate .getSettings ().imp .getOriginalFileInfo ().directory );
116+ }
117+ catch ( final NullPointerException npe )
118+ {
119+ folder = new File ( System .getProperty ( "user.dir" ) ).getParentFile ().getParentFile ();
113120 }
114121
115122 File file ;
116- try {
123+ try
124+ {
117125 String filename = trackmate .getSettings ().imageFileName ;
118- final int dot = filename .indexOf ("." );
119- filename = dot < 0 ? filename : filename .substring (0 , dot );
120- file = new File (folder .getPath () + File .separator + filename +"_Tracks.xml" );
121- } catch (final NullPointerException npe ) {
122- file = new File (folder .getPath () + File .separator + "Tracks.xml" );
126+ final int dot = filename .indexOf ( "." );
127+ filename = dot < 0 ? filename : filename .substring ( 0 , dot );
128+ file = new File ( folder .getPath () + File .separator + filename + "_Tracks.xml" );
129+ }
130+ catch ( final NullPointerException npe )
131+ {
132+ file = new File ( folder .getPath () + File .separator + "Tracks.xml" );
123133 }
124- file = IOUtils .askForFileForSaving ( file , parent , logger );
125- if (null == file ) {
134+ file = IOUtils .askForFileForSaving ( file , parent );
135+ if ( null == file )
136+ {
137+ logger .log ( "Exporting to simple XML aborted.\n " );
126138 return ;
127139 }
128140
129- logger .log (" Writing to file.\n " );
130- final Document document = new Document (root );
131- final XMLOutputter outputter = new XMLOutputter (Format .getPrettyFormat ());
132- try {
133- outputter .output (document , new FileOutputStream (file ));
134- } catch (final FileNotFoundException e ) {
135- logger .error ("Trouble writing to " +file +":\n " + e .getMessage ());
136- } catch (final IOException e ) {
137- logger .error ("Trouble writing to " +file +":\n " + e .getMessage ());
141+ logger .log ( " Writing to file.\n " );
142+ final Document document = new Document ( root );
143+ final XMLOutputter outputter = new XMLOutputter ( Format .getPrettyFormat () );
144+ try
145+ {
146+ outputter .output ( document , new FileOutputStream ( file ) );
147+ }
148+ catch ( final FileNotFoundException e )
149+ {
150+ logger .error ( "Trouble writing to " + file + ":\n " + e .getMessage () );
151+ }
152+ catch ( final IOException e )
153+ {
154+ logger .error ( "Trouble writing to " + file + ":\n " + e .getMessage () );
138155 }
139- logger .log ("Done.\n " );
156+ logger .log ( "Done.\n " );
140157 }
141158
142- private static Element marshall (final Model model , final Settings settings , final Logger logger ) {
143- logger .setStatus ("Marshalling..." );
144- final Element content = new Element (CONTENT_KEY );
159+ private static Element marshall ( final Model model , final Settings settings , final Logger logger )
160+ {
161+ logger .setStatus ( "Marshalling..." );
162+ final Element content = new Element ( CONTENT_KEY );
145163
146- content .setAttribute (NTRACKS_ATT , "" + model .getTrackModel ().nTracks (true ) );
147- content .setAttribute (PHYSUNIT_ATT , model .getSpaceUnits ());
148- content .setAttribute (FRAMEINTERVAL_ATT , "" + settings .dt );
149- content .setAttribute (FRAMEINTERVALUNIT_ATT , "" + model .getTimeUnits ());
150- content .setAttribute (DATE_ATT , TMUtils .getCurrentTimeString ());
151- content .setAttribute (FROM_ATT , TrackMate .PLUGIN_NAME_STR + " v" + TrackMate .PLUGIN_NAME_VERSION );
164+ content .setAttribute ( NTRACKS_ATT , "" + model .getTrackModel ().nTracks ( true ) );
165+ content .setAttribute ( PHYSUNIT_ATT , model .getSpaceUnits () );
166+ content .setAttribute ( FRAMEINTERVAL_ATT , "" + settings .dt );
167+ content .setAttribute ( FRAMEINTERVALUNIT_ATT , "" + model .getTimeUnits () );
168+ content .setAttribute ( DATE_ATT , TMUtils .getCurrentTimeString () );
169+ content .setAttribute ( FROM_ATT , TrackMate .PLUGIN_NAME_STR + " v" + TrackMate .PLUGIN_NAME_VERSION );
152170
153- final Set <Integer > trackIDs = model .getTrackModel ().trackIDs (true );
171+ final Set < Integer > trackIDs = model .getTrackModel ().trackIDs ( true );
154172 int i = 0 ;
155- for (final Integer trackID : trackIDs ) {
173+ for ( final Integer trackID : trackIDs )
174+ {
156175
157- final Set <Spot > track = model .getTrackModel ().trackSpots (trackID );
176+ final Set < Spot > track = model .getTrackModel ().trackSpots ( trackID );
158177
159- final Element trackElement = new Element (TRACK_KEY );
160- trackElement .setAttribute (NSPOTS_ATT , "" + track .size ());
178+ final Element trackElement = new Element ( TRACK_KEY );
179+ trackElement .setAttribute ( NSPOTS_ATT , "" + track .size () );
161180
162181 // Sort them by time
163- final TreeSet <Spot > sortedTrack = new TreeSet <>(Spot .timeComparator );
164- sortedTrack .addAll (track );
165-
166- for (final Spot spot : sortedTrack ) {
167- final int frame = spot .getFeature (Spot .FRAME ).intValue ();
168- final double x = spot .getFeature (Spot .POSITION_X );
169- final double y = spot .getFeature (Spot .POSITION_Y );
170- final double z = spot .getFeature (Spot .POSITION_Z );
171-
172- final Element spotElement = new Element (SPOT_KEY );
173- spotElement .setAttribute (T_ATT , "" +frame );
174- spotElement .setAttribute (X_ATT , "" +x );
175- spotElement .setAttribute (Y_ATT , "" +y );
176- spotElement .setAttribute (Z_ATT , "" +z );
177- trackElement .addContent (spotElement );
182+ final TreeSet < Spot > sortedTrack = new TreeSet <>( Spot .timeComparator );
183+ sortedTrack .addAll ( track );
184+
185+ for ( final Spot spot : sortedTrack )
186+ {
187+ final int frame = spot .getFeature ( Spot .FRAME ).intValue ();
188+ final double x = spot .getFeature ( Spot .POSITION_X );
189+ final double y = spot .getFeature ( Spot .POSITION_Y );
190+ final double z = spot .getFeature ( Spot .POSITION_Z );
191+
192+ final Element spotElement = new Element ( SPOT_KEY );
193+ spotElement .setAttribute ( T_ATT , "" + frame );
194+ spotElement .setAttribute ( X_ATT , "" + x );
195+ spotElement .setAttribute ( Y_ATT , "" + y );
196+ spotElement .setAttribute ( Z_ATT , "" + z );
197+ trackElement .addContent ( spotElement );
178198 }
179- content .addContent (trackElement );
180- logger .setProgress (i ++ / (0d + model .getTrackModel ().nTracks (true )) );
199+ content .addContent ( trackElement );
200+ logger .setProgress ( i ++ / ( 0d + model .getTrackModel ().nTracks ( true ) ) );
181201 }
182202
183- logger .setStatus ("" );
184- logger .setProgress (1 );
203+ logger .setStatus ( "" );
204+ logger .setProgress ( 1 );
185205 return content ;
186206 }
187207
188208 /*
189209 * XML KEYS
190210 */
191211
192- private static final String CONTENT_KEY = "Tracks" ;
193- private static final String DATE_ATT = "generationDateTime" ;
194- private static final String PHYSUNIT_ATT = "spaceUnits" ;
195- private static final String FRAMEINTERVAL_ATT = "frameInterval" ;
196- private static final String FRAMEINTERVALUNIT_ATT = "timeUnits" ;
197- private static final String FROM_ATT = "from" ;
198- private static final String NTRACKS_ATT = "nTracks" ;
199- private static final String NSPOTS_ATT = "nSpots" ;
200- private static final String TRACK_KEY = "particle" ;
201- private static final String SPOT_KEY = "detection" ;
202- private static final String X_ATT = "x" ;
203- private static final String Y_ATT = "y" ;
204- private static final String Z_ATT = "z" ;
205- private static final String T_ATT = "t" ;
212+ private static final String CONTENT_KEY = "Tracks" ;
213+
214+ private static final String DATE_ATT = "generationDateTime" ;
215+
216+ private static final String PHYSUNIT_ATT = "spaceUnits" ;
217+
218+ private static final String FRAMEINTERVAL_ATT = "frameInterval" ;
219+
220+ private static final String FRAMEINTERVALUNIT_ATT = "timeUnits" ;
221+
222+ private static final String FROM_ATT = "from" ;
223+
224+ private static final String NTRACKS_ATT = "nTracks" ;
225+
226+ private static final String NSPOTS_ATT = "nSpots" ;
227+
228+ private static final String TRACK_KEY = "particle" ;
229+
230+ private static final String SPOT_KEY = "detection" ;
231+
232+ private static final String X_ATT = "x" ;
233+
234+ private static final String Y_ATT = "y" ;
235+
236+ private static final String Z_ATT = "z" ;
237+
238+ private static final String T_ATT = "t" ;
206239
207240 @ Plugin ( type = TrackMateActionFactory .class )
208241 public static class Factory implements TrackMateActionFactory
0 commit comments