Search in sources :

Example 81 with Location

use of loci.common.Location in project bioformats by openmicroscopy.

the class MicromanagerReader method buildTIFFList.

/**
 * Populate the list of TIFF files using the given file name as a pattern.
 */
private void buildTIFFList(int posIndex, String baseTiff) {
    LOGGER.info("Building list of TIFFs");
    Position p = positions.get(posIndex);
    if ((p.tiffs.size() > 0 && p.tiffs.size() == p.fileNameMap.size()) || baseTiff == null) {
        return;
    }
    p.tiffs.clear();
    String prefix = "";
    if (baseTiff.indexOf(File.separator) != -1) {
        prefix = baseTiff.substring(0, baseTiff.lastIndexOf(File.separator) + 1);
        baseTiff = baseTiff.substring(baseTiff.lastIndexOf(File.separator) + 1);
    }
    String[] blocks = baseTiff.split("_");
    StringBuilder filename = new StringBuilder();
    for (int t = 0; t < getSizeT(); t++) {
        for (int c = 0; c < getSizeC(); c++) {
            for (int z = 0; z < getSizeZ(); z++) {
                // file names are of format:
                // img_<T>_<channel name>_<T>.tif
                filename.append(prefix);
                if (!prefix.endsWith(File.separator) && !blocks[0].startsWith(File.separator)) {
                    filename.append(File.separator);
                }
                filename.append(blocks[0]);
                filename.append("_");
                int zeros = blocks[1].length() - String.valueOf(t).length();
                for (int q = 0; q < zeros; q++) {
                    filename.append("0");
                }
                filename.append(t);
                filename.append("_");
                String prechannel = filename.toString();
                if (blocks[2].length() > 0) {
                    String channel = p.channels[c];
                    if (channel.indexOf('-') != -1) {
                        channel = channel.substring(0, channel.indexOf('-'));
                    }
                    filename.append(channel);
                }
                filename.append("_");
                zeros = blocks[3].length() - String.valueOf(z).length() - 4;
                for (int q = 0; q < zeros; q++) {
                    filename.append("0");
                }
                filename.append(z);
                filename.append(".tif");
                if (!new Location(filename.toString()).exists() && blocks[2].length() > 0) {
                    // rewind and try using the full channel name
                    filename = new StringBuilder(prechannel);
                    String channel = p.channels[c];
                    filename.append(channel);
                    filename.append("_");
                    zeros = blocks[3].length() - String.valueOf(z).length() - 4;
                    for (int q = 0; q < zeros; q++) {
                        filename.append("0");
                    }
                    filename.append(z);
                    filename.append(".tif");
                }
                p.tiffs.add(filename.toString());
                filename.delete(0, filename.length());
            }
        }
    }
    // adjust timepoint count, if needed
    // acquisitions can be stopped part-way through, but this isn't always
    // noted in the metadata
    int firstEmptyTimepoint = -1;
    int nextFile = 0;
    for (int t = 0; t < getSizeT(); t++) {
        boolean emptyTimepoint = true;
        for (int c = 0; c < getSizeC(); c++) {
            for (int z = 0; z < getSizeZ(); z++) {
                String file = p.tiffs.get(nextFile++);
                if (new Location(file).exists()) {
                    emptyTimepoint = false;
                    break;
                }
            }
            if (!emptyTimepoint) {
                break;
            }
        }
        if (emptyTimepoint && firstEmptyTimepoint < 0) {
            firstEmptyTimepoint = t;
        } else if (!emptyTimepoint && firstEmptyTimepoint >= 0) {
            firstEmptyTimepoint = -1;
        }
    }
    if (firstEmptyTimepoint >= 0) {
        int imageCount = getImageCount() / getSizeT();
        core.get(posIndex).sizeT = firstEmptyTimepoint;
        core.get(posIndex).imageCount = imageCount * getSizeT();
    }
}
Also used : Location(loci.common.Location)

Example 82 with Location

use of loci.common.Location in project bioformats by openmicroscopy.

the class MicromanagerReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
public void initFile(String id) throws FormatException, IOException {
    super.initFile(id);
    tiffReader = new MinimalTiffReader();
    positions = new Vector<Position>();
    LOGGER.info("Reading metadata file");
    // find metadata.txt
    Location file = new Location(currentId).getAbsoluteFile();
    Location parentFile = file.getParentFile();
    if (file.exists()) {
        if (parentFile.getName().indexOf("Pos_") >= 0) {
            parentFile = parentFile.getParentFile();
            String[] dirs = parentFile.list(true);
            Arrays.sort(dirs);
            for (String dir : dirs) {
                if (dir.indexOf("Pos_") >= 0) {
                    Position pos = new Position();
                    Location posDir = new Location(parentFile, dir);
                    pos.metadataFile = new Location(posDir, METADATA).getAbsolutePath();
                    positions.add(pos);
                }
            }
        } else {
            Position pos = new Position();
            Location metadata = new Location(parentFile, METADATA);
            if (!metadata.exists()) {
                if (file.getName().endsWith(METADATA)) {
                    metadata = file;
                } else {
                    metadata = new Location(parentFile, getPrefixMetadataName(file.getName()));
                }
            }
            pos.metadataFile = metadata.getAbsolutePath();
            positions.add(pos);
        }
    }
    int seriesCount = positions.size();
    core.clear();
    for (int i = 0; i < seriesCount; i++) {
        core.add(new CoreMetadata());
        setSeries(i);
        parsePosition(i);
    }
    setSeries(0);
    populateMetadata();
}
Also used : CoreMetadata(loci.formats.CoreMetadata) Location(loci.common.Location)

Example 83 with Location

use of loci.common.Location in project bioformats by openmicroscopy.

the class MicromanagerReader method buildTIFFList.

private void buildTIFFList(int posIndex) throws FormatException {
    Position p = positions.get(posIndex);
    CoreMetadata ms = core.get(posIndex);
    String parent = new Location(p.metadataFile).getParent();
    LOGGER.info("Finding image file names");
    // find the name of a TIFF file
    if (p.tiffs == null) {
        p.tiffs = new Vector<String>();
    }
    if (p.baseTiff != null) {
        buildTIFFList(posIndex, parent + File.separator + p.baseTiff);
    }
    if (p.tiffs.size() == 0) {
        Vector<String> uniqueZ = new Vector<String>();
        Vector<String> uniqueC = new Vector<String>();
        Vector<String> uniqueT = new Vector<String>();
        Location dir = new Location(p.metadataFile).getAbsoluteFile().getParentFile();
        String[] files = dir.list(true);
        Arrays.sort(files);
        for (String f : files) {
            if (checkSuffix(f, "tif") || checkSuffix(f, "tiff")) {
                String[] blocks = f.split("_");
                if (!uniqueT.contains(blocks[1]))
                    uniqueT.add(blocks[1]);
                if (!uniqueC.contains(blocks[2]))
                    uniqueC.add(blocks[2]);
                if (!uniqueZ.contains(blocks[3]))
                    uniqueZ.add(blocks[3]);
                String path = new Location(dir, f).getAbsolutePath();
                p.tiffs.add(path);
            }
        }
        if (getSizeZ() * getSizeC() * getSizeT() != uniqueZ.size() * uniqueC.size() * uniqueT.size()) {
            ms.sizeZ = uniqueZ.size();
            ms.sizeC = uniqueC.size();
            ms.sizeT = uniqueT.size();
        }
        if (p.tiffs.size() == 0) {
            throw new FormatException("Could not find TIFF files.");
        }
    }
}
Also used : CoreMetadata(loci.formats.CoreMetadata) Vector(java.util.Vector) FormatException(loci.formats.FormatException) Location(loci.common.Location)

Example 84 with Location

use of loci.common.Location in project bioformats by openmicroscopy.

the class MicromanagerReader method isThisType.

/* @see loci.formats.IFormatReader#isThisType(String, boolean) */
@Override
public boolean isThisType(String name, boolean open) {
    // not allowed to touch the file system
    if (!open)
        return false;
    if (name.equals(METADATA) || name.endsWith(File.separator + METADATA) || name.equals(XML) || name.endsWith(File.separator + XML) || name.endsWith("_" + METADATA)) {
        final int blockSize = 1048576;
        try {
            RandomAccessInputStream stream = new RandomAccessInputStream(name);
            long length = stream.length();
            String data = stream.readString((int) Math.min(blockSize, length));
            data = data.toLowerCase();
            stream.close();
            return length > 0 && (data.indexOf("micro-manager") >= 0 || data.indexOf("micromanager") >= 0);
        } catch (IOException e) {
            return false;
        }
    } else if (!isGroupFiles()) {
        // is chosen
        return false;
    }
    try {
        Location thisFile = new Location(name).getAbsoluteFile();
        Location parent = thisFile.getParentFile();
        Location metaFile = new Location(parent, METADATA);
        if (!metaFile.exists()) {
            metaFile = new Location(parent, getPrefixMetadataName(thisFile.getName()));
        }
        RandomAccessInputStream s = new RandomAccessInputStream(name);
        boolean validTIFF = isThisType(s);
        s.close();
        return validTIFF && isThisType(metaFile.getAbsolutePath(), open);
    } catch (NullPointerException e) {
    } catch (IOException e) {
    }
    return false;
}
Also used : RandomAccessInputStream(loci.common.RandomAccessInputStream) IOException(java.io.IOException) Location(loci.common.Location)

Example 85 with Location

use of loci.common.Location in project bioformats by openmicroscopy.

the class OMETiffReader method isSingleFile.

// -- IFormatReader API methods --
/* @see loci.formats.IFormatReader#isSingleFile(String) */
@Override
public boolean isSingleFile(String id) throws FormatException, IOException {
    // companion files in a binary-only dataset should always have additional files
    if (checkSuffix(id, "companion.ome")) {
        return false;
    }
    // parse and populate OME-XML metadata
    String fileName = new Location(id).getAbsoluteFile().getAbsolutePath();
    RandomAccessInputStream ras = new RandomAccessInputStream(fileName, 16);
    TiffParser tp = new TiffParser(ras);
    IFD ifd = tp.getFirstIFD();
    long[] ifdOffsets = tp.getIFDOffsets();
    ras.close();
    String xml = ifd.getComment();
    if (service == null)
        setupService();
    OMEXMLMetadata meta;
    try {
        meta = service.createOMEXMLMetadata(xml);
        metaFile = new Location(id).getAbsolutePath();
    } catch (ServiceException se) {
        throw new FormatException(se);
    }
    if (meta.getRoot() == null) {
        throw new FormatException("Could not parse OME-XML from TIFF comment");
    }
    int nImages = 0;
    for (int i = 0; i < meta.getImageCount(); i++) {
        int nChannels = meta.getChannelCount(i);
        if (nChannels == 0)
            nChannels = 1;
        int z = meta.getPixelsSizeZ(i).getValue().intValue();
        int t = meta.getPixelsSizeT(i).getValue().intValue();
        nImages += z * t * nChannels;
    }
    return nImages > 0 && nImages <= ifdOffsets.length;
}
Also used : ServiceException(loci.common.services.ServiceException) IFD(loci.formats.tiff.IFD) OMEXMLMetadata(loci.formats.ome.OMEXMLMetadata) TiffParser(loci.formats.tiff.TiffParser) RandomAccessInputStream(loci.common.RandomAccessInputStream) FormatException(loci.formats.FormatException) Location(loci.common.Location)

Aggregations

Location (loci.common.Location)185 CoreMetadata (loci.formats.CoreMetadata)55 MetadataStore (loci.formats.meta.MetadataStore)51 FormatException (loci.formats.FormatException)49 ArrayList (java.util.ArrayList)47 RandomAccessInputStream (loci.common.RandomAccessInputStream)47 Length (ome.units.quantity.Length)34 IOException (java.io.IOException)28 Timestamp (ome.xml.model.primitives.Timestamp)28 Time (ome.units.quantity.Time)20 IFD (loci.formats.tiff.IFD)15 TiffParser (loci.formats.tiff.TiffParser)15 NonNegativeInteger (ome.xml.model.primitives.NonNegativeInteger)15 PositiveInteger (ome.xml.model.primitives.PositiveInteger)15 DependencyException (loci.common.services.DependencyException)13 ServiceException (loci.common.services.ServiceException)12 File (java.io.File)11 ServiceFactory (loci.common.services.ServiceFactory)11 IniList (loci.common.IniList)10 FilePattern (loci.formats.FilePattern)10