生物表型包

子模块

模块内容

表型数据输入/输出。

输入

主要函数是Bio.henotype.parse(.)它接受输入文件和格式化字符串。这将返回一个给出PlateRecord对象的迭代器:

>>> from Bio import phenotype
>>> for record in phenotype.parse("phenotype/Plates.csv", "pm-csv"):
...     print("%s %i" % (record.id, len(record)))
...
PM01 96
PM09 96

请注意,parse()函数将使用其默认设置调用格式的相关解析器。您可能需要更多的控制,在这种情况下,您需要直接创建特定于格式的序列迭代器。

输入-单个记录

如果您希望您的文件包含且仅包含一条记录,则我们提供以下‘helper’函数,该函数将返回单个PlateRecord,或者在没有记录或多条记录时引发异常:

>>> from Bio import phenotype
>>> record = phenotype.read("phenotype/Plate.json", "pm-json")
>>> print("%s %i" % (record.id, len(record)))
PM01 96

当您只需要一条记录(并且会将多条记录视为错误)时,此样式非常有用。例如,在处理由OPM库保存的PM JSON文件时。

但是,如果您只需要包含多条记录文件中的第一条记录,请在迭代器上使用next()函数:

>>> from Bio import phenotype
>>> record = next(phenotype.parse("phenotype/Plates.csv", "pm-csv"))
>>> print("%s %i" % (record.id, len(record)))
PM01 96

只要文件包含至少一条记录,上述代码就可以工作。请注意,如果有多条记录,则其余记录将被静默忽略。

输出

使用函数Bio.henotype.write(.),该函数接受完整的PlateRecord对象集(作为列表或迭代器)、输出文件句柄(或在最新版本的Biopython中作为字符串的输出文件名),当然还有文件格式::

from Bio import phenotype
records = ...
phenotype.write(records, "example.json", "pm-json")

或者,使用句柄::

from Bio import phenotype
records = ...
with open("example.json", "w") as handle:
   phenotype.write(records, handle, "pm-json")

您需要调用此函数一次(包含所有记录),如果使用句柄,请确保将其关闭以将数据刷新到硬盘。

文件格式

指定文件格式时,请使用小写字符串。

  • JSON格式的PM-JSON-表型微阵列板。

  • PM-CSV-CSV格式的表型微阵列板,这是

    机器供应商格式

请注意,虽然Bio.hentype可以读取上述文件格式,但它只能以JSON格式写入。

Bio.phenotype.write(plates, handle, format)

将完整的PlateRecords集写入文件。

  • PlateRecord-PlateRecord对象的列表(或迭代器)。

  • Handle-要写入的文件句柄对象,或字符串形式的文件名

    (请注意,旧版本的Biopython只接受一个句柄)。

  • 格式-描述要写入的文件格式的小写字符串。

调用此函数后应关闭句柄。

返回写入的记录数(整数形式)。

Bio.phenotype.parse(handle, format)

将表型文件转换为返回PlateRecords的迭代器。

  • Handle-文件的句柄,或字符串形式的文件名

    (请注意,旧版本的Biopython只接受一个句柄)。

  • 格式-描述文件格式的小写字符串。

典型用法是打开要读入的文件,然后循环遍历记录:

>>> from Bio import phenotype
>>> filename = "phenotype/Plates.csv"
>>> for record in phenotype.parse(filename, "pm-csv"):
...    print("ID %s" % record.id)
...    print("Number of wells %i" % len(record))
...
ID PM01
Number of wells 96
ID PM09
Number of wells 96

使用Bio.henotype.read(.)函数,当您只需要一条记录时。

Bio.phenotype.read(handle, format)

将表型文件转换为单个PlateRecord。

  • Handle-文件的句柄,或字符串形式的文件名

    (请注意,旧版本的Biopython只接受一个句柄)。

  • 格式-描述文件格式的字符串。

此函数用于解析正好包含一条记录的表型文件。例如,读取PM JSON文件:

>>> from Bio import phenotype
>>> record = phenotype.read("phenotype/Plate.json", "pm-json")
>>> print("ID %s" % record.id)
ID PM01
>>> print("Number of wells %i" % len(record))
Number of wells 96

如果句柄不包含任何记录,或包含多条记录,则会引发异常。例如::

from Bio import phenotype
record = phenotype.read("plates.csv", "pm-csv")
Traceback (most recent call last):
...
ValueError: More than one record found in handle

但是,如果您想要包含多条记录的文件中的第一条记录,则此函数将引发异常(如上面的示例所示)。请改用:

>>> from Bio import phenotype
>>> record = next(phenotype.parse("phenotype/Plates.csv", "pm-csv"))
>>> print("First record's ID %s" % record.id)
First record's ID PM01

如果要从句柄读取多条记录,请使用Bio.henotype.parse(Handle,Format)函数。