-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmultimodal.nf
280 lines (232 loc) · 7.79 KB
/
multimodal.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
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
#!/usr/bin/env/ nextflow
import groovy.json.*
include { ome_zarr_metadata } from "./main.nf"
include { warnParams } from "./main.nf"
nextflow.enable.dsl=2
params.outdir = ""
params.copy_raw = true
params.description = ""
version="0.5.3"
verbose_log=true
outdir_with_version = "${params.outdir.replaceFirst(/\/*$/, "")}\/${version}"
config_map = [
title: params.title ?: "",
description: params.description ?: "",
url: params.url ?: "http://localhost/",
extend_feature_name: params.extend_feature_name ?: null
]
def copyFile (inputFile, outdir) {
def outfile = file(outdir) / "$inputFile.name"
if (outfile.exists()){
log.warn "File $inputFile.name already exists in $outdir. Skipping copy."
}
else {
inputFile.copyTo(outdir)
}
return inputFile
}
def parseExtendFeature (extend_feature){
if (!extend_feature){
return [ file("NO_FT"), [:] ]
}
else if (extend_feature instanceof String ){
return [
file(extend_feature.endsWith(".h5ad") ? extend_feature : "NO_FT"),
[:]
]
}
else if (extend_feature instanceof Map){
if (extend_feature["path"]){
if (!(extend_feature.path instanceof String && extend_feature.path.endsWith(".h5ad"))){
error "Invalid value for `extend_feature.path`. Expecting an .h5ad file."
}
return [ file(extend_feature.path), extend_feature.args ?: [:] ]
}
else {
error "Invalid map for `extend_feature`. Expecting key `path`."
}
}
error "Invalid value for `extend_feature`"
}
process process_label {
tag "${label_image}"
debug verbose_log
publishDir outdir_with_version, mode:"copy"
input:
tuple val(dataset), path(label_image), val(offset)
output:
tuple val(dataset), path("${reindexed_label_image}")
script:
basename = label_image.baseName
reindexed_label_image = "reindexed-${basename}.ome.zarr"
"""
integrate_image.py \
--label_image_path ${label_image} \
--offset ${offset} \
--out_filename ${reindexed_label_image}
"""
}
process process_anndata {
tag "${anndata}"
debug verbose_log
publishDir outdir_with_version, mode:"copy"
input:
tuple val(dataset), path(anndata), val(offset), val(features), path(features_file), val(features_args)
output:
tuple val(dataset), path("*")
script:
features_str = features
? "--features ${features_file.name != 'NO_FT' ? features_file : features}"
: ""
args_str = features_file.name != 'NO_FT' && features_args
? "--args '" + new JsonBuilder(features_args).toString() + "'" : ""
"""
integrate_anndata.py reindex_and_concat \
--path ${anndata} \
--offset ${offset} \
${features_str} ${args_str}
"""
}
process intersect_anndatas {
debug verbose_log
publishDir outdir_with_version, mode:"copy"
input:
path(anndatas)
output:
path("*")
script:
"""
integrate_anndata.py intersect_features ${anndatas}
"""
}
process Build_multimodal_config {
tag "${project}"
label 'build_config'
debug verbose_log
cache false
publishDir outdir_with_version, mode: "copy"
input:
tuple val(project), val(config_map), val(datasets)
output:
path("${project}-multimodal-config.json")
script:
url_str = config_map.url?.trim() ? "--url \"${config_map.url.trim()}\"" : ""
datasets_str = new JsonBuilder(datasets).toString()
"""
build_config_multimodal.py \
--project "${project}" \
--datasets '${datasets_str}' \
--extended_features "${config_map.extend_feature_name}" \
${url_str} \
--title "${config_map.title}" \
--description "${config_map.description}"
"""
}
workflow {
warnParams()
file(outdir_with_version).mkdirs()
Channel.from(params.data)
.multiMap{ it ->
info: [it.dataset, it.obs_type ?: "cell", it.is_spatial ?: false, it.vitessce_options ?: [:]]
raws : [it.dataset, it.raw_image] // not processed but necessary for writing config
labels : [it.dataset, it.label_image, it.offset]
adatas : [it.dataset, file(it.anndata), it.offset, it.extend_feature,
*parseExtendFeature(it.extend_feature)
]
}
.set{data}
// Filter null raw image
data.raws.filter{ it[1] }
.map{ [it[0], params.copy_raw ? copyFile(file(it[1]), outdir_with_version) : file(it[1])] }
.set{raw_images}
// Process labels filtered out nulls
process_label(data.labels.filter { it[1] }.map{ [it[0], file(it[1]), it[2]] })
// Reindex and concatenate anndata matrices
// filename will be used to join output from intersection
process_anndata(data.adatas)
process_anndata.out
.map{ [it[1].baseName.toString(), it[0], it[1] ] }
.set{reid_output}
// Perform intersection on list of anndatas
// filename without 'intersect-' is used to match the dataset it comes from
// as we cannot get paired dataset-file items as output
// because output is a list of files (they're processed together)
intersect_anndatas(process_anndata.out.collect{ it[1] })
intersect_anndatas.out
.flatMap()
.map{
// strip the extra 'intersect-' from file basename
def basename = it.baseName.split("intersect-")[-1]
[basename, it]
}
.set{intersect_output}
// Get ome zarr metadata from raw and label images
ome_zarr_metadata(
raw_images.map{[
it[0],
"raw",
file("${it[1].toString().replaceFirst(/\/*$/, '')}/OME/METADATA.ome.xml")
]}.concat(
process_label.output.map{[
it[0],
"label",
file("${it[1].toString().replaceFirst(/\/*$/, '')}/OME/METADATA.ome.xml")
]}
)
).set{images_md}
// Structure image data to join to corresponding dataset data
raw_images.map{[it[0], "raw", it[1]]}
.concat(process_label.output.map{[it[0], "label", it[1]]})
.join(images_md, by: [0,1])
.map { dataset, type, path, md ->
[
dataset, type, [path: path.name, md: new JsonSlurper().parseText(md.trim())]
]
}
.groupTuple(by: [0,1])
.map { dataset, type, img ->
[dataset, [type: type, img: img]]
}
.branch { dataset, data ->
raw: data.type == "raw"
label: data.type == "label"
}
.set{imgs}
// Image data structure to be able to perform join:
// [dataset, raw:[raw_imgs], label:[label_imgs]]
imgs.raw
.join(imgs.label, remainder: true)
.map { dataset, raw_data, label_data -> [
dataset,
[
raw: raw_data ? raw_data.img : [],
label: label_data ? label_data.img : []
]
]}
.set{img_map}
// Join reindexed and intersected outputs
// to get [dataset, intersected_file]
reid_output
.join(intersect_output, by: 0)
.map{ [it[1], it[3].name] }
.groupTuple(by: 0)
.set{files}
// tmp_data to replace potential nulls for empty list and map
data.info
.join(files, by:0)
.join(img_map, by: 0, remainder: true)
.map{[*it[0..3], it[4] ?: [], it[5] ?: [:]]}
.set{tmp_data}
// make map of each dataset
tmp_data.map{[
"${it[0]}":
[
obs_type: it[1], is_spatial: it[2], options: it[3],
file_paths: it[4], images: it[5]
]
]}
.reduce{ a,b -> a + b } // merge maps
.map{[params.project, config_map, it]}
.set{data_for_config}
Build_multimodal_config(data_for_config)
}