-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnf_workflow.nf
230 lines (188 loc) · 8.62 KB
/
nf_workflow.nf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env nextflow
nextflow.enable.dsl=2
include { LEARN_TRANSFORM; APPLY_TRANSFORM; NORMALIZE } from './modules/registration.nf'
include { REGISTER_QC } from './modules/register_qc.nf'
include { TILING } from './modules/tiler.nf'
include { TILE_SIZE_ESTIMATOR } from './modules/tile_size_estimator.nf'
include { SPACETX } from './modules/spacetx.nf'
include { JOIN_JSON } from './modules/join_json.nf'
include { MAKE_EXP_JSON } from './modules/experiment_json.nf'
include { SPOT_FINDER as SPOT_FINDER_1 } from './modules/decoding.nf'
include { SPOT_FINDER as SPOT_FINDER_2 } from './modules/decoding.nf'
include { TILE_PICKER } from './modules/tile_picker.nf'
include { THRESHOLD_FINDER } from './modules/threshold_finder.nf'
include { POSTCODE_DECODER } from './modules/postcode_decoding.nf'
include { JOIN_COORDINATES } from './modules/join_coords.nf'
include { DECODER_QC as DECODER_QC_PoSTcode} from './modules/decoder_qc.nf'
include { DECODER_QC as DECODER_QC_Starfish} from './modules/decoder_qc.nf'
include { DECODER_QC as DECODER_QC_PoSTcodeFailed } from './modules/decoder_qc.nf'
include { MERGE_HTML } from './modules/merge_html.nf'
include { CONCAT_CSV } from './modules/concat_csv.nf'
include { CONCAT_NPY } from './modules/concat_npy.nf'
include { TO_SPATIALDATA } from './modules/to_spatialdata.nf'
def filter_channel(image_id) {
if (image_id.contains('anchor_dots')) {
return 'anchor_dots'
} else if (image_id.contains('anchor_nuclei')) {
return 'anchor_nuclei'
} else if (image_id.contains('_DAPI')) {
return 'nuclei'
} else {
return 'primary'
}
}
workflow {
println new File('.').getCanonicalPath()
println "PoSTcode activation: ${params.PoSTcode}"
// Define tuple of round ID and file path for moving images:
movingLearn_ch = Channel
.fromPath(params.inputMovImagesLearnPath)
.map{ f ->
sampleID = f.baseName
return [sampleID[0,1], f]
}
// Learn transformations and save TXT files with output;
// Make parameter metadata channel:
params_reg_ch = Channel.fromPath(params.elastix_parameter_files)
.toSortedList()
learnTransformation_ch = LEARN_TRANSFORM(movingLearn_ch, params.inputRefImagePath, params.rescale_factor, params_reg_ch)
// Define the channel with data for which to apply found transformations:
moving_ch = Channel
.fromPath(params.movingImagesApplyPath)
.map { it ->
[it.baseName[0,1], it]}
// Use previous channel and LEARN_TRANSFORM output to apply transformations based on
// the SampleID for combining both channels:
registered_out_ch = APPLY_TRANSFORM(learnTransformation_ch.combine(moving_ch, by:0))
// Prepare data for tiling by taking the whole name as the sampleID:
renamed_registered_out_ch = registered_out_ch
.map{it -> [it[1].baseName, it[1]]}
// Normalized the missing images:
missing_round = Channel
.fromPath(params.inputUntransformedImagesPath)
.map { it ->
[it.baseName, it]
}
missing_round_norm = NORMALIZE(missing_round)
merged_channel = missing_round_norm.mix(renamed_registered_out_ch)
// Use function 'filter_channel' to change 'merged_channel' id's and
// identify path by image type (primary, nuclei, etc).
redefined_merged_ch = merged_channel
.map { it ->
[filter_channel(it[0]), it[1]]}
regImg_path = registered_out_ch
.map{it -> [it[1]]}
.collect()
anch_path = missing_round_norm
.map{it -> [it[1]]}
.collect()
reg_qc_inputs = regImg_path.combine(
Channel.fromPath(params.inputMovImagesLearnPath).toList())
.combine(Channel.fromPath(params.inputRefImagePath).combine(anch_path))
reg_html = REGISTER_QC(reg_qc_inputs)
// Estimate tile size based on the registered anchor image:
tile_metadata_ch = TILE_SIZE_ESTIMATOR(Channel.fromPath(params.inputRefImagePath))
tile_html = tile_metadata_ch[2]
size_ch = tile_metadata_ch[1]
.map { it ->
it.baseName}
// To use later for the DECODING_POSTCODE process:
// coordinates_csv = tile_metadata_ch[2]
total_fovs_ch = tile_metadata_ch[0]
.splitText()
.map { it -> it.trim() }
// Add tile size as third argument for the input:
redefined_merged_ch_tile = redefined_merged_ch
.combine(size_ch)
.combine(Channel.fromPath(params.ExpMetaJSON))
// TILING PART:
tiled_ch = TILING(redefined_merged_ch_tile)
// To do: merge 'coordinates-fov*'
joined_coords_ch = tiled_ch[1].groupTuple()
coords4spacetx = JOIN_COORDINATES(joined_coords_ch)
grouped_tiled_images = tiled_ch[0].groupTuple()
// Flatten the files on the tuple:
grouped_tiled_images_flat = grouped_tiled_images
.map { it ->
[it[0], it[1].flatten()]}
//grouped_tiled_images_flat.view()
grouped_input = grouped_tiled_images_flat.combine(coords4spacetx, by: 0)
spacetx_out = SPACETX(grouped_input)
// Collect all the output from SpaceTx for feeding the following parts:
all_spacetx_files = spacetx_out
.map {it ->
it[1]}
.flatten()
// Join all spacetx files with codebook and experiment JSONs:
exp_json_ch = MAKE_EXP_JSON(params.ExpMetaJSON)
exp_plus_codebook = Channel.fromPath(params.CodeJSON)
.mix(exp_json_ch)
tuple_with_all = all_spacetx_files
.mix(exp_plus_codebook)
.toList()
tile_picker = TILE_PICKER(tuple_with_all, Channel.of('5'))
tiles = tile_picker
.splitText()
.map{it ->
it[0..6]
}
// Generate Thresholds but first Define parameters
def min_thr = 0.002
def max_thr = 0.009
def n_vals = 10
def increment = (Math.log10(max_thr) - Math.log10(min_thr)) / (n_vals - 1)
def thresholds = (0..<n_vals).collect { Math.pow(10, Math.log10(min_thr) + it * increment) }
merge_tiles_thresh = tiles.combine(thresholds)
merge_tiles_thresh_tile = merge_tiles_thresh.map{
it -> it[0]
}
merge_tiles_thresh_thresh = merge_tiles_thresh.map{
it -> it[1]
}
spots_detected_ch = SPOT_FINDER_1(tuple_with_all, merge_tiles_thresh_tile, merge_tiles_thresh_thresh)
starfish_tables = spots_detected_ch[1].toList()
threshold_results = THRESHOLD_FINDER(
Channel.fromPath(params.ExpMetaJSON),
starfish_tables)
// picked_threshold = threshold_results.splitText()
picked_threshold = threshold_results[0].splitText()
picked_threshold_html = threshold_results[1]
fov_and_threshold_ch = total_fovs_ch.combine(picked_threshold)
only_thr_ch = fov_and_threshold_ch.map{ it -> it[1] }
spots_detected_ch = SPOT_FINDER_2(tuple_with_all, total_fovs_ch, only_thr_ch)
sorted_detected_spots_ch = spots_detected_ch[0].toSortedList()
sorted_starfish_tables = spots_detected_ch[1].toSortedList()
postCode_input = CONCAT_NPY(sorted_detected_spots_ch)
// postCode.view()
starfish_table = CONCAT_CSV(sorted_starfish_tables)
if (params.PoSTcode){
postcode_results = POSTCODE_DECODER(
params.ExpMetaJSON,
Channel.fromPath(params.CodeJSON),
starfish_table,
postCode_input
)
csv_name = postcode_results.collect {
it -> it.baseName
}
if (csv_name.contains("postcode_decoding_failed")==true){
// spatialdata_qc_inputs = regImg_path.combine(anch_path).combine(starfish_table)
// TO_SPATIALDATA(params.ExpMetaJSON, spatialdata_qc_inputs)
decoder_html = DECODER_QC_PoSTcodeFailed(starfish_table, params.ExpMetaJSON)
}else{
spatialdata_qc_inputs = regImg_path.combine(anch_path).combine(postcode_results)
TO_SPATIALDATA(params.ExpMetaJSON, spatialdata_qc_inputs)
decoder_html = DECODER_QC_PoSTcode(postcode_results, params.ExpMetaJSON)
}
}else{
// spatialdata_qc_inputs = regImg_path.combine(anch_path).combine(starfish_table)
// TO_SPATIALDATA(params.ExpMetaJSON, spatialdata_qc_inputs)
decoder_html = DECODER_QC_Starfish(starfish_table, params.ExpMetaJSON)
}
// Concatenate HTML files from all processes
ch_all_html_files = reg_html.merge(tile_html).merge(decoder_html).merge(picked_threshold_html)
MERGE_HTML(ch_all_html_files)
}
workflow.onComplete {
println("Quality control reports and tables were generated in the workflow project directory under the folders \"ISS-reports\" and \"RegisterQc\" as part of the pipeline.")
}