Skip to content

About

mcdb5910 course module on bacterial motility

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

10 Commits

Folders and files

Repository files navigation

MCDB 5910 · Quantifying bacterial motility: notebooks and code

The goal. Suppose you have trajectory data from an experiment: the positions of many cells over time, tracked from a microscopy movie. This material shows how to go from those tracks to quantities that describe how the cells move. We extract kinematics (speed and turning rate), identify behavioral states (runs and tumbles), and measure displacement in space (the mean squared displacement, MSD, and how it compares with analytic models of random walks).

Everything is in the folder notebooks/. In class (MCDB 5910, Day 1, section 3, "Trajectory analysis") we work through NB1 and NB2:

  • NB1 goes from tracks to behavior: speeds from positions, the effect of localization noise and smoothing, runs and tumbles in the (speed, angular velocity) space, and how long a cell remembers its direction.
  • NB2 goes from behavior to displacement: the MSD of Brownian motion, run-and-tumble and the biased random walk, derived analytically and simulated.

NB3 is optional extra reading. The hands-on notebook is for working on your own after class: it opens three real experiments, whose data files you download separately (see The hands-on notebook: real data). Every notebook runs from top to bottom without changes.

Notebook Part What you learn Run time
NB1_tracks_to_states A, B, C A speeds from positions, and how localization noise and smoothing affect them; B runs and tumbles in the (speed, angular velocity) state space; C the orientation autocorrelation and the persistence time ~10 s
NB2_msd D MSD of Brownian motion, run-and-tumble (constant and OU-driven tumble rate) and the biased random walk: equations of motion, analytic results and simulations ~30 s
NB3_extension extra The Lévy walk, and five random walks side by side (random, biased, run-and-tumble, smooth turner, Lévy) with matched parameters ~30 s
hands_on_real_tracks hands-on Opens real tracking experiments (three MATLAB .mat files): what is in each file, loading one, conversion to the course table, metadata read off the data, and trajectory views, then suggestions for your own analysis. Needs the downloaded data files, see below ~30 s

Smoothing explorer. notebooks/smoothing_explorer.html is an interactive page that opens in any web browser, without Python. (If you open it inside JupyterLab, click Trust HTML in the toolbar so the page may run its script.) Move the window slider to see how smoothing changes the speed of a swimming cell. We use it in class for NB1, part A (Try it 2). It is generated by tools/make_smoothing_explorer.py, which also documents how it is computed.

Run the notebooks

You need Miniconda (or Anaconda) and git. In a terminal:

git clone https://github.com/emonetlab/random_walk.git
cd random_walk
conda env create -f environment.yml
conda activate mcdb5910
python tools/install_kernel.py
jupyter lab

In JupyterLab, open a notebook from notebooks/, choose the kernel Python (mcdb5910), and run all cells. You only need the conda env create and install_kernel.py steps once.

The notebooks import the course package motility from the repository folder, so keep them in notebooks/ and open them from the cloned repository.

  • environment.yml creates the environment mcdb5910 with Python 3.12 and every package the notebooks use.
  • tools/install_kernel.py registers the environment as a Jupyter kernel. On Windows, use it instead of plain ipykernel install: a kernel that starts without conda activate cannot find numpy's math library and stops at the first matrix operation (Windows error 0xc06d007f). The script adds the environment folders to the kernel's search path.
  • Without conda: pip install -r requirements.txt in a Python 3.12 environment.

To check your installation, run python -m pytest (tests of the analysis code, a few seconds) and python tools/run_notebooks.py (runs every notebook, ~2 min; the hands-on notebook is skipped if its data files are missing). Both should report no failures.

The hands-on notebook: real data

notebooks/hands_on_real_tracks.ipynb works on real tracking experiments: three movies of swimming bacteria, two species in two environments, tracked and saved as MATLAB .mat files. The goal is to find the species and environment differences with the trajectory analysis of NB1 and NB2. The data files are not in the repository (90 MB in total); you download them once and put them in a folder of the repository. Step by step:

  1. Install and check the environment as described above (conda env create, install_kernel.py, and python -m pytest reports no failures).

  2. Download the three data files from the course Dropbox folder motility_module/data. The link is announced on the Canvas course website (and restated in class):

    • dataset_1.mat (44 MB)
    • dataset_2.mat (45 MB)
    • dataset_3.mat (3 MB)
  3. Move them into the folder data/experimental/ of your cloned repository. The folder exists already and contains a README.txt with these same instructions. Drag the files there in your file browser, or in a terminal from the repository folder:

    mv ~/Downloads/dataset_*.mat data/experimental/

    Afterwards the folder looks like this (the .mat files are ignored by git, so they never end up in a commit):

    random_walk/
    ├── data/
    │   ├── experimental/
    │   │   ├── README.txt
    │   │   ├── dataset_1.mat
    │   │   ├── dataset_2.mat
    │   │   └── dataset_3.mat
    │   └── synthetic/
    └── notebooks/
        └── hands_on_real_tracks.ipynb
    
  4. Open notebooks/hands_on_real_tracks.ipynb in JupyterLab (kernel Python (mcdb5910)) and run all cells. Section 1 of the notebook lists the files it finds in data/experimental/ with their contents; if the folder is empty it stops with a message telling you what is missing. Section 1 also has the line DATASET = "dataset_1": change it to "dataset_2" or "dataset_3" and run all again to look at another experiment.

  5. Then continue on your own. The last section of the notebook lists analyses to try next, from a single track colored by time to the kinematics of NB1 and the MSD of NB2, and the comparison of the three experiments.

The format of the files and what was measured from them are described in The experimental datasets.

What is in this repository

Path Contents
notebooks/ The notebooks.
motility/ The course Python package, imported by the notebooks. simulate: synthetic trajectories (Brownian motion, run-and-tumble) and as_dataset, which makes them look like microscopy data. analysis: velocity, speed and angular velocity (kinematics), velocity noise, per-cell summaries, MSD, autocorrelations, and the analytic MSD formulas. io: load a dataset (load_dataset) and read MATLAB files (mat_files_summary, describe_mat, read_mat, load_mat_tracks). plotting: figure style and the MSD figure.
data/synthetic/ The synthetic datasets (see below).
data/experimental/ Where the three downloaded experimental files go (see The hands-on notebook: real data). Only its README.txt is in git; everything else in it is ignored.
tests/ Tests of the analysis code on trajectories whose answer is known.
tools/ Scripts: install_kernel.py registers the Jupyter kernel; run_notebooks.py runs notebooks without a browser; make_synthetic_data.py regenerates the datasets; make_smoothing_explorer.py regenerates the smoothing explorer.
environment.yml, requirements.txt The software environment (conda or pip).

The datasets

Synthetic

Each dataset has three files in data/synthetic/:

  • <name>.csv: the tracks, one row per detection, with the columns track_id (one number per cell), frame, t (s), x_um and y_um (position in µm). This is the same format as tracking output from real movies.
  • <name>.json: metadata: frame rate, localization noise, track durations, and the parameters used to simulate the cells.
  • <name>_truth.csv (swimmers only): the true state, run or tumble, of every detection. Real data do not have this; here it lets you check your methods.
Dataset Cells Used in
brownian non-swimming cells (Brownian motion, D = 0.35 µm²/s) NB2, question 3 (measuring D and the localization noise)
run_tumble E. coli-like run-and-tumble swimmers: 25 ± 4 µm/s, 1 tumble per second, tumbles of 0.15 s, turn angles 68 ± 36° NB1; NB2, question 4
biased_run_tumble the same swimmers in an attractant gradient toward +x NB2, question 4 (detecting the bias)

How the data are made (tools/make_synthetic_data.py): 400 cells per dataset are simulated for 40 s at 30 frames per second (motility.simulate). Each track is then cut to a random duration (mean 10 s), because real cells leave the focal plane, and every position gets a localization error of 0.2 µm (motility.simulate.as_dataset). The random seeds are fixed, so the script makes identical files every time.

The experimental datasets

Three real tracking experiments, dataset_1.mat, dataset_2.mat and dataset_3.mat (44, 45 and 3 MB): two species of bacteria in two environments; which ones is announced on the Canvas course website and in class. They are not in the repository; The hands-on notebook: real data says where to download them and where to put them (data/experimental/).

Each file holds one MATLAB variable, tracks: a struct array with one element per cell and the fields x, y (positions in µm) and time (s), one value per frame. NaN marks frames in which the tracker lost the cell. motility.io.load_mat_tracks converts a file to the table format above (frame counts frames of the whole movie) and measures the metadata from the data; the hands-on notebook shows how. Measured from the files:

File Cells Frame rate Movie Field of view Track durations (min / median / max) Median frame-to-frame speed
dataset_1 4451 20 fps 10 min 0.93 × 1.75 mm 10 / 16 / 379 s 20 µm/s
dataset_2 4560 20 fps 10 min 3.3 × 3.3 mm 10 / 17 / 600 s 16 µm/s
dataset_3 2173 30 fps 5 min 1.9 × 0.85 mm 1.0 / 1.4 / 92 s 68 µm/s

About

mcdb5910 course module on bacterial motility

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages