/
ReadDataAdvanced.java
267 lines (246 loc) · 8.9 KB
/
ReadDataAdvanced.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
/*
*------------------------------------------------------------------------------
* Copyright (C) 2006-2016 University of Dundee & Open Microscopy Environment.
* 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 training;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import omero.api.IContainerPrx;
import omero.api.IQueryPrx;
import omero.gateway.Gateway;
import omero.gateway.LoginCredentials;
import omero.gateway.SecurityContext;
import omero.gateway.facility.DataManagerFacility;
import omero.log.SimpleLogger;
import omero.model.Dataset;
import omero.model.IObject;
import omero.model.Project;
import omero.model.TagAnnotation;
import omero.sys.Filter;
import omero.sys.ParametersI;
import omero.gateway.model.DatasetData;
import omero.gateway.model.ExperimenterData;
import omero.gateway.model.ImageData;
import omero.gateway.model.ProjectData;
import omero.gateway.model.TagAnnotationData;
/**
* More advanced code for how to load data from an OMERO server.
*
* @author Jean-Marie Burel
* <a href="mailto:j.burel@dundee.ac.uk">j.burel@dundee.ac.uk</a>
* @since Beta4.3.2
*/
public class ReadDataAdvanced
{
//The value used if the configuration file is not used. To edit*/
/** The server address.*/
private static String hostName = "serverName";
/** The username.*/
private static String userName = "userName";
/** The password.*/
private static String password = "password";
//end edit
/** The name of a Dataset.*/
private String datasetName = "MyDataset";
/** The name of a Tag.*/
private String tagName = "MyTag";
private Gateway gateway;
private SecurityContext ctx;
/**
* Creates 3 Datasets with the name defined by {@link #datasetName}.
*/
private void createDatasets()
throws Exception
{
DataManagerFacility dm = gateway.getFacility(DataManagerFacility.class);
for (int i = 0; i < 3; i ++)
{
DatasetData d = new DatasetData();
d.setName(datasetName);
dm.saveAndReturnObject(ctx, d);
}
}
/**
* Creates 3 Tags with the namespace value defined by {@link #tagName}.
*/
private void createTags()
throws Exception
{
DataManagerFacility dm = gateway.getFacility(DataManagerFacility.class);
for (int i = 0; i < 3; i ++)
{
TagAnnotationData t = new TagAnnotationData(tagName);
((TagAnnotation)t.asIObject()).setNs(omero.rtypes.rstring(Setup.TRAINING_NS));
t.setDescription(String.format("%s %s", tagName, i));
dm.saveAndReturnObject(ctx, t);
}
}
/**
* Retrieve the Datasets that match the name value.
*/
private void loadDatasetsByName()
throws Exception
{
final boolean caseSensitive = true;
final Filter filter = new Filter();
// Return the first 10 hits or less.
filter.limit = omero.rtypes.rint(10);
filter.offset = omero.rtypes.rint(0);
IQueryPrx proxy = gateway.getQueryService(ctx);
List<IObject> datasets = (List<IObject>)
proxy.findAllByString("Dataset", "name", datasetName, caseSensitive,
filter);
System.out.println("\nList Datasets:");
for (IObject obj : datasets)
{
Dataset d = (Dataset) obj;
System.out.println("ID: " + d.getId().getValue() + " Name: " +
d.getName().getValue());
}
}
/**
* Retrieve the Tags that match the namespace value.
*/
private void loadTagsByNS()
throws Exception
{
final boolean caseSensitive = true;
final Filter filter = new Filter();
// Return the first 10 hits or less.
filter.limit = omero.rtypes.rint(10);
filter.offset = omero.rtypes.rint(0);
IQueryPrx proxy = gateway.getQueryService(ctx);
List<IObject> tags = (List<IObject>)
proxy.findAllByString("TagAnnotation", "ns",
Setup.TRAINING_NS, caseSensitive, filter);
System.out.println("\nList Tags:");
for (IObject obj : tags)
{
TagAnnotation t = (TagAnnotation) obj;
System.out.println("ID: " + t.getId().getValue() + " NS: " +
t.getNs().getValue());
}
}
/**
* Retrieve the projects and the orphaned datasets i.e. datasets not in
* a project.
*
* If a project contains datasets, the datasets will automatically be loaded.
*/
private void loadProjectsAndOrphanedDatasets()
throws Exception
{
IContainerPrx proxy = gateway.getPojosService(ctx);
ParametersI param = new ParametersI();
long userId = gateway.getLoggedInUser().getId();
param.exp(omero.rtypes.rlong(userId));
//Load the orphaned datasets.
param.orphan();
//Do not load the images.
param.noLeaves(); //indicate to load the images
//param.noLeaves(); //no images loaded, this is the default value.
List<IObject> results = proxy.loadContainerHierarchy(
Project.class.getName(), new ArrayList<Long>(), param);
//You can directly interact with the IObject or the Pojos object.
//Follow interaction with the Pojos.
Iterator<IObject> i = results.iterator();
ProjectData project;
DatasetData dataset;
IObject o;
long datasetId = -1;
while (i.hasNext()) {
o = i.next();
if (o instanceof Project) {
project = new ProjectData((Project) o);
System.err.println("Project:"+project.getId()+" "+
project.getName());
} else if (o instanceof Dataset) {
dataset = new DatasetData((Dataset) o);
System.err.println("Dataset:"+dataset.getId()+" "+
dataset.getName());
if (datasetId < 0) datasetId = dataset.getId();
//Image not loaded.
}
}
//Now load the image for the first orphaned dataset
if (datasetId < 0) return;
param = new ParametersI();
param.exp(omero.rtypes.rlong(userId));
param.leaves();
results = proxy.loadContainerHierarchy(
Dataset.class.getName(), Arrays.asList(datasetId), param);
i = results.iterator();
Iterator j;
while (i.hasNext()) {
dataset = new DatasetData((Dataset) i.next());
Set images = dataset.getImages();
j = images.iterator();
System.err.println("Size:"+images.size());
while (j.hasNext()) {
ImageData image = (ImageData) j.next();
System.err.println("Image:"+image.getId()+" "+image.getName());
}
}
}
/**
* Connects and invokes the various methods.
*
* @param args The login credentials.
*/
ReadDataAdvanced(String[] args)
{
LoginCredentials cred = new LoginCredentials(args);
gateway = new Gateway(new SimpleLogger());
try {
// First connect.
ExperimenterData user = gateway.connect(cred);
ctx = new SecurityContext(user.getGroupId());
createDatasets();
createTags();
loadDatasetsByName();
loadTagsByNS();
loadProjectsAndOrphanedDatasets();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
gateway.disconnect(); // Be sure to disconnect
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* Runs the script without configuration options.
*
* @param args The login credentials.
*/
public static void main(String[] args)
{
if (args == null || args.length == 0)
args = new String[] { "--omero.host=" + hostName,
"--omero.user=" + userName, "--omero.pass=" + password };
new ReadDataAdvanced(args);
System.exit(0);
}
}