-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrMAP.wdl
More file actions
4715 lines (4236 loc) · 167 KB
/
rMAP.wdl
File metadata and controls
4715 lines (4236 loc) · 167 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
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
version 1.0
workflow rMAP {
input {
Array[File] input_reads
File adapters
File reference_genome
# Trimmomatic quality score encoding (default: phred33; set to "phred64" if needed)
String trimmomatic_quality_encoding = "phred33"
Boolean do_trimming = true
Boolean do_quality_control = true
Boolean do_assembly = true
Boolean do_variant_calling = true
Boolean do_annotation = true
Boolean do_amr_profiling = true
Boolean do_mlst = true
Boolean do_pangenome = true
Boolean do_phylogeny = true
Boolean do_mge_analysis = true
Boolean do_reporting = true
Boolean do_blast = true
Boolean use_local_blast = true
File? local_blast_db
File? local_amr_db
File? local_mge_db
File? local_virulence_db
String blast_db = "nt"
Int blast_max_target_seqs = 250
Float blast_evalue = 0.000001
Int blast_min_contig_length = 300
String virulence_db = "vfdb"
Int virulence_min_cov = 60
Float virulence_min_id = 80.0
String phylogeny_model = "-nt -gtr"
String reference_type = "genbank"
Int max_cpus = 8
Int max_memory_gb = 16
Int min_assembly_quality = 50
Int min_read_length = 50
Int min_mapping_quality = 20
Int tree_image_width = 1200
String tree_image_format = "png"
Int tree_font_size = 8
File? logo_file
Boolean do_virulence = true
# Derived values with adjusted resources
Int cpu_4 = if (max_cpus < 4) then max_cpus else 4
Int cpu_8 = if (max_cpus < 8) then max_cpus else 8
Int cpu_16 = max_cpus
Int cpu_2 = if (max_cpus < 2) then max_cpus else 2
Int mem_8 = if (max_memory_gb < 8) then max_memory_gb else 8
Int mem_12 = if (max_memory_gb < 12) then max_memory_gb else 12
Int mem_16 = max_memory_gb
}
meta {
workflow_timeout: "168 hours"
workflow_heartbeat_interval: "10 minutes"
workflow_heartbeat_ttl: "30 minutes"
allowNestedInputs: true
maxRetries: 3
continueOnReturnCode: "0,1"
author: "Gerald Mboowa, Ivan Sserwadda & Stephen Kanyerezi"
email: "gmboowa@gmail.com | ivangunz23@gmail.com | kanyerezi30@gmail.com"
}
call CONFIGURATION {
input:
max_cpus = max_cpus,
max_memory_gb = max_memory_gb
}
call TRIMMING {
input:
input_reads = input_reads,
adapters = adapters,
trimmomatic_quality_encoding = trimmomatic_quality_encoding,
do_trimming = do_trimming,
cpu = cpu_4,
min_length = min_read_length
}
call QUALITY_CONTROL {
input:
input_reads = if do_trimming then TRIMMING.trimmed_reads else input_reads,
do_quality_control = do_quality_control,
cpu = cpu_4
}
call ASSEMBLY {
input:
input_reads = trimmed_reads,
do_assembly = do_assembly,
cpu = cpu_8,
memory_gb = mem_16,
min_contig_len = min_assembly_quality
}
call ANNOTATION {
input:
assembly_output = ASSEMBLY.assembly_output,
do_annotation = do_annotation,
cpu = cpu_8,
memory_gb = mem_16
}
call MLST {
input:
assembly_output = ASSEMBLY.assembly_output,
do_mlst = do_mlst,
cpu = cpu_2
}
call VARIANT_CALLING {
input:
input_reads = if do_trimming then TRIMMING.trimmed_reads else input_reads,
reference_genome = reference_genome,
do_variant_calling = do_variant_calling,
reference_type = reference_type,
cpu = cpu_8,
memory_gb = mem_12,
min_quality = min_mapping_quality
}
call PANGENOME {
input:
annotation_input = ANNOTATION.annotation_output,
do_pangenome = do_pangenome,
cpu = cpu_8,
memory_gb = mem_16,
output_prefix = "rMAP_pangenome"
}
call AMR_PROFILING {
input:
assembly_output = ASSEMBLY.assembly_output,
do_amr_profiling = do_amr_profiling,
local_db = local_amr_db,
use_local_db = use_local_blast,
cpu = cpu_2
}
# >>> REPLACE subworkflow call with a direct scatter over the MGE task <<<
scatter (asm in ASSEMBLY.assembly_output) {
call MGE {
input:
assembly = asm,
do_mge_analysis = do_mge_analysis,
local_db = local_mge_db,
use_local_db = use_local_blast,
cpu = cpu_4
}
}
if (do_virulence) {
call VIRULENCE_ANALYSIS {
input:
assembly_output = ASSEMBLY.assembly_output,
db_name = virulence_db,
local_db = local_virulence_db,
use_local_db = use_local_blast,
min_coverage = virulence_min_cov,
min_identity = virulence_min_id,
cpu = cpu_2
}
}
call BLAST_ANALYSIS {
input:
contig_fastas = ASSEMBLY.assembly_output,
blast_db = blast_db,
local_blast_db = local_blast_db,
use_local_blast = use_local_blast,
max_target_seqs = blast_max_target_seqs,
evalue = blast_evalue,
min_contig_length = blast_min_contig_length,
do_blast = do_blast,
cpu = cpu_8,
memory_gb = mem_12
}
if (do_phylogeny) {
call CORE_PHYLOGENY {
input:
alignment = PANGENOME.core_alignment,
do_phylogeny = true,
model = phylogeny_model,
cpu = cpu_8,
memory_gb = mem_16,
tree_prefix = "core_genes",
bootstrap_replicates = 100
}
call ACCESSORY_PHYLOGENY {
input:
alignment = PANGENOME.accessory_binary,
do_phylogeny = true,
model = phylogeny_model,
cpu = cpu_8,
memory_gb = mem_16,
tree_prefix = "accessory_genes",
bootstrap_replicates = 100
}
if (size(CORE_PHYLOGENY.phylogeny_tree, "B") > 10.0) {
call TREE_VISUALIZATION as CORE_TREE {
input:
input_tree = CORE_PHYLOGENY.phylogeny_tree,
width = tree_image_width,
image_format = tree_image_format,
font_size = tree_font_size,
tree_title = "Core Genes Phylogenetic Tree"
}
}
if (defined(ACCESSORY_PHYLOGENY.phylogeny_tree) && size(ACCESSORY_PHYLOGENY.phylogeny_tree, "B") > 10.0) {
call TREE_VISUALIZATION as ACCESSORY_TREE {
input:
input_tree = ACCESSORY_PHYLOGENY.phylogeny_tree,
width = tree_image_width,
image_format = tree_image_format,
font_size = tree_font_size,
tree_title = "Accessory Genes Phylogenetic Tree"
}
}
}
Array[File] empty_files = []
if (do_reporting) {
call MERGE_REPORTS {
input:
# Core sections
quality_reports = QUALITY_CONTROL.quality_reports,
trimming_report_html = TRIMMING.trimming_report,
assembly_stats_html = ASSEMBLY.assembly_stats,
annotation_summary_html = ANNOTATION.annotation_summary,
pangenome_report_html = PANGENOME.pangenome_report,
gene_heatmap_png = PANGENOME.gene_heatmap,
pangenome_report = PANGENOME.pangenome_report,
gene_heatmap = PANGENOME.gene_heatmap,
mlst_combined_html = MLST.combined_html,
variant_summary_html = VARIANT_CALLING.variant_summary,
# Per-sample sections
amr_reports = AMR_PROFILING.html_reports,
mge_reports = MGE.html_out,
virulence_reports = if do_virulence then VIRULENCE_ANALYSIS.html_reports else empty_files,
# BLAST per-sample HTMLs
blast_reports = BLAST_ANALYSIS.blast_reports,
# Trees (may be empty depending on upstream conditionals)
tree_images = select_all([CORE_TREE.final_image, ACCESSORY_TREE.final_image]),
# Meta
workflow_name = "rMAP Analysis Pipeline",
version = "2.0",
footer_sentence = "This report was generated by rMAP 2.0 pipeline",
logo_file = logo_file,
skip = false
}
}
output {
Array[File] quality_reports = QUALITY_CONTROL.quality_reports
Array[File] trimmed_reads = TRIMMING.trimmed_reads
Array[File] assembly_output = ASSEMBLY.assembly_output
String assembly_dir = ASSEMBLY.assembly_dir_out
Array[File] annotation_output = ANNOTATION.annotation_output
Array[String] annotation_dirs = ANNOTATION.annotation_dirs
Array[File] mlst_output = MLST.mlst_outputs
File combined_mlst = MLST.combined_mlst
Array[File] variant_output = VARIANT_CALLING.variant_output
Array[File] variant_dirs = VARIANT_CALLING.variant_dirs
String variants_dir = VARIANT_CALLING.variants_dir
File gene_presence_absence = PANGENOME.gene_presence_absence
File core_alignment = PANGENOME.core_alignment
File accessory_alignment = PANGENOME.accessory_binary
File? core_phylogeny_output = CORE_PHYLOGENY.phylogeny_tree
File? accessory_phylogeny_output = ACCESSORY_PHYLOGENY.phylogeny_tree
File? core_tree_image = CORE_TREE.final_image
File? accessory_tree_image = ACCESSORY_TREE.final_image
File? core_tree_render_log = CORE_TREE.render_log
File? accessory_tree_render_log = ACCESSORY_TREE.render_log
Array[File] amr_output = AMR_PROFILING.amr_outputs
File combined_amr = AMR_PROFILING.combined_amr
Array[File] plasmid_results = MGE.tsv_out
Array[File] plasmid_html_reports = MGE.html_out
Array[File] blast_results = BLAST_ANALYSIS.blast_results
Array[File] blast_top10 = BLAST_ANALYSIS.blast_top10
Array[File] blast_logs = BLAST_ANALYSIS.blast_logs
Array[File]? virulence_reports = VIRULENCE_ANALYSIS.virulence_reports
File? final_report_html = MERGE_REPORTS.final_report_html
File? final_report_tgz = MERGE_REPORTS.final_report_tgz
}
}
task CONFIGURATION {
input {
Int max_cpus = 16
Int max_memory_gb = 16
}
command <<<
echo "System Configuration:" > config.log
echo "Max CPUs: ~{max_cpus}" >> config.log
echo "Max Memory: ~{max_memory_gb} GB" >> config.log
echo "Workflow version: 2.0" >> config.log
echo "Start time: $(date)" >> config.log
echo "Hostname: $(hostname)" >> config.log
echo "Kernel: $(uname -a)" >> config.log
cat config.log
>>>
runtime {
docker: "ubuntu:20.04"
memory: "1 GB"
cpu: 1
disks: "local-disk 10 HDD"
preemptible: 2
}
output {
String config_output = read_string("config.log")
}
}
task TRIMMING {
input {
Array[File]+ input_reads
File adapters
# Trimmomatic quality score encoding: "phred33" (default) or "phred64"
String trimmomatic_quality_encoding = "phred33"
Boolean do_trimming
Int cpu = 4
Int min_length = 50
Boolean skip = false
}
command <<<
set -euo pipefail
# Function to calculate read statistics
calculate_read_stats() {
local file=$1
local stats_file=$2
local read_end=$3
if [ ! -s "$file" ]; then
echo "Metric,${read_end}" >> "$stats_file"
echo "Number of reads,0" >> "$stats_file"
echo "Average length,0" >> "$stats_file"
echo "GC content,0" >> "$stats_file"
return
fi
local num_reads=$(gzip -cd -- "$file" | awk 'END {print NR/4}')
local avg_length=$(gzip -cd -- "$file" | awk 'NR%4==2 {sum+=length($0)} END {print sum/(NR/4)}')
local gc_content=$(gzip -cd -- "$file" | awk 'NR%4==2 {gc+=gsub(/[GC]/, ""); at+=gsub(/[AT]/, "")} END {print (gc/(gc+at))*100}')
echo "Metric,${read_end}" >> "$stats_file"
echo "Number of reads,$num_reads" >> "$stats_file"
echo "Average length,$avg_length" >> "$stats_file"
echo "GC content,$gc_content" >> "$stats_file"
}
if [ "~{skip}" == "true" ]; then
echo "Skipping trimming process as requested" > trimming_skipped.log
echo "Creating empty output files for portability" >> trimming_skipped.log
mkdir -p trimmed
mkdir -p read_stats
counter=0
R1=""
for file in ~{sep=' ' input_reads}; do
if [ $((counter % 2)) -eq 0 ]; then
R1="$file"
else
R2="$file"
R1_base=$(basename "$R1")
sample_name="${R1_base%_1.fastq.gz}"
sample_name="${sample_name%_2.fastq.gz}"
sample_name="${sample_name%_R1.fastq.gz}"
sample_name="${sample_name%_R2.fastq.gz}"
touch "trimmed/${sample_name}_1.trim.fastq.gz"
touch "trimmed/${sample_name}_2.trim.fastq.gz"
touch "read_stats/${sample_name}_stats.csv"
echo "Created empty output files for sample: $sample_name" >> trimming_skipped.log
fi
counter=$((counter + 1))
done
echo "Sample,Input_Pairs,Both_Surviving,Forward_Only,Reverse_Only,Dropped" > trimming_stats.csv
echo "0,0,0,0,0" >> trimming_stats.csv
cat > trimming_report.html <<EOF
<!DOCTYPE html>
<html>
<head>
<title>Trimming Statistics Report - Skipped</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
.skip-notice {
background-color: #fff3cd;
padding: 20px;
border-radius: 5px;
margin: 20px 0;
text-align: center;
}
</style>
</head>
<body>
<div class="skip-notice">
<h2>Trimming Process Skipped</h2>
<p>Read trimming was skipped as requested in the workflow parameters.</p>
<p>Generated: $(date)</p>
</div>
</body>
</html>
EOF
echo "Skipping completed at $(date)" >> trimming_skipped.log
exit 0
fi
echo "Starting trimming process at $(date)" > trimming.log
echo "Input files received:" >> trimming.log
for f in ~{sep=' ' input_reads}; do
if [ ! -f "$f" ]; then
echo "ERROR: Input file not found: $f" >> trimming.log
exit 1
fi
echo "- $f ($(wc -c < "$f") bytes)" >> trimming.log
done
if [ "~{do_trimming}" == "true" ]; then
mkdir -p trimmed
mkdir -p read_stats
echo "Trimming parameters:" >> trimming.log
echo "- CPU: ~{cpu}" >> trimming.log
echo "- Min length: ~{min_length}" >> trimming.log
echo "Sample,Input_Pairs,Both_Surviving,Forward_Only,Reverse_Only,Dropped" > trimming_stats.csv
counter=0
R1=""
for file in ~{sep=' ' input_reads}; do
if [ $((counter % 2)) -eq 0 ]; then
R1="$file"
else
R2="$file"
R1_base=$(basename "$R1")
sample_name="${R1_base%_1.fastq.gz}"
sample_name="${sample_name%_2.fastq.gz}"
sample_name="${sample_name%_R1.fastq.gz}"
sample_name="${sample_name%_R2.fastq.gz}"
echo "Processing sample: $sample_name (Files: $R1_base and $(basename "$R2"))" >> trimming.log
sample_log="trimmed/${sample_name}.log"
touch "$sample_log"
(
# Select Trimmomatic quality encoding flag (phred33/phred64/none)
PHRED_FLAG=""
case "~{trimmomatic_quality_encoding}" in
phred33|PHRED33) PHRED_FLAG="-phred33" ;;
phred64|PHRED64) PHRED_FLAG="-phred64" ;;
none|NONE|"") PHRED_FLAG="" ;;
*) echo "WARNING: Unknown trimmomatic_quality_encoding=~{trimmomatic_quality_encoding}; running without explicit flag (default phred33)" >> trimming.log ;;
esac
# Ensure clean sample naming
R1_base=$(basename "$R1")
sample_name="${R1_base%_1.fastq.gz}"
sample_name="${sample_name%_2.fastq.gz}"
sample_name="${sample_name%_R1.fastq.gz}"
sample_name="${sample_name%_R2.fastq.gz}"
# Run Trimmomatic
trimmomatic PE ${PHRED_FLAG} -threads ~{cpu} \
"$R1" "$R2" \
"trimmed/${sample_name}_1.trim.fastq.gz" "trimmed/${sample_name}_1.unpair.fastq.gz" \
"trimmed/${sample_name}_2.trim.fastq.gz" "trimmed/${sample_name}_2.unpair.fastq.gz" \
ILLUMINACLIP:~{adapters}:2:30:10:8:true \
LEADING:20 \
TRAILING:20 \
SLIDINGWINDOW:4:20 \
MINLEN:~{min_length} > "$sample_log" 2>&1 || {
echo "WARNING: Trimmomatic failed for sample $sample_name" >> trimming.log
touch "trimmed/${sample_name}_1.trim.fastq.gz" \
"trimmed/${sample_name}_2.trim.fastq.gz" \
"trimmed/${sample_name}_1.unpair.fastq.gz" \
"trimmed/${sample_name}_2.unpair.fastq.gz"
}
)
stats_line=$(grep "Input Read Pairs" "$sample_log" || true)
if [[ -n "${stats_line:-}" ]]; then
input_pairs=$(awk '{print $4}' <<< "$stats_line")
both=$(awk '{print $7}' <<< "$stats_line")
forward=$(awk '{print $12}' <<< "$stats_line")
reverse=$(awk '{print $17}' <<< "$stats_line")
dropped=$(awk '{print $20}' <<< "$stats_line")
echo "$sample_name,$input_pairs,$both,$forward,$reverse,$dropped" >> trimming_stats.csv
else
echo "$sample_name,0,0,0,0,0" >> trimming_stats.csv
echo "WARNING: Could not extract statistics for sample $sample_name" >> trimming.log
fi
> "read_stats/${sample_name}_stats.csv"
if [ -s "trimmed/${sample_name}_1.trim.fastq.gz" ]; then
calculate_read_stats "trimmed/${sample_name}_1.trim.fastq.gz" \
"read_stats/${sample_name}_stats.csv" "R1"
fi
if [ -s "trimmed/${sample_name}_2.trim.fastq.gz" ]; then
calculate_read_stats "trimmed/${sample_name}_2.trim.fastq.gz" \
"read_stats/${sample_name}_stats.csv" "R2"
fi
cat "$sample_log" >> trimming.log
rm "$sample_log"
for f in "trimmed/${sample_name}_1.trim.fastq.gz" "trimmed/${sample_name}_2.trim.fastq.gz"; do
if [ ! -f "$f" ]; then
echo "Creating empty file for failed sample $sample_name" >> trimming.log
touch "$f"
fi
echo "Output file: $f ($(wc -c < "$f") bytes)" >> trimming.log
done
fi
counter=$((counter + 1))
done
if [ $((counter % 2)) -ne 0 ]; then
echo "ERROR: Odd number of input files - last file had no pair" >> trimming.log
exit 1
fi
cat > trimming_report.html <<EOF
<!DOCTYPE html>
<html>
<head>
<title>Trimming Statistics Report</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
h1 { color: #2c3e50; border-bottom: 2px solid #3498db; padding-bottom: 10px; }
table { border-collapse: collapse; width: 100%; margin-top: 20px; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: center; }
th { background-color: #3498db; color: white; }
tr:nth-child(even) { background-color: #f2f2f2; }
.summary { background-color: #f8f9fa; padding: 15px; border-radius: 5px; margin-bottom: 20px; }
.good { color: #27ae60; }
.warning { color: #f39c12; }
.bad { color: #e74c3c; }
.stats-table { margin-top: 30px; }
</style>
</head>
<body>
<h1>Trimming Statistics Report</h1>
<div class="summary">
<p><strong>Date:</strong> $(date)</p>
<p><strong>Parameters:</strong></p>
<ul>
<li>Minimum length: ~{min_length} bp</li>
<li>Threads: ~{cpu}</li>
</ul>
</div>
<h2>Trimming Statistics</h2>
<table>
<thead>
<tr>
<th>Sample</th>
<th>Input Pairs</th>
<th>Both Surviving</th>
<th>Forward Only</th>
<th>Reverse Only</th>
<th>Dropped (%)</th>
<th>Survival Rate (%)</th>
</tr>
</thead>
<tbody>
EOF
while IFS=',' read -r sample input both forward reverse dropped; do
if [ "$sample" == "Sample" ]; then
continue
fi
if [ "$input" -gt 0 ]; then
total_surviving=$((both + forward + reverse))
survival_rate=$(awk -v ts="$total_surviving" -v i="$input" 'BEGIN {printf "%.2f", ts*100/i}')
dropped_rate=$(awk -v d="$dropped" -v i="$input" 'BEGIN {printf "%.2f", d*100/i}')
else
survival_rate="0.00"
dropped_rate="0.00"
fi
if (( $(awk -v sr="$survival_rate" 'BEGIN {print (sr >= 90)}') )); then
class="good"
elif (( $(awk -v sr="$survival_rate" 'BEGIN {print (sr >= 70)}') )); then
class="warning"
else
class="bad"
fi
echo " <tr>" >> trimming_report.html
echo " <td>$sample</td>" >> trimming_report.html
echo " <td>$input</td>" >> trimming_report.html
echo " <td>$both</td>" >> trimming_report.html
echo " <td>$forward</td>" >> trimming_report.html
echo " <td>$reverse</td>" >> trimming_report.html
echo " <td>${dropped_rate}%</td>" >> trimming_report.html
echo " <td class=\"$class\">${survival_rate}%</td>" >> trimming_report.html
echo " </tr>" >> trimming_report.html
done < trimming_stats.csv
cat >> trimming_report.html <<EOF
</tbody>
</table>
</body>
</html>
EOF
echo "Trimming completed successfully at $(date)" >> trimming.log
echo "Output files created:" >> trimming.log
ls -lh trimmed/* >> trimming.log
ls -lh read_stats/* >> trimming.log
echo "Trimming executed (not skipped) on $(date)" > trimming_skipped.log
else
echo "Trimming skipped by user request" > trimming_skipped.txt
echo "Trimming skipped on $(date)" > trimming_skipped.log
fi
>>>
runtime {
docker: "staphb/trimmomatic:0.39"
memory: "8 GB"
cpu: cpu
disks: "local-disk 100 HDD"
continueOnReturnCode: [0, -1]
preemptible: 2
timeout: "8 hours"
}
output {
Array[File] trimmed_reads = if do_trimming then glob("trimmed/*_[12].trim.fastq.gz") else []
Array[File]? read_stats = if do_trimming then glob("read_stats/*_stats.csv") else []
File? trimming_log = if do_trimming then "trimming.log" else "trimming_skipped.txt"
File? trimming_stats = if do_trimming then "trimming_stats.csv" else "trimming_skipped.txt"
File? trimming_report = if do_trimming then "trimming_report.html" else "trimming_skipped.txt"
File? skip_log = "trimming_skipped.log"
}
}
task QUALITY_CONTROL {
input {
Array[File]+ input_reads
Boolean do_quality_control
Int cpu = 4
Boolean run_multiqc = true
Boolean skip = false
}
command <<<
set -euo pipefail
if [ "~{skip}" == "true" ]; then
echo "Skipping quality control as requested" > qc_skip.log
echo "Creating minimal output files for portability" >> qc_skip.log
mkdir -p qc_reports
# Create empty output files
touch qc_reports/summary.html
touch qc_reports/skipped.txt
# Create minimal HTML report
cat > qc_reports/summary.html <<EOF
<!DOCTYPE html>
<html>
<head>
<title>Quality Control Report - Skipped</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
.skip-notice {
background-color: #fff3cd;
padding: 20px;
border-radius: 5px;
margin: 20px 0;
text-align: center;
}
</style>
</head>
<body>
<div class="skip-notice">
<h2>Quality Control Skipped</h2>
<p>Quality control analysis was skipped as requested in the workflow parameters.</p>
<p>Generated: $(date)</p>
</div>
</body>
</html>
EOF
echo "Skipping completed at $(date)" >> qc_skip.log
exit 0
fi
# Debugging: Verify input files
echo "Starting quality control at $(date)" > qc.log
echo "Input files verification:" >> qc.log
for f in ~{sep=' ' input_reads}; do
if [ ! -f "$f" ]; then
echo "ERROR: Input file not found: $f" >> qc.log
exit 1
fi
echo "- $f ($(wc -c < "$f") bytes)" >> qc.log
done
if [ "~{do_quality_control}" == "true" ]; then
mkdir -p qc_reports
echo "Quality control parameters:" >> qc.log
echo "- CPU: ~{cpu}" >> qc.log
echo "- Run MultiQC: ~{run_multiqc}" >> qc.log
echo "Running FastQC..." >> qc.log
# Run FastQC on all files, continue even if some fail
set +e
fastqc -o qc_reports -t ~{cpu} ~{sep=' ' input_reads} 2>> qc.log
set -e
# Generate individual report links file with consistent naming
cat > qc_reports/individual_reports.html <<EOF
<!DOCTYPE html>
<html>
<head>
<title>Individual QC Reports</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
table { border-collapse: collapse; width: 100%; margin-top: 20px; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
th { background-color: #3498db; color: white; }
tr:nth-child(even) { background-color: #f2f2f2; }
</style>
</head>
<body>
<h2>Individual QC Reports</h2>
<table>
<thead>
<tr>
<th>Sample</th>
<th>Report</th>
<th>Raw Data</th>
</tr>
</thead>
<tbody>
EOF
# Add entries for each FastQC report with consistent naming
idx=1
for f in qc_reports/*_fastqc.html; do
sample=$(basename "$f" | sed 's/_fastqc\.html//')
report_name="QC_Report_${idx}_${sample}.html"
cp "$f" "qc_reports/${report_name}"
echo " <tr>" >> qc_reports/individual_reports.html
echo " <td>${sample}</td>" >> qc_reports/individual_reports.html
echo " <td><a href=\"${report_name}\">QC Report ${idx} — ${sample}</a></td>" >> qc_reports/individual_reports.html
zip_file="${f%.html}.zip"
new_zip_name="QC_Data_${idx}_${sample}.zip"
cp "$zip_file" "qc_reports/${new_zip_name}"
echo " <td><a href=\"${new_zip_name}\">Download Data</a></td>" >> qc_reports/individual_reports.html
echo " </tr>" >> qc_reports/individual_reports.html
idx=$((idx+1))
done
cat >> qc_reports/individual_reports.html <<EOF
</tbody>
</table>
</body>
</html>
EOF
if [ "~{run_multiqc}" == "true" ]; then
echo "Running MultiQC..." >> qc.log
# Create a simple MultiQC report alternative if MultiQC isn't available
if ! command -v multiqc &> /dev/null; then
echo "WARNING: MultiQC not found, generating simplified report" >> qc.log
# Generate a simple HTML summary of FastQC results
cat > qc_reports/fastqc_summary.html <<EOF
<!DOCTYPE html>
<html>
<head>
<title>QC Summary Report</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
h1 { color: #2c3e50; border-bottom: 2px solid #3498db; padding-bottom: 10px; }
table { border-collapse: collapse; width: 100%; margin-top: 20px; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
th { background-color: #3498db; color: white; }
tr:nth-child(even) { background-color: #f2f2f2; }
</style>
</head>
<body>
<h1>QC Summary Report</h1>
<p>Generated at $(date)</p>
<p><a href="individual_reports.html">View Individual Reports</a></p>
EOF
# Try to extract some basic metrics from FastQC data
idx=1
for f in qc_reports/*_fastqc/fastqc_data.txt; do
if [ -f "$f" ]; then
sample=$(basename "$f" | sed 's/_fastqc\.fastqc_data\.txt//')
basic_stats=$(awk '/>>Basic Statistics/,/>>END_MODULE/' "$f" | grep -v '>>')
echo "<h3>QC Report ${idx} — ${sample}</h3>" >> qc_reports/fastqc_summary.html
echo "<table>" >> qc_reports/fastqc_summary.html
echo "$basic_stats" | while read -r line; do
if [[ "$line" != "" ]]; then
key=$(echo "$line" | cut -f1)
value=$(echo "$line" | cut -f2)
echo "<tr><td>$key</td><td>$value</td></tr>" >> qc_reports/fastqc_summary.html
fi
done
echo "</table>" >> qc_reports/fastqc_summary.html
idx=$((idx+1))
fi
done
cat >> qc_reports/fastqc_summary.html <<EOF
<p>Note: Full MultiQC report not available. Install MultiQC for more comprehensive analysis.</p>
</body>
</html>
EOF
else
# Run MultiQC if available
multiqc qc_reports -o qc_reports --force 2>> qc.log || {
echo "ERROR: MultiQC failed" >> qc.log
# Create fallback summary
cp qc_reports/individual_reports.html qc_reports/fastqc_summary.html
}
fi
fi
# Verify reports
if [ $(ls qc_reports/*.{html,zip} 2>/dev/null | wc -l) -eq 0 ]; then
echo "WARNING: No QC reports generated, creating empty reports" >> qc.log
touch qc_reports/empty_report.html
fi
# Create a canonical QC summary file we can output consistently
if [ -f qc_reports/multiqc_report.html ]; then
cp qc_reports/multiqc_report.html qc_reports/summary.html
elif [ -f qc_reports/fastqc_summary.html ]; then
cp qc_reports/fastqc_summary.html qc_reports/summary.html
elif [ -f qc_reports/individual_reports.html ]; then
cp qc_reports/individual_reports.html qc_reports/summary.html
elif [ -f qc_reports/empty_report.html ]; then
cp qc_reports/empty_report.html qc_reports/summary.html
else
touch qc_reports/summary.html
fi
echo "Quality control completed at $(date)" >> qc.log
echo "Output files created:" >> qc.log
ls -lh qc_reports/* >> qc.log
# Ensure skip marker exists even when not skipping, so outputs never break
echo "QC executed (not skipped) on $(date)" > qc_skip.log
else
mkdir -p qc_reports
echo "QC skipped by user request" > qc_reports/skipped.txt
# Also ensure skip marker exists for outputs
echo "QC skipped on $(date)" > qc_skip.log
fi
>>>
runtime {
docker: "staphb/fastqc:0.11.9"
memory: "4 GB"
cpu: cpu
disks: "local-disk 50 HDD"
continueOnReturnCode: true
timeout: "4 hours"
}
output {
Array[File] quality_reports = if do_quality_control then glob("qc_reports/QC_Report_*.html") else ["qc_reports/skipped.txt"]
File individual_reports = if do_quality_control then "qc_reports/individual_reports.html" else "qc_reports/skipped.txt"
File? qc_log = if do_quality_control then "qc.log" else "qc_reports/skipped.txt"
File fastqc_summary_html = if do_quality_control then "qc_reports/summary.html" else "qc_reports/skipped.txt"
File skip_log = "qc_skip.log"
}
}
task ASSEMBLY {
input {
Array[File]+ input_reads
String assembler = "megahit"
Boolean do_assembly = true
String output_dir = "assembly"
Int cpu = 8
Int memory_gb = 16
Int min_contig_len = 500
Boolean run_quast = false
Float memory_usage = 0.8
}
command <<<
set -euo pipefail
# Debugging: Verify input files
echo "Starting assembly at $(date)" > assembly.log
echo "Input files verification:" >> assembly.log
for f in ~{sep=' ' input_reads}; do
if [ ! -f "$f" ]; then
echo "ERROR: Input file not found: $f" >> assembly.log
exit 1
fi
echo "- $f ($(wc -c < "$f") bytes)" >> assembly.log
done
if [ "~{do_assembly}" == "true" ]; then
mkdir -p ~{output_dir}
echo "Assembly parameters:" >> assembly.log
echo "- Assembler: ~{assembler}" >> assembly.log
echo "- CPU: ~{cpu}" >> assembly.log
echo "- Memory: ~{memory_gb} GB" >> assembly.log
echo "- Memory usage fraction: ~{memory_usage}" >> assembly.log
echo "- Min contig length: ~{min_contig_len}" >> assembly.log
echo "- Run QUAST: ~{run_quast}" >> assembly.log
files=( ~{sep=' ' input_reads} )
if [ $(( ${#files[@]} % 2 )) -ne 0 ]; then
echo "ERROR: Odd number of input files for assembly" >> assembly.log
exit 1
fi
# Create HTML report header
cat > ~{output_dir}/assembly_stats.html <<EOF
<!DOCTYPE html>
<html>
<head>
<title>Assembly Statistics Report</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
h1 { color: #2c3e50; border-bottom: 2px solid #3498db; padding-bottom: 10px; }
table { border-collapse: collapse; width: 100%; margin-top: 20px; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
th { background-color: #3498db; color: white; }
tr:nth-child(even) { background-color: #f2f2f2; }
</style>
</head>
<body>
<h1>Assembly Statistics Report</h1>
<p>Generated at $(date)</p>
<table>
<thead>
<tr>
<th>Sample</th>
<th>Contigs</th>
<th>Total Length</th>
<th>Min Length</th>
<th>Max Length</th>
<th>Avg Length</th>
<th>N50</th>
</tr>
</thead>
<tbody>
EOF
for ((i=0; i<"${#files[@]}"; i+=2)); do
R1="${files[i]}"
R2="${files[i+1]}"
sample_name=$(basename "$R1" | sed 's/_1.fastq.gz//; s/_1.trim.fastq.gz//; s/_R1.*//; s/_1.*//')
outdir="~{output_dir}/megahit_${sample_name}"
contig_file="~{output_dir}/${sample_name}.fa" # Removed _contigs suffix
echo "Assembling $sample_name (R1: $R1, R2: $R2)" >> assembly.log
# Run assembly in a subshell
(
set +e
megahit \
-1 "$R1" -2 "$R2" \
-o "$outdir" \
-t ~{cpu} \
--memory ~{memory_usage} \