Skip to content

Commit

Permalink
Add methods to create a screen and refactor code to import images to …
Browse files Browse the repository at this point in the history
…screens
  • Loading branch information
ppouchin committed Jun 27, 2022
1 parent 6664dda commit 49283c3
Show file tree
Hide file tree
Showing 4 changed files with 305 additions and 79 deletions.
82 changes: 3 additions & 79 deletions src/main/java/fr/igred/omero/repository/DatasetWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,7 @@
import fr.igred.omero.exception.OMEROServerError;
import fr.igred.omero.exception.ServiceException;
import fr.igred.omero.roi.ROIWrapper;
import loci.formats.in.DefaultMetadataOptions;
import loci.formats.in.MetadataLevel;
import ome.formats.OMEROMetadataStoreClient;
import ome.formats.importer.ImportCandidates;
import ome.formats.importer.ImportConfig;
import ome.formats.importer.ImportContainer;
import ome.formats.importer.ImportLibrary;
import ome.formats.importer.OMEROWrapper;
import ome.formats.importer.cli.ErrorHandler;
import ome.formats.importer.cli.LoggingImportMonitor;
import omero.RLong;
import omero.ServerError;
import omero.gateway.exception.DSAccessException;
import omero.gateway.exception.DSOutOfServiceException;
import omero.gateway.model.DatasetData;
Expand All @@ -45,7 +34,6 @@
import omero.model.DatasetImageLink;
import omero.model.DatasetImageLinkI;
import omero.model.IObject;
import omero.model.Pixels;

import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -55,8 +43,6 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

import static fr.igred.omero.exception.ExceptionHandler.handleServiceOrAccess;
Expand Down Expand Up @@ -445,31 +431,7 @@ public void removeImage(Client client, ImageWrapper image)
*/
public boolean importImages(Client client, String... paths)
throws ServiceException, OMEROServerError, AccessException, IOException, ExecutionException {
boolean success;

ImportConfig config = new ImportConfig();
config.target.set("Dataset:" + data.getId());
config.username.set(client.getUser().getUserName());
config.email.set(client.getUser().getEmail());

OMEROMetadataStoreClient store = client.getImportStore();
try (OMEROWrapper reader = new OMEROWrapper(config)) {
store.logVersionInfo(config.getIniVersionNumber());
reader.setMetadataOptions(new DefaultMetadataOptions(MetadataLevel.ALL));

ImportLibrary library = new ImportLibrary(store, reader);
library.addObserver(new LoggingImportMonitor());

ErrorHandler handler = new ErrorHandler(config);

ImportCandidates candidates = new ImportCandidates(reader, paths, handler);
success = library.importCandidates(config, candidates);
} catch (ServerError se) {
throw new OMEROServerError(se);
} finally {
store.logout();
}

boolean success = importImages(client, data, paths);
refresh(client);
return success;
}
Expand All @@ -490,47 +452,9 @@ public boolean importImages(Client client, String... paths)
*/
public List<Long> importImage(Client client, String path)
throws ServiceException, AccessException, OMEROServerError, ExecutionException {
ImportConfig config = new ImportConfig();
config.target.set("Dataset:" + data.getId());
config.username.set(client.getUser().getUserName());
config.email.set(client.getUser().getEmail());

Collection<Pixels> pixels = new ArrayList<>(1);

OMEROMetadataStoreClient store = client.getImportStore();
try (OMEROWrapper reader = new OMEROWrapper(config)) {
store.logVersionInfo(config.getIniVersionNumber());
reader.setMetadataOptions(new DefaultMetadataOptions(MetadataLevel.ALL));

ImportLibrary library = new ImportLibrary(store, reader);
library.addObserver(new LoggingImportMonitor());

ErrorHandler handler = new ErrorHandler(config);

ImportCandidates candidates = new ImportCandidates(reader, new String[]{path}, handler);

ExecutorService uploadThreadPool = Executors.newFixedThreadPool(config.parallelUpload.get());

List<ImportContainer> containers = candidates.getContainers();
if (containers != null) {
for (int i = 0; i < containers.size(); i++) {
ImportContainer container = containers.get(i);
container.setTarget(data.asDataset());
List<Pixels> imported = library.importImage(container, uploadThreadPool, i);
pixels.addAll(imported);
}
}
uploadThreadPool.shutdown();
} catch (Throwable e) {
throw new OMEROServerError(e);
} finally {
store.logout();
}
List<Long> ids = importImage(client, data, path);
refresh(client);

List<Long> ids = new ArrayList<>(pixels.size());
pixels.forEach(pix -> ids.add(pix.getImage().getId().getValue()));
return ids.stream().distinct().collect(Collectors.toList());
return ids;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@


import fr.igred.omero.Client;
import fr.igred.omero.GatewayWrapper;
import fr.igred.omero.GenericObjectWrapper;
import fr.igred.omero.annotations.FileAnnotationWrapper;
import fr.igred.omero.annotations.GenericAnnotationWrapper;
Expand All @@ -26,6 +27,17 @@
import fr.igred.omero.exception.AccessException;
import fr.igred.omero.exception.OMEROServerError;
import fr.igred.omero.exception.ServiceException;
import loci.formats.in.DefaultMetadataOptions;
import loci.formats.in.MetadataLevel;
import ome.formats.OMEROMetadataStoreClient;
import ome.formats.importer.ImportCandidates;
import ome.formats.importer.ImportConfig;
import ome.formats.importer.ImportContainer;
import ome.formats.importer.ImportLibrary;
import ome.formats.importer.OMEROWrapper;
import ome.formats.importer.cli.ErrorHandler;
import ome.formats.importer.cli.LoggingImportMonitor;
import omero.ServerError;
import omero.constants.metadata.NSCLIENTMAPANNOTATION;
import omero.gateway.exception.DSAccessException;
import omero.gateway.exception.DSOutOfServiceException;
Expand All @@ -35,11 +47,14 @@
import omero.gateway.model.MapAnnotationData;
import omero.gateway.model.TableData;
import omero.gateway.model.TagAnnotationData;
import omero.gateway.util.PojoMapper;
import omero.model.IObject;
import omero.model.NamedValue;
import omero.model.Pixels;
import omero.model.TagAnnotationI;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -49,6 +64,8 @@
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

import static fr.igred.omero.exception.ExceptionHandler.handleServiceOrAccess;
Expand Down Expand Up @@ -680,6 +697,109 @@ public void copyAnnotationLinks(Client client, GenericRepositoryObjectWrapper<?>
}


/**
* Imports all images candidates in the paths to the target in OMERO.
*
* @param client The client handling the connection.
* @param target The import target.
* @param paths Paths to the image files on the computer.
*
* @return If the import did not exit because of an error.
*
* @throws ServiceException Cannot connect to OMERO.
* @throws OMEROServerError Server error.
* @throws IOException Cannot read file.
*/
protected static boolean importImages(GatewayWrapper client, DataObject target, String... paths)
throws ServiceException, OMEROServerError, IOException {
boolean success;

ImportConfig config = new ImportConfig();
String type = PojoMapper.getGraphType(target.getClass());
config.target.set(type + ":" + target.getId());
config.username.set(client.getUser().getUserName());
config.email.set(client.getUser().getEmail());

OMEROMetadataStoreClient store = client.getImportStore();
try (OMEROWrapper reader = new OMEROWrapper(config)) {
store.logVersionInfo(config.getIniVersionNumber());
reader.setMetadataOptions(new DefaultMetadataOptions(MetadataLevel.ALL));

ImportLibrary library = new ImportLibrary(store, reader);
library.addObserver(new LoggingImportMonitor());

ErrorHandler handler = new ErrorHandler(config);

ImportCandidates candidates = new ImportCandidates(reader, paths, handler);
success = library.importCandidates(config, candidates);
} catch (ServerError se) {
throw new OMEROServerError(se);
} finally {
store.logout();
}

return success;
}


/**
* Imports one image file to the target in OMERO.
*
* @param client The client handling the connection.
* @param target The import target.
* @param path Path to the image file on the computer.
*
* @return The list of IDs of the newly imported images.
*
* @throws ServiceException Cannot connect to OMERO.
* @throws OMEROServerError Server error.
*/
protected static List<Long> importImage(GatewayWrapper client, DataObject target, String path)
throws ServiceException, OMEROServerError {
ImportConfig config = new ImportConfig();
String type = PojoMapper.getGraphType(target.getClass());
config.target.set(type + ":" + target.getId());
config.username.set(client.getUser().getUserName());
config.email.set(client.getUser().getEmail());

Collection<Pixels> pixels = new ArrayList<>(1);

OMEROMetadataStoreClient store = client.getImportStore();
try (OMEROWrapper reader = new OMEROWrapper(config)) {
store.logVersionInfo(config.getIniVersionNumber());
reader.setMetadataOptions(new DefaultMetadataOptions(MetadataLevel.ALL));

ImportLibrary library = new ImportLibrary(store, reader);
library.addObserver(new LoggingImportMonitor());

ErrorHandler handler = new ErrorHandler(config);

ImportCandidates candidates = new ImportCandidates(reader, new String[]{path}, handler);

ExecutorService uploadThreadPool = Executors.newFixedThreadPool(config.parallelUpload.get());

List<ImportContainer> containers = candidates.getContainers();
if (containers != null) {
for (int i = 0; i < containers.size(); i++) {
ImportContainer container = containers.get(i);
container.setTarget(target.asIObject());
List<Pixels> imported = library.importImage(container, uploadThreadPool, i);
pixels.addAll(imported);
}
}
uploadThreadPool.shutdown();
} catch (Throwable e) {
throw new OMEROServerError(e);
} finally {
store.logout();
}

List<Long> ids = new ArrayList<>(pixels.size());
pixels.forEach(pix -> ids.add(pix.getImage().getId().getValue()));
return ids.stream().distinct().collect(Collectors.toList());
}


/**
* Policy to specify how to handle objects when they are replaced.
*/
Expand Down
95 changes: 95 additions & 0 deletions src/main/java/fr/igred/omero/repository/ScreenWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,48 @@
package fr.igred.omero.repository;


import fr.igred.omero.Client;
import fr.igred.omero.GatewayWrapper;
import fr.igred.omero.exception.AccessException;
import fr.igred.omero.exception.OMEROServerError;
import fr.igred.omero.exception.ServiceException;
import omero.gateway.exception.DSAccessException;
import omero.gateway.exception.DSOutOfServiceException;
import omero.gateway.model.ScreenData;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutionException;

import static fr.igred.omero.exception.ExceptionHandler.handleServiceOrAccess;


public class ScreenWrapper extends GenericRepositoryObjectWrapper<ScreenData> {

public static final String ANNOTATION_LINK = "ScreenAnnotationLink";


/**
* Constructor of the ProjectWrapper class. Creates a new project and saves it to OMERO.
*
* @param client The client handling the connection.
* @param name Project name.
* @param description Project description.
*
* @throws ServiceException Cannot connect to OMERO.
* @throws AccessException Cannot access data.
* @throws ExecutionException A Facility can't be retrieved or instantiated.
*/
public ScreenWrapper(Client client, String name, String description)
throws ServiceException, AccessException, ExecutionException {
super(new ScreenData());
data.setName(name);
data.setDescription(description);
super.saveAndUpdate(client);
}


/**
* Constructor of the class ScreenWrapper.
*
Expand Down Expand Up @@ -203,4 +235,67 @@ public void setReagentSetIdentifier(String value) {
data.setReagentSetIdentifier(value);
}


/**
* Refreshes the wrapped screen.
*
* @param client The client handling the connection.
*
* @throws ServiceException Cannot connect to OMERO.
* @throws AccessException Cannot access data.
* @throws ExecutionException A Facility can't be retrieved or instantiated.
*/
public void refresh(GatewayWrapper client) throws ServiceException, AccessException, ExecutionException {
try {
data = client.getBrowseFacility()
.getScreens(client.getCtx(), Collections.singletonList(this.getId()))
.iterator().next();
} catch (DSOutOfServiceException | DSAccessException e) {
handleServiceOrAccess(e, "Cannot refresh " + this);
}
}


/**
* Imports all images candidates in the paths to the screen in OMERO.
*
* @param client The client handling the connection.
* @param paths Paths to the image files on the computer.
*
* @return If the import did not exit because of an error.
*
* @throws ServiceException Cannot connect to OMERO.
* @throws AccessException Cannot access data.
* @throws OMEROServerError Server error.
* @throws IOException Cannot read file.
* @throws ExecutionException A Facility can't be retrieved or instantiated.
*/
public boolean importImages(GatewayWrapper client, String... paths)
throws ServiceException, OMEROServerError, AccessException, IOException, ExecutionException {
boolean success = importImages(client, data, paths);
refresh(client);
return success;
}


/**
* Imports one image file to the screen in OMERO.
*
* @param client The client handling the connection.
* @param path Path to the image file on the computer.
*
* @return The list of IDs of the newly imported images.
*
* @throws ServiceException Cannot connect to OMERO.
* @throws AccessException Cannot access data.
* @throws OMEROServerError Server error.
* @throws ExecutionException A Facility can't be retrieved or instantiated.
*/
public List<Long> importImage(GatewayWrapper client, String path)
throws ServiceException, AccessException, OMEROServerError, ExecutionException {
List<Long> ids = importImage(client, data, path);
refresh(client);
return ids;
}

}
Loading

0 comments on commit 49283c3

Please sign in to comment.