Skip to content

Commit

Permalink
merged spec files, fix for copying results
Browse files Browse the repository at this point in the history
also moves refvol to work folder, so should have no warps directory now (as
it is not really needed)..
  • Loading branch information
akhanf committed Jan 10, 2025
1 parent c9ac98b commit 3cd2053
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 74 deletions.
38 changes: 5 additions & 33 deletions hippunfold/workflow/rules/common.smk
Original file line number Diff line number Diff line change
Expand Up @@ -48,36 +48,11 @@ def get_final_spec():
datatype="surf",
den="{density}",
space="{space}",
hemi="{hemi}",
label="{autotop}",
suffix="surfaces.spec",
**inputs.subj_wildcards,
),
density=config["output_density"],
space=ref_spaces,
hemi=config["hemi"],
autotop=config["autotop_labels"],
surfname=config["surf_types"]["hipp"],
allow_missing=True,
)
)
specs.extend(
inputs[config["modality"]].expand(
bids(
root=root,
datatype="surf",
den="{density}",
space="{space}",
hemi="{hemi}",
label="{autotop}",
suffix="surfaces.spec",
**inputs.subj_wildcards,
),
density=config["output_density"],
space=ref_spaces,
hemi=config["hemi"],
autotop=config["autotop_labels"],
surfname=config["surf_types"]["dentate"],
allow_missing=True,
)
)
Expand All @@ -87,15 +62,10 @@ def get_final_spec():
root=root,
datatype="surf",
space="{space}",
hemi="{hemi}",
label="{autotop}",
suffix="surfaces.spec",
**inputs.subj_wildcards,
),
space="corobl",
hemi=config["hemi"],
autotop=config["autotop_labels"],
surfname=config["surf_types"]["hipp"],
allow_missing=True,
)
)
Expand Down Expand Up @@ -306,7 +276,7 @@ if "corobl" in ref_spaces:
input:
os.path.join(work, "{pre}_space-corobl_{post}{suffix}.{ext}"),
output:
os.path.join(root, "{pre}_space-corobl_{post}{suffix,coords}.{ext}"),
os.path.join(root, "{pre,[^/].+}_space-corobl_{post}{suffix,coords}.{ext}"),
group:
"subj"
shell:
Expand All @@ -316,7 +286,9 @@ if "corobl" in ref_spaces:
input:
os.path.join(work, "{pre}_{fromto,from|to}-corobl_{post}{suffix}.{ext}"),
output:
os.path.join(root, "{pre}_{fromto,from|to}-corobl_{post}{suffix,xfm}.{ext}"),
os.path.join(
root, "{pre,[^/].+}_{fromto,from|to}-corobl_{post}{suffix,xfm}.{ext}"
),
group:
"subj"
shell:
Expand All @@ -326,7 +298,7 @@ if "corobl" in ref_spaces:
input:
os.path.join(work, "{pre}_desc-subfields_{post}{suffix}.{ext}"),
output:
os.path.join(root, "{pre}_desc-subfields_{post}{suffix,dseg}.{ext}"),
os.path.join(root, "{pre,[^/].+}_desc-subfields_{post}{suffix,dseg}.{ext}"),
group:
"subj"
shell:
Expand Down
58 changes: 49 additions & 9 deletions hippunfold/workflow/rules/gifti.smk
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
# lookup tables for structure:
hemi_to_structure = {"L": "CORTEX_LEFT", "R": "CORTEX_RIGHT"}


# lookup mappings for structure and surface type:
def get_structure(hemi, label):
if label == "hipp":
if hemi == "L":
Expand Down Expand Up @@ -123,7 +120,7 @@ rule constrain_surf_to_bbox:
**inputs.subj_wildcards
),
ref_nii=bids(
root=root,
root=work,
datatype="warps",
space="unfold",
label="{autotop}",
Expand Down Expand Up @@ -335,7 +332,7 @@ rule create_dlabel_cifti_subfields:
def get_cmd_spec_file(wildcards, input, output):
specfile = output.spec_file
if "hemi" in wildcards._names:
structure = hemi_to_structure[wildcards.hemi]
structure = get_structure(wildcards.hemi, wildcards.label)
else:
structure = "INVALID"
cmds = list()
Expand Down Expand Up @@ -519,7 +516,14 @@ rule create_spec_file_dentate:
"{params.cmds}"


rule merge_lr_spec_file:
def get_cmd_merge_spec(wildcards, input, output):
if len(input.spec_files) == 1:
return f"cp {input} {output}"
else:
return f"wb_command -spec-file-merge {input.spec_files} {output}"


rule merge_lr_spec_file_native:
input:
spec_files=expand(
bids(
Expand All @@ -532,9 +536,11 @@ rule merge_lr_spec_file:
label="{autotop}",
**inputs.subj_wildcards
),
hemi=["L", "R"],
hemi=config["hemi"],
allow_missing=True,
),
params:
cmd=get_cmd_merge_spec,
output:
spec_file=bids(
root=root,
Expand All @@ -550,4 +556,38 @@ rule merge_lr_spec_file:
group:
"subj"
shell:
"wb_command -spec-file-merge {input.spec_files} {output}"
"{params.cmd}"


rule merge_hipp_dentate_spec_file_native:
input:
spec_files=expand(
bids(
root=root,
datatype="surf",
den="{density}",
suffix="surfaces.spec",
space="{space}",
label="{autotop}",
**inputs.subj_wildcards
),
autotop=config["autotop_labels"],
allow_missing=True,
),
params:
cmd=get_cmd_merge_spec,
output:
spec_file=bids(
root=root,
datatype="surf",
den="{density}",
space="{space}",
suffix="surfaces.spec",
**inputs.subj_wildcards
),
container:
config["singularity"]["autotop"]
group:
"subj"
shell:
"{params.cmd}"
118 changes: 90 additions & 28 deletions hippunfold/workflow/rules/native_surf.smk
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,7 @@ rule pad_unfold_ref:
for this)."""
input:
ref_nii=bids(
root=root,
root=work,
space="unfold",
label="{label}",
datatype="warps",
Expand Down Expand Up @@ -1360,33 +1360,29 @@ rule convert_unfoldreg_warp_from_itk_to_world:
"wb_command -convert-warpfield -from-itk {input} -to-world {output}"


def get_unfold_ref(wildcards):
"""function to return either unfoldreg or unfold ref mesh, depending on whether
unfoldreg can be performed (based on atlas wildcards)"""

def get_unfold_ref_name(wildcards):
if (
wildcards.label in config["atlas_files"][config["atlas"]]["label_wildcards"]
and config["no_unfolded_reg"] == False
):
return bids(
root=root,
datatype="surf",
suffix="midthickness.surf.gii",
space="unfoldreg",
hemi="{hemi}",
label="{label}",
**inputs.subj_wildcards
)
return "unfoldreg"
else:
return bids(
root=root,
datatype="surf",
suffix="midthickness.surf.gii",
space="unfold",
hemi="{hemi}",
label="{label}",
**inputs.subj_wildcards
)
return "unfold"


def get_unfold_ref(wildcards):
"""function to return either unfoldreg or unfold ref mesh, depending on whether
unfoldreg can be performed (based on atlas wildcards)"""

return bids(
root=root,
datatype="surf",
suffix="midthickness.surf.gii",
space=get_unfold_ref_name(wildcards),
hemi="{hemi}",
label="{label}",
**inputs.subj_wildcards
)


rule warp_unfold_native_to_unfoldreg:
Expand Down Expand Up @@ -1623,7 +1619,7 @@ rule atlas_label_to_unfold_nii:
input:
atlas_dir=lambda wildcards: Path(download_dir) / "atlas" / wildcards.atlas,
ref_nii=bids(
root=root,
root=work,
space="unfold",
label="{label}",
datatype="warps",
Expand Down Expand Up @@ -1805,7 +1801,7 @@ rule create_spec_file_hipp_native:
atlas=config["atlas"],
allow_missing=True,
),
surfs=expand(
surfs=lambda wildcards: expand(
bids(
root=root,
datatype="surf",
Expand All @@ -1816,7 +1812,7 @@ rule create_spec_file_hipp_native:
**inputs.subj_wildcards
),
surfname=["midthickness"],
space=["{space}", "unfold", "unfoldreg"],
space=["{space}", get_unfold_ref_name(wildcards)],
allow_missing=True,
),
cifti_metrics=lambda wildcards: inputs[config["modality"]].expand(
Expand Down Expand Up @@ -1879,7 +1875,7 @@ rule create_spec_file_dentate_native:
metric=get_gifti_metric_types(wildcards.label),
allow_missing=True,
),
surfs=expand(
surfs=lambda wildcards: expand(
bids(
root=root,
datatype="surf",
Expand All @@ -1890,7 +1886,7 @@ rule create_spec_file_dentate_native:
**inputs.subj_wildcards
),
surfname=["midthickness"],
space=["{space}", "unfold", "unfoldreg"],
space=["{space}", get_unfold_ref_name(wildcards)],
allow_missing=True,
),
cifti_metrics=lambda wildcards: inputs[config["modality"]].expand(
Expand Down Expand Up @@ -1925,6 +1921,72 @@ rule create_spec_file_dentate_native:
"{params.cmds}"


rule merge_lr_spec_file:
input:
spec_files=expand(
bids(
root=root,
datatype="surf",
suffix="surfaces.spec",
hemi="{hemi}",
space="{space}",
label="{autotop}",
**inputs.subj_wildcards
),
hemi=config["hemi"],
allow_missing=True,
),
params:
cmd=get_cmd_merge_spec,
output:
spec_file=bids(
root=root,
datatype="surf",
space="{space}",
suffix="surfaces.spec",
label="{autotop}",
**inputs.subj_wildcards
),
container:
config["singularity"]["autotop"]
group:
"subj"
shell:
"{params.cmd}"


rule merge_hipp_dentate_spec_file:
input:
spec_files=expand(
bids(
root=root,
datatype="surf",
suffix="surfaces.spec",
space="{space}",
label="{autotop}",
**inputs.subj_wildcards
),
autotop=config["autotop_labels"],
allow_missing=True,
),
params:
cmd=get_cmd_merge_spec,
output:
spec_file=bids(
root=root,
datatype="surf",
space="{space}",
suffix="surfaces.spec",
**inputs.subj_wildcards
),
container:
config["singularity"]["autotop"]
group:
"subj"
shell:
"{params.cmd}"


rule cp_native_surf_to_root:
input:
native=bids(
Expand Down
8 changes: 4 additions & 4 deletions hippunfold/workflow/rules/warps.smk
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ rule create_unfold_ref:
orient=lambda wildcards: config["unfold_vol_ref"][wildcards.autotop]["orient"],
output:
nii=bids(
root=root,
root=work,
space="unfold",
label="{autotop}",
datatype="warps",
Expand All @@ -261,7 +261,7 @@ rule create_unfold_ref:
rule create_unfold_coord_map:
input:
nii=bids(
root=root,
root=work,
space="unfold",
label="{autotop}",
datatype="warps",
Expand Down Expand Up @@ -316,7 +316,7 @@ def get_laminar_coords(wildcards):
rule create_warps_hipp:
input:
unfold_ref_nii=bids(
root=root,
root=work,
space="unfold",
label="hipp",
datatype="warps",
Expand Down Expand Up @@ -409,7 +409,7 @@ rule create_warps_hipp:
rule create_warps_dentate:
input:
unfold_ref_nii=bids(
root=root,
root=work,
space="unfold",
label="dentate",
datatype="warps",
Expand Down

0 comments on commit 3cd2053

Please sign in to comment.