use of loci.common.Location in project bioformats by openmicroscopy.
the class PrairieReader method initFile.
// -- Internal FormatReader API methods --
@Override
protected void initFile(String id) throws FormatException, IOException {
super.initFile(id);
tiff = new TiffReader();
if (checkSuffix(id, XML_SUFFIX)) {
xmlFile = new Location(id);
findMetadataFiles();
} else if (checkSuffix(id, CFG_SUFFIX)) {
cfgFile = new Location(id);
findMetadataFiles();
} else if (checkSuffix(id, ENV_SUFFIX)) {
envFile = new Location(id);
findMetadataFiles();
} else {
// we have been given a TIFF file
if (isGroupFiles()) {
findMetadataFiles();
} else {
// NB: File grouping is not allowed, so we enter a special mode,
// which delegates to the TIFF reader for everything.
singleTiffMode = true;
tiff.setId(id);
return;
}
}
currentId = xmlFile.getAbsolutePath();
parsePrairieMetadata();
populateCoreMetadata();
populateOriginalMetadata();
populateOMEMetadata();
}
use of loci.common.Location in project bioformats by openmicroscopy.
the class ImagicReader method isThisType.
// -- IFormatReader API methods --
/* @see loci.formats.IFormatReader#isThisType(String, boolean) */
@Override
public boolean isThisType(String name, boolean open) {
if (checkSuffix(name, "hed")) {
return true;
}
if (!checkSuffix(name, "img") || !open) {
return false;
}
int lastDot = name.lastIndexOf(".");
if (lastDot < 0) {
return false;
}
String headerName = name.substring(0, lastDot);
return new Location(headerName + ".hed").exists();
}
use of loci.common.Location in project bioformats by openmicroscopy.
the class ImprovisionTiffReader method initStandardMetadata.
// -- Internal BaseTiffReader API methods --
/* @see BaseTiffReader#initStandardMetadata() */
@Override
protected void initStandardMetadata() throws FormatException, IOException {
super.initStandardMetadata();
put("Improvision", "yes");
// parse key/value pairs in the comments
String[] comments = new String[ifds.size()];
String tz = null, tc = null, tt = null;
for (int plane = 0; plane < ifds.size(); plane++) {
String comment = ifds.get(plane).getComment();
comments[plane] = comment;
if (comment != null) {
String[] lines = comment.split("\n");
for (String line : lines) {
int equals = line.indexOf('=');
if (equals < 0)
continue;
String key = line.substring(0, equals);
String value = line.substring(equals + 1);
addGlobalMeta(key, value);
if (key.equals("TotalZPlanes"))
tz = value;
else if (key.equals("TotalChannels"))
tc = value;
else if (key.equals("TotalTimepoints"))
tt = value;
else if (key.equals("XCalibrationMicrons")) {
pixelSizeX = DataTools.parseDouble(value);
} else if (key.equals("YCalibrationMicrons")) {
pixelSizeY = DataTools.parseDouble(value);
} else if (key.equals("ZCalibrationMicrons")) {
pixelSizeZ = DataTools.parseDouble(value);
} else if (key.equals("WhiteColour")) {
String[] rgb = value.split(",");
if (rgb.length < 3) {
channelColors.add(null);
continue;
}
int red = 255;
try {
red = Integer.parseInt(rgb[0]);
} catch (NumberFormatException e) {
}
int green = 255;
try {
green = Integer.parseInt(rgb[1]);
} catch (NumberFormatException e) {
}
int blue = 255;
try {
blue = Integer.parseInt(rgb[2]);
} catch (NumberFormatException e) {
}
channelColors.add(new Color(red, green, blue, 255));
}
}
metadata.remove("Comment");
}
}
CoreMetadata m = core.get(0);
m.sizeT = 1;
if (getSizeZ() == 0)
m.sizeZ = 1;
if (getSizeC() == 0)
m.sizeC = 1;
if (tz != null)
m.sizeZ *= Integer.parseInt(tz);
if (tc != null)
m.sizeC *= Integer.parseInt(tc);
if (tt != null)
m.sizeT *= Integer.parseInt(tt);
if (getSizeZ() * getSizeC() * getSizeT() < getImageCount()) {
m.sizeC *= getImageCount();
} else
m.imageCount = getSizeZ() * getSizeT() * Integer.parseInt(tc);
// parse each of the comments to determine axis ordering
long[] stamps = new long[ifds.size()];
int[][] coords = new int[ifds.size()][3];
cNames = new String[getSizeC()];
boolean multipleFiles = false;
for (int i = 0; i < ifds.size(); i++) {
Arrays.fill(coords[i], -1);
String comment = comments[i];
// TODO : can use loci.common.IniParser to parse the comments
comment = comment.replaceAll("\r\n", "\n");
comment = comment.replaceAll("\r", "\n");
String channelName = null;
String[] lines = comment.split("\n");
for (String line : lines) {
int equals = line.indexOf('=');
if (equals < 0)
continue;
String key = line.substring(0, equals);
String value = line.substring(equals + 1);
if (key.equals("TimeStampMicroSeconds")) {
stamps[i] = Long.parseLong(value);
} else if (key.equals("ZPlane"))
coords[i][0] = Integer.parseInt(value);
else if (key.equals("ChannelNo")) {
coords[i][1] = Integer.parseInt(value);
int ndx = Integer.parseInt(value) - 1;
if (cNames[ndx] == null)
cNames[ndx] = channelName;
} else if (key.equals("TimepointName")) {
coords[i][2] = Integer.parseInt(value);
} else if (key.equals("ChannelName")) {
channelName = value;
} else if (key.equals("MultiFileTIFF")) {
multipleFiles = value.equalsIgnoreCase("yes");
}
if (getMetadataOptions().getMetadataLevel() == MetadataLevel.MINIMUM && coords[i][0] >= 0 && coords[i][1] >= 0 && coords[i][2] >= 0) {
break;
}
}
}
if (multipleFiles) {
// look for other TIFF files that belong to this dataset
String currentUUID = getUUID(currentId);
Location parent = new Location(currentId).getAbsoluteFile().getParentFile();
String[] list = parent.list(true);
Arrays.sort(list);
ArrayList<String> matchingFiles = new ArrayList<String>();
for (String f : list) {
String path = new Location(parent, f).getAbsolutePath();
if (isThisType(path) && getUUID(path).equals(currentUUID)) {
matchingFiles.add(path);
}
}
files = matchingFiles.toArray(new String[matchingFiles.size()]);
} else {
files = new String[] { currentId };
}
if (files.length * ifds.size() < getImageCount()) {
files = new String[] { currentId };
m.imageCount = ifds.size();
m.sizeZ = ifds.size();
m.sizeT = 1;
if (!isRGB()) {
m.sizeC = 1;
}
}
readers = new MinimalTiffReader[files.length];
for (int i = 0; i < readers.length; i++) {
readers[i] = new MinimalTiffReader();
readers[i].setId(files[i]);
}
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
// determine average time per plane
long sum = 0;
for (int i = 1; i < stamps.length; i++) {
long diff = stamps[i] - stamps[i - 1];
if (diff > 0)
sum += diff;
}
pixelSizeT = (int) (sum / getSizeT());
}
// determine dimension order
m.dimensionOrder = "XY";
if (isRGB())
m.dimensionOrder += 'C';
for (int i = 1; i < coords.length; i++) {
int zDiff = coords[i][0] - coords[i - 1][0];
int cDiff = coords[i][1] - coords[i - 1][1];
int tDiff = coords[i][2] - coords[i - 1][2];
if (zDiff > 0 && getDimensionOrder().indexOf('Z') < 0) {
m.dimensionOrder += 'Z';
}
if (cDiff > 0 && getDimensionOrder().indexOf('C') < 0) {
m.dimensionOrder += 'C';
}
if (tDiff > 0 && getDimensionOrder().indexOf('T') < 0) {
m.dimensionOrder += 'T';
}
if (m.dimensionOrder.length() == 5)
break;
}
if (getDimensionOrder().indexOf('Z') < 0)
m.dimensionOrder += 'Z';
if (getDimensionOrder().indexOf('C') < 0)
m.dimensionOrder += 'C';
if (getDimensionOrder().indexOf('T') < 0)
m.dimensionOrder += 'T';
}
use of loci.common.Location in project bioformats by openmicroscopy.
the class InveonReader method initFile.
// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
if (!checkSuffix(id, "hdr")) {
id += ".hdr";
}
super.initFile(id);
String headerData = DataTools.readFile(id);
String[] lines = headerData.split("\n");
String date = null;
String institution = null, investigator = null;
String model = null;
String description = null;
Double pixelSizeX = null;
Double pixelSizeY = null;
Double pixelSizeZ = null;
int frames = 0;
for (String line : lines) {
line = line.trim();
if (!line.startsWith("#")) {
int space = line.indexOf(' ');
if (space < 0) {
continue;
}
String key = line.substring(0, space);
String value = line.substring(space + 1);
if (key.equals("institution")) {
institution = value;
} else if (key.equals("investigator")) {
investigator = value;
} else if (key.equals("study")) {
description = value;
} else if (key.equals("model")) {
value = transformModel(value);
model = value;
} else if (key.equals("modality")) {
value = transformModality(value);
} else if (key.equals("modality_configuration")) {
value = transformModalityConfiguration(value);
} else if (key.equals("file_type")) {
value = transformFileType(value);
} else if (key.equals("acquisition_mode")) {
value = transformAcquisitionMode(value);
} else if (key.equals("bed_control")) {
value = transformBedControl(value);
} else if (key.equals("bed_motion")) {
value = transformBedMotion(value);
} else if (key.equals("registration_available")) {
value = transformRegistrationAvailable(value);
} else if (key.equals("normalization_applied")) {
value = transformNormalizationApplied(value);
} else if (key.equals("recon_algorithm")) {
value = transformReconAlgorithm(value);
} else if (key.equals("x_filter")) {
value = transformFilter(value);
} else if (key.equals("y_filter")) {
value = transformFilter(value);
} else if (key.equals("z_filter")) {
value = transformFilter(value);
} else if (key.equals("subject_orientation")) {
value = transformSubjectOrientation(value);
} else if (key.equals("subject_length_units")) {
value = transformSubjectLengthUnits(value);
} else if (key.equals("subject_weight_units")) {
value = transformSubjectWeightUnits(value);
} else if (key.equals("gantry_rotation")) {
value = transformGantryRotation(value);
} else if (key.equals("rotation_direction")) {
value = transformRotationDirection(value);
} else if (key.equals("ct_warping")) {
value = transformCTWarping(value);
} else if (key.equals("ct_projection_interpolation")) {
value = transformCTProjectionInterpolation(value);
} else if (key.equals("event_type")) {
value = transformEventType(value);
} else if (key.equals("projection") || key.equals("ct_projection_center_offset") || key.equals("ct_projection_horizontal_bed_offset")) {
space = value.indexOf(' ');
int index = Integer.parseInt(value.substring(0, space));
value = value.substring(space + 1);
key += " " + index;
} else if (key.equals("user")) {
space = value.indexOf(' ');
key = value.substring(0, space);
value = value.substring(space + 1);
} else if (key.equals("file_name")) {
// remove path from stored file name, if present
value = value.replace('/', File.separatorChar);
value = value.replace('\\', File.separatorChar);
value = value.substring(value.lastIndexOf(File.separator) + 1);
Location header = new Location(currentId).getAbsoluteFile();
datFile = new Location(header.getParent(), value).getAbsolutePath();
} else if (key.equals("time_frames")) {
int sizeT = Integer.parseInt(value);
for (int i = 0; i < core.size(); i++) {
core.get(i).sizeT = sizeT;
}
} else if (key.equals("total_frames")) {
frames = Integer.parseInt(value);
} else if (key.equals("number_of_bed_positions")) {
int nPos = (int) Math.min(frames, Integer.parseInt(value));
if (nPos > 1) {
CoreMetadata original = core.get(0);
core.clear();
for (int i = 0; i < nPos; i++) {
core.add(original);
}
}
} else if (key.equals("data_type")) {
setDataType(value);
} else if (key.equals("x_dimension")) {
int sizeX = Integer.parseInt(value);
for (int i = 0; i < core.size(); i++) {
core.get(i).sizeX = sizeX;
}
} else if (key.equals("y_dimension")) {
int sizeY = Integer.parseInt(value);
for (int i = 0; i < core.size(); i++) {
core.get(i).sizeY = sizeY;
}
} else if (key.equals("z_dimension")) {
int sizeZ = Integer.parseInt(value);
for (int i = 0; i < core.size(); i++) {
core.get(i).sizeZ = sizeZ;
}
} else if (key.equals("scan_time")) {
date = value;
} else if (key.equals("data_file_pointer")) {
String[] values = value.split(" ");
int[] ints = new int[values.length];
for (int i = 0; i < ints.length; i++) {
ints[i] = Integer.parseInt(values[i]);
}
byte[] b = DataTools.intsToBytes(ints, false);
dataPointers.add(DataTools.bytesToLong(b, false));
} else // pixel sizes stored in mm
if (key.equals("pixel_size_x")) {
pixelSizeX = new Double(value) * 1000;
} else if (key.equals("pixel_size_y")) {
pixelSizeY = new Double(value) * 1000;
} else if (key.equals("pixel_size_z")) {
pixelSizeZ = new Double(value) * 1000;
}
addGlobalMeta(key, value);
}
}
for (int i = 0; i < core.size(); i++) {
CoreMetadata ms = core.get(i);
if (ms.sizeZ == 0) {
ms.sizeZ = 1;
}
if (ms.sizeT == 0) {
ms.sizeT = 1;
}
ms.sizeC = 1;
ms.rgb = false;
ms.interleaved = false;
ms.indexed = false;
ms.dimensionOrder = "XYZCT";
ms.imageCount = ms.sizeZ * ms.sizeC * ms.sizeT;
}
MetadataStore store = makeFilterMetadata();
MetadataTools.populatePixels(store, this);
String experimenter = null, instrument = null;
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
experimenter = MetadataTools.createLSID("Experimenter", 0);
store.setExperimenterID(experimenter, 0);
store.setExperimenterUserName(investigator, 0);
store.setExperimenterInstitution(institution, 0);
instrument = MetadataTools.createLSID("Instrument", 0);
store.setInstrumentID(instrument, 0);
store.setMicroscopeModel(model, 0);
}
for (int i = 0; i < core.size(); i++) {
if (date != null) {
String newDate = DateTools.formatDate(date, "EEE MMM dd HH:mm:ss yyyy");
if (newDate != null) {
store.setImageAcquisitionDate(new Timestamp(newDate), i);
}
}
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
if (experimenter != null) {
store.setImageExperimenterRef(experimenter, i);
}
if (instrument != null) {
store.setImageInstrumentRef(instrument, i);
}
store.setImageDescription(description, i);
Length sizeX = FormatTools.getPhysicalSizeX(pixelSizeX);
Length sizeY = FormatTools.getPhysicalSizeY(pixelSizeY);
Length sizeZ = FormatTools.getPhysicalSizeZ(pixelSizeZ);
if (sizeX != null) {
store.setPixelsPhysicalSizeX(sizeX, i);
}
if (sizeY != null) {
store.setPixelsPhysicalSizeY(sizeY, i);
}
if (sizeZ != null) {
store.setPixelsPhysicalSizeZ(sizeZ, i);
}
}
}
}
use of loci.common.Location in project bioformats by openmicroscopy.
the class L2DReader method initFile.
// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
if (!checkSuffix(id, "l2d") && isGroupFiles()) {
// find the corresponding .l2d file
Location parent = new Location(id).getAbsoluteFile().getParentFile();
parent = parent.getParentFile();
String[] list = parent.list();
for (String file : list) {
if (checkSuffix(file, "l2d")) {
initFile(new Location(parent, file).getAbsolutePath());
return;
}
}
throw new FormatException("Could not find .l2d file");
} else if (!isGroupFiles()) {
super.initFile(id);
tiffs = new String[][] { { id } };
TiffReader r = new TiffReader();
r.setMetadataStore(getMetadataStore());
r.setId(id);
core = new ArrayList<CoreMetadata>(r.getCoreMetadataList());
metadataStore = r.getMetadataStore();
final Map<String, Object> globalMetadata = r.getGlobalMetadata();
for (final Map.Entry<String, Object> entry : globalMetadata.entrySet()) {
addGlobalMeta(entry.getKey(), entry.getValue());
}
r.close();
reader = new MinimalTiffReader();
return;
}
super.initFile(id);
String[] scans = getScanNames();
Location parent = new Location(id).getAbsoluteFile().getParentFile();
// remove scan names that do not correspond to existing directories
final List<String> validScans = new ArrayList<String>();
for (String s : scans) {
Location scanDir = new Location(parent, s);
if (scanDir.exists() && scanDir.isDirectory())
validScans.add(s);
}
scans = validScans.toArray(new String[validScans.size()]);
// read metadata from each scan
tiffs = new String[scans.length][];
metadataFiles = new List[scans.length];
core = new ArrayList<CoreMetadata>(scans.length);
String[] comments = new String[scans.length];
String[] wavelengths = new String[scans.length];
String[] dates = new String[scans.length];
String model = null;
tileWidth = new int[scans.length];
tileHeight = new int[scans.length];
core.clear();
for (int i = 0; i < scans.length; i++) {
CoreMetadata ms = new CoreMetadata();
core.add(ms);
setSeries(i);
metadataFiles[i] = new ArrayList<String>();
String scanName = scans[i] + ".scn";
Location scanDir = new Location(parent, scans[i]);
// read .scn file from each scan
String scanPath = new Location(scanDir, scanName).getAbsolutePath();
addDirectory(scanDir.getAbsolutePath(), i);
String scanData = DataTools.readFile(scanPath);
String[] lines = scanData.split("\n");
for (String line : lines) {
if (!line.startsWith("#")) {
String key = line.substring(0, line.indexOf('='));
String value = line.substring(line.indexOf('=') + 1);
addSeriesMeta(key, value);
if (key.equals("ExperimentNames")) {
// TODO : parse experiment metadata - this is typically a list of
// overlay shapes, or analysis data
} else if (key.equals("ImageNames")) {
tiffs[i] = value.split(",");
for (int t = 0; t < tiffs[i].length; t++) {
tiffs[i][t] = new Location(scanDir, tiffs[i][t].trim()).getAbsolutePath();
}
} else if (key.equals("Comments")) {
comments[i] = value;
} else if (key.equals("ScanDate")) {
dates[i] = value;
} else if (key.equals("ScannerName")) {
model = value;
} else if (key.equals("ScanChannels")) {
wavelengths[i] = value;
}
}
}
}
setSeries(0);
reader = new MinimalTiffReader();
MetadataStore store = makeFilterMetadata();
for (int i = 0; i < getSeriesCount(); i++) {
CoreMetadata ms = core.get(i);
ms.imageCount = tiffs[i].length;
ms.sizeC = tiffs[i].length;
ms.sizeT = 1;
ms.sizeZ = 1;
ms.dimensionOrder = "XYCZT";
reader.setId(tiffs[i][0]);
ms.sizeX = reader.getSizeX();
ms.sizeY = reader.getSizeY();
ms.sizeC *= reader.getSizeC();
ms.rgb = reader.isRGB();
ms.indexed = reader.isIndexed();
ms.littleEndian = reader.isLittleEndian();
ms.pixelType = reader.getPixelType();
tileWidth[i] = reader.getOptimalTileWidth();
tileHeight[i] = reader.getOptimalTileHeight();
}
MetadataTools.populatePixels(store, this);
for (int i = 0; i < getSeriesCount(); i++) {
store.setImageName(scans[i], i);
if (dates[i] != null) {
dates[i] = DateTools.formatDate(dates[i], DATE_FORMAT);
if (dates[i] != null) {
store.setImageAcquisitionDate(new Timestamp(dates[i]), i);
}
}
}
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
String instrumentID = MetadataTools.createLSID("Instrument", 0);
store.setInstrumentID(instrumentID, 0);
for (int i = 0; i < getSeriesCount(); i++) {
store.setImageInstrumentRef(instrumentID, i);
store.setImageDescription(comments[i], i);
if (wavelengths[i] != null) {
String[] waves = wavelengths[i].split("[, ]");
if (waves.length < getEffectiveSizeC()) {
LOGGER.debug("Expected {} wavelengths; got {} wavelengths.", getEffectiveSizeC(), waves.length);
}
for (int q = 0; q < waves.length; q++) {
String laser = MetadataTools.createLSID("LightSource", 0, q);
store.setLaserID(laser, 0, q);
Double wave = new Double(waves[q].trim());
Length wavelength = FormatTools.getWavelength(wave);
if (wavelength != null) {
store.setLaserWavelength(wavelength, 0, q);
}
store.setLaserType(getLaserType("Other"), 0, q);
store.setLaserLaserMedium(getLaserMedium("Other"), 0, q);
store.setChannelLightSourceSettingsID(laser, i, q);
}
}
}
store.setMicroscopeModel(model, 0);
store.setMicroscopeType(getMicroscopeType("Other"), 0);
}
}
Aggregations