While-Loop-Multiple-Print GC百分比

时间:2017-05-10 08:41:04

标签: python loops while-loop break

我正在尝试编写一个程序来获取DNA序列中GC的百分比。

print("Enter With Z to finish")
while True:
    sequence_dna = input("Enter with sequence:")
    print("DNA Sequence:\t\t",sequence_dna)
    dna = sequence_dna
    DNA = dna.upper()
    DNAlist = list(DNA)
    CountC = sequence_dna.count("C")
    CountG = sequence_dna.count("G")
    GC = (100*(CountC+CountG)/float(len(dna)))
    print("the percentage of GC is: %.2f"%GC)

我还需要编写一个程序来询问多个DNA字符串,而不仅仅是像我一样。我需要做什么? 程序需要使用break命令结束,并指出哪个DNA序列具有最多的GC。

例如:

In DNA Sequence(0)
Out percentage of DNA Sequence(0)
enter code here
In DNA Sequence(1)
Out percentage of DNA Sequence(1)
enter code
In DNA Sequence(2)
Out percentage of DNA Sequence(2)
   break
   the DNA Sequence(1) have the highest percentage of GC`s

1 个答案:

答案 0 :(得分:0)

这应该有效:

from operator import itemgetter    

print("Enter With 'z' to finish") #must be lowercase 'z'

dna_list = []

while True:
    sequence_dna = input("Enter DNA sequence: ")
    print("DNA Sequence:\t\t",sequence_dna)
    dna = sequence_dna
    DNA = dna.upper()

    dna_list.append(DNA)

    if sequence_dna == 'z':
        break

gc_percentages = []

for sequence in dna_list[:-1]: # Takes all elements of the list except for the empty entry in the last position.

    CountC = sequence.count("C")
    CountG = sequence.count("G")
    GC = str((((CountC + CountG)/len(sequence)) * 100)) + '%'
    print("The percentage of GC content for the sequence in list position %s is: %s"%(dna_list.index(sequence), GC))

    gc_percentages.append((sequence, (((CountC + CountG)/len(sequence)) * 100) ))


sorted_gc_scores = sorted(gc_percentages, key= itemgetter(1), reverse= True)

print ("\nThe sequence with the highest GC percentage of %s is:\n%s" %(str(sorted_gc_scores[0][1]) + '%', sorted_gc_scores[0][0]))

代码运行后,您可以参考dna_list中的哪个序列给出最高的GC百分比。