Skip to content

SgffObject & I/O

SgffObject

Container for all SnapGene file data.

python
from sgffp import SgffObject

Fields

FieldTypeDescription
cookieCookieFile header metadata
blocksDict[int, List[Any]]Parsed block data keyed by type ID

Constructor

python
SgffObject(cookie=Cookie(), blocks={})

Class Methods

new(sequence, *, topology, strandedness, sequence_type) → SgffObject

Create a new SnapGene file with sensible defaults.

ParameterTypeDefaultDescription
sequencestr""DNA/RNA/protein sequence
topologystr"linear""linear" or "circular"
strandednessstr"double""single" or "double"
sequence_typestr"dna""dna", "rna", or "protein"

Model Properties

All lazily loaded and cached. Call invalidate() to reset.

PropertyTypeDescription
sequenceSgffSequenceDNA/RNA/Protein sequence
featuresSgffFeatureListAnnotation features
primersSgffPrimerListPrimers
notesSgffNotesFile notes
propertiesSgffPropertiesSequence properties
historySgffHistoryEdit history
alignmentsSgffAlignmentListAlignable sequences
tracesSgffTraceListChromatogram traces
opsSgffOpsOperations API

Existence Checks

PropertyChecks block
has_notes6
has_properties8
has_features10
has_primers5
has_history7, 11, 29, 30
has_alignments17
has_traces16

Block Access Methods

MethodDescription
typesList of block type IDs present
type(block_id) → BlockListGet all items for a block type
block(block_id) → Any | NoneGet first item of a block type
set(block_id, value)Append value to block type
bset(block_id, value)Replace entire block type
remove(block_id, idx=0) → boolRemove single item by index
bremove(block_id) → boolRemove entire block type
invalidate()Clear all cached model instances

Builder Methods

All return self for chaining.

add_feature(name, type, start, end, strand="+", **kwargs) → SgffObject

Add an annotation feature.

add_primer(name, sequence, bind_position=None, bind_strand="+") → SgffObject

Add a primer.

set_sequence(new_sequence, operation="replace", record_history=True) → SgffObject

Set a new sequence. If record_history=True and history exists, records the change in the history tree.


File header metadata.

python
from sgffp import Cookie
FieldTypeDefaultDescription
type_of_sequenceint11=DNA, 2=Protein, 7=RNA
export_versionint15SnapGene export version
import_versionint19SnapGene import version

BlockList

Wrapper for accessing items of a single block type. Returned by sgff.type(block_id).

MemberDescription
typeBlock type ID
firstFirst item (or None)
lastLast item (or None)
get(idx=0)Item at index (or None)
len(bl)Number of items
bl[idx]Item at index
for item in blIterate items

SgffReader

Read and parse SnapGene files into SgffObject.

python
from sgffp import SgffReader
MethodDescription
SgffReader(source)Create reader from path, Path, or BinaryIO
reader.read() → SgffObjectParse and return
SgffReader.from_file(path) → SgffObjectRead from file path
SgffReader.from_bytes(data) → SgffObjectRead from bytes

SgffWriter

Write SgffObject to SnapGene file format.

python
from sgffp import SgffWriter
MethodDescription
SgffWriter(target)Create writer to path, Path, or BinaryIO
writer.write(sgff)Write SgffObject to target
SgffWriter.to_file(sgff, path)Write to file path
SgffWriter.to_bytes(sgff) → bytesWrite to bytes

Blocks are written in sorted order by type ID.

Released under the MIT License.