Bio.phenotype 套件

子模組

模組內容

表型資料輸入/輸出。

輸入

主要函數是 Bio.phenotype.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() 函數將會使用預設設定調用該格式的相關解析器。您可能需要更多控制權,在這種情況下,您需要直接建立一個特定格式的序列迭代器。

輸入 - 單一記錄

如果您預期您的檔案只包含一個記錄,那麼我們提供以下「輔助」函數,它會返回一個單一的 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.phenotype.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")

您應該呼叫此函數一次(使用所有記錄),如果使用控制代碼,請務必關閉它,以便將資料刷新到硬碟。

檔案格式

當指定檔案格式時,請使用小寫字串。

  • pm-json - JSON 格式的表型微陣列板。

  • pm-csv - CSV 格式的表型微陣列板,即

    機器供應商格式

請注意,雖然 Bio.phenotype 可以讀取上述檔案格式,但它只能以 JSON 格式寫入。

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

將一整組 PlateRecords 寫入檔案。

  • plates - PlateRecord 物件的列表(或迭代器)。

  • handle - 要寫入的檔案控制代碼物件,或作為字串的檔案名稱

    (請注意,舊版本的 Biopython 只接受控制代碼)。

  • format - 描述要寫入的檔案格式的小寫字串。

您應該在呼叫此函數後關閉控制代碼。

返回寫入的記錄數(為整數)。

Bio.phenotype.parse(handle, format)

將表型檔案轉換為返回 PlateRecords 的迭代器。

  • handle - 指向檔案的控制代碼,或作為字串的檔案名稱

    (請注意,舊版本的 Biopython 只接受控制代碼)。

  • format - 描述檔案格式的小寫字串。

典型用法是打開檔案進行讀取,並循環處理記錄

>>> 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.phenotype.read(…) 函數。

Bio.phenotype.read(handle, format)

將表型檔案轉換為單一 PlateRecord。

  • handle - 指向檔案的控制代碼,或作為字串的檔案名稱

    (請注意,舊版本的 Biopython 只接受控制代碼)。

  • format - 描述檔案格式的字串。

此函數用於解析只包含一個記錄的表型檔案。例如,讀取 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.phenotype.parse(handle, format) 函數。