Rosalind 简介和共识:将长字符串写入 Python 中的一行(格式化)
Rosalind Profile and Consensus: Writing long strings to one line in Python (Formatting)
我正在尝试解决 Rosalind 上的一个问题,给定一个最多包含 10 个 1kb 序列的 FASTA 文件,我需要给出共有序列和概况(所有序列中每个碱基有多少个)每个核苷酸共有)。在格式化我的响应的上下文中,我所拥有的代码适用于小序列(已验证)。
但是,当涉及到大序列时,我在格式化我的回复时遇到了问题。
无论长度如何,我期望 return 是:
"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")
缺点("rosalind_cons.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))]
您不小心复制了您的数据。尝试将其替换为:
writing_strings=[ACGT[i] + str(ACGT[i]) for i in range(0,len(ACGT))]
然后将其写入您的输出文件,如下所示:
D.write(writing_strings[i][1:-1])
这是一种从列表中删除括号的懒惰方法。
我正在尝试解决 Rosalind 上的一个问题,给定一个最多包含 10 个 1kb 序列的 FASTA 文件,我需要给出共有序列和概况(所有序列中每个碱基有多少个)每个核苷酸共有)。在格式化我的响应的上下文中,我所拥有的代码适用于小序列(已验证)。
但是,当涉及到大序列时,我在格式化我的回复时遇到了问题。 无论长度如何,我期望 return 是:
"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")
缺点("rosalind_cons.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))]
您不小心复制了您的数据。尝试将其替换为:
writing_strings=[ACGT[i] + str(ACGT[i]) for i in range(0,len(ACGT))]
然后将其写入您的输出文件,如下所示:
D.write(writing_strings[i][1:-1])
这是一种从列表中删除括号的懒惰方法。