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

#56 - make all preproc_t1 modules optional #73

Open
wants to merge 2 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
209 changes: 123 additions & 86 deletions subworkflows/nf-neuro/preproc_t1/main.nf
Original file line number Diff line number Diff line change
Expand Up @@ -24,104 +24,141 @@ workflow PREPROC_T1 {

ch_versions = Channel.empty()

// ** Denoising ** //
// Result : [ meta, image, mask | [] ]
// Steps :
// - join [ meta, image, mask | null ]
// - map [ meta, image, mask | [] ]
ch_nlmeans = ch_image
.join(ch_mask_nlmeans, remainder: true)
.map{ it[0..1] + [it[2] ?: []] }

DENOISING_NLMEANS ( ch_nlmeans )
ch_versions = ch_versions.mix(DENOISING_NLMEANS.out.versions.first())

// ** N4 correction ** //
// Result : [ meta, image, reference | [], mask | [] ]
// Steps :
// - join [ meta, image ] + [ reference, mask ] | [ reference, null ] | [ null ]
// - map [ meta, image, reference | [], mask | [] ]
// - join [ meta, image, reference | [], mask | [], nlmeans-mask | null ]
// - map [ meta, image, reference | [], mask | [] ]
ch_N4 = DENOISING_NLMEANS.out.image
.join(ch_ref_n4, remainder: true)
.map{ it[0..1] + [it[2] ?: [], it[3] ?: []] }
.join(ch_mask_nlmeans, remainder: true)
.map{ it[0..2] + [it[3] ?: it[4] ?: []] }

PREPROC_N4 ( ch_N4 )
ch_versions = ch_versions.mix(PREPROC_N4.out.versions.first())

// ** Resampling ** //
// Result : [ meta, image, reference | [] ]
// Steps :
// - join [ meta, image, reference | null ]
// - map [ meta, image, reference | [] ]
ch_resampling = PREPROC_N4.out.image
.join(ch_ref_resample, remainder: true)
.map{ it[0..1] + [it[2] ?: []] }

IMAGE_RESAMPLE ( ch_resampling )
ch_versions = ch_versions.mix(IMAGE_RESAMPLE.out.versions.first())

// ** Brain extraction ** //
if ( params.run_synthbet ) {
// ** SYNTHBET ** //
// Result : [ meta, image, weights | [] ]
if ( params.run_denoising ) {

// ** Denoising ** //
// Result : [ meta, image, mask | [] ]
// Steps :
// - join [ meta, image, weights | null ]
// - map [ meta, image, weights | [] ]
ch_bet = IMAGE_RESAMPLE.out.image
.join(ch_weights, remainder: true)
// - join [ meta, image, mask | null ]
// - map [ meta, image, mask | [] ]
ch_nlmeans = ch_image
.join(ch_mask_nlmeans, remainder: true)
.map{ it[0..1] + [it[2] ?: []] }

BETCROP_SYNTHBET ( ch_bet )
ch_versions = ch_versions.mix(BETCROP_SYNTHBET.out.versions.first())
DENOISING_NLMEANS ( ch_nlmeans )
ch_versions = ch_versions.mix(DENOISING_NLMEANS.out.versions.first())
image_nlmeans = DENOISING_NLMEANS.out.image
}

else {
image_nlmeans = ch_image
}

// ** Setting BET output ** //
image_bet = BETCROP_SYNTHBET.out.bet_image
mask_bet = BETCROP_SYNTHBET.out.brain_mask
if ( params.run_N4 ) {
// ** N4 correction ** //
// Result : [ meta, image, reference | [], mask | [] ]
// Steps :
// - join [ meta, image ] + [ reference, mask ] | [ reference, null ] | [ null ]
// - map [ meta, image, reference | [], mask | [] ]
// - join [ meta, image, reference | [], mask | [], nlmeans-mask | null ]
// - map [ meta, image, reference | [], mask | [] ]
ch_N4 = image_nlmeans
.join(ch_ref_n4, remainder: true)
.map{ it[0..1] + [it[2] ?: [], it[3] ?: []] }
.join(ch_mask_nlmeans, remainder: true)
.map{ it[0..2] + [it[3] ?: it[4] ?: []] }

PREPROC_N4 ( ch_N4 )
ch_versions = ch_versions.mix(PREPROC_N4.out.versions.first())
image_N4 = PREPROC_N4.out.image
}

else {
image_N4 = image_nlmeans
}

if ( params.run_resampling ) {
// ** Resampling ** //
// Result : [ meta, image, reference | [] ]
// Steps :
// - join [ meta, image, reference | null ]
// - map [ meta, image, reference | [] ]
ch_resampling = image_N4
.join(ch_ref_resample, remainder: true)
.map{ it[0..1] + [it[2] ?: []] }

IMAGE_RESAMPLE ( ch_resampling )
ch_versions = ch_versions.mix(IMAGE_RESAMPLE.out.versions.first())
image_resample = IMAGE_RESAMPLE.out.image
}
else {
// ** ANTSBET ** //
// The template and probability maps are mandatory if running antsBET. Since the
// error message from nextflow when they are absent is either non-informative or
// missing, we use ifEmpty to provide a more informative one.
ch_bet = IMAGE_RESAMPLE.out.image
.join(ch_template.ifEmpty{ error("ANTS BET needs a template") })
.join(ch_probability_map.ifEmpty{ error("ANTS BET needs a tissue probability map") })

BETCROP_ANTSBET ( ch_bet )
ch_versions = ch_versions.mix(BETCROP_ANTSBET.out.versions.first())

// ** Setting BET output ** //
image_bet = BETCROP_ANTSBET.out.t1
mask_bet = BETCROP_ANTSBET.out.mask
image_resample = image_N4
}

// ** Crop image ** //
ch_crop = image_bet
.map{ it + [[]] }
if ( params.run_bet ) {
// ** Brain extraction ** //
if ( params.run_synthbet ) {
// ** SYNTHBET ** //
// Result : [ meta, image, weights | [] ]
// Steps :
// - join [ meta, image, weights | null ]
// - map [ meta, image, weights | [] ]
ch_bet = image_resample
.join(ch_weights, remainder: true)
.map{ it[0..1] + [it[2] ?: []] }

BETCROP_SYNTHBET ( ch_bet )
ch_versions = ch_versions.mix(BETCROP_SYNTHBET.out.versions.first())

// ** Setting BET output ** //
image_bet = BETCROP_SYNTHBET.out.bet_image
mask_bet = BETCROP_SYNTHBET.out.brain_mask
}

else {
// ** ANTSBET ** //
// The template and probability maps are mandatory if running antsBET. Since the
// error message from nextflow when they are absent is either non-informative or
// missing, we use ifEmpty to provide a more informative one.
ch_bet = image_resample
.join(ch_template.ifEmpty{ error("ANTS BET needs a template") })
.join(ch_probability_map.ifEmpty{ error("ANTS BET needs a tissue probability map") })

BETCROP_ANTSBET ( ch_bet )
ch_versions = ch_versions.mix(BETCROP_ANTSBET.out.versions.first())

// ** Setting BET output ** //
image_bet = BETCROP_ANTSBET.out.t1
mask_bet = BETCROP_ANTSBET.out.mask
}
}
else{
image_bet = image_resample
mask_bet = Channel.empty()
}

if ( params.run_crop ) {
// ** Crop image ** //
ch_crop = image_bet
.map{ it + [[]] }

BETCROP_CROPVOLUME_T1 ( ch_crop )
ch_versions = ch_versions.mix(BETCROP_CROPVOLUME_T1.out.versions.first())
BETCROP_CROPVOLUME_T1 ( ch_crop )
ch_versions = ch_versions.mix(BETCROP_CROPVOLUME_T1.out.versions.first())
image_crop = BETCROP_CROPVOLUME_T1.out.image
bbox = BETCROP_CROPVOLUME_T1.out.bounding_box

// ** Crop mask ** //
ch_crop_mask = mask_bet
.join(BETCROP_CROPVOLUME_T1.out.bounding_box)
// ** Crop mask ** //
ch_crop_mask = mask_bet
.join(BETCROP_CROPVOLUME_T1.out.bounding_box)

BETCROP_CROPVOLUME_MASK ( ch_crop_mask )
ch_versions = ch_versions.mix(BETCROP_CROPVOLUME_MASK.out.versions.first())
BETCROP_CROPVOLUME_MASK ( ch_crop_mask )
ch_versions = ch_versions.mix(BETCROP_CROPVOLUME_MASK.out.versions.first())
mask_crop = BETCROP_CROPVOLUME_MASK.out.image
}
else{
image_crop = image_bet
mask_crop = []
bbox = []
}

emit:
t1_final = BETCROP_CROPVOLUME_T1.out.image // channel: [ val(meta), t1-preprocessed ]
mask_final = BETCROP_CROPVOLUME_MASK.out.image // channel: [ val(meta), t1-mask ]
image_nlmeans = DENOISING_NLMEANS.out.image // channel: [ val(meta), t1-after-denoise ]
image_N4 = PREPROC_N4.out.image // channel: [ val(meta), t1-after-unbias ]
image_resample = IMAGE_RESAMPLE.out.image // channel: [ val(meta), t1-after-resample ]
image_bet = image_bet // channel: [ val(meta), t1-after-bet ]
mask_bet = mask_bet // channel: [ val(meta), intermediary-mask ]
crop_box = BETCROP_CROPVOLUME_T1.out.bounding_box // channel: [ val(meta), bounding-box ]
versions = ch_versions // channel: [ versions.yml ]
t1_final = image_crop // channel: [ val(meta), t1-preprocessed ]
mask_final = mask_crop // channel: [ val(meta), t1-mask ]
image_nlmeans = image_nlmeans // channel: [ val(meta), t1-after-denoise ]
image_N4 = image_N4 // channel: [ val(meta), t1-after-unbias ]
image_resample = image_resample // channel: [ val(meta), t1-after-resample ]
image_bet = image_bet // channel: [ val(meta), t1-after-bet ]
mask_bet = mask_bet // channel: [ val(meta), intermediary-mask ]
crop_box = bbox // channel: [ val(meta), bounding-box ]
versions = ch_versions // channel: [ versions.yml ]
}
122 changes: 122 additions & 0 deletions subworkflows/nf-neuro/preproc_t1/tests/main.nf.test
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,126 @@ nextflow_workflow {
)
}
}

test("preproc_t1_quick") {
config "./nextflow_quick.config"

when {
workflow {
"""
ch_split_test_data = LOAD_DATA.out.test_data_directory
.branch{
antsbet: it.simpleName == "antsbet"
t1w: it.simpleName == "T1w"
}
input[0] = ch_split_test_data.t1w.map{
test_data_directory -> [
[ id:'test', single_end:false ],
file("\${test_data_directory}/T1w.nii.gz")
]}
input[1] = ch_split_test_data.antsbet.map{
test_data_directory -> [
[ id:'test', single_end:false ],
[]
]}
input[2] = ch_split_test_data.antsbet.map{
test_data_directory -> [
[ id:'test', single_end:false ],
[]
]}
input[3] = ch_split_test_data.antsbet.map{
test_data_directory -> [
[ id:'test', single_end:false ],
[],
[],
[]
]}
input[4] = ch_split_test_data.t1w.map{
test_data_directory -> [
[ id:'test', single_end:false ],
[],
[]
]}
input[5] = ch_split_test_data.antsbet.map{
test_data_directory -> [
[ id:'test', single_end:false ],
[]
]}
input[6] = ch_split_test_data.antsbet.map{
test_data_directory -> [
[ id:'test', single_end:false ],
[]
]}
"""
}
}

then {
assertAll(
{ assert workflow.success },
{ assert snapshot(workflow.out).match() }
)
}
}

test("preproc_t1_quick_bis") {
config "./nextflow_quick_bis.config"

when {
workflow {
"""
ch_split_test_data = LOAD_DATA.out.test_data_directory
.branch{
antsbet: it.simpleName == "antsbet"
t1w: it.simpleName == "T1w"
}
input[0] = ch_split_test_data.t1w.map{
test_data_directory -> [
[ id:'test', single_end:false ],
file("\${test_data_directory}/T1w.nii.gz")
]}
input[1] = ch_split_test_data.antsbet.map{
test_data_directory -> [
[ id:'test', single_end:false ],
[]
]}
input[2] = ch_split_test_data.antsbet.map{
test_data_directory -> [
[ id:'test', single_end:false ],
[]
]}
input[3] = ch_split_test_data.antsbet.map{
test_data_directory -> [
[ id:'test', single_end:false ],
[],
[],
[]
]}
input[4] = ch_split_test_data.t1w.map{
test_data_directory -> [
[ id:'test', single_end:false ],
[],
[]
]}
input[5] = ch_split_test_data.antsbet.map{
test_data_directory -> [
[ id:'test', single_end:false ],
[]
]}
input[6] = ch_split_test_data.antsbet.map{
test_data_directory -> [
[ id:'test', single_end:false ],
[]
]}
"""
}
}

then {
assertAll(
{ assert workflow.success },
{ assert snapshot(workflow.out).match() }
)
}
}
}
Loading
Loading