简体   繁体   English

补充角色

[英]Getting complement of a character

How can't the codes below work, in order to get the complement of the character entered? 下面的代码如何才能工作,以获取输入字符的补码? It seems like the loop never end, but let say, if I enter 'Z' as dna, why wouldn't it break and quit? 循环似乎永无止境,但可以说,如果我输入“ Z”作为dna,为什么不中断并退出? Did I use the break or if wrongly? 我是使用休息时间还是错误地使用了? How about elif? Elif呢?

def get_complement(dna): def get_complement(dna):

''' (ch) -> ch

Reverse the 'A' to 'T' or vice versa and 'C' to 'G' and vice versa too.
>>> get_complement('A')
'C'
>>> get_complement('G')
'T'

'''
if dna == 'A':
    print ('C')
    if dna == 'C':
        print ('A')
        if dna == 'T':
            print ('G')
            if dna == 'G' :
                print ('T')
                while  {'A', 'C', 'G', 'T'}.isnotsubset(set(dna)) :
                    break
                return ('')

You should set up a map, using a dictionary 您应该使用dictionary来设置地图

complement = {'A': 'C', 'C': 'A', 'T': 'G', 'G': 'T'}

Then for some string you can do 然后可以为一些字符串做

original = "ATCGTCA"
"".join(complement[letter] for letter in original)

Output 输出量

'CGATGAC'

For just a single character: 对于一个字符:

complement['A']

Output 输出量

'C'

As your example is written (and as Cyber has written his answer based on your example) you are not getting the complement. 当您编写示例时(正如赛博根据您的示例编写了答案一样),您并没有获得补充。 You're getting A -> C (instead of the complement T), T -> G instead of A, etc. 您得到的是A-> C(而不是补码T),T-> G而不是A,依此类推。

Using a dictionary as Cyber has done, it should look like this: 像Cyber​​一样使用字典,它应该像这样:

complement = {'A':'T', 'T':'A', 'C':'G', 'G':'C'}

And in code, including a check for non-DNA characters: 在代码中,包括检查非DNA字符:

original = "ATCGTCA"
bad_original = "ATCGTCAZ"

complement = {'A':'T', 'T':'A', 'C':'G', 'G':'C'}
for dna in (original, bad_original):
    try:
        output = "".join([complement[x] for x in dna])
    except KeyError:
        output = "Contains non-DNA characters"

    print output

Where "original" yields "TAGCAGT" and "bad_original" yields "Contains non-DNA characters". 其中“原始”产生“ TAGCAGT”,而“ bad_original”产生“包含非DNA字符”。

Note that this is complement, not the reverse complement, which is usually of more interest. 请注意,这是补码,而不是反向补码,通常会引起更多关注。

More generally, if you are planning on using this for sequences of DNA, you should probably look into the BioPython module ( http://biopython.org/wiki/Seq#Complement_and_reverse_complement ), which will get you complement (and reverse complement) with more versatility, error checking, etc. 更一般而言,如果您打算将其用于DNA序列,则可能应该研究BioPython模块( http://biopython.org/wiki/Seq#Complement_and_reverse_complement ),它将为您提供补充(和反向补充)更多的功能,错误检查等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM