|
| 1 | +/* |
| 2 | + * Deacon: Fast alignment-free decontamination |
| 3 | + * https://github.com/bede/deacon |
| 4 | + * |
| 5 | + * Key features: |
| 6 | + * - Preserves FASTQ headers (critical for read pairing) |
| 7 | + * - Composable indexes via set algebra (union, diff, intersect) |
| 8 | + * - SIMD-accelerated, ~5GB RAM for panhuman index |
| 9 | + */ |
| 10 | + |
| 11 | +process DEACON_BUILD_INDEX { |
| 12 | + /* |
| 13 | + * Build a deacon index from FASTA file(s). |
| 14 | + * Use this for custom contaminant sequences. |
| 15 | + */ |
| 16 | + |
| 17 | + tag "${fasta.simpleName}" |
| 18 | + label "medium" |
| 19 | + |
| 20 | + input: |
| 21 | + path fasta |
| 22 | + |
| 23 | + output: |
| 24 | + path "*.idx", emit: index |
| 25 | + |
| 26 | + script: |
| 27 | + def prefix = fasta.simpleName |
| 28 | + """ |
| 29 | + deacon index build \\ |
| 30 | + --threads ${task.cpus} \\ |
| 31 | + -k ${params.deacon_kmer_size} \\ |
| 32 | + -w ${params.deacon_window_size} \\ |
| 33 | + ${fasta} > ${prefix}.k${params.deacon_kmer_size}w${params.deacon_window_size}.idx |
| 34 | + """ |
| 35 | +} |
| 36 | + |
| 37 | +process DEACON_FETCH_INDEX { |
| 38 | + /* |
| 39 | + * Download a prebuilt deacon index from URL. |
| 40 | + * Takes the URL as a channel value so the process only runs when |
| 41 | + * the input channel is non-empty (no `when:` guard needed). |
| 42 | + * Caches in work directory; use storeDir for persistent caching. |
| 43 | + */ |
| 44 | + |
| 45 | + label "low" |
| 46 | + |
| 47 | + input: |
| 48 | + val url |
| 49 | + |
| 50 | + output: |
| 51 | + path "*.idx", emit: index |
| 52 | + |
| 53 | + script: |
| 54 | + def filename = url.tokenize('/').last() |
| 55 | + """ |
| 56 | + curl -fsSL "${url}" -o ${filename} |
| 57 | + """ |
| 58 | +} |
| 59 | + |
| 60 | +process DEACON_UNION_INDEXES { |
| 61 | + /* |
| 62 | + * Combine multiple deacon indexes via set union. |
| 63 | + * Only called when both a base index and custom index are present. |
| 64 | + */ |
| 65 | + |
| 66 | + label "low" |
| 67 | + |
| 68 | + input: |
| 69 | + path indexes // Collection of .idx files (always 2+) |
| 70 | + |
| 71 | + output: |
| 72 | + path "combined.idx", emit: index |
| 73 | + |
| 74 | + script: |
| 75 | + def idx_list = indexes.collect { it.name }.join(' ') |
| 76 | + """ |
| 77 | + deacon index union ${idx_list} > combined.idx |
| 78 | + """ |
| 79 | +} |
| 80 | + |
| 81 | +process DEACON_DEPLETE { |
| 82 | + /* |
| 83 | + * Remove contaminant reads using deacon filter in deplete mode. |
| 84 | + * |
| 85 | + * Critical: This preserves FASTQ headers verbatim, which is required |
| 86 | + * for repair.sh to re-pair reads after filtering. SPAdes paired-end |
| 87 | + * assembly depends on proper read pairing. |
| 88 | + * |
| 89 | + * Deacon natively handles gzipped input/output (since v0.13.0). |
| 90 | + * When writing .gz output via --output, deacon splits --threads 1:1 |
| 91 | + * between filtering and compression automatically. |
| 92 | + */ |
| 93 | + |
| 94 | + tag "${sample_id}" |
| 95 | + label "medium" |
| 96 | + |
| 97 | + errorStrategy { task.attempt < 3 ? 'retry' : 'ignore' } |
| 98 | + maxRetries 2 |
| 99 | + |
| 100 | + input: |
| 101 | + tuple val(sample_id), val(platform), val(read_structure), path(reads), path(index) |
| 102 | + |
| 103 | + output: |
| 104 | + tuple val(sample_id), val(platform), val(read_structure), path("${sample_id}.depleted.fastq.gz"), emit: reads |
| 105 | + tuple val(sample_id), path("${sample_id}.deacon.json"), emit: stats |
| 106 | + |
| 107 | + script: |
| 108 | + """ |
| 109 | + deacon filter \\ |
| 110 | + --deplete \\ |
| 111 | + --threads ${task.cpus} \\ |
| 112 | + --abs-threshold ${params.deacon_abs_threshold} \\ |
| 113 | + --rel-threshold ${params.deacon_rel_threshold} \\ |
| 114 | + --summary ${sample_id}.deacon.json \\ |
| 115 | + --output ${sample_id}.depleted.fastq.gz \\ |
| 116 | + ${index} \\ |
| 117 | + ${reads} |
| 118 | + """ |
| 119 | +} |
0 commit comments