agentleFS
pradyumnasagarpradyumnasagar/open-research-skills/skills/bioinformatics-sequence/compressed-sequence-files/SKILL.md

compressed-sequence-files

Skill8 starsChanged 5 months agoMITView on GitHub

Read and write gzip, bzip2, and BGZF-compressed FASTA/FASTQ using Python stdlib and Biopython — including indexable BGZF.

The file

We split this skill into the rules it gives an agent

4 rules. About 0 are common, 3 are distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈3

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    No fabricated citations: Every cited work must resolve to a verifiable

    line 20
  2. Distinctive:

    No claim without provenance: Every quantitative or factual claim

    line 21
  3. Distinctive:

    No silent failure: Every script invocation, API call, or tool use must declare its exit status and what to do on non-zero.

    line 22
  4. Very specific:

    After BGZF write, SeqIO.index("out.bgz", "fasta")["known_id"] returns the same record.

    line 175

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md
<!-- metadata: category: bioinformatics-sequence oversight: low version: 1.0.0 author: Pradyumna Jayaram tags: [bioinformatics-sequence, research] difficulty: intermediate -->

Compressed Sequence Files (gzip / bzip2 / BGZF)

Hard rules

  1. No fabricated citations. Every cited work must resolve to a verifiable
  2. No claim without provenance. Every quantitative or factual claim
  3. No silent failure. Every script invocation, API call, or tool use must declare its exit status and what to do on non-zero. A skill that silently swallows errors is a violation.

When to use

  • Reading or writing .fasta.gz, .fastq.gz, .fasta.bz2 in Python.
  • Producing or consuming indexable compressed FASTA (BGZF).
  • Building pipelines that need random access into compressed records.

When NOT to use

"

  • Plain uncompressed FASTA — open with SeqIO.parse(path, "fasta") directly.
  • BAM/CRAM/VCF BGZF: use pysam (see ors-bioinformatics-sequence-pysam-genomics).
  • Whole-genome-scale index needs → use samtools faidx + bgzip, not Biopython.

Prerequisites

  • biopython>=1.83 (ships Bio.bgzf).
  • For production BGZF: htslib ≥ 1.19 (bgzip + tabix).
  • For bzip2: Python's stdlib bz2.

Core workflow

  1. Open in text mode ('rt' / 'wt'). The parser expects text. 'rb' will trip on a bytes-vs-str TypeError.
  2. Pick the format by use case:
    • .gz → archive only, not indexable.
    • .bz2 → smaller but slower; archive only.
    • .bgz → indexable via SeqIO.index(); native for BAM/VCF/tabix.
  3. For new pipelines, prefer BGZF unless you have a hard reason to use plain gzip.
  4. For BAM/VCF, always use htslib bgzip — its blocks are HTSlib-compatible; Biopython's BGZF works but is slower on huge files.

Code patterns

Read gzipped FASTA

import gzip
from Bio import SeqIO

with gzip.open("sequences.fasta.gz", "rt") as fh:
    for rec in SeqIO.parse(fh, "fasta"):
        print(rec.id, len(rec.seq))

Read bzip2

import bz2
from Bio import SeqIO

with bz2.open("reads.fastq.bz2", "rt") as fh:
    recs = list(SeqIO.parse(fh, "fastq"))

Write gzipped output

import gzip
from Bio import SeqIO

with gzip.open("clean.fasta.gz", "wt") as out:
    SeqIO.write(records, out, "fasta")

Read BGZF (text path) and via bgzf.open

from Bio import SeqIO, bgzf

# Either works
for rec in SeqIO.parse("seqs.fasta.bgz", "fasta"):
    ...

with bgzf.open("seqs.fasta.bgz", "rt") as fh:
    for rec in SeqIO.parse(fh, "fasta"):
        ...

Write BGZF for downstream indexing

from Bio import SeqIO, bgzf

with bgzf.open("indexable.fasta.bgz", "wt") as out:
    SeqIO.write(SeqIO.parse("raw.fasta", "fasta"), out, "fasta")

Index a BGZF file (random access by record ID)

from Bio import SeqIO

db = SeqIO.index("seqs.fasta.bgz", "fasta")
print(db["target_id"].seq)
db.close()

# Or persistent on-disk index for huge files
db = SeqIO.index_db("seqs.idx", "seqs.fasta.bgz", "fasta")

Auto-detect by extension (single entry point)

from pathlib import Path
import gzip, bz2
from Bio import SeqIO, bgzf

def open_seq(path: str, fmt: str):
    p = Path(path)
    sfx = "".join(p.suffixes).lower()
    if sfx.endswith(".bgz") or sfx.endswith(".bgzf"):
        fh = bgzf.open(p, "rt")
    elif sfx.endswith(".gz") or sfx.endswith(".bgzf"):
        fh = gzip.open(p, "rt")
    elif sfx.endswith(".bz2"):
        fh = bz2.open(p, "rt")
    else:
        fh = open(p, "r")
    return SeqIO.parse(fh, fmt)

Convert plain gzip to indexable BGZF

import gzip
from Bio import SeqIO, bgzf

with gzip.open("input.fasta.gz", "rt") as src, \
     bgzf.open("indexable.fasta.bgz", "wt") as dst:
    SeqIO.write(SeqIO.parse(src, "fasta"), dst, "fasta")

Low-memory record count (FASTA)

import gzip
from Bio.SeqIO.FastaIO import SimpleFastaParser

with gzip.open("sequences.fasta.gz", "rt") as fh:
    n = sum(1 for _ in SimpleFastaParser(fh))
print(f"{n} sequences")

Common pitfalls

  • Using 'rb' instead of 'rt'. You'll get TypeError: a bytes-like object is required, not 'str'.
  • Expecting SeqIO.index() to work on plain .gz. It does not — convert to BGZF first.
  • Mixing Biopython BGZF with htslib bgzip blocks. They are wire-compatible for FASTA, but for .vcf.gz/.bcf use htslib bgzip end-to-end to avoid rare edge cases.
  • Decompressing to disk then re-reading. Wastes 3-4x disk. Stream from the compressed handle.
  • .bz2 is slow. For long-term archives, yes; for active work, prefer BGZF.

Validation

  • File ends with the correct magic: gzip → 1f 8b, bzip2 → 42 5a, BGZF → 1f 8b 08 04 (with extra fields).
  • After BGZF write, SeqIO.index("out.bgz", "fasta")["known_id"] returns the same record.
  • Round-trip: bgzf open → gzip open byte counts differ only by BGZF block overhead.

Open alternatives

NeedOpen toolWhy
Indexable compressed FASTAhtslib bgzip (≥ 1.19)BAM/VCF-compatible blocks
Fast stats on .fasta.gzseqkit stats -j 8 *.fasta.gzMuch faster than Python
Random access by regionsamtools faidx regions.fa.gz + bgzipStandard, battle-tested
BGZF block dumpbgzip -b 0 -d out.fa.gzInspect block boundaries

References

Changelog

  • 1.0.0 (2026-06-10): Initial adaptation by Pradyumna Jayaram from bio-compressed-files (bioSkills-main/sequence-io/compressed-files).

Cross-references

Other skills in this category:

  • batch-processing
  • bowtie2-alignment
  • bwa-alignment
  • bwa-mem2-alignment
  • codon-usage
  • fastq-quality-scores
  • filter-sequences
  • format-conversion
  • hisat2-alignment
  • motif-search
  • paired-end-fastq
  • pysam-genomics
  • read-write-sequences
  • reverse-complement
  • sam-bam-basics
  • samtools-bam-processing
  • seq-objects
  • sequence-properties
  • sequence-slicing
  • sequence-statistics
  • star-alignment
  • transcription-translation

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.