This skill enables AI agents to help you read, write, and randomly access compressed sequence files (.gz, .bz2, .xz, .bgz) using Biopython plus bgzip/samtools. The central idea: any of these formats can be read sequentially, but only BGZF (blocked gzip) is seekable, so only BGZF can be indexed for random access by SeqIO.index, samtools faidx, or tabix.
pip install biopython
# Optional, for CLI conversion and faidx indexing:
# conda install -c bioconda htslib samtools (provides bgzip + samtools)
gzip, bz2, and lzma are built into Python; no install needed for sequential read/write.
Tell your AI agent what you want to do:
"Parse the gzipped FASTA and print each sequence id and length." "My .bz2 FASTQ won't parse with SeqIO - what handle mode do I need?"
"Write these records straight to a gzip-compressed FASTA without an intermediate plain file." "Save this FASTA as BGZF so I can index it later."
"Convert sequences.fasta.gz to BGZF and extract the region gene_042:1-200." "Build a persistent SQLite index over several BGZF FASTA files and fetch records by id." "samtools faidx says my file is 'not compressed with bgzip' - how do I fix it?"
"Re-compress this plain gzip genome as BGZF without writing a decompressed copy to disk." "Decompress reads.fastq.gz back to plain reads.fastq."
.gz may be plain gzip or BGZF; bgzip -t confirms BGZF).gzip.open/bz2.open/lzma.open/bgzf.open, 'rt' or 'wt').zcat | bgzip or the Bio.bgzf writer) and index with SeqIO.index/index_db or samtools faidx.'rt'/'wt') with SeqIO; a binary 'rb' handle raises TypeError..gz is NOT seekable - SeqIO.index and faidx require BGZF; convert with bgzip first.bgzf.open reads BGZF input only; use gzip.open for plain gzip.gunzip/zcat read it fine - the asymmetry only bites indexing tools.samtools faidx on a BGZF FASTA writes both .fai and .gzi; keep both or region extraction breaks.pyfastx (seek-point index over the gzip stream).bgzf.tell() can be compared and re-seeked but never subtracted to get a byte length.