forked from nhoffman/ya16sdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.nf
executable file
·1489 lines (1248 loc) · 40.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
/*
arf: Download and curate the 16S rRNA sequences from NCBI
Originally developed by Noah Hoffman and Chris Rosenthal
Adapted to nextflow from sconstruct by Jonathan Golob
This workflow interatively updates a cache of 16S rRNA genes
extracted from the NCBI NT database, verifies the annotations,
sequence quality (i.e. gene length and ambiguous bases), and
then validates the annotations of the 16S rRNA genes by
'majority rules'.
This is an approach to generate algorithmically a large,
frequently updated and conservatively annotated 16S rRNA
library for use in 16S rRNA amplicon classification.
Steps:
1) Retrieve an updated list of record ids (i.e. versions)
with annotated 16S rRNA gene from NCBI NT,
as well as the seq start and stop when relevant.
-> Do this for all bacteria, type strains, and archaea
2) (If available), compare this to the starting list of record ids
(versions) *already* in the repo.
3) Mask away any known bad/malformed/invalid record ids
4) Retrieve the new (i.e not in the repo already) records
5) Parse the records, extract the 16S rRNA sequences
6) Validate the annotations via alignment and/or RNA structure
comparison to a library of true 16s rRNA genes
7) Validate the length and percent of ambiguous bases in the rRNA gene to:
/dedup/1200bp/
8) Segregate out 16s rRNA genes from type strains to:
/dedup/1200bp/types/
9) Regex based parsing of taxonomic annotations to find out named, to:
/dedup/1200bp/named/
10) Cluster and remove outliers to generate:
/dedup/1200bp/named/filtered/
11) Add in 'trusted' seqs (manually curated versions in a text file.)
/dedup/1200bp/named/filtered/trusted/
12) Append the new entries to the extant library
13) Use git to version and commit the changes.
*/
// containers
container__arf = 'golob/arf:0.1A'
container__deenurp = 'golob/deenurp:0.2.7'
// User params initialization
params.help = false
params.testing = true
params.repo = './arf'
params.out = './refreshed'
params.email = false
params.ncbi_concurrent_connections = 3
params.retry_max = 1
params.retry_delay = 1000
params.min_len = 500
params.api_key = false
params.debug = false
// maximum number of 16S rRNA to return for a given species
params.species_cap = 5000
params.min_seqs_for_filtering = 5
if (params.debug == true){
params.rRNA16S_bact_search = """16s[All Fields] AND rRNA[Feature Key] AND Bacteria[Organism] AND ${params.min_len} : 99999999999[Sequence Length] NOT(environmental samples[Organism] OR unclassified Bacteria[Organism]) AND sequence_from_type[Filter] AND ("2020/02/01"[Publication Date] : "2020/02/02"[Publication Date])"""
params.rRNA16S_arch_search = """16s[All Fields] AND rRNA[Feature Key] AND Archaea[Organism] AND ${params.min_len} : 99999999999[Sequence Length] NOT(environmental samples[Organism] OR unclassified Bacteria[Organism]) AND ("2020/02/01"[Publication Date] : "2020/02/02"[Publication Date])"""
params.rRNA16S_type_search = """16s[All Fields] AND rRNA[Feature Key] AND (Bacteria[Organism] OR Archaea[Organism]) AND ${params.min_len} : 99999999999[Sequence Length] NOT(environmental samples[Organism] OR unclassified Bacteria[Organism]) AND sequence_from_type[Filter] AND ("2020/02/01"[Publication Date] : "2020/02/02"[Publication Date])"""
}
else {
params.rRNA16S_bact_search = """16s[All Fields] AND rRNA[Feature Key] AND Bacteria[Organism] AND ${params.min_len} : 99999999999[Sequence Length] NOT(environmental samples[Organism] OR unclassified Bacteria[Organism])"""
params.rRNA16S_arch_search = """16s[All Fields] AND rRNA[Feature Key] AND Archaea[Organism] AND ${params.min_len} : 99999999999[Sequence Length] NOT(environmental samples[Organism] OR unclassified Bacteria[Organism])"""
params.rRNA16S_type_search = """16s[All Fields] AND rRNA[Feature Key] AND (Bacteria[Organism] OR Archaea[Organism]) AND ${params.min_len} : 99999999999[Sequence Length] NOT(environmental samples[Organism] OR unclassified Bacteria[Organism]) AND sequence_from_type[Filter]"""
}
// Function which prints help message text
def helpMessage() {
log.info"""
Usage:
nextflow run jgolob/arf <ARGUMENTS>
Required Arguments:
--repo path to directory holding the current repo (default = './arf')
--out path where refreshed repo should be placed (default = './refreshed')
--email Valid email to use with NCBI
--ncbi_concurrent_connections Number of concurrent connections (default = 3)
--retry_max Max retries with NCBI requests (default = 1)
--retry_delay Delay (ms) between retries (default=60000)
--min_len Minimum annotated length of 16s rRNA to even be downloaded (default 500)
--species_cap Maximum number of 16s rRNA genes per annotated species (default 5000)
Options:
--api_key NCBI api key (will increase download rate)
--debug Cuts down the number of records for testing
""".stripIndent()
}
// parameter validation
/*
Repo structure:
./seqs.fasta <- *all* 16S rRNA to date, valid, invalid, etc
./seq_info.csv <- Extract seq info, including version / accession
./refseq_info.csv <- If seqs are from refseq, put info here
./pubmed_info.csv <- Pubmed sources for the sequences
./references.csv <- references for the sequences
./records.txt <- All record IDs that meet our criteria
./dedup/1200bp/ DIR: holds full length 16S rRNA with limited ambiguous bases
./dedup/1200bp/seqs.fasta
./dedup/1200bp/seq_info.csv
./dedup/1200bp/types/ DIR: For 1200bp / type strain sourced 16S rRNA
./dedup/1200bp/types/seqs.fasta
./dedup/1200bp/types/seq_info.csv
./dedup/1200bp/named/ DIR: for 'named' 16sRNA (with a taxonomic annotation)
./dedup/1200bp/named/seqs.fasta
./dedup/1200bp/named/seq_info.csv
./dedup/1200bp/named/tax_id_map.csv
./dedup/1200bp/named/taxonomy.csv <- holds taxonomic info for each gene
./dedup/1200bp/named/filtered/ DIR: for verified (filtered) taxonomic annotations
./dedup/1200bp/named/filtered/seqs.fasta
./dedup/1200bp/named/filtered/seq_info.csv
./dedup/1200bp/named/filtered/tax_id_map.csv
./dedup/1200bp/named/filtered/taxonomy.csv <- holds taxonomic info for each gene
*/
def paramInvalid() {
if (
(!file("${params.repo}").exists()) ||
(!file("${params.repo}").isDirectory())
) {
log.error "Repo path ${params.repo} doesn't exist or isn't a directory.";
return true;
}
if (params.email == false || params.email == null){
log.error "You must supply a valid email.";
return true;
}
// implicit else
return false
}
if (params.help || paramInvalid()){
// Invoke the function above which prints the help message
helpMessage()
// Exit out and do not run anything else
exit 0
}
// Step 0: Load in prior files from the repo
prior_seqs_f = file "${params.repo}/seqs.fasta"
prior_si_f = file "${params.repo}/seq_info.csv"
prior_records_f = file "${params.repo}/records.txt"
prior_unknowns_f = file "${params.repo}/unknowns.txt"
prior_pubmed_f = file "${params.repo}/pubmed_info.csv"
prior_refseq_info_f = file "${params.repo}/refseq_info.csv"
prior_references_f = file "${params.repo}/references.csv"
prior_refseqinfo_f = file "${params.repo}/refseq_info.csv"
prior_outliers_f = file "${params.repo}/dedup/1200bp/named/filtered/outliers.csv"
// Step 1: Retrieve current accessions with a 16S rRNA
// Archaea
process retrieveAcc_archaea {
container 'golob/medirect:0.14.0__bcw.0.3.1B'
label 'io_limited'
errorStrategy 'finish'
executor 'local'
output:
file "archaea_acc.txt" into acc_archaea_f
"""
set -e
ncbi_get_nt_accessions_for_query \
--email ${params.email} \
--ncbi_concurrent_connections ${params.ncbi_concurrent_connections} \
--retry_max ${params.retry_max} --retry_delay ${params.retry_delay} \
--query "${params.rRNA16S_arch_search}" \
--out archaea_acc.txt
if [ -s archaea_acc.txt ]; then echo "DONE"; else rm -f archaea_acc.txt; fi
"""
}
// Bacteria
process retrieveAcc_bacteria {
container 'golob/medirect:0.14.0__bcw.0.3.1B'
label 'io_limited'
errorStrategy 'finish'
executor 'local'
output:
file "bacteria_acc.txt" into acc_bacteria_f
"""
set -e
ncbi_get_nt_accessions_for_query \
--email ${params.email} \
--ncbi_concurrent_connections ${params.ncbi_concurrent_connections} \
--retry_max ${params.retry_max} --retry_delay ${params.retry_delay} \
--query "${params.rRNA16S_bact_search}" \
--out bacteria_acc.txt
if [ -s bacteria_acc.txt ]; then echo "DONE"; else rm -f bacteria_acc.txt; fi
"""
}
// From type strain organisms
process retrieveAcc_types {
container 'golob/medirect:0.14.0__bcw.0.3.1B'
label 'io_limited'
errorStrategy 'finish'
executor 'local'
output:
file "types_acc.txt" into acc_types_f
"""
set -e
ncbi_get_nt_accessions_for_query \
--email ${params.email} \
--ncbi_concurrent_connections ${params.ncbi_concurrent_connections} \
--retry_max ${params.retry_max} --retry_delay ${params.retry_delay} \
--query "${params.rRNA16S_type_search}" \
--out types_acc.txt
if [ -s types_acc.txt ]; then echo "DONE"; else rm -f bacteria_acc.txt; fi
"""
}
// TODO check for modified records by extracting modified dates
// Step 2. Use set adventures to figure out the records we have not looked at
// Can also include here masked records to remove (known empty, etc)
process combineCurrentAccessions {
container 'golob/medirect:0.14.0__bcw.0.3.1B'
label 'io_limited'
errorStrategy 'finish'
input:
file acc_types_f
file acc_archaea_f
file acc_bacteria_f
output:
file "records.current.txt" into records_dl_f
"""
#!/usr/bin/env python
archaea_ver = {
l.strip()
for l in open("${acc_archaea_f}", 'rt')
}
bacteria_ver = {
l.strip()
for l in open("${acc_bacteria_f}", 'rt')
}
type_ver = {
l.strip()
for l in open("${acc_types_f}", 'rt')
}
with open('records.current.txt', 'wt') as out_h:
for acc in sorted(archaea_ver.union(bacteria_ver).union(type_ver)):
out_h.write(acc+"\\n")
"""
}
process accessionsToDownload {
container 'golob/medirect:0.14.0__bcw.0.3.1B'
label 'io_limited'
errorStrategy 'finish'
cache 'deep'
input:
file records_dl_f
file prior_records_f
output:
file "download.txt" into acc_to_download_f
script:
if (params.debug == false)
"""
#!/usr/bin/env python
current_version = {
l.strip()
for l in open("${records_dl_f}")
}
prior_version = {
l.strip()
for l in open("${prior_records_f}")
}
new_versions = sorted(current_version - prior_version)
with open('download.txt', 'wt') as out_h:
for acc in new_versions:
out_h.write(acc+"\\n")
"""
else
"""
#!/usr/bin/env python
current_version = {
l.strip()
for l in open("${records_dl_f}")
}
prior_version = {
l.strip()
for l in open("${prior_records_f}")
}
new_versions = sorted(current_version - prior_version)[0:1000]
with open('download.txt', 'wt') as out_h:
for acc in new_versions:
out_h.write(acc+"\\n")
"""
}
// Split the downloads into chunks to make this all less painful
process split_downloads {
container 'golob/medirect:0.14.0__bcw.0.3.1B'
label 'io_limited'
errorStrategy 'finish'
executor 'local'
input:
file acc_to_download_f
output:
file "download_chunk.*" into download_acc_chunks_list
"""
set -e
split -l 10000 ${acc_to_download_f} download_chunk.
"""
}
// Step 3. For each new accession, find the 16S rRNA features
download_acc_chunks_list
.flatten()
.set{
download_acc_chunks_ch
}
process get16SrRNA_feat {
container 'golob/medirect:0.14.0__bcw.0.3.1B'
label 'io_limited'
errorStrategy 'finish'
executor 'local'
maxForks 2
input:
file (acc_to_download_f) from download_acc_chunks_ch
output:
file ("new_16s_rrna_feat.${acc_to_download_f}.csv") into new_16s_rRNA_feat_ch
script:
if (params.api_key == false)
"""
set -e
mefetch -proc ${task.cpus} -max-retry ${params.retry_max} -retry ${params.retry_delay} \
--email ${params.email} -db nucleotide -mode text -format ft -id ${acc_to_download_f} |
ftract -feature "rrna:product:16S ribosomal RNA" -min-length ${params.min_len} -on-error continue \
-out new_16s_rrna_feat.${acc_to_download_f}.csv
if [ -s new_16s_rrna_feat.${acc_to_download_f}.csv ]; then echo "DONE"; else rm -f new_16s_rrna_feat.${acc_to_download_f}.csv; fi
"""
else
"""
set -e
mefetch -proc ${task.cpus} -max-retry ${params.retry_max} -retry ${params.retry_delay} \
--email ${params.email} -api-key ${params.api_key} -db nucleotide -mode text \
-format ft -id ${acc_to_download_f} |
ftract -feature "rrna:product:16S ribosomal RNA" -min-length ${params.min_len} -on-error continue \
-out new_16s_rrna_feat.${acc_to_download_f}.csv
if [ -s new_16s_rrna_feat.${acc_to_download_f}.csv ]; then echo "DONE"; else rm -f new_16s_rrna_feat.${acc_to_download_f}.csv; fi
"""
}
// Step 4: Download the new 16S rRNA features into genbank format and extract directly
today = new Date().format('dd-MMM-yyyy')
process get16SrRNA_gb {
container 'golob/medirect:0.14.0__bcw.0.3.1B'
label 'io_limited'
errorStrategy 'finish'
cache 'deep'
executor 'local'
maxForks 2
input:
file (new_16s_rRNA_feat_f) from new_16s_rRNA_feat_ch
val today
output:
tuple file ("${new_16s_rRNA_feat_f}.seqs.fasta"),
file ("${new_16s_rRNA_feat_f}.seq_info.csv"),
file ("${new_16s_rRNA_feat_f}.pubmed_info.csv"),
file ("${new_16s_rRNA_feat_f}.references.csv"),
file ("${new_16s_rRNA_feat_f}.refseq_info.csv") into new_dl_features_ch
script:
if (params.api_key == false)
"""
set -e
mefetch -proc ${task.cpus} -max-retry ${params.retry_max} -retry ${params.retry_delay} \
--email ${params.email} -db nucleotide -mode text \
-csv -format gbwithparts -id ${new_16s_rRNA_feat_f} |
extract_genbank ${today} \
${new_16s_rRNA_feat_f}.seqs.fasta \
${new_16s_rRNA_feat_f}.seq_info.csv \
${new_16s_rRNA_feat_f}.pubmed_info.csv \
${new_16s_rRNA_feat_f}.references.csv \
${new_16s_rRNA_feat_f}.refseq_info.csv
"""
else
"""
set -e
mefetch -proc ${task.cpus} -max-retry ${params.retry_max} -retry ${params.retry_delay} \
--email ${params.email} --api-key ${params.api_key} -db nucleotide -mode text \
-csv -format gbwithparts -id ${new_16s_rRNA_feat_f} |
extract_genbank ${today} \
${new_16s_rRNA_feat_f}.seqs.fasta \
${new_16s_rRNA_feat_f}.seq_info.csv \
${new_16s_rRNA_feat_f}.pubmed_info.csv \
${new_16s_rRNA_feat_f}.references.csv \
${new_16s_rRNA_feat_f}.refseq_info.csv
"""
}
new_dl_features_ch
.multiMap { it ->
for_vsearch: [it[0], it[1]]
pubmed: it[2]
references: it[3]
refseq_info: it[4]
}
.set {
new_dl_split_ch
}
new_dl_split_ch.for_vsearch
.filter {
(!file(it[0]).isEmpty()) && (!file(it[1]).isEmpty())
}
.set {
new_dl_features_for_vsearch_ch
}
// Combine batches
// Combine post-vsearch batches!
new_dl_split_ch.pubmed
.filter {
!file(it).isEmpty()
}
.unique()
.toList()
.set {
new_pubmeds
}
process combineBatches_pubmed {
container = 'golob/medirect:0.14.0__bcw.0.3.1B'
label = 'io_limited'
input:
file (new_pubmeds)
output:
file("new_pubmed.csv") into new_pubmed_f
"""
#!/usr/bin/env python
import csv
files = "${new_pubmeds.join(";")}".split(';')
if len(files) > 0:
readers = [
csv.DictReader(open(fn, 'rt'))
for fn in files
]
headers = [
r.fieldnames
for r in readers
]
common_header = headers[0]
for ch in headers[1:]:
common_header += [h for h in ch if h not in common_header]
with open("new_pubmed.csv", 'wt') as out_h:
out_w = csv.DictWriter(out_h, fieldnames=common_header)
out_w.writeheader()
for reader in readers:
out_w.writerows(reader)
else:
open("new_pubmed.csv", 'wt')
"""
}
new_dl_split_ch.references
.filter {
!file(it).isEmpty()
}
.unique()
.toList()
.set {
new_refs
}
process combineBatches_references {
container = 'golob/medirect:0.14.0__bcw.0.3.1B'
label = 'io_limited'
input:
file(new_refs)
output:
file("new_references.csv") into new_refs_f
"""
#!/usr/bin/env python
import csv
files = "${new_refs.join(";")}".split(';')
if len(files) > 0:
readers = [
csv.DictReader(open(fn, 'rt'))
for fn in files
]
headers = [
r.fieldnames
for r in readers
]
common_header = headers[0]
for ch in headers[1:]:
common_header += [h for h in ch if h not in common_header]
with open("new_references.csv", 'wt') as out_h:
out_w = csv.DictWriter(out_h, fieldnames=common_header)
out_w.writeheader()
for reader in readers:
out_w.writerows(reader)
else:
open("new_references.csv", 'wt')
"""
}
new_dl_split_ch.refseq_info
.filter {
!file(it).isEmpty()
}
.unique()
.toList()
.set {
new_refseq_infos
}
process combineBatches_refseq {
container = 'golob/medirect:0.14.0__bcw.0.3.1B'
label = 'io_limited'
input:
file(new_refseq_infos)
output:
file("new_refseq_info.csv") into new_refseq_info_f
"""
#!/usr/bin/env python
import csv
files = "${new_refseq_infos.join(";")}".split(';')
if len(files) > 0:
readers = [
csv.DictReader(open(fn, 'rt'))
for fn in files
]
headers = [
r.fieldnames
for r in readers
]
common_header = headers[0]
for ch in headers[1:]:
common_header += [h for h in ch if h not in common_header]
with open("new_refseq_info.csv", 'wt') as out_h:
out_w = csv.DictWriter(out_h, fieldnames=common_header)
out_w.writeheader()
for reader in readers:
out_w.writerows(reader)
else:
open("new_refseq_info.csv", 'wt')
"""
}
// Step 5: Align against RDP type strains with vsearch to validate
process vsearch_rdp_validate {
container = "${container__arf}"
label 'mem_veryhigh'
cache 'deep'
errorStrategy 'finish'
input:
tuple file (new_16s_seqs_fasta_f), file(new_16s_si_f) from new_dl_features_for_vsearch_ch
file (prior_unknowns_f)
output:
tuple file ("vsearch/${new_16s_seqs_fasta_f.getBaseName()}.seqs.fasta"),
file("vsearch/${new_16s_seqs_fasta_f.getBaseName()}.seq_info.csv"),
file ("vsearch/${new_16s_seqs_fasta_f.getBaseName()}.unknown.fasta"),
file ("vsearch/${new_16s_seqs_fasta_f.getBaseName()}.unknowns.txt") into post_vsearch_ch
script:
"""
set -e
touch ${prior_unknowns_f}
vsearch --threads ${task.cpus} \
--db /db/rdp_16s_type_strains.fasta.gz \
--id 0.70 --iddef 2 --mincols 350 --query_cov 0.70 --strand both \
--maxaccepts 1 --maxrejects 32 --top_hits_only \
--output_no_hits --userfields query+target+qstrand+id+tilo+tihi \
--usearch_global ${new_16s_seqs_fasta_f} \
--userout vsearch.tsv
mkdir -p vsearch
vsearch.py vsearch.tsv ${new_16s_seqs_fasta_f} ${new_16s_si_f} ${prior_unknowns_f} \
vsearch/${new_16s_seqs_fasta_f.getBaseName()}.seqs.fasta vsearch/${new_16s_seqs_fasta_f.getBaseName()}.seq_info.csv vsearch/${new_16s_seqs_fasta_f.getBaseName()}.unknown.fasta vsearch/${new_16s_seqs_fasta_f.getBaseName()}.unknowns.txt
"""
}
// Step 6: Build taxonomy DB Check tax IDs in seq_info using taxtastic
process downloadTaxdump {
container = "${container__arf}"
label = 'io_limited'
errorStrategy 'finish'
publishDir path: "${params.out}/", mode: "copy"
executor 'local'
output:
file "taxdmp.zip" into taxdmp_f
"""
set -e
wget ftp://ftp.ncbi.nlm.nih.gov/pub/taxonomy/taxdmp.zip
"""
}
process buildTaxtasticDB {
container = "${container__arf}"
label = 'io_limited'
publishDir path: "${params.out}/", mode: "copy"
errorStrategy 'finish'
input:
file taxdmp_f
output:
file "taxonomy.db" into taxonomy_db_f
"""
taxit new_database taxonomy.db -z ${taxdmp_f}
"""
}
post_vsearch_ch
.multiMap { it ->
seqs: it[0]
si: it[1]
unknown_fasta: it[2]
unknown_rec: it[3]
}
.set { post_vsearch_split }
post_vsearch_split.si
.toList()
.set {
post_vsearch_si
}
post_vsearch_split.seqs
.toList()
.set {
post_vsearch_seqs
}
post_vsearch_split.unknown_rec
.toList()
.set {
post_vsearch_unknown_recs
}
// Combine post-vsearch batches!
process combineBatches_unknown_recs {
container = 'golob/medirect:0.14.0__bcw.0.3.1B'
label = 'io_limited'
input:
file "inputs/*" from post_vsearch_unknown_recs
output:
file("new_vsearch_unknown.txt") into vsearch_unknowns_txt_f
"""
set -e
cat inputs/* > new_vsearch_unknown.txt
"""
}
process combineBatches_seq {
container = 'golob/medirect:0.14.0__bcw.0.3.1B'
label = 'io_limited'
input:
file "inputs/*" from post_vsearch_seqs
output:
file("new_combined_seqs.fasta") into new_combined_seqs_f
"""
set -e
cat inputs/* > new_combined_seqs.fasta
"""
}
process combineBatches_si {
container = 'golob/medirect:0.14.0__bcw.0.3.1B'
label = 'io_limited'
input:
file(post_vsearch_si)
output:
file("new_combined_seq_info.csv") into new_combined_si_f
"""
#!/usr/bin/env python
import csv
files = "${post_vsearch_si}".split()
readers = [
csv.DictReader(open(fn, 'rt'))
for fn in files
]
headers = [
r.fieldnames
for r in readers
]
common_header = headers[0]
for ch in headers[1:]:
common_header += [h for h in ch if h not in common_header]
with open("new_combined_seq_info.csv", 'wt') as out_h:
out_w = csv.DictWriter(out_h, fieldnames=common_header)
out_w.writeheader()
for reader in readers:
out_w.writerows(reader)
"""
}
process filterUnknownTaxa {
container = "${container__arf}"
label = 'io_limited'
errorStrategy 'finish'
input:
file taxonomy_db_f
file new_combined_si_f
output:
file "filtered/seq_info.csv" into new_filtered_si_f
"""
mkdir -p filtered/
taxit update_taxids \
--unknown-action drop --outfile filtered/seq_info.csv \
${new_combined_si_f} ${taxonomy_db_f}
"""
}
// Step 7: Refresh the repo seqs!
// A bit of fakery for debug
if (params.debug) {
process debug_records {
container 'golob/ya16sdb:0.2C'
label 'io_limited'
errorStrategy 'finish'
cache 'deep'
input:
file "prior_records.txt" from prior_records_f
file "current_records.txt" from records_dl_f
output:
file 'records.txt' into records_current_f
"""
#!/usr/bin/env python3
n = 0
with open('records.txt', 'wt') as out_h:
for l in open('current_records.txt', 'rt'):
out_h.write(l.strip()+'\\n')
for l in open('prior_records.txt', 'rt'):
out_h.write(l.strip()+'\\n')
n += 1
if n > 1000:
break
"""
}} else {
records_current_f = records_dl_f
}
process refreshRecords {
container 'golob/ya16sdb:0.2C'
label 'io_mem'
cache 'deep'
publishDir path: "${params.out}/", mode: "copy"
errorStrategy 'finish'
input:
file "new/records.txt" from records_current_f
file "new/seqs.fasta" from new_combined_seqs_f
file "new/seq_info.csv" from new_filtered_si_f
file "new/pubmed_info.csv" from new_pubmed_f
file "new/references.csv" from new_refs_f
file "new/refseq_info.csv" from new_refseq_info_f
file vsearch_unknowns_txt_f
file "prior/seqs.fasta" from prior_seqs_f
file "prior/seq_info.csv" from prior_si_f
file "prior/pubmed_info.csv" from prior_pubmed_f
file "prior/references.csv" from prior_references_f
file "prior/refseq_info.csv" from prior_refseqinfo_f
file "prior/records.txt" from prior_records_f
output:
file "seqs.fasta" into refresh_seqs_f
file "seq_info.csv" into refresh_si_unverified_f
file "pubmed_info.csv" into refresh_pubmed_f
file "references.csv" into refresh_references_f
file "refseq_info.csv" into refresh_refseqinfo_f
file "records.txt" into refresh_records_f
script:
"""
set -e
touch prior/seqs.fasta
touch prior/seq_info.csv
touch prior/pubmed_info.csv
touch prior/references.csv
touch prior/refseq_info.csv
touch prior/records.txt
mkdir -p refresh/
refresh.py \
new/records.txt \
new/seqs.fasta prior/seqs.fasta \
new/seq_info.csv prior/seq_info.csv \
new/pubmed_info.csv prior/pubmed_info.csv \
new/references.csv prior/references.csv \
new/refseq_info.csv prior/refseq_info.csv \
${vsearch_unknowns_txt_f} prior/records.txt \
seqs.fasta \
seq_info.csv \
pubmed_info.csv \
references.csv \
refseq_info.csv \
records.txt
"""
}
// Step 8: Verify the refreshed tax ID and make a taxtable
process refresh_verifyTaxIds {
container 'golob/ya16sdb:0.2C'
label 'io_mem'
errorStrategy 'finish'
input:
file taxonomy_db_f
file "unverified_si.csv" from refresh_si_unverified_f
output:
file "seq_info.csv" into refresh_si_f
"""
taxit update_taxids \
--unknown-action drop --outfile seq_info.csv \
unverified_si.csv ${taxonomy_db_f}
"""
}
process taxonomyTable_refresh {
container 'golob/taxtastic:0.9.0'
label 'io_mem'
publishDir path: "${params.out}/", mode: "copy"
input:
file taxonomy_db_f
file refresh_si_f
output:
file "taxonomy.csv" into refresh_taxonomy_table_f
"""
taxit -v taxtable --seq-info ${refresh_si_f} --out taxonomy.csv ${taxonomy_db_f}
"""
}
// Step 9: Make a feather file to help sort items around
process buildFeatherSI {
container 'golob/ya16sdb:0.2C'
label 'io_mem'
errorStrategy 'finish'
input:
file refresh_seqs_f
file refresh_si_f
file refresh_taxonomy_table_f
file acc_types_f
file refresh_pubmed_f
file refresh_refseqinfo_f
file taxonomy_db_f
output:
file "seq_info.feather" into refresh_feather_si
"""
to_feather.py ${refresh_si_f} seq_info.feather
taxonomy.py seq_info.feather ${refresh_taxonomy_table_f}
is_type.py seq_info.feather ${acc_types_f}
is_published.py seq_info.feather ${refresh_pubmed_f}
is_refseq.py seq_info.feather ${refresh_refseqinfo_f}
is_valid.py seq_info.feather sqlite:///${taxonomy_db_f}
confidence.py seq_info.feather
seqhash.py seq_info.feather ${refresh_seqs_f}
sort_values.py seq_info.feather "is_type,is_published,is_refseq,ambig_count,modified_date,download_date,seqhash"
"""
}
// Step 10: Split out our deduplicated 1200bp seqs.
process refresh_dd1200bp {
container 'golob/ya16sdb:0.2C'
label 'io_mem'
publishDir path: "${params.out}/dedup/1200bp/", mode: "copy"
errorStrategy 'finish'
input:
file refresh_feather_si
file "unfiltered_seqs.fasta" from refresh_seqs_f
output:
file "seqs.fasta" into refresh_dd1200_seqs_f
file "seq_info.csv" into refresh_dd1200_si_f
file "blast.nhr" into refresh_dd1200_nhr_f
file "blast.nsq" into refresh_dd1200_nsq_f
file "blast.nin" into refresh_dd1200_nin_f
"""
set -e
partition_refs.py \
--drop-duplicate-sequences \
--min-length 1200 \
--prop-ambig-cutoff 0.01 \
unfiltered_seqs.fasta ${refresh_feather_si} \
seqs.fasta seq_info.csv
makeblastdb -dbtype nucl -in seqs.fasta -out blast
"""
}
process refresh_types {
container 'golob/ya16sdb:0.2C'
label 'io_mem'