Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions spimquant/config/snakebids.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ analysis_levels: &analysis_levels
#mapping from analysis_level to set of target rules or files
targets_by_analysis_level:
participant:
- '' # if '', then the first rule is run
- 'all_participant' # if '', then the first rule is run
group:
- 'all_group_stats'
- 'all_group'

# Configure components:
# Each entry creates a new component that can be retreived within the workflow
Expand Down
164 changes: 90 additions & 74 deletions spimquant/workflow/Snakefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,29 @@ inputs = generate_inputs(
)


# get stains from first subject (lazy - should use the correct stain for each subject, when defining target rules)
# to do this, would need to get the channel names in the function that gets the target rules
stains = ZarrNii.from_ome_zarr(inputs["spim"].expand()[0]).list_channels()
include: "rules/common.smk"


stains = get_stains_all_subjects()

stain_for_reg = None
for stain in config["stains_for_reg"]:
if stain in stains:
stain_for_reg = stain
break

if stain_for_reg == None:
raise ValueError(
"a stain for registration was not found in the first OME zarr file"
)
if stain_for_reg is None:
raise ValueError(f"a stain for registration was not found in {stains}")


stains_for_seg = list(set(config["stains_for_seg"]).intersection(set(stains)))

do_coloc = len(stains_for_seg) > 1

# atlas segmentations to use
all_atlas_segs = config["templates"][config["template"]]["atlases"].keys()

if config["atlas_segs"] == None:
if config["atlas_segs"] is None:
atlas_segs = all_atlas_segs
else:
atlas_segs = []
Expand Down Expand Up @@ -106,7 +108,7 @@ rule all_templatereg_deform_zooms:
space="{template}",
stain="{stain}",
res="{res}um",
suffix="SPIM.nii",
suffix="SPIM.nii.gz",
**inputs["spim"].wildcards,
),
template=config["template"],
Expand All @@ -124,7 +126,7 @@ rule all_templatereg_deform:
desc="deform",
space="{template}",
stain="{stain}",
suffix="SPIM.nii",
suffix="SPIM.nii.gz",
**inputs["spim"].wildcards,
),
template=config["template"],
Expand Down Expand Up @@ -156,7 +158,7 @@ rule all_fieldfrac_tune:
stain="{stain}",
level="{level}",
desc="otsu+k{k}i{i}",
suffix="fieldfrac.nii",
suffix="fieldfrac.nii.gz",
**inputs["spim"].wildcards,
),
stain=stains_for_seg,
Expand All @@ -175,7 +177,7 @@ rule all_segment:
seg="{seg}",
space="{template}",
desc="{desc}",
suffix="{stain}+{suffix}.nii",
suffix="{stain}+{suffix}.nii.gz",
**inputs["spim"].wildcards,
),
seg=atlas_segs,
Expand All @@ -184,29 +186,14 @@ rule all_segment:
suffix=config["seg_metrics"],
stain=stains_for_seg,
),
inputs["spim"].expand(
bids(
root=root,
datatype="micr",
seg="{seg}",
space="{template}",
desc="{desc}",
suffix="coloc+{suffix}.nii",
**inputs["spim"].wildcards,
),
seg=atlas_segs,
desc=config["seg_method"],
template=config["template"],
suffix=config["coloc_seg_metrics"],
),
inputs["spim"].expand(
bids(
root=root,
datatype="micr",
stain="{stain}",
level="{level}",
space="{template}",
suffix="SPIM.nii",
suffix="SPIM.nii.gz",
**inputs["spim"].wildcards,
),
stain=stains_for_seg + [stain_for_reg],
Expand All @@ -221,7 +208,7 @@ rule all_segment:
level="{level}",
desc="{desc}",
space="{template}",
suffix="fieldfrac.nii",
suffix="fieldfrac.nii.gz",
**inputs["spim"].wildcards,
),
stain=stains_for_seg,
Expand Down Expand Up @@ -254,7 +241,7 @@ rule all_segment:
stain="{stain}",
level="{level}",
desc="{desc}",
suffix="counts.nii",
suffix="counts.nii.gz",
**inputs["spim"].wildcards,
),
stain=stains_for_seg,
Expand All @@ -268,7 +255,7 @@ rule all_segment:
stain="{stain}",
space="{template}",
desc="{desc}",
suffix="counts.nii",
suffix="counts.nii.gz",
**inputs["spim"].wildcards,
),
stain=stains_for_seg,
Expand All @@ -279,24 +266,43 @@ rule all_segment:
bids(
root=root,
datatype="micr",
desc="{desc}",
stain="{stain}",
space="{template}",
suffix="regqc.html",
**inputs["spim"].wildcards,
),
stain=stain_for_reg,
template=config["template"],
),


rule all_segment_coloc:
input:
inputs["spim"].expand(
bids(
root=root,
datatype="micr",
seg="{seg}",
space="{template}",
suffix="coloccounts.nii",
desc="{desc}",
suffix="coloc+{suffix}.nii.gz",
**inputs["spim"].wildcards,
),
seg=atlas_segs,
desc=config["seg_method"],
template=config["template"],
suffix=config["coloc_seg_metrics"],
),
inputs["spim"].expand(
bids(
root=root,
datatype="micr",
stain="{stain}",
desc="{desc}",
space="{template}",
suffix="regqc.html",
suffix="coloccounts.nii.gz",
**inputs["spim"].wildcards,
),
stain=stain_for_reg,
desc=config["seg_method"],
template=config["template"],
),

Expand Down Expand Up @@ -375,14 +381,6 @@ rule all_imaris_crops:
),


rule all:
default_target: True
input:
rules.all_segment.input,
rules.all_spim_patches.input,
rules.all_mri_reg.input if config["register_to_mri"] else [],


rule all_group_stats:
"""Target rule for group-level statistical analysis."""
input:
Expand Down Expand Up @@ -421,62 +419,73 @@ rule all_group_stats:
bids(
root=root,
datatype="group",
seg="{seg}",
level="{level}",
space="{template}",
desc="{desc}",
metric="coloc+{metric}",
suffix="{stat}.nii.gz",
suffix="{stain}+count.nii.gz",
),
seg=atlas_segs,
desc=config["seg_method"],
template=config["template"],
metric=config["coloc_seg_metrics"],
stat=config["stats_maps"],
level=range(4),
stain=stains_for_seg,
),
# Add contrast-filtered outputs
expand(
bids(
root=root,
datatype="group",
level="{level}",
space="{template}",
desc="{desc}",
suffix="{stain}+count.nii",
contrast="{contrast_column}+{contrast_value}",
suffix="{stain}+count.nii.gz",
),
desc=config["seg_method"],
template=config["template"],
level=range(4),
stain=stains_for_seg,
contrast_column=config["contrast_column"],
contrast_value=config["contrast_values"],
),
# Add group-averaged segstats maps by contrast
expand(
bids(
root=root,
datatype="group",
seg="{seg}",
space="{template}",
level="{level}",
desc="{desc}",
suffix="coloccount.nii",
contrast="{contrast_column}+{contrast_value}",
metric="{stain}+{metric}",
suffix="groupavg.nii.gz",
),
seg=atlas_segs,
desc=config["seg_method"],
template=config["template"],
level=range(4),
stain=stains_for_seg,
metric=config["seg_metrics"],
contrast_column=config["contrast_column"],
contrast_value=config["contrast_values"],
),
# Add contrast-filtered outputs


rule all_group_stats_coloc:
input:
expand(
bids(
root=root,
datatype="group",
level="{level}",
seg="{seg}",
space="{template}",
desc="{desc}",
contrast="{contrast_column}+{contrast_value}",
suffix="{stain}+count.nii",
metric="coloc+{metric}",
suffix="{stat}.nii.gz",
),
seg=atlas_segs,
desc=config["seg_method"],
template=config["template"],
level=range(4),
stain=stains_for_seg,
contrast_column=config["contrast_column"],
contrast_value=config["contrast_values"],
metric=config["coloc_seg_metrics"],
stat=config["stats_maps"],
),
expand(
bids(
Expand All @@ -485,32 +494,25 @@ rule all_group_stats:
space="{template}",
level="{level}",
desc="{desc}",
contrast="{contrast_column}+{contrast_value}",
suffix="coloccount.nii",
suffix="coloccount.nii.gz",
),
desc=config["seg_method"],
template=config["template"],
level=range(4),
contrast_column=config["contrast_column"],
contrast_value=config["contrast_values"],
),
# Add group-averaged segstats maps by contrast
expand(
bids(
root=root,
datatype="group",
seg="{seg}",
space="{template}",
level="{level}",
desc="{desc}",
contrast="{contrast_column}+{contrast_value}",
metric="{stain}+{metric}",
suffix="groupavg.nii.gz",
suffix="coloccount.nii.gz",
),
seg=atlas_segs,
desc=config["seg_method"],
template=config["template"],
stain=stains_for_seg,
metric=config["seg_metrics"],
level=range(4),
contrast_column=config["contrast_column"],
contrast_value=config["contrast_values"],
),
Expand All @@ -534,7 +536,21 @@ rule all_group_stats:
),


include: "rules/common.smk"
rule all_participant:
default_target: True
input:
rules.all_segment.input,
rules.all_spim_patches.input,
rules.all_mri_reg.input if config["register_to_mri"] else [],
rules.all_segment_coloc.input if do_coloc else [],


rule all_group:
input:
rules.all_group_stats.input,
rules.all_group_stats_coloc.input if do_coloc else [],


include: "rules/import.smk"
include: "rules/masking.smk"
include: "rules/templatereg.smk"
Expand Down
12 changes: 12 additions & 0 deletions spimquant/workflow/rules/common.smk
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,15 @@ def get_template_for_reg(wildcards):
return get_template_path(root, wildcards.template, config["template_crop"])
else:
return bids_tpl(root=root, template=wildcards.template, suffix="anat.nii.gz")


def get_stains_all_subjects():

stain_sets = [
set(ZarrNii.from_ome_zarr(zarr).list_channels())
for zarr in inputs["spim"].expand()
]
if all(s == stain_sets[0] for s in stain_sets):
return list(stain_sets[0])
else:
raise ValueError(f"stains across subjects are not consistent, {stain_sets}")
Loading