{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Analyze particles\n", "\n", "This notebook demonstrates how to analyze a batch of images associated to the paper ['Subdiffraction imaging of centrosomes reveals higher-order organizational features of pericentriolar material.'](http://dx.doi.org/10.1038/ncb2591) using the scripting facility available in [Fiji](https://fiji.sc/)\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 analyse an image using Fiji" ] }, { "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": "20423dca-961e-4e3f-b1a8-e4cc52d1934f", "version_major": 2, "version_minor": 0 }, "method": "display_data" }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "a8ba9369-4fda-48f0-a61f-eac62349f0e7", "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": [ "### Connection information " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "public" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "//Connection information\n", "HOST = \"wss://idr.openmicroscopy.org/omero-ws\"\n", "USERNAME = \"public\"\n", "PASSWORD = \"public\"" ] }, { "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(host, user, password) {\n", " \"Connect to OMERO\"\n", "\n", " credentials = new LoginCredentials()\n", " credentials.getServer().setHostname(host)\n", " credentials.getUser().setUsername(user.trim())\n", " credentials.getUser().setPassword(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(HOST, USERNAME, PASSWORD)\n", "println \"connected...\"\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Select an image . Do not re-run this cell.\n", "\n", "Select an image from the [idr0021 study](https://idr.openmicroscopy.org/search/?query=Name:idr0021)." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "scrolled": true }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "25142ad6-854b-454d-92b8-d14ed88c01a2", "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\").onInit({ga['ImageID'] = \"1884807\"})\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": [ "//Image to analyze\n", "image_id = ga['ImageID'].toLong()\n", "group_id = \"-1\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Open the image using Bio-Formats" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "opening Image...\n" ] }, { "data": { "text/plain": [ "loci.plugins.LociImporter@5960cad2" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import ij.IJ\n", "import ij.plugin.frame.RoiManager\n", "\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))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Analyze the image\n", "\n", "We run [fiji-macro-segment.ijm](../scripts/fiji-macro-segment.ijm) on the image.\n", "\n", "The macro file will:\n", " - convert the image into an 8-bit image\n", " - set the autothreshold to MaxEntropy\n", " - run the Analyze Particles plugin" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " \tArea\tMean\tMin\tMax\n", "1\t0.154\t144.031\t22\t188\n", "2\t0.016\t175.300\t162\t186\n", "3\t0.050\t153.188\t95\t189\n", "4\t0.121\t126.701\t0\t189\n", "5\t0.036\t164.870\t128\t189\n", "6\t0.240\t121.431\t0\t189\n", "7\t0.176\t142.321\t15\t189\n", "8\t0.017\t180.364\t171\t189\n", "9\t0.031\t137.600\t57\t189\n", "10\t0.019\t174.250\t153\t189\n", "11\t0.019\t167.917\t144\t187\n", "12\t0.025\t168.500\t148\t188\n", "13\t0.017\t167.091\t142\t187\n", "14\t0.315\t143.085\t0\t189\n", "15\t0.198\t144.730\t49\t189\n", "16\t0.049\t169.645\t147\t187\n", "17\t0.022\t164.500\t140\t183\n", "Analysis completed\n" ] }, { "data": { "text/plain": [ "null" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import ij.IJ\n", "import ij.plugin.frame.RoiManager\n", "\n", "imp = IJ.getImage()\n", "RoiManager.getRoiManager()\n", "IJ.runMacroFile(\"../scripts/fiji-macro-segment.ijm\")\n", "println \"Analysis completed\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Automate\n", "\n", "We now run the macro on all the images in the study. This will take several minutes.\n", "This is mainly to show how to iterate over all the images in a study, you can also have a look at [idr0021.groovy](../scripts/idr0021.groovy)." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "51" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "project_id = 51" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Analyze the images" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running macro on image: 1884856\n", " \tArea\tMean\tMin\tMax\n", "1\t0.027\t128.588\t43\t184\n", "2\t0.020\t125.692\t51\t181\n", "3\t0.016\t148.300\t110\t178\n", "4\t0.017\t143.636\t96\t184\n", "5\t0.022\t130.357\t69\t178\n", "6\t0.030\t130.368\t41\t181\n", "7\t0.017\t157.091\t125\t181\n", "8\t0.449\t115.531\t0\t184\n", "9\t0.016\t134.000\t83\t181\n", "10\t0.016\t136.400\t93\t172\n", "11\t0.016\t146.800\t110\t184\n", "12\t0.025\t130.188\t51\t183\n", "13\t0.019\t134.417\t72\t183\n", "14\t0.019\t126.917\t70\t171\n", "15\t0.022\t149.143\t103\t178\n", "16\t0.020\t138.385\t76\t183\n", "17\t0.030\t104.789\t0\t181\n", "18\t0.016\t147.300\t111\t178\n", "19\t0.027\t149.059\t100\t183\n", "20\t0.025\t161.312\t128\t184\n", "21\t0.017\t160.455\t134\t181\n", "22\t0.047\t138.733\t77\t183\n", "23\t0.023\t128.333\t62\t173\n", "24\t0.016\t160.500\t130\t184\n", "Running macro on image: 1884857\n", " \tArea\tMean\tMin\tMax\n", "1\t0.208\t154.714\t0\t201\n", "2\t0.237\t149.822\t31\t201\n", "3\t0.020\t158.769\t113\t187\n", "4\t0.020\t166.769\t130\t201\n", "5\t0.020\t169.077\t131\t194\n", "6\t0.023\t183.533\t156\t200\n", "7\t0.066\t160.000\t127\t201\n", "8\t0.044\t120.286\t0\t200\n", "9\t0.031\t184.850\t154\t201\n", "10\t0.036\t163.522\t97\t198\n", "11\t0.051\t166.333\t120\t200\n", "12\t0.022\t156.643\t105\t199\n", "Running macro on image: 1884860\n", " \tArea\tMean\tMin\tMax\n", "1\t0.017\t176.636\t144\t200\n", "2\t0.276\t122.497\t0\t199\n", "3\t0.245\t136.745\t9\t199\n", "4\t0.022\t156.000\t96\t197\n", "5\t0.019\t171.750\t129\t199\n", "6\t0.017\t157.455\t109\t194\n", "7\t0.016\t177.900\t152\t196\n", "8\t0.020\t162.308\t116\t197\n", "9\t0.019\t176.167\t145\t196\n", "10\t0.048\t169.677\t121\t200\n", "11\t0.027\t175.706\t151\t197\n", "12\t0.016\t170.100\t138\t196\n", "13\t0.017\t179.182\t153\t199\n", "14\t0.017\t162.273\t121\t193\n", "15\t0.019\t168.167\t143\t189\n", "16\t0.089\t122.298\t25\t200\n", "17\t0.023\t160.933\t108\t196\n", "18\t0.027\t116.353\t28\t194\n", "19\t0.072\t128.109\t14\t199\n", "20\t0.017\t159.455\t114\t197\n", "21\t0.025\t177.812\t140\t199\n", "22\t0.019\t159.000\t110\t200\n", "23\t0.056\t178.556\t127\t199\n", "24\t0.028\t116.167\t20\t195\n", "25\t0.019\t163.667\t122\t197\n", "26\t0.037\t113.625\t0\t195\n", "27\t0.023\t160.200\t101\t200\n", "28\t0.020\t165.000\t118\t194\n", "29\t0.020\t161.538\t111\t196\n", "30\t0.016\t153.800\t109\t195\n", "31\t0.039\t146.440\t84\t196\n", "32\t0.016\t182.500\t166\t196\n", "33\t0.053\t144.676\t74\t200\n", "34\t0.017\t174.818\t144\t196\n", "35\t0.084\t153.222\t95\t200\n", "36\t0.025\t139.125\t60\t200\n", "37\t0.033\t179.571\t139\t198\n", "38\t0.020\t154.154\t96\t193\n", "39\t0.017\t160.636\t119\t192\n", "40\t0.016\t168.400\t133\t200\n", "41\t0.020\t147.538\t84\t197\n", "42\t0.016\t181.700\t158\t199\n", "Running macro on image: 1884843\n", " \tArea\tMean\tMin\tMax\n", "1\t0.243\t136.832\t14\t213\n", "2\t0.205\t119.206\t0\t213\n", "3\t0.017\t188.545\t160\t212\n", "4\t0.028\t198.056\t174\t214\n", "5\t0.016\t185.400\t156\t211\n", "6\t0.019\t168.833\t120\t207\n", "7\t0.016\t193.200\t172\t212\n", "8\t0.033\t189.524\t135\t214\n", "9\t0.016\t193.700\t170\t214\n", "10\t0.020\t185.154\t148\t214\n", "11\t0.017\t194.273\t169\t212\n", "12\t0.020\t184.846\t148\t211\n", "13\t0.028\t190.056\t147\t214\n", "14\t0.022\t193.929\t166\t208\n", "15\t0.033\t195.048\t156\t214\n", "16\t0.016\t193.000\t168\t213\n", "17\t0.016\t185.300\t156\t214\n", "18\t0.022\t185.429\t150\t213\n", "19\t0.022\t180.643\t137\t210\n", "20\t0.016\t192.600\t168\t214\n", "21\t0.019\t189.667\t163\t208\n", "22\t0.019\t195.167\t168\t214\n", "23\t0.039\t190.720\t144\t214\n", "24\t0.024\t197.600\t180\t212\n", "25\t0.025\t197.000\t173\t213\n", "26\t0.020\t188.769\t158\t211\n", "27\t0.028\t188.778\t155\t213\n", "28\t0.027\t191.882\t159\t213\n", "29\t0.020\t186.462\t154\t214\n", "30\t0.129\t152.207\t0\t212\n", "31\t0.016\t195.800\t178\t214\n", "32\t0.041\t155.808\t59\t212\n", "33\t0.056\t169.639\t127\t214\n", "34\t0.016\t190.600\t166\t213\n", "35\t0.017\t196.818\t174\t214\n", "36\t0.020\t183.462\t144\t214\n", "37\t0.017\t190.636\t158\t214\n", "38\t0.016\t193.700\t170\t214\n", "39\t0.019\t194.667\t169\t213\n", "40\t0.017\t185.636\t153\t210\n", "41\t0.019\t193.583\t170\t209\n", "42\t0.016\t189.900\t166\t211\n", "Running macro on image: 1884845\n", " \tArea\tMean\tMin\tMax\n", "1\t0.191\t143.148\t0\t214\n", "2\t0.143\t141.714\t19\t212\n", "3\t0.017\t197.909\t180\t214\n", "4\t0.016\t200.400\t183\t212\n", "5\t0.016\t194.400\t172\t213\n", "6\t0.020\t187.615\t152\t211\n", "7\t0.042\t174.852\t97\t213\n", "8\t0.038\t129.625\t0\t213\n", "9\t0.041\t149.500\t38\t214\n", "10\t0.016\t191.900\t166\t211\n", "11\t0.016\t190.000\t161\t212\n", "Running macro on image: 1884851\n", " \tArea\tMean\tMin\tMax\n", "1\t0.025\t127.250\t43\t186\n", "2\t0.017\t141.909\t80\t191\n", "3\t0.223\t132.979\t0\t193\n", "4\t0.172\t152.036\t70\t192\n", "5\t0.027\t117.588\t11\t179\n", "6\t0.030\t132.526\t30\t190\n", "7\t0.022\t147.786\t81\t192\n", "8\t0.019\t163.167\t127\t188\n", "9\t0.017\t173.818\t149\t193\n", "10\t0.016\t152.100\t107\t191\n", "11\t0.034\t128.909\t44\t190\n", "12\t0.053\t137.324\t68\t192\n", "13\t0.016\t159.000\t122\t185\n", "14\t0.016\t180.600\t158\t193\n", "15\t0.020\t153.231\t102\t186\n", "16\t0.028\t117.056\t0\t189\n", "Running macro on image: 1884841\n", " \tArea\tMean\tMin\tMax\n", "1\t0.016\t202\t179\t221\n", "2\t0.251\t140.419\t42\t221\n", "3\t0.276\t155.335\t0\t220\n", "4\t0.028\t208.278\t196\t221\n", "5\t0.022\t206.429\t187\t221\n", "6\t0.022\t209.571\t198\t220\n", "7\t0.071\t169.800\t79\t221\n", "8\t0.044\t140.393\t0\t221\n", "9\t0.050\t189.844\t113\t221\n", "10\t0.044\t154.929\t44\t221\n", "Running macro on image: 1884849\n" ] } ], "source": [ "import omero.gateway.SecurityContext\n", "import omero.gateway.facility.BrowseFacility\n", "import ij.IJ\n", "\n", "//helper functions\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", "//Load the datasets\n", "def get_datasets(gateway, ctx, project_id) {\n", " \"List all datasets contained in a Project\"\n", "\n", " browse = gateway.getFacility(BrowseFacility)\n", " ids = new ArrayList(1)\n", " ids.add(new Long(project_id))\n", " projects = browse.getProjects(ctx, ids)\n", " return projects[0].getDatasets()\n", "}\n", "\n", "//Load the images\n", "def get_images(gateway, ctx, dataset_id) {\n", " \"List all images contained in a Dataset\"\n", "\n", " browse = gateway.getFacility(BrowseFacility)\n", " ids = new ArrayList(1)\n", " ids.add(new Long(dataset_id))\n", " return browse.getImagesForDatasets(ctx, ids)\n", "}\n", "\n", "user = gateway.getLoggedInUser()\n", "group_id = user.getGroupId()\n", "ctx = new SecurityContext(group_id)\n", "\n", "datasets = get_datasets(gateway, ctx, project_id)\n", "\n", "//Close windows before starting\n", "IJ.run(\"Close All\")\n", "datasets.each() { d ->\n", " dataset_name = d.getName()\n", " // for each dataset load the images\n", " // get all images_ids in the dataset\n", " images = get_images(gateway, ctx, d.getId())\n", " images.each() { image ->\n", " if (image.getName().endsWith(\".tif\")) {\n", " return\n", " }\n", " id = image.getId()\n", " println \"Running macro on image: \"+id\n", " open_image_plus(HOST, USERNAME, PASSWORD, group_id, id)\n", " imp = IJ.getImage()\n", " IJ.runMacroFile(\"../scripts/fiji-macro-segment.ijm\")\n", " }\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Close the connection" ] }, { "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\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### License (BSD 2-Clause)\n", "Copyright (c) 2021, University of Dundee All rights reserved.\n", "\n", "Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:\n", "\n", "Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. " ] } ], "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 }