我有以下代码,它读取一个包含10个基因序列的FASTA文件,并将每个序列作为一个矩阵返回。然而,代码似乎在最后一个序列中丢失了,我想知道为什么?
file=open('/Users/vivianspro/Downloads/rosalind_cons (5).txt', 'r')
line=file.readline()
strings = []
sequence=''
while line:
#line=line.rstrip('\n')
line = line.strip() #empty () automatically strips the \n
if '>' in line:
if sequence != "":
strings.append(sequence)
sequence = ""
#sequence=line
else:
sequence+=line
line=file.readline()
for s in strings:
print(s)
Motifs = []
for seq in strings:
Motifs.append(list(seq))
#make every symbol into an element in the list separated by ,
for s in Motifs:
print(s) ````发布于 2019-02-08 14:14:41
仅当您看到新的>,但在最后一个序列之后没有一个时,才会附加到strings。
这里有一个重构,希望它也能更地道一些。
strings = []
sequence=''
with open('/Users/vivianspro/Downloads/rosalind_cons (5).txt', 'r') as file:
for line in file:
line = line.rstrip('\n')
if line.startswith('>'):
if sequence != "":
strings.append(sequence)
sequence = ""
else:
sequence+=line
# After the last iteration, append once more if we have something to append
if sequence:
strings.append(sequence)发布于 2019-02-08 14:02:11
由于FASTA文件包含以下格式的数据:
>ID1
seq_1
>ID2
seq_2
...根据您的代码,如果您的行只包含一个>,那么您可以尝试附加该序列。这意味着,当您为ID_2迭代时,将为ID_1添加序列。
为了解决这个问题,你可以这样做:
for line in file:
line = line.strip()
if '>' in line: # Line 1
line = file.readline().strip()
# print(line)
strings.append(line)上面的示例使用了这样一个事实:在FASTA文件中,序列紧跟在ID之后,ID包含>字符(您可以更改第1行,以便它只检查第一个字符line[0] == ">")。
https://stackoverflow.com/questions/54586663
复制相似问题