-
Notifications
You must be signed in to change notification settings - Fork 5
/
OMEROMacroExtension.java
1184 lines (1049 loc) · 41.5 KB
/
OMEROMacroExtension.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
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2021 GReD
*
* 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 fr.igred.ij.plugin;
import fr.igred.omero.Client;
import fr.igred.omero.GenericObjectWrapper;
import fr.igred.omero.annotations.TableWrapper;
import fr.igred.omero.annotations.TagAnnotationWrapper;
import fr.igred.omero.exception.AccessException;
import fr.igred.omero.exception.OMEROServerError;
import fr.igred.omero.exception.ServiceException;
import fr.igred.omero.meta.ExperimenterWrapper;
import fr.igred.omero.repository.DatasetWrapper;
import fr.igred.omero.repository.GenericRepositoryObjectWrapper;
import fr.igred.omero.repository.ImageWrapper;
import fr.igred.omero.repository.ProjectWrapper;
import fr.igred.omero.roi.ROIWrapper;
import ij.IJ;
import ij.ImagePlus;
import ij.gui.Overlay;
import ij.gui.Roi;
import ij.macro.ExtensionDescriptor;
import ij.macro.Functions;
import ij.macro.MacroExtension;
import ij.measure.ResultsTable;
import ij.plugin.PlugIn;
import ij.plugin.frame.RoiManager;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;
import static ij.macro.ExtensionDescriptor.newDescriptor;
public class OMEROMacroExtension implements PlugIn, MacroExtension {
private static final String PROJECT = "project";
private static final String DATASET = "dataset";
private static final String IMAGE = "image";
private static final String TAG = "tag";
private static final String INVALID = "Invalid type";
private final ExtensionDescriptor[] extensions = {
newDescriptor("connectToOMERO", this, ARG_STRING, ARG_NUMBER, ARG_STRING, ARG_STRING),
newDescriptor("switchGroup", this, ARG_NUMBER),
newDescriptor("listForUser", this, ARG_STRING),
newDescriptor("list", this, ARG_STRING, ARG_STRING + ARG_OPTIONAL, ARG_NUMBER + ARG_OPTIONAL),
newDescriptor("createDataset", this, ARG_STRING, ARG_STRING, ARG_NUMBER + ARG_OPTIONAL),
newDescriptor("createProject", this, ARG_STRING, ARG_STRING),
newDescriptor("createTag", this, ARG_STRING, ARG_STRING),
newDescriptor("link", this, ARG_STRING, ARG_NUMBER, ARG_STRING, ARG_NUMBER),
newDescriptor("unlink", this, ARG_STRING, ARG_NUMBER, ARG_STRING, ARG_NUMBER),
newDescriptor("addFile", this, ARG_STRING, ARG_NUMBER, ARG_STRING),
newDescriptor("addToTable", this, ARG_STRING,
ARG_STRING + ARG_OPTIONAL, ARG_NUMBER + ARG_OPTIONAL, ARG_STRING + ARG_OPTIONAL),
newDescriptor("saveTable", this, ARG_STRING, ARG_STRING, ARG_NUMBER),
newDescriptor("saveTableAsFile", this, ARG_STRING, ARG_STRING, ARG_STRING + ARG_OPTIONAL),
newDescriptor("clearTable", this, ARG_STRING),
newDescriptor("importImage", this, ARG_NUMBER, ARG_STRING + ARG_OPTIONAL),
newDescriptor("downloadImage", this, ARG_NUMBER, ARG_STRING),
newDescriptor("delete", this, ARG_STRING, ARG_NUMBER),
newDescriptor("getName", this, ARG_STRING, ARG_NUMBER),
newDescriptor("getImage", this, ARG_NUMBER),
newDescriptor("getROIs", this, ARG_NUMBER, ARG_NUMBER + ARG_OPTIONAL, ARG_STRING + ARG_OPTIONAL),
newDescriptor("saveROIs", this, ARG_NUMBER, ARG_STRING + ARG_OPTIONAL),
newDescriptor("sudo", this, ARG_STRING),
newDescriptor("endSudo", this),
newDescriptor("disconnect", this),
};
private final Map<String, TableWrapper> tables = new HashMap<>(1);
private Client client = new Client();
private Client switched = null;
private ExperimenterWrapper user = null;
/**
* Converts a list of GenericObjectWrappers to a comma-delimited list of IDs.
*
* @param list The objects list.
* @param <T> The type of objects.
*
* @return A string containing the corresponding IDs, separated by commas.
*/
private static <T extends GenericObjectWrapper<?>> String listToIDs(Collection<T> list) {
return list.stream()
.mapToLong(T::getId)
.mapToObj(String::valueOf)
.collect(Collectors.joining(","));
}
/**
* Makes sure the requested type is singular and lower case.
*
* @param type The type.
*
* @return The corrected type.
*/
private static String singularType(String type) {
String singular = type.toLowerCase();
int length = singular.length();
if (singular.charAt(length - 1) == 's') {
singular = singular.substring(0, length - 1);
}
return singular;
}
/**
* Converts a Double to a Long.
*
* @param d The Double.
*
* @return The corresponding Long.
*/
private static Long doubleToLong(Double d) {
return d != null ? d.longValue() : null;
}
/**
* Gets the results table with the specified name, or the active table if null.
*
* @param resultsName The name of the ResultsTable.
*
* @return The corresponding ResultsTable.
*/
private static ResultsTable getTable(String resultsName) {
if (resultsName == null) return ResultsTable.getResultsTable();
else return ResultsTable.getResultsTable(resultsName);
}
/**
* Filters the objects list to only keep objects from the set user.
*
* @param list The objects list.
* @param <T> The type of objects.
*
* @return The filtered list.
*/
private <T extends GenericObjectWrapper<?>> List<T> filterUser(List<T> list) {
if (user == null) return list;
else return list.stream().filter(o -> o.getOwner().getId() == user.getId()).collect(Collectors.toList());
}
/**
* Retrieves the object of the specified type with the specified ID.
*
* @param type The type of object.
* @param id The object ID.
*
* @return The object.
*/
private GenericObjectWrapper<?> getObject(String type, long id) {
String singularType = singularType(type);
GenericObjectWrapper<?> object = null;
if (singularType.equals(TAG)) {
try {
object = client.getTag(id);
} catch (OMEROServerError | ServiceException e) {
IJ.error("Could not retrieve tag: " + e.getMessage());
}
} else {
object = getRepositoryObject(type, id);
}
return object;
}
/**
* Retrieves the repository object of the specified type with the specified ID.
*
* @param type The type of object.
* @param id The object ID.
*
* @return The object.
*/
private GenericRepositoryObjectWrapper<?> getRepositoryObject(String type, long id) {
String singularType = singularType(type);
GenericRepositoryObjectWrapper<?> object = null;
try {
switch (singularType) {
case PROJECT:
object = client.getProject(id);
break;
case DATASET:
object = client.getDataset(id);
break;
case IMAGE:
object = client.getImage(id);
break;
default:
IJ.error(INVALID + ": " + type + ".");
}
} catch (ServiceException | AccessException | ExecutionException e) {
IJ.error("Could not retrieve object: " + e.getMessage());
}
return object;
}
/**
* Connects the client to OMERO.
*
* @param host The host name.
* @param port The port.
* @param username The username.
* @param password The password.
*
* @return True if connected, false otherwise.
*/
public boolean connect(String host, int port, String username, String password) {
boolean connected = false;
try {
client.connect(host, port, username, password.toCharArray());
connected = true;
} catch (ServiceException e) {
IJ.error("Could not connect: " + e.getMessage());
}
return connected;
}
/**
* Sets the user whose objects should be listed with the "list" commands.
*
* @param userName The username. Null, empty and "all" removes the filter.
*
* @return The user ID if set, -1 otherwise.
*/
public long setUser(String userName) {
long id = -1L;
if (userName != null && !userName.trim().isEmpty() && !"all".equalsIgnoreCase(userName)) {
if (user != null) id = user.getId();
ExperimenterWrapper newUser = null;
try {
newUser = client.getUser(userName);
} catch (ExecutionException | ServiceException | AccessException | NoSuchElementException e) {
IJ.log("Could not retrieve user: " + userName);
}
if (newUser != null) {
user = newUser;
id = user.getId();
}
} else {
user = null;
}
return id;
}
/**
* Downloads the specified image.
*
* @param imageId The image ID.
* @param path The path where the file(s) should be downloaded.
*
* @return The file path. If multiple files were saved, they are comma-delimited.
*/
public String downloadImage(long imageId, String path) {
List<File> files = new ArrayList<>(0);
try {
files = client.getImage(imageId).download(client, path);
} catch (ServiceException | AccessException | OMEROServerError | ExecutionException | NoSuchElementException e) {
IJ.error("Could not download image: " + e.getMessage());
}
return files.stream().map(File::toString).collect(Collectors.joining(","));
}
/**
* Imports the specified image file to the desired dataset.
*
* @param datasetId The dataset ID.
* @param path The path to the image file.
*
* @return The list of imported IDs, separated by commas.
*/
public String importImage(long datasetId, String path) {
String imagePath = path;
if (path == null) {
ImagePlus imp = IJ.getImage();
imagePath = IJ.getDir("temp") + imp.getTitle() + ".tif";
IJ.save(imp, imagePath);
}
List<Long> imageIds = new ArrayList<>(0);
try {
imageIds = client.getDataset(datasetId).importImage(client, imagePath);
} catch (ServiceException | AccessException | ExecutionException | OMEROServerError | NoSuchElementException e) {
IJ.error("Could not import image: " + e.getMessage());
}
if (path == null) {
try {
Files.deleteIfExists(new File(imagePath).toPath());
} catch (IOException e) {
IJ.error("Could not delete temp image: " + e.getMessage());
}
}
return imageIds.stream().map(String::valueOf).collect(Collectors.joining(","));
}
/**
* Adds a file to an object.
*
* @param type The object type.
* @param id The object ID.
* @param path The path to the file.
*
* @return The uploaded file ID.
*/
public long addFile(String type, long id, String path) {
long fileId = -1;
File file = new File(path);
GenericRepositoryObjectWrapper<?> object = getRepositoryObject(type, id);
if (object != null && file.isFile()) {
try {
fileId = object.addFile(client, file);
} catch (ExecutionException e) {
IJ.error("Could not add file to object: " + e.getMessage());
} catch (InterruptedException e) {
IJ.error(e.getMessage());
Thread.currentThread().interrupt();
}
}
return fileId;
}
/**
* Deletes the file with the specified ID from OMERO.
*
* @param fileId The file ID.
*/
public void deleteFile(long fileId) {
try {
client.deleteFile(fileId);
} catch (ServiceException | AccessException | ExecutionException | OMEROServerError e) {
IJ.error("Could not delete file: " + e.getMessage());
} catch (InterruptedException e) {
IJ.error(e.getMessage());
Thread.currentThread().interrupt();
}
}
/**
* Adds the content of a ResultsTable (for an image) to the table with the specified name.
*
* @param tableName The table name.
* @param results The ResultsTable.
* @param imageId The image ID (can be null).
* @param ijRois The list of ImageJ ROIs.
* @param property The ROI property to group shapes.
*/
public void addToTable(String tableName, ResultsTable results, Long imageId, List<Roi> ijRois, String property) {
TableWrapper table = tables.get(tableName);
if (results == null) {
IJ.error("Results table does not exist.");
} else {
try {
if (table == null) {
table = new TableWrapper(client, results, imageId, ijRois, property);
table.setName(tableName);
tables.put(tableName, table);
} else {
table.addRows(client, results, imageId, ijRois, property);
}
} catch (ExecutionException | ServiceException | AccessException e) {
IJ.error("Could not add results to table: " + e.getMessage());
}
}
}
/**
* Saves the specified table as a file, using the specified delimiter.
*
* @param tableName The table name.
* @param path The path to the file.
* @param delimiter The desired delimiter.
*/
public void saveTableAsFile(String tableName, String path, CharSequence delimiter) {
TableWrapper table = tables.get(tableName);
char sep = delimiter == null || delimiter.length() != 1 ? ',' : delimiter.charAt(0);
try {
table.saveAs(path, sep);
} catch (IOException e) {
IJ.error("Could not create table file: ", e.getMessage());
}
}
/**
* Saves a table to an object on OMERO.
*
* @param tableName The table name.
* @param type The object type.
* @param id The object ID.
*/
public void saveTable(String tableName, String type, long id) {
GenericRepositoryObjectWrapper<?> object = getRepositoryObject(type, id);
if (object != null) {
TableWrapper table = tables.get(tableName);
if (table != null) {
String timestamp = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss").format(ZonedDateTime.now());
String newName;
if (tableName == null || tableName.isEmpty()) newName = timestamp + "_" + table.getName();
else newName = timestamp + "_" + tableName;
table.setName(newName);
try {
object.addTable(client, table);
} catch (ExecutionException | ServiceException | AccessException e) {
IJ.error("Could not save table: " + e.getMessage());
}
} else {
throw new IllegalAccessError("Table is empty!");
}
}
}
/**
* Creates a tag on OMERO.
*
* @param name The tag name.
* @param description The tag description.
*
* @return The tag ID.
*/
public long createTag(String name, String description) {
long id = -1;
try {
TagAnnotationWrapper tag = new TagAnnotationWrapper(client, name, description);
id = tag.getId();
} catch (ServiceException | AccessException | ExecutionException e) {
IJ.error("Could not create tag: " + e.getMessage());
}
return id;
}
/**
* Creates a project on OMERO.
*
* @param name The project name.
* @param description The project description.
*
* @return The project ID.
*/
public long createProject(String name, String description) {
long id = -1;
try {
ProjectWrapper project = new ProjectWrapper(client, name, description);
id = project.getId();
} catch (ServiceException | AccessException | ExecutionException e) {
IJ.error("Could not create project: " + e.getMessage());
}
return id;
}
/**
* Creates a dataset on OMERO.
*
* @param name The dataset name.
* @param description The dataset description.
* @param projectId The ID of the parent project.
*
* @return The dataset ID.
*/
public long createDataset(String name, String description, Long projectId) {
long id = -1;
try {
DatasetWrapper dataset;
if (projectId != null) {
dataset = client.getProject(projectId).addDataset(client, name, description);
} else {
dataset = new DatasetWrapper(name, description);
dataset.saveAndUpdate(client);
}
id = dataset.getId();
} catch (ServiceException | AccessException | ExecutionException e) {
IJ.error("Could not create dataset: " + e.getMessage());
}
return id;
}
/**
* Deletes an object on OMERO.
*
* @param type The object type.
* @param id The object ID.
*/
public void delete(String type, long id) {
GenericObjectWrapper<?> object = getObject(type, id);
try {
if (object != null) client.delete(object);
} catch (ServiceException | AccessException | ExecutionException | OMEROServerError e) {
IJ.error("Could not delete " + type + ": " + e.getMessage());
} catch (InterruptedException e) {
IJ.error(e.getMessage());
Thread.currentThread().interrupt();
}
}
/**
* Lists the objects of the specified type.
*
* @param type The objects type.
*
* @return The comma-delimited list of object IDs.
*/
public String list(String type) {
String singularType = singularType(type);
String results = "";
try {
switch (singularType) {
case PROJECT:
List<ProjectWrapper> projects = client.getProjects();
results = listToIDs(filterUser(projects));
break;
case DATASET:
List<DatasetWrapper> datasets = client.getDatasets();
results = listToIDs(filterUser(datasets));
break;
case IMAGE:
List<ImageWrapper> images = client.getImages();
results = listToIDs(filterUser(images));
break;
case TAG:
List<TagAnnotationWrapper> tags = client.getTags();
results = listToIDs(filterUser(tags));
break;
default:
IJ.error(INVALID + ": " + type + ". Possible values are: projects, datasets, images or tags.");
}
} catch (ServiceException | AccessException | OMEROServerError | ExecutionException e) {
IJ.error("Could not retrieve " + type + ": " + e.getMessage());
}
return results;
}
/**
* Lists the objects of the specified type with the specified name.
*
* @param type The objects type.
* @param name The objects name.
*
* @return The comma-delimited list of object IDs.
*/
public String list(String type, String name) {
String singularType = singularType(type);
String results = "";
try {
switch (singularType) {
case PROJECT:
List<ProjectWrapper> projects = client.getProjects(name);
results = listToIDs(filterUser(projects));
break;
case DATASET:
List<DatasetWrapper> datasets = client.getDatasets(name);
results = listToIDs(filterUser(datasets));
break;
case IMAGE:
List<ImageWrapper> images = client.getImages(name);
results = listToIDs(filterUser(images));
break;
case TAG:
List<TagAnnotationWrapper> tags = client.getTags(name);
results = listToIDs(filterUser(tags));
break;
default:
IJ.error(INVALID + ": " + type + ". Possible values are: projects, datasets, images or tags.");
}
} catch (ServiceException | AccessException | OMEROServerError | ExecutionException e) {
IJ.error(String.format("Could not retrieve %s with name \"%s\": %s", type, name, e.getMessage()));
}
return results;
}
/**
* Lists the objects of the specified type inside the specified container.
*
* @param type The object type.
* @param parent The type of container.
*
* @return The comma-delimited list of object IDs.
*/
public String list(String type, String parent, long id) {
String singularType = singularType(type);
String singularParent = singularType(parent);
String results = "";
try {
switch (singularParent) {
case PROJECT:
ProjectWrapper project = client.getProject(id);
switch (singularType) {
case DATASET:
List<DatasetWrapper> datasets = project.getDatasets();
results = listToIDs(filterUser(datasets));
break;
case IMAGE:
List<ImageWrapper> images = project.getImages(client);
results = listToIDs(filterUser(images));
break;
case TAG:
List<TagAnnotationWrapper> tags = project.getTags(client);
results = listToIDs(filterUser(tags));
break;
default:
IJ.error(INVALID + ": " + type + ". Possible values are: datasets, images or tags.");
}
break;
case DATASET:
DatasetWrapper dataset = client.getDataset(id);
switch (singularType) {
case IMAGE:
List<ImageWrapper> images = dataset.getImages(client);
results = listToIDs(filterUser(images));
break;
case TAG:
List<TagAnnotationWrapper> tags = dataset.getTags(client);
results = listToIDs(filterUser(tags));
break;
default:
IJ.error(INVALID + ": " + type + ". Possible values are: images or tags.");
}
break;
case IMAGE:
if (singularType.equals(TAG)) {
results = listToIDs(filterUser(client.getImage(id).getTags(client)));
} else {
IJ.error("Invalid type: " + type + ". Only possible value is: tags.");
}
break;
case TAG:
TagAnnotationWrapper tag = client.getTag(id);
switch (singularType) {
case PROJECT:
List<ProjectWrapper> projects = tag.getProjects(client);
results = listToIDs(filterUser(projects));
break;
case DATASET:
List<DatasetWrapper> datasets = tag.getDatasets(client);
results = listToIDs(filterUser(datasets));
break;
case IMAGE:
List<ImageWrapper> images = tag.getImages(client);
results = listToIDs(filterUser(images));
break;
default:
IJ.error(INVALID + ": " + type + ". Possible values are: projects, datasets or images.");
}
break;
default:
IJ.error(INVALID + ": " + parent + ". Possible values are: project, dataset, image or tag.");
}
} catch (ServiceException | AccessException | ExecutionException | OMEROServerError e) {
IJ.error("Could not retrieve " + type + " in " + parent + ": " + e.getMessage());
}
return results;
}
/**
* Switches to another user.
*
* @param username The other user's name.
*/
public void sudo(String username) {
switched = client;
try {
client = switched.sudoGetUser(username);
} catch (ServiceException | AccessException | ExecutionException | NoSuchElementException e) {
IJ.error("Could not switch user: " + e.getMessage());
switched = null;
}
}
/**
* Stops acting as another user.
*/
public void endSudo() {
if (switched != null) {
client = switched;
switched = null;
} else {
IJ.error("No sudo has been used before.");
}
}
/**
* Links two objects.
*
* @param type1 The first object type.
* @param id1 The first object ID.
* @param type2 The second object type.
* @param id2 The second object ID.
*/
public void link(String type1, long id1, String type2, long id2) {
String t1 = singularType(type1);
String t2 = singularType(type2);
Map<String, Long> map = new HashMap<>(2);
map.put(t1, id1);
map.put(t2, id2);
Long projectId = map.get(PROJECT);
Long datasetId = map.get(DATASET);
Long imageId = map.get(IMAGE);
Long tagId = map.get(TAG);
try {
// Link tag to repository object
if (t1.equals(TAG) ^ t2.equals(TAG)) {
String obj = t1.equals(TAG) ? t2 : t1;
GenericRepositoryObjectWrapper<?> object = getRepositoryObject(obj, map.get(obj));
if (object != null) object.addTag(client, tagId);
} else if (datasetId == null || (projectId == null && imageId == null)) {
IJ.error(String.format("Cannot link %s and %s", type1, type2));
} else { // Or link dataset to image or project
DatasetWrapper dataset = client.getDataset(datasetId);
if (projectId != null) {
client.getProject(projectId).addDataset(client, dataset);
} else {
dataset.addImage(client, client.getImage(imageId));
}
}
} catch (ServiceException | AccessException | ExecutionException e) {
IJ.error(String.format("Cannot link %s and %s: %s", type1, type2, e.getMessage()));
}
}
/**
* Unlinks two objects.
*
* @param type1 The first object type.
* @param id1 The first object ID.
* @param type2 The second object type.
* @param id2 The second object ID.
*/
public void unlink(String type1, long id1, String type2, long id2) {
String t1 = singularType(type1);
String t2 = singularType(type2);
Map<String, Long> map = new HashMap<>(2);
map.put(t1, id1);
map.put(t2, id2);
Long projectId = map.get(PROJECT);
Long datasetId = map.get(DATASET);
Long imageId = map.get(IMAGE);
Long tagId = map.get(TAG);
try {
// Unlink tag from repository object
if (t1.equals(TAG) ^ t2.equals(TAG)) {
String obj = t1.equals(TAG) ? t2 : t1;
GenericRepositoryObjectWrapper<?> object = getRepositoryObject(obj, map.get(obj));
if (object != null) object.unlink(client, client.getTag(tagId));
} else if (datasetId == null || (projectId == null && imageId == null)) {
IJ.error(String.format("Cannot unlink %s and %s", type1, type2));
} else { // Or unlink dataset from image or project
DatasetWrapper dataset = client.getDataset(datasetId);
if (projectId != null) {
client.getProject(projectId).removeDataset(client, dataset);
} else {
dataset.removeImage(client, client.getImage(imageId));
}
}
} catch (ServiceException | AccessException | ExecutionException | OMEROServerError e) {
IJ.error(String.format("Cannot unlink %s and %s: %s", type1, type2, e.getMessage()));
} catch (InterruptedException e) {
IJ.error(String.format("Cannot unlink %s and %s: %s", type1, type2, e.getMessage()));
Thread.currentThread().interrupt();
}
}
/**
* Retrieves the name of an object.
*
* @param type The object type.
* @param id The object ID.
*
* @return The object name.
*/
public String getName(String type, long id) {
String name = null;
GenericObjectWrapper<?> object = getObject(type, id);
if (object instanceof GenericRepositoryObjectWrapper<?>) {
name = ((GenericRepositoryObjectWrapper<?>) object).getName();
} else if (object instanceof TagAnnotationWrapper) {
name = ((TagAnnotationWrapper) object).getName();
}
return name;
}
/**
* Opens an image.
*
* @param id The image ID.
*
* @return The image, as an {@link ImagePlus}.
*/
public ImagePlus getImage(long id) {
ImagePlus imp = null;
try {
ImageWrapper image = client.getImage(id);
imp = image.toImagePlus(client);
} catch (ServiceException | AccessException | ExecutionException | NoSuchElementException e) {
IJ.error("Could not retrieve image: " + e.getMessage());
}
return imp;
}
/**
* Retrieves the image ROIs and puts the in the ROI Manager, or the image overlay.
*
* @param imp The image in ImageJ.
* @param id The image ID on OMERO.
* @param toOverlay Whether to put ROIs on the overlay.
* @param property The ROI property to group shapes.
*
* @return The number of (2D) ROIs loaded in ImageJ.
*/
public int getROIs(ImagePlus imp, long id, boolean toOverlay, String property) {
List<ROIWrapper> rois = new ArrayList<>(0);
try {
ImageWrapper image = client.getImage(id);
rois = image.getROIs(client);
} catch (ServiceException | AccessException | ExecutionException e) {
IJ.error("Could not retrieve ROIs: " + e.getMessage());
}
List<Roi> ijRois = ROIWrapper.toImageJ(rois, property);
if (toOverlay) {
Overlay overlay = imp.getOverlay();
if (overlay == null) {
overlay = new Overlay();
imp.setOverlay(overlay);
}
for (Roi roi : ijRois) {
roi.setImage(imp);
overlay.add(roi);
}
} else {
RoiManager rm = RoiManager.getInstance();
if (rm == null) rm = RoiManager.getRoiManager();
for (Roi roi : ijRois) {
roi.setImage(imp);
rm.addRoi(roi);
}
}
return ijRois.size();
}
/**
* Saves the ROIs from the ROI Manager and the image overlay to the image on OMERO.
*
* @param imp The image in ImageJ.
* @param id The image ID on OMERO.
* @param property The ROI property to group shapes.
*
* @return The number of (4D) ROIs saved on OMERO.
*/
public int saveROIs(ImagePlus imp, long id, String property) {
int result = 0;
try {
ImageWrapper image = client.getImage(id);
Overlay overlay = imp.getOverlay();
if (overlay != null) {
List<Roi> ijRois = Arrays.asList(overlay.toArray());
List<ROIWrapper> rois = ROIWrapper.fromImageJ(ijRois, property);
rois.forEach(roi -> roi.setImage(image));
for (ROIWrapper roi : rois) {
image.saveROI(client, roi);
}
result += rois.size();
overlay.clear();
List<Roi> newRois = ROIWrapper.toImageJ(rois, property);
for (Roi roi : newRois) {
roi.setImage(imp);
overlay.add(roi);
}
}
RoiManager rm = RoiManager.getInstance();
if (rm != null) {
List<Roi> ijRois = Arrays.asList(rm.getRoisAsArray());
List<ROIWrapper> rois = ROIWrapper.fromImageJ(ijRois, property);
rois.forEach(roi -> roi.setImage(image));
for (ROIWrapper roi : rois) {
image.saveROI(client, roi);
}
result += rois.size();
rm.reset();
List<Roi> newRois = ROIWrapper.toImageJ(rois, property);
for (Roi roi : newRois) {
roi.setImage(imp);
rm.addRoi(roi);
}
}
} catch (ServiceException | AccessException | ExecutionException e) {
IJ.error("Could not save ROIs to image: " + e.getMessage());
}
return result;
}
/**
* Disconnects from OMERO.
*/
public void disconnect() {
if (switched != null) endSudo();
client.disconnect();
}
@Override
public void run(String arg) {
if (!IJ.macroRunning()) {
IJ.error("Cannot install extensions from outside a macro!");
return;
}
Functions.registerExtensions(this);
}
@Override
public ExtensionDescriptor[] getExtensionFunctions() {
return extensions;
}
@Override
public String handleExtension(String name, Object[] args) {
long id;
long id1;
long id2;
String type;
String type1;