Skip to content

Commit

Permalink
Merge pull request #21 from bioshape-analysis/csoub
Browse files Browse the repository at this point in the history
Csoub
  • Loading branch information
clementsoubrier authored Sep 17, 2024
2 parents df2e55d + a2ea057 commit a9339c4
Show file tree
Hide file tree
Showing 17 changed files with 144 additions and 13 deletions.
4 changes: 2 additions & 2 deletions _freeze/posts/AFM-data/index/execute-results/html.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"hash": "10d851187eb2e992125f935bc965f1d2",
"hash": "8bc5f558cca4dc007297cd54321dee9b",
"result": {
"engine": "jupyter",
"markdown": "---\ntitle: \"Extracting cell geometry from Atomic Force Microscopy\"\nsubtitle: \"Part 1: Static analysis\"\n\n\nauthor:\n - name: \"Clément Soubrier\" \n email: \"[email protected]\"\n affiliations:\n - name: KDD Group\n url: \"https://rtviii.xyz/\"\n\n - name: \"Khanh Dao Duc\" \n email: \"[email protected]\"\n affiliations:\n - name: Department of Mathematics, UBC\n url: \"https://www.math.ubc.ca/\"\n - name: Department of Computer Science, UBC\n url: \"https://www.cs.ubc.ca/\"\n\ndate: \"July 31 2024\"\ncategories: [biology, bioinformatics] \n\ncallout-icon: false\nformat:\n html:\n code-fold: true\nbibliography: bibliography.bib\n\nexecute:\n echo: false\n freeze: auto\n\n---\n\n::: {#44521d56 .cell execution_count=1}\n\n::: {.cell-output .cell-output-stdout}\n```\n1\n```\n:::\n:::\n\n\nWe present here the protocole to process biological images such as bacteria atomic force miroscopy data. We want to study the bacteria cell shape and extract the main geometrical feature.\n\n## Biological context\n*Mycobacterium smegmatis* is Grahm-positive rod shape bacterium. It is 3 to 5 $\\mu m$ long and around 500 $nm$ wide. This non-pathogenic species is otften used a biological model to study the pathogenic Mycobacteria such as *M.tuberculosis* (responsible for the tubercuosis) or *M.abscessus*, with which it shares the same cell wall structure[@tyagi2002mycobacterium] . In particular *M.smegmatis* has a fast growth (3-4 hours doubling time compared to 24h for *M. tuberculosis*), allowing for faster experimental protocols. \n\n\nHere are some know properties of *M.smegmatis* bacteria :\n\n* They present variation of cell diameter along their longitudinal axis [@eskandarian2017division]. The cell diameter is represented as a height profile along the cell centerline. We respectively name peaks and troughs the local maxima and minima of this proile.\n\n![3D image of *M.smegmatis*. The orange line represents the height profile.](3D_cell.png)\n\n* They grow following a biphasic and asymetrical polar dynamics[@hannebelle2020biphasic]. The cells elongate from the poles, where material is added. After division, the pre-existing pole (OP) elongate at a high rate, whereas the newly created pole (NP) has first a slow growth, and then switches to a fast growth, after the New End Take Off (NETO).\n\n![Growth dynamics.](Neto.svg)\n\n\n\n\n\n\n## Raw image pre-processing\n#### Data\n\nSeveral data acquisitions were conducted with wild types and different mutant strains. The raw data is composed of AFM log files times series for each experiments. Each log file contain several images, each one representing a physical channel such as height, stiffness, adhesion etc.\n\n## Segmentation\n\n\n## Centerline\n\n",
"markdown": "---\ntitle: \"Extracting cell geometry from Atomic Force Microscopy\"\nsubtitle: \"Part 1: Static analysis\"\njupyter: python3\n\nauthor:\n - name: \"Clément Soubrier\" \n email: \"[email protected]\"\n affiliations:\n - name: KDD Group\n url: \"https://rtviii.xyz/\"\n\n - name: \"Khanh Dao Duc\" \n email: \"[email protected]\"\n affiliations:\n - name: Department of Mathematics, UBC\n url: \"https://www.math.ubc.ca/\"\n - name: Department of Computer Science, UBC\n url: \"https://www.cs.ubc.ca/\"\n\ndate: \"July 31 2024\"\ncategories: [biology, bioinformatics] \n\ncallout-icon: false\nformat:\n html:\n code-fold: true\nbibliography: bibliography.bib\n\nexecute:\n echo: true\n freeze: auto\n warning: false\n\n---\n\n\n\n\n\n\n\nWe present here the protocole to process biological images such as bacteria atomic force miroscopy data. We want to study the bacteria cell shape and extract the main geometrical feature.\n\n## Biological context\n*Mycobacterium smegmatis* is Grahm-positive rod shape bacterium. It is 3 to 5 $\\mu m$ long and around 500 $nm$ wide. This non-pathogenic species is otften used a biological model to study the pathogenic Mycobacteria such as *M.tuberculosis* (responsible for the tubercuosis) or *M.abscessus*, with which it shares the same cell wall structure [@tyagi2002mycobacterium]. In particular *M.smegmatis* has a fast growth (3-4 hours doubling time compared to 24h for *M. tuberculosis*), allowing for faster experimental protocols. \n\n\nHere are some know properties of *M.smegmatis* bacteria :\n\n* They present variation of cell diameter along their longitudinal axis [@eskandarian2017division]. The cell diameter is represented as a height profile along the cell centerline. We respectively name peaks and troughs the local maxima and minima of this profile.\n\n![3D image of *M.smegmatis*. The orange line represents the height profile.](3D_cell.png)\n\n* They grow following a biphasic and asymetrical polar dynamics [@hannebelle2020biphasic]. The cells elongate from the poles, where material is added. After division, the pre-existing pole (OP) elongate at a high rate, whereas the newly created pole (NP) has first a slow growth, and then switches to a fast growth, after the New End Take Off (NETO).\n\n![Growth dynamics.](Neto.svg)\n\n\n\n\n\n\n## Raw image pre-processing\n#### Data\n\nSeveral data acquisitions were conducted with wild types and different mutant strains. The raw data is composed of AFM log files times series for each experiments. Each log file contain several images, each one representing a physical channel such as height, stiffness, adhesion etc. After extraction of the data, forward and backward cells are aligned, artefacts such as image scars are detected and corrected.\n\n![At each time step, images representing different physical variables are produced by the AFM](Figure_1_quarto.png)\n\n## Segmentation\nAt each time steps, images are segmented to detect each cells using the cellpose package [@stringer2021cellpose]. If available, different physical channels are combined to improve the segmentation. Forward and backward images are also combined.\n\n![Images are combined to improve the segmentation](Figure_2_quarto.png)\n\nHere is an example on how to use cellpose on an image. Different models are available (with the `seg_mod` variable), depending on the training datasets. With cellpose 3, different denoising models are also available (with the `denoise_mod` variable). \n\n::: {#2ca55a5e .cell execution_count=1}\n``` {.python .cell-code}\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom cellpose import io, denoise, plot\nfrom PIL import Image\n\n\n'''\nParameters\n'''\n\nimage_path = 'raw_img.png'\npath_to_save = 'segmented_img'\n# Segmentation model type\nseg_mod = 'cyto' \n# Denoizing model\ndenoise_mod = \"denoise_cyto3\" \n# Expected cell diameter (pixels)\ndia = 40\n# Type of segmentation (with / without nuclei, different color channels or not)\nchan = [0,0] \n# Segmentation sensibility parameters\nthres = 0.8\ncelp = 0.4\n\n'''\nComputing segmentation\n'''\n\n\n# Opening image to segment\nimg=np.array(Image.open(image_path))[:,:,1]\n\n# Chosing a model type :\nmodel = denoise.CellposeDenoiseModel(gpu=False, model_type=seg_mod, restore_type=denoise_mod)\n\n# Computing segmentaion\nmasks, flows, st, diams = model.eval(img, diameter = dia, channels=chan, flow_threshold = thres, cellprob_threshold=celp)\n\n\n# Saving the results into a numpy file\nio.masks_flows_to_seg(img, masks, flows, path_to_save, channels=chan, diams=diams)\n\n```\n:::\n\n\nWe plot the final results :\n\n::: {#23f7d201 .cell execution_count=2}\n``` {.python .cell-code}\nplt.imshow(img,cmap='gray')\nplt.show()\n```\n\n::: {.cell-output .cell-output-display}\n![Raw image](index_files/figure-html/cell-3-output-1.png){width=575 height=307}\n:::\n:::\n\n\n::: {#b441ded7 .cell execution_count=3}\n``` {.python .cell-code}\nmask_RGB = plot.mask_overlay(img,masks)\nplt.imshow(mask_RGB)\nplt.show()\n```\n\n::: {.cell-output .cell-output-display}\n![Image with segmented masks overlaid](index_files/figure-html/cell-4-output-1.png){width=575 height=307}\n:::\n:::\n\n\n## Centerline\nSince we are interested in studying the variations of the cell diameter, we define height profile as the value of the cell height along the cell centerline. The cell centerline are computed using a skeletonization algorithm [@zhang1984fast,@lee1994building]. Here is an example of skeletonization \n\n::: {#fcc052f8 .cell execution_count=4}\n``` {.python .cell-code}\nfrom skimage.morphology import skeletonize\n\n# Selecting first mask\nfirst_mask = masks == 1\n\nskel_img = skeletonize(first_mask, method='lee') \nskel = np.argwhere(skel_img)\nplt.imshow(first_mask, cmap='gray')\n\nplt.scatter(skel[:,1], skel[:,0], 0.5*np.ones(np.shape(skel[:,0])), color='r', marker='.')\nplt.show()\n```\n\n::: {.cell-output .cell-output-display}\n![](index_files/figure-html/cell-5-output-1.png){width=575 height=307}\n:::\n:::\n\n\nDepending on the masks shapes, centerlines may have branches :\n\n::: {#1eb253d9 .cell execution_count=5}\n``` {.python .cell-code}\nfrom skimage.morphology import skeletonize\n\n# Selecting first mask\nfirst_mask = masks == 3\n\nskel_img = skeletonize(first_mask) #, method='lee'\nskel = np.argwhere(skel_img)\nplt.imshow(first_mask, cmap='gray')\n\nplt.scatter(skel[:,1], skel[:,0], 0.5*np.ones(np.shape(skel[:,0])), color='r', marker='.')\nplt.show()\n```\n\n::: {.cell-output .cell-output-display}\n![](index_files/figure-html/cell-6-output-1.png){width=575 height=307}\n:::\n:::\n\n\nIn practice, centerlines are pruned and extended to the cell poles, in order to capture the cell length. Other geometrical properties such as masks centroids or outlines are computed as well.\n\n![Final static processing results in real life data. White masks are excluded from the cell tracking algorithm (see part 2). Black dots are cell centroids. The yellow boxes represent artefacts cleaning.](Figure_3_quarto.png)\n\n# References\n\n",
"supporting": [
"index_files"
],
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added posts/AFM-data/Figure_1_quarto.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added posts/AFM-data/Figure_2_quarto.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added posts/AFM-data/Figure_3_quarto.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 24 additions & 1 deletion posts/AFM-data/bibliography.bib
Original file line number Diff line number Diff line change
Expand Up @@ -280,4 +280,27 @@ @article{cohen2013microbial
pages={632--642},
year={2013},
publisher={Elsevier}
}
}

@article{lee1994building,
title={Building skeleton models via 3-D medial surface axis thinning algorithms},
author={Lee, Ta-Chih and Kashyap, Rangasami L and Chu, Chong-Nam},
journal={CVGIP: graphical models and image processing},
volume={56},
number={6},
pages={462--478},
year={1994},
publisher={Elsevier}
}

@article{zhang1984fast,
title={A fast parallel algorithm for thinning digital patterns},
author={Zhang, Tongjie Y and Suen, Ching Y.},
journal={Communications of the ACM},
volume={27},
number={3},
pages={236--239},
year={1984},
publisher={ACM New York, NY, USA}
}

128 changes: 118 additions & 10 deletions posts/AFM-data/index.qmd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "Extracting cell geometry from Atomic Force Microscopy"
subtitle: "Part 1: Static analysis"

jupyter: python3

author:
- name: "Clément Soubrier"
Expand All @@ -28,31 +28,28 @@ format:
bibliography: bibliography.bib

execute:
echo: false
echo: true
freeze: auto
warning: false

---


```{python}
import pandas as pd
print(1)
```


We present here the protocole to process biological images such as bacteria atomic force miroscopy data. We want to study the bacteria cell shape and extract the main geometrical feature.

## Biological context
*Mycobacterium smegmatis* is Grahm-positive rod shape bacterium. It is 3 to 5 $\mu m$ long and around 500 $nm$ wide. This non-pathogenic species is otften used a biological model to study the pathogenic Mycobacteria such as *M.tuberculosis* (responsible for the tubercuosis) or *M.abscessus*, with which it shares the same cell wall structure[@tyagi2002mycobacterium] . In particular *M.smegmatis* has a fast growth (3-4 hours doubling time compared to 24h for *M. tuberculosis*), allowing for faster experimental protocols.
*Mycobacterium smegmatis* is Grahm-positive rod shape bacterium. It is 3 to 5 $\mu m$ long and around 500 $nm$ wide. This non-pathogenic species is otften used a biological model to study the pathogenic Mycobacteria such as *M.tuberculosis* (responsible for the tubercuosis) or *M.abscessus*, with which it shares the same cell wall structure [@tyagi2002mycobacterium]. In particular *M.smegmatis* has a fast growth (3-4 hours doubling time compared to 24h for *M. tuberculosis*), allowing for faster experimental protocols.


Here are some know properties of *M.smegmatis* bacteria :

* They present variation of cell diameter along their longitudinal axis [@eskandarian2017division]. The cell diameter is represented as a height profile along the cell centerline. We respectively name peaks and troughs the local maxima and minima of this proile.
* They present variation of cell diameter along their longitudinal axis [@eskandarian2017division]. The cell diameter is represented as a height profile along the cell centerline. We respectively name peaks and troughs the local maxima and minima of this profile.

![3D image of *M.smegmatis*. The orange line represents the height profile.](3D_cell.png)

* They grow following a biphasic and asymetrical polar dynamics[@hannebelle2020biphasic]. The cells elongate from the poles, where material is added. After division, the pre-existing pole (OP) elongate at a high rate, whereas the newly created pole (NP) has first a slow growth, and then switches to a fast growth, after the New End Take Off (NETO).
* They grow following a biphasic and asymetrical polar dynamics [@hannebelle2020biphasic]. The cells elongate from the poles, where material is added. After division, the pre-existing pole (OP) elongate at a high rate, whereas the newly created pole (NP) has first a slow growth, and then switches to a fast growth, after the New End Take Off (NETO).

![Growth dynamics.](Neto.svg)

Expand All @@ -64,9 +61,120 @@ Here are some know properties of *M.smegmatis* bacteria :
## Raw image pre-processing
#### Data

Several data acquisitions were conducted with wild types and different mutant strains. The raw data is composed of AFM log files times series for each experiments. Each log file contain several images, each one representing a physical channel such as height, stiffness, adhesion etc.
Several data acquisitions were conducted with wild types and different mutant strains. The raw data is composed of AFM log files times series for each experiments. Each log file contain several images, each one representing a physical channel such as height, stiffness, adhesion etc. After extraction of the data, forward and backward cells are aligned, artefacts such as image scars are detected and corrected.

![At each time step, images representing different physical variables are produced by the AFM](Figure_1_quarto.png)

## Segmentation
At each time steps, images are segmented to detect each cells using the cellpose package [@stringer2021cellpose]. If available, different physical channels are combined to improve the segmentation. Forward and backward images are also combined.

![Images are combined to improve the segmentation](Figure_2_quarto.png)

Here is an example on how to use cellpose on an image. Different models are available (with the `seg_mod` variable), depending on the training datasets. With cellpose 3, different denoising models are also available (with the `denoise_mod` variable).

```{python}
import numpy as np
import matplotlib.pyplot as plt
from cellpose import io, denoise, plot
from PIL import Image
'''
Parameters
'''
image_path = 'raw_img.png'
path_to_save = 'segmented_img'
# Segmentation model type
seg_mod = 'cyto'
# Denoizing model
denoise_mod = "denoise_cyto3"
# Expected cell diameter (pixels)
dia = 40
# Type of segmentation (with / without nuclei, different color channels or not)
chan = [0,0]
# Segmentation sensibility parameters
thres = 0.8
celp = 0.4
'''
Computing segmentation
'''
# Opening image to segment
img=np.array(Image.open(image_path))[:,:,1]
# Chosing a model type :
model = denoise.CellposeDenoiseModel(gpu=False, model_type=seg_mod, restore_type=denoise_mod)
# Computing segmentaion
masks, flows, st, diams = model.eval(img, diameter = dia, channels=chan, flow_threshold = thres, cellprob_threshold=celp)
# Saving the results into a numpy file
io.masks_flows_to_seg(img, masks, flows, path_to_save, channels=chan, diams=diams)
```

We plot the final results :

```{python}
#| fig-cap: "Raw image"
plt.imshow(img,cmap='gray')
plt.show()
```

```{python}
#| fig-cap: "Image with segmented masks overlaid"
mask_RGB = plot.mask_overlay(img,masks)
plt.imshow(mask_RGB)
plt.show()
```

## Centerline
Since we are interested in studying the variations of the cell diameter, we define height profile as the value of the cell height along the cell centerline. The cell centerline are computed using a skeletonization algorithm [@zhang1984fast,@lee1994building]. Here is an example of skeletonization

```{python}
from skimage.morphology import skeletonize
# Selecting first mask
first_mask = masks == 1
skel_img = skeletonize(first_mask, method='lee')
skel = np.argwhere(skel_img)
plt.imshow(first_mask, cmap='gray')
plt.scatter(skel[:,1], skel[:,0], 0.5*np.ones(np.shape(skel[:,0])), color='r', marker='.')
plt.show()
```

Depending on the masks shapes, centerlines may have branches :

```{python}
from skimage.morphology import skeletonize
# Selecting first mask
first_mask = masks == 3
skel_img = skeletonize(first_mask) #, method='lee'
skel = np.argwhere(skel_img)
plt.imshow(first_mask, cmap='gray')
plt.scatter(skel[:,1], skel[:,0], 0.5*np.ones(np.shape(skel[:,0])), color='r', marker='.')
plt.show()
```

In practice, centerlines are pruned and extended to the cell poles, in order to capture the cell length. Other geometrical properties such as masks centroids or outlines are computed as well.

![Final static processing results in real life data. White masks are excluded from the cell tracking algorithm (see part 2). Black dots are cell centroids. The yellow boxes represent artefacts cleaning.](Figure_3_quarto.png)

# References
Binary file added posts/AFM-data/raw_img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added posts/AFM-data/segmented_img_seg.npy
Binary file not shown.
Binary file added segmented_img_seg.npy
Binary file not shown.

0 comments on commit a9339c4

Please sign in to comment.