Newbies Guide to Biopython — Part 1
By: Vijini Mallawaarachchi
Tags: Coding, Data Science, Bioinformatics, Programming, Python

Newbies Guide to Biopython — Part 1

A simple guide to common functions of Biopython with examples and code

When you hear the word Biopython what is the first thing that came to your mind? A python library to handle biological data…? You are correct! Biopython provides a set of tools to perform bioinformatics computations on biological data such as DNA data and protein data. I have been using Biopython ever since I started studying bioinformatics and it has never let me down with its functions. It is an amazing library which provides a wide range of functions from reading large files with biological data to aligning sequences. In this article, I will introduce you to some basic functions of Biopython which can make implementations much easier with just a single call.

Background image by Clker-Free-Vector-Images from Pixabay

Getting started

The latest version available when I’m writing this article is biopython-1.77 released in May 2020.

You can install Biopython using pip

pip install biopython

or using conda.

conda install -c conda-forge biopython

You can test whether Biopython is properly installed by executing the following line in the python interpreter.

import Bio

If you get an error such as ImportError: No module named Bio then you haven’t installed Biopython properly in your working environment. If no error messages appear, we are good to go.

In this article, I will be walking you through some examples where Seq , SeqRecord and SeqIO come in handy. We will go through the functions that perform the following tasks.

Creating a sequence Get the reverse complement of a sequence Count the number of occurrences of a nucleotide Find the starting index of a subsequence Reading a sequence file Writing sequences to a file Convert a FASTQ file to FASTA file Separate sequences by ids from a list of ids

1. Creating a sequence

To create your own sequence, you can use the Biopython Seq object. Here is an example.

>>> from Bio.Seq import Seq

>>> my_sequence = Seq("ATGACGTTGCATG")

>>> print("The sequence is", my_sequence)

The sequence is ATGACGTTGCATG

>>> print("The length of the sequence is", len(my_sequence))

The length of the sequence is 13

2. Get the reverse complement of a sequence

You can easily get the reverse complement of a sequence using a single function call reverse_complement() .

>>> print("The reverse complement if the sequence is", my_sequence.reverse_complement())

The reverse complement if the sequence is CATGCAACGTCAT

3. Count the number of occurrences of a nucleotide

You can get the number of occurrence of a particular nucleotide using the count() function.

>>> print("The number of As in the sequence", my_sequence.count("A"))

The number of As in the sequence 3

4. Find the starting index of a subsequence

You can find the starting index of a subsequence using the find() function.

>>> print("Found TTG in the sequence at index", my_sequence.find("TTG"))

Found TTG in the sequence at index 6

5. Reading a sequence file

Biopython’s SeqIO (Sequence Input/Output) interface can be used to read sequence files. The parse() function takes a file (with a file handle and format) and returns a SeqRecord iterator. Following is an example of how to read a FASTA file.

from Bio import SeqIO for record in SeqIO.parse("example.fasta", "fasta"):

print(record.id)

record.id will return the identifier of the sequence. record.seq will return the sequence itself. record.description will return the sequence description.

6. Writing sequences to a file

Biopython’s SeqIO (Sequence Input/Output) interface can be used to write sequences to files. Following is an example where a list of sequences are written to a FASTA file.

from Bio import SeqIO

from Bio.SeqRecord import SeqRecord

from Bio.Alphabet import generic_dna sequences = ["AAACGTGG", "TGAACCG", "GGTGCA", "CCAATGCG"] records = (SeqRecord(Seq(seq, generic_dna), str(index)) for index,seq in enumerate(sequences)) with open("example.fasta", "w") as output_handle:

SeqIO.write( records , output_handle, "fasta")

This code will result in a FASTA file with sequence ids starting from 0. If you want to give a custom id and a description you can create the records as follows.

sequences = ["AAACGTGG", "TGAACCG", "GGTGCA", "CCAATGCG"]

new_sequences = [] i=1 for sequence in sequences :

record = SeqRecord( sequence , id="Seq_"+str(i), name="Seq_"+str(i), description="<custom description>")

new_sequences.append(record) with open("example.fasta", "w") as output_handle:

SeqIO.write( new_sequences , output_handle, "fasta")

The SeqIO.write() function will return the number of sequences written.

7. Convert a FASTQ file to FASTA file

We need to convert DNA data file formats in certain applications. For example, we can do file format conversions from FASTQ to FASTA as follows.

from Bio import SeqIO with open("path/to/fastq/file.fastq", "r") as input_handle, open("path/to/fasta/file.fasta", "w") as output_handle: sequences = SeqIO.parse(input_handle, "fastq")

count = SeqIO.write(sequences, output_handle, "fasta") print("Converted %i records" % count)

If you want to convert a GenBank file to FASTA format,

from Bio import SeqIO



with open(" path/to/genbank/file .gb", "rU") as input_handle, open(" path/to/fasta/file.fasta ", "w") as output_handle:



sequences = SeqIO.parse(input_handle, "genbank")

count = SeqIO.write(sequences, output_handle, "fasta")



print("Converted %i records" % count)

8. Separate sequences by ids from a list of ids

Assume that you have a list of sequence identifiers in a file named list.lst where you want to separate the corresponding sequences from a FASTA file. You can run the following and write those sequences to a file.

from Bio import SeqIO ids = set(x[:-1] for x in open(path+"list.lst")) with open(path+'list.fq', mode='a') as my_output:



for seq in SeqIO.parse(path+"list_sequences.fq", "fastq"):



if seq.id in ids:

my_output.write(seq.format("fastq"))

Final Thoughts

Hope you got an idea of how to use Seq , SeqRecord and SeqIO Biopython functions and will be useful for your research work.

Thank you for reading. I would love to hear your thoughts. Stay tuned for the next part of this article with more usages and Biopython functions.

Cheers, and stay safe!

Link to part 2 is as follows;