.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/applications/plot_colocalization_metrics.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_applications_plot_colocalization_metrics.py: ====================== Colocalization metrics ====================== In this example, we demonstrate the use of different metrics to assess the colocalization of two different image channels. Colocalization can be split into two different concepts: 1. Co-occurence: What proportion of a substance is localized to a particular area? 2. Correlation: What is the relationship in intensity between two substances? .. GENERATED FROM PYTHON SOURCE LINES 17-28 Co-occurence: subcellular localization ====================================== Imagine that we are trying to determine the subcellular localization of a protein - is it located more in the nucleus or cytoplasm compared to a control? We begin by segmenting the nucleus of a sample image as described in another `example `_ and assume that whatever is not in the nucleus is in the cytoplasm. The protein, "protein A", will be simulated as blobs and segmented. .. GENERATED FROM PYTHON SOURCE LINES 28-83 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from matplotlib.colors import LinearSegmentedColormap from scipy import ndimage as ndi from skimage import data, filters, measure, segmentation rng = np.random.default_rng() # segment nucleus nucleus = data.protein_transport()[0, 0, :, :180] smooth = filters.gaussian(nucleus, sigma=1.5) thresh = smooth > filters.threshold_otsu(smooth) fill = ndi.binary_fill_holes(thresh) nucleus_seg = segmentation.clear_border(fill) # protein blobs of varying intensity proteinA = np.zeros_like(nucleus, dtype="float64") proteinA_seg = np.zeros_like(nucleus, dtype="float64") for blob_seed in range(10): blobs = data.binary_blobs( 180, blob_size_fraction=0.5, volume_fraction=(50 / (180**2)), rng=blob_seed ) blobs_image = filters.gaussian(blobs, sigma=1.5) * rng.integers(50, 256) proteinA += blobs_image proteinA_seg += blobs # plot data fig, ax = plt.subplots(3, 2, figsize=(8, 12), sharey=True) ax[0, 0].imshow(nucleus, cmap=plt.cm.gray) ax[0, 0].set_title('Nucleus') ax[0, 1].imshow(nucleus_seg, cmap=plt.cm.gray) ax[0, 1].set_title('Nucleus segmentation') black_magenta = LinearSegmentedColormap.from_list("", ["black", "magenta"]) ax[1, 0].imshow(proteinA, cmap=black_magenta) ax[1, 0].set_title('Protein A') ax[1, 1].imshow(proteinA_seg, cmap=black_magenta) ax[1, 1].set_title('Protein A segmentation') ax[2, 0].imshow(proteinA, cmap=black_magenta) ax[2, 0].imshow(nucleus_seg, cmap=plt.cm.gray, alpha=0.2) ax[2, 0].set_title('Protein A\nwith nucleus overlaid') ax[2, 1].imshow(proteinA_seg, cmap=black_magenta) ax[2, 1].imshow(nucleus_seg, cmap=plt.cm.gray, alpha=0.2) ax[2, 1].set_title('Protein A segmentation\nwith nucleus overlaid') for a in ax.ravel(): a.set_axis_off() .. image-sg:: /auto_examples/applications/images/sphx_glr_plot_colocalization_metrics_001.png :alt: Nucleus, Nucleus segmentation, Protein A, Protein A segmentation, Protein A with nucleus overlaid, Protein A segmentation with nucleus overlaid :srcset: /auto_examples/applications/images/sphx_glr_plot_colocalization_metrics_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 84-90 Intersection coefficient ======================== After segmenting both the nucleus and the protein of interest, we can determine what fraction of the protein A segmentation overlaps with the nucleus segmentation. .. GENERATED FROM PYTHON SOURCE LINES 90-93 .. code-block:: Python measure.intersection_coeff(proteinA_seg, nucleus_seg) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.22 .. GENERATED FROM PYTHON SOURCE LINES 94-109 Manders' Colocalization Coefficient (MCC) ========================================= The overlap coefficient assumes that the area of protein segmentation corresponds to the concentration of that protein - with larger areas indicating more protein. As the resolution of images are usually too small to make out individual proteins, they can clump together within one pixel, making the intensity of that pixel brighter. So, to better capture the protein concentration, we may choose to determine what proportion of the *intensity* of the protein channel is inside the nucleus. This metric is known as Manders' Colocalization Coefficient. In this image, while there are a lot of protein A spots within the nucleus they are dim compared to some of the spots outside the nucleus, so the MCC is much lower than the overlap coefficient. .. GENERATED FROM PYTHON SOURCE LINES 109-112 .. code-block:: Python measure.manders_coloc_coeff(proteinA, nucleus_seg) .. rst-class:: sphx-glr-script-out .. code-block:: none np.float64(0.23144805929657516) .. GENERATED FROM PYTHON SOURCE LINES 113-121 After choosing a co-occurence metric, we can apply the same process to control images. If no control images are available, the Costes method could be used to compare the MCC value of the original image with that of the randomly scrambled image. Information about this method is given in [1]_. .. [1] J. S. Aaron, A. B. Taylor and T.-L. Chew, Image co-localization – co-occurrence versus correlation. J Cell Sci 1 February 2018 131 (3): jcs211847. doi: https://doi.org/10.1242/jcs.211847 .. GENERATED FROM PYTHON SOURCE LINES 123-130 Correlation: association of two proteins ======================================== Now, imagine that we want to know how closely related two proteins are. First, we will generate protein B and plot intensities of the two proteins in every pixel to see the relationship between them. .. GENERATED FROM PYTHON SOURCE LINES 130-154 .. code-block:: Python # generating protein B data that is correlated to protein A for demo proteinB = proteinA + rng.normal(loc=100, scale=10, size=proteinA.shape) # plot images fig, ax = plt.subplots(1, 2, figsize=(8, 8), sharey=True) ax[0].imshow(proteinA, cmap=black_magenta) ax[0].set_title('Protein A') black_cyan = LinearSegmentedColormap.from_list("", ["black", "cyan"]) ax[1].imshow(proteinB, cmap=black_cyan) ax[1].set_title('Protein B') for a in ax.ravel(): a.set_axis_off() # plot pixel intensity scatter fig, ax = plt.subplots() ax.scatter(proteinA, proteinB) ax.set_title('Pixel intensity') ax.set_xlabel('Protein A intensity') ax.set_ylabel('Protein B intensity') .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/applications/images/sphx_glr_plot_colocalization_metrics_002.png :alt: Protein A, Protein B :srcset: /auto_examples/applications/images/sphx_glr_plot_colocalization_metrics_002.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/applications/images/sphx_glr_plot_colocalization_metrics_003.png :alt: Pixel intensity :srcset: /auto_examples/applications/images/sphx_glr_plot_colocalization_metrics_003.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(38.347222222222214, 0.5, 'Protein B intensity') .. GENERATED FROM PYTHON SOURCE LINES 155-157 The intensities look linearly correlated so Pearson's Correlation Coefficient would give us a good measure of how strong the association is. .. GENERATED FROM PYTHON SOURCE LINES 157-161 .. code-block:: Python pcc, pval = measure.pearson_corr_coeff(proteinA, proteinB) print(f"PCC: {pcc:0.3g}, p-val: {pval:0.3g}") .. rst-class:: sphx-glr-script-out .. code-block:: none PCC: 0.844, p-val: 0 .. GENERATED FROM PYTHON SOURCE LINES 162-167 Sometimes the intensities are correlated but not in a linear way. A rank-based correlation coefficient like `Spearman's `_ might give a more accurate measure of the non-linear relationship in that case. .. GENERATED FROM PYTHON SOURCE LINES 167-168 .. code-block:: Python plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.210 seconds) .. _sphx_glr_download_auto_examples_applications_plot_colocalization_metrics.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/scikit-image/scikit-image/v0.24.0?filepath=notebooks/auto_examples/applications/plot_colocalization_metrics.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_colocalization_metrics.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_colocalization_metrics.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_