Python-需要匹配来自2个输入的DNA

时间:2014-04-18 13:45:16

标签: python-3.x

目前正在开展一个项目以执行以下操作:

  • 要求用户输入第一行dna
  • 检查以确保链有效(A,C,T和G,也接受小写)。
  • 输入第一条链后,程序会询问第二条链并检查是否为
    互补
  • A是T,T与A的互补,G与C和C与G互补。
  • 结果应该是:

输入dna的第一个序列:ATGC
输入第二个dna序列:TACG
“他们是互补的”

输入dna的第一个序列:GTC
输入第二个dna序列:GAC
没有补充。

输入第二个dna序列:退出
退出计划.....

#Code starts here:

def compare_DNA_lenght(dna1,dna2):  

    if len(dna1) != len(dna2):
        print ('Input sequence incorrect') 

def complement(sequence):
    """ (str) -> str"""

    replace={'a':'t','t':'a','c':'g','g':'c'}
    complement=''
    print('in the loop',sequence)
    for i in sequence:
        complement=complement+replace[i]

    print(sequence)

while (dna1 != "exit" and dna2 != "exit"):

    dna1= input('Please enter your first sequence:')
    dna2= input('Please enter your second sequence:')

    dna1=dna1.lower() 
    dna2=dna2.lower()

    if (dna1 =="exit" and dna2 =="exit"):
        print ("Exiting program")

    if(dna2 == complement):
        print ("They are complementary")

    elif(dna2 != complement):
        print ("Not a complementary strand")

    print (complement)

2 个答案:

答案 0 :(得分:1)

您应该阅读how while loops work in Python。与此同时,这里有一个基本结构:

while condition:
    code

请注意,您应始终确保condition最终评估为false,否则您将无限循环,导致程序挂起。一个简单的例子是打印数字15

i = 1
while (i <= 5):
    print(i)
    i = i + 1

最终,i6,因此while循环不会执行内部代码,因为6不等于或小于5

while循环之前,您需要声明dna1dna2个变量,这样您的程序就不会抛出错误,说它无法找到这些变量。

dna1= input('Please enter your first sequence:')
dna2= input('Please enter your second sequence:')
    while (dna1 != "exit" and dna2 != "exit"):

此外,您不需要检查两个字符串是否说&#34;退出&#34;打破循环。只需一个即可。

if (dna1 =="exit" or dna2 =="exit"):

在不相关的说明中,正确拼写您的方法名称被视为良好做法。 &#34; compare_DNA_lenght&#34;应该是&#34; compare_DNA_length&#34;。

答案 1 :(得分:1)

  

检查有效的DNA链

因此,您需要做的第一件事就是检查使用的输入是否是有效的DNA。由于DNA只包含A,T,G和C,我们可以通过创建检查A,T,G和C的公式来检查有效性。如果存在,则为真。如果除了A,T,G和A以外的任何字母,则为False下进行。

def is_dna_strand(x):

"""

(str) -> bool
Returns True if the DNA strand entered contains only the DNA bases.

>>> is_dna_strand("AGTC")
'True'
>>> is_dna_strand("AGHFJ")
'False'

"""
    a=x.lower()
i=0
while i != (len(a)):
    if ((a[i])!= "a") and ((a[i])!="t")and((a[i])!="c")and((a[i])!="g"):
        return False
    i+=1

return True
  

检查碱基对

def is_base_pair(x,y):

"""
(str,str) -> bool
Returs true if two parameters form a base pair.

>>> is_base_pair("A","T")
'True'
>>> is_base_pair("A","G")
'False'
"""

    a=x.lower()
    b=y.lower()

    if a=="a" and b=="t":
        return True
    elif a=="t" and b=="a":
        return True
    elif a=="c" and b=="g":
        return True
    elif a=="g" and b=="c":
        return True
    else:
        return False
  

检查它是否是有效的DNA。   因此,这个过程的最后一步是检查整个分子的有效性。

def is_dna(x,y):

"""

(str,str) -> bool
Returs true if the two strands form a properly base-paired DNA.

>>> is_dna("AAGTC","TTCAG")
'True'
>>> is_dna("TCGA","TCAG")
'False'

"""
i=0
j=0
if len(x)==len(y):
    while i < (len(x)):
        b=x[i]
        c=y[i]
        i=i+1
        return is_base_pair(b,c)
else:
    return False
相关问题