<p>You could try something like that:</p>

<pre class="lang-bash prettyprint-override"><code>#!/bin/sh

fq_in="${1}"

bioawk -c fastx '{print substr($comment, 7, 15)}' ${fq_in} | uniq \
    &gt; "barcode1.txt"
bioawk -c fastx '{print substr($seq, 1, 5)}' ${fq_in} | uniq \
    &gt; "barcode2.txt"

for bc1 in $(cat "barcode1.txt")
do
    for bc2 in $(cat "barcode2.txt")
    do
        fq_out="${bc1}_${bc2}.fq"
        bioawk -c fastx -v bc1=${bc1} -v bc2=${bc2} \
            'substr($comment, 7, 15) == bc1 &amp;&amp; substr($seq, 1, 5) == bc2 {print "@"$name" "$comment"\n"$seq"\n+\n"$qual}' \
            ${fq_in} \
            &gt; ${fq_out}
    done
done
</code></pre>

<p>This might be slow. You can a little bit of time if you directly provide lists of barcodes for the for loops.</p>

<p>It could be more efficient to write a custom program that can write to multiple files. It happens that I wrote something that does this in python just yesterday, but only reading the inline barcode. Modifying it to also use the in-header barcode is likely simple.</p>

CC BY-SA 3.0
2018-01-11T16:48:43.597Z
2018-01-11T16:48:43.597Z
bli
<p>Since you've found separate programs to demultiplex by Illumina barcode and in-line barcode (I presume this is actually just the sequence from the cut site), just run one and then the other on its output. Typically the Illumina barcode would have been used to demultiplex the files before you even received them (if not, your sequencing facility is incompetent), in which case you just need to demultiplex by the in-line barcode</p>

CC BY-SA 3.0
2018-01-10T23:05:26.173Z
2018-01-10T23:05:26.173Z
Devon Ryan
<p>The program <code>extract</code> from <a href="https://github.com/CGATOxford/UMI-tools" rel="nofollow noreferrer"><code>UMI-Tools</code></a> will extract the barcode from the read and add it to the ID. It will also handle pair-end data so that even if the barcode is only on one of the reads, it will be added to the ID of both reads.</p>

CC BY-SA 3.0
2018-01-14T20:45:36.607Z
2018-01-14T20:45:36.607Z
Ian Sudbery
<p>I have multiplexed pair-end fastq reads with dual barcodes. The issue is that one barcode is present in the header and one is present at the beginning of the read. I need a method to demultiplex this data, but in order to assign a read to an individual, both barcodes are required, as there is overlap between the barcodes. It seems there are packages available to demultiplex using header ID or in-line barcodes to demultiplex, but not both.</p>

<p>example reads:</p>

<pre><code>@700819F:525:HT235BCXX:2:1101:1139:2144 1:N:0:ATCACGAT
CGAATTGCAGATTTTTTCTGAATAAAGCAGTGCAATAAAATTCCCCGCAAAAACACTTNANNNGNNNNNNNNNNNNNNNNNNNNANNNNNNGNTAATAAA
+
GGGGGIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII#&lt;###.####################.######&lt;#.&lt;&lt;GGGI
@700819F:525:HT235BCXX:2:1101:1212:2172 1:N:0:ATCACGAT
AAGGATGCAGGGCATCTCCCTCAGGCTGCGCTCTATCGAAGTCATCCCAGAATTAGATTCCGACCACAGACCAGTCTTAGTCAAACTAGGACCCGAGTGT
+
GGGGGIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIGIIIIIIIIIIIIIIIIIIIIGIIIIIIGGIIIGIIIIIIIIIIIIIGGIGIGIIIGIGI
@700819F:525:HT235BCXX:2:1101:1110:2173 1:N:0:ATCACGAT
GGTTGTGCAGAAAGAGTTGCTGATAAACTTAGCCATGCAGAACAGAATTATGAGTTAGAAGTATGTATATATATACCAATCACTATATCAACCCATTACC
+
&lt;G.G&lt;GGIIIG.GA.GAGAAGG&lt;AA&lt;A&lt;.&lt;&lt;&lt;GA&lt;.&lt;&lt;.G&lt;.G&lt;&lt;A&lt;GGAA....&lt;G.G..&lt;&lt;.GA..&lt;A.&lt;GG&lt;&lt;&lt;.&lt;..&lt;&lt;.GG.A..G..&lt;&lt;&lt;.&lt;&lt;G
</code></pre>

<p>One of the barcodes is the sequence found at the end of the header line (ATCACGAT). The second barcode is found at the beginning of the sequence (CGAAT in read 1). Both are required for assignment to an individual.</p>

CC BY-SA 3.0
2018-01-10T20:38:19.263Z
Can you define where in the read the barcode should be found?
Bioathlete
CC BY-SA 3.0
2018-01-10T20:51:33.403Z
One of the barcodes is the sequence found at the end of the header line (ATCACGAT). The second barcode is found at the beginning of the sequence (CGAAT in read 1). Both are required for assignment to an individual. This is ddRADSeq data, if that is important.
Caleb Benson
CC BY-SA 3.0
2018-01-10T21:03:07.993Z
Ok that make sense.  I didn't know the expected length of the barcode in the read.
Bioathlete
<fastq>
How to demultiplex pair-end fastq reads with barcode 2 in the identifier line?
# How to demultiplex pair-end fastq reads with barcode 2 in the identifier line?

(Asked by: username_0 on 2018-01-10)

I have multiplexed pair-end fastq reads with dual barcodes. The issue is that one barcode is present in the header and one is present at the beginning of the read. I need a method to demultiplex this data, but in order to assign a read to an individual, both barcodes are required, as there is overlap between the barcodes. It seems there are packages available to demultiplex using header ID or in-line barcodes to demultiplex, but not both.
example reads:
@700819F:525:HT235BCXX:2:1101:1139:2144 1:N:0:ATCACGAT
CGAATTGCAGATTTTTTCTGAATAAAGCAGTGCAATAAAATTCCCCGCAAAAACACTTNANNNGNNNNNNNNNNNNNNNNNNNNANNNNNNGNTAATAAA
+
GGGGGIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII#<###.####################.######<#.<
One of the barcodes is the sequence found at the end of the header line (ATCACGAT). The second barcode is found at the beginning of the sequence (CGAAT in read 1). Both are required for assignment to an individual.

**Tags:** fastq
> **Comments:**
> **username_1**: Can you define where in the read the barcode should be found?  
> **username_0**: One of the barcodes is the sequence found at the end of the header line (ATCACGAT). The second barcode is found at the beginning of the sequence (CGAAT in read 1). Both are required for assignment to an individual. This is ddRADSeq data, if that is important.  
> **username_1**: Ok that make sense.  I didn't know the expected length of the barcode in the read.  

---

## 3 answers

### Answer by username_2 on 2018-01-11. Score: 2

You could try something like that:
#!/bin/sh

fq_in="${1}"

bioawk -c fastx '{print substr($comment, 7, 15)}' ${fq_in} | uniq \
    > "barcode1.txt"
bioawk -c fastx '{print substr($seq, 1, 5)}' ${fq_in} | uniq \
    > "barcode2.txt"

for bc1 in $(cat "barcode1.txt")
do
    for bc2 in $(cat "barcode2.txt")
    do
        fq_out="${bc1}_${bc2}.fq"
        bioawk -c fastx -v bc1=${bc1} -v bc2=${bc2} \
            'substr($comment, 7, 15) == bc1 && substr($seq, 1, 5) == bc2 {print "@"$name" "$comment"\n"$seq"\n+\n"$qual}' \
            ${fq_in} \
            > ${fq_out}
    done
done

This might be slow. You can a little bit of time if you directly provide lists of barcodes for the for loops.
It could be more efficient to write a custom program that can write to multiple files. It happens that I wrote something that does this in python just yesterday, but only reading the inline barcode. Modifying it to also use the in-header barcode is likely simple.


---

### Answer by username_3 on 2018-01-10. Score: 1

Since you've found separate programs to demultiplex by Illumina barcode and in-line barcode (I presume this is actually just the sequence from the cut site), just run one and then the other on its output. Typically the Illumina barcode would have been used to demultiplex the files before you even received them (if not, your sequencing facility is incompetent), in which case you just need to demultiplex by the in-line barcode


---

### Answer by username_4 on 2018-01-14. Score: 1

The program `extract` from [`UMI-Tools`](https://github.com/CGATOxford/UMI-tools) will extract the barcode from the read and add it to the ID. It will also handle pair-end data so that even if the barcode is only on one of the reads, it will be added to the ID of both reads.


---

