Home > Net >  Get reverse complement function's outputs in a string
Get reverse complement function's outputs in a string

Time:10-04

I am coding a reverse complement function. I get my outputs in one straight line. How can I get them in a string?

def reverse_complement(seq):
    final_string = ''
    string_reversal = seq[::-1]
    for each_element in string_reversal:
        if each_element == "A":
            (print("T"))
        if each_element == "T":
            (print("A"))
        if each_element == "G":
            (print("C"))
        if each_element == "C":
            (print("G"))

CodePudding user response:

Instead of printing the base, you should add them in a string. And instead of four if blocks, using a dict will be easy and better.

def reverse_complement(seq):
    complement = {'A':'T', 'C':'G', 'G':'C', 'T':'A'}

    r_complement = ""

    for base in reversed(seq):
        r_complement  = complement.get(base, base)
    
    return r_complement
>>> reverse_complement('TCGGGCCC')
GGGCCCGA

CodePudding user response:

Here is the answer for your problem:

def reverse_complement(seq):
string_reversal = seq[::-1]
for each_element in string_reversal:
    if each_element == "A":
        return "T"
    if each_element == "T":
        return "A"
    if each_element == "G":
        return "C"
    if each_element == "C":
        return "G"
  • Related