Bio.SeqIO.FastaIO 模組
Bio.SeqIO 支援 “fasta”(又稱 FastA 或 Pearson)檔案格式。
您應該透過 Bio.SeqIO 函式來使用此模組。
- Bio.SeqIO.FastaIO.SimpleFastaParser(handle)
將 Fasta 記錄作為字串元組進行迭代。
- 引數
handle - 以文字模式開啟的輸入流
對於每個記錄,會傳回一個包含兩個字串的元組,即 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 記錄作為字串元組進行迭代。
- 引數
handle - 以文字模式開啟的輸入流
在功能上與 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: IO[str] | PathLike | str | bytes, alphabet: None = None)
-
Fasta 檔案的解析器。
- __init__(source: IO[str] | PathLike | str | bytes, alphabet: None = None) None
將 Fasta 記錄作為 SeqRecord 物件進行迭代。
- 引數
source - 以文字模式開啟的輸入流,或檔案的路徑
alphabet - 選用字母,未使用。保留為 None。
預設情況下,這會像呼叫 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
如果您想在寫入之前修改記錄,例如變更每個記錄的 ID,可以使用生成器函式,如下所示
>>> def modify_records(records): ... for record in records: ... record.id = record.id.upper() ... yield record ... >>> with open('Fasta/dups.fasta') as handle: ... for record in modify_records(FastaIterator(handle)): ... print(record.id) ... ALPHA BETA GAMMA ALPHA DELTA
- parse(handle)
開始解析檔案,並傳回 SeqRecord 生成器。
- iterate(handle)
解析檔案並產生 SeqRecord 物件。
- __abstractmethods__ = frozenset({})
- __parameters__ = ()
- class Bio.SeqIO.FastaIO.FastaTwoLineIterator(source)
-
每個記錄恰好有兩行的 Fasta 檔案的解析器。
- __init__(source)
將兩行的 Fasta 記錄(作為 SeqRecord 物件)進行迭代。
- 引數
source - 以文字模式開啟的輸入流,或檔案的路徑
這使用嚴格的 FASTA 解釋,要求每個記錄恰好有兩行(沒有換行)。
只提供寬鬆 FASTA 解析器提供的預設標題到 ID/名稱/描述解析。
- parse(handle)
開始解析檔案,並傳回 SeqRecord 生成器。
- iterate(handle)
解析檔案並產生 SeqRecord 物件。
- __abstractmethods__ = frozenset({})
- __parameters__ = ()
- 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 寫入器 (已過時)。
- 引數
target - 以文字模式開啟的輸出串流,或檔案路徑。
wrap - 用於換行的可選行長度。預設值是在 60 個字元處換行序列。使用零 (或 None) 表示不換行,序列將呈現為單一長行。
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
用於寫入每個記錄兩行的 Fasta 格式檔案的類別 (已過時)。
這表示我們在寫入序列資訊時不進行換行,並且對於空的序列,始終會寫入空白行。
請改用
as_fasta_2line
函數,或使用頂層的Bio.SeqIO.write()
函數,並搭配format="fasta"
。- __init__(handle, record2title=None)
建立每個記錄兩行的 Fasta 寫入器 (已過時)。
- 引數
handle - 輸出檔案的處理 (handle),例如 `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”) 函數會在內部使用此函數。