Simultaneous .replace functionality

后端 未结 4 1400
一生所求
一生所求 2020-12-19 06:52

I have already converted user input of DNA code (A,T,G,C) into RNA code(A,U,G,C). This was fairly easy

RNA_Code=DNA_Code.replace(\'         


        
相关标签:
4条回答
  • 2020-12-19 07:22

    I have a simple solution:

    # get the sequence from the user:
    
    dna_seq = input("Please enter your sequence here: ")
    
    # make a for loop to read the seq one nucleotide at a time and add each one in a new variable
    
    compliment = ""
    
    for n in dna_seq:
    
        if n == "A":
            compliment = compliment + "T"
        elif n == "T":
            compliment = compliment + "A"
        elif n == "G":
            compliment = compliment + "C"
        elif n == "C":
            compliment = compliment + "G"
    
    print(compliment)
    
    0 讨论(0)
  • 2020-12-19 07:33

    Use a translation table:

    RNA_compliment = {
        ord('A'): 'U', ord('U'): 'A',
        ord('G'): 'C', ord('C'): 'G'}
    
    RNA_Code.translate(RNA_compliment)
    

    The str.translate() method takes a mapping from codepoint (a number) to replacement character. The ord() function gives us a codepoint for a given character, making it easy to build your map.

    Demo:

    >>> RNA_compliment = {ord('A'): 'U', ord('U'): 'A', ord('G'): 'C', ord('C'): 'G'}
    >>> 'AUUUGCGGCAAA'.translate(RNA_compliment)
    'UAAACGCCGUUU'
    
    0 讨论(0)
  • 2020-12-19 07:34

    If you're not already using it, I suggest trying out Biopython. It has all sorts of functions for dealing with biological data, including a pretty cool Seq object. There is a reverse_complement() function that does exactly what you're trying to do, and a bunch more that you might not even have thought of yet. Check it out, it's a real time-saver.

    >>> from Bio.Seq import Seq
    >>> from Bio.Alphabet import generic_dna
    >>> my_dna = Seq("AGTACACTGGT", generic_dna)
    >>> my_dna
    Seq('AGTACACTGGT', DNAAlphabet())
    >>> my_dna.complement()
    Seq('TCATGTGACCA', DNAAlphabet())
    >>> my_dna.reverse_complement()
    Seq('ACCAGTGTACT', DNAAlphabet())
    
    0 讨论(0)
  • 2020-12-19 07:38

    You can use a mapping dictionary:

    In [1]: dic={"A":"U","U":"A","G":"C","C":"G"}
    
    In [2]: strs="AUUUGCGGCAAA"
    
    In [3]: "".join(dic[x] for x in strs)
    Out[3]: 'UAAACGCCGUUU'
    
    0 讨论(0)
提交回复
热议问题