循环多次打印 GC 百分比



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

print("Enter With Z to finish")
while True:
    sequence_dna = input("Enter with sequence:")
    print("DNA Sequence:tt",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

这应该有效:

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:tt",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 百分比最高。

最新更新