Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Module] Freesurfer registration/convert #161

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions modules/nf-scil/registration/convert/environment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
name: "registration_convert"
channels:
- Docker
- Apptainer
dependencies:
- "Freesurfer"
90 changes: 90 additions & 0 deletions modules/nf-scil/registration/convert/main.nf
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
process REGISTRATION_CONVERT {
tag "$meta.id"
label 'process_single'

container "freesurfer/freesurfer:7.4.1"
containerOptions "--entrypoint ''"
containerOptions "--env FSLOUTPUTTYPE='NIFTI_GZ'"

input:
tuple val(meta), path(affine), path(deform), path(source), path(target) /* optional, value = [] */, path(fs_license) /* optional, value = [] */

output:
tuple val(meta), path("*.{txt,lta,mat,dat}"), emit: init_transform
tuple val(meta), path("*.{nii,nii.gz,mgz,m3z}"), emit: deform_transform
path "versions.yml" , emit: versions

when:
task.ext.when == null || task.ext.when

script:
def args = task.ext.args ?: ''
def prefix = task.ext.prefix ?: "${meta.id}"

//For arguments definition, lta_convert -h
def invert = task.ext.invert ? "--invert" : ""
def source_geometry_init = "$source" ? "--src " + "$source" : ""
def target_geometry_init = "$target" ? "--trg " + "$target" : ""
def in_format_init = task.ext.in_format_init ? "--in" + task.ext.in_format_init + " " + "$affine" : "--inlta " + "$affine"
def out_format_init = task.ext.out_format_init ? "--out" + task.ext.out_format_init : "--outitk"

//For arguments definition, mri_warp_convert -h
def source_geometry_deform = "$source" ? "--insrcgeom " + "$source" : ""
def in_format_deform = task.ext.in_format_deform ? "--in" + task.ext.in_format_deform + " " + "$deform" : "--inras " + "$deform"
def out_format_deform = task.ext.out_format_deform ? "--out" + task.ext.out_format_deform : "--outitk"
def downsample = task.ext.downsample ? "--downsample" : ""

"""
export ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS=$task.cpus
export OMP_NUM_THREADS=1
export OPENBLAS_NUM_THREADS=1

cp $fs_license \$FREESURFER_HOME/license.txt

declare -A affine_dictionnary=( ["--outlta"]="lta" \
["--outfsl"]="mat" \
["--outmni"]="xfm" \
["--outreg"]="dat" \
["--outniftyreg"]="txt" \
["--outitk"]="txt" \
["--outvox"]="txt" )

ext_affine=\${affine_dictionnary[${out_format_init}]}

declare -A deform_dictionnary=( ["--outm3z"]="m3z" \
["--outfsl"]="nii.gz" \
["--outlps"]="nii.gz" \
["--outitk"]="nii.gz" \
["--outras"]="nii.gz" \
["--outvox"]="mgz" )

ext_deform=\${deform_dictionnary[${out_format_deform}]}

lta_convert ${invert} ${source_geometry_init} ${target_geometry_init} ${in_format_init} ${out_format_init} ${prefix}__init_warp.\${ext_affine}
mri_warp_convert ${source_geometry_deform} ${downsample} ${in_format_deform} ${out_format_deform} ${prefix}__deform_warp.\${ext_deform}

rm \$FREESURFER_HOME/license.txt

cat <<-END_VERSIONS > versions.yml
"${task.process}":
Freesurfer: 7.4.1
END_VERSIONS
"""

stub:
def args = task.ext.args ?: ''
def prefix = task.ext.prefix ?: "${meta.id}"

"""
lta_convert -h
mri_warp_convert -h

touch ${prefix}__init_transform.txt
touch ${prefix}__deform_transform.nii.gz

cat <<-END_VERSIONS > versions.yml
"${task.process}":
Freesurfer: 7.4.1
END_VERSIONS
"""
}
69 changes: 69 additions & 0 deletions modules/nf-scil/registration/convert/meta.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
name: "registration_convert"
description: Freesurfer transform conversion tool. Default usage is aim at receiving freesurfer format and converting to ANTs (ITK). See lta_convert --help and mri_warp_convert --help for alternatives.
keywords:
- Registration
- Brain imaging
- MRI
tools:
- "Freesurfer":
description: "Freesurfer lta_convert (affine conversion) and mri_warp_convert (deform) tools for transform conversion"
homepage: "https://surfer.nmr.mgh.harvard.edu/"
documentation: "https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferWiki"

input:
- meta:
type: map
description: |
Groovy Map containing sample information
e.g. `[ id:'test', single_end:false ]`

- affine:
type: file
description: Affine transform to convert. Default usage expects Freesurfer .lta format from mri_synthmorph
pattern: "*.{lta,txt,fslmat,xfm,dat}"

- deform:
type: file
description: Deform transform to convert. Default usage expects Freesurfer .mgz format from mri_synthmorph
pattern: "*.{nii,nii.gz,mgz,m3z}"

- source:
type: file
description: Moving Nifti volume used for registration. Defines source image geometry
pattern: "*.{nii,nii.gz}"

- target:
type: file
description: Fixed Nifti volume used for registration. Defines target image geometry. (optional)
pattern: "*.{nii,nii.gz}"

- fs_license:
type: file
description: The path to your FreeSurfer license. To get one, go to https://surfer.nmr.mgh.harvard.edu/registration.html. Optional. If you have already set your license as prescribed by Freesurfer (copied to a .license file in your $FREESURFER_HOME), this is not required.
pattern: "*.txt"

output:
- meta:
type: map
description: |
Groovy Map containing sample information
e.g. `[ id:'test', single_end:false ]`

- init_transform:
type: file
description: Init transform. Default usage outputs ANTs (ITK) format .txt
pattern: "*.{txt,lta,mat,dat}"

- deform_transform:
type: file
description: Deform transform. Default usage outputs ANTs (ITK) format .nii.gz
pattern: "*.{nii,nii.gz,mgz,m3z}"

- versions:
type: file
description: File containing software versions
pattern: "versions.yml"

authors:
- "@anroy1"
Loading
Loading