use of ome.xml.model.StructuredAnnotations in project bioformats by openmicroscopy.
the class OMEXMLServiceImpl method populateOriginalMetadata.
/**
* @see OMEXMLService#populateOriginalMetadata(loci.formats.ome.OMEXMLMetadata, Hashtable)
*/
@Override
public void populateOriginalMetadata(OMEXMLMetadata omexmlMeta, Hashtable<String, Object> metadata) {
omexmlMeta.resolveReferences();
if (metadata.size() == 0) {
return;
}
OMEXMLMetadataRoot root = (OMEXMLMetadataRoot) omexmlMeta.getRoot();
StructuredAnnotations annotations = root.getStructuredAnnotations();
if (annotations == null)
annotations = new StructuredAnnotations();
int annotationIndex = annotations.sizeOfXMLAnnotationList();
if (annotationIndex > 0) {
String lastAnnotationID = omexmlMeta.getXMLAnnotationID(annotationIndex - 1);
String lastIndex = lastAnnotationID.substring(lastAnnotationID.lastIndexOf(":") + 1);
try {
int index = Integer.parseInt(lastIndex);
while (index >= annotationIndex) {
annotationIndex++;
}
} catch (NumberFormatException e) {
}
}
for (String key : metadata.keySet()) {
OriginalMetadataAnnotation annotation = new OriginalMetadataAnnotation();
annotation.setID(MetadataTools.createLSID("Annotation", annotationIndex));
annotation.setKeyValue(key, metadata.get(key).toString());
annotations.addXMLAnnotation(annotation);
annotationIndex++;
}
root.setStructuredAnnotations(annotations);
omexmlMeta.setRoot(root);
}
use of ome.xml.model.StructuredAnnotations in project bioformats by openmicroscopy.
the class OMEXMLServiceImpl method populateOriginalMetadata.
/**
* @see OMEXMLService#populateOriginalMetadata(loci.formats.ome.OMEXMLMetadata, java.lang.String, java.lang.String)
*/
@Override
public void populateOriginalMetadata(OMEXMLMetadata omexmlMeta, String key, String value) {
omexmlMeta.resolveReferences();
OMEXMLMetadataRoot root = (OMEXMLMetadataRoot) omexmlMeta.getRoot();
StructuredAnnotations annotations = root.getStructuredAnnotations();
if (annotations == null)
annotations = new StructuredAnnotations();
int annotationIndex = annotations.sizeOfXMLAnnotationList();
if (annotationIndex > 0) {
String lastAnnotationID = omexmlMeta.getXMLAnnotationID(annotationIndex - 1);
String lastIndex = lastAnnotationID.substring(lastAnnotationID.lastIndexOf(":") + 1);
try {
int index = Integer.parseInt(lastIndex);
while (index >= annotationIndex) {
annotationIndex++;
}
} catch (NumberFormatException e) {
}
}
OriginalMetadataAnnotation annotation = new OriginalMetadataAnnotation();
annotation.setID(MetadataTools.createLSID("Annotation", annotationIndex));
annotation.setKeyValue(key, value);
annotations.addXMLAnnotation(annotation);
root.setStructuredAnnotations(annotations);
omexmlMeta.setRoot(root);
}
use of ome.xml.model.StructuredAnnotations in project bioformats by openmicroscopy.
the class OMEXMLServiceImpl method addModuloAlong.
@Override
public void addModuloAlong(OMEXMLMetadata meta, CoreMetadata core, int imageIdx) {
meta.resolveReferences();
if (core.moduloZ.length() == 1 && core.moduloC.length() == 1 && core.moduloT.length() == 1) {
// nothing to populate
return;
}
OMEXMLMetadataRoot root = (OMEXMLMetadataRoot) meta.getRoot();
Image image;
try {
image = root.getImage(imageIdx);
} catch (IndexOutOfBoundsException ieeb) {
// and exiting without doing anything.
return;
}
StructuredAnnotations annotations = root.getStructuredAnnotations();
if (annotations == null)
annotations = new StructuredAnnotations();
int annotationIndex = annotations.sizeOfXMLAnnotationList();
final Set<String> knownModulos = new HashSet<String>();
if (annotationIndex > 0) {
// Check which modulo annotations are already present.
for (int idx = 0; idx < annotationIndex; idx++) {
if (ModuloAnnotation.MODULO_NS.equals(meta.getXMLAnnotationNamespace(idx))) {
// ignore this annotation if it is not linked to the current Image
boolean ignore = true;
String xmlID = meta.getXMLAnnotationID(idx);
for (int link = 0; link < image.sizeOfLinkedAnnotationList(); link++) {
if (xmlID.equals(image.getLinkedAnnotation(link).getID())) {
ignore = false;
break;
}
}
if (ignore) {
continue;
}
String value = meta.getXMLAnnotationValue(idx);
try {
Document doc = XMLTools.parseDOM(value);
NodeList modulos = doc.getElementsByTagName("Modulo");
for (int m = 0; m < modulos.getLength(); m++) {
Node modulo = modulos.item(m);
NodeList children = modulo.getChildNodes();
for (int c = 0; c < children.getLength(); c++) {
Node child = children.item(c);
String name = child.getNodeName();
knownModulos.add(name);
}
}
} catch (Exception e) {
LOGGER.warn("Could not parse XML from annotation: {}", value, e);
}
}
}
// Calculate the next annotation ID that should be used.
String lastAnnotationID = meta.getXMLAnnotationID(annotationIndex - 1);
String lastIndex = lastAnnotationID.substring(lastAnnotationID.lastIndexOf(":") + 1);
try {
int index = Integer.parseInt(lastIndex);
while (index >= annotationIndex) {
annotationIndex++;
}
} catch (NumberFormatException e) {
}
}
int imageAnnotation = 0;
if (core.moduloZ.length() > 1 && !knownModulos.contains("ModuloAlongZ")) {
createModulo(meta, core.moduloZ, annotations, image, imageIdx, annotationIndex, imageAnnotation);
annotationIndex++;
imageAnnotation++;
}
if (core.moduloC.length() > 1 && !knownModulos.contains("ModuloAlongC")) {
createModulo(meta, core.moduloC, annotations, image, imageIdx, annotationIndex, imageAnnotation);
annotationIndex++;
imageAnnotation++;
}
if (core.moduloT.length() > 1 && !knownModulos.contains("ModuloAlongT")) {
createModulo(meta, core.moduloT, annotations, image, imageIdx, annotationIndex, imageAnnotation);
annotationIndex++;
imageAnnotation++;
}
root.setStructuredAnnotations(annotations);
meta.setRoot(root);
}
use of ome.xml.model.StructuredAnnotations in project bioformats by openmicroscopy.
the class FileWriteSPW method cleanup.
/**
* Close the file writer.
*/
public void cleanup() {
// No of planes expected for each image = 1 if not FLIM
int validPlanes = 1;
if (delays != null) {
validPlanes = sizet;
}
OMEXMLMetadataRoot root = (OMEXMLMetadataRoot) omexml.getRoot();
Plate plate = root.getPlate(0);
StructuredAnnotations anns = root.getStructuredAnnotations();
ArrayList<Image> invalidImages = new ArrayList<>();
// if not record those images as being invalid
for (int i = 0; i < expectedImages.length; i++) {
if (expectedImages[i] < validPlanes) {
Image im = root.getImage(i);
invalidImages.add(im);
// remove modulo Annotation if FLIM
if (delays != null) {
XMLAnnotation ann = (XMLAnnotation) im.getLinkedAnnotation(0);
anns.removeXMLAnnotation(ann);
}
}
}
// Now remove all limked wellSnmples and then invalid images
for (int i = 0; i < invalidImages.size(); i++) {
Image im = invalidImages.get(i);
List<WellSample> list = im.copyLinkedWellSampleList();
if (!list.isEmpty()) {
WellSample wellSample = im.getLinkedWellSample(0);
Well well = wellSample.getWell();
well.removeWellSample(wellSample);
}
root.removeImage(im);
}
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
System.err.println("Failed to close file writer.");
}
}
}
use of ome.xml.model.StructuredAnnotations in project bioformats by openmicroscopy.
the class Upgrade201004Test method validateUpgrade.
@Test
public void validateUpgrade() throws ServiceException {
assertEquals(1, ome.sizeOfImageList());
// StringAnnotation --> CommentAnnotation
StructuredAnnotations structuredAnnotations = ome.getStructuredAnnotations();
assertNotNull(structuredAnnotations);
assertEquals(1, structuredAnnotations.sizeOfCommentAnnotationList());
CommentAnnotation commentAnnotation = structuredAnnotations.getCommentAnnotation(0);
assertEquals("StringAnnotation:0", commentAnnotation.getID());
assertEquals("Transform", commentAnnotation.getNamespace());
assertEquals("Foobar", commentAnnotation.getValue());
}
Aggregations