Guest User

Untitled

a guest
Jan 14th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.util.Arrays;
  5. import java.util.List;
  6.  
  7. import Glacier2.CannotCreateSessionException;
  8. import Glacier2.PermissionDeniedException;
  9.  
  10. import omero.ServerError;
  11. import omero.client;
  12. import omero.api.ExporterPrx;
  13. import omero.api.IContainerPrx;
  14. import omero.api.ServiceFactoryPrx;
  15. import omero.model.Dataset;
  16. import omero.model.IObject;
  17. import omero.model.Image;
  18. import omero.sys.ParametersI;
  19.  
  20.  
  21. public class OmeroDatasetExport {
  22.  
  23. private final omero.client client;
  24. private final ServiceFactoryPrx sf;
  25. private final IContainerPrx containerService;
  26. private static final int BUFFER_SIZE = 1024 * 1024; // 1MB
  27.  
  28. public OmeroDatasetExport(String host, int port, String username, String password)
  29. throws CannotCreateSessionException, PermissionDeniedException, ServerError {
  30. client = new client(host, port);
  31. sf = client.createSession(username, password);
  32. containerService = sf.getContainerService();
  33. }
  34.  
  35. public void exportImages(long datasetId) throws ServerError, IOException {
  36. ParametersI param = new ParametersI();
  37. param.leaves();
  38. List<omero.model.IObject> results =
  39. containerService.loadContainerHierarchy(
  40. Dataset.class.getName(), Arrays.asList(datasetId), param);
  41. if (results.size() == 0){
  42. throw new RuntimeException("No dataset with ID: " + datasetId);
  43. }
  44.  
  45. for (IObject result : results) {
  46. Dataset dataset = (Dataset) result;
  47. for (Image image : dataset.linkedImageList()) {
  48. long imageId = image.getId().getValue();
  49. ExporterPrx exporter = sf.createExporter();
  50. try {
  51. exporter.addImage(imageId);
  52. exporter.generateTiff();
  53.  
  54. File file = new File(System.getProperty("user.dir"),
  55. imageId + ".ome.tif");
  56. FileOutputStream fos = new FileOutputStream(file);
  57. long offset = 0;
  58. int readLength = BUFFER_SIZE;
  59. while (readLength == BUFFER_SIZE) {
  60. byte[] buf = exporter.read(offset, BUFFER_SIZE);
  61. fos.write(buf);
  62. readLength = buf.length;
  63. offset += readLength;
  64. }
  65. System.out.println("Exported Image:" + imageId +
  66. " to " + file.getAbsolutePath());
  67. } finally {
  68. exporter.close();
  69. }
  70. }
  71. }
  72. }
  73.  
  74. public void cleanup() throws ServerError {
  75. client.closeSession();
  76. }
  77.  
  78. public static void main(String[] args) throws Exception {
  79. if (args.length != 5) {
  80. System.err.println("Usage: OmeroDatasetExport <host> <port> " +
  81. "<username> <password> <dataset_id>");
  82. }
  83. String host = args[0];
  84. int port = Integer.parseInt(args[1]);
  85. String username = args[2];
  86. String password = args[3];
  87. long datasetId = Long.parseLong(args[4]);
  88. OmeroDatasetExport test = null;
  89. try {
  90. test = new OmeroDatasetExport(host, port, username, password);
  91. test.exportImages(datasetId);
  92. } finally {
  93. if (test != null) {
  94. test.cleanup();
  95. }
  96. }
  97. }
  98. }
Add Comment
Please, Sign In to add comment