我对python比较新,所以请原谅这个问题带来的愚蠢。我有一个genbank文件并编写了一段代码,它将采用前三个最长的基因并将它们放入一个新生成的fasta文件中。
from Bio import SeqIO
file="sequence.gb"
output=open("Top3.faa", "w")
record=SeqIO.parse(file, "genbank")
rec=next(record)
print('The genes with the top 3 longest lengths have beens saved in Top3.faa')
for f in rec.features:
end=f.location.end.position
start=f.location.start.position
length=end-start
bug=(rec.seq)
if f.type=='CDS':
if 'gene' in f.qualifiers:
if length>7000:
geneName=f.qualifiers['gene']
name=str(geneName)
lenth=str(length)
seq=str(bug[start:end])
output.write('>')
output.write(lenth)
output.write('\n')
output.write(seq)
output.write('\n')
output.close()
我正在尝试做的不是手动输入支票,如果它超过7kb,找到代码的方式自己做,并自动找到3个顶部命中。任何形式的帮助,我可以去哪里的方向将非常感激。感谢
答案 0 :(得分:0)
您可以保留N个最大的列表(大小)。
这样的事情(这可能会崩溃,因为我无法测试它,但想法就在那里:
from Bio import SeqIO
file="sequence.gb"
output=open("Top3.faa", "w")
record=SeqIO.parse(file, "genbank")
rec=next(record)
print('The genes with the top 3 longest lengths have beens saved in Top3.faa')
# Largest genes and their size, sorted from the shortest to the longest.
# size first, gene name next, then seq.
largest_genes = [ (0, None, None) ] * 3; # initialize with the number of genes you need.
for f in rec.features:
end = f.location.end.position
start = f.location.start.position
length = end-start
bug = (rec.seq)
if f.type=='CDS' and 'gene' in f.qualifiers:
if length > largest_genes[0][0]: # [0] gives the first, [0] gives the length.
# This one is larger than the smallest one we have.
largest_genes = largest_genes[1:] # drop the smallest one.
# add this one
largest_genes.append((length, f.qualifiers['gene'], str(bug[start:end])))
largest_genes.sort() # re-sort.
for length, name, seq in largest_genes:
# name is not used but available.
output.write('>')
output.write(str(lenth))
output.write('\n')
output.write(seq)
output.write('\n')
output.close()