[英]Reverse Compliment of DNA using user Input String
I want to print the reverse compliment of a user input string of DNA.我想打印用户输入的 DNA 字符串的反向补充。 For Background: Input can be any combination of the characters A C G or T. A and T are compliments and G and C are compliments of one another.
对于背景:输入可以是字符 A C G 或 T 的任意组合。A 和 T 是互补的,G 和 C 是彼此的互补。 My goal is to take the user input and print out the reverse compliment of that string.
我的目标是获取用户输入并打印出该字符串的反向补充。
So far I have been able to reverse the user input string, but I am unsure of how to apply the compliment.到目前为止,我已经能够反转用户输入字符串,但我不确定如何应用赞美。 Any help would be greatly appreciated!
任何帮助将不胜感激!
print("Enter a DNA string:")
string = str(input("S> "))
reverse_string = (string[::-1])
print(reverse_string)
My code so far prints out the user input string reversed, but not the compliment.到目前为止,我的代码打印出反转的用户输入字符串,但不是恭维。
Example input:示例输入:
S> AAAACCCGGT S> AAAACCCGGT
My output TGGCCCAAAA我的 output TGGCCCAAAA
Desired Output ACCGGGTTTT所需 Output ACCGGGTTTT
Just use a dict
to map chars to their complements, apply the mapping to the reverse string and str.join
the chars back together:只需对 map 字符使用
dict
到它们的补码,将映射应用于反向字符串并将str.join
字符重新组合在一起:
# complement mapping
c = {'A': 'T', 'T': 'A', 'G': 'C', 'C': 'G'}
s = 'AAAACCCGGT' # do not shadow string module
rev_comp = ''.join(map(c.get, s[::-1]))
# 'ACCGGGTTTT'
The compliment part can be done via a mapping of A -> T (and T -> A) and similarly for G, C.补充部分可以通过 A -> T(和 T -> A)的映射来完成,对于 G,C 类似。 Use a comprehension to build the compliment string and then reverse it.
使用推导来构建赞美字符串,然后将其反转。
dna = 'AAAACCCGGT'
compliment_mapping = {'A': 'T', 'T': 'A', 'G': 'C', 'C': 'G'}
compliment = ''.join(compliment_mapping[c] for c in dna)
reverse_compliment = compliment[::-1]
print(reverse_compliment)
Output: Output:
ACCGGGTTTT
You can use a dictionary to store the compliment relationship.您可以使用字典来存储恭维关系。 In addition, the use of
reversed
avoids creating a reversed list in the memory, reducing the memory footprint.此外,使用
reversed
可避免在 memory 中创建反向列表,从而减少 memory 占用空间。
print("Enter a DNA string:")
compliment = {'A': 'T', 'T': 'A', 'G': 'C', 'C': 'G'}
while True:
dna = str(input("S> "))
reverse_dna = ''.join(compliment[bp] for bp in reversed(dna))
print(reverse_dna)
Here is the output.这是 output。
Enter a DNA string:
S> AAAACCCGGT
ACCGGGTTTT
S> ACCGGGTTTT
AAAACCCGGT
You can see that the reverse compliment of the reverse compliment is the original DNA string.你可以看到反向恭维的反向恭维是原始的 DNA 字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.