How to Reverse Complement of DNA Sequence in Python

Renesh Bedre    1 minute read

The reverse complement of a DNA sequence involves reversing the sequence and substituting each nucleotide with its complementary base. For example, if the original sequence is “TGATTAATTGG,” the reverse complement would be “CCAATTAATCA”.

The examples below shows how to get the reverse complement of a DNA sequence using Python

Example 1

The rev_com() function from bioinfokit package v2.1.1 can be used to get reverse complement of a DNA sequence

# example sequence TGATTAATTGG
# load package
from bioinfokit.analys import Fasta

# perform reverse complement
Fasta.rev_com(seq="TGATTAATTGG")

Output:

CCAATTAATCA

Example 2

The rev_com() function from bioinfokit package v2.1.1 can also be used for getting the reverse complement of DNA sequences from FASTA file

For example, the input.fasta file contains the following two sequences,

# input.fasta
>1
ATGGGAAAC
TGGAGGAAA
>2
TGAAACCTT

Now, perform the reverse complement of DNA sequences from the the FASTA file,

# load package
from bioinfokit.analys import Fasta

# perform reverse complement
Fasta.rev_com(file="input.fasta")

Output (the output FASTA file will be saved in the same directory with name output_revcom.fasta ):

>1
TTTCCTCCAGTTTCCCAT
>2
AAGGTTTCA

Example 3

The reverse_complement() function from Biopython can be used to get reverse complement of a DNA sequence

# example sequence TGATTAATTGG
# load package
from Bio.Seq import Seq

# perform reverse complement
print(Seq("TGATTAATTGG").reverse_complement())

Output:

CCAATTAATCA

Enhance your skills with courses on genomics and bioinformatics


This work is licensed under a Creative Commons Attribution 4.0 International License

Some of the links on this page may be affiliate links, which means we may get an affiliate commission on a valid purchase. The retailer will pay the commission at no additional cost to you.