If you haven’t spent a lot of time programming in python, many questions and problems that come up in using Biopython are often related to python itself. This section tries to present some ideas and code that come up often (at least for us!) while using the Biopython libraries. If you have any suggestions for useful pointers that could go here, please contribute!
Handles are mentioned quite frequently throughout this documentation, and are also fairly confusing (at least to me!). Basically, you can think of a handle as being a “wrapper” around text information.
Handles provide (at least) two benefits over plain text information:
Handles can deal with text information that is being read (e. g. reading
from a file) or written (e. g. writing information to a file). In the
case of a “read” handle, commonly used functions are read()
,
which reads the entire text information from the handle, and
readline()
, which reads information one line at a time. For
“write” handles, the function write()
is regularly used.
The most common usage for handles is reading information from a file,
which is done using the built-in python function open
. Here, we open a
handle to the file m_cold.fasta
(also available online
here):
>>> handle = open("m_cold.fasta", "r") >>> handle.readline() ">gi|8332116|gb|BE037100.1|BE037100 MP14H09 MP Mesembryanthemum ...\n"
Handles are regularly used in Biopython for passing information to parsers.
One useful thing is to be able to turn information contained in a
string into a handle. The following example shows how to do this using
cStringIO
from the Python standard library:
>>> my_info = 'A string\n with multiple lines.' >>> print my_info A string with multiple lines. >>> import cStringIO >>> my_info_handle = cStringIO.StringIO(my_info) >>> first_line = my_info_handle.readline() >>> print first_line A string >>> second_line = my_info_handle.readline() >>> print second_line with multiple lines.