-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.nf
1434 lines (1075 loc) · 50.7 KB
/
main.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
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
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env nextflow
tertiaryWorkflow = params.tertiaryWorkflow
overwrite = params.overwrite
dropletProtocols = [ '10xv1', '10xv1a', '10xv1i', '10xv2', '10xv3', 'drop-seq', 'seq-well', '10x5prime' ]
enaSshUser = 'null'
if ( params.containsKey('enaSshUser') ){
enaSshUser = params.enaSshUser
}
galaxyCredentials = ''
if ( params.containsKey('galaxyCredentials')){
galaxyCredentials = params.galaxyCredentials
}
galaxyInstance = ''
if ( params.containsKey('galaxyInstance')){
galaxyInstance = params.galaxyInstance
}
skipQuantification = 'no'
skipAggregation = 'no'
skipTertiary = 'no'
if ( params.containsKey('skipTertiary') && params.skipTertiary == 'yes'){
// Skipping tertiary means that for consistency we must also skip the
// quantification and aggregations
skipTertiary = 'yes'
skipQuantification = 'yes'
skipAggregation = 'yes'
}
else if ( params.containsKey('skipAggregation') && params.skipAggregation == 'yes'){
// Skipping aggregation implies skipping quantifiction
skipQuantification = 'yes'
skipAggregation = 'yes'
} else if ( params.containsKey('skipQuantification') && params.skipQuantification == 'yes'){
skipQuantification = 'yes'
}
// Now we pick the SDRFs to use
GIT_IDFS = Channel.fromPath("$SCXA_WORKFLOW_ROOT/metadata/**/*.idf.txt", checkIfExists: true).map{ f -> tuple("${f.simpleName}", f) }
// If user has supplied an experiment ID, then filter SDRFs to just that
// experiment. Otherwise set a blank filter to include all results
doneSuffix=''
idfFilter = ''
if ( params.containsKey('expName')){
idfFilter="${params.expName}"
doneSuffix=".${params.expName}"
}
// Do a join on the SDRF channels to get single tuple per experiment ID, then
// pick the first. This will uniquify by experiment ID and prioritise the Git
// IDFs
IDF = GIT_IDFS
.filter( ~/.*\/.*${idfFilter}\..*/ )
.map{ r -> tuple(r[0], r[1]) }
// Working from the IDF location, derive SDRF and related files
process compile_metadata {
input:
set val(expName), file(idfFile) from IDF
output:
set val(expName), file(idfFile), file("*.sdrf.txt") optional true into SDRF_IDF
set val(expName), file("${expName}.cells.txt") optional true into CELLS
"""
compileExpMetadata.sh $expName $idfFile $overwrite "${doneSuffix}"
"""
}
SDRF_IDF
.join(CELLS, remainder: true)
.into{
COMPILED_METADATA_FOR_STATUS
COMPILED_METADATA_FOR_CELLTYPE
}
// Check the update status of the experiment
process checkExperimentStatus {
cache false
input:
set val(expName), file(idfFile), file(sdrfFile), file(cellsFile) from COMPILED_METADATA_FOR_STATUS
output:
set val(expName), file(idfFile), file(sdrfFile), file(cellsFile), stdout into COMPILED_METADATA_WITH_STATUS
"""
checkExperimentStatus.sh $expName $idfFile $sdrfFile $cellsFile $overwrite
"""
}
// Pipe experiments to different channels depending on update status
UPDATED_EXPERIMENTS = Channel.create()
NOT_UPDATED_EXPERIMENTS = Channel.create()
COMPILED_METADATA_WITH_STATUS.choice( UPDATED_EXPERIMENTS, NOT_UPDATED_EXPERIMENTS ) {a ->
a[4] == 'old' ? 1 : 0
}
// Split the experiment by species- multiple-sepcies experiments will produce
// multiple bundles
process split_by_species {
input:
set val(expName), file(idfFile), file(sdrfFile), file(cellsFile), val(expStatus) from UPDATED_EXPERIMENTS.take(params.numExpsProcessedAtOnce)
output:
set val(expName), file(idfFile), file('split_by_species/*'), file(cellsFile) into MULTISPECIES_META
"""
splitSDRFBySpecies.sh ${sdrfFile} split_by_species
"""
}
// Extract species from the split SDRF file names
MULTISPECIES_META
.transpose()
.map{ row -> tuple( row[0], row[2].toString().split('\\.')[1], row[1], row[2], row[3] ) }
.into{
META_WITH_SPECIES
META_FOR_REF
}
// Need to adjust the IDF to match the SDRF. This is necessary for when we're
// doing the SDRF condensation later
process adjust_idf_for_sdrf{
input:
set val(expName), val(species), file(idfFile), file(sdrfFile), file(cellsFile) from META_WITH_SPECIES
output:
set val("${expName}-${species}"), file("${expName}.${species}.idf.txt"), file(sdrfFile), file(cellsFile) into META_WITH_SPECIES_IDF
set val("${expName}-${species}"), val(expName), val(species) into ES_TAGS
"""
outFile="${expName}.${species}.idf.txt"
cp ${idfFile} \${outFile}.tmp
sed -i 's/${expName}.sdrf.txt/${expName}.${species}.sdrf.txt/' \${outFile}.tmp
mv \${outFile}.tmp \${outFile}
"""
}
// Experiment/ species tags
ES_TAGS.unique().into{
ES_TAGS_FOR_CONFIG
ES_TAGS_FOR_CONDENSE
ES_TAGS_FOR_META_MATCHING
ES_TAGS_FOR_GENE_ANNO
ES_TAGS_FOR_TERTIARY
ES_TAGS_FOR_REUSE_TERTIARY
ES_TAGS_FOR_REUSE_AGG
ES_TAGS_FOR_BUNDLING
}
META_WITH_SPECIES_IDF
.into{
META_WITH_SPECIES_FOR_QUANT
META_WITH_SPECIES_FOR_TERTIARY
}
// Generate the configuration file and parse it to derive protocol. Also derive
// an SDRF file containing only things relevant for analysis. This can be used
// to determine if any SDRF changes impact on analysis and necessitate re-run.
// The fundamental processing unit is experiment/ species/ protocol, and this
// is the first process that those combinations are determined.
//
// NOTE: the references are included in the input just so that they get passed
// through to the protocol-wise channels, not used in the process itself.
process generate_configs {
cache 'deep'
conda 'r-base r-optparse r-data.table r-workflowscriptscommon pyyaml'
input:
set val(esTag), val(expName), val(species), file(idfFile), file(sdrfFile), file(cellsFile) from ES_TAGS_FOR_CONFIG.join(META_WITH_SPECIES_FOR_QUANT)
output:
set val(expName), val(species), file("*.${species}.${expName}.conf"), file("*.${species}.${expName}.meta_for_quant.txt"), file("*.${species}.${expName}.meta_for_tertiary.txt") into CONF_ANALYSIS_META_BY_EXP_SPECIES
"""
# Cells file will be empty (from reminder: true above) for some experiments
cells_options=
cells_filesize=\$(stat --printf="%s" \$(readlink ${cellsFile}))
if [ \$cells_filesize -gt 0 ]; then
cells_options="--cells=$cellsFile"
fi
if [ -n "$params.cellAnalysisFields" ]; then
cells_options="\$cells_options --cell_meta_fields=\\"$params.cellAnalysisFields\\""
if [ -n "$params.cellTypeField" ]; then
cells_options="\$cells_options --cell_type_fields=\\"$params.cellTypeField\\""
fi
fi
mkdir -p tmp
eval "sdrfToNfConf.R \
--sdrf=\$(readlink $sdrfFile) \
--idf=$idfFile \$cells_options \
--name=$expName \
--verbose \
--out_conf \$(pwd)"
"""
}
// The transpose step below gives us the protocol-wise configs, converting:
// [ 'E-MTAB-1234', 'rabbit', [ '10xv2.rabbit.E-MTAB-1234.conf', '10xv3.rabbit.E-MTAB-1234.conf' ], [ '10xv2.rabbit.E-MTAB-1234.meta_for_quant.txt', '10xv3.rabbit.E-MTAB-1234.meta_for_quant.txt' ], ['10xv2.rabbit.E-MTAB-1234.meta_for_tertiary.txt', '10xv3.rabbit.E-MTAB-1234.meta_for_tertiary.txt' ] ]
// to:
//
// [ 'E-MTAB-1234', 'rabbit', '10xv2', '10xv2.rabbit.E-MTAB-1234.conf', '10xv2.rabbit.E-MTAB-1234.meta_for_quant.txt', '10xv2.rabbit.E-MTAB-1234.meta_for_tertiary.txt' ]
// [ 'E-MTAB-1234', 'rabbit', '10xv3', '10xv3.rabbit.E-MTAB-1234.conf', '10xv3.rabbit.E-MTAB-1234.meta_for_quant.txt', '10xv3.rabbit.E-MTAB-1234.meta_for_tertiary.txt' ]
//
// Protocol is first part of '.' - separated file name from sdrfToNfConf, so we
// can use simpleName to extract it
CONF_ANALYSIS_META_BY_EXP_SPECIES
.transpose()
.map { r -> tuple(r[0], r[1], r[2].simpleName, r[2], r[3], r[4]) }
.set{ CONF_ANALYSIS_META_BY_EXP_SPECIES_PROTOCOL }
///////////////////////////////////////////////////////////////////////////////
// In this section we use the above-generated config to learn about the
// experiments. What combinations of experiment, species and protocol do we
// have? Which ones are droplet? Have controlled access expeirments been
// properly provided for?
///////////////////////////////////////////////////////////////////////////////
// Check for controlled access status. This will exclude an experiment unless
// all requirements on user, directory etc are in place for a controlled access
// quantification. This process is intentionally not parallelised with others,
// to act as a bottle neck and cause the experiment to cease processing unless
// this check passes.
//
// We also create compact tags to be used here so we don't always have to pass
// around exp/species/protocol
process check_controlled_access{
conda 'pyyaml'
cache false
input:
set val(expName), val(species), val(protocol), file(confFile), file(metaForQuant), file(metaForTertiary) from CONF_ANALYSIS_META_BY_EXP_SPECIES_PROTOCOL
output:
set val("${expName}-${species}-${protocol}"), val(expName), val(species), val(protocol) into ESP_TAGS
set val("${expName}-${species}-${protocol}"), file(confFile) into CONF_BY_EXP_SPECIES_PROTOCOL
set val("${expName}-${species}-${protocol}"), file(metaForQuant), file(metaForTertiary) into ANALYSIS_META_BY_EXP_SPECIES_PROTOCOL
"""
checkForControlledAccess.sh $expName $confFile
"""
}
ANALYSIS_META_BY_EXP_SPECIES_PROTOCOL.into{
ANALYSIS_META_FOR_CHANGEDCHECK
ANALYSIS_META_FOR_QUANT
}
CONF_BY_EXP_SPECIES_PROTOCOL.into{
CONF_BY_EXP_SPECIES_PROTOCOL_FOR_ES_CONFIG
CONF_BY_EXP_SPECIES_PROTOCOL_FOR_EXTEND
}
// Experiment/ species/ protocol tags
ESP_TAGS.into{
ESP_TAGS_FOR_AGG_ROUTING
ESP_TAGS_FOR_ES_CONFIG
ESP_TAGS_FOR_PROT_LIST
ESP_TAGS_FOR_CONTAMINATION
ESP_TAGS_FOR_CHANGEDCHECK
ESP_TAGS_FOR_EXTEND
ESP_TAGS_FOR_MARKING
ESP_TAGS_FOR_REFERENCE
ESP_TAGS_FOR_SYNCH_REFS
ESP_TAGS_FOR_QUANT_CHECK
ESP_TAGS_FOR_REUSE_QUANT
ESP_TAGS_FOR_QUANT
ESP_TAGS_FOR_AGGR
ESP_TAGS_FOR_IS_DROPLET
ESP_TAGS_FOR_PRIVACY_CHECK
}
// Make comma-separated list of the protocols for each exp/species combo
ESP_TAGS_FOR_PROT_LIST
.map{r -> tuple(r[1], r[2], r[3])}
.groupTuple( by: [0,1] )
.map{r -> tuple((r[0] + '-' + r[1]).toString(), r[2].join(","))}
.set{
PROTOCOLS_BY_EXP_SPECIES
}
// Take the first config file in a species/ experiment combo to use in
// processes not dependent on protocol
ESP_TAGS_FOR_ES_CONFIG
.join(CONF_BY_EXP_SPECIES_PROTOCOL_FOR_ES_CONFIG)
.groupTuple( by: [1,2] )
.map{ r -> tuple( (r[1] + '-' + r[2]).toString(), r[4][0] ) }
.into{
CONF_BY_EXP_SPECIES_FOR_CELLMETA
CONF_BY_EXP_SPECIES_FOR_CELLTYPE
CONF_BY_EXP_SPECIES_FOR_TERTIARY
CONF_BY_EXP_SPECIES_FOR_BUNDLE
}
// Extend config for protocol-wise info
process extend_config_for_protocol{
input:
set val(espTag), val(expName), val(species), val(protocol), file(confFile) from ESP_TAGS_FOR_EXTEND.join(CONF_BY_EXP_SPECIES_PROTOCOL_FOR_EXTEND)
output:
set val(espTag), file("${expName}.${species}.${protocol}.conf") into EXTENDED_CONF
"""
# Add in protocol-specific parameters by 'include'ing external configs
confFileOut=${expName}.${species}.${protocol}.conf
protocolConfig=${baseDir}/conf/protocol/${protocol}.conf
if [ ! -e \$protocolConfig ]; then
echo "\$protocolConfig does not exist" 1>&2
exit 1
fi
echo "includeConfig '${baseDir}/params.config'" > \${confFileOut}.tmp
echo "includeConfig '\$protocolConfig'" >> \${confFileOut}.tmp
cat $confFile >> \${confFileOut}.tmp
mv \${confFileOut}.tmp \${confFileOut}
"""
}
EXTENDED_CONF.into{
EXTENDED_CONF_FOR_CHANGED_CHECK
EXTENDED_CONF_FOR_REF
EXTENDED_CONF_FOR_QUANT
EXTENDED_CONF_FOR_CELL_TO_LIB
}
// Mark droplet protocols
process mark_droplet {
input:
set val(espTag), val(expName), val(species), val(protocol) from ESP_TAGS_FOR_MARKING
output:
set val(espTag), stdout into IS_DROPLET
script:
def isDroplet='False'
if ( dropletProtocols.contains(protocol) ){
isDroplet='True'
}
"""
echo -n "$isDroplet"
"""
}
// Define droplet status at the level of exp/ species/ protocol AND exp/
// species. Not sure how to treat experiments with droplet AND non-droplet, so
// prevent it
IS_DROPLET.into{
IS_DROPLET_PROT
IS_DROPLET_FOR_EXP_SPECIES_TEST
IS_DROPLET_FOR_REUSE_QUANT
IS_DROPLET_FOR_TRANSCRIPT_TO_GENE
}
ESP_TAGS_FOR_IS_DROPLET
.join(IS_DROPLET_FOR_EXP_SPECIES_TEST)
.map{ r -> tuple ( r[1], r[2], r[4]) }
.unique()
.groupTuple( by: [0,1] )
.map{ r -> tuple(r[0], r[1], r[2].unique()) }
.set{
EXP_SPECIES_IS_DROPLETS
}
process check_droplet_status {
input:
set val(expName), val(species), val(isDroplets) from EXP_SPECIES_IS_DROPLETS
output:
set val("${expName}-${species}"), stdout into IS_DROPLET_EXP_SPECIES
script:
length=isDroplets.size()
first=isDroplets[0]
"""
if [ $length -gt 1 ]; then
echo "Experiment $expName has both droplet and non-droplet protocols" 1>&2
exit 1
else
echo -n "$first"
fi
"""
}
IS_DROPLET_EXP_SPECIES.into{
IS_DROPLET_EXP_SPECIES_FOR_CELLMETA
IS_DROPLET_EXP_SPECIES_FOR_TERTIARY
}
///////////////////////////////////////////////////////////////////////////////
// Processes and transformations in this channel will decide how data are
// re-used or re-analysed
///////////////////////////////////////////////////////////////////////////////
// Where analysis is already present, check that the config has actually
// changed in a way that impacts on analysis. This is distinct from
// checkExperimentStatus(), which just flags where the source SDRF has a newer
// timestamp than the analysis. We could have multiple protocol-wise files, but
// the status is returned at the experiment/ species level (note the use of
// groupTuple() below). Quantification is the only stage peformed at the
// protocol-wise level, and if one protocol of a set is being re-quantified we
// should probbly do the others for consistency in reference usage (we normally
// use the most up-to-date reference when quantifying). We could do reference
// checks to prevent unnecessary re-computation of protocols in multi-protocol
// experiments, but that logic seems unnecessary complication to the logic
// right now.
ESP_TAGS_FOR_CHANGEDCHECK
.join(EXTENDED_CONF_FOR_CHANGED_CHECK)
.join(ANALYSIS_META_FOR_CHANGEDCHECK)
.groupTuple( by: [1,2])
.set{
CHANGED_CHECK_INPUTS
}
process check_experiment_changed{
publishDir "$SCXA_CONF/study", mode: 'copy', overwrite: true
input:
set val(espTags), val(expName), val(species), val(protocols), file('confFile/*'), file('metaForQuant/*'), file('metaForTertiary/*') from CHANGED_CHECK_INPUTS
output:
set val(expName), val(species), val(espTags), stdout into CHANGE_STATUS
set file("*.conf"), file("*.meta_for_quant.txt"), file("*.meta_for_tertiary.txt") optional true
"""
expStatus=\$(checkExperimentChanges.sh $expName $species confFile metaForQuant metaForTertiary $skipQuantification $skipAggregation $skipTertiary $overwrite)
if [ "\$expStatus" != 'unchanged' ]; then
cp -P confFile/* metaForTertiary/* metaForQuant/* .
fi
echo -n "\$expStatus"
"""
}
// For no changes for any of the protocol-wise secions of an experiment, we're
// re-reporting the bundle lines, so no further action required
NEW_OR_CHANGED_EXPERIMENTS = Channel.create()
NOT_CHANGED_EXPERIMENTS = Channel.create()
CHANGE_STATUS.choice( NEW_OR_CHANGED_EXPERIMENTS, NOT_CHANGED_EXPERIMENTS ){a ->
a[3] == 'unchanged' ? 1 : 0
}
NOT_CHANGED_EXPERIMENTS
.map{r -> tuple(r[0], r[1])}
.set{
NOT_CHANGED_EXPERIMENTS_FOR_BUNDLES
}
// For experiments we know are due for re-analysis, remove pre-existing
// results (where appropriate). This is also the time to publish the config
// files, overwriting any already in place.
process reset_experiment{
cache 'deep'
input:
set val(expName), val(species), val(espTags), val(expStatus) from NEW_OR_CHANGED_EXPERIMENTS
output:
set val(espTags), val(expStatus) into NEW_OR_RESET_EXPERIMENTS
"""
# Only remove downstream results where we're not re-using them
if [ "$expStatus" = 'changed_for_quantification' ];then
reset_stages="quantification reference aggregation scanpy bundle"
elif [ "$expStatus" = 'changed_for_aggregation' ];then
reset_stages="aggregation scanpy bundle"
elif [ "$expStatus" = 'changed_for_tertiary' ];then
reset_stages="scanpy bundle"
else
reset_stages="bundle"
fi
rm -f $TMPDIR/${expName}.${species}.galaxystate
for stage in \$reset_stages; do
rm -rf $SCXA_RESULTS/$expName/$species/\$stage
done
"""
}
NEW_OR_RESET_EXPERIMENTS
.transpose()
.into{
EXPERIMENTS_FOR_REF
EXPERIMENTS_FOR_ANALYSIS
}
// We've done the right resets, good to go with any new analysis. Route
// experiments different ways depending on in what way a change has been
// observed
TO_RETERTIARY = Channel.create()
TO_REAGGREGATE = Channel.create()
TO_QUANTIFY = Channel.create()
NOT_CHANGED_FOR_QUANT = Channel.create()
NOT_CHANGED_FOR_AGGREGATION = Channel.create()
NOT_CHANGED_FOR_TERTIARY = Channel.create()
// Re-quantify a whole experiment if any of its protocol-wise components
// require it
EXPERIMENTS_FOR_ANALYSIS.join(ESP_TAGS_FOR_QUANT_CHECK).groupTuple( by: [2,3]).choice( TO_QUANTIFY, NOT_CHANGED_FOR_QUANT ) {a ->
a[1].contains('changed_for_quantification') ? 0 : 1
}
TO_QUANTIFY
.transpose()
.map{ r -> tuple( r[0] ) }
.into{
TO_QUANTIFY_FOR_QUANT
TO_QUANTIFY_FOR_REFERENCE
TO_QUANTIFY_FOR_PRIVACY_CHECK
}
NOT_CHANGED_FOR_QUANT
.transpose()
.map{ r -> tuple( r[0], r[1]) }
.into{
NOT_CHANGED_FOR_QUANT_FOR_REFERENCES
NOT_CHANGED_FOR_QUANT_FOR_QUANT
NOT_CHANGED_FOR_QUANT_FOR_REUSE_QUANT
NOT_CHANGED_FOR_QUANT_FOR_ROUTING
}
// Quantification-subseqent steps have protocols merged
NOT_CHANGED_FOR_QUANT_FOR_ROUTING.choice( TO_REAGGREGATE, NOT_CHANGED_FOR_AGGREGATION ) {a ->
a[1] == 'changed_for_aggregation' ? 0 : 1
}
NOT_CHANGED_FOR_AGGREGATION // esp_tag, expStatus
.join( ESP_TAGS_FOR_AGG_ROUTING ) // esp_tag, expStatus, expName, species, protocol
.map{ r -> tuple( (r[2] + '-' + r[3]).toString(), r[1] ) } // es_tag, expStatus
.into{
NOT_CHANGED_FOR_AGGREGATION_FOR_ROUTING
NOT_CHANGED_FOR_AGGREGATION_FOR_REUSE_AGG
}
NOT_CHANGED_FOR_AGGREGATION_FOR_ROUTING.choice( TO_RETERTIARY, NOT_CHANGED_FOR_TERTIARY ) {a ->
a[1] == 'changed_for_tertiary' ? 0 : 1
}
NOT_CHANGED_FOR_TERTIARY
.map{r -> tuple(r[0])}
.into{
NOT_CHANGED_FOR_TERTIARY_FOR_REUSE_TERTIARY
TO_REBUNDLE
}
///////////////////////////////////////////////////////////////////////////////
// Processes to derive results to re-use. These will be combined with novel
// results from other channels as appropriate
///////////////////////////////////////////////////////////////////////////////
// This process re-uses previously generated quantifcation results
process reuse_quantifications {
executor 'local'
input:
set val(espTag), val(expStatus), val(expName), val(species), val(protocol), val(isDroplet) from NOT_CHANGED_FOR_QUANT_FOR_QUANT.join(ESP_TAGS_FOR_REUSE_QUANT).join(IS_DROPLET_FOR_REUSE_QUANT)
output:
set val(espTag), file("results/*") into REUSED_QUANT_RESULTS
set val("${expName}-${species}"), file("*.fa.gz"), file("*.gtf.gz") into REUSED_REFERENCES
set val(espTag), file('transcript_to_gene.txt') into REUSED_TRANSCRIPT_TO_GENE
set val("${expName}-${species}"), file('gene_annotation.txt') into REUSED_GENE_META
"""
retrieveStoredFiles.sh $expName $species reference "*.fa.gz *.gtf.gz transcript_to_gene.txt gene_annotation.txt"
mkdir -p results
if [ $isDroplet == 'True' ]; then
retrieveStoredFiles.sh $expName $species quantification "$protocol/alevin" results/alevin
else
retrieveStoredFiles.sh $expName $species quantification "$protocol/kallisto" results/kallisto
fi
"""
}
// Use existing aggregations if specified. Note that the aggregations can only
// be reused if the quantifications are reused to maintain consistency, so the
// QUANT_RESULTS channel cannot be allowed to go to reuse_aggregation
process reuse_aggregation {
executor 'local'
input:
set val(esTag), val(expStatus), val(expName), val(species) from NOT_CHANGED_FOR_AGGREGATION_FOR_REUSE_AGG.join(ES_TAGS_FOR_REUSE_AGG)
output:
set val(esTag), file("matrices/counts_mtx.zip") into REUSED_COUNT_MATRICES
set val(esTag), file("matrices/tpm_mtx.zip") optional true into REUSED_TPM_MATRICES
set val(esTag), file("matrices/stats.tsv") optional true into REUSED_KALLISTO_STATS
set val(esTag), file("${expName}.${species}.condensed-sdrf.tsv") into REUSED_CONDENSED
set val(esTag), file("${expName}-${species}.metadata.matched.tsv") into REUSED_MATCHED_META
"""
retrieveStoredFiles.sh $expName $species aggregation matrices
retrieveStoredFiles.sh $expName $species metadata "${expName}-${species}.metadata.matched.tsv ${expName}.${species}.condensed-sdrf.tsv"
"""
}
REUSED_COUNT_MATRICES.into{
REUSED_COUNT_MATRICES_FOR_TERTIARY
REUSED_COUNT_MATRICES_FOR_BUNDLING
}
// Derive existing tertiary results for cases where we're just re-bundling
process reuse_tertiary {
executor 'local'
input:
set val(esTag), val(expName), val(species) from NOT_CHANGED_FOR_TERTIARY_FOR_REUSE_TERTIARY.join(ES_TAGS_FOR_REUSE_TERTIARY)
output:
set val(esTag), file("matrices/raw_filtered.zip"), file("matrices/filtered_normalised.zip"), file("clusters_for_bundle.txt"), file("umap"), file("tsne"), file("markers"), file('clustering_software_versions.txt'), file('project.h5ad') into REUSED_TERTIARY_RESULTS
"""
retrieveStoredFiles.sh $expName $species scanpy "matrices clusters_for_bundle.txt umap tsne markers clustering_software_versions.txt project.h5ad"
"""
}
////////////////////////////////////////////////////////////////
// Processes to derive new results
////////////////////////////////////////////////////////////////
// Is experiment public or private?
process check_privacy {
conda "${baseDir}/envs/jq.yml"
input:
set val(espTag), val(expName), val(species), val(protocol) from TO_QUANTIFY_FOR_PRIVACY_CHECK.join(ESP_TAGS_FOR_PRIVACY_CHECK)
output:
set val(espTag), stdout into PRIVACY_STATUS
"""
if echo "\$expName" | grep -q "MTAB"; then
apiKey="isPublic"
apiSearch="https://www.ebi.ac.uk/biostudies/api/v1/search?type=study&accession=${expName}"
response=\$(curl \$apiSearch)
if [ -z "\${response}" ]; then
#echo "ERROR: Got empty response from \${apiSearch} - unable to determine privacy status" >&2
exit 1
else
responseHit=\$(echo \${response} | jq .hits[0])
if [[ "\${responseHit}" == "null" ]]; then
#echo "WARNING: This search returned no hit: \${apiSearch}" >&2
expInfo=""
else
expInfo=\$(echo \$responseHit | jq ."\${apiKey}")
#if [[ "\${expInfo}" == "null" ]]; then
# echo "WARNING: This key does not exist: \${apiKey}" >&2
#fi
fi
fi
if [[ -z "\$expInfo" ]]; then
expInfo="false";
fi
if [ "\$expInfo" == "true" ]; then
privacyStatus=public
else
privacyStatus=private
fi
else
# it's not MTAB
privacyStatus=public
fi
echo -n "\$privacyStatus"
"""
}
// Symlink to correct reference file, re-using the reference from last
// quantification where appropriate. spikes are used in quantification only.
process add_reference {
conda "${baseDir}/envs/refgenie.yml"
cache 'deep'
errorStrategy { task.attempt<=3 ? 'retry' : 'finish' }
publishDir "$SCXA_RESULTS/$expName/$species/reference", mode: 'copyNoFollow', overwrite: true, pattern: '{*.fa.gz,*.gtf.gz}'
input:
set val(espTag), file(confFile), val(expName), val(species), val(protocol) from EXTENDED_CONF_FOR_REF.join(TO_QUANTIFY_FOR_REFERENCE).join(ESP_TAGS_FOR_REFERENCE)
output:
set val(espTag), file('spiked/*cdna*.fa.gz'), file("spiked/*.gtf.gz"), file('spiked/*.idx'), file('spiked/salmon_index') into PREPARED_REFERENCES
set val(espTag), val(expName), val(species), file('spiked/*toplevel.fa.gz'), file("spiked/*.gtf.gz") into REFS_FOR_T2GENE_DROPLET
set val(espTag), val(expName), val(species), file("*.cdna.*.fa.gz") into REFS_FOR_T2GENE_NON_DROPLET
set val("${expName}-${species}"), file("*.cdna.*.fa.gz"), file("*.gtf.gz") into NEW_REFERENCES_FOR_DOWNSTREAM
"""
refgenieSeek.sh $species ${params.islReferenceType} "" \$(pwd)
# If we have spikes, get the spikes references for use in quantification
spikes=\$(parseNfConfig.py --paramFile $confFile --paramKeys params,spikes)
if [ \$spikes != 'None' ]; then
refgenieSeek.sh $species ${params.islReferenceType} "\$spikes" spiked
else
# Copy unspiked symlinks to spiked if no spikes are required
mkdir spiked
cp -P *.gtf.gz *.fa.gz salmon_index *.idx spiked
fi
"""
}
// This process is parameterised by expName, species, protocol, to allow us to
// control contamination index use for those species in future.
process find_contamination_index {
conda "${baseDir}/envs/refgenie.yml"
input:
set val(espTag), val(expName), val(species), val(protocol) from ESP_TAGS_FOR_CONTAMINATION
output:
set val(espTag), stdout into CONTAMINATION_INDEX
"""
refgenie seek contamination/bowtie2_index | tr -d \'\\n\'
"""
}
// References used in tertiary analysis and bundling are a combination of the
// reused and new references depending on individual experiment statuses
REFERENCES_FOR_GENE_ANNO = Channel.create()
NEW_REFERENCES_FOR_DOWNSTREAM.unique()
.tap( REFERENCES_FOR_GENE_ANNO )
.concat(REUSED_REFERENCES.unique())
.into{
REFERENCES_FOR_TERTIARY
REFERENCES_FOR_BUNDLING
}
// Synchronise the GTF and the FASTA
process transcript_to_gene {
publishDir "$SCXA_RESULTS/$expName/$species/reference", mode: 'copy', overwrite: true, pattern: 'transcript_to_gene.txt'
conda "${baseDir}/envs/atlas-gene-annotation-manipulation.yml"
cache 'deep'
memory { 5.GB * task.attempt }
errorStrategy { task.exitStatus == 130 || task.exitStatus == 137 || task.attempt < 3 ? 'retry' : 'ignore' }
maxRetries 3
input:
set val(espTag), val(expName), val(species), file(referenceFasta), file(referenceGtf), val(isDroplet) from REFS_FOR_T2GENE_DROPLET.join(IS_DROPLET_FOR_TRANSCRIPT_TO_GENE)
set val(espTag), val(expName), val(species), file(referenceCDNA) from REFS_FOR_T2GENE_NON_DROPLET
output:
set val(espTag), file('transcript_to_gene.txt') into TRANSCRIPT_TO_GENE
"""
if [ $isDroplet == 'True' ]; then
pyroe make-splici ${referenceFasta} ${referenceGtf} 90 splici_transcriptome
mv splici_transcriptome/splici_fl*.tsv transcript_to_gene.txt
else
gtf2featureAnnotation.R --gtf-file $referenceGtf --version-transcripts \
--parse-cdnas $referenceCDNA --parse-cdna-field "transcript_id" --feature-type \
"transcript" --parse-cdna-names --fill-empty transcript_id --first-field \
"transcript_id" --output-file transcript_to_gene.txt --fields "transcript_id,gene_id" \
--no-header
fi
"""
}
// Make a gene annotation table `
process make_gene_annotation_table {
publishDir "$SCXA_RESULTS/$expName/$species/reference", mode: 'copy', overwrite: true, pattern: 'gene_annotation.txt'
conda "${baseDir}/envs/atlas-gene-annotation-manipulation.yml"
cache 'deep'
memory { 5.GB * task.attempt }
errorStrategy { task.exitStatus == 130 || task.exitStatus == 137 || task.attempt < 3 ? 'retry' : 'ignore' }
maxRetries 3
input:
set val(esTag), val(expName), val(species), file(referenceFasta), file(referenceGtf) from ES_TAGS_FOR_GENE_ANNO.join(REFERENCES_FOR_GENE_ANNO)
output:
set val(esTag), file('gene_annotation.txt') into NEW_GENE_META
"""
gtf2featureAnnotation.R --gtf-file $referenceGtf --version-transcripts \
--parse-cdnas $referenceFasta --parse-cdna-field "gene_id" --feature-type \
"gene" --parse-cdna-names --mito --mito-biotypes $params.mitoBiotypes \
--mito-chr $params.mitoChr --first-field "gene_id" --output-file \
gene_annotation.txt.tmp
# Make sure gene_name etc present
checkGeneAnnoColumns.R gene_annotation.txt.tmp gene_annotation.txt
"""
}
TRANSCRIPT_TO_GENE
.concat(REUSED_TRANSCRIPT_TO_GENE)
.into{
TRANSCRIPT_TO_GENE_QUANT
TRANSCRIPT_TO_GENE_AGGR
}
NEW_GENE_META
.concat(REUSED_GENE_META.unique())
.into{
GENE_META_FOR_TERTIARY
GENE_META_FOR_BUNDLING
}
// Make a configuration for the Fastq provider, and make initial assessment
// of the available ENA download methods. This establishes a mock
// dependency with the quantify process, as per
// http://nextflow-io.github.io/patterns/index.html#_mock_dependency
process initialise_downloader {
conda "${baseDir}/envs/atlas-fastq-provider.yml"
cache false
output:
val true into INIT_DONE
"""
downloaderConfig=\${FASTQ_PROVIDER_CONFIG:-$NXF_TEMP/atlas-fastq-provider/download_config.sh}
initialiseDownload.sh ${baseDir}/params.config \$downloaderConfig $NXF_TEMP/atlas-fastq-provider/fastq_provider.probe
"""
}
INIT_DONE
.into{
INIT_DONE_SMART
INIT_DONE_DROPLET
}
// Compile the info we need for fresh quantification
TO_QUANTIFY_FOR_QUANT
.join(IS_DROPLET_PROT)
.join(ESP_TAGS_FOR_QUANT)
.join(EXTENDED_CONF_FOR_QUANT)
.join(ANALYSIS_META_FOR_QUANT)
.join(PREPARED_REFERENCES.map{tuple(it[0], it[1], it[2], it[3], it[4])})
.join(CONTAMINATION_INDEX)
.join(TRANSCRIPT_TO_GENE_QUANT)
.join(PRIVACY_STATUS)
.set{
QUANT_INPUTS
}
// Separate droplet and smart-type experiments
DROPLET_INPUTS = Channel.create()
SMART_INPUTS = Channel.create()
QUANT_INPUTS.choice( SMART_INPUTS, DROPLET_INPUTS ) {a ->
a[1] == 'True' ? 1 : 0
}
// Run quantification with https://github.com/ebi-gene-expression-group/scxa-smartseq-quantification-workflow
process smart_quantify {
maxForks params.maxConcurrentQuantifications
conda "${baseDir}/envs/nextflow.yml"
cache 'deep'
publishDir "$SCXA_RESULTS/$expName/$species/quantification/$protocol", mode: 'copy', overwrite: true
memory { 10.GB * task.attempt }
errorStrategy { task.attempt<=10 ? 'retry' : 'ignore' }
maxRetries 10
input:
set val(espTag), val(isDroplet), val(expName), val(species), val(protocol), file(confFile), file(metaForQuant), file(metaForTertiary), file(referenceFasta), file(referenceGtf), file(kallistoIndex), file(salmonIndex), val(contaminationIndex), file(transcriptToGene), val(privacyStatus) from SMART_INPUTS
val flag from INIT_DONE_SMART
output:
set val(espTag), file ("kallisto") into SMART_KALLISTO_DIRS
set val(espTag), file ("qc") into SMART_QUANT_QC
file('quantification.log')
"""
submitQuantificationWorkflow.sh 'smart-seq' "$expName" "$species" "$protocol" "$confFile" "$metaForQuant" "$referenceFasta" "$transcriptToGene" "$contaminationIndex" "$kallistoIndex" "$salmonIndex" "$enaSshUser" "$privacyStatus"
"""
}
// Run quantification with https://github.com/ebi-gene-expression-group/scxa-droplet-quantification-workflow
process droplet_quantify {
maxForks params.maxConcurrentQuantifications
conda "${baseDir}/envs/nextflow.yml"
cache 'deep'
publishDir "$SCXA_RESULTS/$expName/$species/quantification/$protocol", mode: 'copy', overwrite: true
memory { 10.GB * task.attempt }
errorStrategy { task.attempt<=5 ? 'retry' : 'ignore' }
input:
set val(espTag), val(isDroplet), val(expName), val(species), val(protocol), file(confFile), file(metaForQuant), file(metaForTertiary), file(referenceFasta), file(referenceGtf), file(kallistoIndex), file(salmonIndex), val(contaminationIndex), file(transcriptToGene), val(privacyStatus) from DROPLET_INPUTS
val flag from INIT_DONE_DROPLET
output:
set val(espTag), file("alevin") into ALEVIN_DROPLET_DIRS
file('quantification.log')
"""
submitQuantificationWorkflow.sh 'droplet' "$expName" "$species" "$protocol" "$confFile" "$metaForQuant" "$referenceFasta" "$transcriptToGene" "$contaminationIndex" "$kallistoIndex" "$salmonIndex" "$enaSshUser" "$privacyStatus"