Search in sources :

Example 1 with OMEXMLMetadata

use of loci.formats.ome.OMEXMLMetadata 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 OMEXMLMetadata

use of loci.formats.ome.OMEXMLMetadata in project bioformats by openmicroscopy.

the class OMEXMLServiceImpl method createOMEXMLMetadata.

/**
 * @see OMEXMLService#createOMEXMLMetadata(java.lang.String, java.lang.String)
 */
@Override
public OMEXMLMetadata createOMEXMLMetadata(String xml, String version) throws ServiceException {
    if (xml != null) {
        xml = XMLTools.sanitizeXML(xml);
    }
    OMEXMLMetadataRoot ome = xml == null ? null : createRoot(transformToLatestVersion(xml));
    OMEXMLMetadata meta = new OMEXMLMetadataImpl();
    if (ome != null)
        meta.setRoot(ome);
    return meta;
}
Also used : OMEXMLMetadata(loci.formats.ome.OMEXMLMetadata) OMEXMLMetadataRoot(ome.xml.meta.OMEXMLMetadataRoot) OMEXMLMetadataImpl(loci.formats.ome.OMEXMLMetadataImpl)

Example 3 with OMEXMLMetadata

use of loci.formats.ome.OMEXMLMetadata in project bioformats by openmicroscopy.

the class TrestleReader method initMetadataStore.

/* @see loci.formats.BaseTiffReader#initMetadataStore() */
@Override
protected void initMetadataStore() throws FormatException {
    super.initMetadataStore();
    MetadataStore store = makeFilterMetadata();
    for (int i = 0; i < getSeriesCount(); i++) {
        store.setImageName("Series " + (i + 1), i);
    }
    MetadataLevel level = getMetadataOptions().getMetadataLevel();
    if (level != MetadataLevel.MINIMUM) {
        // will not be stored with the mask dimensions
        if (level != MetadataLevel.NO_OVERLAYS && !(getMetadataStore() instanceof OMEXMLMetadata)) {
            try {
                parseROIs(store);
            } catch (IOException e) {
                LOGGER.debug("Could not parse ROIs", e);
            }
        }
    }
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) OMEXMLMetadata(loci.formats.ome.OMEXMLMetadata) IOException(java.io.IOException)

Example 4 with OMEXMLMetadata

use of loci.formats.ome.OMEXMLMetadata in project bioformats by openmicroscopy.

the class FakeReaderTest method testTwoPlates.

@Test
public void testTwoPlates() throws Exception {
    Location twoPlates = new FakeImage(mkSubd(wd, "2P.fake")).generateScreen(2, 2, 2, 2, 4);
    reader.setId(twoPlates.getAbsolutePath());
    OMEXMLMetadata metadata = reader.getOmeXmlMetadata();
    int i = reader.getImageCount();
    while (i >= 0) {
        assertEquals(metadata.getChannelCount(i--), reader.getSizeC());
    }
}
Also used : FakeImage(loci.formats.tools.FakeImage) OMEXMLMetadata(loci.formats.ome.OMEXMLMetadata) Location(loci.common.Location) Test(org.testng.annotations.Test)

Example 5 with OMEXMLMetadata

use of loci.formats.ome.OMEXMLMetadata in project bioformats by openmicroscopy.

the class FileExportSPW method initializeMetadata.

/**
 * Populate the minimum amount of metadata required to export a Plate.
 *
 * @param width
 *          the width (in pixels) of the image
 * @param height
 *          the height (in pixels) of the image
 * @param pixelType
 *          the pixel type of the image; @see loci.formats.FormatTools
 */
private IMetadata initializeMetadata(int width, int height, int pixelType) {
    Exception exception = null;
    try {
        // create the OME-XML metadata storage object
        ServiceFactory factory = new ServiceFactory();
        OMEXMLService service = factory.getInstance(OMEXMLService.class);
        OMEXMLMetadata meta = service.createOMEXMLMetadata();
        meta.createRoot();
        int plateIndex = 0;
        // count of images
        int series = 0;
        int well = 0;
        // Create Minimal 2x2 Plate
        meta.setPlateID(MetadataTools.createLSID("Plate", 0), 0);
        meta.setPlateRowNamingConvention(NamingConvention.LETTER, 0);
        meta.setPlateColumnNamingConvention(NamingConvention.NUMBER, 0);
        meta.setPlateRows(new PositiveInteger(rows), 0);
        meta.setPlateColumns(new PositiveInteger(cols), 0);
        meta.setPlateName("First test Plate", 0);
        PositiveInteger pwidth = new PositiveInteger(width);
        PositiveInteger pheight = new PositiveInteger(height);
        char rowChar = 'A';
        for (int row = 0; row < rows; row++) {
            for (int column = 0; column < cols; column++) {
                // set up well
                String wellID = MetadataTools.createLSID("Well:", well);
                meta.setWellID(wellID, plateIndex, well);
                meta.setWellRow(new NonNegativeInteger(row), plateIndex, well);
                meta.setWellColumn(new NonNegativeInteger(column), plateIndex, well);
                for (int fov = 0; fov < fovPerWell; fov++) {
                    // Create Image
                    String imageName = rowChar + ":" + Integer.toString(column + 1) + ":FOV:" + Integer.toString(fov + 1);
                    String imageID = MetadataTools.createLSID("Image", well, fov);
                    meta.setImageID(imageID, series);
                    meta.setImageName(imageName, series);
                    String pixelsID = MetadataTools.createLSID("Pixels", row, well, fov);
                    meta.setPixelsID(pixelsID, series);
                    // specify that the pixel data is stored in big-endian format
                    // change 'TRUE' to 'FALSE' to specify little-endian format
                    meta.setPixelsBinDataBigEndian(Boolean.TRUE, series, 0);
                    // specify that the image is stored in ZCT order
                    meta.setPixelsDimensionOrder(DimensionOrder.XYZCT, series);
                    // specify the pixel type of the image
                    meta.setPixelsType(PixelType.fromString(FormatTools.getPixelTypeString(pixelType)), series);
                    // specify the dimensions of the image
                    meta.setPixelsSizeX(pwidth, series);
                    meta.setPixelsSizeY(pheight, series);
                    meta.setPixelsSizeZ(new PositiveInteger(1), series);
                    meta.setPixelsSizeC(new PositiveInteger(1), series);
                    meta.setPixelsSizeT(new PositiveInteger(sizeT), series);
                    // define each channel and specify the number of samples in the
                    // channel the number of samples is 3 for RGB images and 1 otherwise
                    String channelID = MetadataTools.createLSID("Channel", well, fov);
                    meta.setChannelID(channelID, series, 0);
                    meta.setChannelSamplesPerPixel(new PositiveInteger(1), series, 0);
                    // set sample
                    String wellSampleID = MetadataTools.createLSID("WellSample", well, fov);
                    meta.setWellSampleID(wellSampleID, 0, well, fov);
                    // NB sampleIndex here == series ie the image No
                    meta.setWellSampleIndex(new NonNegativeInteger(series), 0, well, fov);
                    meta.setWellSampleImageRef(imageID, 0, well, fov);
                    // add FLIM ModuloAlongT annotation if required
                    // CoreMetadata modlo = createModuloAnn();
                    // meta.addModuloAlong(meta, modlo, series);
                    series++;
                }
                // end of samples
                well++;
            }
            rowChar++;
        }
        return meta;
    } catch (DependencyException e) {
        exception = e;
    } catch (ServiceException e) {
        exception = e;
    } catch (EnumerationException e) {
        exception = e;
    }
    System.err.println("Failed to populate OME-XML metadata object.");
    exception.printStackTrace();
    return null;
}
Also used : PositiveInteger(ome.xml.model.primitives.PositiveInteger) ServiceException(loci.common.services.ServiceException) ServiceFactory(loci.common.services.ServiceFactory) OMEXMLMetadata(loci.formats.ome.OMEXMLMetadata) NonNegativeInteger(ome.xml.model.primitives.NonNegativeInteger) DependencyException(loci.common.services.DependencyException) EnumerationException(ome.xml.model.enums.EnumerationException) FormatException(loci.formats.FormatException) EnumerationException(ome.xml.model.enums.EnumerationException) ServiceException(loci.common.services.ServiceException) IOException(java.io.IOException) DependencyException(loci.common.services.DependencyException) OMEXMLService(loci.formats.services.OMEXMLService)

Aggregations

OMEXMLMetadata (loci.formats.ome.OMEXMLMetadata)24 ServiceException (loci.common.services.ServiceException)12 ServiceFactory (loci.common.services.ServiceFactory)10 OMEXMLService (loci.formats.services.OMEXMLService)10 DependencyException (loci.common.services.DependencyException)9 IOException (java.io.IOException)8 FormatException (loci.formats.FormatException)8 MetadataStore (loci.formats.meta.MetadataStore)7 OMEXMLMetadataRoot (ome.xml.meta.OMEXMLMetadataRoot)7 Location (loci.common.Location)6 PositiveInteger (ome.xml.model.primitives.PositiveInteger)6 CoreMetadata (loci.formats.CoreMetadata)5 Length (ome.units.quantity.Length)5 NonNegativeInteger (ome.xml.model.primitives.NonNegativeInteger)5 Test (org.testng.annotations.Test)5 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)3 TransformerException (javax.xml.transform.TransformerException)3 MissingLibraryException (loci.formats.MissingLibraryException)3 Image (ome.xml.model.Image)3 EnumerationException (ome.xml.model.enums.EnumerationException)3