Python for bioinformatics: Getting started with sequence analysis in Python
Tags: python, bioinformatics, biopython, dnasequencing
Published: 2020-06-15 08:21:50

---
title: Python for bioinformatics: Getting started with sequence analysis in Python
published: true
date: 2020-02-28 07:36:35 UTC
tags: python,bioinformatics,biopython,dnasequencing
canonical_url: https://itnext.io/python-for-bioinformatics-getting-started-with-sequence-analysis-in-python-e18ce8c4be8
---

![](https://cdn-images-1.medium.com/max/560/0*VBZXiHbzMGvRravE.png)

In this post, I am going to discuss how Python is being used in the field of bioinformatics and how you can use it to analyze sequences of DNA, RNA, and proteins. Yeah, Python is being used by biologists as well.

Before I get into coding, I’d like to give a brief background of bioinformatics and related things.

### What is bioinformatics?

According to Wikipedia:

> Bioinformatics is an interdisciplinary field that develops methods and software tools for understanding biological data. As an interdisciplinary field of science, bioinformatics combines biology, computer science, information engineering, mathematics and statistics to analyze and interpret biological data. Bioinformatics has been used for in silico analyses of biological queries using mathematical and statistical techniques.

Bioinformatics is the field which is a combination of two major fields: Biological data ( _sequences and structures of proteins, DNA, RNAs, and others_ ) and Informatics ( _computer science, statistics, maths, and engineering_). This is why it is called an _interdisciplinary field. The common_ use of bioinformatics includes the identification of genes and SNPs.

The goal of bioinformatics are:

- Sequence Alignment.
- Gene finding.
- Drug discovery and drug designing.
- Protein structure alignment and prediction. etc.

As you have figured out, bioinformatics is a huge field that contains different areas and relevant operations. In this introductory post, we are discussing in brief about sequence analysis.

### What is Sequence Analysis?

According to Wikipedia:

> In bioinformatics, sequence analysis is the process of subjecting a DNA, RNA or peptide sequence to any of a wide range of analytical methods to understand its features, function, structure, or evolution.

_Sequencing_ is the process of finding the primary structure whether it is DNA, RNA.

DNA Sequencing is all about determining the _nucleic acid sequence_. It helps to find out the order of the four bases: **adenine (A)**, **guanine (G)**, **cytosine ©** and **thymine (T)**, in a strand of DNA.

Sequencing is done with the help of sequence machines. Once sequenced, the data is then available for further processing. A textual example of a DNA sequence looks like below:

**_DNA sequencing_** is the method of determining the order of nucleotide in a DNA.

**_RNA sequencing_** is the method to find the quantity of RNA in a biological sample.

**_Protein sequencing_** is the method of determining the amino acid sequence of all or part of a protein or peptide.

OK, enough theory, it’s time to get into code.

### Biopython Development Setup

We are going to use [Biopython](https://biopython.org/), the library for dealing with biological computation and analysis of sequences. To install it you can either use pip or conda , in case you are using Conda based distribution.

pip install biopython

You can check the version as well:

**1.74** as of writing this post.

### Reading Sequence in FASTA format

OK, so we are going to read a DNA sequence that is available in **FASTA** format. FASTA is a text-based format to represent different sequences which are represented in single-letter codes. A typical FASTA format looks like below:

Comments are added after semicolons. Each sequence is separated by a > symbol. The example given above has 3 sequences given in a single FASTA file. This is just a sample otherwise it is not necessary.

We are going to read a sample FASTA file, given [here](https://raw.githubusercontent.com/biopython/biopython/master/Doc/examples/ls_orchid.fasta), which is the sequence of [lady slipper orchids](https://en.wikipedia.org/wiki/Cypripedioideae).

When it runs, it shows output like below:

The very first line of the sequence appears in the form of **id**. The text after each **>** then appears as a sequence.

FASTA is not the only format available. There is another format available, called, GeneBank, having a file extension .gbk.

No change in code as such other than file and file type. The rest of the API is the same across the file format. If I run the above program it prints like below:

You can also mention sequence type by using _Alphabet_. By default, it will be SingleLetterAlphabet()

I imported generic\_dna type and used it in parse() method. When it runs, it prints the following output:

Seq('CGTAACAAGGTTTCCGTAGGTGGACCTCCGGGAGGATCATTGTTGAGATCACAT...GCA', DNAAlphabet())

As you can read DNAAlphabet() is printed now.

### Calculating GC Content

Biopython provides an easy way to calculate **_GC Content(guanine-cytosine content)_**.

You imported the GC method from the utility classes and passed the actual sequence object in it. On execution, it prints the numeric percentage.

### Complement and Reverse Complement

The two antiparallel strands (+ve or 5'-> 3' &-ve or 3'-> 5') are complementary to each other in a DNA molecule.

A nucleotide sequence can be reverse complemented to get a new sequence. Similarly, the complemented sequence can be reverse complemented to get the original sequence. Biopython provides two methods to do this functionality. In Biopython it is very easy to get both of a sequence.

When it runs it prints the following output:

### Conclusion

So this was a brief intro to bioinformatics and Biopython. It is way more than that. You can also apply machine learning methods to[find a new drug](https://newatlas.com/medical/powerful-new-antibiotic-ai-discovery/) for saving lives. Hopefully, I will be writing more on this subject, provided if it’s liked by you 🙂

_Originally published at_ [_http://blog.adnansiddiqi.me_](http://blog.adnansiddiqi.me/python-for-bioinformatics-getting-started-with-sequence-analysis-in-python/) _on February 28, 2020._

* * *