Skip to content

Commit

Permalink
almost working
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan DeKraker - B. Bernhardt Lab committed Feb 14, 2024
1 parent a637da9 commit 9b0496e
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 146 deletions.
2 changes: 1 addition & 1 deletion hippunfold/workflow/Snakefile
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ work = os.path.join(config["root"], "work")


include: "rules/common.smk"
include: "rules/downloads.smk"
include: "rules/preproc_t1.smk"
# include rules only as they are needed..
include: "rules/downloads.smk"


if config["modality"] == "segT1w" or config["modality"] == "segT2w":
Expand Down
79 changes: 31 additions & 48 deletions hippunfold/workflow/rules/downloads.smk
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
rule download_atlas:
params:
url=config["atlas_links_url"][config["atlas"]],
url=expand(config["atlas_links_url"][config["atlas"]]),
output:
model_zip=os.path.join(download_dir, config["atlas"] + ".zip"),
container:
Expand Down Expand Up @@ -31,52 +31,35 @@ rule download_template_shape:
"wget https://{params.url} -O {output.model_zip}"


def atlas_outs():
outs = []
for fn in config["atlas_files"][config["atlas"]]:
for hemi in ["L", "R"]:
outs.append(
download_dir
+ "/"
+ expand(config["atlas_files"][config["atlas"]][fn], hemi=hemi)[0]
)
return list(dict.fromkeys(outs))
def atlas_outs(wildcards):
outs = {}
for fn in config["atlas_files"][wildcards.atlas]:
outs[fn] = download_dir + "/" + expand(config["atlas_files"][wildcards.atlas][fn], hemi=wildcards.hemi)[0]
return outs


def template_outs():
outs = []
def template_outs(wildcards):
outs = {}
for fn in config["template_files"][config["template"]]:
for hemi in ["L", "R"]:
for dir in ["AP", "PD", "IO"]:
outs.append(
download_dir
+ "/"
+ expand(
config["template_files"][config["template"]][fn],
hemi=hemi,
dir=dir,
)[0]
)
return list(dict.fromkeys(outs))


def template_shape_outs():
outs = []
outs[fn] = download_dir + "/" + expand(
config["template_files"][config["template"]][fn],
hemi=hemi,
dir=dir,
modality=modality
)[0]
return outs


def template_shape_outs(wildcards):
outs = {}
for fn in config["template_files"][config["inject_template"]]:
for hemi in ["L", "R"]:
for dir in ["AP", "PD", "IO"]:
for autotop in ["hipp", "dentate"]:
outs.append(
download_dir
+ "/"
+ expand(
config["template_files"][config["inject_template"]][fn],
hemi=hemi,
dir=dir,
autotop=autotop,
)[0]
)
return list(dict.fromkeys(outs))
outs[fn] = download_dir + "/" + expand(
config["template_files"][config["inject_template"]][fn],
hemi=wildcards.hemi,
dir=wildcards.dir,
autotop=wildcards.autotop,
)[0]
return outs


rule unzip_template:
Expand All @@ -85,18 +68,18 @@ rule unzip_template:
params:
dir=os.path.join(download_dir, config["template"]),
output:
template_outs(),
unpack(template_outs),
shell:
"unzip {input.model_zip} -d {params.dir}"


rule unzip_atlas:
input:
model_zip=os.path.join(download_dir, config["atlas"] + ".zip"),
model_zip=os.path.join(download_dir, config[atlas] + ".zip"),
params:
dir=os.path.join(download_dir, config["atlas"]),
dir=os.path.join(download_dir, config[atlas]),
output:
atlas_outs(),
unpack(atlas_outs),
shell:
"unzip {input.model_zip} -d {params.dir}"

Expand All @@ -107,6 +90,6 @@ rule unzip_template_shape:
params:
dir=os.path.join(download_dir, config["inject_template"]),
output:
template_shape_outs(),
unpack(template_shape_outs)
shell:
"unzip {input.model_zip} -d {params.dir}"
34 changes: 8 additions & 26 deletions hippunfold/workflow/rules/gifti.smk
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ rule calculate_thickness_from_surface1:
rule metric_to_nii:
"""converts metric .gii files to .nii for use in ANTs"""
input:
expand(rules.unzip_atlas.output),
metric_gii=bids(
root=work,
datatype="surf",
Expand All @@ -462,10 +463,6 @@ rule metric_to_nii:
label="hipp",
**config["subj_wildcards"]
),
refflatnii=os.path.join(
download_dir,
config["atlas_files"]["multihist7"]["label_nii"],
),
params:
interp="-nearest-vertex 1",
output:
Expand All @@ -485,13 +482,14 @@ rule metric_to_nii:
group:
"subj"
shell:
"wb_command -metric-to-volume-mapping {input.metric_gii} {input.unfoldedsurf} {input.refflatnii} {output.metric_nii} {params.interp}"
"wb_command -metric-to-volume-mapping {input.metric_gii} {input.unfoldedsurf} {input.label_nii} {output.metric_nii} {params.interp}"


rule unfolded_registration:
"""performs 2D registration in unfolded space as in [reference paper]
this is done in a shadow directory to get rid of the tmp files generated by ants."""
input:
expand(rules.unzip_atlas.output),
thickness=bids(
root=work,
datatype="anat",
Expand Down Expand Up @@ -525,18 +523,6 @@ rule unfolded_registration:
label="hipp",
**config["subj_wildcards"]
),
refthickness=lambda wildcards: os.path.join(
download_dir,
config["atlas_files"][wildcards.atlas]["thick"],
),
refcurvature=lambda wildcards: os.path.join(
download_dir,
config["atlas_files"][wildcards.atlas]["curv"],
),
refgyrification=lambda wildcards: os.path.join(
download_dir,
config["atlas_files"][wildcards.atlas]["gyr"],
),
params:
antsparams="-d 2 -t so",
outsuffix="tmp",
Expand Down Expand Up @@ -589,7 +575,7 @@ rule unfolded_registration:
mem_mb=16000,
time=10,
shell:
"antsRegistrationSyNQuick.sh {params.antsparams} -f {input.refthickness} -f {input.refcurvature} -f {input.refgyrification} -m {input.thickness} -m {input.curvature} -m {input.gyrification} -o {params.outsuffix} &> {log} && "
"antsRegistrationSyNQuick.sh {params.antsparams} -f {input.thick} -f {input.curv} -f {input.gyr} -m {input.thickness} -m {input.curvature} -m {input.gyrification} -o {params.outsuffix} &> {log} && "
"cp {params.warpfn} {output.warp} && "
"cp {params.invwarpfn} {output.invwarp}"

Expand Down Expand Up @@ -1053,9 +1039,8 @@ rule calculate_thickness_from_surface2:
rule resample_atlas_to_refvol:
"""this is just done in case the atlas has a different unfolded config than the current run"""
input:
atlas=os.path.join(
download_dir, config["atlas_files"][config["atlas"]]["label_nii"]
),
expand(rules.unzip_atlas.output),
unpack(atlas_outs),
refvol=bids(
root=root,
space="unfold",
Expand Down Expand Up @@ -1090,11 +1075,12 @@ rule resample_atlas_to_refvol:
group:
"subj"
shell:
"antsApplyTransforms -d 3 -n MultiLabel -i {input.atlas} -r {input.refvol} -o {output.label_nii} -v &> {log}"
"antsApplyTransforms -d 3 -n MultiLabel -i {input.label_nii} -r {input.refvol} -o {output.label_nii} -v &> {log}"


rule nii_to_label_gii:
input:
expand(rules.unzip_atlas.output),
label_nii=bids(
root=work,
datatype="anat",
Expand All @@ -1112,10 +1098,6 @@ rule nii_to_label_gii:
"unfold_template_hipp",
"tpl-avg_space-unfold_den-{density}_midthickness.surf.gii",
),
label_list=lambda wildcards: os.path.join(
download_dir,
config["atlas_files"][wildcards.atlas]["label_list"],
),
params:
structure_type=lambda wildcards: hemi_to_structure[wildcards.hemi],
output:
Expand Down
31 changes: 4 additions & 27 deletions hippunfold/workflow/rules/nnunet.smk
Original file line number Diff line number Diff line change
Expand Up @@ -198,28 +198,9 @@ rule unflip_nnunet_nii:
" {input.unflip_ref} -push FLIPPED -copy-transform -o {output.nnunet_seg} "


def get_f3d_ref(wildcards):
if config["modality"] == "T2w":
nii = (
os.path.join(
download_dir,
config["template_files"][config["template"]]["crop_ref"],
),
)
elif config["modality"] == "T1w":
nii = (
os.path.join(
download_dir,
config["template_files"][config["template"]]["crop_refT1w"],
),
)
else:
raise ValueError("modality not supported for nnunet!")
return nii


rule qc_nnunet_f3d:
input:
expand(rules.unzip_template.output),
img=(
bids(
root=work,
Expand All @@ -240,7 +221,6 @@ rule qc_nnunet_f3d:
space="corobl",
hemi="{hemi}"
),
ref=get_f3d_ref,
output:
cpp=bids(
root=work,
Expand Down Expand Up @@ -283,12 +263,13 @@ rule qc_nnunet_f3d:
group:
"subj"
shell:
"reg_f3d -flo {input.img} -ref {input.ref} -res {output.res} -cpp {output.cpp} &> {log} && "
"reg_resample -flo {input.seg} -cpp {output.cpp} -ref {input.ref} -res {output.res_mask} -inter 0 &> {log}"
"reg_f3d -flo {input.img} -ref {input.crop_ref} -res {output.res} -cpp {output.cpp} &> {log} && "
"reg_resample -flo {input.seg} -cpp {output.cpp} -ref {input.crop_ref} -res {output.res_mask} -inter 0 &> {log}"


rule qc_nnunet_dice:
input:
expand(rules.unzip_template.output),
res_mask=bids(
root=work,
datatype="anat",
Expand All @@ -298,10 +279,6 @@ rule qc_nnunet_dice:
space="template",
hemi="{hemi}"
),
ref=os.path.join(
download_dir,
config["template_files"][config["template"]]["Mask_crop"],
),
params:
hipp_lbls=[1, 2, 7, 8],
output:
Expand Down
Loading

0 comments on commit 9b0496e

Please sign in to comment.