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