Search in sources :

Example 1 with StructuredAnnotations

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);
}
Also used : OMEXMLMetadataRoot(ome.xml.meta.OMEXMLMetadataRoot) StructuredAnnotations(ome.xml.model.StructuredAnnotations) OriginalMetadataAnnotation(loci.formats.meta.OriginalMetadataAnnotation)

Example 2 with StructuredAnnotations

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);
}
Also used : OMEXMLMetadataRoot(ome.xml.meta.OMEXMLMetadataRoot) StructuredAnnotations(ome.xml.model.StructuredAnnotations) OriginalMetadataAnnotation(loci.formats.meta.OriginalMetadataAnnotation)

Example 3 with StructuredAnnotations

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);
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) StructuredAnnotations(ome.xml.model.StructuredAnnotations) Image(ome.xml.model.Image) Document(org.w3c.dom.Document) ServiceException(loci.common.services.ServiceException) SAXException(org.xml.sax.SAXException) TransformerException(javax.xml.transform.TransformerException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) OMEXMLMetadataRoot(ome.xml.meta.OMEXMLMetadataRoot) HashSet(java.util.HashSet)

Example 4 with StructuredAnnotations

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.");
        }
    }
}
Also used : OMEXMLMetadataRoot(ome.xml.meta.OMEXMLMetadataRoot) Well(ome.xml.model.Well) ArrayList(java.util.ArrayList) StructuredAnnotations(ome.xml.model.StructuredAnnotations) XMLAnnotation(ome.xml.model.XMLAnnotation) WellSample(ome.xml.model.WellSample) IOException(java.io.IOException) Image(ome.xml.model.Image) Plate(ome.xml.model.Plate)

Example 5 with StructuredAnnotations

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());
}
Also used : CommentAnnotation(ome.xml.model.CommentAnnotation) StructuredAnnotations(ome.xml.model.StructuredAnnotations) Test(org.testng.annotations.Test)

Aggregations

StructuredAnnotations (ome.xml.model.StructuredAnnotations)8 OMEXMLMetadataRoot (ome.xml.meta.OMEXMLMetadataRoot)5 OriginalMetadataAnnotation (loci.formats.meta.OriginalMetadataAnnotation)4 IOException (java.io.IOException)3 Image (ome.xml.model.Image)3 XMLAnnotation (ome.xml.model.XMLAnnotation)3 ArrayList (java.util.ArrayList)2 Hashtable (java.util.Hashtable)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 Test (org.testng.annotations.Test)2 Document (org.w3c.dom.Document)2 Node (org.w3c.dom.Node)2 NodeList (org.w3c.dom.NodeList)2 SAXException (org.xml.sax.SAXException)2 HashSet (java.util.HashSet)1 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)1 TransformerException (javax.xml.transform.TransformerException)1 ServiceException (loci.common.services.ServiceException)1 OMEXMLMetadata (loci.formats.ome.OMEXMLMetadata)1 CommentAnnotation (ome.xml.model.CommentAnnotation)1