| 1 | package NBI::Launcher::RemovePrimers; | ||
| 2 | |||
| 3 | # ============================================================================= | ||
| 4 | # NBI::Launcher::RemovePrimers - Primer removal using cutadapt (paired-end) | ||
| 5 | # | ||
| 6 | # PACKAGED DEMO launcher. To customise for your project, copy to: | ||
| 7 | # ./launchers/remove-primers.pm (project-local, highest priority) | ||
| 8 | # ~/.nbi/launchers/remove-primers.pm (user-level) | ||
| 9 | # | ||
| 10 | # The package declaration must remain NBI::Launcher::RemovePrimers regardless | ||
| 11 | # of where the file lives (nbilaunch uses the package name internally). | ||
| 12 | # | ||
| 13 | # Tool: cutadapt https://cutadapt.readthedocs.io | ||
| 14 | # Mode: paired-end only | ||
| 15 | # Notes: --discard-untrimmed is hardcoded (drop pairs with no primer found) | ||
| 16 | # Primer reverse complements are computed in Perl - no seqfu needed. | ||
| 17 | # IUPAC degenerate bases are fully supported in primer sequences. | ||
| 18 | # ============================================================================= | ||
| 19 | |||
| 20 | use strict; | ||
| 21 | use warnings; | ||
| 22 | use parent 'NBI::Launcher'; | ||
| 23 | |||
| 24 | # ── IUPAC reverse complement ────────────────────────────────────────────────── | ||
| 25 | # Complement table covering all standard IUPAC ambiguity codes. | ||
| 26 | # Add any custom codes here if your primers use non-standard notation. | ||
| 27 | my %IUPAC_COMP = ( | ||
| 28 | A => 'T', T => 'A', C => 'G', G => 'C', # unambiguous bases | ||
| 29 | R => 'Y', Y => 'R', # R = A|G Y = C|T | ||
| 30 | S => 'S', # S = C|G self-complementary | ||
| 31 | W => 'W', # W = A|T self-complementary | ||
| 32 | K => 'M', M => 'K', # K = G|T M = A|C | ||
| 33 | B => 'V', V => 'B', # B = C|G|T V = A|C|G | ||
| 34 | D => 'H', H => 'D', # D = A|G|T H = A|C|T | ||
| 35 | N => 'N', # N = any | ||
| 36 | ); | ||
| 37 | |||
| 38 | sub _rc { | ||
| 39 | my ($seq) = @_; | ||
| 40 | $seq = uc $seq; | ||
| 41 | $seq = reverse $seq; | ||
| 42 | $seq =~ s/([ACGTRYWSKMBVDHN])/$IUPAC_COMP{$1} \/\/ $1/ge; | ||
| 43 | return $seq; | ||
| 44 | } | ||
| 45 | # ───────────────────────────────────────────────────────────────────────────── | ||
| 46 | |||
| 47 | sub new { | ||
| 48 | my ($class) = @_; | ||
| 49 | return $class->SUPER::new( | ||
| 50 | |||
| 51 | name => 'remove-primers', | ||
| 52 | description => 'Remove primers from paired-end reads using cutadapt', | ||
| 53 | version => '3.3', | ||
| 54 | |||
| 55 | # ── Activation ─────────────────────────────────────────────────────── | ||
| 56 | # Option A (used here): Singularity image. | ||
| 57 | # The path can be overridden at submission time via $CUTADAPT_IMG. | ||
| 58 | # | ||
| 59 | # Option B: HPC module - swap the activate block for: | ||
| 60 | # activate => { module => 'cutadapt/3.3' }, | ||
| 61 | # | ||
| 62 | # Option C: conda - swap for: | ||
| 63 | # activate => { conda => 'cutadapt-env' }, | ||
| 64 | activate => { | ||
| 65 | singularity => $ENV{CUTADAPT_IMG} | ||
| 66 | // '/nbi/software/testing/GMH-Tools/images/cutadapt~3.3', | ||
| 67 | }, | ||
| 68 | |||
| 69 | # ── Slurm defaults ─────────────────────────────────────────────────── | ||
| 70 | # All of these can be overridden on the command line: | ||
| 71 | # --queue / --mem / --cpus / --runtime | ||
| 72 | slurm_defaults => { | ||
| 73 | queue => 'qib-short', | ||
| 74 | threads => 8, | ||
| 75 | memory => 12, # GB; cutadapt is mostly I/O-bound | ||
| 76 | runtime => '04:00:00', | ||
| 77 | }, | ||
| 78 | |||
| 79 | # ── Inputs ─────────────────────────────────────────────────────────── | ||
| 80 | inputs => [ | ||
| 81 | { name => 'r1', | ||
| 82 | flag => '-1', | ||
| 83 | type => 'file', | ||
| 84 | required => 1, | ||
| 85 | help => 'Forward reads (R1) FASTQ[.gz] - also used to derive sample name', | ||
| 86 | }, | ||
| 87 | { name => 'r2', | ||
| 88 | flag => '-2', | ||
| 89 | type => 'file', | ||
| 90 | required => 1, | ||
| 91 | help => 'Reverse reads (R2) FASTQ[.gz]', | ||
| 92 | }, | ||
| 93 | ], | ||
| 94 | |||
| 95 | # ── Parameters ─────────────────────────────────────────────────────── | ||
| 96 | params => [ | ||
| 97 | { name => 'fwd_primer', | ||
| 98 | flag => '-f', | ||
| 99 | type => 'string', | ||
| 100 | required => 1, | ||
| 101 | help => 'Forward primer sequence (IUPAC degenerate bases supported)', | ||
| 102 | }, | ||
| 103 | { name => 'rev_primer', | ||
| 104 | flag => '-r', | ||
| 105 | type => 'string', | ||
| 106 | required => 1, | ||
| 107 | help => 'Reverse primer sequence (IUPAC degenerate bases supported)', | ||
| 108 | }, | ||
| 109 | # slurm_sync: --threads is not exposed as a user flag; | ||
| 110 | # it is automatically set to match --cpus (Slurm --cpus-per-task). | ||
| 111 | { name => 'threads', | ||
| 112 | flag => '-j', | ||
| 113 | type => 'int', | ||
| 114 | slurm_sync => 'threads', | ||
| 115 | }, | ||
| 116 | ], | ||
| 117 | |||
| 118 | # ── Outputs ────────────────────────────────────────────────────────── | ||
| 119 | # {sample} is derived from the R1 basename by stripping known suffixes: | ||
| 120 | # _R1, _1, .fastq, .fq, .gz | ||
| 121 | # Override with --sample-name STR on the nbilaunch command line. | ||
| 122 | outputs => [ | ||
| 123 | { name => 'r1_trimmed', | ||
| 124 | pattern => '{sample}_R1.fq.gz', | ||
| 125 | required => 1, | ||
| 126 | help => 'Primer-trimmed forward reads', | ||
| 127 | }, | ||
| 128 | { name => 'r2_trimmed', | ||
| 129 | pattern => '{sample}_R2.fq.gz', | ||
| 130 | required => 1, | ||
| 131 | help => 'Primer-trimmed reverse reads', | ||
| 132 | }, | ||
| 133 | { name => 'report', | ||
| 134 | pattern => '{sample}.cutadapt.txt', | ||
| 135 | required => 0, | ||
| 136 | help => 'cutadapt run report (stdout)', | ||
| 137 | }, | ||
| 138 | { name => 'log', | ||
| 139 | pattern => '{sample}.cutadapt.log', | ||
| 140 | required => 0, | ||
| 141 | help => 'cutadapt stderr log', | ||
| 142 | }, | ||
| 143 | ], | ||
| 144 | |||
| 145 | outdir => { flag => '--outdir', short => '-o', required => 1 }, | ||
| 146 | |||
| 147 | # use_tmpdir: write to $TMPDIR (fast local disk) during the job, | ||
| 148 | # then promote outputs to outdir on success. | ||
| 149 | scratch => { use_tmpdir => 1, cleanup_on_failure => 1 }, | ||
| 150 | ); | ||
| 151 | } | ||
| 152 | |||
| 153 | # ── make_command ────────────────────────────────────────────────────────────── | ||
| 154 | # Builds the cutadapt shell invocation embedded in the generated job script. | ||
| 155 | # | ||
| 156 | # %args contains all resolved values at submission time: | ||
| 157 | # $args{r1}, $args{r2} absolute paths to input files | ||
| 158 | # $args{fwd_primer} forward primer string (may contain IUPAC) | ||
| 159 | # $args{rev_primer} reverse primer string (may contain IUPAC) | ||
| 160 | # $args{threads} injected from Slurm --cpus-per-task | ||
| 161 | # $args{sample} derived sample name (or --sample-name value) | ||
| 162 | # $args{scratch} scratch directory (managed by base class) | ||
| 163 | # | ||
| 164 | # To customise the cutadapt flags (e.g. add --minimum-length, change error | ||
| 165 | # rate, etc.) edit the join() call below. | ||
| 166 | # ───────────────────────────────────────────────────────────────────────────── | ||
| 167 | sub make_command { | ||
| 168 | my ($self, %args) = @_; | ||
| 169 | |||
| 170 | # Reverse complements of both primers (IUPAC-aware, computed at submit time) | ||
| 171 | my $fwd_rc = _rc($args{fwd_primer}); | ||
| 172 | my $rev_rc = _rc($args{rev_primer}); | ||
| 173 | |||
| 174 | # Linked-adapter notation (cutadapt §Linked adapters): | ||
| 175 | # R1 gets fwd_primer...rev_rc (5'-anchored fwd, 3'-anchored rev_rc) | ||
| 176 | # R2 gets rev_primer...fwd_rc (5'-anchored rev, 3'-anchored fwd_rc) | ||
| 177 | my $adapter_r1 = "$args{fwd_primer}...$rev_rc"; | ||
| 178 | my $adapter_r2 = "$args{rev_primer}...$fwd_rc"; | ||
| 179 | |||
| 180 | # Outputs land in scratch; base class moves them to outdir after validation | ||
| 181 | my $out_r1 = "\$SCRATCH/$args{sample}_R1.fq.gz"; | ||
| 182 | my $out_r2 = "\$SCRATCH/$args{sample}_R2.fq.gz"; | ||
| 183 | my $report = "\$SCRATCH/$args{sample}.cutadapt.txt"; | ||
| 184 | my $log = "\$SCRATCH/$args{sample}.cutadapt.log"; | ||
| 185 | |||
| 186 | # Singularity prefix - populated when activate => { singularity => ... }. | ||
| 187 | # Empty string when using module or conda activation (handled by the | ||
| 188 | # activation_lines() section of the script instead). | ||
| 189 | my $img = $self->{activate}{singularity} // ''; | ||
| 190 | my $prefix = $img ? "singularity exec $img " : ''; | ||
| 191 | |||
| 192 | return join(" \\\n ", | ||
| 193 | "${prefix}cutadapt", | ||
| 194 | "-j $args{threads}", | ||
| 195 | "-a $adapter_r1", # linked adapter for R1 | ||
| 196 | "-A $adapter_r2", # linked adapter for R2 | ||
| 197 | '--discard-untrimmed', # drop pairs where no primer is detected | ||
| 198 | "-o $out_r1", | ||
| 199 | "-p $out_r2", | ||
| 200 | $args{r1}, | ||
| 201 | $args{r2}, | ||
| 202 | ) . " \\\n > $report 2> $log"; | ||
| 203 | } | ||
| 204 | |||
| 205 | 1; | ||
| 206 | |||
| 207 | __END__ |