-
Notifications
You must be signed in to change notification settings - Fork 1
/
flea.nf
executable file
·1282 lines (938 loc) · 30.2 KB
/
flea.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
/*
========================================================================================
FLEA (Full-Length Envelope Analyzer) Pipeline
========================================================================================
FLEA Pipeline. Started March 2016.
#### Homepage / Documentation
https://github.com/veg/flea-pipeline
----------------------------------------------------------------------------------------
*/
// TODO: use afterScript for compress_cmd (wasn't working)
// TODO: how launch with web ui and monitor progress
// TODO: progressively retry with longer times, if there is a timeout
// TODO: update tests
// TODO: tune maxaccepts and maxrejects
// TODO: combine all time points for inframe db for frame correction
params.infile = "$HOME/flea/data/P018/data/metadata"
params.msafile = ""
params.results_dir = "results"
make_msa = params.msafile == ""
// TODO: how to avoid duplicating?
Channel.fromPath(params.infile)
.into { metadata_1; metadata_2; metadata_3; metadata_4; metadata_5; metadata_6 }
// read input metadata into tuples
input_files = []
visit_codes = []
infile = file(params.infile)
if( make_msa ) {
infile
.readLines()
.each {
(filename, timepoint_label, date) = it.trim().split()
tpfile = file(infile.parent / filename)
mytuple = tuple(tpfile, timepoint_label)
input_files.add(mytuple)
visit_codes.add(timepoint_label)
}
}
visit_codes_set = visit_codes as Set
if(visit_codes.size() != visit_codes_set.size()) {
println "ERROR: visit codes must be unique"
System.exit(0)
}
input_channel = Channel.from(input_files)
compress_cmd = """for i in `find . ! -type l | grep -E "\\.fasta\$|\\.fastq\$|\\.txt\$|\\.dst\$"`; do gzip "\$i" ; done"""
/* ************************************************************************** */
/* QUALITY SUB-PIPELINE */
// compute final min/max QCS length from reference length
Channel.fromPath( params.reference_dna )
.splitFasta( record: [seqString: true ] )
.map { record -> record.seqString.length() }
.take(1)
.into { reflen_1; reflen_2 }
min_qcs_len = reflen_1
.map { n -> Math.round(n * params.qcs_length_coeff.toBigDecimal()) }
max_qcs_len = reflen_2
.map { n -> Math.round(n * (2.0 - params.qcs_length_coeff.toBigDecimal())) }
// TODO: train head/tail HMM on all sequences from all time points
hmm_train_flag = (params.train_hmm ? '--train' : '')
process quality_pipeline {
tag { label }
publishDir params.results_dir, mode: params.publishMode
time params.slow_time
input:
set 'ccs.fastq', label from input_channel
each minlen from min_qcs_len
each maxlen from max_qcs_len
output:
set '*qcs.fastq.gz', label into qcs_final_1, qcs_final_2, qcs_final_3, qcs_final_4
shell:
'''
# filter by quality score
!{params.usearch} --fastq_filter ccs.fastq \
--fastqout qfiltered.fastq \
--fastq_maxee_rate !{params.max_error_rate} \
--fastq_qmax !{params.qmax} \
--fastq_minlen !{minlen} \
--relabel "!{label}_ccs_" \
--threads !{params.cpus}
# trim ends
!{params.python} !{workflow.projectDir}/flea/trim_tails.py \
--n-jobs !{params.cpus} --fastq \
!{hmm_train_flag} --max-iters !{params.train_hmm_max_iters} \
qfiltered.fastq trimmed.fastq
# filter runs
!{params.python} !{workflow.projectDir}/flea/filter_fastx.py \
runs fastq fasta !{params.run_length} < trimmed.fastq > no_runs.fasta
# filter against contaminants
!{params.usearch} --usearch_global no_runs.fasta \
--db !{params.contaminants_db} \
--notmatched uncontam.fasta \
--matched contam.fasta \
--id !{params.contaminant_identity} \
--strand both \
--qmask none \
--top_hit_only \
--maxrejects !{params.max_rejects} \
--threads !{params.cpus}
# filter against reference
!{params.usearch} --usearch_global uncontam.fasta \
--db !{params.reference_db} \
--userout userout.txt \
--userfields query+qstrand+tstrand+caln \
--id !{params.reference_identity} \
--qmask none \
--strand both \
--top_hit_only \
--maxaccepts !{params.max_accepts} \
--maxrejects !{params.max_rejects} \
--threads !{params.cpus}
# propagate db search to fastq file. trim terminal gaps.
# use trimmed.fastq since that was the last fastq file in the pipeline.
# any sequences filtered out of `no_runs` won't make it through the database
# searches, so it will get filtered out here again.
!{params.python} !{workflow.projectDir}/flea/filter_fastx.py \
userout fastq fastq userout.txt \
< trimmed.fastq > filtered.fastq
# length filter
!{params.python} !{workflow.projectDir}/flea/filter_fastx.py \
length fastq fastq \
!{minlen} !{maxlen} \
< filtered.fastq > !{label}.qcs.fastq
!{compress_cmd}
'''
}
/* ************************************************************************** */
/* CONSENSUS SUB-PIPELINE */
process cluster {
tag { label }
publishDir params.results_dir, mode: params.publishMode
time params.slow_time
input:
set 'qcs.fastq.gz', label from qcs_final_1
output:
set '*.clusters.uc', label into cluster_out
shell:
'''
zcat qcs.fastq.gz > qcs.fastq
# sort by error rate
!{params.python} !{workflow.projectDir}/flea/filter_fastx.py \
!{params.before_cluster} fastq fastq \
< qcs.fastq > qcs.sorted.fastq
# cluster
!{params.usearch} --cluster_fast qcs.sorted.fastq \
-uc !{label}.clusters.uc \
--id !{params.cluster_identity} \
--minsl !{params.min_length_ratio} \
--top_hit_only \
--maxaccepts !{params.max_accepts} \
--maxrejects !{params.max_rejects} \
--threads !{params.cpus}
rm -f qcs.fastq
'''
}
cluster_out
.join (qcs_final_2, by: 1)
.set { consensus_input }
process consensus {
tag { label }
publishDir params.results_dir, mode: params.publishMode
time params.slow_time
input:
set label, 'clusters.uc', 'qcs.fastq.gz' from consensus_input
output:
set '*.clusters.consensus.fasta.gz', label into consensus_out
shell:
'''
zcat qcs.fastq.gz | \
!{params.python} !{workflow.projectDir}/flea/cluster_fastq.py \
--minsize !{params.min_cluster_size} \
clusters.uc .
# function sample clusters and do mafft consensus
doconsensus() {
!{params.python} !{workflow.projectDir}/flea/filter_fastx.py \
sample fastq fasta \
!{params.min_cluster_size} !{params.max_cluster_size} \
< ${1} > ${1}.sampled.fasta
!{params.mafft} --ep 0.5 --quiet --preservecase \
${1}.sampled.fasta > ${1}.sampled.aligned.fasta
# get cluster number so we can put it in the record id
number=$(echo ${1} | cut -d '_' -f 2)
!{params.python} !{workflow.projectDir}/flea/DNAcons.py \
--seed 0 \
-o ${1}.consensus.fasta \
--name !{label}_consensus_${number} \
${1}.sampled.aligned.fasta
}
export -f doconsensus
# run in parallel
!{params.parallel} -j !{params.cpus} 'doconsensus {}' ::: *_raw.fastq
cat *.consensus.fasta > !{label}.clusters.consensus.fasta
# check that all sequences are present
n_expected=`ls *_raw.fastq | wc -l`
n_found=`grep ">" !{label}.clusters.consensus.fasta | wc -l`
if [ "$n_expected" -ne "$n_found" ]; then
echo "ERROR: some consensus sequences are missing"
exit 1
fi
rm -f qcs.fastq
!{compress_cmd}
'''
}
allow_stop_codons = params.do_frame_correction ? "false" : "true"
process inframe_unique_hqcs {
tag { label }
publishDir params.results_dir, mode: params.publishMode
input:
set 'consensus.fasta.gz', label from consensus_out
output:
set '*.consensus.fasta.gz', '*.consensus.unique.fasta.gz', label into inframe_unique_out_1,
inframe_unique_out_2,
inframe_unique_out_3
shell:
'''
zcat consensus.fasta.gz > consensus.fasta
# inframe
!{params.python} !{workflow.projectDir}/flea/filter_fastx.py \
inframe fasta fasta !{allow_stop_codons} \
< consensus.fasta > consensus.inframe.fasta
# unique
!{params.usearch} --fastx_uniques consensus.inframe.fasta \
--fastaout !{label}.consensus.unique.fasta \
--threads !{params.cpus}
cp consensus.fasta !{label}.consensus.fasta
rm -f consensus.fasta
!{compress_cmd}
'''
}
inframe_unique_out_1
.map { it -> it[1] }
.set { inframe_unique_dbs }
process make_inframe_db {
when:
params.do_frame_correction
publishDir params.results_dir, mode: params.publishMode
input:
file '*.consensus.unique.fasta.gz' from inframe_unique_dbs.collect()
output:
file 'inframedb.fasta.gz' into inframe_db_out
shell:
'''
zcat *.consensus.unique.fasta.gz > inframedb.fasta
!{compress_cmd}
'''
}
process frame_correction {
when:
params.do_frame_correction
tag { label }
publishDir params.results_dir, mode: params.publishMode
time params.slow_time
input:
set 'consensus.fasta.gz', 'unused.db.fasta.gz', label from inframe_unique_out_2
file 'inframedb.fasta.gz' from inframe_db_out
output:
set '*.consensus.unique.corrected.fasta.gz', label into frame_correction_out
shell:
'''
zcat consensus.fasta.gz > consensus.fasta
zcat inframedb.fasta.gz > inframedb.fasta
# search
!{params.usearch} --usearch_global consensus.fasta \
--db inframedb.fasta \
--fastapairs pairfile.fasta \
--userout calnfile.txt \
--userfields caln \
--top_hit_only \
--id !{params.reference_identity} \
--qmask none \
--strand plus \
--maxaccepts !{params.max_accepts} \
--maxrejects !{params.max_rejects} \
--threads !{params.cpus}
# frame correction
!{params.python} !{workflow.projectDir}/flea/frame_correction.py \
--deletion-strategy=reference \
--calns=calnfile.txt \
pairfile.fasta corrected.fasta
# filter inframe
!{params.python} !{workflow.projectDir}/flea/filter_fastx.py \
inframe fasta fasta true < corrected.fasta > corrected.inframe.fasta
# deduplicate
!{params.usearch} --fastx_uniques corrected.inframe.fasta \
--fastaout !{label}.consensus.unique.corrected.fasta \
--threads !{params.cpus}
rm -f consensus.fasta
rm -f inframedb.fasta
!{compress_cmd}
'''
}
// NOTE: if previous steps are cached for both values of
// `do_frame_correction`, changing the value of `do_frame_correction`
// doesn't update the symlinks downstream.
compute_copynumbers_input = Channel.create()
if( params.do_frame_correction ) {
frame_correction_out
.join (qcs_final_3, by: 1)
.set { compute_copynumbers_input }
} else {
inframe_unique_out_3
.join (qcs_final_3, by: -1)
.set { compute_copynumbers_input }
}
process compute_copynumbers {
tag { label }
time params.slow_time
input:
set label, 'hqcs.fasta.gz', 'qcs.fastq.gz' from compute_copynumbers_input
output:
file 'hqcs.filtered.fasta.gz' into hqcs_files
file 'copynumber_file.txt.gz' into copynumber_files
shell:
'''
zcat hqcs.fasta.gz > hqcs.fasta
# convert to fasta for usearch
zcat qcs.fastq.gz | \
!{params.python} !{workflow.projectDir}/flea/filter_fastx.py \
convert fastq fasta > qcs.fasta
# search for pairs
!{params.usearch} --usearch_global qcs.fasta \
--db hqcs.fasta \
--userout pairfile.txt \
--userfields query+target \
--top_hit_only \
--id !{params.copynumber_identity} \
--maxqt !{params.copynumber_max_length_ratio} \
-qmask none \
--strand plus \
--maxaccepts !{params.max_accepts} \
--maxrejects !{params.max_rejects} \
--threads !{params.cpus}
# write copynumber file
!{params.python} !{workflow.projectDir}/flea/write_copynumbers.py \
< pairfile.txt > copynumber_file.txt
# filter out HQCS with 0 copynumber
!{params.python} !{workflow.projectDir}/flea/filter_fastx.py \
copynumber fasta fasta copynumber_file.txt \
< hqcs.fasta > hqcs.filtered.fasta
rm -f hqcs.fasta
!{compress_cmd}
'''
}
process merge_timepoints {
publishDir params.results_dir, mode: params.publishMode
executor 'local'
cpus 1
input:
file 'hqcs*.fastq.gz' from hqcs_files.collect()
file 'copynumber*.txt.gz' from copynumber_files.collect()
output:
file 'all_hqcs.fasta.gz' into merged_hqcs_out
shell:
'''
zcat hqcs*.fastq.gz > merged_hqcs.fasta
zcat copynumber*.txt.gz > merged_copynumbers.txt
# add copynumbers to ids, for evo_history
!{params.python} !{workflow.projectDir}/flea/filter_fastx.py \
add_copynumber fasta fasta merged_copynumbers.txt \
< merged_hqcs.fasta > all_hqcs.fasta
!{compress_cmd}
'''
}
/* ************************************************************************** */
/* ALIGNMENT SUB-PIPELINE */
process alignment_pipeline {
publishDir params.results_dir, mode: params.publishMode
time params.slow_time
input:
file 'hqcs.fasta.gz' from merged_hqcs_out
output:
file 'msa.fasta.gz' into alignment_output
shell:
'''
zcat hqcs.fasta.gz > hqcs.fasta
!{params.python} !{workflow.projectDir}/flea/translate.py \
< hqcs.fasta > hqcs_protein.fasta
!{params.mafft} --ep 0.5 --quiet --preservecase \
--thread !{params.cpus} \
hqcs_protein.fasta > msa.aa.fasta
!{params.python} !{workflow.projectDir}/flea/backtranslate.py \
msa.aa.fasta hqcs.fasta msa.fasta
rm -f hqcs.fasta
!{compress_cmd}
'''
}
/* ************************************************************************** */
/* ANALYSIS SUB-PIPELINE */
if( make_msa ) {
msa_file = Channel.value('not_used')
} else {
msa_file = file(params.msafile)
}
process gzip_msa {
executor 'local'
cpus 1
when:
!make_msa
input:
file 'msa.fasta' from msa_file
output:
file 'msa.fasta.gz' into gzipped_msa
"""
gzip -c msa.fasta > msa.fasta.gz
"""
}
msa_out = Channel.create()
if( make_msa ) {
alignment_output.set{ msa_out }
} else {
gzipped_msa.set{ msa_out }
}
msa_out.into{ msa_out_1; msa_out_2; msa_out_3; msa_out_4; msa_out_5; msa_out_6; msa_out_7; msa_out_8; msa_out_9 }
process dates_json_task {
publishDir params.results_dir, mode: params.publishMode
executor 'local'
cpus 1
when:
params.do_analysis
input:
file 'metadata' from metadata_1
output:
file 'dates.json' into dates_json_out
"""
#!${params.python}
import json
from flea.util import get_date_dict
d = get_date_dict('metadata')
result = dict((v, k) for k, v in d.items())
with open('dates.json', 'w') as handle:
json.dump(result, handle, separators=(",\\n", ":"))
"""
}
process copynumbers_json {
publishDir params.results_dir, mode: params.publishMode
executor 'local'
cpus 1
when:
params.do_analysis
input:
file 'msa.fasta.gz' from msa_out_1
output:
file 'copynumbers.json' into copynumbers_json_out
"""
#!${params.python}
import gzip
import json
from Bio import SeqIO
from flea.util import id_to_copynumber
with gzip.open('msa.fasta.gz', 'rt') as handle:
records = SeqIO.parse(handle, 'fasta')
outdict = dict((r.id, id_to_copynumber(r.id)) for r in records)
with open('copynumbers.json', 'w') as handle:
json.dump(outdict, handle, separators=(",\\n", ":"))
"""
}
process get_oldest_label {
executor 'local'
cpus 1
when:
params.do_analysis
input:
file 'metadata' from metadata_2
output:
stdout oldest_label
"""
#!${params.python}
import sys
from flea.util import get_date_dict
d = get_date_dict('metadata')
sys.stdout.write(sorted(d, key=d.get)[0])
"""
}
process mrca {
time params.slow_time
when:
params.do_analysis
input:
file 'msa.fasta.gz' from msa_out_2
val oldest_label
output:
file 'mrca.fasta.gz' into mrca_1, mrca_2, mrca_3, mrca_4
file 'mrca_translated.fasta.gz' into mrca_translated_1, mrca_translated_2, mrca_translated_3
shell:
'''
zcat msa.fasta.gz | \
!{params.python} !{workflow.projectDir}/flea/filter_fastx.py \
prefix fasta fasta !{oldest_label} \
> oldest_seqs.fasta
!{params.python} !{workflow.projectDir}/flea/DNAcons.py \
--seed 0 \
--keep-gaps \
--codon \
--copynumbers \
--name MRCA \
-o mrca.fasta \
oldest_seqs.fasta
!{params.python} !{workflow.projectDir}/flea/translate.py --gapped \
< mrca.fasta > mrca_translated.fasta
!{compress_cmd}
'''
}
// TODO: why do we have to duplicate outputs here?
process add_mrca {
executor 'local'
cpus 1
when:
params.do_analysis
input:
file 'mrca.fasta.gz' from mrca_1
file 'msa.fasta.gz' from msa_out_3
output:
file 'msa_with_mrca.fasta.gz' into msa_with_mrca_1, msa_with_mrca_2
"zcat mrca.fasta.gz msa.fasta.gz | gzip > msa_with_mrca.fasta.gz"
}
process infer_tree {
time params.slow_time
when:
params.do_analysis
input:
file 'msa.fasta.gz' from msa_with_mrca_1
output:
file 'tree.txt' into tree_out
"""
export OMP_NUM_THREADS=${params.cpus}
zcat msa.fasta.gz | ${params.fasttree} -gtr -nt > tree.txt
"""
}
process reroot {
executor 'local'
cpus 1
when:
params.do_analysis
input:
file 'tree.txt' from tree_out
output:
file 'tree.rooted.txt' into rooted_tree_1, rooted_tree_2, rooted_tree_3
"""
#!${params.python}
from Bio import Phylo
tree = next(Phylo.parse('tree.txt', 'newick'))
clade = next(tree.find_clades('MRCA'))
tree.root_with_outgroup(clade)
# also rename for HyPhy
for i, node in enumerate(tree.get_nonterminals()):
node.confidence = None
if node.name != 'MRCA':
node.name = "ancestor_{}".format(i)
Phylo.write([tree], 'tree.rooted.txt', 'newick')
"""
}
process tree_json {
publishDir params.results_dir, mode: params.publishMode
executor 'local'
cpus 1
when:
params.do_analysis
input:
file 'tree.txt' from rooted_tree_1
output:
file 'trees.json' into trees_json_out
"""
#!${params.python}
import json
with open('tree.txt') as handle:
newick_string = handle.read()
result = {'tree': newick_string}
with open('trees.json', 'w') as handle:
json.dump(result, handle, separators=(",\\n", ":"))
"""
}
process translate_msa {
publishDir params.results_dir, mode: params.publishMode
when:
params.do_analysis
input:
file 'msa.fasta.gz' from msa_out_4
output:
file 'msa.aa.fasta.gz' into msa_aa_out_1, msa_aa_out_2, msa_aa_out_3
shell:
'''
zcat msa.fasta.gz > msa.fasta
!{params.python} !{workflow.projectDir}/flea/translate.py \
--gapped \
< msa.fasta > msa.aa.fasta
rm -f msa.fasta
!{compress_cmd}
'''
}
process js_divergence {
publishDir params.results_dir, mode: params.publishMode
time params.slow_time
when:
params.do_analysis
input:
file 'msa.aa.fasta.gz' from msa_aa_out_1
file 'mrca.aa.fasta.gz' from mrca_translated_1
file 'metadata' from metadata_3
output:
file 'js_divergence.json' into js_divergence_json_out
"""
zcat msa.aa.fasta.gz > msa.aa.fasta
zcat mrca.aa.fasta.gz > mrca.aa.fasta
${params.python} ${workflow.projectDir}/flea/js_divergence.py \
msa.aa.fasta mrca.aa.fasta metadata js_divergence.json
rm -f msa.aa.fasta mrca.aa.fasta
"""
}
process manifold_embedding {
publishDir params.results_dir, mode: params.publishMode
time params.slow_time
when:
params.do_analysis
input:
file 'msa.fasta.gz' from msa_out_5
file 'metadata' from metadata_4
output:
file 'manifold.json' into manifold_json_out
shell:
'''
zcat msa.fasta.gz > msa.fasta
!{params.tn93} -t !{params.tn93_threshold} -o dmatrix.dst msa.fasta
!{params.python} !{workflow.projectDir}/flea/manifold_embed.py \
--n-jobs 1 dmatrix.dst metadata manifold.json
rm -f msa.fasta
!{compress_cmd}
'''
}
// TODO: avoid full paths
// TODO: why do command line arguments not work here?
process reconstruct_ancestors {
time params.slow_time
when:
params.do_analysis
input:
file 'msa.fasta.gz' from msa_with_mrca_2
file 'msa.aa.fasta.gz' from msa_aa_out_2
file 'tree.rooted.txt' from rooted_tree_2
output:
file 'msa.aa.ancestors.fasta.gz' into msa_aa_ancestors_out
file 'ancestors.fasta.gz' into ancestors_out
shell:
'''
zcat msa.fasta.gz > msa.fasta
zcat msa.aa.fasta.gz > msa.aa.fasta
echo $(pwd)/msa.fasta >> stdin
echo $(pwd)/tree.rooted.txt >> stdin
echo $(pwd)/ancestors.fasta >> stdin
echo GRM >> stdin
echo 2 >> stdin
!{params.hyphy} !{workflow.projectDir}/hyphy_scripts/reconstructAncestors.bf < stdin
!{params.python} !{workflow.projectDir}/flea/translate.py --gapped \
< ancestors.fasta > ancestors.aa.fasta
cat msa.aa.fasta ancestors.aa.fasta > 'msa.aa.ancestors.fasta'
rm -f msa.fasta msa.aa.fasta
!{compress_cmd}
'''
}
process coordinates_json {
publishDir params.results_dir, mode: params.publishMode
when:
params.do_analysis
input:
file 'mrca.aa.fasta.gz' from mrca_translated_2
output:
file 'coordinates.json' into coordinates_json_out_1, coordinates_json_out_2
"""
zcat mrca.aa.fasta.gz > mrca.aa.fasta
cat mrca.aa.fasta ${params.reference_protein} > pair.fasta
${params.mafft} --ep 0.5 --quiet --preservecase \
--thread ${params.cpus} \
pair.fasta > aligned.fasta
${params.python} ${workflow.projectDir}/flea/coordinates_json.py \
mrca.aa.fasta aligned.fasta ${params.reference_coordinates} coordinates.json
rm -f mrca.aa.fasta
"""
}
process sequences_json {
publishDir params.results_dir, mode: params.publishMode
when:
params.do_analysis
input:
file 'msa.fasta.gz' from msa_aa_ancestors_out
file 'mrca.fasta.gz' from mrca_translated_3
file 'coordinates.json' from coordinates_json_out_1
file 'metadata' from metadata_5
output:
file 'sequences.json' into sequences_json_out
"""
zcat msa.fasta.gz > msa.fasta
zcat mrca.fasta.gz > mrca.fasta
${params.python} ${workflow.projectDir}/flea/sequences_json.py \
msa.fasta mrca.fasta coordinates.json metadata \
${params.reference_protein} ${params.reference_coordinates} \
sequences.json
rm -f msa.fasta mrca.fasta
"""
}
process replace_stop_codons {
when:
params.do_analysis
input:
file 'msa.fasta.gz' from msa_out_6
output:
file 'msa.no_stops.fasta.gz' into msa_no_stops_1, msa_no_stops_2
"""
zcat msa.fasta.gz |
${params.python} ${workflow.projectDir}/flea/filter_fastx.py \
stop_codons fasta fasta | gzip > msa.no_stops.fasta.gz
"""
}
// TODO: why do we need to split output here, but not elsewhere?
process seq_dates {
when:
params.do_analysis
input:
file 'msa.fasta.gz' from msa_out_7
file 'metadata' from metadata_6
output:
file 'dates.json' into seq_dates_1, seq_dates_2
"""
#!${params.python}
import gzip
import json
from Bio import SeqIO
from flea.util import get_date_dict
from flea.util import id_to_label
date_dict = get_date_dict('metadata')
with gzip.open('msa.fasta.gz', 'rt') as handle:
records = SeqIO.parse(handle, "fasta")