Method 1: Verify if a sequence is DNA and RNA
Step 1:
In the set method, we convert the input sequence into a set. We combine the set obtained with a reference DNA set(ATGC) or RNA set(AUGC) using the union function of the set. This is done so that the input sequence is rendered valid even if it does not contain all four types of nucleotide bases. For instance, TTTTTTTAAA is a valid DNA even though it contains only two types of bases. Also, UUUUUUUUGGG is a valid RNA.
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" seq1 = "ATGCAGCTGTGTTACGCGAT" seq2 = "UGGCGGAUAAGCGCA" seq3 = "TYHGGHHHHH" print (seq1 + " is " + verify(seq1)) print (seq2 + " is " + verify(seq2)) print (seq3 + " is " + verify(seq3)) |
Output:
ATGCAGCTGTGTTACGCGAT is DNA UGGCGGAUAAGCGCA is RNA TYHGGHHHHH is Invalid sequence
Step 2:
This function returns a reverse complement of a DNA or RNA strand.
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_st(seq): '''This function returns a reverse complement of a DNA or RNA strand''' verified = verify(seq) if verified = = "DNA" : # complement strand seq = seq.replace( "A" , "t" ).replace( "C" , "g" ).replace( "T" , "a" ).replace( "G" , "c" ) seq = seq.upper() # reverse strand seq = seq[:: - 1 ] return seq elif verified = = "RNA" : # complement strand seq = seq.replace( "A" , "u" ).replace( "C" , "g" ).replace( "U" , "a" ).replace( "G" , "c" ) seq = seq.upper() # reverse strand seq = seq[:: - 1 ] return seq else : return "Invalid sequence" # test variables seq1 = "ATGCAGCTGTGTTACGCGAT" seq2 = "UGGCGGAUAAGCGCA" seq3 = "TYHGGHHHHH" print ( "The reverse complementary strand of " + seq1 + " is " + rev_comp_st(seq1)) print ( "The reverse complementary strand of " + seq2 + " is " + rev_comp_st(seq2)) print ( "The reverse complementary strand of " + seq3 + " is " + rev_comp_st(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