Use of if statement
Another method of finding a complementary sequence of DNA or RNA is the use of if statements. The sequence is first verified if it is DNA or RNA. If a sequence is DNA, All instances of A are replaced by T, all instances of T are replaced by A, all instances of G are replaced by C and all instances of C are replaced by G.
Python3
def verify(sequence): '''This code verifies if a sequence is a DNA or RNA''' # set the input sequence seq = set (sequence) # confirm if its elements is equal to # the set of valid DNA bases # Use a union method to ensure the # sequence is verified if does not # contain all the bases if seq = = { "A" , "T" , "C" , "G" }.union(seq): return "DNA" elif seq = = { "A" , "U" , "C" , "G" }.union(seq): return "RNA" else : return "Invalid sequence" def rev_comp_if(seq): comp = [] if verify(seq) = = "DNA" : for base in seq: if base = = "A" : comp.append( "T" ) elif base = = "G" : comp.append( "C" ) elif base = = "T" : comp.append( "A" ) elif base = = "C" : comp.append( "G" ) elif verify(seq) = = "RNA" : for base in seq: if base = = "U" : comp.append( "A" ) elif base = = "G" : comp.append( "C" ) elif base = = "A" : comp.append( "U" ) elif base = = "C" : comp.append( "G" ) else : return "Invalid Sequence" # reverse the sequence comp_rev = comp[:: - 1 ] # convert list to string comp_rev = "".join(comp_rev) return comp_rev seq1 = "ATGCAGCTGTGTTACGCGAT" seq2 = "UGGCGGAUAAGCGCA" seq3 = "TYHGGHHHHH" print ( "The reverse complementary strand of " + seq1 + " is " + rev_comp_if(seq1)) print ( "The reverse complementary strand of " + seq2 + " is " + rev_comp_if(seq2)) print ( "The reverse complementary strand of " + seq3 + " is " + rev_comp_if(seq3)) |
Output:
The reverse complementary strand of ATGCAGCTGTGTTACGCGAT is ATCGCGTAACACAGCTGCAT
The reverse complementary strand of UGGCGGAUAAGCGCA is UGCGCUUAUCCGCCA
The reverse complementary strand of TYHGGHHHHH is Invalid Sequence
Reverse complement of DNA strand using Python
In this article, we will cover, how to Reverse the complement of DNA or RNA sequences in Python.
Example:
DNA strand: ATGCCGAGCA Complementary Strand: TACGGCTCGT Reverse-Complementary strand: ACGAGCCGTA