//Add dependencies to the classpath %classpath add jar /srv/conda/vnc/Fiji.app/jars/* %classpath add jar /srv/conda/vnc/Fiji.app/plugins/* g = new EasyForm("Enter credentials and continue to the next cell. Do not re-run this cell") g.addTextField("Server").onInit({g['Server'] = "wss://workshop.openmicroscopy.org/omero-ws"}) g.addTextField("UserName") g.addPasswordField("Password") g import omero.gateway.Gateway import omero.gateway.LoginCredentials import omero.log.SimpleLogger // Method to connect to OMERO def connect_to_omero() { "Connect to OMERO" credentials = new LoginCredentials() credentials.getServer().setHostname(g['Server']) credentials.getUser().setUsername(g['UserName'].trim()) credentials.getUser().setPassword(g['Password'].trim()) simpleLogger = new SimpleLogger() gateway = new Gateway(simpleLogger) gateway.connect(credentials) return gateway } // Connect to OMERO println "connecting..." gateway = connect_to_omero() println "connected..." //Move to the next cell after entering a value ga = new EasyForm("Select the Image to analyze") ga.addTextField("ImageID") ga //Connection information HOST = g['Server'] USERNAME = g['UserName'].trim() PASSWORD = g['Password'].trim() //Image to analyze image_id = ga['ImageID'].toLong() group_id = "-1" import ij.IJ //Function to Open an OMERO image using Bio-Formats def open_image_plus(host, username, password, group_id, image_id) { "Open the image using the Bio-Formats Importer" StringBuilder options = new StringBuilder() options.append("location=[OMERO] open=[omero:server=") options.append(host) options.append("\nuser=") options.append(username) options.append("\nport=") options.append(443) options.append("\npass=") options.append(password) options.append("\ngroupID=") options.append(group_id) options.append("\niid=") options.append(image_id) options.append("] ") options.append("windowless=true view=Hyperstack ") IJ.runPlugIn("loci.plugins.LociImporter", options.toString()) } println "opening Image..." // Open the Image using Bio-Formats open_image_plus(HOST, USERNAME, PASSWORD, group_id, String.valueOf(image_id)) // Crop the image println "cropping..." IJ.makeRectangle(0, 0, 200, 200) IJ.run("Crop") import ij.IJ import java.io.File // Save modified image as OME-TIFF using Bio-Formats Exporter imp = IJ.getImage() name = imp.getTitle().replaceAll("\\s","") file = File.createTempFile(name, ".ome.tiff") path_to_file = file.getAbsolutePath() println path_to_file options = "outfile=" + path_to_file + " export compression=Uncompressed windowless=true" IJ.runPlugIn("loci.plugins.LociExporter", options.toString()) imp.changes = false imp.close() import java.lang.reflect.Array import omero.gateway.SecurityContext import omero.gateway.facility.BrowseFacility import omero.gateway.facility.DataManagerFacility import omero.gateway.model.DatasetData import ome.formats.importer.ImportConfig import ome.formats.importer.OMEROWrapper import ome.formats.importer.ImportLibrary import ome.formats.importer.ImportCandidates import ome.formats.importer.cli.ErrorHandler import ome.formats.importer.cli.LoggingImportMonitor import loci.formats.in.DefaultMetadataOptions import loci.formats.in.MetadataLevel //Find the dataset matching the specified ID def find_dataset(gateway, dataset_id) { "Load the Dataset" browse = gateway.getFacility(BrowseFacility) user = gateway.getLoggedInUser() ctx = new SecurityContext(user.getGroupId()) return browse.findIObject(ctx, "omero.model.Dataset", dataset_id) } //Upload the generated image def upload_image(paths, gateway, id) { "Upload an image to OMERO" user = gateway.getLoggedInUser() sessionKey = gateway.getSessionId(user) config = new ImportConfig() config.debug.set('false') config.hostname.set(HOST) config.sessionKey.set(sessionKey) config.port.set(443) dataset = find_dataset(gateway, id) store = config.createStore() reader = new OMEROWrapper(config) library = new ImportLibrary(store, reader) error_handler = new ErrorHandler(config) library.addObserver(new LoggingImportMonitor()) candidates = new ImportCandidates(reader, paths, error_handler) containers = candidates.getContainers() containers.each() { c -> c.setTarget(dataset) } reader.setMetadataOptions(new DefaultMetadataOptions(MetadataLevel.ALL)) return library.importCandidates(config, candidates) } // Create a Dataset d = new DatasetData() d.setName("Cropped Image") dm = gateway.getFacility(DataManagerFacility) user = gateway.getLoggedInUser() ctx = new SecurityContext(user.getGroupId()) d = dm.createDataset(ctx, d, null) // Import the generated OME-TIFF to OMERO println "importing..." str2d = new String[1] str2d[0] = path_to_file success = upload_image(str2d, gateway, d.getId()) println "imported" // Close the connection gateway.disconnect() println "Done" // delete the local OME-TIFF image file.delete()