Skip to content

Instantly share code, notes, and snippets.

@chris-allan
Created January 31, 2012 11:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chris-allan/1710116 to your computer and use it in GitHub Desktop.
Save chris-allan/1710116 to your computer and use it in GitHub Desktop.
OMERO import example in Java
import loci.formats.in.DefaultMetadataOptions;
import loci.formats.in.MetadataLevel;
import ome.formats.OMEROMetadataStoreClient;
import ome.formats.importer.ImportConfig;
import ome.formats.importer.cli.ErrorHandler;
import ome.formats.importer.cli.LoggingImportMonitor;
import omero.model.Dataset;
public class OmeroImport {
public static void main(String[] args) throws Exception {
if (args.length != 6) {
System.err.println("Usage: OmeroImport <host> <port> " +
"<username> <password> <dataset_id> <path>");
System.exit(1);
}
// Set up configuration parameters
ImportConfig config = new ImportConfig();
config.email.set("");
config.sendFiles.set(true);
config.sendReport.set(false);
config.contOnError.set(false);
config.debug.set(false);
config.hostname.set(args[0]);
config.port.set(Integer.parseInt(args[1]));
config.username.set(args[2]);
config.password.set(args[3]);
config.targetClass.set(Dataset.class.getName());
config.targetId.set(Long.parseLong(args[4]));
// Create an array with the paths that we are going to check as being
// an import candidate and subsequently import.
String[] paths = new String[] { args[5] };
// Create a new metadata store using the configuration we've set up,
// this will log in to OMERO.
OMEROMetadataStoreClient store = config.createStore();
try {
store.logVersionInfo(config.getIniVersionNumber());
// Create a OMERO reader wrapper which encompasses the related
// Bio-Formats functionality required for an OMERO import, an
// import library which is responsible for actually performing an
// OMERO import life cycle and an error handler which encompasses
// all error reporting while an import is taking place.
OMEROWrapper reader = new OMEROWrapper(config);
ImportLibrary library = new ImportLibrary(store, reader);
ErrorHandler handler = new ErrorHandler(config);
// Add a logging observer to the import library which will print
// to the log file (defaulting to STDOUT/STDERR) the status of the
// import process.
library.addObserver(new LoggingImportMonitor());
// Calculate the candidates that are required for the import, which
// is done with a metadata level of MINIMUM to avoid parsing and
// populating all metadata during this process.
ImportCandidates candidates =
new ImportCandidates(reader, paths, handler);
// Up the metadata level to ALL so that the actual import itself
// will be complete.
reader.setMetadataOptions(
new DefaultMetadataOptions(MetadataLevel.ALL));
boolean success = library.importCandidates(config, candidates);
if (success) {
System.exit(0);
} else {
System.exit(1);
}
} finally {
store.logout();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment