/
AFIReader.java
331 lines (273 loc) · 10.1 KB
/
AFIReader.java
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
/*
* #%L
* OME Bio-Formats package for reading and converting biological file formats.
* %%
* Copyright (C) 2005 - 2016 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%
*/
package loci.formats.in;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import loci.common.Constants;
import loci.common.DataTools;
import loci.common.Location;
import loci.common.RandomAccessInputStream;
import loci.common.xml.BaseHandler;
import loci.common.xml.XMLTools;
import loci.formats.ChannelSeparator;
import loci.formats.CoreMetadata;
import loci.formats.FormatException;
import loci.formats.FormatReader;
import loci.formats.FormatTools;
import loci.formats.MetadataTools;
import loci.formats.meta.MetadataStore;
import ome.xml.model.primitives.PositiveFloat;
import ome.xml.model.primitives.PositiveInteger;
import ome.xml.model.primitives.Timestamp;
import ome.units.quantity.Length;
import ome.units.quantity.Time;
import ome.units.UNITS;
import org.xml.sax.Attributes;
/**
* AFIReader is the file format reader for Aperio AFI files.
*
* @author Melissa Linkert melissa at glencoesoftware.com
*/
public class AFIReader extends FormatReader {
// -- Constants --
private static final int EXTRA_IMAGES = 3;
// -- Fields --
private ArrayList<String> pixels = new ArrayList<String>();
private ChannelSeparator[] reader;
// -- Constructor --
/** Constructs a new AFI reader. */
public AFIReader() {
super("Aperio AFI", "afi");
domains = new String[] {FormatTools.HISTOLOGY_DOMAIN};
hasCompanionFiles = true;
datasetDescription = "One .afi file and several similarly-named .svs files";
}
// -- IFormatReader API methods --
/* @see loci.formats.IFormatReader#isThisType(RandomAccessInputStream) */
@Override
public boolean isThisType(RandomAccessInputStream stream) throws IOException {
final int blockLen = 4;
if (!FormatTools.validStream(stream, blockLen, false)) return false;
return true;
}
/* @see loci.formats.IFormatReader#isSingleFile(String) */
@Override
public boolean isSingleFile(String id) throws FormatException, IOException {
return false;
}
/* @see loci.formats.IFormatReader#getOptimalTileWidth() */
@Override
public int getOptimalTileWidth() {
return reader[0].getOptimalTileWidth();
}
/* @see loci.formats.IFormatReader#getOptimalTileHeight() */
@Override
public int getOptimalTileHeight() {
return reader[0].getOptimalTileHeight();
}
/* @see loci.formats.IFormatReader#openThumbBytes(int) */
@Override
public byte[] openThumbBytes(int no) throws FormatException, IOException {
FormatTools.assertId(currentId, true, 1);
if (getCoreIndex() >= core.size() - EXTRA_IMAGES) {
reader[0].setCoreIndex(getCoreIndex());
return reader[0].openThumbBytes(no);
}
int coreIndex = getCoreIndex();
setCoreIndex(core.size() - EXTRA_IMAGES - 1);
byte[] thumb = FormatTools.openThumbBytes(this, no);
setCoreIndex(coreIndex);
return thumb;
}
/**
* @see loci.formats.IFormatReader#openBytes(int, byte[], int, int, int, int)
*/
@Override
public byte[] openBytes(int no, byte[] buf, int x, int y, int w, int h)
throws FormatException, IOException
{
FormatTools.checkPlaneParameters(this, no, buf.length, x, y, w, h);
if (getCoreIndex() >= core.size() - EXTRA_IMAGES) {
reader[0].setCoreIndex(getCoreIndex());
return reader[0].openBytes(no, buf, x, y, w, h);
}
int[] coords = getZCTCoords(no);
int channel = coords[1];
int index = getIndex(coords[0], 0, coords[2]);
reader[channel].setCoreIndex(getCoreIndex());
return reader[channel].openBytes(index, buf, x, y, w, h);
}
/* @see loci.formats.IFormatReader#getSeriesUsedFiles(boolean) */
@Override
public String[] getSeriesUsedFiles(boolean noPixels) {
FormatTools.assertId(currentId, true, 1);
if (noPixels) {
return new String[] {currentId};
}
String[] files = new String[pixels.size() + 1];
files[0] = currentId;
for (int i=0; i<pixels.size(); i++) {
files[i + 1] = pixels.get(i);
}
return files;
}
/* @see loci.formats.IFormatReader#fileGroupOption(String) */
@Override
public int fileGroupOption(String id) throws FormatException, IOException {
return FormatTools.MUST_GROUP;
}
/* @see loci.formats.IFormatReader#close(boolean) */
@Override
public void close(boolean fileOnly) throws IOException {
super.close(fileOnly);
if (!fileOnly) {
if (reader != null) {
for (ChannelSeparator r : reader) {
if (r != null) {
r.close();
}
}
}
reader = null;
pixels.clear();
}
}
// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
super.initFile(id);
// The AFI file is just simple XML that lists the .svs files from
// which to read pixel data. Each .svs corresponds to a single channel;
// we assemble the channels in the order in which they are stored in
// the XML.
//
// Note that the last two series are identical across the .svs files,
// so we just use the ones from the first listed file.
String xml = DataTools.readFile(id);
XMLTools.parseXML(xml, new AFIHandler());
String parent = new Location(id).getAbsoluteFile().getParent();
String[] channelNames = new String[pixels.size()];
reader = new ChannelSeparator[pixels.size()];
for (int i=0; i<pixels.size(); i++) {
String file = pixels.get(i);
int underscore = file.indexOf("_");
int fullStop = file.indexOf(".");
if (underscore >= 0 && fullStop > underscore) {
channelNames[i] = file.substring(underscore + 1, fullStop);
}
pixels.set(i, new Location(parent, file).getAbsolutePath());
reader[i] = new ChannelSeparator(new SVSReader());
reader[i].setFlattenedResolutions(hasFlattenedResolutions());
reader[i].setId(pixels.get(i));
}
core = reader[0].getCoreMetadataList();
for (int i=0; i<core.size() - EXTRA_IMAGES; i++) {
CoreMetadata c = core.get(i);
c.sizeC = pixels.size();
c.imageCount = c.sizeC * c.sizeZ * c.sizeT;
c.rgb = false;
if (i == 0) {
c.resolutionCount = core.size() - EXTRA_IMAGES;
}
}
MetadataStore store = makeFilterMetadata();
boolean minimalMetadata =
getMetadataOptions().getMetadataLevel() == MetadataLevel.MINIMUM;
MetadataTools.populatePixels(store, this, !minimalMetadata);
String fileID = currentId.substring(
currentId.lastIndexOf(File.separator) + 1, currentId.lastIndexOf("."));
for (int i=0; i<getSeriesCount(); i++) {
store.setImageName(fileID + " - image #" + (i + 1), i);
}
if (!minimalMetadata) {
Length[] emission = new Length[pixels.size()];
Length[] excitation = new Length[pixels.size()];
Double[] exposure = new Double[pixels.size()];
Timestamp[] datestamp = new Timestamp[pixels.size()];
Length[] physicalSizes = null;
double magnification = Double.NaN;
for (int c=0; c<pixels.size(); c++) {
SVSReader baseReader = (SVSReader) reader[c].getReader();
emission[c] = baseReader.getEmission();
excitation[c] = baseReader.getExcitation();
exposure[c] = baseReader.getExposureTime();
datestamp[c] = baseReader.getDatestamp();
physicalSizes = baseReader.getPhysicalSizes();
if (c == 0) {
magnification = baseReader.getMagnification();
}
}
String instrument = MetadataTools.createLSID("Instrument", 0);
String objective = MetadataTools.createLSID("Objective", 0, 0);
store.setInstrumentID(instrument, 0);
store.setObjectiveID(objective, 0, 0);
store.setObjectiveNominalMagnification(magnification, 0, 0);
for (int i=0; i<getSeriesCount() - EXTRA_IMAGES; i++) {
if (datestamp[0] != null) {
store.setImageAcquisitionDate(datestamp[0], i);
}
store.setImageInstrumentRef(instrument, i);
store.setObjectiveSettingsID(objective, i);
if (i < physicalSizes.length &&
physicalSizes[i] != null &&
physicalSizes[i].value(UNITS.MICROM).doubleValue() - Constants.EPSILON > 0)
{
Length size = physicalSizes[i];
store.setPixelsPhysicalSizeX(size, i);
store.setPixelsPhysicalSizeY(size, i);
}
for (int c=0; c<channelNames.length; c++) {
store.setChannelName(channelNames[c], i, c);
if (emission[c] != null) {
store.setChannelEmissionWavelength(emission[c], i, c);
}
if (excitation[c] != null) {
store.setChannelExcitationWavelength(excitation[c], i, c);
}
store.setPlaneExposureTime(FormatTools.createTime(exposure[c], UNITS.S), i, c);
}
}
}
}
// -- Helper class --
class AFIHandler extends BaseHandler {
private String currentElement;
@Override
public void characters(char[] ch, int start, int length) {
String value = new String(ch, start, length);
if (currentElement.equals("Path") && value.trim().length() > 0) {
pixels.add(value);
}
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes)
{
currentElement = qName;
}
}
}