{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Crop OMERO Image using Fiji\n", "\n", "\n", "This notebook shows how to crop an OMERO image using the crop functionality available in [Fiji](https://imagej.net/Fiji).\n", "\n", "Fiji has been installed with few other plugins including the omero_ij plugin to allow to connect to an OMERO server." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Description\n", "\n", "The following section shows:\n", " * how to connect to OMERO\n", " * how to open an OMERO image using Bio-Formats\n", " * how to crop an image using Fiji\n", " * how to save the generated image as OME-TIFF\n", " * how to import the created OME-TIFF to OMERO" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Setup: start the [desktop](../../desktop) if it is not already up\n", "The link should open in a different window. If you see an error message try refreshing the window." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Setup: Add plugins to Classpath\n", "First we had Fiji and the plugins to the classpath." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "43d1d91f-f79a-4623-a886-61d38508c0a0", "version_major": 2, "version_minor": 0 }, "method": "display_data" }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "700b97a3-b91f-4642-accb-91b0d66539d0", "version_major": 2, "version_minor": 0 }, "method": "display_data" }, "metadata": {}, "output_type": "display_data" } ], "source": [ "//Add dependencies to the classpath\n", "%classpath add jar /srv/conda/vnc/Fiji.app/jars/*\n", "%classpath add jar /srv/conda/vnc/Fiji.app/plugins/*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Enter credentials. Do not re-run this cell." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "cc3d14a2-a6e0-4ae7-8dd7-ccfc7fc41c07", "version_major": 2, "version_minor": 0 }, "method": "display_data" }, "metadata": {}, "output_type": "display_data" } ], "source": [ "g = new EasyForm(\"Enter credentials and continue to the next cell. Do not re-run this cell\")\n", "g.addTextField(\"Server\").onInit({g['Server'] = \"wss://workshop.openmicroscopy.org/omero-ws\"})\n", "g.addTextField(\"UserName\")\n", "g.addPasswordField(\"Password\")\n", "g" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Connect to OMERO" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "connecting...\n", "connected...\n" ] }, { "data": { "text/plain": [ "null" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import omero.gateway.Gateway\n", "import omero.gateway.LoginCredentials\n", "import omero.log.SimpleLogger\n", "\n", "// Method to connect to OMERO\n", "def connect_to_omero() {\n", " \"Connect to OMERO\"\n", "\n", " credentials = new LoginCredentials()\n", " credentials.getServer().setHostname(g['Server'])\n", " credentials.getUser().setUsername(g['UserName'].trim())\n", " credentials.getUser().setPassword(g['Password'].trim())\n", " simpleLogger = new SimpleLogger()\n", " gateway = new Gateway(simpleLogger)\n", " gateway.connect(credentials)\n", " return gateway\n", "\n", "}\n", "\n", "// Connect to OMERO\n", "println \"connecting...\"\n", "gateway = connect_to_omero()\n", "println \"connected...\"\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Select the Image. Do not re-run this cell." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "scrolled": true }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "44ae2872-73e3-4d84-8e42-3bcd9aaad137", "version_major": 2, "version_minor": 0 }, "method": "display_data" }, "metadata": {}, "output_type": "display_data" } ], "source": [ "//Move to the next cell after entering a value\n", "ga = new EasyForm(\"Select the Image to analyze\")\n", "ga.addTextField(\"ImageID\")\n", "ga" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Collect parameters " ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "//Connection information\n", "HOST = g['Server']\n", "USERNAME = g['UserName'].trim()\n", "PASSWORD = g['Password'].trim()\n", "\n", "//Image to analyze\n", "image_id = ga['ImageID'].toLong()\n", "group_id = \"-1\"\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Open the image using Bio-Formats and crop it" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "opening Image...\n", "cropping...\n" ] }, { "data": { "text/plain": [ "null" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import ij.IJ\n", "//Function to Open an OMERO image using Bio-Formats\n", "def open_image_plus(host, username, password, group_id, image_id) {\n", " \"Open the image using the Bio-Formats Importer\"\n", "\n", " StringBuilder options = new StringBuilder()\n", " options.append(\"location=[OMERO] open=[omero:server=\")\n", " options.append(host)\n", " options.append(\"\\nuser=\")\n", " options.append(username)\n", " options.append(\"\\nport=\")\n", " options.append(443)\n", " options.append(\"\\npass=\")\n", " options.append(password)\n", " options.append(\"\\ngroupID=\")\n", " options.append(group_id)\n", " options.append(\"\\niid=\")\n", " options.append(image_id)\n", " options.append(\"] \")\n", " options.append(\"windowless=true view=Hyperstack \")\n", " IJ.runPlugIn(\"loci.plugins.LociImporter\", options.toString())\n", "}\n", "\n", "println \"opening Image...\"\n", "// Open the Image using Bio-Formats\n", "open_image_plus(HOST, USERNAME, PASSWORD, group_id, String.valueOf(image_id))\n", "\n", "// Crop the image\n", "println \"cropping...\"\n", "IJ.makeRectangle(0, 0, 200, 200)\n", "IJ.run(\"Crop\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Save the cropped image as OME-TIFF" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/tmp/33676-EB1_dsRed_(red)_tracking_along_peripheral_SEPT2-YFP_fila...(green)._Spinning_disc_confocal_microscopy..ome.tiff_kymograph4297281605584244054.ome.tiff\n" ] }, { "data": { "text/plain": [ "null" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import ij.IJ\n", "import java.io.File\n", "\n", "// Save modified image as OME-TIFF using Bio-Formats Exporter\n", "imp = IJ.getImage()\n", "name = imp.getTitle().replaceAll(\"\\\\s\",\"\")\n", "file = File.createTempFile(name, \".ome.tiff\")\n", "path_to_file = file.getAbsolutePath()\n", "println path_to_file\n", "options = \"outfile=\" + path_to_file + \" export compression=Uncompressed windowless=true\"\n", "IJ.runPlugIn(\"loci.plugins.LociExporter\", options.toString())\n", "imp.changes = false\n", "imp.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Import the OME-TIFF into a new dataset" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "importing...\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "May 25, 2020 7:22:49 AM java.util.prefs.FileSystemPreferences$1 run\n", "INFO: Created user preferences directory.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Image:57239\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Other imported objects:\n", "Fileset:39953\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "imported\n" ] }, { "data": { "text/plain": [ "null" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import java.lang.reflect.Array\n", "\n", "import omero.gateway.SecurityContext\n", "import omero.gateway.facility.BrowseFacility\n", "import omero.gateway.facility.DataManagerFacility\n", "import omero.gateway.model.DatasetData\n", "\n", "import ome.formats.importer.ImportConfig\n", "import ome.formats.importer.OMEROWrapper\n", "import ome.formats.importer.ImportLibrary\n", "import ome.formats.importer.ImportCandidates\n", "import ome.formats.importer.cli.ErrorHandler\n", "import ome.formats.importer.cli.LoggingImportMonitor\n", "\n", "import loci.formats.in.DefaultMetadataOptions\n", "import loci.formats.in.MetadataLevel\n", "\n", "//Find the dataset matching the specified ID\n", "def find_dataset(gateway, dataset_id) {\n", " \"Load the Dataset\"\n", " browse = gateway.getFacility(BrowseFacility)\n", " user = gateway.getLoggedInUser()\n", " ctx = new SecurityContext(user.getGroupId())\n", " return browse.findIObject(ctx, \"omero.model.Dataset\", dataset_id)\n", "}\n", "\n", "//Upload the generated image\n", "def upload_image(paths, gateway, id) {\n", " \"Upload an image to OMERO\"\n", "\n", " user = gateway.getLoggedInUser()\n", " sessionKey = gateway.getSessionId(user)\n", "\n", " config = new ImportConfig()\n", " config.debug.set('false')\n", " config.hostname.set(HOST)\n", " config.sessionKey.set(sessionKey)\n", " config.port.set(443)\n", " dataset = find_dataset(gateway, id)\n", "\n", " store = config.createStore()\n", " reader = new OMEROWrapper(config)\n", "\n", " library = new ImportLibrary(store, reader)\n", " error_handler = new ErrorHandler(config)\n", "\n", " library.addObserver(new LoggingImportMonitor())\n", " candidates = new ImportCandidates(reader, paths, error_handler)\n", " containers = candidates.getContainers()\n", " containers.each() { c ->\n", " c.setTarget(dataset)\n", " }\n", " reader.setMetadataOptions(new DefaultMetadataOptions(MetadataLevel.ALL))\n", " return library.importCandidates(config, candidates)\n", "}\n", "\n", "// Create a Dataset\n", "d = new DatasetData()\n", "d.setName(\"Cropped Image\")\n", "dm = gateway.getFacility(DataManagerFacility)\n", "user = gateway.getLoggedInUser()\n", "ctx = new SecurityContext(user.getGroupId())\n", "d = dm.createDataset(ctx, d, null)\n", "\n", "// Import the generated OME-TIFF to OMERO\n", "println \"importing...\"\n", "str2d = new String[1]\n", "str2d[0] = path_to_file\n", "success = upload_image(str2d, gateway, d.getId())\n", "println \"imported\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Close the connection and delete local file " ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Done\n" ] }, { "data": { "text/plain": [ "true" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Close the connection\n", "gateway.disconnect()\n", "println \"Done\"\n", "// delete the local OME-TIFF image\n", "file.delete()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Groovy", "language": "groovy", "name": "groovy" }, "language_info": { "codemirror_mode": "groovy", "file_extension": ".groovy", "mimetype": "", "name": "Groovy", "nbconverter_exporter": "", "version": "2.5.6" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": false, "sideBar": false, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": false, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }