Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>life.qbic</groupId>
<artifactId>parent-pom</artifactId>
<version>2.2.5</version>
<version>2.4.0-SNAPSHOT</version>
</parent>

<groupId>life.qbic</groupId>
Expand Down Expand Up @@ -80,6 +80,10 @@
<groupId>ome</groupId>
<artifactId>formats-gpl</artifactId>
</dependency>
<dependency>
<groupId>life.qbic</groupId>
<artifactId>data-model-lib</artifactId>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
Expand Down
85 changes: 85 additions & 0 deletions src/main/java/life/qbic/omero/BasicOMEROClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;
import javax.imageio.ImageIO;
import life.qbic.datamodel.dtos.imaging.Instrument;
import life.qbic.datamodel.dtos.imaging.Location;
import life.qbic.datamodel.dtos.imaging.parameters.ImagingHardware;
import life.qbic.datamodel.people.Address;
import ome.model.annotations.MapAnnotation;
import omero.ServerError;
import omero.api.ExporterPrx;
import omero.api.RawFileStorePrx;
Expand Down Expand Up @@ -966,6 +971,86 @@ public ByteArrayInputStream getThumbnail(long datasetId, long imageId) {

}

/**
* Associates instrument metadata with an existing image in the OMERO server.
* @param instrument containing information to be associated with the image
* @param imageId ID of the image that will be annotated
*/
public void associateInstrumentWithImage(Instrument instrument, Long imageId) {
if (!isConnected()) {
this.connect();
}
// 1. Check if an image with the id is in OMERO. Throw a runtime exception if not
ImageData imageData;
try {
BrowseFacility browseFacility = gateway.getFacility(BrowseFacility.class);
imageData = browseFacility.getImage(securityContext, imageId);
} catch (DSOutOfServiceException dsOutOfServiceException) {
throw new RuntimeException("Error while accessing omero service: broken connection, expired session or not logged in", dsOutOfServiceException);
} catch (ExecutionException executionException) {
throw new RuntimeException("Task aborted unexpectedly.", executionException);
} catch (DSAccessException dsAccessException) {
throw new RuntimeException("Could not pull data from the omero server.", dsAccessException);
}

if (imageData == null) {
throw new IllegalArgumentException("No image available with image id " + imageId);
}
// 2. Use the instrument metadata and store them accordingly next to the image in OMERO.
List<NamedValue> mapAnnotationContent = new ArrayList<>();
ImagingHardware instrumentHardware = instrument.getHardware();
Location instrumentLocation = instrument.getLocation();
Address instrumentLocationAddress = instrumentLocation.getAddress();
try {
mapAnnotationContent.add(new NamedValue("hardware.detector.type", instrumentHardware.getDetector().getType()));
} catch (NullPointerException ignored) {}
try {
mapAnnotationContent.add(new NamedValue("hardware.objective", instrumentHardware.getObjective()));
} catch (NullPointerException ignored) {}
try {
mapAnnotationContent.add(new NamedValue("manufacturer", instrument.getManufacturer()));
} catch (NullPointerException ignored) {}
try {
mapAnnotationContent.add(new NamedValue("location.address.affiliation", instrumentLocationAddress.getAffiliation()));
} catch (NullPointerException ignored) {}
try {
mapAnnotationContent.add(new NamedValue("location.address.country", instrumentLocationAddress.getCountry()));
} catch (NullPointerException ignored) {}
try {
mapAnnotationContent.add(new NamedValue("location.address.street", instrumentLocationAddress.getStreet()));
} catch (NullPointerException ignored) {}
try {
mapAnnotationContent.add(new NamedValue("location.address.zipCode", instrumentLocationAddress.getZipCode().toString()));
} catch (NullPointerException ignored) {}
try {
mapAnnotationContent.add(new NamedValue("location.roomId", instrumentLocation.getRoomId()));
} catch (NullPointerException ignored) {}
Comment on lines +1004 to +1027
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: the underlying problem is, that the instrument properties can be null. So lets make sure they are never null, but empty Strings, Lists or empty objects.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs to be addressed in the instrument constructor then.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created an issue in the data-model-lib qbicsoftware/data-model-lib#55

try {
mapAnnotationContent.add(new NamedValue("model", instrument.getModel()));
} catch (NullPointerException ignored) {}
try {
mapAnnotationContent.add(new NamedValue("serialNumber", instrument.getSerialNumber()));
} catch (NullPointerException ignored) {}
try {
mapAnnotationContent.add(new NamedValue("type", instrument.getType()));
} catch (NullPointerException ignored) {}

MapAnnotationData annotationData = new MapAnnotationData();
annotationData.setContent(mapAnnotationContent);
annotationData.setDescription("Instrument");

try {
DataManagerFacility dataManagerFacility = gateway.getFacility(DataManagerFacility.class);
dataManagerFacility.attachAnnotation(securityContext, annotationData, imageData);
} catch (DSOutOfServiceException | ExecutionException | DSAccessException e) {
// Throw a runtime exception if anything goes wrong during this process
throw new RuntimeException("Could not annotate instrument metadata.", e);
}
// 3. Enjoy coding!
// YAY!
}


/**
* The destructor has to make sure to disconnect from the OMERO server and close the session.
* @throws Throwable
Expand Down