-
Notifications
You must be signed in to change notification settings - Fork 8
/
miniex.nf
539 lines (398 loc) · 19.2 KB
/
miniex.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
#!/usr/bin/env nextflow
nextflow.enable.dsl=2
/*
=====================================================
MINI-EX
=====================================================
Motif-Informed Network Inference from gene EXpression
-----------------------------------------------------
*/
if (params.doMotifAnalysis){
motifLog = "Running TF motif enrichment filtering on ${params.motifFilter}"
} else {
motifLog = "Skipping motif enrichment filtering"
}
miniexVersion = "v2.2"
log.info"""\
Motif-Informed Network Inference from gene EXpression ${miniexVersion}
===========================================================
${motifLog}
Running single-cell cluster enrichment using the top ${params.topMarkers} upregulated genes per cluster
Filtering out regulons of single-cell clusters where the TF is expressed in less than ${params.expressionFilter} % of the cells
Plotting expression specificity and DE calls for the top ${params.topRegulons} regulons
"""
.stripIndent()
scriptEnricher = "$baseDir/bin/enricherv3.2.4"
grnboostDir = "$params.outputDir/grnboost2"
regulonsDir = "$params.outputDir/regulons"
goEnrichmentDir = "$params.outputDir/go_enrichment"
figuresDir = "$params.outputDir/figures"
logDir = "$params.outputDir"
process check_user_input {
input:
path expressionMatrix
path markersOut
path cellsToClusters
path clustersToIdentities
path tfList
path termsOfInterest
path grnboostOut
path featureFileMotifs
path infoTf
path goFile
path geneAliases
val doMotifAnalysis
val topMarkers
val expressionFilter
val motifFilter
val topRegulons
output:
stdout emit: stdoutLog
path("processLog.log"), emit: processLog
"""
echo -n "MINI-EX" "$miniexVersion" "\nPipeline started on: " > "processLog.log"
date >> "processLog.log"
OMP_NUM_THREADS=1 python3 "$baseDir/bin/MINIEX_checkInput.py" "$expressionMatrix" "$markersOut" "$cellsToClusters" "$clustersToIdentities" \
"$tfList" "$termsOfInterest" "$grnboostOut" "$featureFileMotifs" "$infoTf" \
"$goFile" "$geneAliases" "$doMotifAnalysis" "$topMarkers" "$expressionFilter" \
"$motifFilter" "$topRegulons" >> "processLog.log"
# print input validation statistics on stdout
awk '/== INPUT VALIDATION/,/== INPUT FILES/ {if (!/== INPUT (VALIDATION|FILES)/) print}' processLog.log
"""
}
process get_expressed_genes {
input:
tuple val(datasetId), path(matrix)
output:
tuple val(datasetId), path("${datasetId}_expressedGenes.txt")
"""
tail -n +2 "$matrix" | cut -f 1 > "${datasetId}_expressedGenes.txt"
"""
}
process run_grnboost {
publishDir grnboostDir, mode: 'copy'
input:
path tfList
tuple val(datasetId), path(matrix)
output:
tuple val("${datasetId}"), path("${datasetId}_grnboost2.tsv")
"""
OMP_NUM_THREADS=1 python3 "$baseDir/bin/MINIEX_grnboostMultiprocess.py" $tfList "$matrix" "${task.cpus}" "${datasetId}_grnboost2.tsv"
"""
}
process unzip_motif_mappings {
input:
path featureFileMotifs
output:
path "MotifMappings.csv"
"""
gunzip -c $featureFileMotifs > "MotifMappings.csv"
"""
}
process run_enricher_motifs {
input:
path scriptEnricher
path featureFileMotifsUnzipped
tuple val(datasetId), path(modules), path(expressedGenes)
output:
tuple val("${datasetId}"), path("${datasetId}_enricherRegulons.txt")
"""
cat "$featureFileMotifsUnzipped" | grep -F -f "$expressedGenes" > featureFileMotifsfiltered.txt
$scriptEnricher featureFileMotifsfiltered.txt "$modules" -f 0.001 -n \$(cat "$expressedGenes" | wc -l) --min-hits 2 --print-hits -o "${datasetId}_enricherRegulons.txt"
# Throw an error if no enrichment is found
if [ "\$(head ${datasetId}_enricherRegulons.txt | grep -P '^[^#]' | wc -l)" -eq 0 ]; then
echo 'ERROR: Enricher output empty!'
exit 1
fi
"""
}
process filter_motifs {
input:
path infoTf
val motifFilter
tuple val(datasetId), path(enrichedModules)
output:
tuple val("${datasetId}"), path("${datasetId}_enrichedRegulons.txt")
"""
OMP_NUM_THREADS=1 python3 "$baseDir/bin/MINIEX_filterForMotifs.py" $infoTf "$enrichedModules" "${datasetId}_enrichedRegulons.txt" "$motifFilter"
"""
}
process filter_motifs_dummy {
input:
tuple val(datasetId), path(modules)
output:
tuple val("${datasetId}"), path("${datasetId}_enrichedRegulons.txt")
"""
cp "$modules" "${datasetId}_enrichedRegulons.txt"
"""
}
process get_top_degs {
input:
val topMarkers
tuple val(datasetId), path(allMarkers)
output:
tuple val("${datasetId}"), path("${datasetId}_top${topMarkers}cellClusters.out")
"""
OMP_NUM_THREADS=1 python3 "$baseDir/bin/MINIEX_selectTopDEGs.py" $allMarkers "$topMarkers" "${datasetId}_top${topMarkers}cellClusters.out"
"""
}
process run_enricher_cluster {
input:
path scriptEnricher
tuple val(datasetId), path(featureFileCellClusters), path(filteredRegulons), path(expressedGenes)
output:
tuple val("${datasetId}"), path("${datasetId}_enricherCelltypes.txt")
"""
cat "$featureFileCellClusters" | grep -f "$expressedGenes" > feature_file_cell_clusters_filtered.txt
$scriptEnricher feature_file_cell_clusters_filtered.txt "$filteredRegulons" -f 0.001 -n \$(cat "$expressedGenes" | wc -l) --min-hits 2 --print-hits -o "${datasetId}_enricherCelltypes.txt"
# Throw an error if no enrichment is found
if [ "\$(head ${datasetId}_enricherCelltypes.txt | grep -P '^[^#]' | wc -l)" -eq 0 ]; then
echo 'ERROR: Enricher output empty!'
exit 1
fi
"""
}
process filter_expression {
publishDir regulonsDir, mode: 'copy'
input:
path infoTf
val expressionFilter
tuple val(datasetId), path(expressionMatrix), path(cellClusters), path(regulons)
output:
tuple val("${datasetId}"), path("${datasetId}_regulons.tsv")
"""
OMP_NUM_THREADS=1 python3 "$baseDir/bin/MINIEX_filterForTFExp.py" "$expressionMatrix" $infoTf $cellClusters "$expressionFilter" "$regulons" "${datasetId}_regulons.tsv"
"""
}
process make_info_file {
publishDir regulonsDir, mode: 'copy', pattern: '*.tsv'
input:
tuple val(datasetId), path(expressionMatrix), path(grnboostRegulons), path(motifEnrichedRegulons), path(finalRegulons), path(cellClusters), path(clusterIdentities)
path tfList
output:
tuple val("${datasetId}"), path("${datasetId}_TF_info_file.tsv")
tuple val("${datasetId}"), path("${datasetId}_regulonInfoLog.log"), emit: processLog
"""
OMP_NUM_THREADS=1 python3 "$baseDir/bin/MINIEX_makeInfoFile.py" "$expressionMatrix" "$grnboostRegulons" "$motifEnrichedRegulons" "$finalRegulons" $tfList $cellClusters $clusterIdentities "${datasetId}_TF_info_file.tsv" > "${datasetId}_regulonInfoLog.log"
"""
}
process make_regulon_clustermap {
publishDir figuresDir, mode: 'copy'
input:
tuple val(datasetId), path(clusterIdentities), path(finalRegulons)
output:
tuple val("${datasetId}"), path("${datasetId}_clustermap.svg"), path("${datasetId}_clustermap.png")
"""
OMP_NUM_THREADS=1 python3 "$baseDir/bin/MINIEX_visualClustermap.py" "$clusterIdentities" "$finalRegulons" "${datasetId}_clustermap"
"""
}
process get_network_centrality {
input:
tuple val(datasetId), path(finalRegulons)
output:
tuple val("${datasetId}"), path("${datasetId}_networkCentrality.txt")
"""
OMP_NUM_THREADS=1 python3 "$baseDir/bin/MINIEX_network_analysis.py" "$finalRegulons" "${datasetId}_networkCentrality.txt"
"""
}
process make_go_enrichment_files {
input:
tuple val(datasetId), path(finalRegulons), path(goFile)
output:
tuple val("${datasetId}"), path("${datasetId}_setFileRegulons.out"), path("${datasetId}_featureFileGO.out")
"""
OMP_NUM_THREADS=1 python3 "$baseDir/bin/MINIEX_makeFilesEnrichment.py" "$finalRegulons" "$goFile" "${datasetId}_setFileRegulons.out" "${datasetId}_featureFileGO.out"
"""
}
process run_enricher_go {
publishDir goEnrichmentDir, mode: 'copy'
input:
path scriptEnricher
tuple val(datasetId), path(set), path(featureFile), path(expressedGenes)
output:
tuple val("${datasetId}"), path("${datasetId}_enricherGO.txt")
"""
cat "$featureFile" | grep -f "$expressedGenes" > feature_file_filtered.txt
$scriptEnricher feature_file_filtered.txt "$set" -f 0.05 -n \$(cat "$expressedGenes" | wc -l) --min-hits 2 -p -o "${datasetId}_enricherGO.txt"
# Throw an error if no enrichment is found
if [ "\$(head ${datasetId}_enricherGO.txt | grep -P '^[^#]' | wc -l)" -eq 0 ]; then
echo 'ERROR: Enricher output empty!'
exit 1
fi
"""
}
process check_reference {
input:
tuple val(datasetId), path(finalRegulons), path(goFile)
path termsOfInterest
output:
tuple val("${datasetId}"), stdout
"""
OMP_NUM_THREADS=1 python3 "$baseDir/bin/MINIEX_checkRef.py" "$finalRegulons" "$goFile" "$termsOfInterest"
"""
}
process make_ref_ranking_dataframe {
input:
path geneAliases
path termsOfInterest
tuple val(datasetId), path(clusterIdentities), path(finalRegulons), path(qValueCluster), path(networkCentrality), path(goEnrichment), path(allMarkers), path(goFile)
output:
tuple val("${datasetId}"), path("${datasetId}_dfForRanking.txt")
"""
OMP_NUM_THREADS=1 python3 "$baseDir/bin/MINIEX_makeRankingDf_ref.py" "$clusterIdentities" "$finalRegulons" "$geneAliases" "$qValueCluster" "$goFile" "$termsOfInterest" "$networkCentrality" "$goEnrichment" "$allMarkers" "${datasetId}_dfForRanking.txt"
"""
}
process make_std_ranking_dataframe {
input:
path geneAliases
tuple val(datasetId), path(clusterIdentities), path(finalRegulons), path(qValueCluster), path(networkCentrality), path(allMarkers), val(goFile)
output:
tuple val("${datasetId}"), path("${datasetId}_dfForRanking.txt")
"""
OMP_NUM_THREADS=1 python3 "$baseDir/bin/MINIEX_makeRankingDf_std.py" "$clusterIdentities" "$finalRegulons" "$geneAliases" "$qValueCluster" "$goFile" "$networkCentrality" "$allMarkers" "${datasetId}_dfForRanking.txt"
"""
}
process make_borda {
publishDir regulonsDir, mode: 'copy', pattern: '*.{xlsx,tsv}'
input:
tuple val(datasetId), path(regulonsDataframe), val(ref)
output:
tuple val("${datasetId}"), path("${datasetId}_rankedRegulons.xlsx"), emit: processOut
tuple val("${datasetId}"), path("${datasetId}_rankedRegulons.tsv")
tuple val("${datasetId}"), path("${datasetId}_bordaProcessLog.log"), emit: processLog
"""
OMP_NUM_THREADS=1 python3 "$baseDir/bin/MINIEX_makeBorda.py" "$regulonsDataframe" "${datasetId}_rankedRegulons" "$ref" > "${datasetId}_bordaProcessLog.log"
"""
}
process score_edges {
publishDir regulonsDir, mode: 'copy'
input:
tuple val(datasetId), path(regulons), path(rankedRegulons), path(modules)
output:
tuple val(datasetId), path("${datasetId}_edgeTable.tsv")
"""
OMP_NUM_THREADS=1 python3 "$baseDir/bin/MINIEX_scoreEdges.py" $regulons $rankedRegulons $modules ${datasetId}_edgeTable.tsv
"""
}
process make_top_regulons_heatmaps {
publishDir figuresDir, mode: 'copy'
input:
tuple val(datasetId), path(rankedRegulons)
val topRegulons
output:
tuple val(datasetId), path("${datasetId}_heatmapSpecificity.svg"), path("${datasetId}_heatmapSpecificity.png"), path("${datasetId}_heatmapDEcalls.svg"), path("${datasetId}_heatmapDEcalls.png")
"""
OMP_NUM_THREADS=1 python3 "$baseDir/bin/MINIEX_visualHeatmapTop150.py" "$rankedRegulons" "${datasetId}_heatmapSpecificity" "${datasetId}_heatmapDEcalls" "$topRegulons"
"""
}
process make_regmaps {
publishDir figuresDir, mode: 'copy'
input:
tuple val(datasetId), path(expressionMatrix), path(cellClusters), path(clusterIdentities), path(rankedRegulons)
val topRegulons
output:
tuple val(datasetId), path("${datasetId}_regmap_*.svg"), path("${datasetId}_regmap_*.png")
"""
OMP_NUM_THREADS=1 python3 "$baseDir/bin/MINIEX_visualRegmap.py" -c $cellClusters \
-i $clusterIdentities \
-r $rankedRegulons \
-m $expressionMatrix \
-t 10,25,50,100,$topRegulons \
-d $datasetId
"""
}
process make_log_file {
publishDir logDir, mode: 'copy'
input:
tuple path(checkInputLog), val(datasetId), path(regulonInfoLog), path(bordaLog)
output:
path("${datasetId}_log.txt")
"""
cat "$checkInputLog" "$regulonInfoLog" "$bordaLog" > "${datasetId}_log.txt"
echo -n "Pipeline ended on: " >> "${datasetId}_log.txt"
date >> "${datasetId}_log.txt"
"""
}
workflow {
check_user_input(
Channel.fromPath(params.expressionMatrix, checkIfExists:true).collect(),
Channel.fromPath(params.markersOut, checkIfExists:true).collect(),
Channel.fromPath(params.cellsToClusters, checkIfExists:true).collect(),
Channel.fromPath(params.clustersToIdentities, checkIfExists:true).collect(),
Channel.fromPath(params.tfList, checkIfExists:true).collect(),
params.termsOfInterest != null ? Channel.fromPath(params.termsOfInterest, checkIfExists:true).collect() : "/dummy_path_tof",
params.grnboostOut != null ? Channel.fromPath(params.grnboostOut, checkIfExists:true).collect() : "/dummy_path_grnb",
params.doMotifAnalysis == true? Channel.fromPath(params.featureFileMotifs, checkIfExists:true) : "/dummy_path_motif",
Channel.fromPath(params.infoTf, checkIfExists:true).collect(),
params.goFile != null ? Channel.fromPath(params.goFile, checkIfExists:true).collect() : "/dummy_path_go",
params.geneAliases != null ? Channel.fromPath(params.geneAliases, checkIfExists:true).collect() : "/dummy_gene_aliases",
params.doMotifAnalysis,
params.topMarkers,
params.expressionFilter,
params.motifFilter,
params.topRegulons)
check_user_input.out.stdoutLog.view() // print the output of check_user_input to the terminal
matrix_ch = Channel.fromPath(params.expressionMatrix).map { n -> [ n.baseName.split("_")[0], n ] }
if (params.grnboostOut == null){
run_grnboost(params.tfList,matrix_ch)
grnboost_ch = run_grnboost.out
}
if (params.grnboostOut != null){
grnboost_ch = Channel.fromPath(params.grnboostOut).map { n -> [ n.baseName.split("_")[0], n ] }
}
expressed_genes_ch = get_expressed_genes(matrix_ch)
grnboost_combined_ch = grnboost_ch.join(expressed_genes_ch)
if (params.doMotifAnalysis){
unzip_motif_mappings(params.featureFileMotifs)
run_enricher_motifs(scriptEnricher,unzip_motif_mappings.out,grnboost_combined_ch)
filter_motifs(params.infoTf,params.motifFilter,run_enricher_motifs.out)
filter_motifs_ch = filter_motifs.out
} else {
filter_motifs_dummy(grnboost_ch)
filter_motifs_ch = filter_motifs_dummy.out
}
deg_ch = Channel.fromPath(params.markersOut).map { n -> [ n.baseName.split("_")[0], n ] }
cluster_enrich_ch = get_top_degs(params.topMarkers,deg_ch)
cluster_enrich_combined_ch = cluster_enrich_ch.join(filter_motifs_ch).join(expressed_genes_ch)
run_enricher_cluster(scriptEnricher,cluster_enrich_combined_ch)
cluster_ch = Channel.fromPath(params.cellsToClusters).map { n -> [ n.baseName.split("_")[0], n ] }
filter_combined_ch = matrix_ch.join(cluster_ch).join(run_enricher_cluster.out)
filter_expression(params.tfList,params.expressionFilter,filter_combined_ch)
cluster_ids_ch = Channel.fromPath(params.clustersToIdentities).map { n -> [ n.baseName.split("_")[0], n ] }
info_ch = matrix_ch.join(grnboost_ch).join(filter_motifs_ch).join(filter_expression.out).join(cluster_ch).join(cluster_ids_ch)
make_info_file(info_ch,params.tfList)
regulons_ident_ch = cluster_ids_ch.join(filter_expression.out)
make_regulon_clustermap(regulons_ident_ch)
get_network_centrality(filter_expression.out)
if ( params.goFile != null ){
make_go_enrichment_files_input_ch = filter_expression.out.combine(Channel.fromPath(params.goFile))
make_go_enrichment_files_ch = make_go_enrichment_files(make_go_enrichment_files_input_ch)
make_go_enrichment_files_combined_ch = make_go_enrichment_files_ch.join(expressed_genes_ch)
run_enricher_go(scriptEnricher,make_go_enrichment_files_combined_ch)
}
if ( params.geneAliases == null )
gene_aliases_path = "$baseDir/data/.dummy_empty_file.txt"
else
gene_aliases_path = params.geneAliases
if ( params.goFile != null && params.termsOfInterest != null ){
check_reference(make_go_enrichment_files_input_ch,params.termsOfInterest)
check_reference_trimmed = check_reference.out.map { n,it -> [ n, it.trim() ] }
rankingRef_combined_ch = cluster_ids_ch.join(filter_expression.out).join(run_enricher_cluster.out).join(get_network_centrality.out).join(run_enricher_go.out).join(deg_ch).combine(Channel.fromPath(params.goFile))
make_ref_ranking_dataframe(gene_aliases_path,params.termsOfInterest,rankingRef_combined_ch)
borda_input_ch = make_ref_ranking_dataframe.out.join(check_reference_trimmed)
} else {
rankingStd_combined_ch = cluster_ids_ch.join(filter_expression.out).join(run_enricher_cluster.out).join(get_network_centrality.out).join(deg_ch).combine(Channel.value(false))
make_std_ranking_dataframe(gene_aliases_path,rankingStd_combined_ch)
borda_input_ch = make_std_ranking_dataframe.out.combine(Channel.value('std'))
}
make_borda(borda_input_ch)
score_edges_ch = filter_expression.out.join(make_borda.out.processOut).join(grnboost_ch)
score_edges(score_edges_ch)
make_top_regulons_heatmaps(make_borda.out.processOut,params.topRegulons)
make_regmaps_input_ch = matrix_ch.join(cluster_ch).join(cluster_ids_ch).join(make_borda.out.processOut)
make_regmaps(make_regmaps_input_ch, params.topRegulons)
make_log_file(check_user_input.out.processLog.combine(make_info_file.out.processLog.join(make_borda.out.processLog)))
}
workflow.onComplete {
log.info ( workflow.success ? "Done!" : "Oops .. something went wrong" )
}