Search in sources :

Example 36 with DependencyException

use of loci.common.services.DependencyException in project bioformats by openmicroscopy.

the class ZeissLSMReader method parseMDB.

/**
 * Parse a .mdb file and return a list of referenced .lsm files.
 */
private String[] parseMDB(String mdbFile) throws FormatException, IOException {
    Location mdb = new Location(mdbFile).getAbsoluteFile();
    Location parent = mdb.getParentFile();
    MDBService mdbService = null;
    try {
        ServiceFactory factory = new ServiceFactory();
        mdbService = factory.getInstance(MDBService.class);
    } catch (DependencyException de) {
        throw new FormatException("MDB Tools Java library not found", de);
    }
    try {
        mdbService.initialize(mdbFile);
    } catch (Exception e) {
        return null;
    }
    Vector<Vector<String[]>> tables = mdbService.parseDatabase();
    mdbService.close();
    final List<String> referencedLSMs = new ArrayList<String>();
    int referenceCount = 0;
    for (Vector<String[]> table : tables) {
        String[] columnNames = table.get(0);
        String tableName = columnNames[0];
        for (int row = 1; row < table.size(); row++) {
            String[] tableRow = table.get(row);
            for (int col = 0; col < tableRow.length; col++) {
                String key = tableName + " " + columnNames[col + 1];
                if (currentId != null) {
                    addGlobalMetaList(key, tableRow[col]);
                }
                if (tableName.equals("Recordings") && columnNames[col + 1] != null && columnNames[col + 1].equals("SampleData")) {
                    String filename = tableRow[col].trim();
                    filename = filename.replace('\\', File.separatorChar);
                    filename = filename.replace('/', File.separatorChar);
                    filename = filename.substring(filename.lastIndexOf(File.separator) + 1);
                    if (filename.length() > 0) {
                        Location file = new Location(parent, filename);
                        if (file.exists()) {
                            referencedLSMs.add(file.getAbsolutePath());
                        }
                    }
                    referenceCount++;
                }
            }
        }
    }
    if (referencedLSMs.size() == referenceCount) {
        return referencedLSMs.toArray(new String[0]);
    }
    String[] fileList = parent.list(true);
    Arrays.sort(fileList);
    for (int i = 0; i < fileList.length; i++) {
        Location f = new Location(fileList[i]);
        if (!f.exists()) {
            f = new Location(parent, fileList[i]);
        }
        String absolutePath = f.getAbsolutePath();
        if (checkSuffix(fileList[i], "mdb") && (!absolutePath.equals(mdbFile) && !fileList[i].equals(mdbFile) && !absolutePath.equals(new Location(mdbFile).getAbsolutePath()))) {
            if (referencedLSMs.size() > 0) {
                return referencedLSMs.toArray(new String[0]);
            }
            break;
        }
    }
    referencedLSMs.clear();
    int mdbCount = 0;
    for (int i = 0; i < fileList.length; i++) {
        String absolutePath = new Location(parent, fileList[i]).getAbsolutePath();
        if (checkSuffix(fileList[i], "lsm")) {
            referencedLSMs.add(absolutePath);
        } else if (checkSuffix(fileList[i], "mdb")) {
            mdbCount++;
        }
    }
    if (mdbCount > 1 || ((referencedLSMs.size() > referenceCount) && mdbCount > 1)) {
        for (int i = 0; i < fileList.length; i++) {
            String absolutePath = new Location(parent, fileList[i]).getAbsolutePath();
            if (checkSuffix(fileList[i], "mdb") && !absolutePath.endsWith(mdbFile)) {
                String[] files = parseMDB(absolutePath);
                for (String f : files) {
                    referencedLSMs.remove(f);
                }
            }
        }
    }
    return referencedLSMs.toArray(new String[0]);
}
Also used : MDBService(loci.formats.services.MDBService) ServiceFactory(loci.common.services.ServiceFactory) ArrayList(java.util.ArrayList) DependencyException(loci.common.services.DependencyException) FormatException(loci.formats.FormatException) DependencyException(loci.common.services.DependencyException) FormatException(loci.formats.FormatException) IOException(java.io.IOException) Vector(java.util.Vector) Location(loci.common.Location)

Example 37 with DependencyException

use of loci.common.services.DependencyException in project bioformats by openmicroscopy.

the class FileWriteSPW method initializeMetadata.

/**
 * Populate the minimum amount of metadata required to export a Plate.
 */
private IMetadata initializeMetadata(int[][] nFovs) {
    Exception exception = null;
    try {
        // create the OME-XML metadata storage object
        ServiceFactory factory = new ServiceFactory();
        service = factory.getInstance(OMEXMLService.class);
        OMEXMLMetadata meta = service.createOMEXMLMetadata();
        // IMetadata meta = service.createOMEXMLMetadata();
        meta.createRoot();
        int plateIndex = 0;
        // count of images
        int series = 0;
        int well = 0;
        meta.setPlateDescription(plateDescription, 0);
        meta.setPlateID(MetadataTools.createLSID("Plate", 0), 0);
        meta.setPlateRowNamingConvention(NamingConvention.LETTER, 0);
        meta.setPlateColumnNamingConvention(NamingConvention.NUMBER, 0);
        meta.setPlateRows(new PositiveInteger(rows), 0);
        meta.setPlateColumns(new PositiveInteger(cols), 0);
        meta.setPlateName("First test Plate", 0);
        PositiveInteger pwidth = new PositiveInteger(width);
        PositiveInteger pheight = new PositiveInteger(height);
        char rowChar = 'A';
        for (int row = 0; row < rows; row++) {
            for (int column = 0; column < cols; column++) {
                // set up well
                String wellID = MetadataTools.createLSID("Well", well);
                meta.setWellID(wellID, plateIndex, well);
                meta.setWellRow(new NonNegativeInteger(row), plateIndex, well);
                meta.setWellColumn(new NonNegativeInteger(column), plateIndex, well);
                int nFOV = nFovs[row][column];
                for (int fov = 0; fov < nFOV; fov++) {
                    // Create Image NB numberng in the Name goes from 1->n not 0-> n-1
                    String imageName = rowChar + ":" + Integer.toString(column + 1) + ":FOV:" + Integer.toString(fov + 1);
                    String imageID = MetadataTools.createLSID("Image", well, fov);
                    meta.setImageID(imageID, series);
                    meta.setImageName(imageName, series);
                    String pixelsID = MetadataTools.createLSID("Pixels", well, fov);
                    meta.setPixelsID(pixelsID, series);
                    // specify that the pixel data is stored in big-endian format
                    // change 'TRUE' to 'FALSE' to specify little-endian format
                    meta.setPixelsBigEndian(Boolean.TRUE, series);
                    // specify that the image is stored in ZCT order
                    meta.setPixelsDimensionOrder(DimensionOrder.XYZCT, series);
                    // specify the pixel type of the image
                    meta.setPixelsType(PixelType.fromString(FormatTools.getPixelTypeString(pixelType)), series);
                    // specify the dimensions of the image
                    meta.setPixelsSizeX(pwidth, series);
                    meta.setPixelsSizeY(pheight, series);
                    meta.setPixelsSizeZ(new PositiveInteger(1), series);
                    meta.setPixelsSizeC(new PositiveInteger(1), series);
                    meta.setPixelsSizeT(new PositiveInteger(sizet), series);
                    // define each channel and specify the number of samples in the channel
                    // the number of samples is 3 for RGB images and 1 otherwise
                    String channelID = MetadataTools.createLSID("Channel", well, fov);
                    meta.setChannelID(channelID, series, 0);
                    meta.setChannelSamplesPerPixel(new PositiveInteger(1), series, 0);
                    // set sample
                    String wellSampleID = MetadataTools.createLSID("WellSample", well, fov);
                    meta.setWellSampleID(wellSampleID, 0, well, fov);
                    // NB sampleIndex here == series ie the image No
                    meta.setWellSampleIndex(new NonNegativeInteger(series), 0, well, fov);
                    meta.setWellSampleImageRef(imageID, 0, well, fov);
                    if (exposureTimes != null && exposureTimes.length == sizet) {
                        for (int t = 0; t < sizet; t++) {
                            meta.setPlaneTheT(new NonNegativeInteger(t), series, t);
                            meta.setPlaneTheC(new NonNegativeInteger(0), series, t);
                            meta.setPlaneTheZ(new NonNegativeInteger(0), series, t);
                            meta.setPlaneExposureTime(new Time(exposureTimes[t], ome.units.UNITS.SECOND), series, t);
                        }
                    }
                    // add FLIM ModuloAlongT annotation if required
                    if (delays != null) {
                        CoreMetadata modlo = createModuloAnn(meta);
                        service.addModuloAlong(meta, modlo, series);
                    }
                    series++;
                }
                // end of samples
                well++;
            }
            rowChar++;
        }
        expectedImages = new int[series];
        // System.out.println(dump);
        return meta;
    } catch (DependencyException | ServiceException | EnumerationException e) {
        exception = e;
    }
    System.err.println("Failed to populate OME-XML metadata object.");
    return null;
}
Also used : PositiveInteger(ome.xml.model.primitives.PositiveInteger) ServiceFactory(loci.common.services.ServiceFactory) NonNegativeInteger(ome.xml.model.primitives.NonNegativeInteger) Time(ome.units.quantity.Time) DependencyException(loci.common.services.DependencyException) CoreMetadata(loci.formats.CoreMetadata) EnumerationException(ome.xml.model.enums.EnumerationException) ServiceException(loci.common.services.ServiceException) DependencyException(loci.common.services.DependencyException) FormatException(loci.formats.FormatException) IOException(java.io.IOException) OMEXMLService(loci.formats.services.OMEXMLService) ServiceException(loci.common.services.ServiceException) OMEXMLMetadata(loci.formats.ome.OMEXMLMetadata) EnumerationException(ome.xml.model.enums.EnumerationException)

Example 38 with DependencyException

use of loci.common.services.DependencyException in project bioformats by openmicroscopy.

the class MultiFileExportExample method main.

public static void main(String[] args) throws FormatException, IOException {
    if (args.length < 2) {
        System.out.println("Usage: java MultiFileExportExample <infile> <output file extension>");
        System.exit(1);
    }
    ImageReader reader = new ImageReader();
    IMetadata metadata;
    try {
        ServiceFactory factory = new ServiceFactory();
        OMEXMLService service = factory.getInstance(OMEXMLService.class);
        metadata = service.createOMEXMLMetadata();
    } catch (DependencyException exc) {
        throw new FormatException("Could not create OME-XML store.", exc);
    } catch (ServiceException exc) {
        throw new FormatException("Could not create OME-XML store.", exc);
    }
    reader.setMetadataStore(metadata);
    reader.setId(args[0]);
    ImageWriter writer = new ImageWriter();
    writer.setMetadataRetrieve(metadata);
    String baseFile = args[0].substring(0, args[0].lastIndexOf("."));
    writer.setId(baseFile + "_s0_z0" + args[1]);
    for (int series = 0; series < reader.getSeriesCount(); series++) {
        reader.setSeries(series);
        writer.setSeries(series);
        int planesPerFile = reader.getImageCount() / reader.getSizeZ();
        for (int z = 0; z < reader.getSizeZ(); z++) {
            String file = baseFile + "_s" + series + "_z" + z + args[1];
            writer.changeOutputFile(file);
            for (int image = 0; image < planesPerFile; image++) {
                int[] zct = FormatTools.getZCTCoords(reader.getDimensionOrder(), 1, reader.getEffectiveSizeC(), reader.getSizeT(), planesPerFile, image);
                int index = FormatTools.getIndex(reader, z, zct[1], zct[2]);
                writer.saveBytes(image, reader.openBytes(index));
            }
        }
    }
    reader.close();
    writer.close();
}
Also used : IMetadata(loci.formats.meta.IMetadata) ServiceException(loci.common.services.ServiceException) ServiceFactory(loci.common.services.ServiceFactory) ImageWriter(loci.formats.ImageWriter) ImageReader(loci.formats.ImageReader) DependencyException(loci.common.services.DependencyException) OMEXMLService(loci.formats.services.OMEXMLService) FormatException(loci.formats.FormatException)

Example 39 with DependencyException

use of loci.common.services.DependencyException in project bioformats by openmicroscopy.

the class VeecoReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
protected void initFile(String id) throws FormatException, IOException {
    super.initFile(id);
    CoreMetadata m = core.get(0);
    try {
        ServiceFactory factory = new ServiceFactory();
        netcdf = factory.getInstance(NetCDFService.class);
        netcdf.setFile(id);
    } catch (DependencyException e) {
        throw new MissingLibraryException(e);
    }
    Vector<String> variableList = netcdf.getVariableList();
    if (variableList.size() == 0) {
        throw new FormatException("No image data found");
    }
    String imageName = variableList.get(0);
    try {
        image = netcdf.getVariableValue(imageName);
    } catch (ServiceException e) {
        throw new FormatException("Could not retrieve image data", e);
    }
    Hashtable<String, Object> attributes = netcdf.getVariableAttributes(imageName);
    for (String attr : attributes.keySet()) {
        addGlobalMeta(attr, attributes.get(attr));
    }
    if (image instanceof byte[][]) {
        byte[][] byteImage = (byte[][]) image;
        m.sizeX = byteImage[0].length;
        m.sizeY = byteImage.length;
        m.pixelType = FormatTools.INT8;
    } else if (image instanceof short[][]) {
        short[][] shortImage = (short[][]) image;
        m.sizeX = shortImage[0].length;
        m.sizeY = shortImage.length;
        m.pixelType = FormatTools.INT16;
        // set the endianness to use when unpacking pixels
        // NetCDF may not return the pixels with a constant endianness,
        // so this ensures that the reader corrects accordingly (see ticket 12085)
        short nativeMin = 0;
        short nativeMax = 0;
        short swappedMin = 0;
        short swappedMax = 0;
        for (int y = 0; y < shortImage.length; y++) {
            for (int x = 0; x < shortImage[y].length; x++) {
                if (shortImage[y][x] < nativeMin) {
                    nativeMin = shortImage[y][x];
                }
                if (shortImage[y][x] > nativeMax) {
                    nativeMax = shortImage[y][x];
                }
                short swapped = DataTools.swap(shortImage[y][x]);
                if (swapped < swappedMin) {
                    swappedMin = swapped;
                }
                if (swapped > swappedMax) {
                    swappedMax = swapped;
                }
            }
        }
        unpackEndian = nativeMin <= swappedMin && nativeMax >= swappedMax;
    }
    m.sizeZ = 1;
    m.sizeC = 1;
    m.sizeT = 1;
    m.imageCount = m.sizeZ * m.sizeC * m.sizeT;
    m.dimensionOrder = "XYCZT";
    m.littleEndian = false;
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
}
Also used : ServiceFactory(loci.common.services.ServiceFactory) DependencyException(loci.common.services.DependencyException) CoreMetadata(loci.formats.CoreMetadata) FormatException(loci.formats.FormatException) MetadataStore(loci.formats.meta.MetadataStore) ServiceException(loci.common.services.ServiceException) MissingLibraryException(loci.formats.MissingLibraryException) NetCDFService(loci.formats.services.NetCDFService)

Example 40 with DependencyException

use of loci.common.services.DependencyException in project bioformats by openmicroscopy.

the class CellH5Writer method setId.

// -- FormatWriter API methods --
/* @see loci.formats.FormatWriter#setId(String) */
@Override
public void setId(String id) throws FormatException, IOException {
    if (id.equals(currentId))
        return;
    super.setId(id);
    try {
        ServiceFactory factory = new ServiceFactory();
        jhdf = factory.getInstance(JHDFService.class);
        jhdf.setFileForWrite(id);
    } catch (DependencyException e) {
        throw new MissingLibraryException(JHDFServiceImpl.NO_JHDF_MSG, e);
    }
    MetadataRetrieve retrieve = getMetadataRetrieve();
    int sizeX = retrieve.getPixelsSizeX(0).getValue();
    int sizeY = retrieve.getPixelsSizeY(0).getValue();
    int sizeZ = retrieve.getPixelsSizeZ(0).getValue();
    int sizeC = retrieve.getPixelsSizeC(0).getValue();
    int sizeT = retrieve.getPixelsSizeT(0).getValue();
    int type = FormatTools.pixelTypeFromString(retrieve.getPixelsType(0).toString());
    bpp = FormatTools.getBytesPerPixel(type);
    LOGGER.info("CellH5Writer: Found image with dimensions XYZCT {}x{}x{}x{}x{}, and bits per pixel {}", sizeX, sizeY, sizeZ, sizeC, sizeT, bpp);
    String plate = "PLATE_00";
    String well = "WELL_00";
    int site = 1;
    if (retrieve.getPlateCount() > 0) {
        plate = retrieve.getPlateName(0);
        well = retrieve.getWellExternalIdentifier(0, 0);
        site = retrieve.getWellSampleIndex(0, 0, 0).getValue();
        LOGGER.info("CellH5Writer: Found plate information Plate / Well / Site {} / {} / {}", plate, well, site);
    } else {
        LOGGER.info("CellH5Writer: No plate information found. Using default values...");
    }
    jhdf.createGroup(CellH5Constants.DEFINITION + CellH5Reader.CellH5Constants.OBJECT);
    jhdf.createGroup(CellH5Constants.DEFINITION + CellH5Reader.CellH5Constants.FEATURE);
    jhdf.createGroup(CellH5Constants.DEFINITION + CellH5Reader.CellH5Constants.IMAGE);
    outputPath = String.format("/sample/0/plate/%s/experiment/%s/position/%d/image/channel", plate, well, site);
    jhdf.initIntArray(outputPath, new long[] { sizeC, sizeT, sizeZ, sizeY, sizeX }, bpp);
}
Also used : ServiceFactory(loci.common.services.ServiceFactory) MissingLibraryException(loci.formats.MissingLibraryException) JHDFService(loci.formats.services.JHDFService) DependencyException(loci.common.services.DependencyException) MetadataRetrieve(loci.formats.meta.MetadataRetrieve)

Aggregations

DependencyException (loci.common.services.DependencyException)51 ServiceFactory (loci.common.services.ServiceFactory)49 FormatException (loci.formats.FormatException)39 ServiceException (loci.common.services.ServiceException)32 OMEXMLService (loci.formats.services.OMEXMLService)32 MissingLibraryException (loci.formats.MissingLibraryException)15 MetadataStore (loci.formats.meta.MetadataStore)14 CoreMetadata (loci.formats.CoreMetadata)13 IMetadata (loci.formats.meta.IMetadata)13 IOException (java.io.IOException)12 Location (loci.common.Location)10 ImageReader (loci.formats.ImageReader)10 Length (ome.units.quantity.Length)10 PositiveInteger (ome.xml.model.primitives.PositiveInteger)9 OMEXMLMetadata (loci.formats.ome.OMEXMLMetadata)8 ArrayList (java.util.ArrayList)7 MetadataRetrieve (loci.formats.meta.MetadataRetrieve)6 EnumerationException (ome.xml.model.enums.EnumerationException)6 File (java.io.File)5 IFormatWriter (loci.formats.IFormatWriter)5