Search in sources :

Example 66 with ServiceFactory

use of loci.common.services.ServiceFactory 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 67 with ServiceFactory

use of loci.common.services.ServiceFactory 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 68 with ServiceFactory

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

the class MakeLZW method main.

public static void main(String[] args) throws Exception {
    ImageReader reader = new ImageReader();
    ServiceFactory factory = new ServiceFactory();
    OMEXMLService service = factory.getInstance(OMEXMLService.class);
    IMetadata omexmlMeta = service.createOMEXMLMetadata();
    reader.setMetadataStore(omexmlMeta);
    TiffWriter writer = new TiffWriter();
    for (int i = 0; i < args.length; i++) {
        String inFile = args[i];
        String outFile = "lzw-" + inFile;
        System.out.print("Converting " + inFile + " to " + outFile);
        reader.setId(inFile);
        writer.setMetadataRetrieve(omexmlMeta);
        writer.setCompression("LZW");
        writer.setId(outFile);
        int planeCount = reader.getImageCount();
        for (int p = 0; p < planeCount; p++) {
            System.out.print(".");
            byte[] plane = reader.openBytes(p);
            writer.saveBytes(p, plane);
        }
        System.out.println(" [done]");
    }
}
Also used : IMetadata(loci.formats.meta.IMetadata) TiffWriter(loci.formats.out.TiffWriter) ServiceFactory(loci.common.services.ServiceFactory) ImageReader(loci.formats.ImageReader) OMEXMLService(loci.formats.services.OMEXMLService)

Example 69 with ServiceFactory

use of loci.common.services.ServiceFactory 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 70 with ServiceFactory

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

the class PrintLensNA method main.

public static void main(String[] args) throws DependencyException, FormatException, IOException, ServiceException {
    // parse command line arguments
    if (args.length < 1) {
        System.err.println("Usage: java PrintLensNA imageFile");
        System.exit(1);
    }
    String id = args[0];
    // configure reader
    IFormatReader reader = new ImageReader();
    ServiceFactory factory = new ServiceFactory();
    OMEXMLService service = factory.getInstance(OMEXMLService.class);
    IMetadata meta = service.createOMEXMLMetadata();
    reader.setMetadataStore(meta);
    System.out.println("Initializing file: " + id);
    // parse metadata
    reader.setId(id);
    // output metadata values
    int instrumentCount = meta.getInstrumentCount();
    System.out.println("There are " + instrumentCount + " instrument(s) associated with this file");
    for (int i = 0; i < instrumentCount; i++) {
        int objectiveCount = meta.getObjectiveCount(i);
        System.out.println();
        System.out.println("Instrument #" + i + " [" + meta.getInstrumentID(i) + "]: " + objectiveCount + " objective(s) found");
        for (int o = 0; o < objectiveCount; o++) {
            Double lensNA = meta.getObjectiveLensNA(i, o);
            System.out.println("\tObjective #" + o + " [" + meta.getObjectiveID(i, o) + "]: LensNA=" + lensNA);
        }
    }
}
Also used : IFormatReader(loci.formats.IFormatReader) IMetadata(loci.formats.meta.IMetadata) ServiceFactory(loci.common.services.ServiceFactory) ImageReader(loci.formats.ImageReader) OMEXMLService(loci.formats.services.OMEXMLService)

Aggregations

ServiceFactory (loci.common.services.ServiceFactory)94 OMEXMLService (loci.formats.services.OMEXMLService)73 DependencyException (loci.common.services.DependencyException)49 FormatException (loci.formats.FormatException)38 IMetadata (loci.formats.meta.IMetadata)35 ServiceException (loci.common.services.ServiceException)30 ImageReader (loci.formats.ImageReader)23 MissingLibraryException (loci.formats.MissingLibraryException)15 BeforeMethod (org.testng.annotations.BeforeMethod)15 MetadataStore (loci.formats.meta.MetadataStore)14 PositiveInteger (ome.xml.model.primitives.PositiveInteger)14 IOException (java.io.IOException)13 CoreMetadata (loci.formats.CoreMetadata)13 Length (ome.units.quantity.Length)12 Location (loci.common.Location)11 OMEXMLMetadata (loci.formats.ome.OMEXMLMetadata)10 InputStream (java.io.InputStream)9 ArrayList (java.util.ArrayList)8 MetadataRetrieve (loci.formats.meta.MetadataRetrieve)8 BeforeClass (org.testng.annotations.BeforeClass)8