Search in sources :

Example 11 with FilePattern

use of loci.formats.FilePattern in project bioformats by openmicroscopy.

the class BioRadReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
    // always initialize a PIC file, even if we were given something else
    if (!checkSuffix(id, PIC_SUFFIX)) {
        Location dir = new Location(id).getAbsoluteFile().getParentFile();
        String[] list = dir.list(true);
        for (int i = 0; i < list.length; i++) {
            if (checkSuffix(list[i], PIC_SUFFIX)) {
                id = new Location(dir.getAbsolutePath(), list[i]).getAbsolutePath();
            }
        }
        if (!checkSuffix(id, PIC_SUFFIX)) {
            throw new FormatException("No .pic files found - invalid dataset.");
        }
    }
    super.initFile(id);
    in = new RandomAccessInputStream(id);
    in.order(true);
    offset = new ArrayList<Double>();
    gain = new ArrayList<Double>();
    used = new ArrayList<String>();
    used.add(new Location(currentId).getAbsolutePath());
    LOGGER.info("Reading image dimensions");
    noteStrings = new ArrayList<Note>();
    // read header
    CoreMetadata m = core.get(0);
    m.sizeX = in.readShort();
    m.sizeY = in.readShort();
    int npic = in.readShort();
    m.imageCount = npic;
    int ramp1min = in.readShort();
    int ramp1max = in.readShort();
    boolean notes = in.readInt() != 0;
    m.pixelType = in.readShort() == 0 ? FormatTools.UINT16 : FormatTools.UINT8;
    int imageNumber = in.readShort();
    String name = in.readString(32);
    for (int i = 0; i < name.length(); i++) {
        if (name.charAt(i) == 0) {
            name = name.substring(0, i);
            break;
        }
    }
    float magFactor = 1f;
    int lens = 0;
    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        int merged = in.readShort();
        int color1 = in.readShort();
        int fileId = in.readShort();
        int ramp2min = in.readShort();
        int ramp2max = in.readShort();
        int color2 = in.readShort();
        int edited = in.readShort();
        lens = in.readShort();
        magFactor = in.readFloat();
        // check validity of header
        if (fileId != PIC_FILE_ID) {
            throw new FormatException("Invalid file header : " + fileId);
        }
        // populate metadata fields
        addGlobalMeta("nx", getSizeX());
        addGlobalMeta("ny", getSizeY());
        addGlobalMeta("npic", getImageCount());
        addGlobalMeta("ramp1_min", ramp1min);
        addGlobalMeta("ramp1_max", ramp1max);
        addGlobalMeta("notes", notes);
        addGlobalMeta("image_number", imageNumber);
        addGlobalMeta("name", name);
        addGlobalMeta("merged", MERGE_NAMES[merged]);
        addGlobalMeta("color1", color1);
        addGlobalMeta("file_id", fileId);
        addGlobalMeta("ramp2_min", ramp2min);
        addGlobalMeta("ramp2_max", ramp2max);
        addGlobalMeta("color2", color2);
        addGlobalMeta("edited", edited);
        addGlobalMeta("lens", lens);
        addGlobalMeta("mag_factor", magFactor);
    } else
        in.skipBytes(20);
    // skip image data
    int imageLen = getSizeX() * getSizeY();
    int bpp = FormatTools.getBytesPerPixel(getPixelType());
    in.skipBytes(bpp * getImageCount() * imageLen + 6);
    m.sizeZ = getImageCount();
    m.sizeC = 1;
    m.sizeT = 1;
    m.orderCertain = false;
    m.rgb = false;
    m.interleaved = false;
    m.littleEndian = LITTLE_ENDIAN;
    m.metadataComplete = true;
    m.falseColor = true;
    LOGGER.info("Reading notes");
    MetadataStore store = makeFilterMetadata();
    // read notes
    readNotes(in, true);
    LOGGER.info("Populating metadata");
    // look for companion metadata files
    final List<String> pics = new ArrayList<String>();
    if (isGroupFiles()) {
        Location parent = new Location(currentId).getAbsoluteFile().getParentFile();
        String parentPath = parent.getAbsolutePath();
        String[] list = parent.list(true);
        Arrays.sort(list);
        for (int i = 0; i < list.length; i++) {
            if (list[i].endsWith("lse.xml")) {
                String path = new Location(parentPath, list[i]).getAbsolutePath();
                used.add(path);
                DefaultHandler handler = new BioRadHandler();
                RandomAccessInputStream xml = new RandomAccessInputStream(path);
                XMLTools.parseXML(xml, handler);
                xml.close();
                used.remove(currentId);
                for (int q = 0; q < list.length; q++) {
                    if (checkSuffix(list[q], PIC_SUFFIX)) {
                        path = new Location(parentPath, list[q]).getAbsolutePath();
                        pics.add(path);
                        if (!used.contains(path))
                            used.add(path);
                    }
                }
            } else if (list[i].endsWith("data.raw")) {
                used.add(new Location(parentPath, list[i]).getAbsolutePath());
            }
        }
    }
    // populate Pixels
    m.dimensionOrder = "XYCTZ";
    boolean multipleFiles = parseNotes(store);
    if (multipleFiles && isGroupFiles() && pics.isEmpty()) {
        // do file grouping
        used.remove(currentId);
        long length = new Location(currentId).length();
        FilePattern pattern = new FilePattern(new Location(id).getAbsoluteFile());
        String[] patternFiles = pattern.getFiles();
        for (String file : patternFiles) {
            Location f = new Location(file);
            if (f.length() == length) {
                pics.add(file);
                if (!used.contains(f.getAbsolutePath())) {
                    used.add(f.getAbsolutePath());
                }
            }
        }
        if (pics.size() == 1)
            m.sizeC = 1;
    }
    picFiles = pics.toArray(new String[pics.size()]);
    Arrays.sort(picFiles);
    if (picFiles.length > 0) {
        if (getSizeC() == 0)
            m.sizeC = 1;
        m.imageCount = npic * picFiles.length;
        if (multipleFiles) {
            m.sizeT = getImageCount() / (getSizeZ() * getSizeC());
        } else
            m.sizeC = getImageCount() / (getSizeZ() * getSizeT());
    } else
        picFiles = null;
    if (getEffectiveSizeC() != getSizeC() && !isRGB()) {
        m.sizeC = 1;
    }
    LOGGER.info("Reading lookup tables");
    lut = new byte[getEffectiveSizeC()][][];
    for (int channel = 0; channel < lut.length; channel++) {
        int plane = getIndex(0, channel, 0);
        String file = picFiles == null ? currentId : picFiles[plane % picFiles.length];
        LOGGER.trace("reading table for C = {} from {}", channel, file);
        RandomAccessInputStream s = new RandomAccessInputStream(file);
        s.order(true);
        readLookupTables(s);
        s.close();
        if (lut == null)
            break;
    }
    m.indexed = lut != null;
    MetadataTools.populatePixels(store, this);
    store.setImageName(name, 0);
    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        // link Instrument and Image
        String instrumentID = MetadataTools.createLSID("Instrument", 0);
        store.setInstrumentID(instrumentID, 0);
        store.setImageInstrumentRef(instrumentID, 0);
        // link Objective to Image using ObjectiveSettings
        String objectiveID = MetadataTools.createLSID("Objective", 0, 0);
        store.setObjectiveID(objectiveID, 0, 0);
        store.setObjectiveSettingsID(objectiveID, 0);
        store.setObjectiveLensNA(new Double(lens), 0, 0);
        store.setObjectiveNominalMagnification(new Double(magFactor), 0, 0);
        store.setObjectiveCorrection(getCorrection("Other"), 0, 0);
        store.setObjectiveImmersion(getImmersion("Other"), 0, 0);
        // link Detector to Image
        for (int i = 0; i < getEffectiveSizeC(); i++) {
            Double detectorOffset = i < offset.size() ? offset.get(i) : null;
            Double detectorGain = i < gain.size() ? gain.get(i) : null;
            if (detectorOffset != null || detectorGain != null) {
                String detectorID = MetadataTools.createLSID("Detector", 0, i);
                store.setDetectorSettingsID(detectorID, 0, i);
                store.setDetectorID(detectorID, 0, i);
                store.setDetectorType(getDetectorType("Other"), 0, i);
            }
            if (detectorOffset != null) {
                store.setDetectorSettingsOffset(detectorOffset, 0, i);
            }
            if (detectorGain != null) {
                store.setDetectorSettingsGain(detectorGain, 0, i);
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) CoreMetadata(loci.formats.CoreMetadata) FormatException(loci.formats.FormatException) DefaultHandler(org.xml.sax.helpers.DefaultHandler) MetadataStore(loci.formats.meta.MetadataStore) RandomAccessInputStream(loci.common.RandomAccessInputStream) FilePattern(loci.formats.FilePattern) Location(loci.common.Location)

Example 12 with FilePattern

use of loci.formats.FilePattern in project bioformats by openmicroscopy.

the class FilePatternTest method testFindPattern.

@Test(dataProvider = "booleanStates")
public void testFindPattern(Boolean createFiles) throws IOException {
    int minZ = 1, maxZ = 2;
    int minT = 1, maxT = 12;
    int minC = 9, maxC = 11;
    // 2nd prefix empty to test sub-block detection
    String[] prefixes = { "foo_", "", "c" };
    String[] formats = { "%d", "%02d", "%d" };
    String[] blocks = { String.format("<%d-%d>", minZ, maxZ), String.format("<%02d-%02d>", minT, maxT), String.format("<%d-%d>", minC, maxC) };
    String suffix = ".ext";
    String pattern = mkPattern(prefixes, blocks, suffix);
    List<String> names = new ArrayList<String>();
    names.add("outlier.ext");
    for (int z = minZ; z <= maxZ; z++) {
        for (int t = minT; t <= maxT; t++) {
            for (int c = minC; c <= maxC; c++) {
                names.add(mkPattern(prefixes, new String[] { String.format(formats[0], z), String.format(formats[1], t), String.format(formats[2], c) }, suffix));
            }
        }
    }
    String[] namesA = names.toArray(new String[names.size()]);
    if (!createFiles) {
        assertEquals(FilePattern.findPattern(namesA[1], null, namesA), pattern);
        // test excludeAxes
        String[] minCBlocks = new String[] { blocks[0], blocks[1], Integer.toString(minC) };
        int[] excl = new int[] { AxisGuesser.C_AXIS };
        assertEquals(FilePattern.findPattern(namesA[1], null, namesA, excl), mkPattern(prefixes, minCBlocks, suffix));
        return;
    }
    Path wd = Files.createTempDirectory("");
    wd.toFile().deleteOnExit();
    String absPattern = resolveToString(wd, pattern);
    String[] fullNames = mkFiles(wd, namesA);
    assertEquals(FilePattern.findPattern(fullNames[1]), absPattern);
    assertEquals(FilePattern.findPattern(namesA[1], wd.toString()), absPattern);
    assertEquals(FilePattern.findPattern(new File(fullNames[1])), absPattern);
    assertEquals(FilePattern.findPattern(new Location(fullNames[1])), absPattern);
    // test constructors that use findPattern
    FilePattern fp = new FilePattern(new Location(fullNames[1]));
    assertEquals(fp.getPattern(), absPattern);
    fp = new FilePattern(namesA[1], wd.toString());
    assertEquals(fp.getPattern(), absPattern);
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) FilePattern(loci.formats.FilePattern) File(java.io.File) Location(loci.common.Location) Test(org.testng.annotations.Test)

Example 13 with FilePattern

use of loci.formats.FilePattern in project bioformats by openmicroscopy.

the class FilePatternTest method testSingleFile.

@Test
public void testSingleFile() {
    String pattern = "test.fake";
    FilePattern fp = new FilePattern(pattern);
    assertTrue(fp.isValid());
    assertTrue(fp.isRegex());
    assertEquals(fp.getPattern(), pattern);
    assertEquals(fp.getFiles(), new String[] { pattern });
}
Also used : FilePattern(loci.formats.FilePattern) Test(org.testng.annotations.Test)

Example 14 with FilePattern

use of loci.formats.FilePattern in project bioformats by openmicroscopy.

the class FilePatternTest method testBadPatterns.

@Test(dataProvider = "badPatterns")
public void testBadPatterns(String pattern) {
    FilePattern fp = new FilePattern(pattern);
    assertFalse(fp.isValid());
    assertNotNull(fp.getErrorMessage());
}
Also used : FilePattern(loci.formats.FilePattern) Test(org.testng.annotations.Test)

Example 15 with FilePattern

use of loci.formats.FilePattern in project bioformats by openmicroscopy.

the class TCSReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
    Location l = new Location(id).getAbsoluteFile();
    Location parent = l.getParentFile();
    String[] list = parent.list();
    Arrays.sort(list);
    boolean isXML = checkSuffix(id, XML_SUFFIX);
    if (list != null) {
        for (String file : list) {
            if (checkSuffix(file, XML_SUFFIX) && !isXML && isGroupFiles()) {
                xmlFile = new Location(parent, file).getAbsolutePath();
                break;
            } else if (checkSuffix(file, TiffReader.TIFF_SUFFIXES) && isXML) {
                initFile(new Location(parent, file).getAbsolutePath());
                return;
            }
        }
    }
    if (isXML)
        xmlFile = l.getAbsolutePath();
    super.initFile(id);
    MetadataStore store = makeFilterMetadata();
    in = new RandomAccessInputStream(id, 16);
    tiffParser = new TiffParser(in);
    tiffs = new ArrayList<String>();
    IFDList ifds = tiffParser.getIFDs();
    String date = ifds.get(0).getIFDStringValue(IFD.DATE_TIME);
    if (date != null) {
        datestamp = DateTools.getTime(date, "yyyy:MM:dd HH:mm:ss");
    }
    groupFiles();
    addGlobalMeta("Number of image files", tiffs.size());
    tiffReaders = new TiffReader[tiffs.size()];
    for (int i = 0; i < tiffReaders.length; i++) {
        tiffReaders[i] = new TiffReader();
    }
    tiffReaders[0].setId(tiffs.get(0));
    int[] ch = new int[ifds.size()];
    int[] idx = new int[ifds.size()];
    long[] stamp = new long[ifds.size()];
    int channelCount = 0;
    CoreMetadata ms0 = core.get(0);
    ms0.sizeZ = 1;
    ms0.sizeC = tiffReaders[0].getSizeC();
    ms0.dimensionOrder = isRGB() ? "XYC" : "XY";
    if (isGroupFiles()) {
        try {
            FilePattern fp = new FilePattern(new Location(currentId).getAbsoluteFile());
            AxisGuesser guesser = new AxisGuesser(fp, "XYTZC", 1, ifds.size(), 1, true);
            int[] axisTypes = guesser.getAxisTypes();
            int[] count = fp.getCount();
            for (int i = axisTypes.length - 1; i >= 0; i--) {
                if (axisTypes[i] == AxisGuesser.Z_AXIS) {
                    if (getDimensionOrder().indexOf('Z') == -1) {
                        ms0.dimensionOrder += 'Z';
                    }
                    ms0.sizeZ *= count[i];
                } else if (axisTypes[i] == AxisGuesser.C_AXIS) {
                    if (getDimensionOrder().indexOf('C') == -1) {
                        ms0.dimensionOrder += 'C';
                    }
                    ms0.sizeC *= count[i];
                }
            }
        } catch (NullPointerException e) {
        }
    }
    for (int i = 0; i < ifds.size(); i++) {
        String document = ifds.get(i).getIFDStringValue(IFD.DOCUMENT_NAME);
        if (document == null)
            continue;
        int index = document.indexOf("INDEX");
        String s = document.substring(8, index).trim();
        ch[i] = Integer.parseInt(s);
        if (ch[i] > channelCount)
            channelCount = ch[i];
        int space = document.indexOf(" ", index + 6);
        if (space < 0)
            continue;
        String n = document.substring(index + 6, space).trim();
        idx[i] = Integer.parseInt(n);
        date = document.substring(space, document.indexOf("FORMAT")).trim();
        stamp[i] = DateTools.getTime(date, DATE_FORMAT, ".");
        addGlobalMetaList("Timestamp for plane", stamp[i]);
    }
    ms0.sizeT = 0;
    // determine the axis sizes and ordering
    boolean unique = true;
    for (int i = 0; i < stamp.length; i++) {
        for (int j = i + 1; j < stamp.length; j++) {
            if (stamp[j] == stamp[i]) {
                unique = false;
                break;
            }
        }
        if (unique) {
            ms0.sizeT++;
            if (getDimensionOrder().indexOf('T') < 0) {
                ms0.dimensionOrder += 'T';
            }
        } else if (i > 0) {
            if ((ch[i] != ch[i - 1]) && getDimensionOrder().indexOf('C') < 0) {
                ms0.dimensionOrder += 'C';
            } else if (getDimensionOrder().indexOf('Z') < 0) {
                ms0.dimensionOrder += 'Z';
            }
        }
        unique = true;
    }
    if (getDimensionOrder().indexOf('Z') < 0)
        ms0.dimensionOrder += 'Z';
    if (getDimensionOrder().indexOf('C') < 0)
        ms0.dimensionOrder += 'C';
    if (getDimensionOrder().indexOf('T') < 0)
        ms0.dimensionOrder += 'T';
    if (getSizeC() == 0)
        ms0.sizeC = 1;
    if (getSizeT() == 0)
        ms0.sizeT = 1;
    if (channelCount == 0)
        channelCount = 1;
    if (getSizeZ() <= 1) {
        ms0.sizeZ = ifds.size() / (getSizeT() * channelCount);
    }
    ms0.sizeC *= channelCount;
    ms0.imageCount = getSizeZ() * getSizeT() * getSizeC();
    // cut up comment
    String comment = ifds.get(0).getComment();
    if (comment != null && comment.startsWith("[") && getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        String[] lines = comment.split("\n");
        for (String line : lines) {
            if (!line.startsWith("[")) {
                int eq = line.indexOf('=');
                if (eq < 0)
                    continue;
                String key = line.substring(0, eq).trim();
                String value = line.substring(eq + 1).trim();
                if (key.equals("VoxelSizeX")) {
                    try {
                        voxelX = Double.parseDouble(value);
                    } catch (NumberFormatException e) {
                    }
                } else if (key.equals("VoxelSizeY")) {
                    try {
                        voxelY = Double.parseDouble(value);
                    } catch (NumberFormatException e) {
                    }
                } else if (key.equals("VoxelSizeZ")) {
                    try {
                        voxelZ = Double.parseDouble(value);
                    } catch (NumberFormatException e) {
                    }
                }
                addGlobalMeta(key, value);
            }
        }
        metadata.remove("Comment");
    }
    ms0.sizeX = tiffReaders[0].getSizeX();
    ms0.sizeY = tiffReaders[0].getSizeY();
    ms0.rgb = tiffReaders[0].isRGB();
    ms0.pixelType = tiffReaders[0].getPixelType();
    ms0.littleEndian = tiffReaders[0].isLittleEndian();
    ms0.interleaved = tiffReaders[0].isInterleaved();
    ms0.falseColor = true;
    ms0.indexed = tiffReaders[0].isIndexed();
    if (isRGB())
        ms0.imageCount /= (getSizeC() / channelCount);
    if (getSizeZ() * getSizeT() * getEffectiveSizeC() != (ifds.size() * tiffReaders.length)) {
        int c = getEffectiveSizeC();
        if (c == 0)
            c = 1;
        ms0.sizeT = (ifds.size() * tiffReaders.length) / (c * getSizeZ());
        ms0.imageCount = getSizeT() * c * getSizeZ();
        if (getSizeT() == 0) {
            ms0.sizeT = 1;
            ms0.imageCount = ifds.size() * tiffReaders.length;
        }
    }
    if (getImageCount() == ifds.size() * getSizeZ() * getSizeT() && ifds.size() > 1) {
        if (getSizeZ() == 1) {
            ms0.sizeZ = ifds.size();
        } else if (getSizeT() == 1) {
            ms0.sizeT = ifds.size();
        } else
            ms0.sizeZ *= ifds.size();
    }
    if (xmlFile != null) {
        // parse XML metadata
        String xml = DataTools.readFile(xmlFile);
        xml = XMLTools.sanitizeXML(PREFIX + xml + SUFFIX);
        LeicaHandler handler = new LeicaHandler(store, getMetadataOptions().getMetadataLevel());
        XMLTools.parseXML(xml, handler);
        metadata = handler.getGlobalMetadata();
        MetadataTools.merge(handler.getGlobalMetadata(), metadata, "");
        core = handler.getCoreMetadataList();
        for (int i = 0; i < getSeriesCount(); i++) {
            CoreMetadata ms = core.get(i);
            if (tiffs.size() < ms.imageCount) {
                int div = ms.imageCount / ms.sizeC;
                ms.imageCount = tiffs.size();
                if (div >= ms.sizeZ)
                    ms.sizeZ /= div;
                else if (div >= ms.sizeT)
                    ms.sizeT /= div;
            }
            ms.dimensionOrder = getSizeZ() > getSizeT() ? "XYCZT" : "XYCTZ";
            ms.rgb = false;
            ms.interleaved = false;
            ms.indexed = tiffReaders[0].isIndexed();
        }
    }
    MetadataTools.populatePixels(store, this, true);
    Length sizeX = FormatTools.getPhysicalSizeX(voxelX);
    Length sizeY = FormatTools.getPhysicalSizeY(voxelY);
    Length sizeZ = FormatTools.getPhysicalSizeZ(voxelZ);
    if (sizeX != null) {
        store.setPixelsPhysicalSizeX(sizeX, 0);
    }
    if (sizeY != null) {
        store.setPixelsPhysicalSizeY(sizeY, 0);
    }
    if (sizeZ != null) {
        store.setPixelsPhysicalSizeZ(sizeZ, 0);
    }
}
Also used : CoreMetadata(loci.formats.CoreMetadata) MetadataStore(loci.formats.meta.MetadataStore) Length(ome.units.quantity.Length) IFDList(loci.formats.tiff.IFDList) TiffParser(loci.formats.tiff.TiffParser) RandomAccessInputStream(loci.common.RandomAccessInputStream) FilePattern(loci.formats.FilePattern) AxisGuesser(loci.formats.AxisGuesser) Location(loci.common.Location)

Aggregations

FilePattern (loci.formats.FilePattern)19 Location (loci.common.Location)10 ArrayList (java.util.ArrayList)6 Test (org.testng.annotations.Test)6 IOException (java.io.IOException)4 CoreMetadata (loci.formats.CoreMetadata)4 FormatException (loci.formats.FormatException)4 File (java.io.File)3 RandomAccessInputStream (loci.common.RandomAccessInputStream)3 AxisGuesser (loci.formats.AxisGuesser)3 MetadataStore (loci.formats.meta.MetadataStore)3 BigInteger (java.math.BigInteger)2 Path (java.nio.file.Path)2 ServiceFactory (loci.common.services.ServiceFactory)2 ImageReader (loci.formats.ImageReader)2 OMEXMLService (loci.formats.services.OMEXMLService)2 TiffParser (loci.formats.tiff.TiffParser)2 GenericDialog (ij.gui.GenericDialog)1 YesNoCancelDialog (ij.gui.YesNoCancelDialog)1 DirectoryChooser (ij.io.DirectoryChooser)1