Guide#

  • This guide focuses on our most common workflow (IF/iDISCO –> LSFM –> atlas registration –> voxel-wise stats –> cluster validation).

  • These video tutorials cover roughly similar content, but please refer to this guide and command help guides for additional info.

  • For getting started, test commands using these downsampled images from an example mouse.





Command Help Guides#

  • For help on a command, run this in the terminal:

<command> -h
  • Help guides often include info on prereqs, inputs, outputs, next steps, usage, and other notes.

  • Viewing the terminal help provides default values and additional options not covered in this guide.

  • Provide -v when running commands for verbose mode (to see info about function calls, parameters, etc.)

To view help on arguments for each command in the online documentation, go to the page for that module/script, scroll to the parse_args() function, and click the link for viewing the source code.



Listing Commands#

# List common commands
unravel_commands -c

# List common commands, aliases, and descriptions:
uc -cad

# List all commands matching a string (e.g., vstats):
uc -ad -f vstats  # Find commands related to voxel-wise stats 

# List all commands and the modules/scripts that they run:
uc -m

Hint

  • Prefixes group together related commands. Use tab completion in the terminal to quickly view and access sets of commands within each group.



Common Commands#

  • reg_prep: Prepare registration (resample the autofluo image to lower res).

  • reg: Perform registration (e.g., register the autofluo image to an average template).

  • reg_check: Check registration (aggregate the autofluo and warped atlas images).

  • warp_to_atlas: Warp full res tissue space images to atlas space.

  • warp_to_native: Warp images to native img space, unpad, and scale to full res.

  • warp_to_fixed: Warp full res tissue space images to fixed img space and unpad.

  • seg_copy_tifs: Copy TIF images (copy select tifs to target dir for training ilastik).

  • seg_brain_mask: Create brain mask (segment resampled autofluo tifs).

  • seg_ilastik: Perform pixel classification w/ Ilastik to segment features of interest.

  • rstats: Compute regional cell counts, regional volumes, or regional cell densities.

  • rstats_summary: Summarize regional cell densities.

  • io_metadata: Save raw voxel dimensions in microns and image dimensions to parameters/metadata.txt.

  • io_nii_info: Print info about NIfTI files.

  • img_avg: Average NIfTI images.

  • img_unique: Find unique intensities in images.

  • img_max: Print the max intensity value in an image.

  • img_spatial_avg: Perform spatial averaging on images.

  • img_rb: Apply rolling ball filter to TIF images.



Set Up#

Recommended steps to set up for analysis:

Back Up Raw Data#

  • For Heifets lab members, we keep one copy of raw data on an external drive and another on a remote server (Dan and Austen have access)

Stitch Z-stacks#

Make Sample Folders#

  • Make a folder named after each condition in the experiment folder(s)

. # This root level folder is referred to as the experiment directory (exp dir) 
├── Control
└── Treatment
  • Make sample folders in the directories named after each condition

.
├── Control
│   ├── sample01
│   └── sample02
└── Treatment
    ├── sample03
    └── sample04

Add Images to sample?? Directories#

  • For example, image.czi, image.h5, folder(s) with tif series, or an .ome.tif.

.
├── Control
│   ├── sample01
│   │   └── <raw/stitched image(s)>
│   └── sample02
│       └── <raw/stitched image(s)>
└── Treatment
    ├── sample03
    │   └── <raw/stitched image(s)>
    └── sample04
        └── <raw/stitched image(s)>

Log Paths, Commands, etc.#

Make a sample_key.csv:#

It should have these columns:

  • dir_name,condition

  • sample01,control

  • sample02,treatment

Defining Common Variables for Command Arguments#

Optional: Clean TIFFs#

  • If raw data is in the form of a tif series, consider running:

utils_clean_tifs -t <path to directories with tifs relative to ./sample?? folders> -m
  • This will remove spaces from files names and move files other than *.tif to the parent directory



Analysis Overview#

Overview of a typical workflow (voxel-wise analyses followed by cluster validation):

flowchart TD A(((LSFM))) A --> B[Stitched 3D autofluorescence image] B --> C(Resample image to 50 µm resolution) C --> D(Segment tissue with Ilastik to mask external voxels) D --> E(Register image to an average template brain) A --> F[Stitched 3D immunofluorescence image] F --> G(Remove autofluorescence from IF images) G --> H(Warp IF images to atlas space) E --> H H --> I(Preprocess IF images: z-score, smooth, and average left/right sides) I --> J(Perform voxel-wise statistical analyses) J --> K(Apply FDR correction of 1-p value maps) K --> L(Warp significant voxel clusters back to tissue space) F --> N(Segment c-Fos+ cells or other features using Ilastik) L --> M(Validate clusters with cell/label density measurements) N --> M D --> I E --> L


Training Ilastik#

Ilastik has two purposes for our typical workflow:

  1. During registration, Ilastik is used to segment tissue, creating brain masks that refine registration of autofluo images and z-scoring of IF images. After training Ilastik, segmentation of 50 µm autolfluo images is automated with seg_brain_mask.

_images/Ilastik_brain_mask_example.jpg
  1. During cluster validation, Ilastik labels c-Fos+ cells or other features of interest for quantification of cell or label densities within clusters from voxel-wise statistics. Segmentation of full-res IF images is automated with seg_ilastik.

_images/Ilastik_c-Fos_example.jpg

Pixel classification documentation

Setting up Ilastik for batch processing

# Add this to your ~/.bashrc or ~/.zshrc terminal config file:
export PATH=/usr/local/ilastik-1.4.0.post1-Linux:$PATH   # Update the path and version

# Optional: add a shortcut command for launching Ilastik via the terminal
alias ilastik=run_ilastik.sh  # run_ilastik.sh could be replaced w/ the full path to the executable file

# Ilastik executable files for each OS:
#     - Linux and WSL: /usr/local/ilastik-1.4.0.post1-Linux/run_ilastik.sh
#     - Mac: /Applications/Ilastik.app/Contents/ilastik-release/run_ilastik.sh
#     - Windows: C:\Program Files\ilastik-1.3.3post3\run_ilastik.bat

# Source your terminal config file for these edits to take effect: 
. ~/.bashrc  # Or close and reopen the terminal

Training Ilastik

Launch Ilastik

  • Either double click on the application or run: ilastik (if you set up an alias)

  1. Input Data

    • Gather training slices to a folder from all samples with seg_copy_tifs

    • Drag and drop training slices into the ilastik GUI (e.g., from a dir w/ 3 slices per sample and > 2 samples per condition)

    • ctrl+A to select training slices -> right-click -> Edit shared properties -> Storage: Copy into project file -> Ok

  2. Feature Selection

    • Select Features… -> select all features (control+a) or an optimized subset (faster but less accurate).

    • Optional: to find an optimal subset of features, select all features, train Ilastik, turn off Live Updates, click Suggest Features, select a subset, and refine training.

  3. Training

    • Brightness/contrast: select the gradient button and click and drag in the image (faster if zoomed in)

    • Zoom: control/command + mouse wheel scroll, control/command + 2 finger scroll, or - and + (i.e., shift + =)

    • Pan: shift + left click and drag or click mouse wheel and drag

    • With label 1 and the brush tool selected, paint on c-Fos+ cells or another feature of interest

    • With label 2 and the brush tool selected, paint on the background (e.g., any pixel that is not a cell)

    • Turn on Live Update to preview pixel classification (faster if zoomed in) and refine training (e.g., if some cells are classified as background, paint more cells with label 1).

      • s will toggle the segmentation on and off.

      • p will toggle the prediction on and off.

      • Toggle eyes to show/hide layers and/or adjust transparency of layers.

    • Change Current View to see other training slices. Check segmentation for these and refine as needed.

    • Save the project in the experiment summary folder and close if using this script to run ilastik in headless mode for segmenting all images.

    • Segment 50 µm autofluo images

    • Segment immunofluo images to segment c-Fos+ cells or other features

Notes

  • If you want to go back to steps 1 & 2, turn Live Updates off

  • It is possible at add extra labels with a (e.g., if you want to segment somata with one label and axons with another label)

  • If you accidentally press a, turn off Live Updates and press x next to the extra label to delete it.

  • If the segmentation for label 1 fuses neighboring cells, draw a thin line in between them with label 2.



Registration#

flowchart TD A(((LSFM))) A --> B[Stitch z-stacks if necessary] B --> C(io_metadata: Save raw voxel dimentions in microns to ./sample??/parameters/metadata.txt) C --> D(reg_prep: Resample autofluorescence images to 50 µm resolution for registration) D --> E(seg_copy_tifs: Copy select slices from 50 µm autofl images to create brain masks using Ilastik) E --> F(Train Ilastik using pixel classification to segment autofl brains) F --> G(seg_brain_mask: Generate autofl brain masks with Ilastik and apply them to the 50 µm images) G --> H(Optionally use 3D Slicer to make the masked autofl image and mask better match the average template) H --> I(reg: Register the template brain with the masked autofl images and warp the atlas) I --> J(reg_check: Aggregate padded autofl images and warped atlas images from reg) J --> K(Assess registration quality in FSLeyes and refine if necessary by repeating the 3D Slicer step, etc.)

io_metadata#

unravel.image_io.metadata

  • Extract or specify x and z voxel sizes in microns (saves to ./sample??/parameters/metadata.txt).

  • You can add these resolutions to env_var.sh as global variables (e.g., $XY and $Z) and load them before running commands with: source path/to/env_var.sh

  • Guide on defining common command arguments in env_var.sh

  • For batch processing, run the io_metadata command from a directory containing sample?? folders, within a sample?? folder, or use -d to pass in a list of paths to sample folders or directories containing sample folders.

# To specify x and z voxel sizes in microns, use the -x and -z flags.
io_metadata -i <path to image or directory with TIFFs relative to sample??/> -x $XY -z $Z [-d $DIRS] # Remove square brackets from optional arguments
# If env_var.sh is not used, pass in the voxel sizes like this: -x 3.5232 -z 6.

# If -x and -z are omitted, io_metadata will attempt to extract this information from the image metadata.
io_metadata -i <rel_path/full_res_img> [-d $DIRS]
# 'rel_path' refers to the relative path from within the sample?? folders.
# Glob patterns can be used for -i (e.g., *.czi).

reg_prep#

unravel.register.reg_prep

  • Prepare autofluorescence images for registration (resample them to 50 µm isotropic resolution and save to ./sample??/reg_inputs/)

reg_prep -i <rel_path/image> [-d $DIRS] # -i options: tif_dir, .czi, .h5, .zarr, or .tif (e.g., *.czi)
# If using *.czi, by default the first channel is used (--channel 0), since that is usually the autofluo channel

seg_copy_tifs#

unravel.segment.copy_tifs

  • Copy resampled autofluo .tif files from each sample for making a brain mask with ilastik

  • In the example below -s 0000 0005 0050 means that the 1st, 6th, and 51st tif will be copied to the target directory

  • The target directory is the current working directory unless -td is used to specify an output path.

seg_copy_tifs -i reg_inputs/autofl_??um_tifs -s 0000 0005 0050 [-td brain_mask] [-d $DIRS]  

Train Ilastik to segment autofluo tissue to make a brain mask

seg_brain_mask#

unravel.segment.brain_mask

  • Makes reg_inputs/autofl_??um_brain_mask.nii.gz and reg_inputs/autofl_??um_masked.nii.gz for reg

seg_brain_mask -ie <path/ilastik_executable> -ilp <path/trained_ilastik_project.ilp> [-d $DIRS]

Making the autofluorescence image better match the average template image

  • seg_brain_mask zeros out voxels outside of the brain. This prevents the average template (moving image) from being pulled outward during registration (reg).

  • If non-zero voxles outside the brain remain and are affecting reg quality, use 3D slicer to zero them out by painting in 3D (segmentation module).

  • If there is missing tissue, use 3D slicer to fill in gaps.

reg#

unravel.register.reg

  • Register an average template brain/atlas to a resampled autofluo brain.

reg -m <path/template.nii.gz> -bc -sm 0.4 -ort <3 letter orientation code> -m2 <path/atlas_CCFv3_2020_30um.nii.gz> [-d $DIRS]

# Example if using env_var.sh (assuming an RPS orientation):
reg -m $TEMPLATE -bc -pad -sm 0.4 -ort RPS -a $ATLAS -d $DIRS

# -bc performs N4 bias field correction to make image intensities more uniform
# -sm smooths the autofluo image by the amount specified
# If you want to use an unmasked autofluo image, provide: -mas None -f reg_inputs/autofl_50um.nii.gz. Other commands alos assume that a masked autofluo image was used, so check each help guide for default arguments.

reg_check#

unravel.register.reg_check

  • Check registration by copying these images from each sample??/reg_ouputs folder to a target directory:

    • autofl_??um_masked_fixed_reg_input.nii.gz

    • atlas_in_tissue_space.nii.gz

reg_check [-td reg_results] [-d $DIRS]  # Default for -td: copy images to the current dir.
# The default warped atlas from reg is atlas_CCFv3_2020_30um_in_tissue_space.nii.gz (in ./sample??/.reg_outputs). Use -wa <image_name.nii.gz> to set this.

Checking registration with FSLeyes

_images/FSLeyes_autofl_image_from_reg.jpg

Use FSLeyes to view the autofluo image from reg (sample??/reg_ouputs/autofl_50um_masked_fixed_reg_input.nii.gz).#

_images/FSLeyes_atlas_warped_to_tissue_from_reg.jpg

Use FSLeyes to view the atlas warped to the the tissue (sample??/reg_ouputs/atlas_CCFv3_2020_30um_in_tissue_space.nii.gz)#

Setting up Allen brain atlas coloring in FSLeyes

  • Add UNRAVEL/_other/fsleyes_luts/ccfv3_2020.lut to the following location:

    • Linux: /home/<your_username>/.config/fsleyes/luts/ccfv3_2020.lut

    • MacOS: /usr/local/fsl/fslpython/envs/fslpython/lib/python3.8/site-packages/fsleyes/assets/luts/ccfv3_2020.lut

    • Windows: C:\Users\<your_username>\AppData\Roaming\fsleyes\luts\ccfv3_2020.lut

  • On MacOS, run the following command to add write permissions to the LUT folder:

sudo chmod a+w /usr/local/fsl/fslpython/envs/fslpython/lib/python3.8/site-packages/fsleyes/assets/luts
  • On MacOS, edit order.txt to include ccfv3_2020.lut.

  • To remove other LUTs, move them up a directory level, but keep random.lut in place.

  • Open atlas_CCFv3_2020_30um.nii.gz in FSLeyes.

  • Select the atlas and change “3D/4D volume” to “Label image.”

  • Switch the lookup table from random to ccfv3_2020.

  • Click the circle icon at the top to convert the atlas to a wireframe view.

  • If you don’t want to select the LUT every time, make a copy of random.lut and replace its contents with those of ccfv3_2020.lut.



Voxel-wise Statistics#

flowchart TD A(((LSFM))) A --> B[Stitch z-stacks if required] B --> C(vstats_prep: Optionally remove autofluorescence and warp immunofluo images to atlas space) C --> D(vstats_z_score: Optionally z-score IF images from each brain individually) D --> E(utils_agg_files: Aggregate atlas-space IF images from all sample folders into the current directory) E --> F(vstats_whole_to_avg: Optionally smooth image and average left/right sides) F --> G(Check inputs for vstats: Open IF images and the atlas in FSLeyes to confirm aligment with the atlas and other IF images) G --> H(Prep inputs for vstats: Add IF images to the current directory, without other .nii.gz files) H --> I(utils_prepend: Prepend a one-word condition to each IF image) I --> J(For ANOVA designs, run fsl and set up the design. If two groups are present, proceed to the next step) J --> K(vstats: Run voxel-wise analyses using FSL's randomise tool) K --> L(View 1-p value maps in FSLeyes and consult 'Cluster-wise stats' for multiple comparisons correction and cluster validation)

vstats_prep#

unravel.voxel_stats.vstats_prep

  • Preprocess immunofluo images and warp them to atlas space for voxel-wise statistics.

# Example with rolling ball background subtration (pixel radius of 4):
vstats_prep -i cfos -o cFos_rb4_atlas_space.nii.gz -rb 4 -a $ATLAS [-d $DIRS]

# Usage:
vstats_prep -i <rel_path/image> -o cFos_rb4_atlas_space.nii.gz -a $ATLAS [-sa 3] [-rb 4] [--channel 1] [-d $DIRS]
# --channel is for .czi images (1 is the second channel)
_images/FSLeyes_Ai14_image_in_CCFv3_30um_space.jpg

Use FSLeyes to view the fluorescently labeled image in atlas space.#

vstats_z_score#

unravel.voxel_stats.z_score

  • Z-score atlas space images using tissue masks (from brain_mask) and/or an atlas mask.

# Z-scoring using the brain mask (-i and -tmas paths are relative to the sample?? folder)
vstats_z_score -i atlas_space/sample??_cFos_rb4_atlas_space.nii.gz [-d $DIRS] 

# Z-scoring using an atlas mask
vstats_z_score -i 'atlas_space/*.nii.gz' -amas $MASK [-d $DIRS] 

# Using a tissue mask and an atlas mask
vstats_z_score -i 'atlas_space/*.nii.gz' -tmas reg_inputs/autofl_50um_brain_mask.nii.gz -amas $MASK [-d $DIRS] 

utils_agg_files#

unravel.utilities.aggregate_files_from_sample_dirs

  • Aggregate pre-processed immunofluorescence (IF) images for voxel-wise stats

utils_agg_files -i atlas_space/*_cFos_rb4_atlas_space_z.nii.gz [-td path/target_dir] [-d $DIRS]

vstats_whole_to_avg#

unravel.voxel_stats.whole_to_LR_avg

  • Smooth and average left and right hemispheres together

# Run this in the folder with the .nii.gz images to process
vstats_whole_to_avg -k 0.1 --parallel -v  # A 0.05 mm - 0.1 mm kernel radius is recommended for smoothing

# Process all *.nii.gz files in the current directory with no smoothing:
vstats_whole_to_avg [-tp] [-amas $MASK] # Provide -tp for parallel processing.

# Process files matching the pattern with smoothing:
vstats_whole_to_avg -i '*_cFos_rb4_atlas_space_z.nii.gz.nii.gz' -k 0.1 [-tp] [-amas $MASK]
# A 0.1 mm kernel radius is recommended for smoothing (-k)

Setting up voxel-wise stats

  1. Create a vstats folder and subfolders for each analysis:

    • Name subfolders succinctly (this name is added to other folder and file names).

  2. Generate and add .nii.gz files to vstats subfolders:

    • Input images are from vstats_prep and may have been z-scored with vstats_z_score (we z-score c-Fos labeling as intensities are not extreme)

    • For bilateral data, left and right sides can be averaged with vstats_whole_to_avg (then use a unilateral hemisphere mask for vstats and cstats_fdr).

    • We smooth data (e.g., with a 100 µm kernel) to account for innacuracies in registration

      • This can be performed with vstats_whole_to_avg or vstats

    • Prepend filenames with a one word condition (e.g., drug_sample01_atlas_space_z.nii.gz).

      • Camel case is ok for the condition.

      • utils_prepend can add conditions to filenames.

      • Group order is alphabetical (e.g., drug is group 1 and saline is group 2).

    • View the images in FSLeyes to ensure they are aligned with the atlas and the sides are correct.

    • Other .nii.gz images unrelated to voxel-wise analyses should be excluded from the vstats subfolder.

  3. Determine Analysis Type:

    • If there are 2 groups, vstats may be used after pre-processing.

    • If there are more than 2 groups, prepare for an ANOVA as described below

vstats Outputs

  • T-test outputs:

    • vox_p_tstat1.nii.gz: Uncorrected p-values for tstat1 (group 1 > group 2).

    • vox_p_tstat2.nii.gz: Uncorrected p-values for tstat2 (group 1 < group 2).

  • ANOVA outputs:

    • vox_p_fstat1.nii.gz: Uncorrected p-values for fstat1 (1st contrast, e.g., drug vs. saline).

    • vox_p_fstat2.nii.gz: Uncorrected p-values for fstat2 (2nd contrast, e.g., context1 vs. context2).

    • vox_p_fstat3.nii.gz: Uncorrected p-values for fstat3 (3rd contrast, e.g., interaction).

Example: Preparing for an ANOVA

  1. Setup Design Matrix:

    • For an ANOVA, create ./vstats/vstats_dir/stats/design/.

    • Open terminal from ./stats and run: fsl.

    • Navigate to Misc -> GLM Setup.

  2. GLM Setup Window:

    • Select Higher-level / non-timeseries design.

    • Set # inputs to the total number of samples.

  3. EVs Tab in GLM Window:

    • Set # of main EVs to 4.

    • Name EVs (e.g., EV1 = group 1).

    • Set Group to 1 for all.

  4. Design Matrix:

    • Under EV1, enter 1 for each subject in group 1 (1 row/subject). EV2-4 are 0 for these rows.

    • Under EV2, enter 1 for each subject in group 2, starting with the row after the last row for group 1.

    • Follow this pattern for EV3 and EV4.

  5. Contrasts & F-tests Tab in GLM Window:

    • Set # of Contrasts to 3 for a 2x2 ANOVA:

      • C1: Main_effect_<e.g.,drug> 1 1 -1 -1 (e.g., EV1/2 are drug groups and EV3/4 are saline groups).

      • C2: Main_effect_<e.g., context> 1 -1 1 -1 (e.g., EV1/3 were in context1 and EV2/4 were in context2).

      • C3: Interaction 1 -1 -1 1.

    • Set # of F-tests to 3:

      • F1: Click upper left box.

      • F2: Click middle box.

      • F3: Click lower right box.

  6. Finalize GLM Setup:

    • In the GLM Setup window, click Save, then click design, and click OK.

  7. Run Voxel-wise Stats:

    • From the vstats_dir, run: vstats.

Background on FSL’s Randomise Tool

vstats#

unravel.voxel_stats.vstats

  • Run voxel-wise stats using FSL’s randomise_parallel command

  • Outputs saved in stats/

# Run this in the folder with the input IF images (the current directory name will be prepended to outputs)
vstats -mas $MASK -a $ATLAS [-p 18000] [-k 0.1]
# -p sets the number of permutations.
# -k is for smoothing if inputs have not yet been smoothed (-k is the kernel size in mm)
# See the vstats help for providing additional arguments to FSL's randomise_parallel

Outputs from voxel-wise stats

  • Outputs in ./stats/

    • vox_p maps are uncorrected 1-p value maps

    • tstat1: group1 > group2

    • tstat2: group2 > group1

    • fstat*: f contrast w/ ANOVA design (non-directional p value maps)

    • fstat1 corresponds to the first Contrast defined in step 5 above.



Cluster-wise Statistics#

flowchart TD A(vstats: Generates 1-p value maps with vox_p in the filename) A --> B(cstats_fdr_range: Input a 1-p map to find FDR q values that yield clusters) B --> C(cstats_fdr: Apply FDR correction on the 1-p map to identify clusters of significant voxels) C --> D(cstats_mirror_indices: For whole-brains, recursively mirror cluster maps in ./stats/) D --> E(seg_copy_tifs: Copy select full resolution TIFFs for Ilastik training) E --> F(seg_ilastik: Perform pixel classification with a trained Ilastik project) F --> G(cstats_validation: Warp clusters from atlas to full resolution tissue space, count cells, and calculate cluster volumes) G --> H(cstats_summary: Aggregate and analyze cluster validation data)

False Discovery Rate (FDR) Correction#

cstats_fdr_range#

unravel.cluster_stats.fdr_range

  • Outputs a list of FDR q values that yield clusters.

cstats_fdr_range -i <path/vox_p_tstat1.nii.gz> -mas $MASK

cstats_fdr#

unravel.cluster_stats.fdr

  • Perform FDR correction on a 1-p value map to define clusters

  • Outputs are saved to a new folder in stats/ named after the input image and q value

cstats_fdr -i <path/vox_p_tstat1.nii.gz> -mas $MASK -q 0.05 0.01 0.001 [-ms 100] 
# For -q, copy the output from cstats_fdr_range
# For -ms, set the number of voxels for a cluster (e.g., 100 for c-Fos or 400 if labeling is sparse)

# Perform FDR correction on multiple directional 1-p value maps
for j in *_vox_p_*.nii.gz ; do q_values=$(cstats_fdr_range -mas $MASK -i $j) ; cstats_fdr -mas $MASK -i $j -q $q_values ; done

# ANOVAs output non-directioanl 1-p value maps. Optionally convert them into a directional cluster index:
q_values=$(cstats_fdr_range -i vox_p_fstat1.nii.gz -mas $MASK) ; cstats_fdr -i vox_p_fstat1.nii.gz -mas $MASK -o fstat1 -v -a1 Control_avg.nii.gz -a2 Deep_avg.nii.gz -q $q_values
# Make averaged images for each group (-a1 and -a2 args) with the img_avg command

cstats_mirror_indices#

unravel.cluster_stats.recursively_mirror_rev_cluster_indices

  • Recursively flip the content of rev_cluster_index.nii.gz images (to validate w/ clusters in left and right hemispheres)

  • Run this in the ./stats/ folder to process all subdirs with reverse cluster maps (cluster IDs go from large to small)

# Use -m RH if a right hemisphere mask was used (otherwise use -m LH)
cstats_mirror_indices -m RH [-i glob_pattern]


Segmentation of Full-Resolution Fluorescence Images#

1) seg_copy_tifs#

unravel.segment.copy_tifs

  • Copy or extract full res tif files to a target dir for training Ilastik to segment labels of interest

Tip

Copy/extract 3 slices from each sample, with at least 3 samples per condition.

seg_copy_tifs -i <rel_path/raw_image> -s 0100 0500 1000 [-td ilastik_segmentation] [-d $DIRS]
# Default for target dir (-td) is current dir
# The name of the dir with full-res tifs can be passed in for -i

2) Train Ilastik to segment c-Fos+ cells or other features#

3) seg_ilastik#

unravel.segment.ilastik_pixel_classification

  • Segment features of interest (e.g., c-Fos+ cells) in full-resulution images using a trained Ilastik project (pixel classification)

seg_ilastik -ie <path/ilastik_executable> -ilp <path/ilastik_project.ilp> -i <rel_path to tif_dir or image> -o seg_dir --labels 1 [--rm_out_tifs] [--channel 1] [-d $DIRS]
# --labels is a space-separated list of labels to convert into binary .nii.gz images.
# --channel is for .czi images (1 is the second channel)


Cluster Validation and Statistics#

flowchart TD A(cstats_org_data: Organize cell/label density data from cluster validation) A --> B(cstats_group_data: Pool left and right hemisphere data) B --> C(utils_prepend: Prepend CSV names with conditions) C --> D(cstats: Run statistics to determine cluster validity) D --> E(cstats_index: Create valid cluster maps and CSVs for sunburst plots) E --> F(cstats_brain_model: Create files for 3D brain models) F --> G(cstats_table: Create tables summarizing top regions and volumes) G --> H(cstats_prism: Generate CSVs for plotting bar graphs with Prism) H --> I(cstats_legend: Create legend files defining region abbreviations) I --> J(Summarize cluster validation rates, aggregate files for 3D brains, compile SI tables, etc.)

cstats_validation#

unravel.cluster_stats.validation

  • Warps cluster index from atlas space to tissue space, crops clusters, applies segmentation mask, and quantifies cell or label densities

# Cell density measurements in all clusters
cstats_validation -m <path/rev_cluster_index_to_warp_from_atlas_space.nii.gz> -s  <rel_path/seg_img.nii.gz> [-d $DIRS]

# Label density measurements in select clusters
cstats_validation -m <path/rev_cluster_index_to_warp_from_atlas_space.nii.gz> -s  <rel_path/seg_img.nii.gz> -de label_density -c 1 3 4 [-d $DIRS]

# Use -n to save the cluster map in tissue space (-n rel_path/native_cluster_index.zarr). Slower than w/o saving.
# -inp nearestNeighbor is default, but for smoother clusters use multiLabel (slower)

# Processing multiple FDR q value thresholds and both hemispheres (assumes that cstats_mirror_indices has been run):
for q in 0.005 0.01 0.05 0.1 ; do for side in LH RH ; do cstats_validation  -m path/vstats/contrast/stats/contrast_vox_p_tstat1_q${q}/contrast_vox_p_tstat1_q${q}_rev_cluster_index_${side}.nii.gz -s rel_path/seg_img.nii.gz [-d $DIRS] ; done ; done

cstats_summary#

unravel.cluster_stats.summary

  • Aggregates and analyzes cluster validation data from cstats_validation

  • Update parameters in /UNRAVEL/unravel/cstats/cluster_summary.ini and save it with the experiment

  • group1 and group2 must match conditions in the sample_key.csv

# Usage if running directly after ``cstats_validation``
cstats_summary -c <path/cluster_summary.ini> -cvd 'psilocybin_v_saline_tstat1_q*' -vd <path/vstats_dir> -sk $SAMPLE_KEY --groups <group1> <group2> -hg <higher_group> [-d $DIRS]

# Usage if running after ``cstats_validation`` and ``cstats_org_data``:
cstats_summary -c <path/cluster_summary.ini> -sk $SAMPLE_KEY  --groups <group1> <group2> -hg <higher_group> [-d $DIRS]

# See the help guide for cstats_validation if you want to pool data using condition prefixes


Sunburst Plots#

Sunburst plots of regional volumes

  • CSVs for making sunburst plots are output by cstats_validation for each valid cluster map.

  • Output location: main_experiment_dir/cstats/<vstats_contrast>_q/_valid_clusters/valid_clusters_sunburst.csv

  • Or sunburst CSVs can be generated using cstats_sunburst

  • Sunburst CSVs can be copy and pasted into the sunburst plot tool of the Flourish web app

_images/sunburst_1_flourish.jpg
  • Make a free account w/ Flourish.

  • Login.

  • Select “New visualization”.

_images/sunburst_2_plot_types.jpg

Under “Hierarchy” select “Sunburst”#

_images/sunburst_3_adding_data.jpg
  • Select the “Data” tab.

  • Set it up like this with columns A-K. Categories/nesting: B-J, Size by: K.

  • Always delete the entirety of the columns before pasting in values.

  • Paste in values for columns A-K from a *_sunburst.csv.

  • Switch to the “Preview” tab to view the plot.

_images/sunburst_4_RGB_values.jpg
  • Paste the contents of sunburst_RGBs.csv into Colors –> Custom overrides.

  • Location: UNRAVEL/unravel/core/csvs/sunburst_RGBs.

  • cstats_sunburst can output it w/ -rgb.

  • Set Min font size under “Labels” to 0.5 to show labels.

  • Increase Min font size above the Max size to hide them.

  • Take a screenshot of the plot to save it.



3D Brain Models#

Visualize cluster maps, etc. in a 3D brain

  • Files for making 3D brains in DSI Studio are output by cstats_validation for each valid cluster map.

  • Output location: main_experiment_dir/cstats/3D_brains/

  • Alternate location: main_experiment_dir/cstats/_q/_valid_clusters/

  • Output image: _q_rev_cluster_index__valid_clusters_ABA_WB.nii.gz

  • Output look up table: _q_rev_cluster_index__valid_clusters_rgba.txt

  • These files can be generated with cstats_brain_model (use -m for a bilateral representation of a unilateral map)

Visualization in DSI Studio

  • Open DSI studio

  • Click on “Step T3”

  • Change the drop down to select all files and select the binary atlas (mask_CCFv3_2020_30um.nii.gz)

  • If you open the binary atlas in the 3D_brains folder, it is easier to open other files from there later

  • Make it visible: Slices –> Add Isosurface –> Full –> OK –> Zoom out (mouse wheel or 2 finger scroll)

Display settings

_images/brain_model_settings_1.jpg
_images/brain_model_settings_2.jpg

Layout

_images/brain_model_layout.jpg
  • The zoom and viewer dimensions determine the output video size.

  • This layout allows for 1080p videos with my MacBook (adjust as needed).

  • Move panels on the right down and make them as small as possible.

  • Line up the left edge of the viewer with View.

  • Zoom to the view 0.28 (lower right of viewer)

  • Zoom out to 0.18 for axial.

Adding Regions and Color

  • Regions –> Open Region… –> load _q_rev_cluster_index__valid_clusters_ABA_WB.nii.gz

  • Regions Misc –> Load Region Color… –> load _q_rev_cluster_index__valid_clusters_rgba.txt

Making Figures

  • Use z, x, and c to snap to specific orientations.

  • Use View –> Save Camera View… to save an oblique orientation

  • Load it with: View –> Open Camera View…

  • Capture screenshots

Recording Videos

  • For a video: press z for a sagittal orientation (e.g., facing to the right), View –> Save Rotation Video/Images…

  • The output .avi is large, but can be exported in another format

  • For example, in Adobe Premiere Pro –> add the video to the timeline –> right click to double video speed –> export with SD resolution (480p) –> scale to fit –> export.

  • Composite videos with multiple cluster maps can be made with Adobe Premiere Pro using the exported videos.



Valid Cluster Info Tables#

  • Cluster info .xlsx tables are output by cstats_validation for each valid cluster map.

  • Output location: valid_clusters_tables_and_legend

  • Alternatively, they can be made with cstats_table and cstats_legend

  • Compile each _q_valid_clusters_table.xlsx and legend.xlsx into one .xlsx SI table

  • Example SI Table from our initial UNRAVEL paper



Region-wise Statistics#

flowchart TD A(((LSFM))) A --> B[3D autofluorescence image] B --> C(Registration to an average template brain) C --> D(Optional: warp_to_native: Warp the atlas to full-resolution tissue space and save as .zarr) A --> E[3D immunofluorescence image] E --> F(seg_copy_tifs & seg_ilastik: Segment c-Fos+ cells using Ilastik) F --> G(rstats: Perform regional cell counting and volume measurements) D --> G G --> H(utils_agg_files: Aggregate CSV outputs from rstats across all sample folders) H --> I(rstats_summary: Plot cell densities by region and summarize results)

rstats#

unravel.region_stats.regional_cell_densities

  • Quantify regional cell densities or label densities

# Usage if the atlas is already in native space from ``warp_to_native``:
rstats -s <rel_path/segmentation_image.nii.gz> -a <rel_path/native_atlas_split.nii.gz> -c Saline -t cell_densities [-d <paths to sample folders in the Saline group or the folders containing them>]

# Usage if the native atlas is not available; it is not saved (faster):
rstats -s rel_path/segmentation_image.nii.gz -m $SPLIT -c Saline -t cell_densities [-d <paths to sample folders in the Saline group or the folders containing them>]

Aggregate outputs from rstats with utils_agg_files.

rstats_summary#

unravel.region_stats.regional_cell_densities_summary

  • Plot cell densities for each region and summarize results.

  • CSVs from rstats should be in the current directory

  • CSV columns:

    • Region_ID,Side,Name,Abbr,Saline_sample06,Saline_sample07,…,MDMA_sample01,…,Meth_sample23,…

# Usage for Tukey tests:
rstats_summary --groups Saline MDMA Meth -hemi <both l or r> [-y cell_density] [-div 10000]
# -div 10000 divides cells per cubic mm by 10000 (for plotting)
# -y is the y-axis label

# Usage for t-tests:
rstats_summary --groups Saline MDMA -hemi <both l or r>  -t t-test -c Saline [-alt two-sided] [-y cell_density] [-div 10000] 


Example sample?? Folder Structure#

.
├── atlas_space  # Dir with images warped to atlas space
├── cfos_seg_ilastik_1  # Example dir with segmentations from ilastik
├── clusters  # Dir with cell/label density CSVs from cstats_validation   ├── Control_v_Treatment_vox_p_tstat1_q0.005
│      └── cell_density_data.csv
│   └── Control_v_Treatment_vox_p_tstat2_q0.05
│       └── cell_density_data.csv
├── parameters  # Optional dir for things like metadata.txt
├── reg_inputs  # From reg_prep (autofl image resampled for reg) and seg_brain_mask (mask, masked autofl)
├── regional_cell_densities  # CSVs with regional cell densities data
├── reg_outputs  # Outputs from reg. These images are typically padded w/ empty voxels. 
└── image.czi # Or other raw/stitched image type

Example Experiment Folder Structure#

.
├── exp_notes.txt
├── env_var.sh
├── sample_key.csv
├── Control
│   ├── sample01
│   └── sample02
├── Treatment
│   ├── sample03
│   └── sample04
├── atlas
│   ├── atlas_CCFv3_2020_30um.nii.gz  # Each atlas region has a unique intensity/ID   ├── atlas_CCFv3_2020_30um_split.nii.gz  # Intensities in the left hemisphere are increased by 20,000   ├── average_template_CCFv3_30um.nii.gz  # Average template brain that is aligned with the atlas   └── mask_CCFv3_2020_30um_RH_wo_root_ventricles_fibers_OB.nii.gz  # Right hemisphere mask that excludes undefined regions (root), ventricles, fiber tracts, and the olfactory bulb
├── check_reg  # run check_reg from here
├── brain_mask
│   ├── brain_mask.ilp  # Ilastik project trained with the pixel classification workflow to segment the brain in resampled autofluo images   ├── sample01_slice_0000.tif
│   ├── sample01_slice_0005.tif
│   ├── sample01_slice_0050.tif
│   ├── ...
│   └── sample04_slice_0050.tif
├── vstats  # Voxel-wise stats   └── Control_v_Treatment        ├── Control_sample01_rb4_atlas_space_z.tif
│       ├── Control_sample02_rb4_atlas_space_z.tif
│       ├── Treatment_sample03_rb4_atlas_space_z.tif
│       ├── Treatment_sample04_rb4_atlas_space_z.tif
│       └── stats            ├── Control_v_Treatment_vox_p_tstat1.nii.gz  # 1 minus p value map showing where Control (group 1) > Treatment (group2)           ├── Control_v_Treatment_vox_p_tstat2.nii.gz  # 1-p value map showing where Treatment (group 2) > Control (group 1)           ├── Control_v_Treatment_vox_p_tstat1_q0.005  # cluster correction folder              ├── 1-p_value_threshold.txt  # FDR adjusted 1-p value threshold for the uncorrected 1-p value map              ├── p_value_threshold.txt  # FDR adjusted p value threshold              ├── min_cluster_size_in_voxels.txt  # Often 100 voxels for c-Fos. For sparser signals like amyloid beta plaques, consider 400 or more              └── ..._rev_cluster_index.nii.gz # cluster map (index) with cluster IDs going from large to small (input for cstats_validation)           ├── ...
│           └── Control_v_Treatment_vox_p_tstat2_q0.05
├── cstats  # cluster stats (validation)   ├── Control_v_Treatment_vox_p_tstat1_q0.005
│      ├── Control_sample01_cell_density_data.csv
│      ├── Control_sample02_cell_density_data.csv
│      ├── Treatment_sample03_cell_density_data.csv
│      ├── Treatment_sample04_cell_density_data.csv
│      ├── _valid_clusters  # Contains data for sunburst plots, 3D brains, the xlsx table, etc.      ├── _valid_cluster_stats  # Contains t-test_results.csv for adding asterisks to the xlsx table, etc.      ├── _valid_clusters_prism  # For making valid cluster bar graphs with GraphPad Prism. Clusters are sorted by anatomy (refer to the xlsx table for annotating it)      └── p_value_threshold.txt
│   ├── 3D_brains  # Use these for vizualization in DSI Studio   ├── valid_clusters_tables_and_legend  # Use these .xlsx tables to make an SI table   ├── cluster_validation_summary_t-test.csv  # Use this to summarize cluster validation rates, etc.   ├── ...
│   └── Control_v_Treatment_vox_p_tstat2_q0.05
└── rstats # regional stats
    ├── Control_sample01_regional_cell_densities.csv
    ├── ...
    ├── Treatment_sample04_regional_cell_densities.csv
    ├── regional_cell_densities_all.csv
    └── Folders with plots for each region