use of loci.common.Location in project bioformats by openmicroscopy.
the class AnalyzeReader method isThisType.
// -- IFormatReader API methods --
/* @see loci.formats.IFormatReader#isThisType(String, boolean) */
@Override
public boolean isThisType(String name, boolean open) {
if (!super.isThisType(name, open))
return false;
if (!open)
return false;
String headerFile = checkSuffix(name, "hdr") ? name : null;
String extension = name.substring(name.lastIndexOf(".") + 1);
name = name.substring(0, name.lastIndexOf("."));
if (extension.equals("img"))
extension = "hdr";
else if (extension.equals("IMG"))
extension = "HDR";
else if (extension.equals("hdr"))
extension = "img";
else if (extension.equals("HDR"))
extension = "IMG";
if (extension.equalsIgnoreCase("hdr")) {
headerFile = name + "." + extension;
}
boolean validHeader = false;
try {
RandomAccessInputStream headerStream = new RandomAccessInputStream(headerFile);
validHeader = isThisType(headerStream);
headerStream.close();
} catch (IOException e) {
}
return new Location(name + "." + extension).exists() && validHeader;
}
use of loci.common.Location in project bioformats by openmicroscopy.
the class BDReader method isThisType.
// -- IFormatReader API methods --
/* @see loci.formats.IFormatReader#isThisType(String, boolean) */
@Override
public boolean isThisType(String name, boolean open) {
Location location = new Location(name);
String id = location.getAbsolutePath();
boolean dirCheck = location.isDirectory();
if (dirCheck)
return false;
if (name.endsWith(EXPERIMENT_FILE))
return true;
if (!open)
return false;
try {
id = locateExperimentFile(id);
} catch (FormatException f) {
return false;
} catch (IOException f) {
return false;
} catch (NullPointerException e) {
return false;
}
if (id.endsWith(EXPERIMENT_FILE)) {
return true;
}
return super.isThisType(name, open);
}
use of loci.common.Location in project bioformats by openmicroscopy.
the class BDReader method readMetaData.
private IniList readMetaData(String id) throws IOException {
IniParser parser = new IniParser();
RandomAccessInputStream idStream = new RandomAccessInputStream(id);
IniList exp = parser.parseINI(new BufferedReader(new InputStreamReader(idStream, Constants.ENCODING)));
IniList plate = null;
IniList xyz = null;
// Read Plate File
for (String filename : metadataFiles) {
if (checkSuffix(filename, "plt")) {
RandomAccessInputStream stream = new RandomAccessInputStream(filename);
plate = parser.parseINI(new BufferedReader(new InputStreamReader(stream, Constants.ENCODING)));
stream.close();
} else if (checkSuffix(filename, "xyz")) {
RandomAccessInputStream stream = new RandomAccessInputStream(filename);
xyz = parser.parseINI(new BufferedReader(new InputStreamReader(stream, Constants.ENCODING)));
stream.close();
} 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;
}
for (String filename : rootList) {
String name = new Location(filename).getName();
if (name.startsWith("Well ")) {
wellLabels.add(name.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.clear();
int coresize = wellLabels.size() * fieldRows * fieldCols;
CoreMetadata ms0 = new CoreMetadata();
core.add(ms0);
for (int i = 1; i < coresize; i++) {
core.add(new CoreMetadata());
}
ms0.sizeC = Integer.parseInt(exp.getTable("General").get("Dyes"));
ms0.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)));
}
if (xyz != null) {
IniTable zTable = xyz.getTable("Z1Axis");
boolean zEnabled = "1".equals(zTable.get("Z1AxisEnabled")) && "1".equals(zTable.get("Z1AxisMode"));
if (zEnabled) {
ms0.sizeZ = (int) Double.parseDouble(zTable.get("Z1AxisValue")) + 1;
} else {
ms0.sizeZ = 1;
}
} else {
ms0.sizeZ = 1;
}
// Count Images
ms0.sizeT = 0;
for (String channelName : channelNames) {
int images = 0;
for (String filename : wellList.get(1)) {
if (filename.startsWith(channelName) && filename.endsWith(".tif")) {
images++;
}
}
if (images > getImageCount()) {
ms0.sizeT = images / getSizeZ();
ms0.imageCount = getSizeZ() * getSizeT() * channelNames.size();
}
}
idStream.close();
return exp;
}
use of loci.common.Location in project bioformats by openmicroscopy.
the class BDReader method initFile.
// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
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();
rootList = dir.list(true);
Arrays.sort(rootList);
for (int i = 0; i < rootList.length; i++) {
String file = rootList[i];
Location f = new Location(dir, file);
rootList[i] = f.getAbsolutePath();
if (!f.isDirectory()) {
if (checkSuffix(file, META_EXT) && !f.isDirectory()) {
metadataFiles.add(f.getAbsolutePath());
}
} else {
String[] wells = f.list(true);
Arrays.sort(wells);
wellList.add(wells);
for (String well : wells) {
Location wellFile = new Location(f, well);
if (!wellFile.isDirectory()) {
if (checkSuffix(well, META_EXT)) {
metadataFiles.add(wellFile.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);
}
final List<String> uniqueRows = new ArrayList<String>();
final List<String> uniqueColumns = new ArrayList<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();
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, new String[] { "txt", "bmp", "adf", "roi" })) {
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));
}
}
}
}
int coresize = core.size();
core.clear();
for (int i = 0; i < coresize; i++) {
CoreMetadata ms = new CoreMetadata();
core.add(ms);
ms.sizeC = nChannels;
ms.sizeZ = nSlices;
ms.sizeT = nTimepoints;
ms.sizeX = sizeX / fieldCols;
ms.sizeY = sizeY / fieldRows;
ms.pixelType = pixelType;
ms.rgb = rgb;
ms.interleaved = interleaved;
ms.indexed = indexed;
ms.littleEndian = littleEndian;
ms.dimensionOrder = "XYZTC";
ms.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);
PositiveInteger fieldCount = FormatTools.getMaxFieldCount(fieldRows * fieldCols);
if (fieldCount != null) {
store.setPlateAcquisitionMaximumFieldCount(fieldCount, 0, 0);
}
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;
}
}
Double magnification = new Double(mag);
store.setObjectiveNominalMagnification(magnification, 0, 0);
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);
Length emission = FormatTools.getEmissionWavelength(emWave[c]);
Length excitation = FormatTools.getExcitationWavelength(exWave[c]);
if (emission != null) {
store.setChannelEmissionWavelength(emission, i, c);
}
if (excitation != null) {
store.setChannelExcitationWavelength(excitation, i, 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(new Time(exposure[zct[1]], UNITS.SECOND), 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(new Time(timestamp, UNITS.SECOND), 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);
}
}
}
use of loci.common.Location in project bioformats by openmicroscopy.
the class BDReader method locateExperimentFile.
// -- Helper methods --
/* Locate the experiment file given any file in set */
private String locateExperimentFile(String id) throws FormatException, IOException {
if (!checkSuffix(id, "exp")) {
Location parent = new Location(id).getAbsoluteFile().getParentFile();
if (checkSuffix(id, "tif"))
parent = parent.getParentFile();
Location expFile = new Location(parent, EXPERIMENT_FILE);
if (expFile.exists()) {
return expFile.getAbsolutePath();
}
throw new FormatException("Could not find " + EXPERIMENT_FILE + " in " + parent.getAbsolutePath());
}
return id;
}
Aggregations