Answers.Body: <p>This is not possible in a single cutadapt call, but can be done fairly seamlessly with a pipe between two calls and with cutadapt's interleaving feature.  (Thanks to the author for <a href="https://github.com/marcelm/cutadapt/issues/576" rel="nofollow noreferrer">help with this</a>.)</p>
  <hr />
  <p>Giving the R2 3' adapter to one cutadapt command and interleaving the read pairs to piped output, a second cutadapt command can take that interleaved output and use both of R1's adapters as a linked adapter:</p>
  <pre><code>cutadapt -A GGGGGGGG --interleaved example.R{1,2}.fastq | \
  cutadapt -a &quot;^AAAAAAAA...TTTTTTTT&quot; --interleaved --discard-untrimmed -o out.R1.fastq -p out.R2.fastq -
  </code></pre>
  <p>Now I can add other filters to the second call.</p>
  <p>An additional read pair that should be filtered:</p>
  <p>R1:</p>
  <pre><code>@read4 would be kept but R2 is too short
  AAAAAAAACAATTTTTACTCTAGAAATGTCTGTGCTCATCACCCCGACACCGAATAGCTATGACTCAC
  +
  FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  </code></pre>
  <p>R2:</p>
  <pre><code>@read4
  CTACTAGACATATACCCTGGGGGGGGTTCCCCGTGAAGACGGTTACGATGTGGACAAGCACTCCAGCG
  +
  FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  </code></pre>
  <p>I can now give a minimum length that applies to both in each pair:</p>
  <pre><code>cutadapt -A GGGGGGGG --interleaved example.R{1,2}.fastq | \
  cutadapt -a &quot;^AAAAAAAA...TTTTTTTT&quot; --interleaved --discard-untrimmed --minimum-length 30 -o out.R1.fastq -p out.R2.fastq -
  </code></pre>
  <p>With that I get read1 and read3 in the output, as intended.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-10-20T17:33:08.843Z
Answers.LastActivityDate: 2021-10-20T17:33:08.843Z
Answers.UserDisplayName: Jesse
Body: <p>I'm using cutadapt 3.5 to trim adapters and perform some filtering on paired-end data.
  Both R1 and R2 sequences have 3' adapters that <em>might</em> be found depending on the sequence length, but R1 also has an anchored 5' adapter that I want to require for the pair to be kept in the output.  For example, if that required R1 5' adapter is AAAAAAAA, the R1 3' adapter is TTTTTTTT, and the R2 3' adapter is GGGGGGGG, and I have this input:</p>
  <p>R1:</p>
  <pre><code>@read1 should be kept
  AAAAAAAACGTCCTGGGATTTGTAATAATATTTTAGTTCTGAGCGACAAGTAAGGGATAATTTTTTTT
  +
  FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  @read2 should be filtered
  TACCAACTACATTTAGCTTCAGGCTAGTGATGCCCGCCGTCGGCACACTGGACACATGGCTTTTTTTT
  +
  FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  @read3 should be kept
  AAAAAAAACAATTTTTACTCTAGAAATGTCTGTGCTCATCACCCCGACACCGAATAGCTATGACTCAC
  +
  FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  </code></pre>
  <p>and R2:</p>
  <pre><code>@read1
  AGGCTCACCGCCACTGTTGTACCTTCCTATCGCCACTCTAAGATCTATGTAACCTCTCCCGGGGGGGG
  +
  FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  @read2
  TGAATAATACTGCGGTTTAATCGATATATTCGGGATTATGCAAGACACCCTACGTACTTAGGGGGGGG
  +
  FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  @read3
  TACTGGAGGGTACAAGCCCGGACTATCCACGGCGTCAGCTGGCTTAGCATTGAAGGTCGGCGGTGTGT
  +
  FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  </code></pre>
  <p>I'm aiming to get this R1 out:</p>
  <pre><code>@read1 should be kept
  CGTCCTGGGATTTGTAATAATATTTTAGTTCTGAGCGACAAGTAAGGGATAA
  +
  FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  @read3 should be kept
  CAATTTTTACTCTAGAAATGTCTGTGCTCATCACCCCGACACCGAATAGCTATGACTCAC
  +
  FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  </code></pre>
  <p>And this R2 out:</p>
  <pre><code>@read1
  AGGCTCACCGCCACTGTTGTACCTTCCTATCGCCACTCTAAGATCTATGTAACCTCTCCC
  +
  FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  @read3
  TACTGGAGGGTACAAGCCCGGACTATCCACGGCGTCAGCTGGCTTAGCATTGAAGGTCGGCGGTGTGT
  +
  FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  </code></pre>
  <p>This approach doesn't work as it discards read3:</p>
  <pre><code>cutadapt -a '^AAAAAAAA...TTTTTTTT' -A 'GGGGGGGG' example.R{1,2}.fastq --discard-untrimmed -o out.R1.fastq -p out.R2.fastq
  </code></pre>
  <p>I expect that's because the required/optional behavior only applies to the linked adapter for R1, and R2 is considered untrimmed for read3, which then filters the whole pair because of the <a href="https://cutadapt.readthedocs.io/en/stable/guide.html#filtering-paired-end-reads" rel="nofollow noreferrer">default</a> <code>--pair-filter=any</code> behavior (if <em>either</em> read is untrimmed, the pair is discarded).  I found I can get the filtering I want with <code>--pair-filter first</code>, but then the other filtering criteria like quality and length will only apply to R1, too.</p>
  <p>My question is, can I get the trimmed/untrimmed behavior I'm looking for (read1 and read3 trimmed, read2 untrimmed) while also applying cutadapt's filtering to both reads in each pair? In other words, can I specify to cutadapt that R1's anchored adapter is required and the other two optional, without resorting to changing the pair filter setting?</p>
  
Tags: <ngs><fastq><trimming><cutadapt>
Title: When using cutadapt, can I specify an R2 adapter as optional when I have a required R1 adapter?
ThreadText: # When using cutadapt, can I specify an R2 adapter as optional when I have a required R1 adapter?
  
  (Asked by: username_0 on 2021-10-19)
  
  I'm using cutadapt 3.5 to trim adapters and perform some filtering on paired-end data.
  Both R1 and R2 sequences have 3' adapters that *might* be found depending on the sequence length, but R1 also has an anchored 5' adapter that I want to require for the pair to be kept in the output. For example, if that required R1 5' adapter is AAAAAAAA, the R1 3' adapter is TTTTTTTT, and the R2 3' adapter is GGGGGGGG, and I have this input:
  
  R1:
  
  ```
  @read1 should be kept
  AAAAAAAACGTCCTGGGATTTGTAATAATATTTTAGTTCTGAGCGACAAGTAAGGGATAATTTTTTTT
  +
  FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  @read2 should be filtered
  TACCAACTACATTTAGCTTCAGGCTAGTGATGCCCGCCGTCGGCACACTGGACACATGGCTTTTTTTT
  +
  FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  @read3 should be kept
  AAAAAAAACAATTTTTACTCTAGAAATGTCTGTGCTCATCACCCCGACACCGAATAGCTATGACTCAC
  +
  FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  
  ```
  
  and R2:
  
  ```
  @read1
  AGGCTCACCGCCACTGTTGTACCTTCCTATCGCCACTCTAAGATCTATGTAACCTCTCCCGGGGGGGG
  +
  FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  @read2
  TGAATAATACTGCGGTTTAATCGATATATTCGGGATTATGCAAGACACCCTACGTACTTAGGGGGGGG
  +
  FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  @read3
  TACTGGAGGGTACAAGCCCGGACTATCCACGGCGTCAGCTGGCTTAGCATTGAAGGTCGGCGGTGTGT
  +
  FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  
  ```
  
  I'm aiming to get this R1 out:
  
  ```
  @read1 should be kept
  CGTCCTGGGATTTGTAATAATATTTTAGTTCTGAGCGACAAGTAAGGGATAA
  +
  FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  @read3 should be kept
  CAATTTTTACTCTAGAAATGTCTGTGCTCATCACCCCGACACCGAATAGCTATGACTCAC
  +
  FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  
  ```
  
  And this R2 out:
  
  ```
  @read1
  AGGCTCACCGCCACTGTTGTACCTTCCTATCGCCACTCTAAGATCTATGTAACCTCTCCC
  +
  FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  @read3
  TACTGGAGGGTACAAGCCCGGACTATCCACGGCGTCAGCTGGCTTAGCATTGAAGGTCGGCGGTGTGT
  +
  FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  
  ```
  
  This approach doesn't work as it discards read3:
  
  ```
  cutadapt -a '^AAAAAAAA...TTTTTTTT' -A 'GGGGGGGG' example.R{1,2}.fastq --discard-untrimmed -o out.R1.fastq -p out.R2.fastq
  
  ```
  
  I expect that's because the required/optional behavior only applies to the linked adapter for R1, and R2 is considered untrimmed for read3, which then filters the whole pair because of the [default](https://cutadapt.readthedocs.io/en/stable/guide.html#filtering-paired-end-reads) `--pair-filter=any` behavior (if *either* read is untrimmed, the pair is discarded). I found I can get the filtering I want with `--pair-filter first`, but then the other filtering criteria like quality and length will only apply to R1, too.
  
  My question is, can I get the trimmed/untrimmed behavior I'm looking for (read1 and read3 trimmed, read2 untrimmed) while also applying cutadapt's filtering to both reads in each pair? In other words, can I specify to cutadapt that R1's anchored adapter is required and the other two optional, without resorting to changing the pair filter setting?
  
  **Tags:** ngs, fastq, trimming, cutadapt
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2021-10-20. Score: 0 | Accepted answer
  
  This is not possible in a single cutadapt call, but can be done fairly seamlessly with a pipe between two calls and with cutadapt's interleaving feature. (Thanks to the author for [help with this](https://github.com/marcelm/cutadapt/issues/576).)
  
  ---
  
  Giving the R2 3' adapter to one cutadapt command and interleaving the read pairs to piped output, a second cutadapt command can take that interleaved output and use both of R1's adapters as a linked adapter:
  
  ```
  cutadapt -A GGGGGGGG --interleaved example.R{1,2}.fastq | \
  cutadapt -a "^AAAAAAAA...TTTTTTTT" --interleaved --discard-untrimmed -o out.R1.fastq -p out.R2.fastq -
  
  ```
  
  Now I can add other filters to the second call.
  
  An additional read pair that should be filtered:
  
  R1:
  
  ```
  @read4 would be kept but R2 is too short
  AAAAAAAACAATTTTTACTCTAGAAATGTCTGTGCTCATCACCCCGACACCGAATAGCTATGACTCAC
  +
  FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  
  ```
  
  R2:
  
  ```
  @read4
  CTACTAGACATATACCCTGGGGGGGGTTCCCCGTGAAGACGGTTACGATGTGGACAAGCACTCCAGCG
  +
  FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  
  ```
  
  I can now give a minimum length that applies to both in each pair:
  
  ```
  cutadapt -A GGGGGGGG --interleaved example.R{1,2}.fastq | \
  cutadapt -a "^AAAAAAAA...TTTTTTTT" --interleaved --discard-untrimmed --minimum-length 30 -o out.R1.fastq -p out.R2.fastq -
  
  ```
  
  With that I get read1 and read3 in the output, as intended.
  
  
  ---
  
  