use of loci.formats.services.MDBService in project bioformats by openmicroscopy.
the class APLReader method initFile.
// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
super.initFile(id);
LOGGER.debug("Initializing {}", id);
// find the corresponding .mtb file
if (!checkSuffix(id, "mtb")) {
if (checkSuffix(id, METADATA_SUFFIXES)) {
int separator = id.lastIndexOf(File.separator);
if (separator < 0)
separator = 0;
int underscore = id.lastIndexOf("_");
if (underscore < separator || checkSuffix(id, "apl")) {
underscore = id.lastIndexOf(".");
}
String mtbFile = id.substring(0, underscore) + "_d.mtb";
if (!new Location(mtbFile).exists()) {
throw new FormatException(".mtb file not found");
}
currentId = new Location(mtbFile).getAbsolutePath();
} else {
Location parent = new Location(id).getAbsoluteFile().getParentFile();
parent = parent.getParentFile();
String[] list = parent.list(true);
for (String f : list) {
if (checkSuffix(f, "mtb")) {
currentId = new Location(parent, f).getAbsolutePath();
break;
}
}
if (!checkSuffix(currentId, "mtb")) {
throw new FormatException(".mtb file not found");
}
}
}
String mtb = new Location(currentId).getAbsolutePath();
LOGGER.debug("Reading .mtb file '{}'", mtb);
MDBService mdb = null;
try {
ServiceFactory factory = new ServiceFactory();
mdb = factory.getInstance(MDBService.class);
} catch (DependencyException de) {
throw new FormatException("MDB Tools Java library not found", de);
}
String[] columnNames = null;
List<String[]> rows = null;
try {
mdb.initialize(mtb);
rows = mdb.parseDatabase().get(0);
columnNames = rows.get(0);
String[] tmpNames = columnNames;
columnNames = new String[tmpNames.length - 1];
System.arraycopy(tmpNames, 1, columnNames, 0, columnNames.length);
} finally {
mdb.close();
}
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
for (int i = 1; i < rows.size(); i++) {
String[] row = rows.get(i);
for (int q = 0; q < row.length; q++) {
addGlobalMetaList(columnNames[q], row[q]);
}
}
}
used = new ArrayList<String>();
used.add(mtb);
String tnb = mtb.substring(0, mtb.lastIndexOf("."));
if (tnb.lastIndexOf("_") > tnb.lastIndexOf(File.separator)) {
tnb = tnb.substring(0, tnb.lastIndexOf("_"));
}
used.add(tnb + "_1.tnb");
used.add(tnb + ".apl");
String idPath = new Location(id).getAbsolutePath();
if (!used.contains(idPath) && checkSuffix(idPath, METADATA_SUFFIXES)) {
used.add(idPath);
}
// calculate indexes to relevant metadata
int calibrationUnit = DataTools.indexOf(columnNames, "Calibration Unit");
int colorChannels = DataTools.indexOf(columnNames, "Color Channels");
int frames = DataTools.indexOf(columnNames, "Frames");
int calibratedHeight = DataTools.indexOf(columnNames, "Height");
int calibratedWidth = DataTools.indexOf(columnNames, "Width");
int path = DataTools.indexOf(columnNames, "Image Path");
if (path == -1) {
path = DataTools.indexOf(columnNames, "Path");
}
int filename = DataTools.indexOf(columnNames, "File Name");
int magnification = DataTools.indexOf(columnNames, "Magnification");
int width = DataTools.indexOf(columnNames, "X-Resolution");
int height = DataTools.indexOf(columnNames, "Y-Resolution");
int imageType = DataTools.indexOf(columnNames, "Image Type");
int imageName = DataTools.indexOf(columnNames, "Image Name");
int zLayers = DataTools.indexOf(columnNames, "Z-Layers");
String parentDirectory = mtb.substring(0, mtb.lastIndexOf(File.separator));
// look for the directory that contains TIFF and XML files
LOGGER.debug("Searching {} for a directory with TIFFs", parentDirectory);
Location dir = new Location(parentDirectory);
String topDirectory = null;
int index = 2;
String pathName = rows.get(index++)[path].trim();
while (pathName.equals("") && index < rows.size()) {
pathName = rows.get(index++)[path].trim();
}
pathName = pathName.replace('\\', File.separatorChar);
pathName = pathName.replaceAll("/", File.separator);
String[] dirs = pathName.split(File.separatorChar == '\\' ? "\\\\" : File.separator);
for (int i = dirs.length - 1; i >= 0; i--) {
if (dirs[i].indexOf("_DocumentFiles") > 0) {
Location file = new Location(dir, dirs[i]);
if (file.exists()) {
topDirectory = file.getAbsolutePath();
break;
}
}
}
if (topDirectory == null) {
String[] list = dir.list();
for (String f : list) {
LOGGER.debug(" '{}'", f);
Location file = new Location(dir, f);
if (file.isDirectory() && f.indexOf("_DocumentFiles") > 0) {
topDirectory = file.getAbsolutePath();
LOGGER.debug("Found {}", topDirectory);
break;
}
}
}
if (topDirectory == null) {
throw new FormatException("Could not find a directory with TIFF files.");
}
final List<Integer> seriesIndexes = new ArrayList<Integer>();
for (int i = 1; i < rows.size(); i++) {
String file = parseFilename(rows.get(i), filename, path);
if (file.equals(""))
continue;
file = topDirectory + File.separator + file;
if (new Location(file).exists() && checkSuffix(file, "tif")) {
seriesIndexes.add(i);
}
}
int seriesCount = seriesIndexes.size();
core.clear();
tiffFiles = new String[seriesCount];
xmlFiles = new String[seriesCount];
parser = new TiffParser[seriesCount];
ifds = new IFDList[seriesCount];
for (int i = 0; i < seriesCount; i++) {
CoreMetadata ms = new CoreMetadata();
core.add(ms);
int secondRow = seriesIndexes.get(i);
int firstRow = secondRow - 1;
String[] row2 = rows.get(firstRow);
String[] row3 = rows.get(secondRow);
if (frames > -1) {
ms.sizeT = parseDimension(row3[frames]);
}
if (zLayers > -1) {
ms.sizeZ = parseDimension(row3[zLayers]);
}
if (colorChannels > -1) {
ms.sizeC = parseDimension(row3[colorChannels]);
} else if (imageType > -1) {
if (row3[imageType] != null && row3[imageType].equals("RGB")) {
ms.sizeC = 3;
}
}
ms.dimensionOrder = "XYCZT";
if (ms.sizeZ == 0)
ms.sizeZ = 1;
if (ms.sizeC == 0)
ms.sizeC = 1;
if (ms.sizeT == 0)
ms.sizeT = 1;
xmlFiles[i] = topDirectory + File.separator + parseFilename(row2, filename, path);
tiffFiles[i] = topDirectory + File.separator + parseFilename(row3, filename, path);
parser[i] = new TiffParser(tiffFiles[i]);
parser[i].setDoCaching(false);
ifds[i] = parser[i].getIFDs();
for (IFD ifd : ifds[i]) {
parser[i].fillInIFD(ifd);
}
// get core metadata from TIFF file
IFD ifd = ifds[i].get(0);
PhotoInterp photo = ifd.getPhotometricInterpretation();
int samples = ifd.getSamplesPerPixel();
ms.sizeX = (int) ifd.getImageWidth();
ms.sizeY = (int) ifd.getImageLength();
ms.rgb = samples > 1 || photo == PhotoInterp.RGB;
ms.pixelType = ifd.getPixelType();
ms.littleEndian = ifd.isLittleEndian();
ms.indexed = photo == PhotoInterp.RGB_PALETTE && ifd.containsKey(IFD.COLOR_MAP);
ms.imageCount = ifds[i].size();
if (ms.sizeZ * ms.sizeT * (ms.rgb ? 1 : ms.sizeC) != ms.imageCount) {
ms.sizeT = ms.imageCount / (ms.rgb ? 1 : ms.sizeC);
ms.sizeZ = 1;
}
}
MetadataStore store = makeFilterMetadata();
MetadataTools.populatePixels(store, this);
for (int i = 0; i < seriesCount; i++) {
String[] row = rows.get(seriesIndexes.get(i));
// populate Image data
MetadataTools.setDefaultCreationDate(store, mtb, i);
store.setImageName(row[imageName].trim(), i);
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
// populate Dimensions data
// calculate physical X and Y sizes
double realWidth = Double.parseDouble(row[calibratedWidth]);
double realHeight = Double.parseDouble(row[calibratedHeight]);
String units = row[calibrationUnit];
CoreMetadata ms = core.get(i);
double px = realWidth / ms.sizeX;
double py = realHeight / ms.sizeY;
Length physicalSizeX = FormatTools.getPhysicalSizeX(px, units);
Length physicalSizeY = FormatTools.getPhysicalSizeY(py, units);
if (physicalSizeX != null) {
store.setPixelsPhysicalSizeX(physicalSizeX, i);
}
if (physicalSizeY != null) {
store.setPixelsPhysicalSizeY(physicalSizeY, i);
}
}
}
setSeries(0);
}
use of loci.formats.services.MDBService in project bioformats by openmicroscopy.
the class ZeissLSMReader method parseMDB.
/**
* Parse a .mdb file and return a list of referenced .lsm files.
*/
private String[] parseMDB(String mdbFile) throws FormatException, IOException {
Location mdb = new Location(mdbFile).getAbsoluteFile();
Location parent = mdb.getParentFile();
MDBService mdbService = null;
try {
ServiceFactory factory = new ServiceFactory();
mdbService = factory.getInstance(MDBService.class);
} catch (DependencyException de) {
throw new FormatException("MDB Tools Java library not found", de);
}
try {
mdbService.initialize(mdbFile);
} catch (Exception e) {
return null;
}
Vector<Vector<String[]>> tables = mdbService.parseDatabase();
mdbService.close();
final List<String> referencedLSMs = new ArrayList<String>();
int referenceCount = 0;
for (Vector<String[]> table : tables) {
String[] columnNames = table.get(0);
String tableName = columnNames[0];
for (int row = 1; row < table.size(); row++) {
String[] tableRow = table.get(row);
for (int col = 0; col < tableRow.length; col++) {
String key = tableName + " " + columnNames[col + 1];
if (currentId != null) {
addGlobalMetaList(key, tableRow[col]);
}
if (tableName.equals("Recordings") && columnNames[col + 1] != null && columnNames[col + 1].equals("SampleData")) {
String filename = tableRow[col].trim();
filename = filename.replace('\\', File.separatorChar);
filename = filename.replace('/', File.separatorChar);
filename = filename.substring(filename.lastIndexOf(File.separator) + 1);
if (filename.length() > 0) {
Location file = new Location(parent, filename);
if (file.exists()) {
referencedLSMs.add(file.getAbsolutePath());
}
}
referenceCount++;
}
}
}
}
if (referencedLSMs.size() == referenceCount) {
return referencedLSMs.toArray(new String[0]);
}
String[] fileList = parent.list(true);
Arrays.sort(fileList);
for (int i = 0; i < fileList.length; i++) {
Location f = new Location(fileList[i]);
if (!f.exists()) {
f = new Location(parent, fileList[i]);
}
String absolutePath = f.getAbsolutePath();
if (checkSuffix(fileList[i], "mdb") && (!absolutePath.equals(mdbFile) && !fileList[i].equals(mdbFile) && !absolutePath.equals(new Location(mdbFile).getAbsolutePath()))) {
if (referencedLSMs.size() > 0) {
return referencedLSMs.toArray(new String[0]);
}
break;
}
}
referencedLSMs.clear();
int mdbCount = 0;
for (int i = 0; i < fileList.length; i++) {
String absolutePath = new Location(parent, fileList[i]).getAbsolutePath();
if (checkSuffix(fileList[i], "lsm")) {
referencedLSMs.add(absolutePath);
} else if (checkSuffix(fileList[i], "mdb")) {
mdbCount++;
}
}
if (mdbCount > 1 || ((referencedLSMs.size() > referenceCount) && mdbCount > 1)) {
for (int i = 0; i < fileList.length; i++) {
String absolutePath = new Location(parent, fileList[i]).getAbsolutePath();
if (checkSuffix(fileList[i], "mdb") && !absolutePath.endsWith(mdbFile)) {
String[] files = parseMDB(absolutePath);
for (String f : files) {
referencedLSMs.remove(f);
}
}
}
}
return referencedLSMs.toArray(new String[0]);
}
use of loci.formats.services.MDBService in project bioformats by openmicroscopy.
the class MissingMDBServiceTest method testInstantiate.
@Test(expectedExceptions = { DependencyException.class })
public void testInstantiate() throws DependencyException {
MDBService service = sf.getInstance(MDBService.class);
assertNotNull(service);
}
Aggregations