Question: Simple Fasta Parsing Is Not Simple.
3
gravatar for Vlinxify
10.5 years ago by
Vlinxify120
Rotterdam, Netherlands
Vlinxify120 wrote:

When trying out the examples from chapter 2.3 of the biopython 1.54b tutorial I keep running into this very annoying problem: When I use:

from Bio import SeqIO

for seq_record in SeqIO.parse("ls_orchid.fasta", "fasta"):

    print seq_record.id
    print repr(seq_record.seq)
    print len(seq_record)

The Python interpreter tells me I should use a handle in stead of filenames. I use biopython 1.53 and not 1.54b for which this tutorial was meant. I can't find an older archived version of the tutorial I could use to help me learn how to use a handle in stead of the simple filename method (ls_orchid.fasta in this case.). I know that in older versions of biopython you have to do everything in handlers but not in the newer versions.

The new tutorial now on-line has an obscure last chapter on how to use handlers but that's hardly helpfull.

I think the tutorial is great, I just havent got the right version installed. I use Ubuntu and downloaded biopython via ubuntu software center.

Can anyone help me use a handler and get the above parsing example working in 1.53?

Thank you,

file biopython python fasta • 3.4k views
ADD COMMENTlink modified 2.1 years ago by RamRS30k • written 10.5 years ago by Vlinxify120
5
gravatar for Gurado
10.5 years ago by
Gurado280
Gurado280 wrote:

try the following

f is a filehandle that is associated with your file of interest

from Bio import SeqIO

f = open("ls_orchid.fasta")
for seq_record in SeqIO.parse(f, "fasta"):
    print seq_record.id
    print repr(seq_record.seq)
    print len(seq_record)
f.close()
ADD COMMENTlink modified 2.1 years ago by RamRS30k • written 10.5 years ago by Gurado280

It worked! So this is how handlers work. I remember somewhere I read that you always have to close the handler too, so I know why u use the last statement too. You wrap it and then you unwrap it.

Thanks Gurado :D

ADD REPLYlink written 10.5 years ago by Vlinxify120

Closing handles is particularly important for output, but good practice on input too. When the handle goes out of scope and gets garbage collected it will be closed automatically.

ADD REPLYlink written 10.5 years ago by Peter5.8k
5
gravatar for Eric Normandeau
10.5 years ago by
Quebec, Canada
Eric Normandeau10k wrote:

Hi, Just a very tiny addition to Gurado's answer, to make the thing more beautiful in Python.

from Bio import SeqIO

with open("ls_orchid.fasta") as f:
    for seq_record in SeqIO.parse(f, "fasta"):
        print seq_record.id
        print repr(seq_record.seq)
        print len(seq_record)

Using this form, you don't have to manually close the handler (or forget to do so!). The file connection is closed automatically when you get out of the 'with open()' portion. Cheers!

ADD COMMENTlink modified 2.1 years ago by RamRS30k • written 10.5 years ago by Eric Normandeau10k
1

The 'with' statement is elegant.. but, may be this is my pet peeve, I try to avoid using it -- simply because it adds one more level of indentation.

ADD REPLYlink written 10.5 years ago by Haibao Tang3.0k

I LOVE using the with object nowadays. I really wonder how I ever lived without it.

ADD REPLYlink written 10.5 years ago by Will4.5k

That won't work on Python 2.4 (which Biopython still supports for now)

ADD REPLYlink written 10.5 years ago by Peter5.8k
4
gravatar for Giovanni M Dall'Olio
10.5 years ago by
London, UK
Giovanni M Dall'Olio27k wrote:

The tutorial you are reading describes a feature that has been introduced in biopython 1.54.

With all the earlier versions, SeqIO required a file-like handler as the first argument, so you had to do something like SeqIO.parse(open("seq.fasta", "r"), "fasta") as explained in the other answers.

Starting from biopython 1.54, SeqIO.parse can accept either a file handler or a filename as argument, so SeqIO.parse("ls_orchid.fasta", "fasta") works correctly.

I'll notify the biopython's devs to add a note to this in the current tutorial, it may be useful to somebody else.

ADD COMMENTlink modified 2.1 years ago by RamRS30k • written 10.5 years ago by Giovanni M Dall'Olio27k

Nice clear answer Giovanni :)

I've just clarified the Tutorial's FAQ to point to the appendix on handles, where I've added a couple of examples.

ADD REPLYlink written 10.5 years ago by Peter5.8k

That one bit me too a few weeks ago I admit..

ADD REPLYlink written 10.5 years ago by Daniel Swan13k

You are welcome :-)

ADD REPLYlink written 10.5 years ago by Giovanni M Dall'Olio27k
2
gravatar for Peter
10.5 years ago by
Peter5.8k
Scotland, UK
Peter5.8k wrote:

Others have already shown some explicit examples of how to use a handle - and the tutorial has been updated to try and make this clearer for Biopython 1.54.

You also asked where to find older copies of the tutorial, they are included in the .zip or .tar.gz archives of Biopython on our downloads page. For Biopython 1.53 try:

http://biopython.org/DIST/biopython-1.53.zip

http://biopython.org/DIST/biopython-1.53.tar.gz

Hope that helps.

ADD COMMENTlink modified 8.5 years ago by Istvan Albert ♦♦ 85k • written 10.5 years ago by Peter5.8k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 984 users visited in the last hour