-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmain_multi_backend.nf
More file actions
911 lines (734 loc) · 25.6 KB
/
Copy pathmain_multi_backend.nf
File metadata and controls
911 lines (734 loc) · 25.6 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
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
#!/usr/bin/env nextflow
nextflow.enable.dsl = 2
params.ecephys_path = DATA_PATH
params.params_file = null
// Git repository prefix - can be overridden via command line or environment variable
params.git_repo_prefix = System.getenv('GIT_REPO_PREFIX') ?: 'https://github.com/AllenNeuralDynamics/aind-'
// Helper function for git cloning
def gitCloneFunction = '''
clone_repo() {
local repo_url="$1"
local commit_hash="$2"
echo "cloning git repo: \${repo_url} (commit: \${commit_hash})..."
git clone "\${repo_url}" capsule-repo
git -C capsule-repo -c core.fileMode=false checkout "\${commit_hash}" --quiet
mv capsule-repo/code capsule/code
rm -rf capsule-repo
}
'''
def buildStepArgs(Map json_section, String cli_param_name) {
def args_map = json_section ? new LinkedHashMap(json_section) : [:]
if (cli_param_name in params_keys && params[cli_param_name] instanceof String) {
println "Merging ${cli_param_name} from JSON with CLI args: ${params[cli_param_name]}"
def cli_tokens = params[cli_param_name].trim().split(/\s+/) as List
for (int i = 0; i < cli_tokens.size(); i++) {
if (cli_tokens[i].startsWith('--')) {
def key = cli_tokens[i].substring(2).replace('-', '_')
def value = (i + 1 < cli_tokens.size() && !cli_tokens[i + 1].startsWith('--')) ? cli_tokens[++i] : true
args_map[key] = value
}
}
}
return args_map ? "--params '${groovy.json.JsonOutput.toJson(args_map)}'" : ""
}
println "DATA_PATH: ${DATA_PATH}"
println "RESULTS_PATH: ${RESULTS_PATH}"
// Load parameters from JSON file if provided
def json_params = [:]
if (params.params_file) {
json_params = new groovy.json.JsonSlurper().parseText(new File(params.params_file).text)
println "Loaded parameters from ${params.params_file}"
}
println "PARAMS: ${params}"
// get commit hashes for capsules
def parse_capsule_versions() {
// Check for custom versions file first, fall back to default
def versionsFile = file("${baseDir}/capsule_versions_custom.env")
if (!versionsFile.exists()) {
versionsFile = file("${baseDir}/capsule_versions.env")
}
capsule_versions = versionsFile.toString()
println "Using custom capsule versions file at: ${capsule_versions}"
// Read versions from main_sorters_slurm.nf - this needs to be accessible by included workflows too.
def versions = [:]
if (file(capsule_versions).exists()) {
file(capsule_versions).eachLine { line ->
if (line.contains('=')) {
def idx = line.indexOf('=')
def key = line.substring(0, idx).trim()
def value = line.substring(idx + 1).trim().replaceAll(/^["']|["']$/, '')
versions[key] = value
}
}
} else {
println "Warning: Capsule versions file not found at ${capsule_versions}. Using empty versions map."
}
versions
}
params.versions = parse_capsule_versions()
// container tag
params.container_tag = "si-${params.versions['SPIKEINTERFACE_VERSION']}"
println "CONTAINER TAG: ${params.container_tag}"
params.extra_installs = params.versions['EXTRA_INSTALLS'] ?: ""
if (params.extra_installs) {
println "Extra installs specified: ${params.extra_installs}"
} else {
println "No extra installs specified."
}
def extra_installs_list = params.extra_installs ? params.extra_installs.split(',').collect { it.trim() }.findAll { it } : []
def extra_installs_cmd = extra_installs_list ? "pip install " + extra_installs_list.collect { "'" + it + "'" }.join(' ') : ""
def extra_installs_echo = extra_installs_list ? "echo 'installing extra packages: " + extra_installs_list.join(', ') + "'" : ""
// params keys on the outer level were loaded via CLI flags (the `json_params` are from the `params_file`)
params_keys = params.keySet()
// if not specified, assume local executor
if (!params_keys.contains('executor')) {
params.executor = "local"
}
// set global n_jobs for local executor
if (params.executor == "local")
{
if ("n_jobs" in params_keys) {
n_jobs = params.n_jobs
}
else {
n_jobs = -1
}
println "N JOBS: ${n_jobs}"
job_args=" --n-jobs ${n_jobs}"
}
else {
job_args=""
}
// set runmode
if ("runmode" in params_keys) {
runmode = params.runmode
}
else {
runmode = "full"
}
println "Using RUNMODE: ${runmode}"
if (params.params_file) {
println "Using parameters from JSON file: ${params.params_file}"
} else {
println "No parameters file provided, using command line arguments."
}
// Build params: merge CLI overrides, stringify once
def job_dispatch_args = buildStepArgs(json_params.job_dispatch, "job_dispatch_args")
def preprocessing_args = buildStepArgs(json_params.preprocessing, "preprocessing_args")
def postprocessing_args = buildStepArgs(json_params.postprocessing, "postprocessing_args")
def curation_args = buildStepArgs(json_params.curation, "curation_args")
def visualization_kwargs = buildStepArgs(json_params.visualization, "visualization_kwargs")
def nwb_ecephys_args = buildStepArgs(json_params.nwb?.ecephys, "nwb_ecephys_args")
// Spikesorting: resolve sorter-specific sub-map
def sorter = null
if (params.params_file && json_params.spikesorting) {
sorter = json_params.spikesorting.sorter ?: null
}
if (sorter == null && "sorter" in params_keys) {
sorter = params.sorter ?: "kilosort4"
}
if (sorter == null) {
println "No sorter specified, defaulting to kilosort4"
sorter = "kilosort4"
}
def spikesorting_args = buildStepArgs(
json_params.spikesorting ? json_params.spikesorting[sorter] : null,
"spikesorting_args"
)
println "Using SORTER: ${sorter} with args: ${spikesorting_args}"
if (runmode == 'fast'){
preprocessing_args = "--motion skip"
postprocessing_args = "--skip-extensions spike_locations,principal_components"
nwb_ecephys_args = "--skip-lfp"
println "Running in fast mode. Setting parameters:"
println "preprocessing_args: ${preprocessing_args}"
println "postprocessing_args: ${postprocessing_args}"
println "nwb_ecephys_args: ${nwb_ecephys_args}"
}
// Process definitions
process job_dispatch {
tag 'job-dispatch'
def container_name = "ghcr.io/allenneuraldynamics/aind-ephys-pipeline-base:${params.container_tag}"
container container_name
input:
path input_folder, stageAs: 'capsule/data/ecephys_session'
output:
path 'capsule/results/*', emit: results
path 'max_duration.txt', emit: max_duration_file // file containing the value
script:
"""
#!/usr/bin/env bash
set -e
${extra_installs_echo}
${extra_installs_cmd}
mkdir -p capsule
mkdir -p capsule/data
mkdir -p capsule/results
mkdir -p capsule/scratch
if [[ ${params.executor} == "slurm" ]]; then
echo "[${task.tag}] allocated task time: ${task.time}"
fi
TASK_DIR=\$(pwd)
echo "[${task.tag}] cloning git repo..."
${gitCloneFunction}
clone_repo "${params.versions['JOB_DISPATCH_REPO']}" "${params.versions['JOB_DISPATCH_COMMIT']}"
echo "[${task.tag}] running capsule..."
cd capsule/code
chmod +x run
./run ${job_dispatch_args}
MAX_DURATION_MIN=\$(python get_max_recording_duration_min.py)
cd \$TASK_DIR
echo "\$MAX_DURATION_MIN" > max_duration.txt
echo "[${task.tag}] completed!"
"""
}
process preprocessing {
tag 'preprocessing'
def container_name = "ghcr.io/allenneuraldynamics/aind-ephys-pipeline-base:${params.container_tag}"
container container_name
input:
val max_duration_minutes
path ecephys_session_input, stageAs: 'capsule/data/ecephys_session'
path job_dispatch_results, stageAs: 'capsule/data/*'
output:
path 'capsule/results/*', emit: results
script:
"""
#!/usr/bin/env bash
set -e
${extra_installs_echo}
${extra_installs_cmd}
mkdir -p capsule
mkdir -p capsule/data
mkdir -p capsule/results
mkdir -p capsule/scratch
if [[ ${params.executor} == "slurm" ]]; then
echo "[${task.tag}] allocated task time: ${task.time}"
# Make sure N_JOBS matches allocated CPUs on SLURM
export N_JOBS_EXT=${task.cpus}
fi
echo "[${task.tag}] cloning git repo..."
${gitCloneFunction}
clone_repo "${params.versions['PREPROCESSING_REPO']}" "${params.versions['PREPROCESSING_COMMIT']}"
echo "[${task.tag}] running capsule..."
cd capsule/code
chmod +x run
./run ${preprocessing_args} ${job_args}
echo "[${task.tag}] completed!"
"""
}
process spikesort_kilosort25 {
tag 'spikesort-kilosort25'
def container_name = "ghcr.io/allenneuraldynamics/aind-ephys-spikesort-kilosort25:${params.container_tag}"
container container_name
input:
val max_duration_minutes
path preprocessing_results, stageAs: 'capsule/data/*'
output:
path 'capsule/results/*', emit: results
script:
"""
#!/usr/bin/env bash
set -e
mkdir -p capsule
mkdir -p capsule/data
mkdir -p capsule/results
mkdir -p capsule/scratch
if [[ ${params.executor} == "slurm" ]]; then
echo "[${task.tag}] allocated task time: ${task.time}"
# Make sure N_JOBS matches allocated CPUs on SLURM
export N_JOBS_EXT=${task.cpus}
fi
echo "[${task.tag}] cloning git repo..."
${gitCloneFunction}
clone_repo "${params.versions['SPIKESORT_KS25_REPO']}" "${params.versions['SPIKESORT_KS25_COMMIT']}"
echo "[${task.tag}] running capsule..."
cd capsule/code
chmod +x run
./run ${spikesorting_args} ${job_args}
echo "[${task.tag}] completed!"
"""
}
process spikesort_kilosort4 {
tag 'spikesort-kilosort4'
def container_name = "ghcr.io/allenneuraldynamics/aind-ephys-spikesort-kilosort4:${params.container_tag}"
container container_name
input:
val max_duration_minutes
path preprocessing_results, stageAs: 'capsule/data/*'
output:
path 'capsule/results/*', emit: results
script:
"""
#!/usr/bin/env bash
set -e
mkdir -p capsule
mkdir -p capsule/data
mkdir -p capsule/results
mkdir -p capsule/scratch
if [[ ${params.executor} == "slurm" ]]; then
echo "[${task.tag}] allocated task time: ${task.time}"
# Make sure N_JOBS matches allocated CPUs on SLURM
export N_JOBS_EXT=${task.cpus}
fi
echo "[${task.tag}] cloning git repo..."
${gitCloneFunction}
clone_repo "${params.versions['SPIKESORT_KS4_REPO']}" "${params.versions['SPIKESORT_KS4_COMMIT']}"
echo "[${task.tag}] running capsule..."
cd capsule/code
chmod +x run
./run ${spikesorting_args} ${job_args}
echo "[${task.tag}] completed!"
"""
}
process spikesort_spykingcircus2 {
tag 'spikesort-spykingcircus2'
def container_name = "ghcr.io/allenneuraldynamics/aind-ephys-pipeline-base:${params.container_tag}"
container container_name
input:
val max_duration_minutes
path preprocessing_results, stageAs: 'capsule/data/*'
output:
path 'capsule/results/*', emit: results
script:
"""
#!/usr/bin/env bash
set -e
mkdir -p capsule
mkdir -p capsule/data
mkdir -p capsule/results
mkdir -p capsule/scratch
if [[ ${params.executor} == "slurm" ]]; then
echo "[${task.tag}] allocated task time: ${task.time}"
# Make sure N_JOBS matches allocated CPUs on SLURM
export N_JOBS_EXT=${task.cpus}
fi
echo "[${task.tag}] cloning git repo..."
${gitCloneFunction}
clone_repo "${params.versions['SPIKESORT_SC2_REPO']}" "${params.versions['SPIKESORT_SC2_COMMIT']}"
echo "[${task.tag}] running capsule..."
cd capsule/code
chmod +x run
./run ${spikesorting_args} ${job_args}
echo "[${task.tag}] completed!"
"""
}
process spikesort_lupin {
tag 'spikesort-lupin'
def container_name = "ghcr.io/allenneuraldynamics/aind-ephys-pipeline-base:${params.container_tag}"
container container_name
input:
val max_duration_minutes
path preprocessing_results, stageAs: 'capsule/data/*'
output:
path 'capsule/results/*', emit: results
script:
"""
#!/usr/bin/env bash
set -e
mkdir -p capsule
mkdir -p capsule/data
mkdir -p capsule/results
mkdir -p capsule/scratch
if [[ ${params.executor} == "slurm" ]]; then
echo "[${task.tag}] allocated task time: ${task.time}"
# Make sure N_JOBS matches allocated CPUs on SLURM
export N_JOBS_EXT=${task.cpus}
fi
echo "[${task.tag}] cloning git repo..."
${gitCloneFunction}
clone_repo "${params.versions['SPIKESORT_LUPIN_REPO']}" "${params.versions['SPIKESORT_LUPIN_COMMIT']}"
echo "[${task.tag}] running capsule..."
cd capsule/code
chmod +x run
./run ${spikesorting_args} ${job_args}
echo "[${task.tag}] completed!"
"""
}
process postprocessing {
tag 'postprocessing'
def container_name = "ghcr.io/allenneuraldynamics/aind-ephys-pipeline-base:${params.container_tag}"
container container_name
input:
val max_duration_minutes
path ecephys_session_input, stageAs: 'capsule/data/ecephys_session'
path job_dispatch_results, stageAs: 'capsule/data/*'
path preprocessing_results, stageAs: 'capsule/data/*'
path spikesort_results, stageAs: 'capsule/data/*'
output:
path 'capsule/results/*', emit: results
script:
"""
#!/usr/bin/env bash
set -e
${extra_installs_echo}
${extra_installs_cmd}
mkdir -p capsule
mkdir -p capsule/data
mkdir -p capsule/results
mkdir -p capsule/scratch
if [[ ${params.executor} == "slurm" ]]; then
echo "[${task.tag}] allocated task time: ${task.time}"
# Make sure N_JOBS matches allocated CPUs on SLURM
export N_JOBS_EXT=${task.cpus}
fi
echo "[${task.tag}] cloning git repo..."
${gitCloneFunction}
clone_repo "${params.versions['POSTPROCESSING_REPO']}" "${params.versions['POSTPROCESSING_COMMIT']}"
echo "[${task.tag}] running capsule..."
cd capsule/code
chmod +x run
./run ${postprocessing_args} ${job_args}
echo "[${task.tag}] completed!"
"""
}
process curation {
tag 'curation'
def container_name = "ghcr.io/allenneuraldynamics/aind-ephys-pipeline-base:${params.container_tag}"
container container_name
input:
val max_duration_minutes
path postprocessing_results, stageAs: 'capsule/data/*'
output:
path 'capsule/results/*', emit: results
script:
"""
#!/usr/bin/env bash
set -e
mkdir -p capsule
mkdir -p capsule/data
mkdir -p capsule/results
mkdir -p capsule/scratch
if [[ ${params.executor} == "slurm" ]]; then
echo "[${task.tag}] allocated task time: ${task.time}"
# Make sure N_JOBS matches allocated CPUs on SLURM
export N_JOBS_EXT=${task.cpus}
fi
echo "[${task.tag}] cloning git repo..."
${gitCloneFunction}
clone_repo "${params.versions['CURATION_REPO']}" "${params.versions['CURATION_COMMIT']}"
echo "[${task.tag}] running capsule..."
cd capsule/code
chmod +x run
./run ${curation_args} ${job_args}
echo "[${task.tag}] completed!"
"""
}
process visualization {
tag 'visualization'
def container_name = "ghcr.io/allenneuraldynamics/aind-ephys-pipeline-base:${params.container_tag}"
container container_name
input:
val max_duration_minutes
path ecephys_session_input, stageAs: 'capsule/data/ecephys_session'
path job_dispatch_results, stageAs: 'capsule/data/*'
path preprocessing_results, stageAs: 'capsule/data/*'
path spikesort_results, stageAs: 'capsule/data/*'
path postprocessing_results, stageAs: 'capsule/data/*'
path curation_results, stageAs: 'capsule/data/*'
output:
path 'capsule/results/*', emit: results
script:
"""
#!/usr/bin/env bash
set -e
${extra_installs_echo}
${extra_installs_cmd}
mkdir -p capsule
mkdir -p capsule/data
mkdir -p capsule/results
mkdir -p capsule/scratch
if [[ ${params.executor} == "slurm" ]]; then
echo "[${task.tag}] allocated task time: ${task.time}"
# Make sure N_JOBS matches allocated CPUs on SLURM
export N_JOBS_EXT=${task.cpus}
fi
echo "[${task.tag}] cloning git repo..."
${gitCloneFunction}
clone_repo "${params.versions['VISUALIZATION_REPO']}" "${params.versions['VISUALIZATION_COMMIT']}"
echo "[${task.tag}] running capsule..."
cd capsule/code
chmod +x run
./run ${visualization_kwargs}
echo "[${task.tag}] completed!"
"""
}
process results_collector {
tag 'result-collector'
def container_name = "ghcr.io/allenneuraldynamics/aind-ephys-pipeline-base:${params.container_tag}"
container container_name
publishDir "$RESULTS_PATH", saveAs: { filename -> new File(filename).getName() }, mode: 'copy'
input:
val max_duration_minutes
path ecephys_session_input, stageAs: 'capsule/data/ecephys_session'
path job_dispatch_results, stageAs: 'capsule/data/*'
path preprocessing_results, stageAs: 'capsule/data/*'
path spikesort_results, stageAs: 'capsule/data/*'
path postprocessing_results, stageAs: 'capsule/data/*'
path curation_results, stageAs: 'capsule/data/*'
path visualization_results, stageAs: 'capsule/data/*'
output:
path 'capsule/results/*', emit: results
path 'capsule/results/*', emit: nwb_data
path 'capsule/results/*', emit: qc_data
script:
"""
#!/usr/bin/env bash
set -e
${extra_installs_echo}
${extra_installs_cmd}
mkdir -p capsule
mkdir -p capsule/data
mkdir -p capsule/results
mkdir -p capsule/scratch
if [[ ${params.executor} == "slurm" ]]; then
echo "[${task.tag}] allocated task time: ${task.time}"
fi
echo "[${task.tag}] cloning git repo..."
${gitCloneFunction}
clone_repo "${params.versions['RESULTS_COLLECTOR_REPO']}" "${params.versions['RESULTS_COLLECTOR_COMMIT']}"
echo "[${task.tag}] running capsule..."
cd capsule/code
chmod +x run
./run --pipeline-data-path ${DATA_PATH} --pipeline-results-path ${RESULTS_PATH}
echo "[${task.tag}] completed!"
"""
}
process quality_control {
tag 'quality-control'
def container_name = "ghcr.io/allenneuraldynamics/aind-ephys-pipeline-base:${params.container_tag}"
container container_name
input:
val max_duration_minutes
path ecephys_session_input, stageAs: 'capsule/data/ecephys_session'
path job_dispatch_results, stageAs: 'capsule/data/*'
path results_data, stageAs: 'capsule/data/*'
output:
path 'capsule/results/*', emit: results
script:
"""
#!/usr/bin/env bash
set -e
${extra_installs_echo}
${extra_installs_cmd}
mkdir -p capsule
mkdir -p capsule/data
mkdir -p capsule/results
mkdir -p capsule/scratch
if [[ ${params.executor} == "slurm" ]]; then
echo "[${task.tag}] allocated task time: ${task.time}"
# Make sure N_JOBS matches allocated CPUs on SLURM
export N_JOBS_EXT=${task.cpus}
fi
echo "[${task.tag}] cloning git repo..."
${gitCloneFunction}
clone_repo "${params.versions['QUALITY_CONTROL_REPO']}" "${params.versions['QUALITY_CONTROL_COMMIT']}"
echo "[${task.tag}] running capsule..."
cd capsule/code
chmod +x run
./run --pipeline-data-path ${DATA_PATH}
echo "[${task.tag}] completed!"
"""
}
process quality_control_collector {
tag 'qc-collector'
def container_name = "ghcr.io/allenneuraldynamics/aind-ephys-pipeline-base:${params.container_tag}"
container container_name
publishDir "$RESULTS_PATH", saveAs: { filename -> new File(filename).getName() }, mode: 'copy'
input:
val max_duration_minutes
path quality_control_results, stageAs: 'capsule/data/*'
output:
path 'capsule/results/*'
script:
"""
#!/usr/bin/env bash
set -e
mkdir -p capsule
mkdir -p capsule/data
mkdir -p capsule/results
mkdir -p capsule/scratch
if [[ ${params.executor} == "slurm" ]]; then
echo "[${task.tag}] allocated task time: ${task.time}"
fi
echo "[${task.tag}] cloning git repo..."
${gitCloneFunction}
clone_repo "${params.versions['QUALITY_CONTROL_COLLECTOR_REPO']}" "${params.versions['QUALITY_CONTROL_COLLECTOR_COMMIT']}"
echo "[${task.tag}] running capsule..."
cd capsule/code
chmod +x run
./run
echo "[${task.tag}] completed!"
"""
}
process nwb_ecephys {
tag 'nwb-ecephys'
def container_name = "ghcr.io/allenneuraldynamics/aind-ephys-pipeline-nwb:${params.container_tag}"
container container_name
input:
val max_duration_minutes
path ecephys_session_input, stageAs: 'capsule/data/ecephys_session'
path job_dispatch_results, stageAs: 'capsule/data/*'
output:
path 'capsule/results/*', emit: results
script:
"""
#!/usr/bin/env bash
set -e
${extra_installs_echo}
${extra_installs_cmd}
mkdir -p capsule
mkdir -p capsule/data
mkdir -p capsule/results
mkdir -p capsule/scratch
if [[ ${params.executor} == "slurm" ]]; then
echo "[${task.tag}] allocated task time: ${task.time}"
# Make sure N_JOBS matches allocated CPUs on SLURM
export N_JOBS_EXT=${task.cpus}
fi
echo "[${task.tag}] cloning git repo..."
${gitCloneFunction}
clone_repo "${params.versions['NWB_ECEPHYS_REPO']}" "${params.versions['NWB_ECEPHYS_COMMIT']}"
echo "[${task.tag}] running capsule..."
cd capsule/code
chmod +x run
./run ${nwb_ecephys_args}
echo "[${task.tag}] completed!"
"""
}
process nwb_units {
tag 'nwb-units'
def container_name = "ghcr.io/allenneuraldynamics/aind-ephys-pipeline-nwb:${params.container_tag}"
container container_name
publishDir "$RESULTS_PATH/nwb", saveAs: { filename -> new File(filename).getName() }, mode: 'copy'
input:
val max_duration_minutes
path ecephys_session_input, stageAs: 'capsule/data/ecephys_session'
path job_dispatch_results, stageAs: 'capsule/data/*'
path results_data, stageAs: 'capsule/data/*'
path nwb_ecephys_results, stageAs: 'capsule/data/*'
output:
path 'capsule/results/*'
script:
"""
#!/usr/bin/env bash
set -e
${extra_installs_echo}
${extra_installs_cmd}
mkdir -p capsule
mkdir -p capsule/data
mkdir -p capsule/results
mkdir -p capsule/scratch
echo "[${task.tag}] cloning git repo..."
${gitCloneFunction}
clone_repo "${params.versions['NWB_UNITS_REPO']}" "${params.versions['NWB_UNITS_COMMIT']}"
if [[ ${params.executor} == "slurm" ]]; then
echo "[${task.tag}] allocated task time: ${task.time}"
fi
echo "[${task.tag}] running capsule..."
cd capsule/code
chmod +x run
./run
echo "[${task.tag}] completed!"
"""
}
workflow {
// Input channel from ecephys path
ecephys_ch = Channel.fromPath(params.ecephys_path + "/", type: 'any')
// Job dispatch
job_dispatch_out = job_dispatch(ecephys_ch.collect())
max_duration_file = job_dispatch_out.max_duration_file
max_duration_minutes = max_duration_file.map { it.text.trim() }
max_duration_minutes.view { "Max recording duration: ${it}min" }
// Preprocessing
preprocessing_out = preprocessing(
max_duration_minutes,
ecephys_ch.collect(),
job_dispatch_out.results.flatten()
)
// Spike sorting based on selected sorter
// def spikesort
if (sorter == 'kilosort25') {
spikesort_out = spikesort_kilosort25(
max_duration_minutes,
preprocessing_out.results
)
} else if (sorter == 'kilosort4') {
spikesort_out = spikesort_kilosort4(
max_duration_minutes,
preprocessing_out.results
)
} else if (sorter == 'spykingcircus2') {
spikesort_out = spikesort_spykingcircus2(
max_duration_minutes,
preprocessing_out.results
)
} else if (sorter == 'lupin') {
spikesort_out = spikesort_lupin(
max_duration_minutes,
preprocessing_out.results
)
} else {
error "Unsupported sorter: ${sorter}"
}
// Postprocessing
postprocessing_out = postprocessing(
max_duration_minutes,
ecephys_ch.collect(),
job_dispatch_out.results.flatten(),
preprocessing_out.results.collect(),
spikesort_out.results.collect()
)
// Curation
curation_out = curation(
max_duration_minutes,
postprocessing_out.results
)
// Visualization
visualization_out = visualization(
max_duration_minutes,
ecephys_ch.collect(),
job_dispatch_out.results.collect(),
preprocessing_out.results,
spikesort_out.results.collect(),
postprocessing_out.results.collect(),
curation_out.results.collect()
)
// Results collection
results_collector_out = results_collector(
max_duration_minutes,
ecephys_ch.collect(),
job_dispatch_out.results.collect(),
preprocessing_out.results.collect(),
spikesort_out.results.collect(),
postprocessing_out.results.collect(),
curation_out.results.collect(),
visualization_out.results.collect()
)
// Quality control
quality_control_out = quality_control(
max_duration_minutes,
ecephys_ch.collect(),
job_dispatch_out.results.flatten(),
results_collector_out.qc_data.collect()
)
// Quality control collection
quality_control_collector(
max_duration_minutes,
quality_control_out.results.collect()
)
// NWB ecephys
nwb_ecephys_out = nwb_ecephys(
max_duration_minutes,
ecephys_ch.collect(),
job_dispatch_out.results.collect(),
)
// NWB units
nwb_units(
max_duration_minutes,
ecephys_ch.collect(),
job_dispatch_out.results.collect(),
results_collector_out.nwb_data.collect(),
nwb_ecephys_out.results.collect()
)
}