-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathmain.nf
More file actions
285 lines (252 loc) · 7.75 KB
/
main.nf
File metadata and controls
285 lines (252 loc) · 7.75 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
include { checkItemAllowed } from "${meta.resources_dir}/helper.nf"
workflow auto {
findStates(params, meta.config)
| meta.workflow.run(
auto: [publish: "state"]
)
}
// construct list of methods and control methods
methods = [
embed_cell_types,
embed_cell_types_jittered,
no_integration,
no_integration_batch,
shuffle_integration,
shuffle_integration_by_batch,
shuffle_integration_by_cell_type,
batchelor_fastmnn,
batchelor_mnn_correct,
bbknn,
combat,
density_adaptive,
geneformer,
harmony,
harmonypy,
liger,
mnnpy,
pyliger,
scalex,
scanorama,
scanvi,
scgpt_finetuned.run(
args: [model: file("s3://openproblems-work/cache/scGPT_human.zip")]
),
scgpt_zeroshot.run(
args: [model: file("s3://openproblems-work/cache/scGPT_human.zip")]
),
scimilarity.run(
args: [model: file("s3://openproblems-work/cache/scimilarity-model_v1.1.tar.gz")]
),
scprint,
scvi,
uce.run(
args: [model: file("s3://openproblems-work/cache/uce-model-v5.zip")]
)
]
// construct list of metrics
metrics = [
asw_batch,
asw_label,
cell_cycle_conservation,
clustering_overlap,
graph_connectivity,
hvg_overlap,
isolated_label_asw,
isolated_label_f1,
kbet,
kbet_pg,
kbet_pg_label,
lisi,
pcr
]
workflow run_wf {
take:
input_ch
main:
/****************************
* EXTRACT DATASET METADATA *
****************************/
dataset_ch = input_ch
// store join id
| map{ id, state ->
[id, state + ["_meta": [join_id: id]]]
}
// extract the dataset metadata
| extract_uns_metadata.run(
fromState: [input: "input_solution"],
toState: { id, output, state ->
state + [
dataset_uns: readYaml(output.output).uns
]
}
)
/***************
* RUN METHODS *
***************/
score_ch = dataset_ch
// run all methods
| runEach(
components: methods,
// use the 'filter' argument to only run a method on the normalisation the component is asking for
filter: { id, state, comp ->
def norm = state.dataset_uns.normalization_id
def pref = comp.config.info.preferred_normalization
// if the preferred normalisation is none at all,
// we can pass whichever dataset we want
def norm_check = (norm == "log_cp10k" && pref == "counts") || norm == pref
def method_check = checkItemAllowed(
comp.config.name,
state.methods_include,
state.methods_exclude,
"methods_include",
"methods_exclude"
)
method_check && norm_check
},
// define a new 'id' by appending the method name to the dataset id
id: { id, state, comp ->
id + "." + comp.config.name
},
// use 'fromState' to fetch the arguments the component requires from the overall state
fromState: { id, state, comp ->
def new_args = [:]
if (comp.config.info.type == "method") {
new_args.input = state.input_dataset
} else if (comp.config.info.type == "control_method") {
new_args.input_dataset = state.input_dataset
new_args.input_solution = state.input_solution
}
new_args
},
// use 'toState' to publish that component's outputs to the overall state
toState: { id, output, state, comp ->
state + [
method_id: comp.config.name,
method_types: comp.config.info.method_types,
method_output: output.output
]
}
)
/******************
* PROCESS OUTPUT *
******************/
| process_integration.run(
fromState: [
input_integrated: "method_output",
input_dataset: "input_dataset",
expected_method_types: "method_types",
resolutions: "resolutions"
],
toState: { id, output, state ->
// Add method types to the state
// This is done here because state can't be passed from the processing subworkflow
def method_types_cleaned = []
if ("feature" in state.method_types) {
method_types_cleaned += ["feature", "embedding", "graph"]
} else if ("embedding" in state.method_types) {
method_types_cleaned += ["embedding", "graph"]
} else if ("graph" in state.method_types) {
method_types_cleaned += ["graph"]
}
def new_state = state + [
method_output_processed: output.output,
method_types_cleaned: method_types_cleaned
]
new_state
}
)
/***************
* RUN METRICS *
***************/
// run all metrics
| runEach(
components: metrics,
id: { id, state, comp ->
id + "." + comp.config.name
},
filter: { id, state, comp ->
comp.config.info.metric_type in state.method_types_cleaned
},
// use 'fromState' to fetch the arguments the component requires from the overall state
fromState: [
input_solution: "input_solution",
input_integrated: "method_output_processed"
],
// use 'toState' to publish that component's outputs to the overall state
toState: { id, output, state, comp ->
state + [
metric_id: comp.config.name,
metric_output: output.output
]
}
)
// extract the scores
| extract_uns_metadata.run(
key: "extract_scores",
fromState: [input: "metric_output"],
toState: { id, output, state ->
state + [
score_uns: readYaml(output.output).uns
]
}
)
| joinStates { ids, states ->
// store the scores in a file
def score_uns = states.collect{it.score_uns}
def score_uns_yaml_blob = toYamlBlob(score_uns)
def score_uns_file = tempFile("score_uns.yaml")
score_uns_file.write(score_uns_yaml_blob)
["output", [output_scores: score_uns_file]]
}
/******************************
* GENERATE OUTPUT YAML FILES *
******************************/
// TODO: can we store everything below in a separate helper function?
// extract the dataset metadata
meta_ch = dataset_ch
// only keep one of the normalization methods
| filter{ id, state ->
state.dataset_uns.normalization_id == "log_cp10k"
}
| joinStates { ids, states ->
// store the dataset metadata in a file
def dataset_uns = states.collect{state ->
def uns = state.dataset_uns.clone()
uns.remove("normalization_id")
uns
}
def dataset_uns_yaml_blob = toYamlBlob(dataset_uns)
def dataset_uns_file = tempFile("dataset_uns.yaml")
dataset_uns_file.write(dataset_uns_yaml_blob)
// store the method configs in a file
def method_configs = methods.collect{it.config}
def method_configs_yaml_blob = toYamlBlob(method_configs)
def method_configs_file = tempFile("method_configs.yaml")
method_configs_file.write(method_configs_yaml_blob)
// store the metric configs in a file
def metric_configs = metrics.collect{it.config}
def metric_configs_yaml_blob = toYamlBlob(metric_configs)
def metric_configs_file = tempFile("metric_configs.yaml")
metric_configs_file.write(metric_configs_yaml_blob)
// store the task info in a file
def viash_file = meta.resources_dir.resolve("_viash.yaml")
// create output state
def new_state = [
output_dataset_info: dataset_uns_file,
output_method_configs: method_configs_file,
output_metric_configs: metric_configs_file,
output_task_info: viash_file,
_meta: states[0]._meta
]
["output", new_state]
}
// merge all of the output data
output_ch = score_ch
| mix(meta_ch)
| joinStates{ ids, states ->
def mergedStates = states.inject([:]) { acc, m -> acc + m }
[ids[0], mergedStates]
}
emit:
output_ch
}