-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileExportSPW.java
More file actions
425 lines (336 loc) · 13.1 KB
/
FileExportSPW.java
File metadata and controls
425 lines (336 loc) · 13.1 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
* #%L
* OME Bio-Formats package for reading and converting biological file formats.
* %%
* Copyright (C) 2005 - 2014 Open Microscopy Environment:
* - Board of Regents of the University of Wisconsin-Madison
* - Glencoe Software, Inc.
* - University of Dundee
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import loci.common.services.DependencyException;
import loci.common.services.ServiceException;
import loci.common.services.ServiceFactory;
import loci.formats.CoreMetadata;
import loci.formats.FormatException;
import loci.formats.FormatTools;
import loci.formats.ImageWriter;
import loci.formats.meta.IMetadata;
import loci.formats.ome.OMEXMLMetadata;
import loci.formats.services.OMEXMLService;
import loci.formats.MetadataTools;
import ome.xml.model.enums.DimensionOrder;
import ome.xml.model.enums.EnumerationException;
import ome.xml.model.enums.PixelType;
import ome.xml.model.primitives.PositiveInteger;
import ome.xml.model.primitives.NonNegativeInteger;
import ome.xml.model.enums.NamingConvention;
/**
* Example class that shows how to export raw pixel data to OME-TIFF as a Plate using
* Bio-Formats version 4.2 or later.
*/
public class FileExportSPW {
private int sizeT = 3;
private int rows = 2;
private int cols = 2;
/** The file writer. */
private ImageWriter writer;
/** The name of the current output file. */
private String outputFile;
/** The name of the current output file. */
private String outputBase;
/**
* Construct a new FileExport that will save to the specified file.
*
* @param outputFile the file to which we will export
*/
public FileExportSPW(String outputBase) {
this.outputBase = outputBase;
}
/** Save a single 2x2 uint16 plane of data. */
public void export() {
int width = 4, height = 4;
int pixelType = FormatTools.UINT16;
Exception exception = null;
int series = 0;
int index = 0;
int nSeries = rows * cols;
Path path;
IMetadata [] omexmlSets = initializeMetadata(width, height, pixelType);
while (series < nSeries) {
outputFile = outputBase + Integer.toString(series + 1) + ".ome.tiff";
path = FileSystems.getDefault().getPath(outputFile);
//delete if exists
//NB deleting old files seems to be critical when changing size
try {
boolean success = Files.deleteIfExists(path);
System.out.println("Delete status: " + success);
} catch (IOException | SecurityException e) {
System.err.println(e);
}
IMetadata omexml = omexmlSets[series];
// only save a plane if the file writer was initialized successfully
boolean initializationSuccess = initializeWriter(omexml);
if (initializationSuccess) {
System.out.println("saving to ");
System.out.println(outputFile);
index = 0;
for (int p = 0; p < sizeT; p++) {
savePlane(width, height, pixelType, index, 0);
index++;
}
cleanup();
series++;
} //endif
} //endwhile
}
/**
* Set up the file writer.
*
* @param omexml the IMetadata object that is to be associated with the writer
* @return true if the file writer was successfully initialized; false if an
* error occurred
*/
private boolean initializeWriter(IMetadata omexml) {
// create the file writer and associate the OME-XML metadata with it
writer = new ImageWriter();
writer.setMetadataRetrieve(omexml);
Exception exception = null;
try {
writer.setId(outputFile);
}
catch (FormatException e) {
exception = e;
}
catch (IOException e) {
exception = e;
}
if (exception != null) {
System.err.println("Failed to initialize file writer.");
exception.printStackTrace();
}
return exception == null;
}
/**
* Populate the minimum amount of metadata required to export a Plate.
*
* @param width the width (in pixels) of the image
* @param height the height (in pixels) of the image
* @param pixelType the pixel type of the image; @see loci.formats.FormatTools
*/
private IMetadata[] initializeMetadata(int width, int height, int pixelType) {
Exception exception = null;
PositiveInteger pwidth = new PositiveInteger(width);
PositiveInteger pheight = new PositiveInteger(height);
String plateId = MetadataTools.createLSID("Plate", 0);
int series = 0;
int well = 0;
OMEXMLMetadata [] metaDataSets = new OMEXMLMetadata[4];
String [] imageIDs = new String[4];
imageIDs[0] = MetadataTools.createLSID("Image:0", 0);
imageIDs[1] = MetadataTools.createLSID("Image:1", 0);
imageIDs[2] = MetadataTools.createLSID("Image:2", 0);
imageIDs[3] = MetadataTools.createLSID("Image:3", 0);
String [] wellIDs = new String[4];
wellIDs[0] = MetadataTools.createLSID("Well:0", 0);
wellIDs[1] = MetadataTools.createLSID("Well:1", 0);
wellIDs[2] = MetadataTools.createLSID("Well:2", 0);
wellIDs[3] = MetadataTools.createLSID("Well:3", 0);
String [] wellSampleIDs = new String[4];
wellSampleIDs[0] = MetadataTools.createLSID("WellSample:0", 0);
wellSampleIDs[1] = MetadataTools.createLSID("WellSample:1", 0);
wellSampleIDs[2] = MetadataTools.createLSID("WellSample:2", 0);
wellSampleIDs[3] = MetadataTools.createLSID("WellSample:3", 0);
String suffixStr;
int plateIndex = 0;
int sampleIndex = 0;
try {
ServiceFactory factory = new ServiceFactory();
OMEXMLService service = factory.getInstance(OMEXMLService.class);
for (int row = 0; row < rows; row++) {
for (int column = 0; column < cols; column++) {
// create the OME-XML metadata storage object
OMEXMLMetadata meta = service.createOMEXMLMetadata();
//IMetadata meta = service.createOMEXMLMetadata();
meta.createRoot();
// Create Minimal 2x2 Plate
meta.setPlateID(plateId, 0);
meta.setPlateRowNamingConvention(NamingConvention.LETTER, 0);
meta.setPlateColumnNamingConvention(NamingConvention.NUMBER, 0);
meta.setPlateRows(new PositiveInteger(rows), 0);
meta.setPlateColumns(new PositiveInteger(cols), 0);
meta.setPlateName("First test Plate", 0);
suffixStr = Integer.toString(well);
// Create Image
String imageID = imageIDs[well];
meta.setImageID(imageID, series);
meta.setImageName("Image: " + suffixStr, series);
meta.setPixelsID("Pixels:0:"+suffixStr, series);
// specify that the pixel data is stored in big-endian format
// change 'TRUE' to 'FALSE' to specify little-endian format
meta.setPixelsBinDataBigEndian(Boolean.TRUE, series, 0);
// specify that the images are stored in ZCT order
meta.setPixelsDimensionOrder(DimensionOrder.XYZCT, series);
// specify that the pixel type of the images
meta.setPixelsType(PixelType.fromString(FormatTools.getPixelTypeString(pixelType)), series);
// specify the dimensions of the images
meta.setPixelsSizeX(pwidth, series);
meta.setPixelsSizeY(pheight, series);
meta.setPixelsSizeZ(new PositiveInteger(1), series);
meta.setPixelsSizeC(new PositiveInteger(1), series);
meta.setPixelsSizeT(new PositiveInteger(sizeT), series);
// define each channel and specify the number of samples in the channel
// the number of samples is 3 for RGB images and 1 otherwise
meta.setChannelID("Channel:0:"+suffixStr, series,0 );
meta.setChannelSamplesPerPixel(new PositiveInteger(1), series, 0);
// create a well for each row & column within each of the metadata blocs
int inwell = 0;
for (int inrow = 0; inrow < rows; inrow++) {
for (int incolumn = 0; incolumn < cols; incolumn++) {
String wellID = wellIDs[inwell];
String inImageID = imageIDs[inwell];
// set up wells
meta.setWellID(wellID, plateIndex, inwell);
meta.setWellRow(new NonNegativeInteger(inrow), plateIndex, inwell);
meta.setWellColumn(new NonNegativeInteger(incolumn), plateIndex, inwell);
// one sample per well
String wellSampleID = wellSampleIDs[inwell];
meta.setWellSampleID(wellSampleID, 0, inwell, sampleIndex);
meta.setWellSampleIndex(new NonNegativeInteger(inwell), 0, inwell, sampleIndex);
//void setWellSampleImageRef(String image,int plateIndex, int wellIndex, int wellSampleIndex)
meta.setWellSampleImageRef(inImageID, 0, inwell, sampleIndex);
inwell++;
}
}
// add FLIM ModuloAlongT annotation if required
CoreMetadata modlo = createModuloAnn(meta);
service.addModuloAlong(meta, modlo, series);
metaDataSets[well] = meta;
well++;
}
}
//String dump = meta.dumpXML();
//System.out.println("dump = ");
//System.out.println(dump);
return metaDataSets;
}
catch (ServiceException e) {
exception = e;
}
catch (EnumerationException e) {
exception = e;
}
catch (DependencyException e) {
exception = e;
}
System.err.println("Failed to populate OME-XML metadata object.");
exception.printStackTrace();
return null;
}
/**
* Add ModuloAlong annotation.
*/
private CoreMetadata createModuloAnn(IMetadata meta) {
CoreMetadata modlo = new CoreMetadata();
modlo.moduloT.type = loci.formats.FormatTools.LIFETIME;
modlo.moduloT.unit = "ps";
modlo.moduloT.typeDescription = "Gated";
modlo.moduloT.labels = new String[sizeT];
for (int i = 0; i < sizeT; i++) {
modlo.moduloT.labels[i] = Integer.toString(i * 1000);
}
return modlo;
}
/**
* Generate a plane of pixel data and save it to the output file.
*
* @param width the width of the image in pixels
* @param height the height of the image in pixels
* @param pixelType the pixel type of the image; @see loci.formats.FormatTools
*/
private void savePlane(int width, int height, int pixelType, int index, int series ) {
byte[] plane = createImage(width, height, pixelType, index, series);
Exception exception = null;
try {
writer.saveBytes(index, plane);
}
catch (FormatException e) {
exception = e;
}
catch (IOException e) {
exception = e;
}
if (exception != null) {
System.err.println("Failed to save plane.");
exception.printStackTrace();
}
}
/**
* Generate a plane of pixel data.
*
* @param width the width of the image in pixels
* @param height the height of the image in pixels
* @param pixelType the pixel type of the image; @see loci.formats.FormatTools
*/
private byte[] createImage(int width, int height, int pixelType, int index, int series) {
// create a blank image of the specified size
int bpp = FormatTools.getBytesPerPixel(pixelType);
byte[] img = new byte[width * height * bpp];
ByteBuffer bb = ByteBuffer.wrap(img);
bb.order(ByteOrder.BIG_ENDIAN);
// fill it with background
for (int i = 0; i < img.length; i += bpp) {
bb.putShort(i, (short) 200);
}
//then set 1 pixel to non-zero. Different values in each plane
switch (index) {
case 0: bb.putShort(series * bpp, (short) 1000);
break;
case 1: bb.putShort(series * bpp, (short) 700);
break;
case 2: bb.putShort(series * bpp, (short) 300);
break;
}
return img;
}
/** Close the file writer. */
private void cleanup() {
try {
writer.close();
}
catch (IOException e) {
System.err.println("Failed to close file writer.");
e.printStackTrace();
}
}
/**
* To export a file to OME-TIFF:
*
* $ java FileExport output-file.ome.tiff
*/
public static void main(String[] args) throws Exception {
String baseName = "SPWFromJava";
FileExportSPW exporter = new FileExportSPW(baseName);
exporter.export();
}
}