Skip to content

Instantly share code, notes, and snippets.

@NicoKiaru
Created May 8, 2023 21:17
Show Gist options
  • Save NicoKiaru/7aa6f2e2725673885eb54e7c5927fc5e to your computer and use it in GitHub Desktop.
Save NicoKiaru/7aa6f2e2725673885eb54e7c5927fc5e to your computer and use it in GitHub Desktop.
A class that can be used to (partially) bind CZI file properties to Java objects
package loci.formats.in.libczi;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.ArrayList;
import loci.common.DataTools;
import ome.units.UNITS;
import ome.units.quantity.Length;
/**
* Java class used by JAXB to parse CZI Image Document xml
*
*
*
*/
/*
* Usage in previous ZeissCZIReader:
*
* in translateExperiment:
* positionGroups = Experiment.ExperimentBlocks[0].AcquisitionBlock.TilesSetup.?.PositionGroup[]
*
* if (positionGroups!=null) {
* get pos from positionGroups.{Position.{X, Y, Z}, TilesX, TilesY}
* } else {
* regionsSetup = Experiment.ExperimentBlocks[0].AcquisitionBlock.RegionsSetup
* if (regionsSetup!=null) {
* regionsSetup.SampleHolder.Template.{ShapeRows, ShapeColumns}
* if (Template!=null) {
* do stuff with plates
* }
* } else {
* Give up!
* }
* }
*
*
*
*
*
*
*/
public class XmlCZI {
@XmlRootElement(name = "ImageDocument")
public static class ImageDocument {
public Metadata Metadata; // Capital letter to match the xml format
}
public static class Metadata {
public Experiment Experiment;
public Information Information;
public Scaling Scaling;
public DisplaySetting DisplaySetting;
}
public static class Experiment {
@XmlAttribute(name="Version")
public String Version;
public ExperimentBlocks ExperimentBlocks;
}
public static class ExperimentBlocks {
@XmlElement(type = AcquisitionBlock.class)
public AcquisitionBlock AcquisitionBlock;
}
public static class AcquisitionBlock {
public SubDimensionSetupsLvl0 SubDimensionSetups;
public HelperSetups HelperSetups;
}
public static class SubDimensionSetupsLvl0 {
public RegionsSetup RegionsSetup;
}
public static class RegionsSetup {
@XmlAttribute(name = "IsActivated")
public boolean IsActivated;
public SampleHolder SampleHolder;
public SubDimensionSetupsLvl1 SubDimensionSetups;
}
public static class SampleHolder {
public ArrayList<TileRegions> TileRegions;
}
public static class TileRegions {
public ArrayList<TileRegion> TileRegion;
}
public static class TileRegion {
@XmlAttribute(name = "Name")
String Name;
@XmlAttribute(name = "Id")
long Id;
/*
<CenterPosition>39673.619,17098.942</CenterPosition>
<ContourSize>555.738,545.447</ContourSize>
<Columns>7</Columns>
<Rows>7</Rows>
<Z>6226.53</Z>
<TemplateShapeId></TemplateShapeId>
<IsUsedForAcquisition>true</IsUsedForAcquisition>
<IsProtected>false</IsProtected>
<AreSupportPointsOutsideContourAllowed>false</AreSupportPointsOutsideContourAllowed>
<PreferSupportPointsZ>false</PreferSupportPointsZ>
<GeometryLockingMode>None</GeometryLockingMode>
<Contour Type="Polygon">
<Points>39414.959,17098.6 39430.74,17084.192 39434.856,17078.017 39438.973,17074.586 39443.775,17069.784 39446.52,17066.353 39451.322,17062.237 39455.439,17058.12 39460.242,17054.003 39463.672,17049.887 39467.103,17046.456 39471.219,17041.654 39476.022,17037.537 39478.766,17033.42 39482.883,17029.99 39486.313,17025.873 39490.43,17023.129 39495.919,17018.326 39500.035,17015.582 39504.838,17012.151 39508.269,17010.093 39512.385,17005.29 39518.56,17000.488 39521.99,16996.371 39525.421,16992.941 39528.165,16989.51 39532.968,16986.08 39537.771,16982.649 39540.515,16979.219 39544.632,16975.102 39548.748,16969.613 39552.179,16966.183 39555.609,16963.439 39559.726,16959.322 39563.842,16955.205 39567.273,16952.461 39572.075,16947.658 39575.506,16944.914 39579.623,16940.797 39584.425,16936.681 39587.17,16933.25 39591.286,16929.82 39595.403,16925.703 39600.205,16920.215 39604.322,16915.412 39607.753,16911.295 39611.869,16907.179 39615.986,16903.062 39619.416,16898.946 39622.161,16895.515 39626.277,16891.398 39630.394,16887.968 39634.51,16883.851 39637.941,16882.479 39641.371,16879.735 39645.488,16877.677 39649.604,16874.932 39655.093,16870.816 39659.21,16867.385 39662.64,16865.327 39666.071,16863.955 39669.501,16862.582 39672.932,16861.21 39677.048,16859.152 39680.479,16855.721 39684.595,16853.663 39689.398,16852.291 39693.515,16849.547 39696.945,16848.174 39701.748,16846.802 39705.864,16846.116 39709.981,16844.058 39713.411,16843.372 39716.842,16842.686 39720.958,16841.313 39725.075,16839.941 39728.506,16838.569 39731.936,16837.197 39736.053,16835.825 39739.483,16835.139 39742.914,16833.766 39746.344,16832.394 39750.461,16831.708 39753.891,16831.022 39757.322,16830.336 39761.438,16829.65 39765.555,16828.964 39769.671,16828.964 39773.102,16828.278 39776.532,16828.278 39780.649,16827.591 39784.079,16826.905 39787.51,16826.905 39790.94,16826.905 39795.057,16826.905 39799.173,16826.905 39802.604,16826.905 39806.034,16826.905 39810.151,16826.905 39813.582,16826.905 39817.012,16826.219 39821.129,16826.219 39825.931,16826.219 39830.734,16826.219 39834.85,16826.905 39838.281,16827.591 39843.77,16827.591 39847.2,16828.278 39851.317,16828.964 39854.747,16829.65 39858.178,16831.022 39862.294,16832.394 39866.411,16833.08 39870.528,16833.766 39873.958,16836.511 39878.075,16839.941 39881.505,16843.372 39883.563,16846.802 39886.308,16850.233 39887.68,16853.663 39889.052,16857.094 39892.483,16861.21 39893.855,16864.641 39895.227,16868.757 39895.913,16873.56 39897.285,16878.363 39897.971,16882.479 39898.657,16887.282 39899.344,16891.398 39900.03,16896.201 39900.716,16899.632 39901.402,16903.062 39902.774,16907.865 39903.46,16911.981 39904.832,16916.784 39904.832,16920.215 39905.518,16923.645 39906.205,16927.762 39906.891,16931.192 39907.577,16934.623 39907.577,16938.739 39908.263,16942.17 39908.949,16946.286 39908.949,16949.717 39910.321,16953.833 39910.321,16959.322 39911.007,16964.125 39911.007,16968.241 39911.693,16975.788 39912.379,16981.277 39912.379,16984.708 39913.752,16988.824 39913.752,16995.685 39914.438,17000.488 39915.124,17007.349 39915.81,17010.779 39917.182,17016.954 39917.868,17020.385 39917.868,17025.187 39918.554,17028.618 39919.24,17034.107 39919.926,17039.595 39920.613,17045.77 39921.299,17050.573 39922.671,17054.003 39922.671,17058.12 39923.357,17061.55 39924.043,17065.667 39924.729,17070.47 39925.415,17073.9 39925.415,17078.017 39927.474,17085.564 39928.846,17091.739 39928.846,17095.855 39930.904,17100.658 39930.904,17104.088 39932.276,17110.263 39933.648,17114.38 39935.021,17119.183 39935.707,17123.299 39937.079,17127.416 39937.765,17130.846 39939.137,17134.277 39939.823,17138.393 39941.195,17141.824 39942.568,17146.626 39944.626,17150.743 39945.998,17154.173 39947.37,17157.604 39948.743,17161.034 39950.115,17164.465 39951.487,17167.895 39951.487,17171.326 39948.056,17173.384 39944.626,17174.07 39939.823,17174.756 39936.393,17174.756 39932.962,17175.442 39928.846,17175.442 39925.415,17176.815 39921.985,17177.501 39917.868,17178.187 39913.066,17178.873 39908.949,17178.873 39905.518,17178.873 39898.657,17179.559 39894.541,17180.245 39891.11,17180.245 39885.622,17180.931 39882.191,17181.617 39878.761,17182.303 39873.272,17183.676 39869.841,17183.676 39866.411,17184.362 39862.294,17185.048 39857.492,17185.734 39854.061,17186.42 39849.259,17186.42 39844.456,17187.106 39841.025,17187.792 39836.223,17189.164 39832.106,17189.85 39828.676,17191.223 39823.873,17191.909 39818.384,17193.281 39812.895,17194.653 39808.093,17196.025 39803.976,17197.398 39799.173,17199.456 39795.743,17200.828 39792.313,17202.2 39788.196,17203.572 39784.079,17206.317 39780.649,17207.689 39775.16,17211.806 39771.73,17213.178 39767.613,17215.236 39763.496,17217.294 39758.694,17220.725 39754.577,17223.469 39751.147,17225.528 39746.344,17228.272 39742.914,17231.016 39738.797,17233.761 39734.68,17237.191 39730.564,17239.936 39725.761,17244.738 39722.331,17247.483 39718.214,17251.599 39713.411,17255.716 39709.981,17258.46 39705.178,17262.577 39701.062,17266.693 39696.259,17270.124 39690.084,17276.299 39685.968,17279.729 39679.793,17284.532 39674.99,17288.648 39671.56,17291.393 39668.815,17295.509 39665.385,17297.568 39663.326,17300.998 39660.582,17304.429 39657.151,17307.859 39653.721,17311.29 39649.604,17316.092 39646.86,17319.523 39642.743,17323.639 39639.313,17326.384 39635.196,17330.5 39631.766,17333.245 39628.335,17336.675 39624.905,17340.106 39621.474,17342.164 39617.358,17345.594 39613.927,17347.653 39612.555,17351.083 39609.125,17353.828 39605.694,17357.258 39602.264,17360.002 39599.519,17363.433 39595.403,17366.177 39591.972,17368.236 39588.542,17369.608 39584.425,17370.294 39580.995,17370.98 39576.192,17371.666 39572.762,17371.666 39567.959,17371.666 39563.156,17371.666 39557.667,17371.666 39552.179,17371.666 39548.748,17371.666 39543.946,17371.666 39538.457,17371.666 39532.968,17371.666 39526.793,17371.666 39520.618,17371.666 39515.816,17371.666 39511.013,17371.666 39504.838,17370.294 39501.408,17370.294 39495.919,17370.294 39491.116,17369.608 39485.627,17368.236 39482.197,17367.55 39478.766,17366.177 39473.964,17365.491 39470.533,17364.119 39467.103,17363.433 39463.672,17360.689 39459.556,17358.63 39455.439,17354.514 39450.636,17349.711 39445.834,17344.908 39441.717,17341.478 39438.973,17336.675 39434.856,17332.559 39433.484,17329.128 39431.426,17325.698 39430.053,17322.267 39427.995,17318.837 39426.623,17315.406 39424.565,17311.976 39423.879,17308.545 39421.82,17303.056 39419.762,17299.626 39418.39,17296.195 39415.645,17290.021 39413.587,17285.218 39412.215,17280.415 39410.843,17276.299 39410.157,17272.868 39409.471,17268.752 39408.784,17263.949 39407.412,17259.146 39406.04,17252.971 39405.354,17246.797 39404.668,17241.308 39402.61,17237.877 39401.924,17232.388 39401.237,17228.958 39400.551,17224.841 39400.551,17221.411 39399.865,17216.608 39399.179,17212.492 39397.807,17207.689 39397.121,17202.2 39396.435,17195.339 39396.435,17187.792 39396.435,17184.362 39395.749,17179.559 39395.749,17175.442 39395.749,17172.012 39395.749,17165.837 39395.749,17161.034 39395.749,17155.546 39395.749,17152.115 39395.749,17147.999 39395.749,17143.882 39395.749,17140.452 39396.435,17137.021 39396.435,17133.591 39397.121,17129.474 39397.807,17124.671 39398.493,17120.555 39399.179,17116.438 39399.179,17113.008 39400.551,17109.577 39401.237,17104.775 39401.237,17101.344 39402.61,17097.914 39414.959,17098.6</Points>
</Contour>
<SupportPoints></SupportPoints>
*/
@XmlElement
String CenterPosition;
@XmlElement
String ContourSize;
@XmlElement
int Columns;
@XmlElement
int Rows;
@XmlElement
double Z;
@XmlElement
boolean IsUsedForAcquisition;
@XmlElement
boolean IsProtected;
public Length getCenterPositionX() {
String[] pos = CenterPosition.split(",");
return new Length(DataTools.parseDouble(pos[0]), UNITS.MICROMETER);
}
public Length getCenterPositionY() {
String[] pos = CenterPosition.split(",");
return new Length(DataTools.parseDouble(pos[1]), UNITS.MICROMETER);
}
}
public static class SubDimensionSetupsLvl1 {
public TilesSetup TilesSetup;
}
public static class TilesSetup {
public SubDimensionSetupsLvl2 SubDimensionSetups;
}
public static class SubDimensionSetupsLvl2 {
public MultiTrackSetup MultiTrackSetup;
}
public static class MultiTrackSetup {
@XmlAttribute(name = "IsActivated")
public boolean IsActivated;
@XmlElement(name = "Track")
public ArrayList<Track> tracks;
}
public static class Track {
@XmlAttribute(name = "IsActivated")
public boolean IsActivated;
@XmlAttribute(name = "IsSelected")
public boolean IsSelected;
public DetectionModeSetup DetectionModeSetup;
@XmlElement(name = "Channels")
public ArrayList<ChannelTrack> Channels;
}
public static class HelperSetups {
public AcquisitionModeSetup AcquisitionModeSetup;
}
public static class AcquisitionModeSetup {
@XmlAttribute(name = "IsActivated")
public boolean IsActivated;
public Detectors Detectors;
@XmlElement(name = "SelectedDetector")
public String SelectedDetector;
}
public static class Detectors {
ArrayList<Detector> detectors;
}
public static class Detector {
@XmlAttribute(name = "Id")
String Id;
}
public static class DetectionModeSetup {
}
public static class ChannelTrack {
// <Channel IsActivated="false" IsSelected="false" ChannelSetupId="638046990515120378" Name="Ch1-T1" Description="">
@XmlAttribute(name = "IsActivated")
boolean IsActivated;
@XmlAttribute(name = "IsSelected")
boolean IsSelected;
@XmlAttribute(name = "ChannelSetupId")
String ChannelSetupId;
@XmlAttribute(name = "Name")
String Name;
@XmlAttribute(name = "Description")
String Description;
@XmlElement(name = "Color")
String Color;
// Missing:
//String exposure = getFirstNodeValue(channel, "ExposureTime");
//String gain = getFirstNodeValue(channel, "EMGain");
}
public static class Information {
/*Element information = (Element) informations.item(0);
Element image = getFirstNode(information, "Image");
Element user = getFirstNode(information, "User");
Element environment = getFirstNode(information, "Environment");
Element instrument = getFirstNode(information, "Instrument");
Element document = getFirstNode(information, "Document");*/
Image Image;
User User;
Instrument Instrument;
Document Document;
}
public static class Image {
@XmlElement(name = "ComponentBitCount")
int ComponentBitCount;
@XmlElement(name = "AcquisitionDateAndTime")
String AcquisitionDateAndTime;
ObjectiveSettings ObjectiveSettings;
/*<SizeX>70603</SizeX>
<SizeY>60036</SizeY>
<SizeZ>1</SizeZ>
<SizeT>1</SizeT>
<SizeS>11</SizeS>
<SizeH>1</SizeH>
<SizeM>16</SizeM>*/
@XmlElement(name = "SizeX")
int SizeX;
@XmlElement(name = "SizeY")
int SizeY;
@XmlElement(name = "SizeZ")
int SizeZ;
@XmlElement(name = "SizeT")
int SizeT;
@XmlElement(name = "SizeS")
int SizeS;
@XmlElement(name = "SizeH")
int SizeH;
@XmlElement(name = "SizeM")
int SizeM;
@XmlElement(name = "SizeC")
int SizeC;
Dimensions Dimensions;
}
public static class User {
}
public static class Instrument {
ArrayList<Microscope> Microscopes;
ArrayList<LightSource> LightSources;
ArrayList<DetectorInstrument> Detectors;
ArrayList<Objective> Objectives;
}
public static class Document {
}
public static class ObjectiveSettings {
@XmlElement(name = "RefractiveIndex")
double RefractiveIndex;
@XmlElement(name = "Medium")
public String Medium;
public ObjectiveRef ObjectiveRef;
}
public static class ObjectiveRef {
@XmlAttribute(name = "Id")
public String Id;
}
public static class MicroscopeRef {
@XmlAttribute(name = "Id")
public String Id;
}
public static class Dimensions {
@XmlElement(name = "Channels")
public ArrayList<ChannelDimension> Channels;
public X X;
public Y Y;
public T T;
public Z Z;
public S S;
}
public static class ChannelDimension {
//ArrayList<ChannelExperiment> Channels;
@XmlAttribute(name = "Id")
public String Id;
@XmlAttribute(name = "Name")
public String Name;
@XmlElement(name="ExcitationWavelength")
public double ExcitationWavelength;
@XmlElement(name="EmissionWavelength")
public double EmissionWavelength;
@XmlElement(name="Color")
public String Color;
@XmlElement(name="IlluminationType")
public String IlluminationType;
@XmlElement(name="ContrastMethod")
public String ContrastMethod;
@XmlElement(name="AcquisitionMode")
public String AcquisitionMode;
public DetectorSettings DetectorSettings;
public ArrayList<LightSourceSettings> LightSourcesSettings;
}
public class DetectorSettings {
@XmlElement(name = "HasFocusRoi")
public boolean HasFocusRoi;
@XmlElement(name = "DigitalGain")
public int DigitalGain;
@XmlElement(name = "Offset")
public int Offset;
@XmlElement(name = "DetectorMode")
public String DetectorMode;
@XmlElement(name = "Voltage")
public int Voltage;
@XmlElement(name = "PhotonConversionFactor")
public double PhotonConversionFactor;
public Detector Detector;
}
public static class LightSourceSettings {
@XmlElement(name = "Wavelength")
public int Wavelength;
}
public static class X {
@XmlElement(name = "AxisOrientation")
public int AxisOrientation;
}
public static class Y {
@XmlElement(name = "AxisOrientation")
public int AxisOrientation;
}
public static class T {
}
public static class Z {
}
public static class S {
public ArrayList<Scene> Scenes;
}
public static class Scene {
@XmlAttribute(name = "Index")
public int Index;
@XmlAttribute(name = "Name")
public String Name;
@XmlElement(name = "RegionId")
public long RegionId;
@XmlElement(name = "ScanMode")
public long ScanMode;
@XmlElement
public String CenterPosition;
@XmlElement
public String ContourSize;
@XmlElement
public String ContourType;
public Length getCenterPositionX() {
String[] pos = CenterPosition.split(",");
return new Length(DataTools.parseDouble(pos[0]), UNITS.MICROMETER);
}
public Length getCenterPositionY() {
String[] pos = CenterPosition.split(",");
return new Length(DataTools.parseDouble(pos[1]), UNITS.MICROMETER);
}
}
public static class Microscope {
@XmlAttribute(name = "Id")
public String Id;
@XmlAttribute(name = "Name")
public String Name;
@XmlElement(name = "UserDefinedName")
public String UserDefinedName;
@XmlElement(name = "System")
public String System;
}
public static class LightSource {
@XmlAttribute(name = "Id")
public String Id;
}
public static class DetectorInstrument { // Different name because it's used two times, maybe with different fields
/*<Detector Id="Detector: Spectral" Name="Spectral">
<GammaDefault>1</GammaDefault>
<Type>GaAsP-PMT</Type>
<Zoom>1.5,1.5</Zoom>
<Offset>-16.563032488441</Offset>
<Manufacturer>
<Model></Model>
</Manufacturer>
</Detector>*/
@XmlAttribute(name = "Id")
public String Id;
@XmlAttribute(name = "Name")
public String Name;
@XmlElement(name = "Type")
public String Type;
}
public static class Objective {
@XmlAttribute(name = "Id")
public String Id;
@XmlAttribute(name = "Name")
public String Name;
/*
<LensNA>1.4</LensNA>
<NominalMagnification>63</NominalMagnification>
<WorkingDistance>193</WorkingDistance>
<PupilGeometry>Circular</PupilGeometry>
<ImmersionRefractiveIndex>1.518</ImmersionRefractiveIndex>
<Immersion>Oil</Immersion>
*/
@XmlAttribute(name = "LensNA")
public double LensNA;
@XmlAttribute(name = "NominalMagnification")
public double NominalMagnification;
@XmlAttribute(name = "ImmersionRefractiveIndex")
public double ImmersionRefractiveIndex;
@XmlAttribute(name = "Immersion")
public String Immersion;
}
public static class Scaling {
/*
<Items>
<Distance Id="X">
<Value>7.059082892416223E-08</Value>
<DefaultUnitFormat>µm</DefaultUnitFormat>
</Distance>
<Distance Id="Y">
<Value>7.059082892416223E-08</Value>
<DefaultUnitFormat>µm</DefaultUnitFormat>
</Distance>
</Items>
*/
public ArrayList<Distance> Distances;
}
public static class Distance {
@XmlAttribute(name = "Id")
public String Id;
@XmlElement(name = "Value")
public double Value;
@XmlElement(name = "DefaultUnitFormat")
public String DefaultUnitFormat;
}
public static class DisplaySetting {
ArrayList<Channel> Channels;
}
public static class Channel {
@XmlAttribute(name = "Id")
public String Id;
@XmlAttribute(name = "Name")
public String Name;
@XmlElement(name = "BitCountRange")
public int BitCountRange;
@XmlElement(name = "PixelType")
public String PixelType;
@XmlElement(name = "Description")
public String Description;
@XmlElement(name = "DyeName")
public String DyeName;
@XmlElement(name = "IlluminationType")
public String IlluminationType;
@XmlElement(name = "Color")
public String Color;
/*
<BitCountRange>8</BitCountRange>
<PixelType>Gray8</PixelType>
<Description>DAPI</Description>
<DyeName>DAPI</DyeName>
<IlluminationType>Fluorescence</IlluminationType>
<DyeMaxEmission>465</DyeMaxEmission>
<DyeMaxExcitation>353</DyeMaxExcitation>
<DyeId>McNamara-Boswell-0434</DyeId>
<DyeDatabaseId>66071726-cbd4-4c41-b371-0a6eee4ae9c5</DyeDatabaseId>
<Color>#FFB3B3B3</Color>
<OriginalColor>#FFB3B3B3</OriginalColor>
*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment