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

added background subtraction #48

Open
wants to merge 24 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
3 changes: 0 additions & 3 deletions nextflow.config
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ params {
// Illumination correction
illumination = null

// Background subtraction
backsub = false

// MultiQC options
multiqc_config = null
multiqc_title = null
Expand Down
4 changes: 0 additions & 4 deletions nextflow_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@
"cellpose_model": {
"type": "string",
"description": "optional model file for cellpose segmentation"
},
"backsub": {
"type": "boolean",
"description": "boolean to flag whether or not to apply background subtraction"
}
}
},
Expand Down
23 changes: 23 additions & 0 deletions subworkflows/local/utils_nfcore_mcmicro_pipeline/main.nf
RobJY marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,29 @@ def validateInputMarkersheet( markersheet_data ) {
error("Duplicate [channel, cycle] pairs: ${dups}")
}

// validate backsub columns if present
def exposure_list = markersheet_data.findResults{ _1, _2, _3, _4, _5, _6, exposure, _8, _9 -> exposure != [] ? exposure : null }
RobJY marked this conversation as resolved.
Show resolved Hide resolved
def background_list = markersheet_data.findResults{ _1, _2, _3, _4, _5, _6, _7, background, _9 -> background != [] ? background: null }
def remove_list = markersheet_data.findResults{ _1, _2, _3, _4, _5, _6, _7, _8, remove -> remove != [] ? remove : null }

if (background_list.size() == 0 && (exposure_list.size() > 0 || remove_list.size() > 0)) {
RobJY marked this conversation as resolved.
Show resolved Hide resolved
error("No values in background column, but values in either exposure or remove columns. Must have background column values to perform background subtraction.")
} else if (background_list.size() > 0) {
inter_list = marker_name_list.intersect(background_list)
if (inter_list.size() != background_list.size()) {
outliers_list = background_list - inter_list
error('background column values must exist in the marker_name column. The following background column values do not exist in the marker_name column: ' + outliers_list)
}

if (exposure_list.size() == 0) {
error('You must have at least one value in the exposure column to perform background subtraction')
}

if (remove_list.size() == 0) {
error ('You must have at least one value in the remove column to perform background subtraction')
}
}

return markersheet_data
}

Expand Down
1 change: 0 additions & 1 deletion tests/main.nf.test
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,6 @@ nextflow_workflow {
when {
params {
segmentation = "mesmer"
backsub = true
}
workflow {
"""
Expand Down
90 changes: 75 additions & 15 deletions workflows/mcmicro.nf
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,80 @@ workflow MCMICRO {
| ASHLAR
ch_versions = ch_versions.mix(ASHLAR.out.versions)

// // Run Background Correction
if (params.backsub) {
ch_backsub_markers = ch_markersheet
.map { ['channel_number,cycle_number,marker_name,exposure,background,remove',
it.collect{ channel_number, cycle_number, marker_name, _1, _2, _3, exposure, background, remove ->
channel_number + "," + cycle_number + "," + marker_name + "," + exposure + "," + background + "," + remove}] }
.flatten()
.map { it.replace('[]', '') }
.collectFile(name: 'markers_backsub.csv', sort: false, newLine: true, storeDir: "${workDir}")
BACKSUB(ASHLAR.out.tif, [[id:"$ASHLAR.out.tif[0]['id']"], "${workDir}/markers_backsub.csv"])
ch_segmentation_input = BACKSUB.out.backsub_tif
ch_versions = ch_versions.mix(BACKSUB.out.versions)
} else {
ch_segmentation_input = ASHLAR.out.tif
}
// Run Background Correction
ASHLAR_OUT_BACKSUB = ch_markersheet
.map { it.collect{ _1, _2, _3, _4, _5, _6, exposure, background, remove -> [exposure, background, remove]} }
.flatten()
.filter { it != [] }
.toList()
.combine(ASHLAR.out.tif)
.map { it.size() == 2 ? [it[0] + [backsub: false], [it[1]]] : [it[-2] + [backsub: true], it[-1]] }

ch_backsub_markers = ch_markersheet
.map { ['channel_number,cycle_number,marker_name,exposure,background,remove',
it.collect{ channel_number, cycle_number, marker_name, _1, _2, _3, exposure, background, remove ->
channel_number + "," + cycle_number + "," + marker_name + "," + exposure + "," + background + "," + remove}] }
.flatten()
.map { it.replace('[]', '') }
.collectFile(name: 'markers_backsub.csv', sort: false, newLine: true)

BACKSUB_INPUT = ASHLAR_OUT_BACKSUB
.combine(ch_backsub_markers)
.dump(tag: 'BACKSUB IN')
.multiMap{ meta, image, marker ->
image: [meta, image]
markers: [meta, marker]
}

BACKSUB_INPUT_IMAGE = BACKSUB_INPUT.image
.branch{ meta, image ->
exists: meta.backsub
return [meta, image]
}

BACKSUB_INPUT_MARKERS = BACKSUB_INPUT.markers
.branch{ meta, markers ->
exists: meta.backsub
return [meta, markers]
}
BACKSUB(BACKSUB_INPUT_IMAGE.exists, BACKSUB_INPUT_MARKERS.exists)


/* seems like this should replace the blocks (BACKSUB_INPUT, BACKSUB_INPUT_IMAGE and BACKSUB_INPUT_MARKERS) above.
from https://github.com/nextflow-io/nextflow/issues/1208 it seems like this should work,
but as described using null fails
ASHLAR_OUT_BACKSUB
.combine(ch_backsub_markers)
.dump(tag: 'BACKSUB IN')
.multiMap{ meta, image, marker ->
// image: meta.backsub ? [meta, image] : Channel.empty()
// markers: meta.backsub ? [meta, marker] : Channel.empty()
image: meta.backsub ? [meta, image] : null
markers: meta.backsub ? [meta, marker] : null
// image: meta.backsub ? [meta, image] : []
// markers: meta.backsub ? [meta, marker] : []
}
| BACKSUB
*/

/* another idea to replace the blocks (BACKSUB_INPUT, BACKSUB_INPUT_IMAGE and BACKSUB_INPUT_MARKERS) above.
would like to have a single branch condition return 2 channel outputs,
but I don't see how to do it. Is something like this possible?
BACKSUB_INPUT_TEST = ASHLAR_OUT_BACKSUB
.combine(ch_backsub_markers)
.branch{ meta, image, markers ->
exists: meta.backsub
return [[meta1, image], [meta, markers]]
}
BACKSUB(BACKSUB_INPUT_TEST.exists)
*/

ch_segmentation_input = ASHLAR_OUT_BACKSUB
.concat(BACKSUB.out.backsub_tif)
.map { [it[-2], it[-1]] }
ch_versions = ch_versions.mix(BACKSUB.out.versions)



// Run Segmentation

Expand Down Expand Up @@ -119,6 +178,7 @@ workflow MCMICRO {
['marker_name'] +
it.collect{ _1, _2, marker_name, _4, _5, _6, _7, _8, _9 -> '"' + marker_name + '"' }
}
.dump(tag: "MARKERS")
.collectFile(name: 'markers.csv', sort: false, newLine: true)

ASHLAR.out.tif
Expand Down