-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathOriginalMetadataRequestI.java
336 lines (306 loc) · 11 KB
/
OriginalMetadataRequestI.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
332
333
334
335
336
/*
* Copyright (C) 2013 Glencoe Software, Inc. All rights reserved.
*
* 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package omero.cmd.fs;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.AbstractMap;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.TreeMap;
import loci.formats.IFormatReader;
import loci.formats.ImageReader;
import ome.api.IQuery;
import ome.io.nio.PixelsService;
import ome.model.annotations.FileAnnotation;
import ome.model.core.Image;
import ome.model.core.Pixels;
import ome.model.fs.Fileset;
import ome.parameters.Parameters;
import ome.services.util.ReadOnlyStatus;
import omero.RType;
import omero.cmd.ERR;
import omero.cmd.Helper;
import omero.cmd.IRequest;
import omero.cmd.OriginalMetadataRequest;
import omero.cmd.OriginalMetadataResponse;
import omero.cmd.Response;
import omero.cmd.HandleI.Cancel;
import omero.constants.annotation.file.ORIGINALMETADATA;
import omero.constants.namespaces.NSCOMPANIONFILE;
import omero.util.IceMapper;
import com.google.common.collect.ImmutableMap;
/**
* Original metadata loader, handling both pre-FS and post-FS data.
*
* @author Josh Moore, josh at glencoesoftware.com
* @since 5.0.0
*/
public class OriginalMetadataRequestI extends OriginalMetadataRequest implements
IRequest, ReadOnlyStatus.IsAware {
private static final long serialVersionUID = -1L;
private final OriginalMetadataResponse rsp = new OriginalMetadataResponse();
private final PixelsService pixelsService;
private Helper helper;
public OriginalMetadataRequestI(PixelsService pixelsService) {
this.pixelsService = pixelsService;
}
//
// CMD API
//
public Map<String, String> getCallContext() {
Map<String, String> all = new HashMap<String, String>();
all.put("omero.group", "-1");
return all;
}
public void init(Helper helper) {
this.helper = helper;
this.helper.setSteps(1);
}
public Object step(int step) {
helper.assertStep(step);
loadFileset();
if (rsp.filesetId == null) {
loadFileAnnotation();
}
return null;
}
@Override
public void finish() throws Cancel {
// no-op
}
public void buildResponse(int step, Object object) {
helper.assertResponse(step);
if (helper.isLast(step)) {
helper.setResponseIfNull(rsp);
}
}
public Response getResponse() {
return helper.getResponse();
}
//
// LOADING & PARSING
//
/**
* Searches for a {@link Fileset} attached to this {@link Image}, and if present,
* uses Bio-Formats to parse the metadata into the {@link OriginalMetadataResponse}
* instance. If no {@link Fileset} is present, then there <em>may</em> be a
* {@link FileAnnotation} present which has a static version of the metadata.
*/
protected void loadFileset() {
rsp.filesetId = firstIdOrNull("select i.fileset.id from Image i where i.id = :id");
if (rsp.filesetId != null) {
final Image image = helper.getServiceFactory().getQueryService().get(Image.class, imageId);
final Pixels pixels = image.getPrimaryPixels();
try {
final IFormatReader reader = pixelsService.getBfReader(pixels);
final Hashtable<String, Object> global = reader.getGlobalMetadata();
final Hashtable<String, Object> series = reader.getSeriesMetadata();
rsp.globalMetadata = wrap(global);
rsp.seriesMetadata = wrap(series);
} catch (Throwable t) {
helper.cancel(new ERR(), t, "bf-reader-failure", "pixels", ""+pixels.getId());
}
}
}
/**
* Only called if {@link #loadFileset()} finds no {@link Fileset}. If any {@link FileAnnotation}
* instances with the appropriate namespace and name are found, the first one is taken and
* parsed into the {@link OriginalMetadataResponse}.
*/
protected void loadFileAnnotation() {
rsp.fileAnnotationId = firstIdOrNull(
"select a.id from Image i join i.annotationLinks l join l.child a " +
"where i.id = :id and a.file.name = '" + ORIGINALMETADATA.value + "' " +
"and a.ns = '" + NSCOMPANIONFILE.value + "'");
if (rsp.fileAnnotationId != null) {
final IQuery iQuery = helper.getServiceFactory().getQueryService();
final FileAnnotation fileAnnotation = iQuery.get(FileAnnotation.class, rsp.fileAnnotationId.getValue());
final String filePath = pixelsService.getFilesPath(fileAnnotation.getFile().getId());
parseOriginalMetadataTxt(new File(filePath));
}
}
//
// HELPERS
//
/**
* Use {@link IQuery#projection(String, Parameters)} to load the first
* long which matches the given query. This means that the first return
* value in the select statement should likely be the id of an object.
*/
protected omero.RLong firstIdOrNull(String query) {
List<Object[]> ids = helper.getServiceFactory().getQueryService()
.projection(query, new Parameters().addId(imageId).page(0, 1));
if (ids != null && ids.size() > 0) {
Object[] id = ids.get(0);
if (id != null && id.length > 0) {
return omero.rtypes.rlong((Long) id[0]);
}
}
return null;
}
/**
* Use {@link IceMapper} to convert from {@link Object} instances in
* the given {@link Hashtable} to {@link RType} instances. This may
* throw an exception on unknown types.
*/
protected Map<String, RType> wrap(Hashtable<String, Object> table) {
final Map<String, RType> rv = new HashMap<String, RType>();
if (table == null || table.size() == 0) {
return rv;
}
final IceMapper mapper = new IceMapper();
for (Entry<String, Object> entry : table.entrySet()) {
String key = entry.getKey();
Object val = entry.getValue();
try {
if (val instanceof Short) {
// Likely could be handled in toRType
rv.put(key, mapper.toRType(((Short) val).intValue()));
} else {
rv.put(key, mapper.toRType(val));
}
} catch (Exception e) {
String msg = String.format("Could not convert to rtype: " +
"key=%s, value=%s, type=%s ", key, val,
(val == null ? "null" : val.getClass()));
if (helper == null) {
// from command-line
System.err.println(msg);
} else {
helper.warn(msg);
}
}
}
return rv;
}
/**
* Split the given string at the rightmost '=' character among those that are the least enclosed by some kind of bracket.
* @param keyValue the key = value string
* @return the extracted key and value, or <code>null</code> if there is no '=' character
*/
private static Map.Entry<String, String> splitOnEquals(String keyValue) {
Integer equalsIndex = null;
Integer equalsSmallestDepth = null;
int currentIndex = 0;
int currentDepth = 0;
while (currentIndex < keyValue.length()) {
switch (keyValue.charAt(currentIndex)) {
case '(':
case '[':
case '{':
currentDepth++;
break;
case ')':
case ']':
case '}':
currentDepth--;
break;
case '=':
if (equalsSmallestDepth == null || currentDepth <= equalsSmallestDepth) {
equalsIndex = currentIndex;
equalsSmallestDepth = currentDepth;
}
break;
}
currentIndex++;
}
if (equalsIndex == null) {
return null;
} else {
return new AbstractMap.SimpleImmutableEntry<>(
keyValue.substring(0, equalsIndex).trim(),
keyValue.substring(equalsIndex + 1).trim());
}
}
/**
* Read the given INI-style file and populate the maps with the properties from the corresponding sections.
* @param file the file to read
*/
protected void parseOriginalMetadataTxt(File file) {
final Pattern section = Pattern.compile("\\s*\\[\\s*(.+?)\\s*\\]\\s*");
rsp.globalMetadata = new TreeMap<String, RType>();
rsp.seriesMetadata = new TreeMap<String, RType>();
final ImmutableMap<String, Map<String, RType>> sections =
ImmutableMap.of("GlobalMetadata", rsp.globalMetadata, "SeriesMetadata", rsp.seriesMetadata);
Map<String, RType> currentSection = null;
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
while (true) {
String line;
line = in.readLine();
if (line == null) {
break;
}
Matcher matcher;
if ((matcher = section.matcher(line)).matches()) {
currentSection = sections.get(matcher.group(1));
} else if (currentSection != null) {
final Entry<String, String> keyValue = splitOnEquals(line);
if (keyValue != null) {
currentSection.put(keyValue.getKey(), omero.rtypes.rstring(keyValue.getValue()));
}
}
}
} catch (IOException e) {
if (helper != null) {
helper.cancel(new ERR(), e, "reader-failure", "original-metadata", file.getPath());
}
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) { }
}
}
}
public static void main(String[] args) throws Exception {
OriginalMetadataRequestI omr = new OriginalMetadataRequestI(null);
ImageReader reader = new ImageReader();
for (String file : args) {
reader.setId(file);
final Hashtable<String, Object> bfglobal = reader.getGlobalMetadata();
final Hashtable<String, Object> bfseries = reader.getSeriesMetadata();
Map<String, RType> global = omr.wrap(bfglobal);
Map<String, RType> series = omr.wrap(bfseries);
printMap("[GlobalMetadata]", global);
printMap("[SeriesMetadata]", series);
}
reader.close();
}
private static void printMap(String title, Map<String, RType> map) {
System.out.println(title);
for (Map.Entry<String, RType> entry : map.entrySet()) {
System.out.print(entry.getKey());
System.out.print("=");
System.out.print(entry.getValue());
System.out.print("\n");
}
}
@Override
public boolean isReadOnly(ReadOnlyStatus readOnly) {
return true;
}
}