SnapGene File Format Parser
A reverse-engineered parser and writer for SnapGene .dna / .rna / .prot files. Supports all 17 known block types with typed Python models, a chainable builder pattern, and a history operations API.
Installation
pip install sgffp
Requires Python 3.10+.
Quick Start
from sgffp import SgffReader, SgffWriter, SgffObject
# Read a SnapGene file
sgff = SgffReader.from_file("plasmid.dna")
# Access data via typed properties
print(sgff.sequence.value)
print(sgff.features[0].name)
# Modify and write back
sgff.sequence.topology = "circular"
SgffWriter.to_file(sgff, "output.dna")
# Create a new file from scratch
sgff = (
SgffObject.new("ATGCATGCATGC", topology="circular")
.add_feature("GFP", "CDS", 0, 8)
.add_primer("fwd", "ATGC", bind_position=0)
)
SgffWriter.to_file(sgff, "new_plasmid.dna")
History Operations
Record cloning operations with automatic history tracking:
sgff.ops.insert_fragment("ATCGATCG")
sgff.ops.digest("GGCC", InputSummary={"manipulation": "digest"})
# Or build an entire tree from multiple source files
vector = SgffReader.from_file("vector.dna")
insert = SgffReader.from_file("insert.dna")
sgff.ops.build_from_spec(
[
{"id": 1, "operation": "insertFragment", "sequence": "...",
"name": "Final", "children": [2, 3]},
{"id": 2, "source": vector},
{"id": 3, "source": insert},
],
final_sequence="...",
)
How It Works
SnapGene files use a TLV (Type-Length-Value) binary format after a 19-byte header. Each block has a 1-byte type ID and a 4-byte length, with encoding varying by type: UTF-8 for sequences, XML for annotations, 2-bit GATC encoding for compressed DNA, LZMA for history, and ZTR for chromatogram traces.
SgffReader parses blocks via a version-aware SCHEME dispatch table (resolved from the file's export_version, so old and new SnapGene layouts are handled without version branches in the parsers themselves) and stores them in SgffObject.blocks (a Dict[int, List]). Typed model properties (sgff.sequence, sgff.features, sgff.history, etc.) are lazily loaded from the blocks dict and sync changes back automatically. SgffWriter serializes blocks back to binary in sorted order, normalizing to the modern format by default (or preserving the source version with preserve=True).
Supported Block Types
| ID | Block Type | Format | Model |
|---|---|---|---|
| 0 | DNA Sequence | UTF-8 | SgffSequence |
| 1 | Compressed DNA | Section-based (2-bit DNA / IUPAC / N-run + lowercase) | SgffSequence |
| 5 | Primers | XML | SgffPrimerList |
| 6 | Notes | XML | SgffNotes |
| 7 | History Tree | XML (LZMA in v15+, plain in older) | SgffHistory |
| 8 | Sequence Properties | XML | SgffProperties |
| 10 | Features | XML | SgffFeatureList |
| 11 | History Nodes | Binary + TLV | SgffHistory |
| 14 | Custom Enzyme Sets | XML | |
| 16 | Trace Container | Binary + TLV | SgffTraceList |
| 17 | Alignable Sequences | XML | SgffAlignmentList |
| 18 | ZTR Trace (in 16) | ZTR | SgffTrace |
| 20 | Strand Colors | XML | |
| 21 | Protein Sequence | UTF-8 | SgffSequence |
| 23 | File Attachments | Binary + zlib XML | SgffAttachmentList |
| 27 | Trace Alignment | BGZF + BAM | SgffTraceAlignment |
| 28 | Enzyme Visibilities | XML | |
| 29 | History Modifier | LZMA + XML | SgffHistory |
| 30 | History Content | LZMA + TLV | SgffHistory |
| 32 | RNA Sequence | UTF-8 | SgffSequence |
| 34 | RNA Structure | LZMA + JSON |
Blocks 2, 3, 13, 35 are SnapGene enzyme caches. Modern SnapGene (export_version >= 15) regenerates them, so they are skipped; legacy files (< 15) are not regenerated by SnapGene, so blocks 2, 3, 13 are retained there to keep enzymes on round-trip. See Versioning.
CLI
sff parse plasmid.dna # Export to JSON
sff info plasmid.dna -v # Show detailed file info
sff tree plasmid.dna # Display history timeline
sff check plasmid.dna -l # List block types
sff filter plasmid.dna -k 0,10 -o minimal.dna
sff convert old.dna -o new.dna # Re-save, normalizing to the modern format
sff convert old.dna -o new.dna -p # Re-save, preserving the source version
All read commands accept stdin (cat file.dna | sff info).
Development
git clone https://github.com/merv1n34k/sgffp.git
cd sgffp
uv sync --dev
# Run tests
uv run pytest tests/ -v
# Docs (VitePress)
cd docs && bun install && bun run docs:dev
Documentation
Full guides, API reference, CLI reference, and binary format specification:
Acknowledgments
This project would not have been possible without previous work done by
- Damien Goutte-Gattat, see his PDF on SGFF structure: https://incenp.org/dvlpt/docs/binary-sequence-formats/binary-sequence-formats.pdf
- Isaac Luo, for his version of SnapGene reader: https://github.com/IsaacLuo/SnapGeneFileReader
- Kale Kundert, for autosnapgene, a SnapGene automation tool: https://github.com/kalekundert/autosnapgene
Contributions
Also would like to say thank for the people who helped the project:
- Manuel Lera-Ramirez (@manulera) for his PRs and suggestions
- Cory Tobin (@cory-mozza) for reviewing new blocks
- Nicola Zilio (@nicolazilio) for legacy SnapGene support and issue reviews
License
Distributed under MIT licence, see LICENSE for more.
Metadata
Release files for sgffp 0.22.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| sgffp-0.22.1.tar.gz | 419.3 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| sgffp-0.22.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 479.9 kB
Release files / sgffp-0.22.1.tar.gz
| Download URL | sgffp-0.22.1.tar.gz |
|---|---|
| Size | 419.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4b27461e5c60e9a22adaf909beb621e8c7112b5d18221cde91b3f058b79f131b
|
|
BLAKE2b-256 checksum How to use checksums |
6f6c3d453d19c3b15e831bd7c438ff6169676d400b7bef769174159df74b369f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 12, 2026.
Transparency logRelease files / sgffp-0.22.1-py3-none-any.whl
| Download URL | sgffp-0.22.1-py3-none-any.whl |
|---|---|
| Size | 60.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
63a953fa784c066caf49a500820f7e38f6d77390d5d6567c7b5abb2b5d7b606a
|
|
BLAKE2b-256 checksum How to use checksums |
bab4e5816f0c64015f79cc7518a46de05369263f36a85d38d6f6171a6d6ac0e7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 12, 2026.
Transparency logSecurity for sgffp 0.22.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for sgffp-0.22.1.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 12, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.