Properly parsing FASTQ is a nightmare. It's not possible to start at a random position inTo emphasise the file and guarantee that a seek forwardissue (or backwards) to an '@' symbol will arrive atas outlined in the start of a FASTQ header. That's a problem if log(1/n)
time complexityupdate), consider an input file with one record that is desired5 million bases long, because the problem meansand one records that it's not possible to read through the entire file to get a sampleis 100 bases long. You want an equal probability of sequencesselecting either of these two records. Any random seek methods will overwhelmingly pick out the long record.
IfI expect that indexing the allowed FASTQ format were more strict, i.e. 4 lines perlocations of record: [header, sequence, header, quality], then it would be possible to do this: seek to a random location in the file, seek to find starts is really the next line that contains only a +
(preceded by a DNA sequence to exclude +
in the quality string), seek backwards two linesworkable option here, and there'sparticularly if multi-line records are possible. Create an index file containing the start locations of a FASTQeach record (as identically sized integers, e.
If the file were indexedg. 64-bit), then it would be possible to do the shuffling onsample from the index and use thatfile (which is an identical length for each record) to select reads fromfetch the file..start location. butI'd envisage that this file would also require a complexity of over n
only contain the start locations; any additional metadata (because at least indexing is neededincluding sequence name) would require seeking in the original file.
Once the indexing is done, the file can be compressed with bgzip, with specific offsets retrieved using the samtools view-b
has theand -s <float>
option for subsampling aligned reads, but that's in BAM formatoptions. However, rather than FASTQ format (andI expect that compression would still go through the whole file before arriving at the last read)not be particularly efficient if multiple random records were desired.