Half-sequence and half mythical-beast, "unaligned" BAM files are used to store FASTQ files.
SAM/BAM are alignment formats, thus it feels quite anachronistic to use them to store "unaligned" sequences.
On the other hand BAM files have quite a few advantages over FASTQ:
Line oriented (all information on the sequence is on a single line),
Can store sample information via tags,
There are many tools that can operate on BAM files (extract by tags, filter by tags, etc)
In addition, the BAM format also stores not just the alignments but the entire reads sequences. To take advantage of the previously listed features, some bioinformaticians began storing their original, raw data in a so-called "unaligned" BAM. Thus we have "unaligned" reads in an "alignment format".
This recipe demonstrates code that will:
Accesses an NCBI BioProject with a given accession number.
Downloads 5 sequencing runs from the project.
Transforms each downloaded FASTQ file into a BAM file while tagging the reads from that file with the SRR number tag.
Merges the resulting BAM files into a single BAM that now contains all sequencing data for the project in just one file.
Finally the recipe demonstrates the code needed to revert the process of extracting the original data from an unaligned BAM