下面是我的Rosalind问题的代码,用于计算GC含量,而不使用Biopython。有没有人能给我一些改进的建议?例如,我不能将seq_list中的最后一个序列包含在For循环中,因此必须再追加一次。另外,有没有更好的方法来配对seq_name和GC内容,这样我就可以轻松地打印出GC含量最高的序列名?非常感谢
# to open FASTA format sequence file:
s=open('5_GC_content.txt','r').readlines()
# to create two lists, one for names, one for sequences
name_list=[]
seq_list=[]
data='' # to put the sequence from several lines together
for line in s:
line=line.strip()
for i in line:
if i == '>':
name_list.append(line[1:])
if data:
seq_list.append(data)
data=''
break
else:
line=line.upper()
if all([k==k.upper() for k in line]):
data=data+line
seq_list.append(data) # is there a way to include the last sequence in the for loop?
GC_list=[]
for seq in seq_list:
i=0
for k in seq:
if k=="G" or k=='C':
i+=1
GC_cont=float(i)/len(seq)*100.0
GC_list.append(GC_cont)
m=max(GC_list)
print name_list[GC_list.index(m)] # to find the index of max GC
print "{:0.6f}".format(m)发布于 2015-01-08 07:53:23
if all([k==k.upper() for k in line]):你为什么不直接检查一下那个line == line.upper()呢?
i=0
for k in seq:
if k=="G" or k=='C':
i+=1可以替换为i=sum(如果'G‘中的k,’C‘中的k,则序列中的k为1)
有没有办法在for循环中包含最后一个序列?
我认为没有更好的方法来做到这一点。
发布于 2015-03-04 11:16:00
要避免第二次追加您的seq列表,请删除:
if all([k==k.upper() for k in line]):
data=data+line并将其添加到line.strip()下面
您面临的问题是,当您第一次进入for i in line循环时,数据是一个空字符串。因此,if data:是假的。
https://stackoverflow.com/questions/27830401
复制相似问题