Help with BioPython manual example
3
0
Entering edit mode
9.8 years ago

I wish to index a multi-fasta file I have but I'm having problems getting the BioPython manual example to work.

From section 5.4.2 (pg 59). I have the required fasta file in my Python folder

The first few lines work just as they do in the manual:

>>> from Bio import SeqIO
>>> orchid_dict = SeqIO.index("ls_orchid.fasta", "fasta")
>>> len(orchid_dict)
94

But the next line does not. Here's what it returns

>>> orchid_dict.keys()
<dict_keyiterator object at 0x0000000002245F98>

Would someone be able to explain what might be going on and why the output isn't the same as in the manual?

BioPython • 3.2k views
ADD COMMENT
0
Entering edit mode

Thanks for reporting this, I have just updated the Biopython documentation which will be included with our next release: https://github.com/biopython/biopython/commit/f9b81d6d06a2259779404f1674ac0dde3747d52e

ADD REPLY
3
Entering edit mode
9.8 years ago
russhh 5.7k

You're using python3 and in the update from 2.7 to 3, dict.keys() now returns an key iterator rather than a list of keynames. The code examples you are copying were written in python2.7, so you may see a few differences.

Nonetheless, list(orchid_dict.keys()) should give you the same result as in the example

ADD COMMENT
0
Entering edit mode

Nonetheless, there are caveats to using list(dict.keys()): http://blog.labix.org/2008/06/27/watch-out-for-listdictkeys-in-python-3

ADD REPLY
0
Entering edit mode

Thanks! That did indeed give me output from the manual. Thanks to everyone else as well for helping.

ADD REPLY
0
Entering edit mode
ADD REPLY
1
Entering edit mode
9.8 years ago
matted 7.8k

keys() is giving you an iterator, not a list. I imagine this is a code change that's newer than the documentation you referenced, performed to make the library more efficient and aligned with Python 3 behavior (see here).

If you want to see the reference names in an interactive session, just type list(orchid_dict.keys()) and it will work as you expect. Otherwise, you can use the keys() iterator in your code in exactly the same way as you would before.

ADD COMMENT
1
Entering edit mode
9.8 years ago
lelle ▴ 830

Are you using Python 3? I think the behaviour of the keys function has changed from Python 2 to Python 3.

Try:

list(orchid_dict.keys())

to get the old behaviour.

ADD COMMENT

Login before adding your answer.

Traffic: 814 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6