This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Sequence extaction using Biopython

Hi.

I am a new user of Biopython. I need to extract some sequences from a fasta file based on their coordinates. The file is composed of 10 chromosomes and I need to extract say sequence with coordinate such as chr3:101456..105689 .How do I do it. Thanks in advance!

genome

People, this might be an old post, but from Bio import SeqIO does not seem to work. is it depreciated?

Please use ADD COMMENT or ADD REPLY to answer to previous reactions, as such this thread remains logically structured and easy to follow. I have now moved your post but as you can see it's not optimal. Adding an answer should only be used for providing a solution to the question asked.

This probably would have been more appropriate as a separate question - since it's not the same as the initial question.

does not seem to work.

At the very minimum, provide the error message. We can't get in front of your pc and see what goes on. Biopython for sure isn't deprecated. But I just think you haven't installed biopython. Read the installation instructions.

2 answers

Read the fasta file:

from Bio import SeqIO

chrs = {}
for seq in SeqIO.parse("filename.fa", "fasta"):
    chrs[seq.id] = seq.seq

Extract:

chrs[chrname][from_pos:to_pos]

e.g., chrs['chr3'][101455:105689]

Be aware that the sequence is 0-based and the last coordinate is one after the end coordinate like everything in python

Thanks, the program does work but it is repeating itself. What shall I do. Here's what I wrote

from Bio import SeqIO
chrs={}
for seq in SeqIO.parse("brapav5.fa","fasta"):
    chrs[seq.id]=seq.seq
    print chrs['A01'][101455:105689]

You should read the sequences only once and then retrieve as many times as you want.

Log in to answer this question.