文件的反向补码



任务是:编写一个脚本(叫它你想要的),可以分析快速文件(MySequences.fasta)通过查找序列的反向补。使用python .

from itertools import repeat
#opening file
filename = "MySequences.fasta"
file = open(filename, 'r')
#reading the file
for line in file:
line = line.strip()
if ">" in line:
header = line
elif (len(line) == 0):
continue
else:
seq = line
#reverse complement
def reverse_complement(seq):
compline = ''
for n in seq:
if n == 'A':
compline += 'T'
elif n == 'T':
compline += 'A'
elif n == 'C':
compline += 'G'
elif n == 'G':
compline += 'C'
return((compline)[::-1])
#run each line
for line in file:
rc = reverse_complement(seq)
print(rc)    

您在错误的地方运行了函数。要为每个迭代器运行函数,请在此处运行函数。

#reading the file
for line in file:
line = line.strip()
if ">" in line:
header = line
elif (len(line) == 0):
continue
else:
seq = line
#run function for each line, each time.
rc = reverse_complement(seq)
print(rc)

在前面的代码中,所有的迭代都是成功的。但是你没有把这一行放到每次运行的函数中。在之前的代码中,毕竟是迭代,只分配了最后一行。因此你把函数的最后一行放在最后。这就是为什么你的代码只打印一行。

解决方案。

from itertools import repeat
#reverse complement
def reverse_complement(seq):
compline = ''
for n in seq:
if n == 'A':
compline += 'T'
elif n == 'T':
compline += 'A'
elif n == 'C':
compline += 'G'
elif n == 'G':
compline += 'C'
return((compline)[::-1])

#opening file
filename = "MySequences.fasta"
file = open(filename, 'r')

#reading the file
for line in file:
line = line.strip()
if ">" in line:
header = line
elif (len(line) == 0):
continue
else:
seq = line
#run each line
rc = reverse_complement(seq)
print(rc)    

还有,这是你的另一个错误。你用seq代替line作为输入。但是,即使你修复了这个问题,由于我之前告诉过你的原因,这段代码也不会工作。

for line in file:
rc = reverse_complement(seq)
print(rc)   

相关内容

  • 没有找到相关文章

最新更新