/
getObjectAnnotations.m
145 lines (135 loc) · 5.9 KB
/
getObjectAnnotations.m
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
function annotations = getObjectAnnotations(session, annotationType, parentType, ids, varargin)
% GETOBJECTANNOTATIONS Retrieve annotations of a given type associated with an object
%
% anns = getObjectAnnotations(session, annotationType, parentType, ids)
% returns all annotations of type annotationType linked to the object of
% type parentType and identifiers ids owned by the session user.
%
% anns = getObjectAnnotations(session, annotationType, parentType,
% parents) returns all annotations of type annotationType linked to the
% input parent objects of type parentType owned by the session user.
%
% Optional parameters:
%
% anns = getObjectAnnotations(session, annotationType, parentType, ids,
% 'include', include) only returns annotations with the input namespace.
%
% anns = getObjectAnnotations(session, annotationType, parentType, ids,
% 'exclude', exclude) excludes annotations with the input namespace.
%
% anns = getObjectAnnotations(session, annotationType, parentType, ids,
% 'owner', ownerid) returns annotations owned by the user with
% identifier ownerid. Use -1 to return the annotations owned by all
% users.
% Default: identifier of the session owner.
%
% anns = getObjectAnnotations(session, annotationType, parentType, ids,
% 'flatten', tf) sets the flatting option for the ouptut. If true, all
% found annotations are returned as a single array of annotations. If
% false, they are returned as a cell array where the i-th element is an
% array of all the annotations linked to the i-th object.
% Default: true.
%
% anns = getObjectAnnotations(session, annotationType, parentType, ids,
% 'group', groupid) returns annotations belonging to the input groupid.
% Use -1 to return the annotations belonging to all groups.
% Default: current session groupid.
%
% All additional options input as parameter/value pairs are passed to
% the OMERO service.
%
% Examples:
%
% anns = getObjectAnnotations(session, annotationType, parentType,
% ids)
% anns = getObjectAnnotations(session, annotationType, parentType,
% parents)
% anns = getObjectAnnotations(session, annotationType, parentType,
% ids, 'include', include)
% anns = getObjectAnnotations(session, annotationType, parentType,
% ids, 'exclude', exclude)
% anns = getObjectAnnotations(session, annotationType, parentType,
% ids, 'owner', -1)
% anns = getObjectAnnotations(session, annotationType, parentType,
% ids, 'flatten', true, 'uid', -1, 'omero.group', -1)
%
% See also: GETIMAGEFILEANNOTATIONS, GETIMAGETAGANNOTATIONS,
% GETIMAGECOMMENTANNOTATIONS
% Copyright (C) 2013-2014 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.
% Input check
annotations = getAnnotationTypes();
objects = getObjectTypes();
defaultownerid = session.getAdminService().getEventContext().userId;
ip = inputParser;
ip.addRequired('annotationType', @(x) ischar(x) && ismember(x, {annotations.name}));
ip.addRequired('parentType', @(x) ischar(x) && ismember(x, {objects.name}));
ip.addRequired('ids', @(x) isvector(x) || isempty(x));
ip.addParamValue('include', [], @(x) iscellstr(x) || ischar(x));
ip.addParamValue('exclude', [], @(x) iscellstr(x) || ischar(x));
ip.addParamValue('flatten', true, @(x) isscalar(x) && (x || ~x));
ip.addParamValue('owner', defaultownerid, @isscalar);
ip.addParamValue('group', [], @(x) isscalar(x) && isnumeric(x));
ip.KeepUnmatched = true;
ip.parse(annotationType, parentType, ids, varargin{:});
% Load existing file annotations
metadataService = session.getMetadataService();
% Convert input into java.util.ArrayList;
if ~isnumeric(ids),
ids = arrayfun(@(x) x.getId().getValue(), ids);
end
context = structToHashMap(ip.Unmatched);
if ~isempty(ip.Results.group)
context.put(...
'omero.group', java.lang.String(num2str(ip.Results.group)));
elseif ~context.containsKey('omero.group') && ~isempty(ids)
context.put('omero.group', '-1');
end
ids = toJavaList(ids, 'java.lang.Long');
include = toJavaList(ip.Results.include, 'java.lang.String');
exclude = toJavaList(ip.Results.exclude, 'java.lang.String');
parameters = omero.sys.ParametersI;
parameters.exp(rlong(ip.Results.owner));
% Read annotations
object = objects(strcmp(parentType, {objects.name}));
annotation = annotations(strcmp(annotationType, {annotations.name}));
annotations = metadataService.loadSpecifiedAnnotationsLinkedTo(...
annotation.class, include, exclude, object.class, ids, parameters, ...
context);
% Aggregate all annotations into a java.util.ArrayList
if ip.Results.flatten
annotationList = java.util.ArrayList();
i = annotations.values.iterator;
while (i.hasNext())
j = i.next().iterator();
while (j.hasNext())
annotationList.add(j.next());
end
end
% Convert java.util.ArrayList into a Matlab array
annotations = toMatlabList(annotationList);
else
annotationList = cell(ids.size(), 1);
for n = 1:ids.size()
annotationListPerId = annotations.get( ...
java.lang.Long(ids.get(n - 1)));
if ~isempty(annotationListPerId)
annotationList{n} = toMatlabList(annotationListPerId);
end
end
annotations = annotationList;
end