-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnakefile
1413 lines (1312 loc) · 65 KB
/
Snakefile
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
# Ideally, format your hostcoes as 'species'_'accession'_'uniquenr/library/strain/treatment' for later automatic processing.
HOSTCODES= ['Azrub_IRRI_479','Azfil_lab_250', 'Azfil_lab_500', 'Azfil_lab_800', 'Azfil_minuscyano_170', 'Azfil_minuscyano_350', 'Azfil_wild_galgw_E_1', 'Azfil_wild_galgw_E_2', 'Azfil_wild_galgw_E_3', 'Azfil_wild_galgw_P_2', 'Azfil_wild_galgw_P_3', 'Azfil_wild_galgw_P_4', 'Azmex_IRRI_486', 'Azmic_IRRI_456', 'Aznil_IRRI_479', 'Azspnov_IRRI1_472', 'Azspnov_IRRI2_489']
DIRECTIONS=["1","2"]
# refined is a manual subset of the final libraries/assemblies chosen for final analysis
REFINED = ['Azrub_IRRI_479', 'Azfil_lab', 'Azfil_minuscyano', 'Azfil_wild', 'Azmex_IRRI_486', 'Azmic_IRRI_456', 'Aznil_IRRI_479', 'Azspnov_IRRI1_472', 'Azspnov_IRRI2_489']
# Assuming that host codes are formated as 'species'_'accession'_'uniquenr/library/strain/treatment' the following line of code
# autmatically creates an array of host accession combinations for cross-assembly of different strains,sequencing libraries, treatments.
# Likewise, the code below creates a list of species which have multiple sequencing libraries.
def get_species_cross_assembly():
return_species=set([])
for i in set([i.split('_',3)[0] for i in HOSTCODES]):
species_sub=list(filter(lambda x:i in x, HOSTCODES))
if len(species_sub) >= 2 :
return_species.add(i)
return(list(return_species))
def get_hosts_cross_assembly():
return_hosts=set([])
for i in set([i.split('_',3)[0] + '_' + i.split('_',3)[1] for i in HOSTCODES]):
host_sub=list(filter(lambda x:i in x, HOSTCODES))
if len(host_sub) >= 2 :
return_hosts.add(i)
return(list(return_hosts))
def get_single_hosts():
return_hosts=set([])
for i in HOSTCODES:
species = i.split('_',3)[0] + '_' + i.split('_',3)[1]
host_sub = list(filter(lambda x:species in x, HOSTCODES))
if len(host_sub) == 1 :
return_hosts.add(i)
return(list(return_hosts))
SPECIES=get_species_cross_assembly()
HOSTS=get_hosts_cross_assembly()
SINGLEHOSTS=get_single_hosts()
ASSEMBLYTYPES=['singles_doublefiltered','singles_hostfiltered'] # ,'hybrid_doublefiltered'] #,'species_doublefiltered']
# Ideally these should only contain letter, numbers and underscores. Exceptionally, these can contain points but they will be replaced by underscores for anvi'o
BINNINGSIGNALS=['dijkhuizen2018.E.1', 'dijkhuizen2018.E.2', 'dijkhuizen2018.E.3', 'dijkhuizen2018.P.2', 'dijkhuizen2018.P.3', 'dijkhuizen2018.P.4','ran2010.nostoc.SRR066216','ran2010.nostoc.SRR066217','ran2010.nostoc.SRR3923641','ran2010.nostoc.SRR3923645','ran2010.nostoc.SRR3923646']
ASSEMBLYFILES=['contigs','scaffolds']
## 'All'-rules
rule all_assemblies_and_annotations:
input:
"analyses/assembly_stats_and_taxonomy.tab",
expand("data/bins_{assemblytype}/{hostcode}.BAT.names.txt",assemblytype=ASSEMBLYTYPES,hostcode=HOSTCODES),
expand("data/bins_{assemblytype}_checkm/{hostcode}/{hostcode}.checkm_out",assemblytype=ASSEMBLYTYPES,hostcode=HOSTCODES),
expand("data/assembly_{assemblytype}_anvio/{hostcode}/{hostcode}_contigs_db_run_ncbi_cogs.done",assemblytype='singles_doublefiltered',hostcode=HOSTCODES),
expand("data/assembly_{assemblytype}_binningsignals_anvio/MERGED_{hostcode}/CAT_taxonomy_imported.done",assemblytype='singles_doublefiltered',hostcode=HOSTCODES),
expand("data/assembly_{assemblytype}_binningsignals_anvio/MERGED_{hostcode}/PROFILE_db_imported-metabat2.done",assemblytype='singles_doublefiltered',hostcode=HOSTCODES),
"analyses/assembly-hybrid_stats_and_taxonomy.tab",
expand("data/bins_{assemblytype}/{hostcode}.BAT.names.txt",assemblytype='hybrid_doublefiltered',hostcode=HOSTS),
expand("data/bins_{assemblytype}_checkm/{hostcode}/{hostcode}.checkm_out",assemblytype='hybrid_doublefiltered',hostcode=HOSTS),
expand("data/assembly_{assemblytype}_anvio/{hostcode}/{hostcode}_contigs_db_run_ncbi_cogs.done",assemblytype='hybrid_doublefiltered',hostcode=HOSTS),
expand("data/assembly_{assemblytype}_binningsignals_anvio/MERGED_{hostcode}/CAT_taxonomy_imported.done",assemblytype='hybrid_doublefiltered',hostcode=HOSTS),
expand("data/assembly_{assemblytype}_binningsignals_anvio/MERGED_{hostcode}/PROFILE_db_imported-metabat2.done",assemblytype='hybrid_doublefiltered',hostcode=HOSTS)
rule all_exported_bins:
input:
expand("data/curated_bins/{{collection}}/{hostcode}",hostcode=HOSTS),
expand("data/curated_bins/{{collection}}/{hostcode}",hostcode=SINGLEHOSTS)
output:
touch("data/curated_bins/{collection}.exported")
rule all_exported_bins_refined_checkm_BAT:
input:
expand("data/curated_bins/{collection}/{hostcode}/{hostcode}.checkm_out.txt", collection='refined', hostcode=REFINED),
expand("data/curated_bins/{collection}/{hostcode}/{hostcode}.BAT.names.txt" , collection='refined', hostcode=REFINED)
rule trim:
input:
expand("data/sequencing_genomic_trimmed/{hostcode}_R{PE}.fastq.gz",hostcode=HOSTCODES,PE=DIRECTIONS)
## Read QC stuff and rules for a data flow figure
rule fastqc_raw_data:
input:
"data/sequencing_genomic/{hostcode}_R{PE}.fastq.gz"
output:
directory("analyses/analyses_reads/{hostcode}_{PE}")
shell:
"mkdir {output} 2> /dev/null && fastqc -o {output} {input}"
rule fastqc_trimmed_data:
input:
"data/sequencing_genomic_trimmed/{hostcode}_{PE}.fastq.gz"
output:
directory("analyses/analyses_reads/{hostcode}_{PE}")
shell:
"mkdir {output} 2> /dev/null && fastqc -o {output} {input}"
rule fastqc_filtered_data:
input:
"data/sequencing_genomic_trimmed_filtered_corrected/{hostcode}/corrected/{hostcode}.{PE}.fastq.00.0_0.cor.fastq.gz"
output:
directory("analyses/analyses_reads_trimmed_filtered/{hostcode}_{PE}")
shell:
"mkdir {output} 2> /dev/null && fastqc -o {output} {input}"
rule fastqc_doublefiltered_data:
input:
"data/sequencing_doublefiltered/{hostcode}/{hostcode}.{PE}.fastq.gz"
output:
directory("analyses/analyses_reads_doublefiltered/{hostcode}_{PE}")
shell:
"mkdir {output} 2> /dev/null && fastqc -o {output} {input}"
rule collect_reads_stats:
input:
expand("analyses/analyses_reads/{hostcode}_{PE}" ,hostcode=HOSTCODES,PE=DIRECTIONS),
expand("analyses/analyses_reads_trimmed/{hostcode}_{PE}" ,hostcode=HOSTCODES,PE=DIRECTIONS),
expand("analyses/analyses_reads_trimmed_filtered/{hostcode}_{PE}" ,hostcode=HOSTCODES,PE=DIRECTIONS),
expand("analyses/analyses_reads_doublefiltered/{hostcode}_{PE}" ,hostcode=HOSTCODES,PE=DIRECTIONS)
# expand("data/assembly_{assemblytype}/{hostcode}/{assemblyfile}.fasta",assemblytype=ASSEMBLYTYPES,hostcode=HOSTCODES,assemblyfile='contigs')
output:
"analyses/reads_stats.tab"
shell:
"bash ./scripts/collect_reads_stats.bash > {output}"
## reference rules
rule download_azolla_genome:
output:
"references/host_genome/host_genome.fasta"
shell:
"wget ftp://ftp.fernbase.org/Azolla_filiculoides/Azolla_asm_v1.1/Azolla_filiculoides.genome_v1.2.fasta -O {output}"
rule download_azolla_proteins:
output:
"references/host_genome/host_proteins.fasta"
shell:
"wget ftp://ftp.fernbase.org/Azolla_filiculoides/Azolla_asm_v1.1/Azolla_filiculoides.protein.highconfidence_v1.1.fasta -O {output}"
rule CAT_download:
output:
# db=temp("references/CAT_prepare_20190108/2019-01-08_CAT_database"),
tf=temp("references/CAT_prepare_20190108/2019-01-08_taxonomy"),
nr=temp("references/CAT_prepare_20190108/2019-01-08_CAT_database/2019-01-08.nr.gz")
shell:
"cd ./references && wget -qO - http://tbb.bio.uu.nl/bastiaan/CAT_prepare/CAT_prepare_20190108.tar.gz | tar -xz"
rule CAT_customise:
input:
db="references/CAT_prepare_20190108/2019-01-08_CAT_database/2019-01-08.nr.gz",
tf="references/CAT_prepare_20190108/2019-01-08_taxonomy",
custom_proteins="references/host_genome/host_proteins.fasta"
output:
nr="references/CAT_customised_20190108/CAT_database_customised/2019-1-08.nr.gz",
db=directory("references/CAT_customised_20190108/CAT_database_customised"),
tf=directory("references/CAT_customised_20190108/taxonomy_customised"),
tf_id="references/CAT_customised_20190108/taxonomy_customised/2019-01-08.prot.accession2taxid.gz"
shell:
"""
cp {input.db} {output.nr}
cp -r {input.tf}/* {output.tf}
pigz -c {input.custom_proteins} >> {output.nr}
grep '>' {input.custom_proteins} | tr -d '>' | awk -v OFS='\t' '{{print $0, $0, 84609, 0}}' | pigz -c >> {output.tf_id}
"""
rule CAT_build:
input:
db="references/CAT_customised_20190108/CAT_database_customised",
tf="references/CAT_customised_20190108/taxonomy_customised"
output:
"references/CAT_customised_20190108/CAT_database_customised/2019-03-27.nr.dmnd"
log:
stdout="logs/CAT_build_nr+host.stdout",
stderr="logs/CAT_build_nr+host.stderr"
threads: 100
shell:
"CAT prepare --existing -d {input.db} -t {input.tf} -n {threads} > {log.stdout} 2> {log.stderr}"
rule CAT_prepare_ORFS_host:
input:
"references/host_genome/host_genome.fasta"
output:
p="references/host_genome/host.predicted_proteins.faa",
g="references/host_genome/host.predicted_proteins.gff",
params:
"-p meta -g 11 -q -f gff"
threads: 1
log:
stdout="logs/CAT_host_prodigal.stdout",
stderr="logs/CAT_host_prodigal.stderr"
shell:
"prodigal -i {input} -a {output.p} -o {output.g} {params} 2> {log.stderr} > {log.stdout}"
rule CAT_classify_host:
input:
prot="references/host_genome/host.predicted_proteins.faa",
contigs="references/host_genome/host_genome.fasta",
dmnd="references/CAT_customised_20190108/CAT_database_customised/2019-03-27.nr.dmnd",
db="references/CAT_customised_20190108/CAT_database_customised",
tf="references/CAT_customised_20190108/taxonomy_customised"
params:
prefix="references/host_genome/CAT/host",
options="-r 5"
output:
i="references/host_genome/CAT/host.contig2classification.txt"
threads: 100
shadow: 'shallow'
log:
stdout="logs/CAT_classify_host.stdout",
stderr="logs/CAT_classify_host.stderr"
shell:
"CAT contigs {params.options} -p {input.prot} -c {input.contigs} -d {input.db} -t {input.tf} --out_prefix {params.prefix} -n {threads} 2> {log.stderr} > {log.stdout}"
# shall I remove host.alignment.diamond, it's huge and does not serve a purpose further on.
rule CAT_add_names:
input:
i="references/host_genome/CAT/host.contig2classification.txt",
tf="references/CAT_customised_20190108/taxonomy_customised"
output:
"references/host_genome/contig_taxonomy.tab"
log:
stdout="logs/CAT_addnames_host.stdout",
stderr="logs/CAT_addnames_host.stderr"
params: "--only_official"
threads: 1
shell:
"CAT add_names {params} -i {input.i} -t {input.tf} -o {output} > {log.stdout} 2> {log.stderr}"
rule CAT_filter_contignames:
input:
"references/host_genome/contig_taxonomy.tab"
output:
"references/host_genome/contig_filterlist.txt"
threads: 1
log:
"logs/CAT_contigfilterlist.stderr"
shell:
"cat {input} | grep -v '#' | grep -v Bacteria | cut -f 1 > {output} 2> {log}"
rule CAT_get_bacterial_contignames:
input:
"references/host_genome/contig_taxonomy.tab"
output:
"references/host_genome/bacterial_contigs.txt"
threads: 1
log:
"logs/CAT_contigfilterlist.stderr"
shell:
"cat {input} | grep -v '#' | grep Bacteria | cut -f 1 > {output} 2> {log}"
rule create_host_bacterialcontigs_fasta:
input:
n="references/host_genome/bacterial_contigs.txt",
f="references/host_genome/host_genome.fasta"
output:
expand("references/host_genome/{host}_host-genome_bacterial_contigs.fasta",host='Azfil_lab')
threads: 1
log:
stdout="logs/CAT_create_host_bacterial_contigs.stdout",
stderr="logs/CAT_create_host_bacterial_contigs.stderr"
shell:
"samtools faidx {input.f} -o {output} -r {input.n} > {log.stdout} 2> {log.stderr}"
rule create_host_filter_fasta:
input:
n="references/host_genome/contig_filterlist.txt",
f="references/host_genome/host_genome.fasta"
output:
"references/host_genome/host_filter.fasta"
threads: 1
log:
stdout="logs/CAT_createhostfilter.stdout",
stderr="logs/CAT_createhostfilter.stderr"
shell:
"samtools faidx {input.f} -o {output} -r {input.n} > {log.stdout} 2> {log.stderr}"
rule create_host_filter_bt2_index:
input:
"references/host_genome/host_filter.fasta"
params:
"references/host_genome/host_filter_bt2index/host_filter"
output:
expand("references/host_genome/host_filter_bt2index/host_filter.{i}.bt2",i=range(1,4)),
expand("references/host_genome/host_filter_bt2index/host_filter.rev.{i}.bt2",i=range(1,2))
threads: 100
log:
stdout="logs/CAT_createhostfilterbt2index.stdout",
stderr="logs/CAT_createhostfilterbt2index.stderr"
shell:
"bowtie2-build --threads {threads} {input} {params} > {log.stdout} 2> {log.stderr}"
## data processing rules
rule trimmomatic_genomic_sequencing:
input:
expand("data/sequencing_genomic/{{hostcode}}_R{PE}.fastq.gz", PE=DIRECTIONS)
params:
"LEADING:5 TRAILING:5 SLIDINGWINDOW:4:15 MINLEN:36"
output:
p1=temp("data/sequencing_genomic_trimmed/{hostcode}_R1.fastq.gz"),
p2=temp("data/sequencing_genomic_trimmed/{hostcode}_R2.fastq.gz")
threads: 4
resources: io=1
log:
stdout="logs/trimmomatic_genomicsequencing.{hostcode}.stdout",
stderr="logs/trimmomatic_genomicsequencing.{hostcode}.stderr",
log="logs/trimmomatic_genomicsequencing.{hostcode}.log",
summary="logs/trimmomatic_genomicsequencing_summary{hostcode}.log"
shell:
"trimmomatic PE -threads {threads} -trimlog {log.log} -summary {log.summary} {input} {output.p1} /dev/null {output.p2} /dev/null {params} > {log.stdout} 2> {log.stderr}"
rule filter_for_host:
input:
expand("references/host_genome/host_filter_bt2index/host_filter.{i}.bt2",i=range(1,4)),
expand("references/host_genome/host_filter_bt2index/host_filter.rev.{i}.bt2",i=range(1,2)),
s1=expand("data/sequencing_genomic_trimmed/{{hostcode}}_R{PE}.fastq.gz",PE=1),
s2=expand("data/sequencing_genomic_trimmed/{{hostcode}}_R{PE}.fastq.gz",PE=2)
params:
opts="--very-fast",
i="references/host_genome/host_filter_bt2index/host_filter",
outbase="data/sequencing_genomic_trimmed_filtered/{hostcode}"
output:
temp(expand("data/sequencing_genomic_trimmed_filtered/{{hostcode}}.{PE}",PE=DIRECTIONS))
threads: 100
log:
stderr="logs/bowtie2filterforhost{hostcode}.stderr"
shell:
"bowtie2 {params.opts} --threads {threads} --un-conc-gz {params.outbase} -x {params.i} -1 {input.s1} -2 {input.s2} > /dev/null 2> {log.stderr}"
rule filter_for_host_rename:
input:
reads=expand("data/sequencing_genomic_trimmed_filtered/{{hostcode}}.{PE}",PE=DIRECTIONS),
s1=expand("data/sequencing_genomic_trimmed_filtered/{{hostcode}}.{PE}",PE=1),
s2=expand("data/sequencing_genomic_trimmed_filtered/{{hostcode}}.{PE}",PE=2)
output:
reads=expand("data/sequencing_genomic_trimmed_filtered/{{hostcode}}.{PE}.fastq.gz",PE=DIRECTIONS),
s1=expand("data/sequencing_genomic_trimmed_filtered/{{hostcode}}.{PE}.fastq.gz",PE=1),
s2=expand("data/sequencing_genomic_trimmed_filtered/{{hostcode}}.{PE}.fastq.gz",PE=2)
shell:
"mv {input.s1} {output.s1} && mv {input.s2} {output.s2}"
rule spades_hammer:
input:
reads=expand("data/sequencing_genomic_trimmed_filtered/{{hostcode}}.{PE}.fastq.gz",PE=DIRECTIONS),
s1=expand("data/sequencing_genomic_trimmed_filtered/{{hostcode}}.{PE}.fastq.gz",PE=1),
s2=expand("data/sequencing_genomic_trimmed_filtered/{{hostcode}}.{PE}.fastq.gz",PE=2)
params:
options="--only-error-correction",
basedir=lambda w: expand("data/sequencing_genomic_trimmed_filtered_corrected/{hostcode}/",hostcode=w.hostcode)
output:
reads=expand("data/sequencing_genomic_trimmed_filtered_corrected/{{hostcode}}/corrected/{{hostcode}}.{PE}.fastq.00.0_0.cor.fastq.gz",PE=DIRECTIONS)
threads: 100
log:
stdout="logs/SPAdes_correct_sequencing{hostcode}.stdout",
stderr="logs/SPAdes_correct_sequencing{hostcode}.stderr"
shell:
"spades.py {params.options} -t {threads} -1 {input.s1} -2 {input.s2} -o {params.basedir} > {log.stdout} 2> {log.stderr}"
rule spades_first_assembly:
input:
s1=ancient(expand("data/sequencing_genomic_trimmed_filtered_corrected/{{hostcode}}/corrected/{{hostcode}}.{PE}.fastq.00.0_0.cor.fastq.gz",PE=1)),
s2=ancient(expand("data/sequencing_genomic_trimmed_filtered_corrected/{{hostcode}}/corrected/{{hostcode}}.{PE}.fastq.00.0_0.cor.fastq.gz",PE=2))
params:
basedir= lambda w: expand("data/assembly_{assemblytype}/{hostcode}/", hostcode=w.hostcode, assemblytype='singles_hostfiltered'),
options="--meta --only-assembler"
output:
contigs=expand("data/assembly_{assemblytype}/{{hostcode}}/{assemblyfile}.fasta",assemblytype='singles_hostfiltered',assemblyfile='contigs'),
scaffolds=expand("data/assembly_{assemblytype}/{{hostcode}}/{assemblyfile}.fasta",assemblytype='singles_hostfiltered',assemblyfile='scaffolds')
threads: 100
shadow: "shallow"
resources:
mem_mb=500
log:
stdout=expand("logs/SPADES_assembly_{assemblytype}_{{hostcode}}.stdout",assemblytype='singles_hostfiltered'),
stderr=expand("logs/SPADES_assembly_{assemblytype}_{{hostcode}}.stderr",assemblyfile='contigs',assemblytype='singles_hostfiltered')
shell:
"spades.py {params.options} -t {threads} -m {resources.mem_mb} -1 {input.s1} -2 {input.s2} -o {params.basedir} > {log.stdout} 2> {log.stderr}"
## process assembly for taxonomy including a taxonomy based second filter
rule CAT_prepare_ORFS:
input:
assembly="data/assembly_{assemblytype}/{hostcode}/{assemblyfile}.fasta"
output:
p="data/assembly_{assemblytype}/{hostcode}/{assemblyfile}_predicted_proteins.fasta",
g="data/assembly_{assemblytype}/{hostcode}/{assemblyfile}_predicted_proteins.gff"
params:
"-p meta -g 11 -q -f gff"
threads: 1
log:
stdout="logs/CAT_assembly_{assemblytype}_{assemblyfile}_prodigal_{hostcode}.stdout",
stderr="logs/CAT_assembly_{assemblytype}_{assemblyfile}_prodigal_{hostcode}.stderr"
shell:
"prodigal -i {input.assembly} -a {output.p} -o {output.g} {params} 2> {log.stderr} > {log.stdout}"
rule CAT_classify_contigs_assembly:
input:
assembly="data/assembly_{assemblytype}/{hostcode}/{assemblyfile}.fasta",
dmnd="references/CAT_customised_20190108/CAT_database_customised/2019-03-27.nr.dmnd",
db="references/CAT_customised_20190108/CAT_database_customised",
tf="references/CAT_customised_20190108/taxonomy_customised",
p="data/assembly_{assemblytype}/{hostcode}/{assemblyfile}_predicted_proteins.fasta"
output:
i="data/assembly_{assemblytype}/{hostcode}/CAT_{hostcode}_{assemblyfile}.contig2classification.txt",
# g=expand("data/assembly_{assemblytype}/{{hostcode}}/CAT_{{hostcode}}.predicted_proteins.gff",assemblytype='singles_hostfiltered'),
# f=expand("data/assembly_{assemblytype}/{{hostcode}}/CAT_{{hostcode}}.predicted_proteins.faa",assemblytype='singles_hostfiltered'),
o="data/assembly_{assemblytype}/{hostcode}/CAT_{hostcode}_{assemblyfile}.ORF2LCA.txt",
l="data/assembly_{assemblytype}/{hostcode}/CAT_{hostcode}_{assemblyfile}.log"
shadow: 'shallow'
params:
b = lambda w: expand("data/assembly_{assemblytype}/{hostcode}/CAT_{hostcode}_{assemblyfile}", assemblytype=w.assemblytype, hostcode=w.hostcode, assemblyfile=w.assemblyfile),
options = '-r 5'
threads: 100
resources:
mem_mb=30000
log:
stdout="logs/CAT_assembly_{assemblytype}_{assemblyfile}_classification_{hostcode}.stdout",
stderr="logs/CAT_assembly_{assemblytype}_{assemblyfile}_classification_{hostcode}.stderr"
shell:
"CAT contigs {params.options} -c {input.assembly} -d {input.db} -p {input.p} -t {input.tf} --out_prefix {params.b} -n {threads} 2> {log.stderr} > {log.stdout}"
rule CAT_add_names_assembly:
input:
i="data/assembly_{assemblytype}/{hostcode}/CAT_{hostcode}_{assemblyfile}.contig2classification.txt",
tf="references/CAT_customised_20190108/taxonomy_customised"
output:
"data/assembly_{assemblytype}/{hostcode}/CAT_{hostcode}_{assemblyfile}_taxonomy.tab"
params:
"--only_official"
log:
stdout="logs/CAT_assembly_{assemblytype}_{assemblyfile}_classification_taxonomy_{hostcode}.stdout",
stderr="logs/CAT_assembly_{assemblytype}_{assemblyfile}_classification_taxonomy_{hostcode}.stderr"
threads: 1
shell:
"CAT add_names {params} -i {input.i} -t {input.tf} -o {output} > {log.stdout} 2> {log.stderr}"
rule CAT_filter_contignames_first_spades_assembly:
input:
expand("data/assembly_{{assemblytype}}/{{hostcode}}/CAT_{{hostcode}}_{assemblyfile}_taxonomy.tab",assemblyfile='scaffolds')
output:
expand("data/assembly_{{assemblytype}}/{{hostcode}}/CAT_{{hostcode}}_{assemblyfile}_filterlist.txt",assemblyfile='scaffolds')
threads: 1
log:
expand("logs/CAT_assembly_{{assemblytype}}_{assemblyfile}filterlist_{{hostcode}}.stderr",assemblyfile='scaffolds')
shell:
"cat {input} | grep -v Eukaryota | cut -f 1 | sort -n > {output} 2> {log}"
rule BAT_filter_contignames_bins:
input:
"data/bins_{assemblytype}/{hostcode}.BAT.names.txt"
output:
"data/assembly_{assemblytype}/{hostcode}/BAT_{hostcode}_filterlist.txt"
params:
binfolder = "data/bins_{assemblytype}/{hostcode}"
log:
"logs/BAT_filter_contignames_bins_{assemblytype}_{hostcode}"
shell:
"""
for f in $(cat {input} | grep Eukaryota | cut -f 1)
do grep '>' {params.binfolder}/$f.fa
done | tr -d '>' | cut -f 1 > {output} 2> {log}
"""
rule combine_filter_contignames:
input:
"data/assembly_{assemblytype}/{hostcode}/BAT_{hostcode}_filterlist.txt",
expand("data/assembly_{{assemblytype}}/{{hostcode}}/CAT_{{hostcode}}_{assemblyfile}_filterlist.txt",assemblyfile='scaffolds')
output:
expand("data/assembly_{{assemblytype}}/{{hostcode}}/combined_filterlist_{{hostcode}}_{assemblyfile}.tab",assemblyfile='scaffolds')
shell:
"cat {input} | cut -f 1 | sort | uniq > {output}"
rule create_filter_fasta_first_spades_assembly:
input:
n=expand("data/assembly_{{assemblytype}}/{{hostcode}}/combined_filterlist_{{hostcode}}_{assemblyfile}.tab",assemblyfile='scaffolds'),
f=expand("data/assembly_{{assemblytype}}/{{hostcode}}/{assemblyfile}_short_names.fasta",assemblyfile='scaffolds')
output:
"data/assembly_{assemblytype}/{hostcode}/CAT_BAT_filter_{hostcode}.fasta"
threads: 1
log:
stdout="logs/CAT_create_assembly_{assemblytype}_filter_{hostcode}.stdout",
stderr="logs/CAT_create_assembly_{assemblytype}_filter_{hostcode}.stderr"
shell:
"samtools faidx {input.f} -o {output} -r {input.n} > {log.stdout} 2> {log.stderr}"
rule create_first_spades_assembly_filter_bt2_index:
input:
"data/assembly_{assemblytype}/{hostcode}/CAT_BAT_filter_{hostcode}.fasta"
params:
filter = lambda w: expand("data/assembly_{assemblytype}/{hostcode}/CAT_BAT_filter_bt2index_{hostcode}/{hostcode}_filter",assemblytype=w.assemblytype, hostcode=w.hostcode)
output:
expand("data/assembly_{{assemblytype}}/{{hostcode}}/CAT_BAT_filter_bt2index_{{hostcode}}/{{hostcode}}_filter.{i}.bt2",i=range(1,4)),
expand("data/assembly_{{assemblytype}}/{{hostcode}}/CAT_BAT_filter_bt2index_{{hostcode}}/{{hostcode}}_filter.rev.{i}.bt2",i=range(1,2))
threads: 12
log:
stdout="logs/CAT_create_assembly_filter_{assemblytype}_filterbt2index_{hostcode}.stdout",
stderr="logs/CAT_create_assembly_filter_{assemblytype}_filterbt2index_{hostcode}.stderr"
shell:
"bowtie2-build --threads {threads} {input} {params.filter} > {log.stdout} 2> {log.stderr}"
rule filter_for_assembly:
input:
expand("data/assembly_{assemblytype}/{{hostcode}}/CAT_BAT_filter_bt2index_{{hostcode}}/{{hostcode}}_filter.{i}.bt2",i=range(1,4),assemblytype='singles_hostfiltered'),
expand("data/assembly_{assemblytype}/{{hostcode}}/CAT_BAT_filter_bt2index_{{hostcode}}/{{hostcode}}_filter.rev.{i}.bt2",i=range(1,2),assemblytype='singles_hostfiltered'),
s1=expand("data/sequencing_genomic_trimmed_filtered_corrected/{{hostcode}}/corrected/{{hostcode}}.{PE}.fastq.00.0_0.cor.fastq.gz",PE=1,),
s2=expand("data/sequencing_genomic_trimmed_filtered_corrected/{{hostcode}}/corrected/{{hostcode}}.{PE}.fastq.00.0_0.cor.fastq.gz",PE=2)
params:
opts="--very-sensitive",
i = lambda w : expand("data/assembly_{assemblytype}/{hostcode}/CAT_BAT_filter_bt2index_{hostcode}/{hostcode}_filter",assemblytype='singles_hostfiltered', hostcode = w.hostcode),
outbase= lambda w : expand("data/sequencing_doublefiltered/{hostcode}/{hostcode}", hostcode=w.hostcode)
output:
b1= expand("data/sequencing_doublefiltered/{{hostcode}}/{{hostcode}}.{PE}.fastq.gz",PE=1),
b2= expand("data/sequencing_doublefiltered/{{hostcode}}/{{hostcode}}.{PE}.fastq.gz",PE=2),
sin="data/sequencing_doublefiltered/{hostcode}/{hostcode}.singletons.fastq.gz"
threads: 36
log:
stderr="logs/bowtie2_filter_for_assembly_doublefilter_{hostcode}.stderr",
samstderr="logs/bowtie2_filter_for_assembly_doublefilter_{hostcode}_samtoolsfastq.stderr",
samstdout="logs/bowtie2_filter_for_assembly_doublefilter_{hostcode}_samtoolsfastq.stdout"
shell:
"""
bowtie2 {params.opts} --threads {threads} \
-x {params.i} \
-1 {input.s1} \
-2 {input.s2} \
2> {log.stderr} \
| samtools fastq -f 13 \
-@ {threads} \
-n -c 9 \
-1 {output.b1} \
-2 {output.b2} \
-0 {output.sin} \
- \
2> {log.samstderr} \
> {log.samstdout}
"""
## double filtered assemblies
rule spades_second_assembly:
input:
reads=expand("data/sequencing_doublefiltered/{{hostcode}}/{{hostcode}}.{PE}.fastq.gz",PE=DIRECTIONS),
s1=expand("data/sequencing_doublefiltered/{{hostcode}}/{{hostcode}}.{PE}.fastq.gz",PE=1),
s2=expand("data/sequencing_doublefiltered/{{hostcode}}/{{hostcode}}.{PE}.fastq.gz",PE=2)
params:
options="--meta --only-assembler",
basedir=lambda w: expand("data/assembly_{assemblytype}/{hostcode}/",assemblytype='singles_doublefiltered',hostcode=w.hostcode)
output:
contigs=protected(expand("data/assembly_{assemblytype}/{{hostcode}}/contigs.fasta",assemblytype='singles_doublefiltered')),
scaffolds=protected(expand("data/assembly_{assemblytype}/{{hostcode}}/scaffolds.fasta",assemblytype='singles_doublefiltered')),
graph=protected(expand("data/assembly_{assemblytype}/{{hostcode}}/assembly_graph.fastg",assemblytype='singles_doublefiltered')),
graph_scaffolds=protected(expand("data/assembly_{assemblytype}/{{hostcode}}/assembly_graph_with_scaffolds.gfa",assemblytype='singles_doublefiltered')),
datasetyaml=protected(expand("data/assembly_{assemblytype}/{{hostcode}}/input_dataset.yaml",assemblytype='singles_doublefiltered')),
paramfile=protected(expand("data/assembly_{assemblytype}/{{hostcode}}/params.txt",assemblytype='singles_doublefiltered'))
threads: 100
shadow: "shallow"
resources:
mem_mb=500
log:
stdout=expand("logs/SPADES_assembly_{assemblytype}_{{hostcode}}.stdout",assemblytype='singles_doublefiltered'),
stderr=expand("logs/SPADES_assembly_{assemblytype}_{{hostcode}}.stderr",assemblytype='singles_doublefiltered')
shell:
"spades.py {params.options} -t {threads} -m {resources.mem_mb} -1 {input.s1} -2 {input.s2} -o {params.basedir} > {log.stdout} 2> {log.stderr}"
## assembly analyses and diagnostigs
rule collect_assembly_stats_singles:
input:
expand("data/assembly_{assemblytype}/{hostcode}/CAT_{hostcode}_{assemblyfile}_taxonomy.tab",assemblytype=ASSEMBLYTYPES,hostcode=HOSTCODES,assemblyfile=ASSEMBLYFILES)
params:
assemblytype= lambda w : expand("{assemblytype}",assemblytype=ASSEMBLYTYPES),
hostcode= lambda w : expand("{hostcode}",hostcode=HOSTCODES),
assemblyfile= lambda w : expand("{assemblyfile}",assemblyfile=ASSEMBLYFILES)
output:
"analyses/assembly_stats_and_taxonomy.tab"
threads: 12
resources:
mem_mb=1000
shell:
"""
scripts/make_assembly_stats_and_taxonomy.bash "{params.assemblytype}" "{params.hostcode}" "{params.assemblyfile}" {threads} {resources.mem_mb} {output}
"""
rule collect_assembly_stats_hybrid:
input:
expand("data/assembly_{assemblytype}/{hostcode}/CAT_{hostcode}_{assemblyfile}_taxonomy.tab",assemblytype='hybrid_doublefiltered',hostcode=HOSTS,assemblyfile=ASSEMBLYFILES)
params:
assemblytype= lambda w : expand("{assemblytype}",assemblytype='hybrid_doublefiltered'),
hostcode= lambda w : expand("{hostcode}",hostcode=HOSTS),
assemblyfile= lambda w : expand("{assemblyfile}",assemblyfile=ASSEMBLYFILES)
output:
"analyses/assembly-hybrid_stats_and_taxonomy.tab"
threads: 12
resources:
mem_mb=1000
shell:
"""
scripts/make_assembly_stats_and_taxonomy.bash "{params.assemblytype}" "{params.hostcode}" "{params.assemblyfile}" {threads} {resources.mem_mb} {output}
"""
## assembly processing for binning an Anvi'o
ruleorder: shorten_scaffold_names_awk > shorten_scaffold_names_anvi
rule shorten_scaffold_names_awk:
input:
scaffolds=expand("data/assembly_{assemblytype}/{{hostcode}}/{{assemblyfile}}.fasta",assemblytype='singles_hostfiltered')
output:
scaffolds=expand("data/assembly_{assemblytype}/{{hostcode}}/{{assemblyfile}}_short_names.fasta",assemblytype='singles_hostfiltered')
shell:
"""awk -F '_' '/>NODE/{{$0=">NODE_"$2}}1' {input} > {output}"""
rule CAT_filter_unclassified_and_eukaryotic_scaffolds_for_anvio:
input:
expand("data/assembly_{{assemblytype}}/{{hostcode}}/CAT_{{hostcode}}_{assemblyfile}_taxonomy.tab",assemblyfile='scaffolds')
output:
expand("data/assembly_{{assemblytype}}/{{hostcode}}/CAT_{{hostcode}}_{assemblyfile}_minus-unclassified_minus-eukaryotic_filterlist.txt",assemblyfile='scaffolds')
threads: 1
log:
expand("logs/CAT_assembly_{{assemblytype}}_{assemblyfile}filterlist_{{hostcode}}.stderr",assemblyfile='scaffolds')
shell:
"cat {input} | grep -v Eukaryota | grep -v unclassified | grep -v '#' | cut -f 1 | sort -n > {output} 2> {log}"
rule filter_unclassfied_filter_eukaryotic:
input:
scaffolds="data/assembly_{assemblytype}/{hostcode}/{assemblyfile}.fasta",
filterlist=expand("data/assembly_{{assemblytype}}/{{hostcode}}/CAT_{{hostcode}}_{assemblyfile}_minus-unclassified_minus-eukaryotic_filterlist.txt",assemblyfile='scaffolds')
output:
scaffolds="data/assembly_{assemblytype}/{hostcode}/{assemblyfile}_minus-unclassified_minus-eukaryotic.fasta"
log:
stdout="logs/filter-final-fasta-eukaryotic-unclassfied_{assemblytype}_{hostcode}_{assemblyfile}.stdout",
stderr="logs/filter-final-fasta-eukaryotic-unclassified_{assemblytype}_{hostcode}_{assemblyfile}.stderr"
shell:
"samtools faidx {input.scaffolds} -o {output.scaffolds} -r {input.filterlist} > {log.stdout} 2> {log.stderr}"
rule shorten_scaffold_names_anvi:
input:
scaffolds="data/assembly_{assemblytype}/{hostcode}/{assemblyfile}_minus-unclassified_minus-eukaryotic.fasta"
output:
scaffolds="data/assembly_{assemblytype}/{hostcode}/{assemblyfile}_short_names.fasta"
log:
report="logs/anvi-script-reformat-fasta_{assemblytype}_{hostcode}_{assemblyfile}.report",
stdout="logs/anvi-script-reformat-fasta_{assemblytype}_{hostcode}_{assemblyfile}.stdout",
stderr="logs/anvi-script-reformat-fasta_{assemblytype}_{hostcode}_{assemblyfile}.stderr"
conda:
"envs/anvio.yaml"
shell:
"anvi-script-reformat-fasta -l 2500 --simplify-names -r {log.report} {input} -o {output} > {log.stdout} 2> {log.stderr} "
rule bwa_index_assembly_scaffolds:
input:
scaffolds=expand("data/assembly_{{assemblytype}}/{{hostcode}}/{assemblyfile}_short_names.fasta",assemblyfile='scaffolds')
params:
outbase=lambda w: expand("data/assembly_{assemblytype}/{hostcode}/scaffolds_bwa_index/{assemblyfile}",assemblyfile='scaffolds',assemblytype=w.assemblytype,hostcode=w.hostcode)
output:
expand("data/assembly_{{assemblytype}}/{{hostcode}}/scaffolds_bwa_index/scaffolds.{ext}",ext=['bwt','pac','ann','sa','amb'])
threads: 1
log:
stdout="logs/bwa_index_{assemblytype}_{hostcode}.stdout",
stderr="logs/bwa_index_{assemblytype}_{hostcode}.stderr"
shell:
"bwa index -p {params.outbase} {input} > {log.stdout} 2> {log.stderr}"
import os.path
def get_binning_reads(wildcards):
index={'index': expand("data/assembly_{{assemblytype}}/{{hostcode}}/scaffolds_bwa_index/scaffolds.{ext}",ext=['bwt','pac','ann','sa','amb']) }
if wildcards.assemblytype != 'hybrid_doublefiltered' :
pathpe=("data/sequencing_binning_signals/" + wildcards.binningsignal + ".trimmed_paired.R1.fastq.gz")
pathse=("data/sequencing_binning_signals/" + wildcards.binningsignal + ".trimmed.fastq.gz")
if os.path.isfile(pathpe) == True :
dict = {'reads' : expand("data/sequencing_binning_signals/{binningsignal}.trimmed_paired.R{PE}.fastq.gz", PE=[1,2],binningsignal=wildcards.binningsignal) }
elif os.path.isfile(pathse) == True :
dict = {'reads' : expand("data/sequencing_binning_signals/{binningsignal}.trimmed.fastq.gz", binningsignal=wildcards.binningsignal) }
return dict
dict.update(index)
return dict
elif wildcards.assemblytype == 'hybrid_doublefiltered' :
if len(list(filter(lambda x:wildcards.binningsignal in x, BINNINGSIGNALS))) > 0 :
pathpe=("data/sequencing_binning_signals/" + wildcards.binningsignal + ".trimmed_paired.R1.fastq.gz")
pathse=("data/sequencing_binning_signals/" + wildcards.binningsignal + ".trimmed.fastq.gz")
index={'index': expand("data/assembly_{{assemblytype}}/{{hostcode}}/scaffolds_bwa_index/scaffolds.{ext}",ext=['bwt','pac','ann','sa','amb']) }
if os.path.isfile(pathpe) == True :
dict = {'reads' : expand("data/sequencing_binning_signals/{binningsignal}.trimmed_paired.R{PE}.fastq.gz", PE=[1,2],binningsignal=wildcards.binningsignal) }
elif os.path.isfile(pathse) == True :
dict = {'reads' : expand("data/sequencing_binning_signals/{binningsignal}.trimmed.fastq.gz", binningsignal=wildcards.binningsignal) }
return dict
dict.update(index)
elif len(list(filter(lambda x:wildcards.binningsignal in x, BINNINGSIGNALS))) == 0 :
dict = { 'reads' : expand("data/sequencing_genomic_trimmed/{hostcode}_R{PE}.fastq.gz",PE=DIRECTIONS,hostcode=wildcards.binningsignal) }
dict.update(index)
return dict
dict.update(index)
return dict
rule backmap_bwa_mem:
input:
unpack(get_binning_reads),
expand("data/assembly_{{assemblytype}}/{{hostcode}}/scaffolds_bwa_index/scaffolds.{ext}",ext=['bwt','pac','ann','sa','amb'])
params:
index=lambda w: expand("data/assembly_{assemblytype}/{hostcode}/scaffolds_bwa_index/scaffolds",assemblytype=w.assemblytype,hostcode=w.hostcode)
output:
temp("data/assembly_{assemblytype}_binningsignals/{hostcode}/{hostcode}+{binningsignal}.bam")
threads: 100
log:
stdout="logs/bwa_backmap_samtools_{assemblytype}_{hostcode}_{binningsignal}.stdout",
samstderr="logs/bwa_backmap_samtools_{assemblytype}_{hostcode}_{binningsignal}.stdout",
stderr="logs/bwa_backmap_{assemblytype}_{hostcode}_{binningsignal}.stderr"
shell:
"bwa mem -t {threads} {params.index} {input.reads} 2> {log.stderr} | samtools view -F 4 -@ {threads} -b -o {output} 2> {log.samstderr} > {log.stdout}"
# def get_source_binning_reads(wildcards):
# if wildcards.assemblytype != 'hybrid_doublefiltered':
# s1={'s1' : expand("data/sequencing_genomic_trimmed/{hostcode}_R{PE}.fastq.gz",PE=1,hostcode=wildcards.hostcode) }
# s2={'s2' : expand("data/sequencing_genomic_trimmed/{hostcode}_R{PE}.fastq.gz",PE=2,hostcode=wildcards.hostcode) }
# index={'index' : expand("data/assembly_{assemblytype}/{hostcode}/scaffolds_bwa_index/scaffolds.{ext}",ext=['bwt','pac','ann','sa','amb'],hostcode=wildcards.hostcode,assemblytype=wildcards.assemblytype)}
# input={}
# input.update(s1)
# input.update(s2)
# input.update(index)
# (input)
# elif wildcards.assemblytype == 'hybrid_doublefiltered':
# s1={'s1' : expand("data/sequencing_genomic_trimmed/{hostcode}_R{PE}.fastq.gz",PE=1,hostcode=wildcards.hostcode) }
# s2={'s2' : expand("data/sequencing_genomic_trimmed/{hostcode}_R{PE}.fastq.gz",PE=2,hostcode=wildcards.hostcode) }
# index={'index' : expand("data/assembly_{assemblytype}/{host}/scaffolds_bwa_index/scaffolds.{ext}",ext=['bwt','pac','ann','sa','amb'],host=wildcards.hostcode,assemblytype=wildcards.assemblytype)}
# input={}
# input.update(s1)
# input.update(s2)
# input.update(index)
# return(input)
# return(input)
ruleorder: BLASR_backmap_long_reads > backmap_bwa_mem_assemblysource
rule BLASR_backmap_long_reads:
input:
reads="data/sequencing_genomic-longreads_trimmed/Azfil_lab_longreads-selfcorrected_trimmed.fasta",
scaffolds=expand("data/assembly_{{assemblytype}}/{{hostcode}}/{assemblyfile}_short_names.fasta",assemblyfile='scaffolds')
output:
temp("data/assembly_{assemblytype}_binningsignals/{hostcode}/{hostcode}+pacbio_reads.bam")
threads: 100
params:
"--hitPolicy allbest --bam"
shadow: "shallow"
conda:
"envs/blasr.yaml"
log:
stdout="logs/BLASR_filter_long_reads_{assemblytype}_{hostcode}.stdout",
stderr="logs/BLASR_filter_long_reads_{assemblytype}_{hostcode}.stderr"
shell:
"blasr {input.reads} {input.scaffolds} {params} --nproc {threads} --out {output} > {log.stdout} 2> {log.stderr}"
rule backmap_bwa_mem_assemblysource:
input:
#unpack(get_source_binning_reads)
s1=expand("data/sequencing_genomic_trimmed/{{hostcode}}_R{PE}.fastq.gz",PE=1),
s2=expand("data/sequencing_genomic_trimmed/{{hostcode}}_R{PE}.fastq.gz",PE=2),
index=expand("data/assembly_{{assemblytype}}/{{hostcode}}/scaffolds_bwa_index/scaffolds.{ext}",ext=['bwt','pac','ann','sa','amb'])
params:
index=lambda w: expand("data/assembly_{assemblytype}/{hostcode}/scaffolds_bwa_index/scaffolds",assemblytype=w.assemblytype,hostcode=w.hostcode)
output:
temp("data/assembly_{assemblytype}_binningsignals/{hostcode}/{hostcode}+{hostcode}.bam")
threads: 100
log:
stdout="logs/bwa_backmap_samtools_{assemblytype}_{hostcode}.stdout",
samstderr="logs/bwa_backmap_samtools_{assemblytype}_{hostcode}.stdout",
stderr="logs/bwa_backmap_{assemblytype}_{hostcode}.stderr"
shell:
"bwa mem -t {threads} {params.index} {input.s1} {input.s2} 2> {log.stderr} | samtools view -F 4 -@ {threads} -b -o {output} 2> {log.samstderr} > {log.stdout}"
rule backmap_samtools_sort:
input:
"data/assembly_{assemblytype}_binningsignals/{hostcode}/{hostcode}+{binningsignal}.bam"
output:
"data/assembly_{assemblytype}_binningsignals/{hostcode}/{hostcode}+{binningsignal}.sorted.bam"
threads: 100
resources:
mem_mb=2000
log:
stdout="logs/bwa_backmap_samtools_sort_{assemblytype}_{hostcode}_{binningsignal}.stdout",
stderr="logs/bwa_backmap_samtools_sort_{assemblytype}_{hostcode}_{binningsignal}.stderr"
shell:
"samtools sort -@ {threads} -m {resources.mem_mb}M -o {output} {input} > {log.stdout} 2> {log.stderr}"
rule backmap_samtools_index_binningsignal:
input:
"data/assembly_{assemblytype}_binningsignals/{hostcode}/{hostcode}+{binningsignal}.sorted.bam"
output:
"data/assembly_{assemblytype}_binningsignals/{hostcode}/{hostcode}+{binningsignal}.sorted.bam.bai"
log:
stdout="logs/bwa_backmap_samtools_index_{assemblytype}_{hostcode}_{binningsignal}.stdout",
stderr="logs/bwa_backmap_samtools_index_{assemblytype}_{hostcode}_{binningsignal}.stderr"
threads: 100
shell:
"samtools index -@ {threads} {input} > {log.stdout} 2> {log.stderr}"
def get_bams_for_binning(wildcards):
if wildcards.assemblytype != 'hybrid_doublefiltered':
input=expand("data/assembly_{assemblytype}_binningsignals/{hostcode}/{hostcode}+{hostcode}.sorted.bam",assemblytype=wildcards.assemblytype,hostcode=wildcards.hostcode) + expand("data/assembly_{assemblytype}_binningsignals/{hostcode}/{hostcode}+{binningsignal}.sorted.bam",binningsignal=BINNINGSIGNALS,assemblytype=wildcards.assemblytype,hostcode=wildcards.hostcode)
return(input)
elif wildcards.assemblytype == 'hybrid_doublefiltered':
HOST_LIBRARIES=list(filter(lambda x:wildcards.hostcode in x, HOSTCODES)) + BINNINGSIGNALS
input=expand("data/assembly_{assemblytype}_binningsignals/{host}/{host}+{hostcode}.sorted.bam",assemblytype=wildcards.assemblytype,host=wildcards.hostcode,hostcode=HOST_LIBRARIES)
return(input)
rule jgi_summarize_script:
input:
get_bams_for_binning
output:
"data/assembly_{assemblytype}/{hostcode}/{hostcode}_depthmatrix.tab"
log:
stdout="logs/jgi_summarize_script_{assemblytype}_{hostcode}.stdout",
stderr="logs/jgi_summarize_script_{assemblytype}_{hostcode}.stdout"
threads: 32
shell:
"jgi_summarize_bam_contig_depths --minContigLength 2500 --percentIdentity 80 --outputDepth {output} {input} > {log.stdout} 2> {log.stderr}"
checkpoint metabat2:
input:
scaffolds="data/assembly_{assemblytype}/{hostcode}/scaffolds_short_names.fasta",
depthmatrix="data/assembly_{assemblytype}/{hostcode}/{hostcode}_depthmatrix.tab"
output:
bins=directory("data/bins_{assemblytype}/{hostcode,[A-Za-z0-9_]+}")
params:
prefix=lambda w: expand("data/bins_{assemblytype}/{hostcode}/{hostcode}_bin",assemblytype=w.assemblytype,hostcode=w.hostcode)
threads: 72
log:
stdout="logs/metabat2_{assemblytype}_{hostcode}.stdout",
stderr="logs/metabat2_{assemblytype}_{hostcode}.stdout"
shell:
"metabat2 -t {threads} -i {input.scaffolds} -a {input.depthmatrix} -o {params.prefix} > {log.stdout} 2> {log.stderr}"
rule checkm_set_data_folder:
input:
"references/checkm_data"
output:
touch("references/checkm_data_setroot.done")
conda:
"envs/checkm.yaml"
shell:
"checkm data setRoot {input}"
rule checkm:
input:
bins="data/bins_{assemblytype}/{hostcode}",
set_root="references/checkm_data_setroot.done"
output:
table="data/bins_{assemblytype}_checkm/{hostcode}/{hostcode}.checkm_out"
params:
options="-x fa --pplacer_threads=12 --tab_table",
dir=lambda w:expand("data/bins_{assemblytype}_checkm/{hostcode}",assemblytype=w.assemblytype,hostcode=w.hostcode)
threads: 72
log:
stdout="logs/checkm_{assemblytype}_{hostcode}.stdout",
stderr="logs/checkm_{assemblytype}_{hostcode}.stderr"
conda:
"envs/checkm.yaml"
shell:
"""
if [ -d {params.dir} ]
then rm -rf {params.dir}
fi
checkm lineage_wf -t {threads} {params.options} {input.bins} {params.dir} -f {output.table} > {log.stdout} 2> {log.stderr}
"""
rule prodigal_get_ORFs_for_CAT_bins:
input:
bindir="data/bins_{assemblytype}/{hostcode}"
output:
a="data/bins_{assemblytype}/{hostcode}.BAT.concatenated.predicted_proteins.faa",
o="data/bins_{assemblytype}/{hostcode}.BAT.concatenated.predicted_proteins.gff"
params:
"-p meta -g 11 -q -f gff"
shell:
"prodigal -i <(cat {input}/*.fa ) -a {output.a} -o {output.o} {params}"
rule CAT_bins:
input:
# a="data/bins_{assemblytype}/{hostcode}.BAT.concatenated.predicted_proteins.faa",
# o="data/bins_{assemblytype}/{hostcode}.BAT.concatenated.predicted_proteins.gff",
bindir="data/bins_{assemblytype}/{hostcode}",
dmnd="references/CAT_customised_20190108/CAT_database_customised/2019-03-27.nr.dmnd",
db="references/CAT_customised_20190108/CAT_database_customised",
tf="references/CAT_customised_20190108/taxonomy_customised"
output:
"data/bins_{assemblytype}/{hostcode}.BAT.bin2classification.txt"
# shadow: 'shallow'
params:
options= " -s '.fa' ",
prefix=lambda w : expand( "data/bins_{assemblytype}/{hostcode}.BAT" , assemblytype=w.assemblytype , hostcode=w.hostcode )
threads: 72
log:
stdout="logs/BAT_{assemblytype}_{hostcode}.stdout",
stderr="logs/BAT_{assemblytype}_{hostcode}.stderr"
shell:
"CAT bins -n {threads} -b {input.bindir} -d {input.db} -t {input.tf} {params.options} -o {params.prefix} > {log.stdout} 2> {log.stderr}"
rule BAT_add_names:
input:
i="data/bins_{assemblytype}/{hostcode}.BAT.bin2classification.txt",
tf="references/CAT_customised_20190108/taxonomy_customised"
output:
"data/bins_{assemblytype}/{hostcode}.BAT.names.txt",
params:
"--only_official"
log:
stdout="logs/BAT_assembly_{assemblytype}_classification_taxonomy_{hostcode}.stdout",
stderr="logs/BAT_assembly_{assemblytype}_classification_taxonomy_{hostcode}.stderr"
threads: 1
shell:
"CAT add_names {params} -i {input.i} -t {input.tf} -o {output} > {log.stdout} 2> {log.stderr}"
rule anvi_gen_contigs_database:
input:
scaffolds="data/assembly_{assemblytype}/{hostcode}/scaffolds_short_names.fasta"
output:
db="data/assembly_{assemblytype}_anvio/{hostcode}/{hostcode}_contigs.db"
log:
stdout="logs/anvi-gen-contigs-database_{assemblytype}_{hostcode}.stdout",
stderr="logs/anvi-gen-contigs-database_{assemblytype}_{hostcode}.stderr"
params:
"-n 'sample {hostcode} assembly {assemblytype}'"
conda:
"envs/anvio.yaml"
shell:
"""
anvi-gen-contigs-database -f {input} -o {output} {params} > {log.stdout} 2> {log.stderr}
"""
rule anvi_run_hmms:
input:
db="data/assembly_{assemblytype}_anvio/{hostcode}/{hostcode}_contigs.db"
output:
touch("data/assembly_{assemblytype}_anvio/{hostcode}/{hostcode}_contigs_db_run_hmms.done")
threads: 5
log:
stdout="logs/anvi-run-hmms_{assemblytype}_{hostcode}.stdout",
stderr="logs/anvi-run-hmms_{assemblytype}_{hostcode}.stderr"
conda:
"envs/anvio.yaml"
shell:
"anvi-run-hmms -c {input} -T {threads} > {log.stdout} 2> {log.stderr}"
rule anvi_setup_ncbi_cogs:
output:
dir=directory("references/anvi_ncbi_cogs")
threads: 100
log:
stdout="logs/anvi-setup-cogs.stdout",
stderr="logs/anvi-setup-cogs.stderr"
conda:
"envs/anvio.yaml"
shell:
"anvi-setup-ncbi-cogs -T {threads} --just-do-it --cog-data-dir {output.dir} > {log.stdout} 2> {log.stderr}"
rule anvi_run_ncbi_cogs:
input:
db="data/assembly_{assemblytype}_anvio/{hostcode}/{hostcode}_contigs.db",
dir="references/anvi_ncbi_cogs"
output:
touch("data/assembly_{assemblytype}_anvio/{hostcode}/{hostcode}_contigs_db_run_ncbi_cogs.done")
threads: 20
params: "--sensitive"
log:
stdout="logs/anvi-run-cogs_{assemblytype}_{hostcode}.stdout",
stderr="logs/anvi-run-cogs_{assemblytype}_{hostcode}.stderr"
conda:
"envs/anvio.yaml"
shell:
"anvi-run-ncbi-cogs {params} -c {input.db} -T {threads} --cog-data-dir {input.dir} > {log.stdout} 2> {log.stderr}"
rule prepare_anvi_import_cat_taxonomy:
input:
report="logs/anvi-script-reformat-fasta_{assemblytype}_{hostcode}_{assemblyfile}.report",
taxonomy="data/assembly_{assemblytype}/{hostcode}/CAT_{hostcode}_{assemblyfile}_taxonomy.tab",
profile=ancient("data/assembly_{assemblytype}_binningsignals_anvio/MERGED_{hostcode}/PROFILE.db")
output:
"data/assembly_{assemblytype}/{hostcode}/CAT_{hostcode}_{assemblyfile}_taxonomy_shortnames.tab"
threads: 2
conda:
"envs/anvio.yaml"
log:
"logs/prepare-anvi-import-cat-taxonomy_{assemblytype}_{hostcode}_{assemblyfile}.stderr"
shell:
"""
echo "item_name\tcategorical_kingdom\tcategorical_phylum\tcategorical_class\tcategorical_order\tcategorical_family\tcategorical_genus\tcategorical_species" \
> {output} 2> {log}
join -1 2 -2 1 \
<( sort -k2d {input.report} ) \
<(tail -n +2 {input.taxonomy} \
| cut -f 1,7- \
| sed 's/: [01]\.[0-9][0-9]//g' \
| tr ' ' '_' \
| sort -k1d )\
| tr ' ' '\t' \
| cut -f 2- \
| sort -n \
>> {output}.tmp 2>> {log}
join -1 1 -2 1 --check-order -a 1 \
<(anvi-get-split-coverages --list-splits -p {input.profile} \
2>> {log} \
| sed 's/_split/\t_split/g' | sort -k1d) \
<( sort -k1d {output}.tmp ) \
| tr ' ' '\t' \
| sort -k1n,2n \
| sed 's/\t_split/_split/g' \
>> {output} 2>> {log}
rm {output}.tmp 2>> {log}
"""