from Bio import SeqIO
path_to_file = ("/Users/richard/Desktop/AllSurfaceGlycoproteinSeqs.fasta")
with open(path_to_file, mode='r') as handle:
out = open("/Users/name/Desktop/texas1.fasta", 'w')
for record in SeqIO.parse(handle, 'fasta'):
identifer = record.id
description = record.description
sequence = record.seq
if "TX" in description:
out.write("{}\n".format(identifer))
out.write("{}\n".format(description))
out.write("{}\n".format(sequence))
out.close()
該代碼目前為我提供了所有幾千個輸出,但我只想要創建的新檔案中的前 100 個輸出。
uj5u.com熱心網友回復:
添加一個計數器, break當你有足夠的時候(不確定你想在哪種條件下計數,所以我會增加 TX 記錄上的計數器):
with open(path_to_file, mode='r') as handle:
out = open("/Users/name/Desktop/texas1.fasta", 'w')
counter = 0
for record in SeqIO.parse(handle, 'fasta'):
if counter >= 100:
break
identifer = record.id
description = record.description
sequence = record.seq
if "TX" in description:
counter = 1
out.write("{}\n".format(identifer))
out.write("{}\n".format(description))
out.write("{}\n".format(sequence))
注意。你不需要創建中間變數(identifer = record.id)你可以直接做out.write("{}\n".format(record.id))
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/388177.html
標籤:Python
