seqioFile¶
The seqioFile class provides reading and writing of FASTA/FASTQ files, with automatic format detection and gzip support.
Constructor¶
Opens a FASTA/FASTQ file for reading or writing.
Parameters
path(str): File path. Use"-"for stdin (read mode) or stdout (write mode).mode("r"|"w"):"r"for reading,"w"for writing. Default"r".compressed(bool): Force gzip compression. Ifpathends with.gz, compression is automatically enabled.
Raises
ValueError: Ifmodeis not"r"or"w".
Notes
- If
pathends with.gz, the file is treated as gzip-compressed regardless of thecompressedargument. - In read mode, the file format (FASTA/FASTQ) is automatically detected per record.
- The file handle should be closed via
.close()or preferably used as a context manager (with seqioFile(...) as f).
Examples
# Read a plain text FASTA/FASTQ file
reader = seqioFile("data.fa", "r")
# Write a gzipped FASTA file (automatic detection via .gz extension)
writer = seqioFile("output.fa.gz", "w")
# Read from stdin
import sys
reader = seqioFile("-", "r")
Read Methods¶
All read methods return a Record object, or None at end-of-file.
readOne()¶
Reads the next record, auto-detecting FASTA/FASTQ format.
Returns
RecordorNone: The next record, orNoneif EOF.
Raises
ValueError: If file not opened in read mode.
Example
readFasta()¶
Reads the next FASTA record (skips FASTQ records).
Returns
RecordorNone: The next FASTA record, orNoneif EOF.
Raises
ValueError: If file not opened in read mode.
readFastq()¶
Reads the next FASTQ record (skips FASTA records).
Returns
RecordorNone: The next FASTQ record, orNoneif EOF.
Raises
ValueError: If file not opened in read mode.
Write Methods¶
writeOne()¶
writeOne(
name: str,
sequence: str,
quality: Optional[str] = None,
comment: Optional[str] = None,
) -> None
Writes a single record in FASTA (if quality is None) or FASTQ format.
Parameters
name(str): Sequence identifier.sequence(str): Nucleotide sequence.quality(Optional[str]): Quality scores (required for FASTQ).comment(Optional[str]): Optional comment line (aftername).
Raises
ValueError: If file not opened in write mode.AssertionError: Ifqualityis provided butlen(quality) != len(sequence).
Example
with seqioFile("out.fa", "w") as f:
f.writeOne("seq1", "ACGT", comment="example")
with seqioFile("out.fq", "w") as f:
f.writeOne("read1", "ACGT", "IIII")
writeFasta()¶
Convenience wrapper for writeOne(name, sequence, comment=comment).
writeFastq()¶
Convenience wrapper for writeOne(name, sequence, quality, comment=comment).
writeRecord()¶
Writes an existing Record object.
Parameters
record(Record): The record to write.fastq(bool): IfTrue, write as FASTQ (requiresrecord.quality). DefaultFalse(FASTA).
Raises
ValueError: If file not opened in write mode.AssertionError: Iffastq=Trueandlen(record.sequence) != len(record.quality).
Writer Configuration¶
set_write_options()¶
set_write_options(
*,
lineWidth: Optional[int] = None,
includeComments: Optional[bool] = None,
baseCase: Optional[Literal["upper", "lower"]] = None,
) -> None
Adjusts formatting of written records.
Parameters
lineWidth(Optional[int]): Wrap sequence lines to this width (must be > 0). DefaultNone(no wrapping).includeComments(Optional[bool]): Whether to write comment lines (FASTA>header comment). DefaultNone(keep current).baseCase(Optional["upper" | "lower"]): Convert sequence letters to uppercase or lowercase. DefaultNone(keep original).
Raises
ValueError: If file not opened in write mode.AssertionError: IflineWidth <= 0.
Example
with seqioFile("out.fa", "w") as f:
f.set_write_options(lineWidth=60, baseCase="upper")
f.writeFasta("seq", "acgtacgt")
Properties¶
readable → bool¶
True if file opened in read mode.
writable → bool¶
True if file opened in write mode.
size → int¶
File size in bytes (read mode only).
offset → int¶
Current byte offset within the file (read mode only).
Utility Methods¶
reset()¶
Rewinds the file to the beginning (read mode only). Allows re‑reading the same file.
fflush()¶
Flushes any buffered data to disk (write mode only).
close()¶
Closes the file handle. Called automatically when exiting a with block.
__iter__()¶
Enables iteration over records: for record in file: ....
Example
Context Manager¶
seqioFile supports the context‑manager protocol for automatic closing: