fastseqio is a Python library for fast reading and writing of FASTA/FASTQ files.
It supports plain text and gzip-compressed files (.gz) and provides a small, practical API centered on:
seqioFile: file reader/writerRecord: sequence record object
pip install fastseqiofrom fastseqio import seqioFile
with seqioFile("test-data/test2.fa", "r") as f:
for record in f:
print(record.name, record.sequence)from fastseqio import seqioFile
with seqioFile("out.fa", "w") as f:
f.writeFasta("seq1", "ACGGGGGGGTTTT")
f.writeFasta("seq2", "TTTTCCCCAAAAG")from fastseqio import seqioFile
with seqioFile("out.fq", "w") as f:
f.writeFastq("read1", "ACGT", "IIII")seqioFile(path: str, mode: 'r' | 'w' = 'r', compressed: bool = False)path: input/output file path. Use"-"for stdin/stdout.mode:"r": read mode"w": write mode
compressed: force gzip mode when needed (for example whenpath="-").
Notes:
- If
pathends with.gz, compression is enabled automatically. - Iteration consumes the file sequentially. To re-read from the beginning, call
reset().
readOne()→ read next record (auto format)readFasta()→ read next FASTA recordreadFastq()→ read next FASTQ recordfor record in file:→ iterate withreadOne()internally
All read methods return Record or None at end-of-file.
writeFasta(name, sequence, comment=None)writeFastq(name, sequence, quality, comment=None)writeOne(name, sequence, quality=None, comment=None)- writes FASTA when
qualityis not provided - writes FASTQ when
qualityis provided
- writes FASTA when
writeRecord(record, fastq=False)
Validation:
- For FASTQ, sequence length must equal quality length.
file.set_write_options(
lineWidth=80, # optional; when provided it must be > 0
includeComments=True, # optional
baseCase="upper", # optional: "upper" or "lower"
)reset()→ rewind file to beginningfflush()→ flush write bufferclose()→ close filereadable/writablesize→ file size in bytesoffset→ current byte offset
from fastseqio import Record
record = Record("seq1", "ACGGGG", quality=None, comment="example")record.namerecord.sequencerecord.qualityrecord.commentrecord.length(same aslen(record))
record.upper(inplace=False)record.lower(inplace=False)record.reverse(inplace=False)record.subseq(start, length)record.hpc()(homopolymer compression)record.kmers(k)(iterator of k-mers)
Example:
from fastseqio import Record
r = Record("id", "AAACCCGGGTTT")
print(r.hpc()) # ACGT
print(list(r.kmers(4))) # ['AAAC', 'AACC', ...]from fastseqio import seqioFile
# Compression auto-detected by .gz suffix
with seqioFile("reads.fa.gz", "w") as w:
w.writeFasta("seq1", "ACGTACGT")
with seqioFile("reads.fa.gz", "r") as r:
for rec in r:
print(rec.name)Use context managers (with seqioFile(...) as f) so files are always closed correctly.