我正在努力解决Rosalind的一个问题,在1kb的FASTA文件中最多有10个序列,我需要给出共有序列和概况(所有序列中每个碱基共有多少个共同点)每个核苷酸)。在格式化我的响应的上下文中,我的代码适用于小序列(已验证)。
但是,在涉及大型序列时,我在格式化响应方面存在问题。 无论长度如何,我期望返回的是:
"consensus sequence"
"A: one line string of numbers without commas"
"C: one line string """" "
"G: one line string """" "
"T: one line string """" "
全部彼此对齐并在各自的行上对齐,或者至少有一些格式允许我将此格式作为一个单元向前进行,以保持对齐的完整性。
但是当我为一个大序列运行我的代码时,我得到每个单独的字符串低于由换行符分解的共识序列,大概是因为字符串本身太长了。我一直在努力想办法绕过这个问题,但我的搜索却毫无结果。我正在考虑一些迭代编写算法,它可以写出上述期望的全部内容,但是在块中任何帮助都会非常感激。为了完整起见,我在下面附上了我的全部代码,并根据需要使用了块注释,尽管是主要部分。
def cons(file):
#returns consensus sequence and profile of a FASTA file
import os
path = os.path.abspath(os.path.expanduser(file))
with open(path,"r") as D:
F=D.readlines()
#initialize list of sequences, list of all strings, and a temporary storage
#list, respectively
SEQS=[]
mystrings=[]
temp_seq=[]
#get a list of strings from the file, stripping the newline character
for x in F:
mystrings.append(x.strip("\n"))
#if the string in question is a nucleotide sequence (without ">")
#i'll store that string into a temporary variable until I run into a string
#with a ">", in which case I'll join all the strings in my temporary
#sequence list and append to my list of sequences SEQS
for i in range(1,len(mystrings)):
if ">" not in mystrings[i]:
temp_seq.append(mystrings[i])
else:
SEQS.append(("").join(temp_seq))
temp_seq=[]
SEQS.append(("").join(temp_seq))
#set up list of nucleotide counts for A,C,G and T, in that order
ACGT= [[0 for i in range(0,len(SEQS[0]))],
[0 for i in range(0,len(SEQS[0]))],
[0 for i in range(0,len(SEQS[0]))],
[0 for i in range(0,len(SEQS[0]))]]
#assumed to be equal length sequences. Counting amount of shared nucleotides
#in each column
for i in range(0,len(SEQS[0])-1):
for j in range(0, len(SEQS)):
if SEQS[j][i]=="A":
ACGT[0][i]+=1
elif SEQS[j][i]=="C":
ACGT[1][i]+=1
elif SEQS[j][i]=="G":
ACGT[2][i]+=1
elif SEQS[j][i]=="T":
ACGT[3][i]+=1
ancstr=""
TR_ACGT=list(zip(*ACGT))
acgt=["A: ","C: ","G: ","T: "]
for i in range(0,len(TR_ACGT)-1):
comp=TR_ACGT[i]
if comp.index(max(comp))==0:
ancstr+=("A")
elif comp.index(max(comp))==1:
ancstr+=("C")
elif comp.index(max(comp))==2:
ancstr+=("G")
elif comp.index(max(comp))==3:
ancstr+=("T")
'''
writing to file... trying to get it to write as
consensus sequence
A: blah(1line)
C: blah(1line)
G: blah(1line)
T: blah(line)
which works for small sequences. but for larger sequences
python keeps adding newlines if the string in question is very long...
'''
myfile="myconsensus.txt"
writing_strings=[acgt[i]+' '.join(str(n) for n in ACGT[i] for i in range(0,len(ACGT))) for i in range(0,len(acgt))]
with open(myfile,'w') as D:
D.writelines(ancstr)
D.writelines("\n")
for i in range(0,len(writing_strings)):
D.writelines(writing_strings[i])
D.writelines("\n")
cons(“rosalind_cons.txt”)
答案 0 :(得分:0)
你的代码完全没问题,除了这一行:
writing_strings=[acgt[i]+' '.join(str(n) for n in ACGT[i] for i in range(0,len(ACGT))) for i in range(0,len(acgt))]
您不小心复制了数据。尝试将其替换为:
writing_strings=[ACGT[i] + str(ACGT[i]) for i in range(0,len(ACGT))]
然后将其写入输出文件,如下所示:
D.write(writing_strings[i][1:-1])
这是摆脱列表中括号的懒惰方式。