Biopython提供了一个模块Bio.SeqIO
来分别从文件读取序列和向文件写入序列(任何流)。它支持生物信息学中几乎所有可用的文件格式。大多数软件为不同的文件格式提供了不同的方法。但是,Biopython有意识地遵循一种方法,通过SeqRecord
对象向用户显示已解析的序列数据。
在下一节中将了解有关SeqRecord
的更多信息。
1. SeqRecord
Bio.SeqRecord
模块提供SeqRecord来保存序列的元信息以及序列数据本身,如下所示:
seq
− 一个实际的顺序。id
− 给定序列的主要标识符,默认类型是字符串。name
− 序列的名称,默认类型是字符串。description
− 显示有关该序列的人类可读信息。annotations
− 有关序列的其他信息的字典。
可以按以下指定的方式导入SeqRecord
:
from Bio.SeqRecord import SeqRecord
在接下来的部分中,让我们了解使用实际序列文件解析序列文件的细微差别。
2. 解析序列文件格式
本节说明有关如何解析两种最受欢迎的序列文件格式FASTA
和GenBank
。
2.1. FASTA
FASTA是用于存储序列数据的最基本的文件格式。最初,FASTA是用于DNA和蛋白质序列比对的软件包,该软件包是在生物信息学的早期发展过程中开发的,主要用于搜索序列相似性。
Biopython提供了一个示例FASTA文件,可以从- https://github.com/biopython/biopython/blob/master/Doc/examples/ls_orchid.fasta 访问该文件。
将此文件下载为orchid.fasta
并将其保存到Biopython示例目录中。
Bio.SeqIO
模块提供parse()
方法来处理序列文件,可以按如下方式导入 -
from Bio.SeqIO import parse
parse()
方法包含两个参数,第一个是文件句柄,第二个是文件格式。
>>> file = open('path/to/biopython/sample/orchid.fasta')
>>> for record in parse(file, "fasta"):
... print(record.id)
...
gi|2765658|emb|Z78533.1|CIZ78533
gi|2765657|emb|Z78532.1|CCZ78532
..........
..........
gi|2765565|emb|Z78440.1|PPZ78440
gi|2765564|emb|Z78439.1|PBZ78439
>>>
在这里,parse()
方法返回一个可迭代的对象,该对象在每次迭代时都返回SeqRecord
。可迭代的它提供了许多复杂而简单的方法。
next()
next()
方法返回可迭代对象中的下一个可用项目,可以使用它来获得第一个序列,如下所示:
>>> first_seq_record = next(SeqIO.parse(open('path/to/biopython/sample/orchid.fasta'),'fasta'))
>>> first_seq_record.id 'gi|2765658|emb|Z78533.1|CIZ78533'
>>> first_seq_record.name 'gi|2765658|emb|Z78533.1|CIZ78533'
>>> first_seq_record.seq Seq('CGTAACAAGGTTTCCGTAGGTGAACCTGCGGAAGGATCATTGATGAGACCGTGG...CGC', SingleLetterAlphabet())
>>> first_seq_record.description 'gi|2765658|emb|Z78533.1|CIZ78533 C.irapeanum 5.8S rRNA gene and ITS1 and ITS2 DNA'
>>> first_seq_record.annotations
{}
>>>
此处,seq_record.annotations
为空,因为FASTA格式不支持序列注释。
列表理解
可以使用列表理解将可迭代对象转换为列表,如下所示:
>>> seq_iter = SeqIO.parse(open('path/to/biopython/sample/orchid.fasta'),'fasta')
>>> all_seq = [seq_record for seq_record in seq_iter] >>> len(all_seq)
94
>>>
在这里,使用len
方法获取序列中项目的总数。如下所示:
>>> seq_iter = SeqIO.parse(open('path/to/biopython/sample/orchid.fasta'),'fasta')
>>> max_seq = max(len(seq_record.seq) for seq_record in seq_iter)
>>> max_seq
789
>>>
也可以使用以下代码过滤序列 -
>>> seq_iter = SeqIO.parse(open('path/to/biopython/sample/orchid.fasta'),'fasta')
>>> seq_under_600 = [seq_record for seq_record in seq_iter if len(seq_record.seq) < 600]
>>> for seq in seq_under_600:
... print(seq.id)
...
gi|2765606|emb|Z78481.1|PIZ78481
gi|2765605|emb|Z78480.1|PGZ78480
gi|2765601|emb|Z78476.1|PGZ78476
gi|2765595|emb|Z78470.1|PPZ78470
gi|2765594|emb|Z78469.1|PHZ78469
gi|2765564|emb|Z78439.1|PBZ78439
>>>
将SqlRecord
对象(已解析的数据)的集合写入文件就像调用SeqIO.write
方法一样简单,如下所示:
file = open("converted.fasta", "w)
SeqIO.write(seq_record, file, "fasta")
此方法可以有效地用于转换以下指定的格式 -
file = open("converted.gbk", "w)
SeqIO.write(seq_record, file, "genbank")
2.2. GenBank
它是基因的更丰富的序列格式,并且包括用于各种注释的字段。Biopython提供了一个示例GenBank文件,可以访问 - https://github.com/biopython/biopython/blob/master/Doc/examples/ls_orchid.fasta 并下载。
将文件下载为orchid.gbk
并将其保存到Biopython示例目录中。
由于Biopython提供了一个函数 - parse()
可以解析所有生物信息学格式。解析GenBank
格式就像在parse
方法中更改format
选项一样简单。
参考下面给出的代码:
>>> from Bio import SeqIO
>>> from Bio.SeqIO import parse
>>> seq_record = next(parse(open('path/to/biopython/sample/orchid.gbk'),'genbank'))
>>> seq_record.id
'Z78533.1'
>>> seq_record.name
'Z78533'
>>> seq_record.seq Seq('CGTAACAAGGTTTCCGTAGGTGAACCTGCGGAAGGATCATTGATGAGACCGTGG...CGC', IUPACAmbiguousDNA())
>>> seq_record.description
'C.irapeanum 5.8S rRNA gene and ITS1 and ITS2 DNA'
>>> seq_record.annotations {
'molecule_type': 'DNA',
'topology': 'linear',
'data_file_division': 'PLN',
'date': '30-NOV-2006',
'accessions': ['Z78533'],
'sequence_version': 1,
'gi': '2765658',
'keywords': ['5.8S ribosomal RNA', '5.8S rRNA gene', 'internal transcribed spacer', 'ITS1', 'ITS2'],
'source': 'Cypripedium irapeanum',
'organism': 'Cypripedium irapeanum',
'taxonomy': [
'Eukaryota',
'Viridiplantae',
'Streptophyta',
'Embryophyta',
'Tracheophyta',
'Spermatophyta',
'Magnoliophyta',
'Liliopsida',
'Asparagales',
'Orchidaceae',
'Cypripedioideae',
'Cypripedium'],
'references': [
Reference(title = 'Phylogenetics of the slipper orchids (Cypripedioideae:
Orchidaceae): nuclear rDNA ITS sequences', ...),
Reference(title = 'Direct Submission', ...)
]
}