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);
}
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;
}
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);
}
}
}
}
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());
}
}
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;
}
Aggregations