Search in sources :

Example 21 with CoreMetadata

use of loci.formats.CoreMetadata in project bioformats by openmicroscopy.

the class OIRReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
    super.initFile(id);
    Location current = new Location(id).getAbsoluteFile();
    Location parent = current.getParentFile();
    ArrayList<String> extraFiles = new ArrayList<String>();
    if (parent != null) {
        String[] fileList = parent.list();
        Arrays.sort(fileList);
        String prefix = current.getName();
        if (prefix.indexOf(".") > 0) {
            prefix = prefix.substring(0, prefix.lastIndexOf("."));
        } else if (!checkSuffix(id, "oir")) {
            prefix = prefix.substring(0, prefix.lastIndexOf("_"));
        }
        for (String file : fileList) {
            // look for files ending with an underscore plus a 5 digit number
            if (file.startsWith(prefix + "_") && file.length() == prefix.length() + 6) {
                try {
                    int index = Integer.parseInt(file.substring(file.lastIndexOf("_") + 1));
                    if (index != extraFiles.size() + 1) {
                        LOGGER.warn("Found file {} with index {}; expected index {}", file, index, extraFiles.size() + 1);
                    }
                    extraFiles.add(new Location(parent, file).getAbsolutePath());
                } catch (NumberFormatException e) {
                }
            } else if (file.startsWith(prefix) && checkSuffix(file, "oir")) {
                current = new Location(parent, file);
                currentId = current.getAbsolutePath();
            }
        }
    }
    CoreMetadata m = core.get(0);
    m.littleEndian = true;
    m.sizeZ = 1;
    m.sizeC = 1;
    m.sizeT = 1;
    m.indexed = true;
    m.falseColor = true;
    try (RandomAccessInputStream s = new RandomAccessInputStream(currentId, BUFFER_SIZE)) {
        readPixelsFile(current.getAbsolutePath(), s);
    }
    for (String file : extraFiles) {
        try (RandomAccessInputStream s = new RandomAccessInputStream(file, BUFFER_SIZE)) {
            readPixelsFile(file, s);
        }
    }
    m.sizeC *= channels.size();
    m.imageCount = getSizeC() * getSizeZ() * getSizeT();
    LOGGER.debug("blocks per plane = {}", blocksPerPlane);
    LOGGER.debug("number of pixel blocks = {}", pixelBlocks.size());
    if (blocksPerPlane * getImageCount() != pixelBlocks.size()) {
        // if blocks are missing, set the Z and T dimensions to match the last
        // plane for which at least one block exists
        int maxZ = 0;
        int maxT = 0;
        for (String uid : pixelBlocks.keySet()) {
            LOGGER.debug("  checking uid = {}", uid);
            int thisZ = getZ(uid);
            int thisT = getT(uid);
            int block = getBlock(uid);
            // only count through last full plane
            if (block == blocksPerPlane - 1) {
                if (thisZ > maxZ) {
                    maxZ = thisZ;
                }
                if (thisT > maxT) {
                    maxT = thisT;
                }
                if (thisZ < minZ) {
                    minZ = thisZ;
                }
                if (thisT < minT) {
                    minT = thisT;
                }
            }
        }
        m.sizeZ = (maxZ - minZ) + 1;
        m.sizeT = (maxT - minT) + 1;
        m.imageCount = getSizeC() * getSizeZ() * getSizeT();
    } else {
        minZ = 0;
        minT = 0;
    }
    m.dimensionOrder = "XYC";
    if (getSizeZ() == 1 || getSizeT() == 1) {
        m.dimensionOrder += "ZT";
    } else {
        int zIndex = baseName.toLowerCase().indexOf("z");
        int tIndex = baseName.toLowerCase().indexOf("t");
        if (zIndex < tIndex) {
            m.dimensionOrder += "TZ";
        } else {
            m.dimensionOrder += "ZT";
        }
    }
    pixelUIDs = pixelBlocks.keySet().toArray(new String[pixelBlocks.size()]);
    Arrays.sort(pixelUIDs, new Comparator<String>() {

        @Override
        public int compare(String s1, String s2) {
            int lastUnderscore1 = s1.lastIndexOf("_");
            int lastUnderscore2 = s2.lastIndexOf("_");
            Integer block1 = new Integer(s1.substring(lastUnderscore1 + 1));
            Integer block2 = new Integer(s2.substring(lastUnderscore2 + 1));
            int underscore1 = s1.lastIndexOf("_", lastUnderscore1 - 1);
            int underscore2 = s2.lastIndexOf("_", lastUnderscore2 - 1);
            String prefix1 = s1.substring(0, underscore1);
            String prefix2 = s2.substring(0, underscore2);
            String channel1 = s1.substring(underscore1 + 1, lastUnderscore1);
            String channel2 = s2.substring(underscore2 + 1, lastUnderscore2);
            if (!prefix1.equals(prefix2)) {
                return s1.compareTo(s2);
            }
            if (!channel1.equals(channel2)) {
                Integer index1 = -1;
                Integer index2 = -2;
                for (int i = 0; i < channels.size(); i++) {
                    if (channels.get(i).id.equals(channel1)) {
                        index1 = i;
                    }
                    if (channels.get(i).id.equals(channel2)) {
                        index2 = i;
                    }
                }
                return index1.compareTo(index2);
            }
            return block1.compareTo(block2);
        }
    });
    if (LOGGER.isTraceEnabled()) {
        for (int i = 0; i < pixelUIDs.length; i++) {
            LOGGER.trace("pixel UID #{} = {}", i, pixelUIDs[i]);
        }
    }
    // populate original metadata
    Hashtable<String, Object> tmpMeta = new Hashtable<String, Object>();
    addMeta("Creation date", acquisitionDate, tmpMeta);
    addMeta("Pixel Length X", physicalSizeX, tmpMeta);
    addMeta("Pixel Length Y", physicalSizeY, tmpMeta);
    addMeta("Z step", physicalSizeZ, tmpMeta);
    for (Channel channel : channels) {
        String prefix = "Channel " + channel.name + " ";
        addMetaList(prefix + "ID", channel.id, tmpMeta);
        addMetaList(prefix + "color", channel.color, tmpMeta);
        addMetaList(prefix + "pinhole", channel.pinhole, tmpMeta);
        addMetaList(prefix + "start wavelength", channel.excitation, tmpMeta);
        addMetaList(prefix + "end wavelength", channel.emission, tmpMeta);
        addMetaList(prefix + "linked laser index", channel.laserIndex, tmpMeta);
    }
    for (Objective objective : objectives) {
        addMetaList("Objective Lens name", objective.name, tmpMeta);
        addMetaList("Objective Lens magnification", objective.magnification, tmpMeta);
        addMetaList("Objective Lens na", objective.na, tmpMeta);
        addMetaList("Objective Lens wd", objective.wd, tmpMeta);
        addMetaList("Objective Lens refractive index", objective.ri, tmpMeta);
        addMetaList("Objective Lens immersion", objective.immersion, tmpMeta);
    }
    for (Laser laser : lasers) {
        String prefix = "Laser " + laser.name + " ";
        addMetaList(prefix + "ID", laser.id, tmpMeta);
        addMetaList(prefix + "data ID", laser.dataId, tmpMeta);
        addMetaList(prefix + "power", laser.power, tmpMeta);
        addMetaList(prefix + "transmissivity", laser.transmissivity, tmpMeta);
        addMetaList(prefix + "wavelength", laser.wavelength, tmpMeta);
    }
    for (Detector detector : detectors) {
        addMetaList("Detector ID", detector.id, tmpMeta);
        addMetaList("Detector linked channel ID", detector.channelId, tmpMeta);
        addMetaList("Detector voltage", detector.voltage, tmpMeta);
        addMetaList("Detector offset", detector.offset, tmpMeta);
        addMetaList("Detector gain", detector.gain, tmpMeta);
    }
    // this ensures that global metadata keys will be sorted before series keys
    MetadataTools.merge(tmpMeta, metadata, "- ");
    // populate MetadataStore
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
    String instrumentID = MetadataTools.createLSID("Instrument", 0);
    store.setInstrumentID(instrumentID, 0);
    store.setImageInstrumentRef(instrumentID, 0);
    for (int i = 0; i < lasers.size(); i++) {
        Laser l = lasers.get(i);
        String lsid = MetadataTools.createLSID("LightSource", 0, i);
        store.setLaserID(lsid, 0, i);
        store.setLaserModel(l.id, 0, i);
        if (l.wavelength != null) {
            store.setLaserWavelength(FormatTools.getWavelength(l.wavelength, null), 0, i);
        }
    }
    for (int i = 0; i < detectors.size(); i++) {
        Detector detector = detectors.get(i);
        String lsid = MetadataTools.createLSID("Detector", 0, i);
        store.setDetectorID(lsid, 0, i);
        store.setDetectorOffset(detector.offset, 0, i);
        store.setDetectorVoltage(FormatTools.createElectricPotential(detector.voltage, UNITS.VOLT), 0, i);
        store.setDetectorGain(detector.gain, 0, i);
    }
    for (int i = 0; i < objectives.size(); i++) {
        Objective objective = objectives.get(i);
        String lsid = MetadataTools.createLSID("Objective", 0, i);
        store.setObjectiveID(lsid, 0, i);
        store.setObjectiveModel(objective.name, 0, i);
        store.setObjectiveNominalMagnification(objective.magnification, 0, i);
        store.setObjectiveLensNA(objective.na, 0, i);
        store.setObjectiveWorkingDistance(FormatTools.createLength(objective.wd, UNITS.MILLIMETRE), 0, i);
        store.setObjectiveImmersion(objective.immersion, 0, i);
        if (i == 0) {
            store.setObjectiveSettingsID(lsid, 0);
            store.setObjectiveSettingsRefractiveIndex(objective.ri, 0);
        }
    }
    if (acquisitionDate != null) {
        store.setImageAcquisitionDate(acquisitionDate, 0);
    }
    if (physicalSizeX != null) {
        store.setPixelsPhysicalSizeX(physicalSizeX, 0);
    }
    if (physicalSizeY != null) {
        store.setPixelsPhysicalSizeY(physicalSizeY, 0);
    }
    if (physicalSizeZ != null) {
        store.setPixelsPhysicalSizeZ(physicalSizeZ, 0);
    }
    for (int c = 0; c < channels.size(); c++) {
        Channel ch = channels.get(c);
        store.setChannelName(ch.name, 0, c);
        if (ch.color != null) {
            store.setChannelColor(ch.color, 0, c);
        }
        if (ch.pinhole != null) {
            store.setChannelPinholeSize(ch.pinhole, 0, c);
        }
        if (ch.emission != null) {
            store.setChannelEmissionWavelength(ch.emission, 0, c);
        }
        if (ch.excitation != null) {
            store.setChannelExcitationWavelength(ch.excitation, 0, c);
        }
        for (int d = 0; d < detectors.size(); d++) {
            if (detectors.get(d).channelId.equals(ch.id)) {
                store.setDetectorSettingsID(MetadataTools.createLSID("Detector", 0, d), 0, c);
            }
        }
        if (ch.laserIndex >= 0 && ch.laserIndex < lasers.size()) {
            String laserId = MetadataTools.createLSID("LightSource", 0, ch.laserIndex);
            store.setChannelLightSourceSettingsID(laserId, 0, c);
        }
    }
}
Also used : Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) CoreMetadata(loci.formats.CoreMetadata) MetadataStore(loci.formats.meta.MetadataStore) RandomAccessInputStream(loci.common.RandomAccessInputStream) Location(loci.common.Location)

Example 22 with CoreMetadata

use of loci.formats.CoreMetadata in project bioformats by openmicroscopy.

the class OIRReader method parseAxis.

private void parseAxis(Element dimensionAxis) {
    CoreMetadata m = core.get(0);
    Element axis = getFirstChild(dimensionAxis, "commonparam:axis");
    Element size = getFirstChild(dimensionAxis, "commonparam:maxSize");
    Element start = getFirstChild(dimensionAxis, "commonparam:startPosition");
    Element end = getFirstChild(dimensionAxis, "commonparam:endPosition");
    Element step = getFirstChild(dimensionAxis, "commonparam:step");
    if (axis != null && size != null) {
        String name = axis.getTextContent();
        if (name.equals("ZSTACK")) {
            if (m.sizeZ <= 1) {
                m.sizeZ = Integer.parseInt(size.getTextContent());
            }
        } else if (name.equals("TIMELAPSE")) {
            if (m.sizeT <= 1) {
                m.sizeT = Integer.parseInt(size.getTextContent());
            }
        } else if (name.equals("LAMBDA")) {
            m.sizeC = Integer.parseInt(size.getTextContent());
            m.moduloC.type = FormatTools.SPECTRA;
            m.moduloC.start = DataTools.parseDouble(start.getTextContent());
            m.moduloC.step = DataTools.parseDouble(step.getTextContent());
            // calculate the end point, as the stored value may be too large
            m.moduloC.end = m.moduloC.start + m.moduloC.step * (m.sizeC - 1);
        } else {
            LOGGER.warn("Unhandled axis '{}'", name);
        }
    }
}
Also used : Element(org.w3c.dom.Element) CoreMetadata(loci.formats.CoreMetadata)

Example 23 with CoreMetadata

use of loci.formats.CoreMetadata in project bioformats by openmicroscopy.

the class OIRReader method parseFrameProperties.

private void parseFrameProperties(Element root) throws FormatException {
    CoreMetadata m = core.get(0);
    Element imageDefinition = getFirstChild(root, "commonframe:imageDefinition");
    if (imageDefinition != null) {
        Element width = getFirstChild(imageDefinition, "base:width");
        Element height = getFirstChild(imageDefinition, "base:height");
        Element depth = getFirstChild(imageDefinition, "base:depth");
        Element bitCount = getFirstChild(imageDefinition, "base:bitCounts");
        if (width != null) {
            m.sizeX = Integer.parseInt(width.getTextContent());
        }
        if (height != null) {
            m.sizeY = Integer.parseInt(height.getTextContent());
        }
        if (depth != null) {
            int bytes = Integer.parseInt(depth.getTextContent());
            m.pixelType = FormatTools.pixelTypeFromBytes(bytes, false, false);
        }
        if (bitCount != null) {
            m.bitsPerPixel = Integer.parseInt(bitCount.getTextContent());
        }
    }
    Element general = getFirstChild(root, "commonframe:general");
    if (general != null) {
        Element name = getFirstChild(general, "base:name");
        if (name != null) {
            baseName = name.getTextContent();
        }
    }
}
Also used : Element(org.w3c.dom.Element) CoreMetadata(loci.formats.CoreMetadata)

Example 24 with CoreMetadata

use of loci.formats.CoreMetadata in project bioformats by openmicroscopy.

the class OpenlabRawReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
    super.initFile(id);
    in = new RandomAccessInputStream(id);
    // read the 12 byte file header
    LOGGER.info("Verifying Openlab RAW format");
    if (!in.readString(4).equals("OLRW")) {
        throw new FormatException("Openlab RAW magic string not found.");
    }
    LOGGER.info("Populating metadata");
    CoreMetadata m = core.get(0);
    int version = in.readInt();
    m.imageCount = in.readInt();
    offsets = new int[getImageCount()];
    offsets[0] = 12;
    in.skipBytes(8);
    m.sizeX = in.readInt();
    m.sizeY = in.readInt();
    in.skipBytes(1);
    m.sizeC = in.read();
    bytesPerPixel = in.read();
    in.skipBytes(1);
    long stampMs = in.readLong();
    if (stampMs > 0) {
        stampMs /= 1000000;
        stampMs -= (67 * 365.25 * 24 * 60 * 60);
    } else
        stampMs = System.currentTimeMillis();
    String stamp = DateTools.convertDate(stampMs, DateTools.UNIX);
    in.skipBytes(4);
    int len = in.read() & 0xff;
    String imageName = in.readString(len - 1).trim();
    if (getSizeC() <= 1)
        m.sizeC = 1;
    else
        m.sizeC = 3;
    int plane = getSizeX() * getSizeY() * bytesPerPixel;
    for (int i = 1; i < getImageCount(); i++) {
        offsets[i] = offsets[i - 1] + HEADER_SIZE + plane;
    }
    m.sizeZ = getImageCount();
    m.sizeT = 1;
    m.rgb = getSizeC() > 1;
    m.dimensionOrder = isRGB() ? "XYCZT" : "XYZTC";
    m.interleaved = false;
    m.littleEndian = false;
    m.metadataComplete = true;
    m.indexed = false;
    m.falseColor = false;
    switch(bytesPerPixel) {
        case 1:
        case 3:
            m.pixelType = FormatTools.UINT8;
            break;
        case 2:
            m.pixelType = FormatTools.UINT16;
            break;
        default:
            m.pixelType = FormatTools.FLOAT;
    }
    addGlobalMeta("Width", getSizeX());
    addGlobalMeta("Height", getSizeY());
    addGlobalMeta("Bytes per pixel", bytesPerPixel);
    addGlobalMeta("Image name", imageName);
    addGlobalMeta("Timestamp", stamp);
    addGlobalMeta("Version", version);
    // The metadata store we're working with.
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
    if (stamp != null) {
        store.setImageAcquisitionDate(new Timestamp(stamp), 0);
    }
    store.setImageName(imageName, 0);
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) RandomAccessInputStream(loci.common.RandomAccessInputStream) CoreMetadata(loci.formats.CoreMetadata) Timestamp(ome.xml.model.primitives.Timestamp) FormatException(loci.formats.FormatException)

Example 25 with CoreMetadata

use of loci.formats.CoreMetadata in project bioformats by openmicroscopy.

the class OpenlabReader method parseImageNames.

private void parseImageNames(int s) {
    final List<String> uniqueF = new ArrayList<String>();
    final List<String> uniqueC = new ArrayList<String>();
    final List<String> uniqueZ = new ArrayList<String>();
    final List<String> uniqueT = new ArrayList<String>();
    String[] axes = new String[] { "Z", "C", "T" };
    CoreMetadata m = core.get(s);
    m.dimensionOrder = "XY";
    for (PlaneInfo plane : planes) {
        if (plane == null || plane.series != s)
            continue;
        String name = plane.planeName;
        // check for a specific name format:
        // <channel name><optional Z section>_<plate>_<well>_<field>
        String[] tokens = name.split("_");
        boolean validField = false;
        try {
            Integer.parseInt(tokens[tokens.length - 1]);
            validField = true;
        } catch (NumberFormatException e) {
        }
        if (tokens.length == 4 && !tokens[0].toLowerCase().endsWith("xy") && validField) {
            specialPlateNames = true;
            if (!uniqueF.contains(tokens[3])) {
                uniqueF.add(tokens[3]);
            }
            plane.channelName = tokens[0];
            int endIndex = 0;
            while (endIndex < plane.channelName.length() && !Character.isDigit(plane.channelName.charAt(endIndex))) {
                endIndex++;
            }
            String zSection = plane.channelName.substring(endIndex);
            if (zSection.equals(""))
                zSection = "1";
            plane.channelName = plane.channelName.substring(0, endIndex);
            if (!uniqueC.contains(plane.channelName)) {
                uniqueC.add(plane.channelName);
            }
            if (!uniqueZ.contains(zSection)) {
                uniqueZ.add(zSection);
            }
            m.dimensionOrder = "XYCTZ";
            plane.wavelength = uniqueC.indexOf(plane.channelName);
            plane.timepoint = 0;
            plane.zPosition = uniqueZ.indexOf(zSection);
            plane.series = uniqueF.indexOf(tokens[3]);
        } else {
            for (String axis : axes) {
                List<String> unique = null;
                if (axis.equals("Z"))
                    unique = uniqueZ;
                else if (axis.equals("C"))
                    unique = uniqueC;
                else if (axis.equals("T"))
                    unique = uniqueT;
                int index = name.indexOf(axis + "=");
                if (index == -1)
                    index = name.indexOf(axis + " =");
                if (index != -1) {
                    int nextEqual = name.indexOf("=", index + 3);
                    if (nextEqual < 0) {
                        nextEqual = (int) Math.min(index + 3, name.length());
                    }
                    int end = name.lastIndexOf(" ", nextEqual);
                    if (end < index)
                        end = name.length();
                    String i = name.substring(name.indexOf("=", index), end);
                    if (!unique.contains(i)) {
                        unique.add(i);
                        if (unique.size() > 1 && m.dimensionOrder.indexOf(axis) == -1) {
                            m.dimensionOrder += axis;
                        }
                    }
                }
            }
        }
    }
    if (specialPlateNames) {
        m.sizeC *= uniqueC.size();
        m.sizeT = uniqueT.size();
        if (m.sizeT == 0)
            m.sizeT = 1;
        m.sizeZ = uniqueZ.size();
        if (m.sizeZ == 0)
            m.sizeZ = 1;
        m.imageCount = m.sizeC * m.sizeZ * m.sizeT;
        if (uniqueF.size() > core.size()) {
            CoreMetadata currentSeries = core.get(s);
            int seriesCount = uniqueF.size();
            core.clear();
            for (int i = 0; i < seriesCount; i++) {
                core.add(currentSeries);
            }
        }
        return;
    }
    // Just in case the resize changed it
    m = core.get(s);
    if (m.rgb && uniqueC.size() <= 1) {
        m.dimensionOrder = m.dimensionOrder.replaceAll("C", "");
        m.dimensionOrder = "XYC" + m.dimensionOrder.substring(2);
    }
    for (String axis : axes) {
        if (m.dimensionOrder.indexOf(axis) == -1) {
            m.dimensionOrder += axis;
        }
    }
    if (uniqueC.size() > 1) {
        m.sizeC *= uniqueC.size();
        m.sizeZ /= uniqueC.size();
    }
    if (uniqueT.size() > 1) {
        m.sizeT = uniqueT.size();
        m.sizeZ /= m.sizeT;
    }
    int newCount = getSizeZ() * getSizeT();
    if (!isRGB())
        newCount *= getSizeC();
    if (newCount < getImageCount()) {
        char firstAxis = getDimensionOrder().charAt(2);
        if (firstAxis == 'Z')
            m.sizeZ++;
        else if (firstAxis == 'C' && !isRGB())
            m.sizeC++;
        else
            m.sizeT++;
        m.imageCount = getSizeZ() * getSizeT();
        if (!isRGB())
            m.imageCount *= getSizeC();
    } else if (newCount > getImageCount())
        m.imageCount = newCount;
}
Also used : ArrayList(java.util.ArrayList) CoreMetadata(loci.formats.CoreMetadata)

Aggregations

CoreMetadata (loci.formats.CoreMetadata)211 MetadataStore (loci.formats.meta.MetadataStore)130 RandomAccessInputStream (loci.common.RandomAccessInputStream)108 FormatException (loci.formats.FormatException)87 Length (ome.units.quantity.Length)74 Location (loci.common.Location)55 ArrayList (java.util.ArrayList)50 Timestamp (ome.xml.model.primitives.Timestamp)44 IFD (loci.formats.tiff.IFD)33 Time (ome.units.quantity.Time)30 IOException (java.io.IOException)21 TiffParser (loci.formats.tiff.TiffParser)19 PhotoInterp (loci.formats.tiff.PhotoInterp)17 NonNegativeInteger (ome.xml.model.primitives.NonNegativeInteger)16 IFDList (loci.formats.tiff.IFDList)15 DependencyException (loci.common.services.DependencyException)14 ServiceFactory (loci.common.services.ServiceFactory)13 PositiveInteger (ome.xml.model.primitives.PositiveInteger)13 IniList (loci.common.IniList)9 HashMap (java.util.HashMap)8