I have paired-end sequencing reads (Illumina) in .fastq format (I have 2 files, so not interleaved) that I want to demultiplex.
They contain 2 samples which each have 2 barcodes, one for the forward read and one for the reverse read (so 4 barcodes in total).
A typical read looks like this:

first_part_primer - barcode - last_part_primer - rest_of_sequence

The primer sequence before the barcode can have different lengths or sometimes it is missing completely.

I have tried using [sabre](https://github.com/najoshi/sabre) with the following command:

    sabre pe -f file1.fq -r file2.fq -b barcode.txt -u unmatched_file1.fq -w unmatched_file2.fq -c

where the barcode.txt is formatted as is suggested in the sabre documentation.
This works fine when the barcodes are at the beginning of the reads, but when the barcodes are somewhere in the middle of the reads, it doesn't recognize them. This seems to be the case for most demultiplexing tools I have seen.
I have also found [demultiplex](https://demultiplex.readthedocs.io/en/latest/usage.html), but this gives errors during installation.

My question is how to demultiplex the reads in this case?
Please let me know if you have any suggestions or solutions.

**Edit**

This is an example of how the reads look

> @A00153:690:HJVN5DSXY:1:1101:1470:1000 1:N:0:GAACGTGA+AACTGGTG
AGGTCAGTCACATGGTTAGGACGCAGATAGACAACGAAAACGAACGGGATAAAATATTTAACTTGCGGGACGGATTCAGCTCTCACTACGACCAGCACTACCTAAGAAGATCGGAAGAGCACACGTCTGAACTCCAGTCACGAACGTGAA
+
F:FFFF:FFFFF:F:,,F:,FF:FFFFFFFFFFFF:FFFF,FFFF,:,FFFFFFFFFFFFF,FFFFF:,F,FFFFFFFF,FFFFFFFFF,FFFF,:F,F::FF,::,F:FFF,FFFFFFF,F::FF,:,FFFFFFF:FFF,F:FF,FF::

> @A00153:690:HJVN5DSXY:1:1101:3568:1000 2:N:0:GAACGTGA+AACTGGTG
AGGTCAGTCACATGGTTAGGACGCAGCGAGTAAACGAAAACGAACGGGATAAATACGGTAATCGAAAACCGATACGATCGGCATAGAAAAGGTTGACAAGGAAATTGACGAATTGAAGCAGAAACTGGAAAACTTGGTAAAACAAGAAGC
+
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF,FFFFFFFFFFFFFFFFFFFFFFF:F:FFFFFFFFFFFFFFFFFFFFFFFF:FFF:FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF:FFFFFFFFFFFFFFF

> @A00153:690:HJVN5DSXY:1:1101:4056:1000 2:N:0:GAACGTGA+AACTGGTG
AGGTCAGTCACATGGTTAGGACGCAGCGAGTAAACGAAAACGAACGGGATAAATACGGTAATCGAAAACCGATACGATCCGGTCGGGTTAAAGTCGAAATCGGACGGGAACCGGTATTTTTGTTCGGTAAAATCACACATGGCTACGAAC
+
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF:FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

The first example is repeated below where the italics indicate the primer sequence and the bold text the barcode sequence.

*AGGTCAGTCACATGGTTAGGACGCA **GATAGACA** ACGAAAACGAACGGGATAAA* ATATTTAACTTGCGGGACGGATTCAGCTCTCACTACGACCAGCACTACCTAAGAAGATCGGAAGAGCACACGTCTGAACTCCAGTCACGAACGTGAA
Inspired by the comments of both @GenoMax and @swbarnes2, I have found a solution, though possibly not the most efficient one.
It is a combination of bash codes and [```bbsplitpairs.sh```](https://jgi.doe.gov/data-and-tools/bbtools/bb-tools-user-guide/repair-guide/) (part of the bbduk package).
It uses the following steps:

1. ```grep --no-group-separator -h -A 2 -B 1 -E 'barcode1|barcode2' $file1 $file2 > $outputfile```
This searches for all lines in both fastq files that contain either of the two barcodes. When a line contains one of the barcodes, it stores that line (the actual sequence), the previous line (the header)(```-B 1```) and the next two lines (the dummy line with the ```+``` and the quality score of the read)(```-A 2```) to an output file. The reads from both input files are stored in a single output file (the output file is thus interleaved now).

2. ```cat $outputfile | paste - - - - | sort -k1,1 -S 1G | tr '\t' '\n' > $outputfilesorted```
The disadvantage of the first step is that the read pairs are not ordered. All the downstream processes expect that read pairs are stored together like this:
 - readpair 1, forward read
 - readpair 1, reverse read
 - readpair 2, forward read
 - readpair 2, reverse read
 - etc.

To accomplish this, the reads are sorted based on their headers so that the reads that form a pair are subsequent reads. This is stored in another output file that should have the same size as the output file from the first step.

3. ```bash bbsplitpairs.sh -Xmx1g in=$outputfilesorted out=$outputpair outs=$outputsing fint```
After step 2 the fastq files are correctly formatted, but there are still singletons in the files (reads that do not have a paired partner because the paired partner either has a barcode that belongs to another sample or does not contain any barcodes at all). Those singleton reads have to be removed from the files in order for the downstream processes to work correctly.
For this I use the bbsplitpairs tools from the bbmap package.

These three steps are repeated for all samples that are present in the data with the appropriate barcodes.

When I check the output files, the reads indeed seem to be properly split and combined.