Python DNA序列中的GC内容(Rosaland):如何改进我的代码?

Python DNA序列中的GC内容(Rosaland):如何改进我的代码?,python,Python,下面是我在不使用Biopython的情况下计算GC含量的Rosalind问题的代码。 谁能给我一些改进的建议吗?例如,我不能在For循环中包含seq_列表中的最后一个序列,而必须再追加一次。 另外,是否有更好的方法将seq_name和GC内容配对,以便我可以轻松打印出具有最高GC内容的序列名? 多谢各位 # to open FASTA format sequence file: s=open('5_GC_content.txt','r').readlines() # to create two

下面是我在不使用Biopython的情况下计算GC含量的Rosalind问题的代码。 谁能给我一些改进的建议吗?例如,我不能在For循环中包含seq_列表中的最后一个序列,而必须再追加一次。 另外,是否有更好的方法将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)
你为什么不检查一下那行==line.upper

可以替换为 如果k在['G','C']

有没有办法在for循环中包含最后一个序列


我认为没有更好的方法可以做到这一点。

为了避免第二次添加您的seq列表,请删除:

if all([k==k.upper() for k in line]):
    data=data+line
并将其添加到line.strip下面

您面临的问题是,第一次输入FORI in line循环时,数据是一个空字符串。因此,如果data:为False

i=0
for k in seq:
    if k=="G" or k=='C':
        i+=1
if all([k==k.upper() for k in line]):
    data=data+line