Search in sources :

Example 6 with NonNegativeInteger

use of ome.xml.model.primitives.NonNegativeInteger in project bioformats by openmicroscopy.

the class ScanrReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
    super.initFile(id);
    if (metadataFiles.size() > 0) {
        // this dataset has already been initialized
        return;
    }
    // make sure we have the .xml file
    if (!checkSuffix(id, "xml") && isGroupFiles()) {
        Location parent = new Location(id).getAbsoluteFile().getParentFile();
        if (checkSuffix(id, "tif") && parent.getName().equalsIgnoreCase("Data")) {
            parent = parent.getParentFile();
        }
        String[] list = parent.list();
        for (String file : list) {
            if (file.equals(XML_FILE)) {
                id = new Location(parent, file).getAbsolutePath();
                super.initFile(id);
                break;
            }
        }
        if (!checkSuffix(id, "xml")) {
            throw new FormatException("Could not find " + XML_FILE + " in " + parent.getAbsolutePath());
        }
    } else if (!isGroupFiles() && checkSuffix(id, "tif")) {
        TiffReader r = new TiffReader();
        r.setMetadataStore(getMetadataStore());
        r.setId(id);
        core = new ArrayList<CoreMetadata>(r.getCoreMetadataList());
        metadataStore = r.getMetadataStore();
        final Map<String, Object> globalMetadata = r.getGlobalMetadata();
        for (final Map.Entry<String, Object> entry : globalMetadata.entrySet()) {
            addGlobalMeta(entry.getKey(), entry.getValue());
        }
        r.close();
        tiffs = new String[] { id };
        reader = new MinimalTiffReader();
        return;
    }
    Location dir = new Location(id).getAbsoluteFile().getParentFile();
    String[] list = dir.list(true);
    for (String file : list) {
        Location f = new Location(dir, file);
        if (checkSuffix(file, METADATA_SUFFIXES) && !f.isDirectory()) {
            metadataFiles.add(f.getAbsolutePath());
        }
    }
    // parse XML metadata
    String xml = DataTools.readFile(id).trim();
    if (xml.startsWith("<?")) {
        xml = xml.substring(xml.indexOf("?>") + 2);
    }
    xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" + xml;
    XMLTools.parseXML(xml, new ScanrHandler());
    final List<String> uniqueRows = new ArrayList<String>();
    final List<String> uniqueColumns = new ArrayList<String>();
    if (wellRows == 0 || wellColumns == 0) {
        for (String well : wellLabels.keySet()) {
            if (!Character.isLetter(well.charAt(0)))
                continue;
            String row = well.substring(0, 1).trim();
            String column = well.substring(1).trim();
            if (!uniqueRows.contains(row) && row.length() > 0)
                uniqueRows.add(row);
            if (!uniqueColumns.contains(column) && column.length() > 0) {
                uniqueColumns.add(column);
            }
        }
        wellRows = uniqueRows.size();
        wellColumns = uniqueColumns.size();
        if (wellRows * wellColumns != wellCount) {
            adjustWellDimensions();
        }
    }
    int nChannels = getSizeC() == 0 ? channelNames.size() : (int) Math.min(channelNames.size(), getSizeC());
    if (nChannels == 0)
        nChannels = 1;
    int nSlices = getSizeZ() == 0 ? 1 : getSizeZ();
    int nTimepoints = getSizeT();
    int nWells = wellCount;
    int nPos = 0;
    if (foundPositions)
        nPos = fieldPositionX.length;
    else
        nPos = fieldRows * fieldColumns;
    if (nPos == 0)
        nPos = 1;
    // get list of TIFF files
    Location dataDir = new Location(dir, "data");
    list = dataDir.list(true);
    if (list == null) {
        // try to find the TIFFs in the current directory
        list = dir.list(true);
    } else
        dir = dataDir;
    if (nTimepoints == 0 || list.length < nTimepoints * nChannels * nSlices * nWells * nPos) {
        nTimepoints = list.length / (nChannels * nWells * nPos * nSlices);
        if (nTimepoints == 0)
            nTimepoints = 1;
    }
    tiffs = new String[nChannels * nWells * nPos * nTimepoints * nSlices];
    Arrays.sort(list, new Comparator<String>() {

        @Override
        public int compare(String s1, String s2) {
            int lastSeparator1 = s1.lastIndexOf(File.separator) + 1;
            int lastSeparator2 = s2.lastIndexOf(File.separator) + 1;
            String dir1 = s1.substring(0, lastSeparator1);
            String dir2 = s2.substring(0, lastSeparator2);
            if (!dir1.equals(dir2)) {
                return dir1.compareTo(dir2);
            }
            int dash1 = s1.indexOf("-", lastSeparator1);
            int dash2 = s2.indexOf("-", lastSeparator2);
            String label1 = dash1 < 0 ? "" : s1.substring(lastSeparator1, dash1);
            String label2 = dash2 < 0 ? "" : s2.substring(lastSeparator2, dash2);
            if (label1.equals(label2)) {
                String remainder1 = dash1 < 0 ? s1 : s1.substring(dash1);
                String remainder2 = dash2 < 0 ? s2 : s2.substring(dash2);
                return remainder1.compareTo(remainder2);
            }
            Integer index1 = wellLabels.get(label1);
            Integer index2 = wellLabels.get(label2);
            if (index1 == null && index2 != null) {
                return 1;
            } else if (index1 != null && index2 == null) {
                return -1;
            }
            return index1.compareTo(index2);
        }
    });
    int lastListIndex = 0;
    int next = 0;
    String[] keys = wellLabels.keySet().toArray(new String[wellLabels.size()]);
    Arrays.sort(keys, new Comparator<String>() {

        @Override
        public int compare(String s1, String s2) {
            char row1 = s1.charAt(0);
            char row2 = s2.charAt(0);
            final Integer col1 = new Integer(s1.substring(1));
            final Integer col2 = new Integer(s2.substring(1));
            if (row1 < row2) {
                return -1;
            } else if (row1 > row2) {
                return 1;
            }
            return col1.compareTo(col2);
        }
    });
    int realPosCount = 0;
    for (int well = 0; well < nWells; well++) {
        int missingWellFiles = 0;
        int wellIndex = wellNumbers.get(well);
        String wellPos = getBlock(wellIndex, "W");
        int originalIndex = next;
        for (int pos = 0; pos < nPos; pos++) {
            String posPos = getBlock(pos + 1, "P");
            int posIndex = next;
            for (int z = 0; z < nSlices; z++) {
                String zPos = getBlock(z, "Z");
                for (int t = 0; t < nTimepoints; t++) {
                    String tPos = getBlock(t, "T");
                    for (int c = 0; c < nChannels; c++) {
                        for (int i = lastListIndex; i < list.length; i++) {
                            String file = list[i];
                            if (file.indexOf(wellPos) != -1 && file.indexOf(zPos) != -1 && file.indexOf(posPos) != -1 && file.indexOf(tPos) != -1 && file.indexOf(channelNames.get(c)) != -1) {
                                tiffs[next++] = new Location(dir, file).getAbsolutePath();
                                if (c == nChannels - 1) {
                                    lastListIndex = i;
                                }
                                break;
                            }
                        }
                        if (next == originalIndex) {
                            missingWellFiles++;
                        }
                    }
                }
            }
            if (posIndex != next)
                realPosCount++;
        }
        if (next == originalIndex && well < keys.length) {
            wellLabels.remove(keys[well]);
        }
        if (next == originalIndex && missingWellFiles == nSlices * nTimepoints * nChannels * nPos) {
            wellNumbers.remove(well);
        }
    }
    nWells = wellNumbers.size();
    if (wellLabels.size() > 0 && wellLabels.size() != nWells) {
        uniqueRows.clear();
        uniqueColumns.clear();
        for (String well : wellLabels.keySet()) {
            if (!Character.isLetter(well.charAt(0)))
                continue;
            String row = well.substring(0, 1).trim();
            String column = well.substring(1).trim();
            if (!uniqueRows.contains(row) && row.length() > 0)
                uniqueRows.add(row);
            if (!uniqueColumns.contains(column) && column.length() > 0) {
                uniqueColumns.add(column);
            }
        }
        nWells = uniqueRows.size() * uniqueColumns.size();
        adjustWellDimensions();
    }
    if (realPosCount < nPos) {
        nPos = realPosCount;
    }
    reader = new MinimalTiffReader();
    reader.setId(tiffs[0]);
    int sizeX = reader.getSizeX();
    int sizeY = reader.getSizeY();
    int pixelType = reader.getPixelType();
    tileWidth = reader.getOptimalTileWidth();
    tileHeight = reader.getOptimalTileHeight();
    switch(pixelType) {
        case FormatTools.INT8:
            pixelType = FormatTools.UINT8;
            break;
        case FormatTools.INT16:
            pixelType = FormatTools.UINT16;
            break;
    }
    boolean rgb = reader.isRGB();
    boolean interleaved = reader.isInterleaved();
    boolean indexed = reader.isIndexed();
    boolean littleEndian = reader.isLittleEndian();
    reader.close();
    int seriesCount = nWells * nPos;
    core.clear();
    for (int i = 0; i < seriesCount; i++) {
        CoreMetadata ms = new CoreMetadata();
        core.add(ms);
        ms.sizeC = nChannels;
        ms.sizeZ = nSlices;
        ms.sizeT = nTimepoints;
        ms.sizeX = sizeX;
        ms.sizeY = sizeY;
        ms.pixelType = pixelType;
        ms.rgb = rgb;
        ms.interleaved = interleaved;
        ms.indexed = indexed;
        ms.littleEndian = littleEndian;
        ms.dimensionOrder = "XYCTZ";
        ms.imageCount = nSlices * nTimepoints * nChannels;
        ms.bitsPerPixel = 12;
    }
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
    store.setPlateID(MetadataTools.createLSID("Plate", 0), 0);
    store.setPlateColumns(new PositiveInteger(wellColumns), 0);
    store.setPlateRows(new PositiveInteger(wellRows), 0);
    String plateAcqID = MetadataTools.createLSID("PlateAcquisition", 0, 0);
    store.setPlateAcquisitionID(plateAcqID, 0, 0);
    int nFields = 0;
    if (foundPositions) {
        nFields = fieldPositionX.length;
    } else {
        nFields = fieldRows * fieldColumns;
    }
    PositiveInteger fieldCount = FormatTools.getMaxFieldCount(nFields);
    if (fieldCount != null) {
        store.setPlateAcquisitionMaximumFieldCount(fieldCount, 0, 0);
    }
    for (int i = 0; i < getSeriesCount(); i++) {
        int field = i % nFields;
        int well = i / nFields;
        int index = well;
        while (wellNumbers.get(index) == null && index < wellNumbers.size()) {
            index++;
        }
        int wellIndex = wellNumbers.get(index) == null ? index : wellNumbers.get(index) - 1;
        int wellRow = wellIndex / wellColumns;
        int wellCol = wellIndex % wellColumns;
        if (field == 0) {
            store.setWellID(MetadataTools.createLSID("Well", 0, well), 0, well);
            store.setWellColumn(new NonNegativeInteger(wellCol), 0, well);
            store.setWellRow(new NonNegativeInteger(wellRow), 0, well);
        }
        String wellSample = MetadataTools.createLSID("WellSample", 0, well, field);
        store.setWellSampleID(wellSample, 0, well, field);
        store.setWellSampleIndex(new NonNegativeInteger(i), 0, well, field);
        String imageID = MetadataTools.createLSID("Image", i);
        store.setWellSampleImageRef(imageID, 0, well, field);
        store.setImageID(imageID, i);
        String name = "Well " + (well + 1) + ", Field " + (field + 1) + " (Spot " + (i + 1) + ")";
        store.setImageName(name, i);
        store.setPlateAcquisitionWellSampleRef(wellSample, 0, 0, i);
    }
    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        for (int i = 0; i < getSeriesCount(); i++) {
            for (int c = 0; c < getSizeC(); c++) {
                store.setChannelName(channelNames.get(c), i, c);
            }
            Length x = FormatTools.getPhysicalSizeX(pixelSize);
            Length y = FormatTools.getPhysicalSizeY(pixelSize);
            if (x != null) {
                store.setPixelsPhysicalSizeX(x, i);
            }
            if (y != null) {
                store.setPixelsPhysicalSizeY(y, i);
            }
            if (fieldPositionX != null && fieldPositionY != null) {
                int field = i % nFields;
                int well = i / nFields;
                final Length posX = fieldPositionX[field];
                final Length posY = fieldPositionY[field];
                store.setWellSamplePositionX(posX, 0, well, field);
                store.setWellSamplePositionY(posY, 0, well, field);
                for (int c = 0; c < getSizeC(); c++) {
                    int image = getIndex(0, c, 0);
                    store.setPlaneTheZ(new NonNegativeInteger(0), i, image);
                    store.setPlaneTheC(new NonNegativeInteger(c), i, image);
                    store.setPlaneTheT(new NonNegativeInteger(0), i, image);
                    store.setPlanePositionX(fieldPositionX[field], i, image);
                    store.setPlanePositionY(fieldPositionY[field], i, image);
                    // exposure time is stored in milliseconds
                    // convert to seconds before populating MetadataStore
                    Double time = exposures.get(c);
                    if (time != null) {
                        time /= 1000;
                        store.setPlaneExposureTime(new Time(time, UNITS.SECOND), i, image);
                    }
                    if (deltaT != null) {
                        store.setPlaneDeltaT(new Time(deltaT, UNITS.SECOND), i, image);
                    }
                }
            }
        }
        String row = wellRows > 26 ? "Number" : "Letter";
        String col = wellRows > 26 ? "Letter" : "Number";
        store.setPlateRowNamingConvention(getNamingConvention(row), 0);
        store.setPlateColumnNamingConvention(getNamingConvention(col), 0);
        store.setPlateName(plateName, 0);
    }
}
Also used : PositiveInteger(ome.xml.model.primitives.PositiveInteger) NonNegativeInteger(ome.xml.model.primitives.NonNegativeInteger) ArrayList(java.util.ArrayList) Time(ome.units.quantity.Time) CoreMetadata(loci.formats.CoreMetadata) FormatException(loci.formats.FormatException) PositiveInteger(ome.xml.model.primitives.PositiveInteger) NonNegativeInteger(ome.xml.model.primitives.NonNegativeInteger) MetadataStore(loci.formats.meta.MetadataStore) Length(ome.units.quantity.Length) HashMap(java.util.HashMap) Map(java.util.Map) Location(loci.common.Location)

Example 7 with NonNegativeInteger

use of ome.xml.model.primitives.NonNegativeInteger in project bioformats by openmicroscopy.

the class ObjectBasedOMEModelMock method makePlate.

private Plate makePlate() {
    Plate plate = new Plate();
    plate.setID(InOutCurrentTest.PLATE_ID);
    plate.setRows(InOutCurrentTest.WELL_ROWS);
    plate.setColumns(InOutCurrentTest.WELL_COLS);
    plate.setRowNamingConvention(InOutCurrentTest.WELL_ROW);
    plate.setColumnNamingConvention(InOutCurrentTest.WELL_COL);
    TimestampAnnotation plateAnnotation = new TimestampAnnotation();
    plateAnnotation.setID(InOutCurrentTest.PLATE_ANNOTATION_ID);
    plateAnnotation.setValue(new Timestamp(InOutCurrentTest.PLATE_ANNOTATION_VALUE));
    plateAnnotation.setNamespace(InOutCurrentTest.GENERAL_ANNOTATION_NAMESPACE);
    plate.linkAnnotation(plateAnnotation);
    annotations.addTimestampAnnotation(plateAnnotation);
    int wellSampleIndex = 0;
    for (int row = 0; row < InOutCurrentTest.WELL_ROWS.getValue(); row++) {
        for (int col = 0; col < InOutCurrentTest.WELL_COLS.getValue(); col++) {
            Well well = new Well();
            well.setID(String.format("Well:%d_%d", row, col));
            well.setRow(new NonNegativeInteger(row));
            well.setColumn(new NonNegativeInteger(col));
            if (row == 0 && col == 0) {
                LongAnnotation annotation = new LongAnnotation();
                annotation.setID(InOutCurrentTest.WELL_ANNOTATION_ID);
                annotation.setValue(InOutCurrentTest.WELL_ANNOTATION_VALUE);
                annotation.setNamespace(InOutCurrentTest.GENERAL_ANNOTATION_NAMESPACE);
                well.linkAnnotation(annotation);
                annotations.addLongAnnotation(annotation);
            }
            WellSample sample = new WellSample();
            sample.setID(String.format("WellSample:%d_%d", row, col));
            sample.setIndex(new NonNegativeInteger(wellSampleIndex));
            sample.linkImage(ome.getImage(0));
            well.addWellSample(sample);
            plate.addWell(well);
            wellSampleIndex++;
        }
    }
    return plate;
}
Also used : TimestampAnnotation(ome.xml.model.TimestampAnnotation) NonNegativeInteger(ome.xml.model.primitives.NonNegativeInteger) Well(ome.xml.model.Well) LongAnnotation(ome.xml.model.LongAnnotation) WellSample(ome.xml.model.WellSample) Timestamp(ome.xml.model.primitives.Timestamp) Plate(ome.xml.model.Plate)

Example 8 with NonNegativeInteger

use of ome.xml.model.primitives.NonNegativeInteger in project bioformats by openmicroscopy.

the class ColumbusReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
protected void initFile(String id) throws FormatException, IOException {
    Location xml = findXML(id);
    if (null == xml) {
        throw new FormatException("Could not find " + XML_FILE);
    }
    id = xml.getAbsolutePath();
    super.initFile(id);
    Location parent = new Location(currentId).getAbsoluteFile().getParentFile();
    // parse plate layout and image dimensions from the XML files
    String xmlData = DataTools.readFile(id);
    MeasurementHandler handler = new MeasurementHandler();
    XMLTools.parseXML(xmlData, handler);
    String[] parentDirectories = parent.list(true);
    Arrays.sort(parentDirectories);
    ArrayList<String> timepointDirs = new ArrayList<String>();
    for (String file : parentDirectories) {
        Location absFile = new Location(parent, file);
        if (absFile.isDirectory()) {
            timepointDirs.add(absFile.getAbsolutePath());
            for (String f : absFile.list(true)) {
                if (!checkSuffix(f, "tif")) {
                    if (!metadataFiles.contains(file + File.separator + f)) {
                        metadataFiles.add(file + File.separator + f);
                    }
                }
            }
        }
    }
    for (int i = 0; i < metadataFiles.size(); i++) {
        String metadataFile = metadataFiles.get(i);
        int end = metadataFile.indexOf(File.separator);
        String timepointPath = end < 0 ? "" : parent + File.separator + metadataFile.substring(0, end);
        Location f = new Location(parent + File.separator + metadataFile);
        if (!f.exists()) {
            metadataFile = metadataFile.substring(end + 1);
            f = new Location(parent, metadataFile);
        }
        String path = f.getAbsolutePath();
        metadataFiles.set(i, path);
        if (checkSuffix(path, "columbusidx.xml")) {
            int timepoint = timepointDirs.indexOf(timepointPath);
            if (timepointDirs.size() == 0) {
                timepoint = 0;
            }
            parseImageXML(path, timepoint);
        }
    }
    // process plane list to determine plate size
    Comparator<Plane> planeComp = new Comparator<Plane>() {

        public int compare(Plane p1, Plane p2) {
            if (p1.row != p2.row) {
                return p1.row - p2.row;
            }
            if (p1.col != p2.col) {
                return p1.col - p2.col;
            }
            if (p1.field != p2.field) {
                return p1.field - p2.field;
            }
            if (p1.timepoint != p2.timepoint) {
                return p1.timepoint - p2.timepoint;
            }
            if (p1.channel != p2.channel) {
                return p1.channel - p2.channel;
            }
            return 0;
        }
    };
    Plane[] tmpPlanes = planes.toArray(new Plane[planes.size()]);
    Arrays.sort(tmpPlanes, planeComp);
    planes.clear();
    reader = new MinimalTiffReader();
    reader.setId(tmpPlanes[0].file);
    core = reader.getCoreMetadataList();
    CoreMetadata m = core.get(0);
    m.sizeC = 0;
    m.sizeT = 0;
    ArrayList<Integer> uniqueSamples = new ArrayList<Integer>();
    ArrayList<Integer> uniqueRows = new ArrayList<Integer>();
    ArrayList<Integer> uniqueCols = new ArrayList<Integer>();
    for (Plane p : tmpPlanes) {
        planes.add(p);
        int sampleIndex = p.row * handler.getPlateColumns() + p.col;
        if (!uniqueSamples.contains(sampleIndex)) {
            uniqueSamples.add(sampleIndex);
        }
        if (!uniqueRows.contains(p.row)) {
            uniqueRows.add(p.row);
        }
        if (!uniqueCols.contains(p.col)) {
            uniqueCols.add(p.col);
        }
        // counts are assumed to be non-sparse
        if (p.field >= nFields) {
            nFields = p.field + 1;
        }
        if (p.channel >= getSizeC()) {
            m.sizeC = p.channel + 1;
        }
        if (p.timepoint >= getSizeT()) {
            m.sizeT = p.timepoint + 1;
        }
    }
    m.sizeZ = 1;
    m.imageCount = getSizeZ() * getSizeC() * getSizeT();
    m.dimensionOrder = "XYCTZ";
    m.rgb = false;
    int seriesCount = uniqueSamples.size() * nFields;
    for (int i = 1; i < seriesCount; i++) {
        core.add(m);
    }
    // populate the MetadataStore
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this, true);
    store.setScreenID(MetadataTools.createLSID("Screen", 0), 0);
    store.setScreenName(handler.getScreenName(), 0);
    store.setPlateID(MetadataTools.createLSID("Plate", 0), 0);
    store.setPlateName(handler.getPlateName(), 0);
    store.setPlateRows(new PositiveInteger(handler.getPlateRows()), 0);
    store.setPlateColumns(new PositiveInteger(handler.getPlateColumns()), 0);
    String imagePrefix = handler.getPlateName() + " Well ";
    int wellSample = 0;
    int nextWell = -1;
    Timestamp date = new Timestamp(acquisitionDate);
    long timestampSeconds = date.asInstant().getMillis() / 1000;
    for (Integer row : uniqueRows) {
        for (Integer col : uniqueCols) {
            if (!uniqueSamples.contains(row * handler.getPlateColumns() + col)) {
                continue;
            }
            nextWell++;
            store.setWellID(MetadataTools.createLSID("Well", 0, nextWell), 0, nextWell);
            store.setWellRow(new NonNegativeInteger(row), 0, nextWell);
            store.setWellColumn(new NonNegativeInteger(col), 0, nextWell);
            for (int field = 0; field < nFields; field++) {
                Plane p = lookupPlane(row, col, field, 0, 0);
                String wellSampleID = MetadataTools.createLSID("WellSample", 0, nextWell, field);
                store.setWellSampleID(wellSampleID, 0, nextWell, field);
                store.setWellSampleIndex(new NonNegativeInteger(wellSample), 0, nextWell, field);
                if (p != null) {
                    store.setWellSamplePositionX(new Length(p.positionX, UNITS.REFERENCEFRAME), 0, nextWell, field);
                    store.setWellSamplePositionY(new Length(p.positionY, UNITS.REFERENCEFRAME), 0, nextWell, field);
                }
                String imageID = MetadataTools.createLSID("Image", wellSample);
                store.setImageID(imageID, wellSample);
                store.setWellSampleImageRef(imageID, 0, nextWell, field);
                store.setImageName(imagePrefix + (char) (row + 'A') + (col + 1) + " Field #" + (field + 1), wellSample);
                store.setImageAcquisitionDate(date, wellSample);
                if (p != null) {
                    p.series = wellSample;
                    store.setPixelsPhysicalSizeX(FormatTools.getPhysicalSizeX(p.sizeX), p.series);
                    store.setPixelsPhysicalSizeY(FormatTools.getPhysicalSizeY(p.sizeY), p.series);
                    for (int c = 0; c < getSizeC(); c++) {
                        p = lookupPlane(row, col, field, 0, c);
                        if (p != null) {
                            p.series = wellSample;
                            store.setChannelName(p.channelName, p.series, p.channel);
                            if ((int) p.emWavelength > 0) {
                                store.setChannelEmissionWavelength(FormatTools.getEmissionWavelength(p.emWavelength), p.series, p.channel);
                            }
                            if ((int) p.exWavelength > 0) {
                                store.setChannelExcitationWavelength(FormatTools.getExcitationWavelength(p.exWavelength), p.series, p.channel);
                            }
                            store.setChannelColor(p.channelColor, p.series, p.channel);
                        }
                        for (int t = 0; t < getSizeT(); t++) {
                            p = lookupPlane(row, col, field, t, c);
                            if (p != null) {
                                p.series = wellSample;
                                store.setPlaneDeltaT(new Time(p.deltaT - timestampSeconds, UNITS.SECOND), p.series, getIndex(0, c, t));
                            }
                        }
                    }
                }
                wellSample++;
            }
        }
    }
}
Also used : PositiveInteger(ome.xml.model.primitives.PositiveInteger) NonNegativeInteger(ome.xml.model.primitives.NonNegativeInteger) ArrayList(java.util.ArrayList) Time(ome.units.quantity.Time) CoreMetadata(loci.formats.CoreMetadata) Timestamp(ome.xml.model.primitives.Timestamp) FormatException(loci.formats.FormatException) Comparator(java.util.Comparator) PositiveInteger(ome.xml.model.primitives.PositiveInteger) NonNegativeInteger(ome.xml.model.primitives.NonNegativeInteger) MetadataStore(loci.formats.meta.MetadataStore) Length(ome.units.quantity.Length) Location(loci.common.Location)

Example 9 with NonNegativeInteger

use of ome.xml.model.primitives.NonNegativeInteger in project bioformats by openmicroscopy.

the class CellH5Reader method parseStructure.

private void parseStructure() throws FormatException {
    seriesCount = 0;
    core.clear();
    // read experiment structure and collect coordinates
    String path_to_plate = CellH5Constants.PREFIX_PATH + CellH5Constants.PLATE;
    LOGGER.info("Plate :" + path_to_plate);
    for (String plate : jhdf.getMember(path_to_plate)) {
        String path_to_well = path_to_plate + plate + CellH5Constants.WELL;
        LOGGER.info("Well :" + path_to_well);
        for (String well : jhdf.getMember(path_to_well)) {
            String path_to_site = path_to_well + well + CellH5Constants.SITE;
            LOGGER.info("Site :" + path_to_site);
            for (String site : jhdf.getMember(path_to_site)) {
                CellH5PositionList.add(new CellH5Coordinate(plate, well, site));
            }
        }
    }
    if (CellH5PositionList.size() == 0) {
        throw new FormatException("No series found in file...");
    }
    List<String> seriesNames = new ArrayList<String>();
    List<String> seriesPlate = new ArrayList<String>();
    List<String> seriesWell = new ArrayList<String>();
    List<String> seriesSite = new ArrayList<String>();
    for (CellH5Coordinate coord : CellH5PositionList) {
        if (jhdf.exists(coord.pathToImageData)) {
            CoreMetadata m = new CoreMetadata();
            core.add(m);
            setSeries(seriesCount);
            LOGGER.debug(coord.pathToImageData);
            int[] ctzyx = jhdf.getShape(coord.pathToImageData);
            m.sizeC = ctzyx[0];
            m.sizeT = ctzyx[1];
            m.sizeZ = ctzyx[2];
            m.sizeY = ctzyx[3];
            m.sizeX = ctzyx[4];
            m.resolutionCount = 1;
            m.thumbnail = false;
            m.imageCount = getSizeC() * getSizeT() * getSizeZ();
            m.dimensionOrder = "XYZTC";
            m.rgb = false;
            m.thumbSizeX = 128;
            m.thumbSizeY = 128;
            m.orderCertain = false;
            m.littleEndian = true;
            m.interleaved = false;
            m.indexed = true;
            int bpp = jhdf.getElementSize(coord.pathToImageData);
            if (bpp == 1) {
                m.pixelType = FormatTools.UINT8;
            } else if (bpp == 2) {
                m.pixelType = FormatTools.UINT16;
            } else if (bpp == 4) {
                m.pixelType = FormatTools.INT32;
            } else {
                throw new FormatException("Pixel type not understood. Only 8, " + "16 and 32 bit images supported");
            }
            seriesNames.add(String.format("P_%s, W_%s_%s", coord.plate, coord.well, coord.site));
            seriesPlate.add(coord.plate);
            seriesWell.add(coord.well);
            seriesSite.add(coord.site);
            CellH5PathsToImageData.add(coord.pathToImageData);
            seriesCount++;
        }
    }
    for (CellH5Coordinate coord : CellH5PositionList) {
        if (jhdf.exists(coord.pathToSegmentationData)) {
            CoreMetadata m = new CoreMetadata();
            core.add(m);
            setSeries(seriesCount);
            LOGGER.debug(coord.pathToSegmentationData);
            int[] ctzyx = jhdf.getShape(coord.pathToSegmentationData);
            m.sizeC = ctzyx[0];
            m.sizeT = ctzyx[1];
            m.sizeZ = ctzyx[2];
            m.sizeY = ctzyx[3];
            m.sizeX = ctzyx[4];
            m.resolutionCount = 1;
            m.thumbnail = false;
            m.imageCount = getSizeC() * getSizeT() * getSizeZ();
            m.dimensionOrder = "XYZTC";
            m.rgb = false;
            m.thumbSizeX = 128;
            m.thumbSizeY = 128;
            m.orderCertain = false;
            m.littleEndian = true;
            m.interleaved = false;
            m.indexed = true;
            int bpp = jhdf.getElementSize(coord.pathToSegmentationData);
            if (bpp == 1) {
                m.pixelType = FormatTools.UINT8;
            } else if (bpp == 2) {
                m.pixelType = FormatTools.UINT16;
            } else if (bpp == 4) {
                m.pixelType = FormatTools.INT32;
            } else {
                throw new FormatException("Pixel type not understood. Only 8, " + "16 and 32 bit images supported");
            }
            seriesNames.add(String.format("P_%s, W_%s_%s label image", coord.plate, coord.well, coord.site));
            seriesPlate.add(coord.plate);
            seriesWell.add(coord.well);
            seriesSite.add(coord.site);
            CellH5PathsToImageData.add(coord.pathToSegmentationData);
            seriesCount++;
        }
    }
    if (seriesCount == 0) {
        throw new FormatException("No image data found...");
    }
    store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
    for (int s = 0; s < seriesNames.size(); s++) {
        String image_id = MetadataTools.createLSID("Image", s);
        store.setImageName(seriesNames.get(s), s);
        String plate_id = MetadataTools.createLSID("Plate", 0);
        store.setPlateID(plate_id, 0);
        store.setPlateName(seriesPlate.get(s), 0);
        String well_id = MetadataTools.createLSID("Well", 0);
        store.setWellID(well_id, 0, 0);
        String cellh5WellCoord = seriesWell.get(s);
        String wellRowLetter = cellh5WellCoord.substring(0, 1);
        String wellColNumber = cellh5WellCoord.substring(1);
        int wellRowLetterIndex = "ABCDEFGHIJKLMNOP".indexOf(wellRowLetter);
        int wellColNumberIndex = -1;
        try {
            wellColNumberIndex = Integer.parseInt(wellColNumber);
        } catch (NumberFormatException e) {
        // 
        }
        if (wellRowLetterIndex > -1 && wellColNumberIndex > 0) {
            store.setWellRow(new NonNegativeInteger(wellRowLetterIndex), 0, 0);
            store.setWellColumn(new NonNegativeInteger(wellColNumberIndex - 1), 0, 0);
        } else {
            store.setWellRow(new NonNegativeInteger(0), 0, 0);
            store.setWellColumn(new NonNegativeInteger(0), 0, 0);
        }
        store.setWellExternalIdentifier(cellh5WellCoord, 0, 0);
        String site_id = MetadataTools.createLSID("WellSample", 0);
        store.setWellSampleID(site_id, 0, 0, 0);
        store.setWellSampleIndex(NonNegativeInteger.valueOf(seriesSite.get(s)), 0, 0, 0);
        store.setWellSampleImageRef(image_id, 0, 0, 0);
    }
    setSeries(0);
    parseCellObjects();
}
Also used : NonNegativeInteger(ome.xml.model.primitives.NonNegativeInteger) ArrayList(java.util.ArrayList) CoreMetadata(loci.formats.CoreMetadata) FormatException(loci.formats.FormatException)

Example 10 with NonNegativeInteger

use of ome.xml.model.primitives.NonNegativeInteger in project bioformats by openmicroscopy.

the class CellH5Reader method parseROIs.

private void parseROIs(int s) {
    int objectIdx = 0;
    List<Color> classColors = new ArrayList<Color>();
    int roiIndexOffset = 0;
    CellH5Coordinate coord = CellH5PositionList.get(0);
    for (String cellObjectName : cellObjectNames) {
        LOGGER.info("Parse segmentation ROIs for cell object {} : {}", cellObjectName, objectIdx);
        String featureName = CellH5Constants.FEATURE + cellObjectName + "/";
        String pathToBoundingBox = coord.pathToPosition + featureName + CellH5Constants.BBOX;
        String pathToClassDefinition = CellH5Constants.DEFINITION + featureName + CellH5Constants.CLASS_LABELS;
        boolean hasClassification = false;
        if (jhdf.exists(pathToClassDefinition)) {
            String classColorHexString;
            HDF5CompoundDataMap[] classDef = jhdf.readCompoundArrayDataMap(pathToClassDefinition);
            for (int cls = 0; cls < classDef.length; cls++) {
                classColorHexString = (String) classDef[cls].get("color");
                classColors.add(hex2Rgb(classColorHexString));
            }
            if (classDef.length > 0) {
                hasClassification = true;
                classes = jhdf.readCompoundArrayDataMap(coord.pathToPosition + featureName + CellH5Constants.PREDICTED_CLASS_LABELS);
            }
        }
        if (jhdf.exists(pathToBoundingBox)) {
            bbox = jhdf.readCompoundArrayDataMap(pathToBoundingBox);
            times = jhdf.readCompoundArrayDataMap(coord.pathToPosition + CellH5Constants.OBJECT + cellObjectName);
            int roiChannel = getChannelIndexOfCellObjectName(cellObjectName);
            int roiZSlice = 0;
            for (int roiIndex = 0; roiIndex < bbox.length; roiIndex++) {
                int roiManagerRoiIndex = roiIndex + roiIndexOffset;
                int roiTime = (Integer) times[roiIndex].get("time_idx");
                int objectLabelId = (Integer) times[roiIndex].get("obj_label_id");
                int left = (Integer) bbox[roiIndex].get("left");
                int right = (Integer) bbox[roiIndex].get("right");
                int top = (Integer) bbox[roiIndex].get("top");
                int bottom = (Integer) bbox[roiIndex].get("bottom");
                int width = right - left;
                int height = bottom - top;
                String roiID = MetadataTools.createLSID("ROI", roiManagerRoiIndex);
                store.setROIID(roiID, roiManagerRoiIndex);
                store.setImageROIRef(roiID, s, roiManagerRoiIndex);
                store.setROIName(cellObjectName + " " + objectLabelId, roiManagerRoiIndex);
                String shapeID = MetadataTools.createLSID("Shape", roiManagerRoiIndex, 0);
                store.setRectangleID(shapeID, roiManagerRoiIndex, 0);
                store.setRectangleX((double) left, roiManagerRoiIndex, 0);
                store.setRectangleY((double) top, roiManagerRoiIndex, 0);
                store.setRectangleWidth((double) width, roiManagerRoiIndex, 0);
                store.setRectangleHeight((double) height, roiManagerRoiIndex, 0);
                store.setRectangleText(cellObjectName, roiManagerRoiIndex, 0);
                store.setRectangleTheT(new NonNegativeInteger(roiTime), roiManagerRoiIndex, 0);
                store.setRectangleTheC(new NonNegativeInteger(roiChannel), roiManagerRoiIndex, 0);
                store.setRectangleTheZ(new NonNegativeInteger(roiZSlice), roiManagerRoiIndex, 0);
                Color strokeColor;
                if (hasClassification) {
                    int classLabelIDx = (Integer) classes[roiIndex].get("label_idx");
                    strokeColor = classColors.get(classLabelIDx);
                } else {
                    strokeColor = new Color(COLORS[objectIdx][0], COLORS[objectIdx][1], COLORS[objectIdx][2], 0xff);
                }
                store.setRectangleStrokeColor(strokeColor, roiManagerRoiIndex, 0);
            }
            objectIdx++;
            roiIndexOffset += bbox.length;
        } else {
            LOGGER.info("No Segmentation data found...");
            break;
        }
    }
}
Also used : NonNegativeInteger(ome.xml.model.primitives.NonNegativeInteger) HDF5CompoundDataMap(ch.systemsx.cisd.hdf5.HDF5CompoundDataMap) NonNegativeInteger(ome.xml.model.primitives.NonNegativeInteger) Color(ome.xml.model.primitives.Color) ArrayList(java.util.ArrayList)

Aggregations

NonNegativeInteger (ome.xml.model.primitives.NonNegativeInteger)28 PositiveInteger (ome.xml.model.primitives.PositiveInteger)17 CoreMetadata (loci.formats.CoreMetadata)15 FormatException (loci.formats.FormatException)14 MetadataStore (loci.formats.meta.MetadataStore)14 Location (loci.common.Location)13 ArrayList (java.util.ArrayList)12 Length (ome.units.quantity.Length)11 Time (ome.units.quantity.Time)11 Timestamp (ome.xml.model.primitives.Timestamp)9 IOException (java.io.IOException)6 ServiceException (loci.common.services.ServiceException)6 RandomAccessInputStream (loci.common.RandomAccessInputStream)5 DependencyException (loci.common.services.DependencyException)5 ServiceFactory (loci.common.services.ServiceFactory)5 OMEXMLMetadata (loci.formats.ome.OMEXMLMetadata)5 OMEXMLService (loci.formats.services.OMEXMLService)5 IFD (loci.formats.tiff.IFD)5 HashMap (java.util.HashMap)3 Map (java.util.Map)3