Search in sources :

Example 16 with MetadataStore

use of loci.formats.meta.MetadataStore in project bioformats by openmicroscopy.

the class LeicaSCNReader method initMetadataStore.

/* @see loci.formats.BaseTiffReader#initMetadataStore() */
@Override
protected void initMetadataStore() throws FormatException {
    super.initMetadataStore();
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this, true);
    HashMap<String, Integer> instrumentIDs = new HashMap<String, Integer>();
    int instrumentidno = 0;
    HashMap<String, String> objectives = new HashMap<String, String>();
    int objectiveidno = 0;
    int parent = 0;
    for (int s = 0; s < getSeriesCount(); s++) {
        int coreIndex = seriesToCoreIndex(s);
        ImageCollection c = handler.collection;
        Image i = handler.imageMap.get(coreIndex);
        int subresolution = coreIndex - parent;
        if (!hasFlattenedResolutions()) {
            subresolution = 0;
        }
        if (core.get(s).resolutionCount > 1) {
            parent = s;
        } else if (core.get(parent).resolutionCount - 1 == subresolution) {
            parent = s + 1;
        }
        Dimension dimension = i.pixels.lookupDimension(0, 0, subresolution);
        if (dimension == null) {
            throw new FormatException("No dimension information for subresolution=" + subresolution);
        }
        // Leica units are nanometres; convert to µm
        double sizeX = i.vSizeX / 1000.0;
        double sizeY = i.vSizeY / 1000.0;
        final Length offsetX = new Length(i.vOffsetX, UNITS.REFERENCEFRAME);
        final Length offsetY = new Length(i.vOffsetY, UNITS.REFERENCEFRAME);
        double sizeZ = i.vSpacingZ / 1000.0;
        store.setPixelsPhysicalSizeX(FormatTools.getPhysicalSizeX(sizeX / dimension.sizeX), s);
        store.setPixelsPhysicalSizeY(FormatTools.getPhysicalSizeY(sizeY / dimension.sizeY), s);
        store.setPixelsPhysicalSizeZ(FormatTools.getPhysicalSizeZ(sizeZ), s);
        if (instrumentIDs.get(i.devModel) == null) {
            String instrumentID = MetadataTools.createLSID("Instrument", instrumentidno);
            instrumentIDs.put(i.devModel, instrumentidno);
            store.setInstrumentID(instrumentID, instrumentidno);
            instrumentidno++;
        }
        int inst = instrumentIDs.get(i.devModel);
        String objectiveName = i.devModel + ":" + i.objMag;
        if (objectives.get(objectiveName) == null) {
            String objectiveID = MetadataTools.createLSID("Objective", inst, objectiveidno);
            objectives.put(objectiveName, objectiveID);
            store.setObjectiveID(objectiveID, inst, objectiveidno);
            Double mag = Double.parseDouble(i.objMag);
            store.setObjectiveNominalMagnification(mag, inst, objectiveidno);
            store.setObjectiveCalibratedMagnification(mag, inst, objectiveidno);
            store.setObjectiveLensNA(new Double(i.illumNA), inst, objectiveidno);
            objectiveidno++;
        }
        store.setImageInstrumentRef(MetadataTools.createLSID("Instrument", inst), s);
        store.setObjectiveSettingsID(objectives.get(objectiveName), s);
        // TODO: Only "brightfield" has been seen in example files
        if (i.illumSource.equals("brightfield")) {
            store.setChannelIlluminationType(IlluminationType.TRANSMITTED, s, 0);
        } else {
            store.setChannelIlluminationType(IlluminationType.OTHER, s, 0);
            LOGGER.debug("Unknown illumination source {}", i.illumSource);
        }
        CoreMetadata ms = core.get(s);
        for (int q = 0; q < ms.imageCount; q++) {
            store.setPlanePositionX(offsetX, s, q);
            store.setPlanePositionY(offsetY, s, q);
        }
        store.setImageName(i.name + " (R" + subresolution + ")", s);
        store.setImageDescription("Collection " + c.name, s);
        store.setImageAcquisitionDate(new Timestamp(i.creationDate), s);
        // Original metadata...
        addSeriesMeta("collection.name", c.name);
        addSeriesMeta("collection.uuid", c.uuid);
        addSeriesMeta("collection.barcode", c.barcode);
        addSeriesMeta("collection.ocr", c.ocr);
        addSeriesMeta("creationDate", i.creationDate);
        addSeriesMeta("device.model for image", i.devModel);
        addSeriesMeta("device.version for image", i.devVersion);
        addSeriesMeta("view.sizeX for image", i.vSizeX);
        addSeriesMeta("view.sizeY for image", i.vSizeY);
        addSeriesMeta("view.offsetX for image", i.vOffsetX);
        addSeriesMeta("view.offsetY for image", i.vOffsetY);
        addSeriesMeta("view.spacingZ for image", i.vSpacingZ);
        addSeriesMeta("scanSettings.objectiveSettings.objective for image", i.objMag);
        addSeriesMeta("scanSettings.illuminationSettings.numericalAperture for image", i.illumNA);
        addSeriesMeta("scanSettings.illuminationSettings.illuminationSource for image", i.illumSource);
    }
}
Also used : HashMap(java.util.HashMap) CoreMetadata(loci.formats.CoreMetadata) Timestamp(ome.xml.model.primitives.Timestamp) FormatException(loci.formats.FormatException) MetadataStore(loci.formats.meta.MetadataStore) Length(ome.units.quantity.Length)

Example 17 with MetadataStore

use of loci.formats.meta.MetadataStore in project bioformats by openmicroscopy.

the class MIASReader 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 (checkSuffix(id, "txt")) {
        // first need to find a relevant TIFF file
        Location base = new Location(id).getAbsoluteFile();
        Location plate = null;
        if (base.getParentFile().getName().equals("Batchresults")) {
            Location experiment = base.getParentFile().getParentFile();
            String[] plates = experiment.list(true);
            Arrays.sort(plates);
            plate = new Location(experiment, plates[0]);
        } else {
            plate = base.getParentFile();
            if (plate.getName().equals("results"))
                plate = plate.getParentFile();
        }
        String[] list = plate.list(true);
        for (String f : list) {
            if (f.startsWith("Well")) {
                Location well = new Location(plate, f);
                String[] wellList = well.list(true);
                for (String file : wellList) {
                    String path = new Location(well, file).getAbsolutePath();
                    if (isThisType(path) && checkSuffix(path, new String[] { "tif", "tiff" })) {
                        initFile(path);
                        return;
                    }
                }
            }
        }
        throw new FormatException("Could not locate an appropriate TIFF file.");
    }
    if (!isGroupFiles()) {
        tiffs = new String[][] { { id } };
        readers = new MinimalTiffReader[1][1];
        readers[0][0] = new MinimalTiffReader();
        TiffReader r = new TiffReader();
        r.setMetadataStore(getMetadataStore());
        r.setId(tiffs[0][0]);
        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();
        tileRows = 1;
        tileCols = 1;
        return;
    }
    analysisFiles = new ArrayList<AnalysisFile>();
    // MIAS is a high content screening format which supports multiple plates,
    // wells and fields.
    // Most of the metadata comes from the directory hierarchy, as very little
    // metadata is present in the actual files.
    // 
    // The directory hierarchy is either:
    // 
    // <experiment name>                        top level experiment directory
    // Batchresults                           analysis results for experiment
    // <plate number>_<plate barcode>         one directory for each plate
    // results                              analysis results for plate
    // Well<xxxx>                           one directory for each well
    // mode<x>_z<xxx>_t<xxx>_im<x>_<x>.tif
    // 
    // or:
    // 
    // <experiment name>                        top level experiment directory
    // <plate number>                         plate directory (3 digits)
    // <well number>                        well directory (4 digits)
    // <channel number>                   channel directory (1 digit)
    // <tile row>_<tile col>_<Z>_<T>.tif
    // 
    // Each TIFF file contains a single grayscale plane.  The "mode" block
    // refers to the channel number; the "z" and "t" blocks refer to the
    // Z section and timepoint, respectively.  The "im<x>_<x>" block gives
    // the row and column coordinates of the image within a mosaic.
    // 
    // We are initially given one of these TIFF files; from there, we need
    // to find the top level experiment directory and work our way down to
    // determine how many plates and wells are present.
    LOGGER.info("Building list of TIFF files");
    Location baseFile = new Location(id).getAbsoluteFile();
    Location plate = baseFile.getParentFile().getParentFile();
    String plateName = plate.getName();
    if (!(plateName.length() == 3 || (plateName.length() > 3 && plateName.replaceAll("\\d", "").startsWith("-")))) {
        plate = plate.getParentFile();
        plateName = plate.getName();
    }
    int plateNumber = Integer.parseInt(plateName.substring(0, 3));
    Location experiment = plate.getParentFile();
    String[] directories = experiment.list(true);
    Arrays.sort(directories);
    for (String dir : directories) {
        Location f = new Location(experiment, dir);
        if (dir.equals("Batchresults")) {
            String[] results = f.list(true);
            for (String result : results) {
                Location file = new Location(f, result);
                if (result.startsWith("NEO_Results")) {
                    resultFile = file.getAbsolutePath();
                    AnalysisFile af = new AnalysisFile();
                    af.filename = resultFile;
                    analysisFiles.add(af);
                } else if (result.startsWith("NEO_PlateOutput_")) {
                    int plateIndex = Integer.parseInt(result.substring(16, 19));
                    if (plateIndex == plateNumber) {
                        AnalysisFile af = new AnalysisFile();
                        af.filename = file.getAbsolutePath();
                        af.plate = 0;
                        analysisFiles.add(af);
                    }
                }
            }
        }
    }
    String[] list = plate.list(true);
    Arrays.sort(list);
    final List<String> wellDirectories = new ArrayList<String>();
    for (String dir : list) {
        Location f = new Location(plate, dir);
        if (f.getName().startsWith("Well") || f.getName().length() == 4) {
            // directory name is valid, but we need to make sure that the
            // directory contains a TIFF or a subdirectory
            String[] wellList = f.list(true);
            if (wellList != null) {
                boolean validWell = false;
                for (String potentialTIFF : wellList) {
                    if (potentialTIFF.toLowerCase().endsWith(".tif") || new Location(f, potentialTIFF).isDirectory()) {
                        validWell = true;
                        break;
                    }
                }
                if (validWell)
                    wellDirectories.add(f.getAbsolutePath());
            }
        } else if (f.getName().equals("results")) {
            String[] resultsList = f.list(true);
            for (String result : resultsList) {
                // exclude proprietary program state files
                if (!result.endsWith(".sav") && !result.endsWith(".dsv") && !result.endsWith(".dat")) {
                    Location r = new Location(f, result);
                    AnalysisFile af = new AnalysisFile();
                    af.filename = r.getAbsolutePath();
                    af.plate = 0;
                    if (result.toLowerCase().startsWith("well")) {
                        af.well = Integer.parseInt(result.substring(4, 8)) - 1;
                    }
                    analysisFiles.add(af);
                }
            }
        } else if (f.getName().equals("Nugenesistemplate.txt")) {
            templateFile = f.getAbsolutePath();
        }
    }
    int nWells = wellDirectories.size();
    LOGGER.debug("Found {} wells.", nWells);
    readers = new MinimalTiffReader[nWells][];
    tiffs = new String[nWells][];
    int[] zCount = new int[nWells];
    int[] cCount = new int[nWells];
    int[] tCount = new int[nWells];
    String[] order = new String[nWells];
    wellNumber = new int[nWells];
    String[] wells = wellDirectories.toArray(new String[nWells]);
    Arrays.sort(wells);
    for (int j = 0; j < nWells; j++) {
        Location well = new Location(wells[j]);
        String wellName = well.getName().replaceAll("Well", "");
        wellNumber[j] = Integer.parseInt(wellName) - 1;
        String[] tiffFiles = well.list(true);
        final List<String> tmpFiles = new ArrayList<String>();
        for (String tiff : tiffFiles) {
            String name = tiff.toLowerCase();
            if (name.endsWith(".tif") || name.endsWith(".tiff")) {
                tmpFiles.add(new Location(well, tiff).getAbsolutePath());
            }
        }
        if (tmpFiles.size() == 0) {
            LOGGER.debug("No TIFFs in well directory {}", wells[j]);
            // directories which contain the TIFFs
            for (String dir : tiffFiles) {
                Location file = new Location(well, dir);
                if (dir.length() == 1 && file.isDirectory()) {
                    cCount[j]++;
                    String[] tiffs = file.list(true);
                    for (String tiff : tiffs) {
                        String name = tiff.toLowerCase();
                        if (name.endsWith(".tif") || name.endsWith(".tiff")) {
                            tmpFiles.add(new Location(file, tiff).getAbsolutePath());
                        }
                    }
                }
            }
        }
        tiffFiles = tmpFiles.toArray(new String[0]);
        if (ArrayUtils.isEmpty(tiffFiles)) {
            throw new FormatException("Empty dataset - No tiff files were found.");
        }
        Location firstTiff = new Location(tiffFiles[0]);
        List<String> names = new ArrayList<String>();
        for (Location f : firstTiff.getParentFile().listFiles()) {
            names.add(f.getName());
        }
        FilePattern fp = new FilePattern(FilePattern.findPattern(firstTiff.getName(), null, names.toArray(new String[names.size()])));
        String[] blocks = fp.getPrefixes();
        order[j] = "XY";
        int[] count = fp.getCount();
        for (int block = blocks.length - 1; block >= 0; block--) {
            blocks[block] = blocks[block].toLowerCase();
            blocks[block] = blocks[block].substring(blocks[block].lastIndexOf("_") + 1);
            if (blocks[block].equals("z")) {
                zCount[j] = count[block];
                order[j] += 'Z';
            } else if (blocks[block].equals("t")) {
                tCount[j] = count[block];
                order[j] += 'T';
            } else if (blocks[block].equals("mode")) {
                cCount[j] = count[block];
                order[j] += 'C';
            } else if (blocks[block].equals("im"))
                tileRows = count[block];
            else if (blocks[block].equals(""))
                tileCols = count[block];
            else if (blocks[block].replaceAll("\\d", "").length() == 0) {
                if (block == 3)
                    tileRows = count[block];
                else if (block == 2)
                    tileCols = count[block];
                else if (block == 0) {
                    zCount[j] = count[block];
                    order[j] += 'Z';
                } else if (block == 1) {
                    tCount[j] = count[block];
                    order[j] += 'T';
                }
            } else {
                throw new FormatException("Unsupported block '" + blocks[block]);
            }
        }
        Arrays.sort(tiffFiles);
        tiffs[j] = tiffFiles;
        LOGGER.debug("Well {} has {} files.", j, tiffFiles.length);
        readers[j] = new MinimalTiffReader[tiffFiles.length];
        for (int k = 0; k < tiffFiles.length; k++) {
            readers[j][k] = new MinimalTiffReader();
        }
    }
    // Populate core metadata
    LOGGER.info("Populating core metadata");
    int nSeries = tiffs.length;
    bpp = new int[nSeries];
    if (readers.length == 0) {
        throw new FormatException("No wells were found.");
    }
    // assume that all wells have the same width, height, and pixel type
    readers[0][0].setId(tiffs[0][0]);
    tileWidth = readers[0][0].getSizeX();
    tileHeight = readers[0][0].getSizeY();
    if (tileCols == 0)
        tileCols = 1;
    if (tileRows == 0)
        tileRows = 1;
    core.clear();
    for (int i = 0; i < nSeries; i++) {
        CoreMetadata ms = new CoreMetadata();
        core.add(ms);
        ms.sizeZ = zCount[i];
        ms.sizeC = cCount[i];
        ms.sizeT = tCount[i];
        if (ms.sizeZ == 0)
            ms.sizeZ = 1;
        if (ms.sizeC == 0)
            ms.sizeC = 1;
        if (ms.sizeT == 0)
            ms.sizeT = 1;
        ms.sizeX = tileWidth * tileCols;
        ms.sizeY = tileHeight * tileRows;
        ms.pixelType = readers[0][0].getPixelType();
        ms.sizeC *= readers[0][0].getSizeC();
        ms.rgb = readers[0][0].isRGB();
        ms.littleEndian = readers[0][0].isLittleEndian();
        ms.interleaved = readers[0][0].isInterleaved();
        ms.indexed = readers[0][0].isIndexed();
        ms.falseColor = readers[0][0].isFalseColor();
        ms.dimensionOrder = order[i];
        if (ms.dimensionOrder.indexOf('Z') == -1) {
            ms.dimensionOrder += 'Z';
        }
        if (ms.dimensionOrder.indexOf('C') == -1) {
            ms.dimensionOrder += 'C';
        }
        if (ms.dimensionOrder.indexOf('T') == -1) {
            ms.dimensionOrder += 'T';
        }
        ms.imageCount = ms.sizeZ * ms.sizeT * cCount[i];
        if (ms.imageCount == 0) {
            ms.imageCount = 1;
        }
        bpp[i] = FormatTools.getBytesPerPixel(ms.pixelType);
    }
    // Populate metadata hashtable
    LOGGER.info("Populating metadata hashtable");
    if (resultFile != null && getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        String[] cols = null;
        final List<String> rows = new ArrayList<String>();
        boolean doKeyValue = true;
        int nStarLines = 0;
        String analysisResults = DataTools.readFile(resultFile);
        String[] lines = analysisResults.split("\n");
        for (String line : lines) {
            line = line.trim();
            if (line.length() == 0)
                continue;
            if (line.startsWith("******") && line.endsWith("******"))
                nStarLines++;
            if (doKeyValue) {
                String[] n = line.split("\t");
                if (n[0].endsWith(":"))
                    n[0] = n[0].substring(0, n[0].length() - 1);
                if (n.length >= 2)
                    addGlobalMeta(n[0], n[1]);
            } else {
                if (cols == null)
                    cols = line.split("\t");
                else
                    rows.add(line);
            }
            if (nStarLines == 2)
                doKeyValue = false;
        }
        for (String row : rows) {
            String[] d = row.split("\t");
            for (int col = 3; col < cols.length; col++) {
                addGlobalMeta("Plate " + d[0] + ", Well " + d[2] + " " + cols[col], d[col]);
                if (cols[col].equals("AreaCode")) {
                    String wellID = d[col].replaceAll("\\D", "");
                    wellColumns = Integer.parseInt(wellID);
                }
            }
        }
    }
    // Populate MetadataStore
    LOGGER.info("Populating MetadataStore");
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this, true);
    // assume that a 96 well plate is 8x12, and a 384 well plate is 16x24
    if (wellColumns == 0) {
        if (nWells == 96) {
            wellColumns = 12;
        } else if (nWells == 384) {
            wellColumns = 24;
        } else {
            LOGGER.warn("Could not determine the plate dimensions.");
            wellColumns = 24;
        }
    }
    store.setPlateID(MetadataTools.createLSID("Plate", 0), 0);
    String plateAcqId = MetadataTools.createLSID("PlateAcquisition", 0, 0);
    store.setPlateAcquisitionID(plateAcqId, 0, 0);
    store.setPlateAcquisitionMaximumFieldCount(new PositiveInteger(1), 0, 0);
    for (int well = 0; well < nWells; well++) {
        int wellIndex = wellNumber[well];
        int row = wellIndex / wellColumns;
        int wellCol = (wellIndex % wellColumns) + 1;
        char wellRow = (char) ('A' + row);
        store.setWellID(MetadataTools.createLSID("Well", 0, well), 0, well);
        store.setWellRow(new NonNegativeInteger(row), 0, well);
        store.setWellColumn(new NonNegativeInteger(wellCol - 1), 0, well);
        String imageID = MetadataTools.createLSID("Image", well);
        String wellSampleID = MetadataTools.createLSID("WellSample", 0, well, 0);
        store.setWellSampleID(wellSampleID, 0, well, 0);
        store.setWellSampleIndex(new NonNegativeInteger(well), 0, well, 0);
        store.setImageID(imageID, well);
        store.setImageName("Well " + wellRow + wellCol, well);
        store.setWellSampleImageRef(imageID, 0, well, 0);
        store.setPlateAcquisitionWellSampleRef(wellSampleID, 0, 0, well);
    }
    MetadataLevel level = getMetadataOptions().getMetadataLevel();
    if (level != MetadataLevel.MINIMUM) {
        String experimentID = MetadataTools.createLSID("Experiment", 0);
        store.setExperimentID(experimentID, 0);
        store.setExperimentType(getExperimentType("Other"), 0);
        store.setExperimentDescription(experiment.getName(), 0);
        // populate SPW metadata
        store.setPlateColumnNamingConvention(getNamingConvention("Number"), 0);
        store.setPlateRowNamingConvention(getNamingConvention("Letter"), 0);
        parseTemplateFile(store);
        plateName = plateName.substring(plateName.indexOf('-') + 1);
        store.setPlateName(plateName, 0);
        store.setPlateExternalIdentifier(plateName, 0);
        for (int well = 0; well < nWells; well++) {
            // populate Image/Pixels metadata
            store.setImageExperimentRef(experimentID, well);
            String instrumentID = MetadataTools.createLSID("Instrument", 0);
            store.setInstrumentID(instrumentID, 0);
            store.setImageInstrumentRef(instrumentID, well);
        }
        roiFiles = new ArrayList<AnalysisFile>();
        for (AnalysisFile af : analysisFiles) {
            String file = af.filename;
            String name = new Location(file).getName();
            if (!name.startsWith("Well"))
                continue;
            if (name.endsWith("AllModesOverlay.tif")) {
                roiFiles.add(af);
            } else if (name.endsWith("overlay.tif")) {
                roiFiles.add(af);
            }
        }
        if (level != MetadataLevel.NO_OVERLAYS) {
            // populate image-level ROIs
            Color[] colors = new Color[getSizeC()];
            int nextROI = 0;
            for (AnalysisFile af : analysisFiles) {
                String file = af.filename;
                String name = new Location(file).getName();
                if (!name.startsWith("Well"))
                    continue;
                int[] position = getPositionFromFile(file);
                int well = position[0];
                if (name.endsWith("detail.txt")) {
                    String data = DataTools.readFile(file);
                    String[] lines = data.split("\n");
                    int start = 0;
                    while (start < lines.length && !lines[start].startsWith("Label")) {
                        start++;
                    }
                    if (start >= lines.length)
                        continue;
                    String[] columns = lines[start].split("\t");
                    List<String> columnNames = Arrays.asList(columns);
                    for (int j = start + 1; j < lines.length; j++) {
                        populateROI(columnNames, lines[j].split("\t"), well, nextROI++, position[1], position[2], store);
                    }
                } else if (name.endsWith("AllModesOverlay.tif")) {
                    // results/Well<nnnn>_mode<n>_z<nnn>_t<nnn>_AllModesOverlay.tif
                    if (colors[position[3]] != null)
                        continue;
                    try {
                        colors[position[3]] = getChannelColorFromFile(file);
                    } catch (IOException e) {
                    }
                    if (colors[position[3]] == null)
                        continue;
                    for (int s = 0; s < getSeriesCount(); s++) {
                        store.setChannelColor(colors[position[3]], s, position[3]);
                    }
                    if (position[3] == 0) {
                        nextROI += parseMasks(store, well, nextROI, file);
                    }
                } else if (name.endsWith("overlay.tif")) {
                    nextROI += parseMasks(store, well, nextROI, file);
                }
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) PositiveInteger(ome.xml.model.primitives.PositiveInteger) NonNegativeInteger(ome.xml.model.primitives.NonNegativeInteger) Color(ome.xml.model.primitives.Color) IOException(java.io.IOException) CoreMetadata(loci.formats.CoreMetadata) FormatException(loci.formats.FormatException) MetadataStore(loci.formats.meta.MetadataStore) FilePattern(loci.formats.FilePattern) HashMap(java.util.HashMap) Map(java.util.Map) Location(loci.common.Location)

Example 18 with MetadataStore

use of loci.formats.meta.MetadataStore in project bioformats by openmicroscopy.

the class MRCReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
public void initFile(String id) throws FormatException, IOException {
    super.initFile(id);
    in = new RandomAccessInputStream(id);
    MetadataLevel level = getMetadataOptions().getMetadataLevel();
    CoreMetadata m = core.get(0);
    LOGGER.info("Reading header");
    // check endianness
    in.seek(ENDIANNESS_OFFSET);
    m.littleEndian = in.read() == 68;
    // read dimension information from 1024 byte header
    in.seek(0);
    in.order(isLittleEndian());
    m.sizeX = in.readInt();
    m.sizeY = in.readInt();
    m.sizeZ = in.readInt();
    // We are using BigInteger here because of the very real possiblity
    // of not just an int overflow but also a long overflow when multiplying
    // sizeX * sizeY * sizeZ.
    BigInteger v = BigInteger.valueOf(getSizeX());
    v = v.multiply(BigInteger.valueOf(getSizeY()));
    v = v.multiply(BigInteger.valueOf(getSizeZ()));
    if (getSizeX() < 0 || getSizeY() < 0 || getSizeZ() < 0 || (v.compareTo(BigInteger.valueOf(in.length())) > 0)) {
        LOGGER.debug("Detected endianness is wrong, swapping");
        m.littleEndian = !isLittleEndian();
        in.seek(0);
        in.order(isLittleEndian());
        m.sizeX = in.readInt();
        m.sizeY = in.readInt();
        m.sizeZ = in.readInt();
    }
    m.sizeC = 1;
    m.rgb = false;
    int mode = in.readInt();
    switch(mode) {
        case 0:
            in.seek(IMODSTAMP_OFFSET);
            if (in.readInt() == 1146047817) {
                m.pixelType = FormatTools.INT8;
            } else {
                m.pixelType = FormatTools.UINT8;
            }
            break;
        case 1:
            m.pixelType = FormatTools.INT16;
            break;
        case 6:
            m.pixelType = FormatTools.UINT16;
            break;
        case 2:
            m.pixelType = FormatTools.FLOAT;
            break;
        case 3:
            m.pixelType = FormatTools.UINT32;
            break;
        case 4:
            m.pixelType = FormatTools.DOUBLE;
            break;
        case 16:
            m.sizeC = 3;
            m.pixelType = FormatTools.UINT8;
            m.rgb = true;
            break;
    }
    in.seek(GRIDSIZE_OFFSET);
    // pixel size = xlen / mx
    double xSize = 0d, ySize = 0d, zSize = 0d;
    if (level != MetadataLevel.MINIMUM) {
        int mx = in.readInt();
        int my = in.readInt();
        int mz = in.readInt();
        float xlen = in.readFloat();
        float ylen = in.readFloat();
        float zlen = in.readFloat();
        // physical sizes are stored in ångströms
        xSize = (xlen / mx);
        ySize = (ylen / my);
        zSize = (zlen / mz);
        addGlobalMeta("Grid size (X)", mx);
        addGlobalMeta("Grid size (Y)", my);
        addGlobalMeta("Grid size (Z)", mz);
        addGlobalMeta("Cell size (X)", xlen);
        addGlobalMeta("Cell size (Y)", ylen);
        addGlobalMeta("Cell size (Z)", zlen);
        addGlobalMeta("Alpha angle", in.readFloat());
        addGlobalMeta("Beta angle", in.readFloat());
        addGlobalMeta("Gamma angle", in.readFloat());
        in.skipBytes(12);
    // min, max and mean pixel values
    } else
        in.skipBytes(48);
    double minValue = in.readFloat();
    double maxValue = in.readFloat();
    addGlobalMeta("Minimum pixel value", minValue);
    addGlobalMeta("Maximum pixel value", maxValue);
    addGlobalMeta("Mean pixel value", in.readFloat());
    int bytes = FormatTools.getBytesPerPixel(getPixelType());
    double range = Math.pow(2, bytes * 8) - 1;
    double pixelTypeMin = 0;
    boolean signed = FormatTools.isSigned(getPixelType());
    if (signed) {
        pixelTypeMin -= (range / 2);
    }
    double pixelTypeMax = pixelTypeMin + range;
    // See https://trac.openmicroscopy.org/ome/ticket/4619
    if (pixelTypeMax < maxValue || pixelTypeMin > minValue && signed) {
        switch(getPixelType()) {
            case FormatTools.INT16:
                m.pixelType = FormatTools.UINT16;
                break;
            case FormatTools.INT32:
                m.pixelType = FormatTools.UINT32;
                break;
        }
    }
    int ispg = in.readInt();
    addGlobalMeta("ISPG", ispg);
    addGlobalMeta("Is data cube", ispg == 1);
    extHeaderSize = in.readInt();
    if (level != MetadataLevel.MINIMUM) {
        in.skipBytes(64);
        int idtype = in.readShort();
        String type = "unknown";
        if (idtype >= 0 && idtype < TYPES.length)
            type = TYPES[idtype];
        addGlobalMeta("Series type", type);
        addGlobalMeta("Lens", in.readShort());
        addGlobalMeta("ND1", in.readShort());
        addGlobalMeta("ND2", in.readShort());
        addGlobalMeta("VD1", in.readShort());
        addGlobalMeta("VD2", in.readShort());
        for (int i = 0; i < 6; i++) {
            addGlobalMetaList("Angle", in.readFloat());
        }
        in.skipBytes(24);
        addGlobalMeta("Number of useful labels", in.readInt());
        for (int i = 0; i < 10; i++) {
            addGlobalMetaList("Label", in.readString(80));
        }
    }
    LOGGER.info("Populating metadata");
    m.sizeT = 1;
    m.dimensionOrder = isRGB() ? "XYCZT" : "XYZTC";
    m.imageCount = getSizeZ() * (isRGB() ? 1 : getSizeC());
    m.interleaved = true;
    m.indexed = false;
    m.falseColor = false;
    m.metadataComplete = true;
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
    if (level != MetadataLevel.MINIMUM) {
        Length sizeX = FormatTools.getPhysicalSizeX(xSize, UNITS.ANGSTROM);
        Length sizeY = FormatTools.getPhysicalSizeY(ySize, UNITS.ANGSTROM);
        Length sizeZ = FormatTools.getPhysicalSizeZ(zSize, UNITS.ANGSTROM);
        if (sizeX != null) {
            store.setPixelsPhysicalSizeX(sizeX, 0);
        }
        if (sizeY != null) {
            store.setPixelsPhysicalSizeY(sizeY, 0);
        }
        if (sizeZ != null) {
            store.setPixelsPhysicalSizeZ(sizeZ, 0);
        }
    }
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) Length(ome.units.quantity.Length) BigInteger(java.math.BigInteger) RandomAccessInputStream(loci.common.RandomAccessInputStream) CoreMetadata(loci.formats.CoreMetadata)

Example 19 with MetadataStore

use of loci.formats.meta.MetadataStore in project bioformats by openmicroscopy.

the class FEITiffReader method initMetadataStore.

/* @see BaseTiffReader#initMetadataStore() */
@Override
protected void initMetadataStore() throws FormatException {
    super.initMetadataStore();
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
    if (date != null) {
        date = DateTools.formatDate(date, DATE_FORMAT);
        if (date != null) {
            store.setImageAcquisitionDate(new Timestamp(date), 0);
        }
    }
    if (imageName != null) {
        store.setImageName(imageName, 0);
    }
    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        if (imageDescription != null) {
            store.setImageDescription(imageDescription, 0);
        }
        if (userName != null) {
            store.setExperimenterID(MetadataTools.createLSID("Experimenter", 0), 0);
            store.setExperimenterLastName(userName, 0);
        }
        if (microscopeModel != null) {
            String instrument = MetadataTools.createLSID("Instrument", 0);
            store.setInstrumentID(instrument, 0);
            store.setImageInstrumentRef(instrument, 0);
            store.setMicroscopeModel(microscopeModel, 0);
        }
        if (detectors != null && detectors.size() > 0) {
            String instrument = MetadataTools.createLSID("Instrument", 0);
            store.setInstrumentID(instrument, 0);
            store.setImageInstrumentRef(instrument, 0);
            for (int i = 0; i < detectors.size(); i++) {
                String detectorID = MetadataTools.createLSID("Detector", 0, i);
                store.setDetectorID(detectorID, 0, i);
                store.setDetectorModel(detectors.get(i), 0, i);
                store.setDetectorType(getDetectorType("Other"), 0, i);
            }
        }
        if (magnification != null) {
            store.setObjectiveID(MetadataTools.createLSID("Objective", 0, 0), 0, 0);
            store.setObjectiveNominalMagnification(magnification, 0, 0);
            store.setObjectiveCorrection(getCorrection("Other"), 0, 0);
            store.setObjectiveImmersion(getImmersion("Other"), 0, 0);
        }
        store.setStageLabelX(stageX, 0);
        store.setStageLabelY(stageY, 0);
        store.setStageLabelZ(stageZ, 0);
        store.setStageLabelName("", 0);
        boolean helios = ifds.get(0).containsKey(HELIOS_TAG);
        Unit<Length> unit = UNITS.MICROMETER;
        if (helios) {
            unit = UNITS.METER;
        }
        Length physicalSizeX = FormatTools.getPhysicalSizeX(sizeX, unit);
        Length physicalSizeY = FormatTools.getPhysicalSizeY(sizeY, unit);
        if (physicalSizeX != null) {
            store.setPixelsPhysicalSizeX(physicalSizeX, 0);
        }
        if (physicalSizeY != null) {
            store.setPixelsPhysicalSizeY(physicalSizeY, 0);
        }
        if (timeIncrement != null) {
            store.setPixelsTimeIncrement(new Time(timeIncrement, UNITS.SECOND), 0);
        }
    }
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) Length(ome.units.quantity.Length) Time(ome.units.quantity.Time) Timestamp(ome.xml.model.primitives.Timestamp)

Example 20 with MetadataStore

use of loci.formats.meta.MetadataStore in project bioformats by openmicroscopy.

the class FlexReader method initFlexFile.

private void initFlexFile(String id) throws FormatException, IOException {
    LOGGER.debug("initFlexFile({})", id);
    boolean doGrouping = true;
    Location currentFile = new Location(id).getAbsoluteFile();
    LOGGER.info("Storing well indices");
    try {
        String name = currentFile.getName();
        int[] well = getWell(name);
        if (well[0] > nRows)
            nRows = well[0];
        if (well[1] > nCols)
            nCols = well[1];
    } catch (NumberFormatException e) {
        LOGGER.debug("Could not parse well indices", e);
        doGrouping = false;
    }
    LOGGER.info("Looking for other .flex files");
    if (!isGroupFiles())
        doGrouping = false;
    if (isGroupFiles()) {
        LOGGER.debug("Attempting to find files in the same dataset.");
        try {
            findFiles(currentFile);
        } catch (NullPointerException e) {
            LOGGER.debug("", e);
        } catch (IOException e) {
            LOGGER.debug("", e);
        }
        if (measurementFiles.isEmpty()) {
            LOGGER.warn("Measurement files not found.");
        } else {
            for (String f : measurementFiles) {
                if (checkSuffix(f, RES_SUFFIX)) {
                    parseResFile(f);
                }
            }
        }
    }
    MetadataStore store = makeFilterMetadata();
    LOGGER.info("Making sure that all .flex files are valid");
    final List<String> flex = new ArrayList<String>();
    if (doGrouping) {
        // group together .flex files that are in the same directory
        Location dir = currentFile.getParentFile();
        String[] files = dir.list(true);
        for (String file : files) {
            // file names should be nnnnnnnnn.flex, where 'n' is 0-9
            LOGGER.debug("Checking if {} belongs in the same dataset.", file);
            if (file.endsWith(".flex") && file.length() == 14) {
                flex.add(new Location(dir, file).getAbsolutePath());
                LOGGER.debug("Added {} to dataset.", flex.get(flex.size() - 1));
            }
        }
    }
    String[] files = doGrouping ? flex.toArray(new String[flex.size()]) : new String[] { currentFile.getAbsolutePath() };
    if (files.length == 0) {
        if (Location.getMappedFile(currentFile.getName()) != null) {
            files = new String[] { currentFile.getName() };
        } else {
            files = new String[] { currentFile.getAbsolutePath() };
        }
    }
    LOGGER.debug("Determined that {} .flex files belong together.", files.length);
    groupFiles(files, store);
    populateMetadataStore(store);
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Location(loci.common.Location)

Aggregations

MetadataStore (loci.formats.meta.MetadataStore)180 CoreMetadata (loci.formats.CoreMetadata)130 RandomAccessInputStream (loci.common.RandomAccessInputStream)97 Length (ome.units.quantity.Length)82 FormatException (loci.formats.FormatException)66 Timestamp (ome.xml.model.primitives.Timestamp)54 Location (loci.common.Location)51 Time (ome.units.quantity.Time)41 ArrayList (java.util.ArrayList)34 IFD (loci.formats.tiff.IFD)21 DependencyException (loci.common.services.DependencyException)16 IOException (java.io.IOException)15 TiffParser (loci.formats.tiff.TiffParser)15 NonNegativeInteger (ome.xml.model.primitives.NonNegativeInteger)15 ServiceFactory (loci.common.services.ServiceFactory)14 PositiveInteger (ome.xml.model.primitives.PositiveInteger)13 IFDList (loci.formats.tiff.IFDList)12 MetadataRetrieve (loci.formats.meta.MetadataRetrieve)11 ServiceException (loci.common.services.ServiceException)10 Map (java.util.Map)9