{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Cell/particle Counting and scoring stained objects using CellProfiler" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook demonstrates how to process plates associated to the paper ['Integration of biological data by kernels on graph nodes allows prediction of new genes involved in mitotic chromosome condensation.'](http://dx.doi.org/10.1091/mbc.E13-04-0221) using [CellProfiler](http://cellprofiler.org/).\n", "We use the example pipeline [Cell/particle counting, and scoring the percentage of stained objects](http://cellprofiler.org/examples/#PercentPositive). This pipeline is for two channels images only." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Import Packages" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# %tb\n", "\n", "# Import Cell Profiler Dependencies\n", "import cellprofiler\n", "import cellprofiler.preferences as cpprefs\n", "import cellprofiler.module as cpm\n", "import cellprofiler.pipeline as cpp\n", "cpprefs.set_headless()\n", "\n", "# Inject Image module used to inject OMERO image planes into Cell Profiler Pipeline\n", "from cellprofiler.modules.injectimage import InjectImage\n", "\n", "# Import Numpy\n", "import numpy as np\n", "\n", "# Import Python System Packages\n", "import os\n", "import tempfile\n", "import pandas\n", "import warnings\n", "\n", "# Import Matplotlib\n", "import matplotlib\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Set Cell Output Directory" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "new_output_directory = os.path.normcase(tempfile.mkdtemp())\n", "cpprefs.set_default_output_directory(new_output_directory)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create connection to IDR (Public Database : Read-only!)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from idr import connection; \n", "conn = connection()\n", "print conn" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Fetch the Plate that contains the Images to be analysed" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plate_id = 422\n", "plate = conn.getObject(\"Plate\", plate_id)\n", "print 'Plate Name: ', plate.getName()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Load pipeline and inspect modules\n", "from os.path import expanduser\n", "home = expanduser(\"~\")\n", "pipeline = cpp.Pipeline()\n", "pipeline.load(home+\"/notebooks/includes/ExamplePercentPositive.cppipe\")\n", "\n", "# Remove first 4 modules: Images, Metadata, NamesAndTypes, Groups...\n", "# (replaced by InjectImage module below)\n", "for i in range(4):\n", " print 'Remove module: ', pipeline.modules()[0].module_name\n", " pipeline.remove_module(1)\n", "\n", "print 'Pipeline modules:'\n", "for module in pipeline.modules():\n", " print module.module_num, module.module_name" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Run Cell Profiler Pipeline on a Plate" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "warnings.filterwarnings('ignore')\n", "\n", "Nuclei = pandas.DataFrame()\n", "files = list()\n", "\n", "# create list from generator\n", "wells = list(plate.listChildren())\n", "well_count = len(wells)\n", "\n", "# Set the limit to a lower value e.g. 10 wells to speed up the process.\n", "limit = well_count\n", "for count, well in zip(range(limit), wells):\n", " print 'Well: %s/%s' % (count + 1, well_count), 'row:', well.row, 'column:', well.column\n", " # Load a single Image per Well\n", " image = well.getImage(0)\n", " pixels = image.getPrimaryPixels()\n", " size_c = image.getSizeC()\n", " \n", " # For each Image in IDR, we copy the pipeline and inject image modules\n", " pipeline_copy = pipeline.copy()\n", " \n", " # Inject image for each Channel (pipeline only handles 2 channels)\n", " for c in range(0, size_c):\n", " \n", " plane = pixels.getPlane(0, c, 0)\n", " image_name = image.getName()\n", " \n", " # Name of the channel expected in the pipeline\n", " if c == 0:\n", " image_name = 'OrigBlue'\n", " if c == 1:\n", " image_name = 'OrigGreen'\n", " \n", " inject_image_module = InjectImage(image_name, plane)\n", " inject_image_module.set_module_num(1)\n", " pipeline_copy.add_module(inject_image_module)\n", " \n", " m = pipeline_copy.run()\n", " \n", " #Results obtained as CSV from Cell Profiler\n", " path = new_output_directory + '/Nuclei.csv'\n", " f = pandas.read_csv(path, index_col=None, header=0)\n", " f['Image'] = image.getId()\n", " f['Well'] = well.getId()\n", " f['Cell_Count'] = len(f.index)\n", " files.append(f)\n", "\n", "Nuclei = pandas.concat(files, ignore_index=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Calculate statistics" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Nuclei.describe()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "matplotlib.rcParams['figure.figsize'] = (32.0, 30.0)\n", "#Drop few columns\n", "df = Nuclei.drop(['Image', 'ImageNumber', 'Well', 'ObjectNumber', 'Number_Object_Number', 'Classify_PH3Neg', 'Classify_PH3Pos'], axis=1)\n", "df.hist();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "### Close the connection" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "conn.close()" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "### License\n", "\n", "Copyright (C) 2018 University of Dundee. All Rights Reserved.\n", "\n", "This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.\n", "\n", "This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA." ] } ], "metadata": { "kernelspec": { "display_name": "OMERO Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.14" } }, "nbformat": 4, "nbformat_minor": 2 }