private String[][] getTiffs(String dir) { Location f = new Location(dir); Vector<Vector<String>> files = new Vector<Vector<String>>(); String[] wells = f.list(true); Arrays.sort(wells); for (String filename : wells) { Location file = new Location(f, filename).getAbsoluteFile(); if (file.isDirectory() && filename.startsWith("Well ")) { String[] list = file.list(true); Vector<String> tiffList = new Vector<String>(); Arrays.sort(list); for (String tiff : list) { if (tiff.matches(".* - n\\d\\d\\d\\d\\d\\d\\.tif")) { tiffList.add(new Location(file, tiff).getAbsolutePath()); } } files.add(tiffList); } } String[][] tiffFiles = new String[files.size()][]; for (int i = 0; i < tiffFiles.length; i++) { tiffFiles[i] = files.get(i).toArray(new String[0]); } return tiffFiles; }
/* @see loci.formats.FormatReader#initFile(String) */ protected void initFile(String id) throws FormatException, IOException { if (!checkSuffix(id, "tif")) { Location parent = new Location(id).getAbsoluteFile().getParentFile(); String[] list = parent.list(true); for (String f : list) { if (checkSuffix(f, "tif")) { String path = new Location(parent, f).getAbsolutePath(); if (isThisType(path)) { id = path; break; } } } } super.initFile(id); }
/* @see loci.formats.FormatReader#initFile(String) */ 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(); String metadataFile = METADATA; if (file.exists()) { metadataFile = new Location(parentFile, METADATA).getAbsolutePath(); // look for other positions 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(); pos.metadataFile = metadataFile; positions.add(pos); } } core = new CoreMetadata[positions.size()]; for (int i = 0; i < positions.size(); i++) { core[i] = new CoreMetadata(); setSeries(i); parsePosition(i); } setSeries(0); populateMetadata(); }
private void buildTIFFList(int posIndex) throws FormatException { Position p = positions.get(posIndex); String parent = new Location(p.metadataFile).getParent(); LOGGER.info("Finding image file names"); // find the name of a TIFF file p.tiffs = new Vector<String>(); // build list of TIFF files 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]); p.tiffs.add(new Location(dir, f).getAbsolutePath()); } } core[posIndex].sizeZ = uniqueZ.size(); core[posIndex].sizeC = uniqueC.size(); core[posIndex].sizeT = uniqueT.size(); if (p.tiffs.size() == 0) { throw new FormatException("Could not find TIFF files."); } } }
/* @see loci.formats.FormatReader#initFile(String) */ protected void initFile(String id) throws FormatException, IOException { super.initFile(id); if (!checkSuffix(id, "vsi")) { Location current = new Location(id).getAbsoluteFile(); Location parent = current.getParentFile(); parent = parent.getParentFile(); Location grandparent = parent.getParentFile(); String vsi = parent.getName(); vsi = vsi.substring(1, vsi.length() - 1) + ".vsi"; Location vsiFile = new Location(grandparent, vsi); if (!vsiFile.exists()) { throw new FormatException("Could not find .vsi file."); } else { id = vsiFile.getAbsolutePath(); } } parser = new TiffParser(id); ifds = parser.getIFDs(); RandomAccessInputStream vsi = new RandomAccessInputStream(id); vsi.order(parser.getStream().isLittleEndian()); vsi.seek(8); readTags(vsi); vsi.seek(parser.getStream().getFilePointer()); vsi.skipBytes(273); ArrayList<String> files = new ArrayList<String>(); Location file = new Location(id).getAbsoluteFile(); Location dir = file.getParentFile(); String name = file.getName(); name = name.substring(0, name.lastIndexOf(".")); Location pixelsDir = new Location(dir, "_" + name + "_"); String[] stackDirs = pixelsDir.list(true); if (stackDirs != null) { for (String f : stackDirs) { Location stackDir = new Location(pixelsDir, f); String[] pixelsFiles = stackDir.list(true); if (pixelsFiles != null) { for (String pixelsFile : pixelsFiles) { if (checkSuffix(pixelsFile, "ets")) { files.add(new Location(stackDir, pixelsFile).getAbsolutePath()); } } } } } files.add(file.getAbsolutePath()); usedFiles = files.toArray(new String[files.size()]); core = new CoreMetadata[files.size() - 1 + ifds.size()]; tileOffsets = new Long[files.size() - 1][]; rows = new int[files.size() - 1]; cols = new int[files.size() - 1]; nDimensions = new int[core.length]; IFDList exifs = parser.getExifIFDs(); compressionType = new int[core.length]; tileX = new int[core.length]; tileY = new int[core.length]; tileMap = new HashMap[core.length]; for (int s = 0; s < core.length; s++) { core[s] = new CoreMetadata(); } for (int s = 0; s < core.length; s++) { tileMap[s] = new HashMap<TileCoordinate, Integer>(); if (s == 0 && !hasFlattenedResolutions()) { core[s].resolutionCount = ifds.size() + (files.size() == 1 ? 0 : 1); } if (s < files.size() - 1) { setSeries(s); parseETSFile(files.get(s), s); core[s].littleEndian = compressionType[s] == RAW; core[s].interleaved = core[s].rgb; if (s == 0 && exifs.size() > 0) { IFD exif = exifs.get(0); int newX = exif.getIFDIntValue(IFD.PIXEL_X_DIMENSION); int newY = exif.getIFDIntValue(IFD.PIXEL_Y_DIMENSION); if (getSizeX() > newX || getSizeY() > newY) { core[s].sizeX = newX; core[s].sizeY = newY; } } setSeries(0); } else { IFD ifd = ifds.get(s - files.size() + 1); PhotoInterp p = ifd.getPhotometricInterpretation(); int samples = ifd.getSamplesPerPixel(); core[s].rgb = samples > 1 || p == PhotoInterp.RGB; core[s].sizeX = (int) ifd.getImageWidth(); core[s].sizeY = (int) ifd.getImageLength(); core[s].sizeZ = 1; core[s].sizeT = 1; core[s].sizeC = core[s].rgb ? samples : 1; core[s].littleEndian = ifd.isLittleEndian(); core[s].indexed = p == PhotoInterp.RGB_PALETTE && (get8BitLookupTable() != null || get16BitLookupTable() != null); core[s].imageCount = 1; core[s].pixelType = ifd.getPixelType(); core[s].interleaved = false; core[s].falseColor = false; core[s].thumbnail = s != 0; } core[s].metadataComplete = true; core[s].dimensionOrder = "XYCZT"; } vsi.close(); MetadataStore store = makeFilterMetadata(); MetadataTools.populatePixels(store, this); }
private IniList readMetaData(String id) throws IOException { IniParser parser = new IniParser(); IniList exp = parser.parseINI( new BufferedReader(new InputStreamReader(new FileInputStream(id), Constants.ENCODING))); IniList plate = null; // Read Plate File for (String filename : metadataFiles) { if (checkSuffix(filename, "plt")) { plate = parser.parseINI( new BufferedReader( new InputStreamReader(new FileInputStream(filename), Constants.ENCODING))); } else if (filename.endsWith("RoiSummary.txt")) { roiFile = filename; if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) { RandomAccessInputStream s = new RandomAccessInputStream(filename); String line = s.readLine().trim(); while (!line.endsWith(".adf\"")) { line = s.readLine().trim(); } plateName = line.substring(line.indexOf(":")).trim(); plateName = plateName.replace('/', File.separatorChar); plateName = plateName.replace('\\', File.separatorChar); for (int i = 0; i < 3; i++) { plateName = plateName.substring(0, plateName.lastIndexOf(File.separator)); } plateName = plateName.substring(plateName.lastIndexOf(File.separator) + 1); s.close(); } } } if (plate == null) throw new IOException("No Plate File"); IniTable plateType = plate.getTable("PlateType"); if (plateName == null) { plateName = plateType.get("Brand"); } plateDescription = plateType.get("Brand") + " " + plateType.get("Description"); int nWells = Integer.parseInt(plateType.get("Wells")); if (nWells == 96) { wellRows = 8; wellCols = 12; } else if (nWells == 384) { wellRows = 16; wellCols = 24; } Location dir = new Location(id).getAbsoluteFile().getParentFile(); String[] wellList = dir.list(); Arrays.sort(wellList); for (String filename : wellList) { if (filename.startsWith("Well ")) { wellLabels.add(filename.split("\\s|\\.")[1]); } } IniTable imageTable = exp.getTable("Image"); boolean montage = imageTable.get("Montaged").equals("1"); if (montage) { fieldRows = Integer.parseInt(imageTable.get("TilesY")); fieldCols = Integer.parseInt(imageTable.get("TilesX")); } else { fieldRows = 1; fieldCols = 1; } core = new CoreMetadata[wellLabels.size() * fieldRows * fieldCols]; core[0] = new CoreMetadata(); // Hack for current testing/development purposes // Not all channels have the same Z!!! How to handle??? // FIXME FIXME FIXME core[0].sizeZ = 1; // FIXME FIXME FIXME // END OF HACK core[0].sizeC = Integer.parseInt(exp.getTable("General").get("Dyes")); core[0].bitsPerPixel = Integer.parseInt(exp.getTable("Camera").get("BitdepthUsed")); IniTable dyeTable = exp.getTable("Dyes"); for (int i = 1; i <= getSizeC(); i++) { channelNames.add(dyeTable.get(Integer.toString(i))); } // Count Images core[0].sizeT = 0; Location well = new Location(dir.getAbsolutePath(), "Well " + wellLabels.get(1)); for (String channelName : channelNames) { int timepoints = 0; for (String filename : well.list()) { if (filename.startsWith(channelName) && filename.endsWith(".tif")) { timepoints++; } } if (timepoints > getSizeT()) { core[0].sizeT = timepoints; } } return exp; }
/* @see loci.formats.FormatReader#initFile(String) */ protected void initFile(String id) throws FormatException, IOException { // make sure we have the experiment file id = locateExperimentFile(id); super.initFile(id); Location dir = new Location(id).getAbsoluteFile().getParentFile(); for (String file : dir.list(true)) { Location f = new Location(dir, file); if (!f.isDirectory()) { if (checkSuffix(file, META_EXT)) { metadataFiles.add(f.getAbsolutePath()); } } } // parse Experiment metadata IniList experiment = readMetaData(id); if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) { objective = experiment.getTable("Geometry").get("Name"); IniTable camera = experiment.getTable("Camera"); binning = camera.get("BinX") + "x" + camera.get("BinY"); parseChannelData(dir); addGlobalMeta("Objective", objective); addGlobalMeta("Camera binning", binning); } Vector<String> uniqueRows = new Vector<String>(); Vector<String> uniqueColumns = new Vector<String>(); for (String well : wellLabels) { String row = well.substring(0, 1).trim(); String column = well.substring(1).trim(); if (!uniqueRows.contains(row) && row.length() > 0) uniqueRows.add(row); if (!uniqueColumns.contains(column) && column.length() > 0) { uniqueColumns.add(column); } } int nSlices = getSizeZ() == 0 ? 1 : getSizeZ(); int nTimepoints = getSizeT(); int nWells = wellLabels.size(); int nChannels = getSizeC() == 0 ? channelNames.size() : getSizeC(); if (nChannels == 0) nChannels = 1; tiffs = getTiffs(dir.getAbsolutePath()); reader = new MinimalTiffReader(); reader.setId(tiffs[0][0]); int sizeX = reader.getSizeX(); int sizeY = reader.getSizeY(); int pixelType = reader.getPixelType(); boolean rgb = reader.isRGB(); boolean interleaved = reader.isInterleaved(); boolean indexed = reader.isIndexed(); boolean littleEndian = reader.isLittleEndian(); if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) { IniParser parser = new IniParser(); for (String metadataFile : metadataFiles) { String filename = new Location(metadataFile).getName(); if (!checkSuffix(metadataFile, "txt")) { String data = DataTools.readFile(metadataFile); IniList ini = parser.parseINI(new BufferedReader(new StringReader(data))); HashMap<String, String> h = ini.flattenIntoHashMap(); for (String key : h.keySet()) { addGlobalMeta(filename + " " + key, h.get(key)); } } } } for (int i = 0; i < getSeriesCount(); i++) { core[i] = new CoreMetadata(); core[i].sizeC = nChannels; core[i].sizeZ = nSlices; core[i].sizeT = nTimepoints; core[i].sizeX = sizeX / fieldCols; core[i].sizeY = sizeY / fieldRows; core[i].pixelType = pixelType; core[i].rgb = rgb; core[i].interleaved = interleaved; core[i].indexed = indexed; core[i].littleEndian = littleEndian; core[i].dimensionOrder = "XYZTC"; core[i].imageCount = nSlices * nTimepoints * nChannels; } MetadataStore store = makeFilterMetadata(); boolean populatePlanes = getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM; MetadataTools.populatePixels(store, this, populatePlanes); String plateAcqID = MetadataTools.createLSID("PlateAcquisition", 0, 0); store.setPlateAcquisitionID(plateAcqID, 0, 0); if (fieldRows * fieldCols > 0) { store.setPlateAcquisitionMaximumFieldCount(new PositiveInteger(fieldRows * fieldCols), 0, 0); } else { LOGGER.warn("Expected positive value for MaximumFieldCount; got {}", fieldRows * fieldCols); } for (int row = 0; row < wellRows; row++) { for (int col = 0; col < wellCols; col++) { int index = row * wellCols + col; store.setWellID(MetadataTools.createLSID("Well", 0, index), 0, index); store.setWellRow(new NonNegativeInteger(row), 0, index); store.setWellColumn(new NonNegativeInteger(col), 0, index); } } for (int i = 0; i < getSeriesCount(); i++) { int well = i / (fieldRows * fieldCols); int field = i % (fieldRows * fieldCols); MetadataTools.setDefaultCreationDate(store, tiffs[well][0], i); String name = wellLabels.get(well); String row = name.substring(0, 1); Integer col = Integer.parseInt(name.substring(1)); int index = (row.charAt(0) - 'A') * wellCols + col - 1; String wellSampleID = MetadataTools.createLSID("WellSample", 0, index, field); store.setWellSampleID(wellSampleID, 0, index, field); store.setWellSampleIndex(new NonNegativeInteger(i), 0, index, field); String imageID = MetadataTools.createLSID("Image", i); store.setWellSampleImageRef(imageID, 0, index, field); store.setImageID(imageID, i); store.setImageName(name + " Field #" + (field + 1), i); store.setPlateAcquisitionWellSampleRef(wellSampleID, 0, 0, i); } MetadataLevel level = getMetadataOptions().getMetadataLevel(); if (level != MetadataLevel.MINIMUM) { String instrumentID = MetadataTools.createLSID("Instrument", 0); store.setInstrumentID(instrumentID, 0); String objectiveID = MetadataTools.createLSID("Objective", 0, 0); store.setObjectiveID(objectiveID, 0, 0); if (objective != null) { String[] tokens = objective.split(" "); String mag = tokens[0].replaceAll("[xX]", ""); String na = null; int naIndex = 0; for (int i = 0; i < tokens.length; i++) { if (tokens[i].equals("NA")) { naIndex = i + 1; na = tokens[naIndex]; break; } } Integer magnification = new Integer(mag); if (magnification > 0) { store.setObjectiveNominalMagnification(new PositiveInteger(magnification), 0, 0); } else { LOGGER.warn("Expected positive value for NominalMagnification; got {}", magnification); } if (na != null) { na = na.substring(0, 1) + "." + na.substring(1); store.setObjectiveLensNA(new Double(na), 0, 0); } if (naIndex + 1 < tokens.length) { store.setObjectiveManufacturer(tokens[naIndex + 1], 0, 0); } } // populate LogicalChannel data for (int i = 0; i < getSeriesCount(); i++) { store.setImageInstrumentRef(instrumentID, i); store.setObjectiveSettingsID(objectiveID, i); for (int c = 0; c < getSizeC(); c++) { store.setChannelName(channelNames.get(c), i, c); if (emWave[c] > 0) { store.setChannelEmissionWavelength(new PositiveInteger(emWave[c]), i, c); } else { LOGGER.warn("Expected positive value for EmissionWavelength; got {}", emWave[c]); } if (exWave[c] > 0) { store.setChannelExcitationWavelength(new PositiveInteger(exWave[c]), i, c); } else { LOGGER.warn("Expected positive value for ExcitationWavelength; got {}", exWave[c]); } String detectorID = MetadataTools.createLSID("Detector", 0, c); store.setDetectorID(detectorID, 0, c); store.setDetectorSettingsID(detectorID, i, c); store.setDetectorSettingsGain(gain[c], i, c); store.setDetectorSettingsOffset(offset[c], i, c); store.setDetectorSettingsBinning(getBinning(binning), i, c); } long firstPlane = 0; for (int p = 0; p < getImageCount(); p++) { int[] zct = getZCTCoords(p); store.setPlaneExposureTime(exposure[zct[1]], i, p); String file = getFilename(i, p); if (file != null) { long plane = getTimestamp(file); if (p == 0) { firstPlane = plane; } double timestamp = (plane - firstPlane) / 1000.0; store.setPlaneDeltaT(timestamp, i, p); } } } store.setPlateID(MetadataTools.createLSID("Plate", 0), 0); store.setPlateRowNamingConvention(getNamingConvention("Letter"), 0); store.setPlateColumnNamingConvention(getNamingConvention("Number"), 0); store.setPlateName(plateName, 0); store.setPlateDescription(plateDescription, 0); if (level != MetadataLevel.NO_OVERLAYS) { parseROIs(store); } } }
/* @see loci.formats.in.BaseTiffReader#initStandardMetadata() */ protected void initStandardMetadata() throws FormatException, IOException { super.initStandardMetadata(); ifds = tiffParser.getIFDs(); for (IFD ifd : ifds) { tiffParser.fillInIFD(ifd); } String comment = ifds.get(0).getComment(); String[] values = comment.split(";"); for (String v : values) { int eq = v.indexOf("="); if (eq < 0) continue; String key = v.substring(0, eq).trim(); String value = v.substring(eq + 1).trim(); addGlobalMeta(key, value); if (key.equals("OverlapsXY")) { String[] overlapValues = value.split(" "); overlaps = new int[ifds.size() * 2]; for (int i = 0; i < overlapValues.length; i++) { overlaps[i] = Integer.parseInt(overlapValues[i]); } } } core = new CoreMetadata[ifds.size()]; for (int i = 0; i < core.length; i++) { setSeries(i); core[i] = new CoreMetadata(); if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {} } setSeries(0); // repopulate core metadata for (int s = 0; s < core.length; s++) { IFD ifd = ifds.get(s); PhotoInterp p = ifd.getPhotometricInterpretation(); int samples = ifd.getSamplesPerPixel(); core[s].rgb = samples > 1 || p == PhotoInterp.RGB; long numTileRows = ifd.getTilesPerColumn() - 1; long numTileCols = ifd.getTilesPerRow() - 1; int overlapX = overlaps[s * 2]; int overlapY = overlaps[s * 2 + 1]; core[s].sizeX = (int) (ifd.getImageWidth() - (numTileCols * overlapX)); core[s].sizeY = (int) (ifd.getImageLength() - (numTileRows * overlapY)); core[s].sizeZ = 1; core[s].sizeT = 1; core[s].sizeC = core[s].rgb ? samples : 1; core[s].littleEndian = ifd.isLittleEndian(); core[s].indexed = p == PhotoInterp.RGB_PALETTE && (get8BitLookupTable() != null || get16BitLookupTable() != null); core[s].imageCount = 1; core[s].pixelType = ifd.getPixelType(); core[s].metadataComplete = true; core[s].interleaved = false; core[s].falseColor = false; core[s].dimensionOrder = "XYCZT"; core[s].thumbnail = s > 0; } // look for all of the other associated metadata files files = new ArrayList<String>(); Location baseFile = new Location(currentId).getAbsoluteFile(); Location parent = baseFile.getParentFile(); String name = baseFile.getName(); if (name.indexOf(".") >= 0) { name = name.substring(0, name.indexOf(".") + 1); } roiFile = new Location(parent, name + "ROI").getAbsolutePath(); roiDrawFile = new Location(parent, name + "ROI-draw").getAbsolutePath(); String[] list = parent.list(true); for (String f : list) { if (!f.equals(baseFile.getName())) { files.add(new Location(parent, f).getAbsolutePath()); } } }