Search in sources :

Example 1 with Image

use of ome.xml.model.Image in project bioformats by openmicroscopy.

the class OMEXMLServiceImpl method validateOMEXML.

/**
 * @see OMEXMLService#validateOMEXML(java.lang.String, boolean)
 */
@Override
public boolean validateOMEXML(String xml, boolean pixelsHack) {
    // HACK: Inject a TiffData element beneath any childless Pixels elements.
    if (pixelsHack) {
        // convert XML string to DOM
        Document doc = null;
        Exception exception = null;
        try {
            doc = XMLTools.parseDOM(xml);
        } catch (ParserConfigurationException exc) {
            exception = exc;
        } catch (SAXException exc) {
            exception = exc;
        } catch (IOException exc) {
            exception = exc;
        }
        if (exception != null) {
            LOGGER.info("Malformed OME-XML", exception);
            return false;
        }
        // inject TiffData elements as needed
        NodeList list = doc.getElementsByTagName("Pixels");
        for (int i = 0; i < list.getLength(); i++) {
            Node node = list.item(i);
            NodeList children = node.getChildNodes();
            boolean needsTiffData = true;
            for (int j = 0; j < children.getLength(); j++) {
                Node child = children.item(j);
                String name = child.getLocalName();
                if ("TiffData".equals(name) || "BinData".equals(name)) {
                    needsTiffData = false;
                    break;
                }
            }
            if (needsTiffData) {
                // inject TiffData element
                Node tiffData = doc.createElement("TiffData");
                node.insertBefore(tiffData, node.getFirstChild());
            }
        }
        // convert tweaked DOM back to XML string
        try {
            xml = XMLTools.getXML(doc);
        } catch (TransformerConfigurationException exc) {
            exception = exc;
        } catch (TransformerException exc) {
            exception = exc;
        }
        if (exception != null) {
            LOGGER.info("Internal XML conversion error", exception);
            return false;
        }
    }
    try {
        OMEXMLMetadata omexml = createOMEXMLMetadata(xml);
        OMEXMLMetadataRoot root = (OMEXMLMetadataRoot) omexml.getRoot();
        for (int image = 0; image < root.sizeOfImageList(); image++) {
            Image img = root.getImage(image);
            for (int i = 0; i < img.sizeOfLinkedAnnotationList(); i++) {
                Annotation annotation = img.getLinkedAnnotation(i);
                if (!(annotation instanceof XMLAnnotation)) {
                    continue;
                }
                String annotationXML = ((XMLAnnotation) annotation).getValue();
                Document annotationRoot = XMLTools.parseDOM(annotationXML);
                NodeList nodes = annotationRoot.getElementsByTagName("ModuloAlongZ");
                if (nodes.getLength() > 0) {
                    ((XMLAnnotation) annotation).setValue("");
                }
                nodes = annotationRoot.getElementsByTagName("ModuloAlongC");
                if (nodes.getLength() > 0) {
                    ((XMLAnnotation) annotation).setValue("");
                }
                nodes = annotationRoot.getElementsByTagName("ModuloAlongT");
                if (nodes.getLength() > 0) {
                    ((XMLAnnotation) annotation).setValue("");
                }
            }
        }
        omexml.setRoot(root);
        xml = getOMEXML(omexml);
    } catch (ServiceException | ParserConfigurationException | SAXException | IOException e) {
        LOGGER.warn("Could not remove Modulo annotations", e);
    }
    return XMLTools.validateXML(xml, "OME-XML", SCHEMA_CLASSPATH_READER);
}
Also used : TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) IOException(java.io.IOException) Document(org.w3c.dom.Document) Image(ome.xml.model.Image) 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) Annotation(ome.xml.model.Annotation) ModuloAnnotation(loci.formats.meta.ModuloAnnotation) OriginalMetadataAnnotation(loci.formats.meta.OriginalMetadataAnnotation) XMLAnnotation(ome.xml.model.XMLAnnotation) SAXException(org.xml.sax.SAXException) ServiceException(loci.common.services.ServiceException) OMEXMLMetadata(loci.formats.ome.OMEXMLMetadata) OMEXMLMetadataRoot(ome.xml.meta.OMEXMLMetadataRoot) XMLAnnotation(ome.xml.model.XMLAnnotation) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerException(javax.xml.transform.TransformerException)

Example 2 with Image

use of ome.xml.model.Image 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 3 with Image

use of ome.xml.model.Image in project bioformats by openmicroscopy.

the class OMEXMLServiceImpl method removeBinData.

/**
 * @see OMEXMLService#removeBinData(OMEXMLMetadata)
 */
@Override
public void removeBinData(OMEXMLMetadata omexmlMeta) {
    omexmlMeta.resolveReferences();
    OMEXMLMetadataRoot root = (OMEXMLMetadataRoot) omexmlMeta.getRoot();
    List<Image> images = root.copyImageList();
    for (Image img : images) {
        Pixels pix = img.getPixels();
        List<BinData> binData = pix.copyBinDataList();
        for (BinData bin : binData) {
            pix.removeBinData(bin);
        }
        pix.setMetadataOnly(null);
    }
    omexmlMeta.setRoot(root);
}
Also used : OMEXMLMetadataRoot(ome.xml.meta.OMEXMLMetadataRoot) BinData(ome.xml.model.BinData) Image(ome.xml.model.Image) Pixels(ome.xml.model.Pixels)

Example 4 with Image

use of ome.xml.model.Image in project bioformats by openmicroscopy.

the class InOutCurrentTest method testValidWellSamples.

@Test(dependsOnMethods = { "testValidPlateNode" })
public void testValidWellSamples() {
    Plate plate = ome.getPlate(0);
    Integer wellSampleIndex = 0;
    for (int row = 0; row < plate.getRows().getValue(); row++) {
        for (int col = 0; col < plate.getColumns().getValue(); col++) {
            Well well = plate.getWell(row * plate.getColumns().getValue() + col);
            assertEquals(1, well.sizeOfWellSampleList());
            WellSample sample = well.getWellSample(0);
            assertNotNull(sample);
            assertEquals(String.format("WellSample:%d_%d", row, col), sample.getID());
            assertEquals(wellSampleIndex, sample.getIndex().getValue());
            Image image = sample.getLinkedImage();
            assertNotNull(image);
            assertEquals(IMAGE_ID, image.getID());
            wellSampleIndex++;
        }
    }
}
Also used : PositiveInteger(ome.xml.model.primitives.PositiveInteger) Well(ome.xml.model.Well) WellSample(ome.xml.model.WellSample) Image(ome.xml.model.Image) Plate(ome.xml.model.Plate) Test(org.testng.annotations.Test)

Example 5 with Image

use of ome.xml.model.Image in project bioformats by openmicroscopy.

the class ObjectBasedOMEModelMock method makeImage.

private Image makeImage() {
    // Create <Image/>
    Image image = new Image();
    image.setID(InOutCurrentTest.IMAGE_ID);
    ListAnnotation listAnnotation = new ListAnnotation();
    listAnnotation.setID(InOutCurrentTest.IMAGE_LIST_ANNOTATION_ID);
    listAnnotation.setNamespace(InOutCurrentTest.GENERAL_ANNOTATION_NAMESPACE);
    annotations.addListAnnotation(listAnnotation);
    BooleanAnnotation annotation = new BooleanAnnotation();
    annotation.setID(InOutCurrentTest.IMAGE_ANNOTATION_ID);
    annotation.setValue(InOutCurrentTest.IMAGE_ANNOTATION_VALUE);
    annotation.setNamespace(InOutCurrentTest.GENERAL_ANNOTATION_NAMESPACE);
    listAnnotation.linkAnnotation(annotation);
    image.linkAnnotation(listAnnotation);
    annotations.addBooleanAnnotation(annotation);
    // Create <Pixels/>
    Pixels pixels = new Pixels();
    pixels.setID(InOutCurrentTest.PIXELS_ID);
    pixels.setSizeX(new PositiveInteger(InOutCurrentTest.SIZE_X));
    pixels.setSizeY(new PositiveInteger(InOutCurrentTest.SIZE_Y));
    pixels.setSizeZ(new PositiveInteger(InOutCurrentTest.SIZE_Z));
    pixels.setSizeC(new PositiveInteger(InOutCurrentTest.SIZE_C));
    pixels.setSizeT(new PositiveInteger(InOutCurrentTest.SIZE_T));
    pixels.setDimensionOrder(InOutCurrentTest.DIMENSION_ORDER);
    pixels.setType(InOutCurrentTest.PIXEL_TYPE);
    // Create <TiffData/>
    TiffData tiffData = new TiffData();
    // Create <UUID/>
    UUID uuid = new UUID();
    uuid.setValue(InOutCurrentTest.TIFF_DATA_UUID);
    tiffData.setUUID(uuid);
    pixels.addTiffData(tiffData);
    // Create <Channel/> under <Pixels/>
    for (int i = 0; i < InOutCurrentTest.SIZE_C; i++) {
        Channel channel = new Channel();
        channel.setID("Channel:" + i);
        if (i == 0) {
            XMLAnnotation channelAnnotation = new XMLAnnotation();
            channelAnnotation.setID(InOutCurrentTest.CHANNEL_ANNOTATION_ID);
            channelAnnotation.setValue(InOutCurrentTest.CHANNEL_ANNOTATION_VALUE);
            channelAnnotation.setNamespace(InOutCurrentTest.GENERAL_ANNOTATION_NAMESPACE);
            channel.linkAnnotation(channelAnnotation);
            annotations.addXMLAnnotation(channelAnnotation);
        }
        pixels.addChannel(channel);
    }
    // Put <Pixels/> under <Image/>
    image.setPixels(pixels);
    return image;
}
Also used : BooleanAnnotation(ome.xml.model.BooleanAnnotation) PositiveInteger(ome.xml.model.primitives.PositiveInteger) TiffData(ome.xml.model.TiffData) Channel(ome.xml.model.Channel) ListAnnotation(ome.xml.model.ListAnnotation) XMLAnnotation(ome.xml.model.XMLAnnotation) Image(ome.xml.model.Image) UUID(ome.xml.model.UUID) Pixels(ome.xml.model.Pixels)

Aggregations

Image (ome.xml.model.Image)29 Pixels (ome.xml.model.Pixels)15 OMEXMLMetadataRoot (ome.xml.meta.OMEXMLMetadataRoot)11 Test (org.testng.annotations.Test)10 Channel (ome.xml.model.Channel)8 PositiveInteger (ome.xml.model.primitives.PositiveInteger)8 ServiceException (loci.common.services.ServiceException)7 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 FormatException (loci.formats.FormatException)5 Instrument (ome.xml.model.Instrument)5 DependencyException (loci.common.services.DependencyException)4 XMLAnnotation (ome.xml.model.XMLAnnotation)4 BeforeClass (org.testng.annotations.BeforeClass)4 Node (org.w3c.dom.Node)4 NodeList (org.w3c.dom.NodeList)4 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)3 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)3 TransformerException (javax.xml.transform.TransformerException)3 Location (loci.common.Location)3