ClustalFile#

class biotite.sequence.io.clustal.ClustalFile[source]#

Bases: TextFile, MutableMapping[str, str]

This class represents a file in ClustalW format (.aln).

A ClustalW file starts with a header line beginning with CLUSTAL, followed by blocks of aligned sequences separated by blank lines. Each sequence line contains a sequence name followed by a segment of the gapped sequence. An optional consensus line, containing only *, :, . and space characters, may follow each block.

This class is used in a dictionary-like manner, implementing the MutableMapping interface: Sequence names are used as keys, and strings containing the full gapped sequences are the corresponding values.

Examples

>>> import os.path
>>> file = ClustalFile()
>>> file["seq1"] = "ADTRCGTARDCGTR-DRTCGRAGD"
>>> file["seq2"] = "ADTRCGT---CGTRADRTCGRAGD"
>>> print(file["seq1"])
ADTRCGTARDCGTR-DRTCGRAGD
>>> for name, seq in file.items():
...     print(name, seq)
seq1 ADTRCGTARDCGTR-DRTCGRAGD
seq2 ADTRCGT---CGTRADRTCGRAGD
copy() Self#

Create a deep copy of this object.

Returns:
copy

A copy of this object.

classmethod read(file: PathLike[str] | str | IO[str]) Self#

Read a ClustalW file.

Parameters:
filefile-like object or str

The file to be read. Alternatively a file path can be supplied.

Returns:
file_objectClustalFile

The parsed file.

static read_iter(file: str | PathLike[str] | IO[str]) Iterator[str]#

Create an iterator over each line of the given text file.

Parameters:
filefile-like object or str

The file to be read. Alternatively a file path can be supplied.

Yields:
linestr

The current line in the file.

write(file: str | PathLike[str] | IO[str]) None#

Write the contents of this object into a file (or file-like object).

Parameters:
filefile-like object or str

The file to be written to. Alternatively a file path can be supplied.

static write_iter(file: str | PathLike[str] | IO[str], lines: Iterable[str]) None#

Iterate over the given lines of text and write each line into the specified file.

In contrast to write(), each line of text is not stored in an intermediate TextFile, but is directly written to the file. Hence, this static method may save a large amount of memory if a large file should be written, especially if the lines are provided as generator.

Parameters:
filefile-like object or str

The file to be written to. Alternatively a file path can be supplied.

linesgenerator or array-like of str

The lines of text to be written. Must not include line break characters.