Bio.SeqIO.FastaIO模块¶
Bio.SeqIO支持“FASTA”(又名FASTA或Pearson)文件格式。
您需要通过Bio.SeqIO函数使用此模块。
- Bio.SeqIO.FastaIO.SimpleFastaParser(handle)¶
将FASTA记录作为字符串元组迭代。
- 参数:
句柄-以文本模式打开的输入流
对于每条记录,将返回两个字符串的元组、FASTA标题行(没有前导‘>’字符)和序列(删除了任何空格)。标题行不分为标识符(第一个单词)和注释或描述。
>>> with open("Fasta/dups.fasta") as handle: ... for values in SimpleFastaParser(handle): ... print(values) ... ('alpha', 'ACGTA') ('beta', 'CGTC') ('gamma', 'CCGCC') ('alpha (again - this is a duplicate entry to test the indexing code)', 'ACGTA') ('delta', 'CGCGC')
- Bio.SeqIO.FastaIO.FastaTwoLineParser(handle)¶
将未包装的FASTA记录作为字符串元组迭代。
- 参数:
句柄-以文本模式打开的输入流
在功能上与SimpleFastaParser相同,但将FASTA格式严格解释为每条记录正好两行,带有描述的大于符号标识符,以及没有换行的序列。
任何换行都会引发异常,多余的空行也会引发异常(除了作为记录第二行的零长度序列的特殊情况)。
示例
此文件对每个FASTA记录使用两行:
>>> with open("Fasta/aster_no_wrap.pro") as handle: ... for title, seq in FastaTwoLineParser(handle): ... print("%s = %s..." % (title, seq[:3])) ... gi|3298468|dbj|BAA31520.1| SAMIPF = GGH...
此等效文件使用换行:
>>> with open("Fasta/aster.pro") as handle: ... for title, seq in FastaTwoLineParser(handle): ... print("%s = %s..." % (title, seq[:3])) ... Traceback (most recent call last): ... ValueError: Expected FASTA record starting with '>' character. Perhaps this file is using FASTA line wrapping? Got: 'MTFGLVYTVYATAIDPKKGSLGTIAPIAIGFIVGANI'
- class Bio.SeqIO.FastaIO.FastaIterator(source, alphabet=None, title2ids=None)¶
-
FASTA文件的解析器。
- __init__(source, alphabet=None, title2ids=None)¶
将FASTA记录作为SeqRecord对象迭代。
- 参数:
source-以文本模式打开的输入流,或文件路径
字母表-可选的字母表,不使用。保留为无。
title2ids-一个函数,当给定FASTA文件的标题(没有开头>)时,它将以字符串元组的形式返回记录的id、名称和描述(按该顺序)。如果未指定,则整个标题行将用作描述,第一个单词用作id和名称。
默认情况下,它的作用类似于调用Bio.SeqIO.parse(Handle,“Fasta”),没有对标题行进行自定义处理:
>>> with open("Fasta/dups.fasta") as handle: ... for record in FastaIterator(handle): ... print(record.id) ... alpha beta gamma alpha delta
但是,您可以提供Title2ids函数来更改以下内容:
>>> def take_upper(title): ... return title.split(None, 1)[0].upper(), "", title >>> with open("Fasta/dups.fasta") as handle: ... for record in FastaIterator(handle, title2ids=take_upper): ... print(record.id) ... ALPHA BETA GAMMA ALPHA DELTA
- parse(handle)¶
开始解析文件,并返回SeqRecord生成器。
- iterate(handle)¶
解析文件并生成SeqRecord对象。
- __abstractmethods__ = frozenset({})¶
- class Bio.SeqIO.FastaIO.FastaTwoLineIterator(source)¶
-
FASTA文件的解析器,每条记录正好有两行。
- __init__(source)¶
迭代两行FASTA记录(作为SeqRecord对象)。
- 参数:
source-以文本模式打开的输入流,或文件路径
这使用了对FASTA的严格解释,即每个记录只需要两行(没有换行)。
仅提供由宽松的FASTA解析器提供的ID/名称/描述解析的默认标题。
- parse(handle)¶
开始解析文件,并返回SeqRecord生成器。
- iterate(handle)¶
解析文件并生成SeqRecord对象。
- __abstractmethods__ = frozenset({})¶
- class Bio.SeqIO.FastaIO.FastaWriter(target, wrap=60, record2title=None)¶
-
类以写入FASTA格式文件(已过时)。
请使用
as_fasta
函数,或者顶层Bio.SeqIO.write()
函数,而不是使用format="fasta"
。- __init__(target, wrap=60, record2title=None)¶
创建FASTA编写器(已过时)。
- 参数:
目标-以文本模式或文件路径打开的输出流。
换行-用于换行序列行的可选行长。默认情况下,将序列换行为60个字符,如果不换行,则使用零(或无),为序列提供单个长行。
record2title-可选函数,用于返回用于每条记录标题行的文本。默认情况下,使用record.id和record.description的组合。如果record.description以record.id开头,则只使用record.description。
您可以使用::
handle = open(filename, "w") writer = FastaWriter(handle) writer.write_file(myRecords) handle.close()
或者,遵循顺序文件写入系统,例如::
handle = open(filename, "w") writer = FastaWriter(handle) writer.write_header() # does nothing for Fasta files ... Multiple writer.write_record() and/or writer.write_records() calls ... writer.write_footer() # does nothing for Fasta files handle.close()
- write_record(record)¶
将单个FASTA记录写入文件。
- class Bio.SeqIO.FastaIO.FastaTwoLineWriter(handle, record2title=None)¶
基类:
FastaWriter
类编写每个记录2行的FASTA格式文件(已过时)。
这意味着我们在不换行的情况下写入序列信息,并且将始终为空序列写入一个空行。
请使用
as_fasta_2line
函数,或者顶层Bio.SeqIO.write()
函数,而不是使用format="fasta"
。- __init__(handle, record2title=None)¶
为每个记录创建2行FASTA写入器(已过时)。
- 参数:
句柄-输出文件的句柄,例如由open(filename,“w”)返回的句柄
record2title-可选函数,用于返回用于每条记录标题行的文本。默认情况下,使用record.id和record.description的组合。如果record.description以record.id开头,则只使用record.description。
您可以使用::
handle = open(filename, "w") writer = FastaWriter(handle) writer.write_file(myRecords) handle.close()
或者,遵循顺序文件写入系统,例如::
handle = open(filename, "w") writer = FastaWriter(handle) writer.write_header() # does nothing for Fasta files ... Multiple writer.write_record() and/or writer.write_records() calls ... writer.write_footer() # does nothing for Fasta files handle.close()
- Bio.SeqIO.FastaIO.as_fasta(record)¶
将SeqRecord转换为FASTA格式的字符串。
它由SeqRecord的.format(“Fasta”)方法和SeqIO.write(.,.,“Fasta”)函数在内部使用。
- Bio.SeqIO.FastaIO.as_fasta_2line(record)¶
将SeqRecord转换为两行FASTA格式的字符串。
它由SeqRecord的.format(“Fasta-2line”)方法和SeqIO.write(.,.,“Fasta-2line”)函数在内部使用。